Skip to content

Commit

Permalink
feat: define Link interface for CID
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala authored and rvagg committed Oct 12, 2022
1 parent a9a9347 commit 88e29ea
Show file tree
Hide file tree
Showing 34 changed files with 1,041 additions and 437 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/typecheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
- name: Typecheck
uses: gozala/typescript-error-reporter-action@v1.0.8
with:
project: test/tsconfig.json
project: tsconfig.json
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
"./cid": {
"import": "./src/cid.js"
},
"./link": {
"import": "./src/link.js"
},
"./basics": {
"import": "./src/basics.js"
},
Expand Down Expand Up @@ -101,6 +104,12 @@
},
"./codecs/raw": {
"import": "./src/codecs/raw.js"
},
"./interface": {
"import": "./src/interface.js"
},
"./bytes": {
"import": "./src/bytes.js"
}
},
"devDependencies": {
Expand Down
83 changes: 25 additions & 58 deletions src/bases/base.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
import basex from '../../vendor/base-x.js'
import { coerce } from '../bytes.js'

/**
* @typedef {import('./interface').BaseEncoder} BaseEncoder
* @typedef {import('./interface').BaseDecoder} BaseDecoder
* @typedef {import('./interface').BaseCodec} BaseCodec
*/

/**
* @template {string} T
* @typedef {import('./interface').Multibase<T>} Multibase
*/
/**
* @template {string} T
* @typedef {import('./interface').MultibaseEncoder<T>} MultibaseEncoder
*/
// Linter can't see that API is used in types.
// eslint-disable-next-line
import * as API from './interface.js'

/**
* Class represents both BaseEncoder and MultibaseEncoder meaning it
Expand All @@ -23,8 +11,8 @@ import { coerce } from '../bytes.js'
* @class
* @template {string} Base
* @template {string} Prefix
* @implements {MultibaseEncoder<Prefix>}
* @implements {BaseEncoder}
* @implements {API.MultibaseEncoder<Prefix>}
* @implements {API.BaseEncoder}
*/
class Encoder {
/**
Expand All @@ -40,7 +28,7 @@ class Encoder {

/**
* @param {Uint8Array} bytes
* @returns {Multibase<Prefix>}
* @returns {API.Multibase<Prefix>}
*/
encode (bytes) {
if (bytes instanceof Uint8Array) {
Expand All @@ -51,29 +39,18 @@ class Encoder {
}
}

/**
* @template {string} Prefix
* @typedef {import('./interface').MultibaseDecoder<Prefix>} MultibaseDecoder
*/

/**
* @template {string} Prefix
* @typedef {import('./interface').UnibaseDecoder<Prefix>} UnibaseDecoder
*/

/**
* @template {string} Prefix
*/
/**
* Class represents both BaseDecoder and MultibaseDecoder so it could be used
* to decode multibases (with matching prefix) or just base decode strings
* with corresponding base encoding.
* @class
* @template {string} Base
* @template {string} Prefix
* @implements {MultibaseDecoder<Prefix>}
* @implements {UnibaseDecoder<Prefix>}
* @implements {BaseDecoder}
* @implements {API.MultibaseDecoder<Prefix>}
* @implements {API.UnibaseDecoder<Prefix>}
* @implements {API.BaseDecoder}
*/
class Decoder {
/**
Expand Down Expand Up @@ -109,7 +86,7 @@ class Decoder {

/**
* @template {string} OtherPrefix
* @param {UnibaseDecoder<OtherPrefix>|ComposedDecoder<OtherPrefix>} decoder
* @param {API.UnibaseDecoder<OtherPrefix>|ComposedDecoder<OtherPrefix>} decoder
* @returns {ComposedDecoder<Prefix|OtherPrefix>}
*/
or (decoder) {
Expand All @@ -119,30 +96,25 @@ class Decoder {

/**
* @template {string} Prefix
* @typedef {import('./interface').CombobaseDecoder<Prefix>} CombobaseDecoder
*/

/**
* @template {string} Prefix
* @typedef {Record<Prefix, UnibaseDecoder<Prefix>>} Decoders
* @typedef {Record<Prefix, API.UnibaseDecoder<Prefix>>} Decoders
*/

/**
* @template {string} Prefix
* @implements {MultibaseDecoder<Prefix>}
* @implements {CombobaseDecoder<Prefix>}
* @implements {API.MultibaseDecoder<Prefix>}
* @implements {API.CombobaseDecoder<Prefix>}
*/
class ComposedDecoder {
/**
* @param {Record<Prefix, UnibaseDecoder<Prefix>>} decoders
* @param {Decoders<Prefix>} decoders
*/
constructor (decoders) {
this.decoders = decoders
}

/**
* @template {string} OtherPrefix
* @param {UnibaseDecoder<OtherPrefix>|ComposedDecoder<OtherPrefix>} decoder
* @param {API.UnibaseDecoder<OtherPrefix>|ComposedDecoder<OtherPrefix>} decoder
* @returns {ComposedDecoder<Prefix|OtherPrefix>}
*/
or (decoder) {
Expand All @@ -167,30 +139,25 @@ class ComposedDecoder {
/**
* @template {string} L
* @template {string} R
* @param {UnibaseDecoder<L>|CombobaseDecoder<L>} left
* @param {UnibaseDecoder<R>|CombobaseDecoder<R>} right
* @param {API.UnibaseDecoder<L>|API.CombobaseDecoder<L>} left
* @param {API.UnibaseDecoder<R>|API.CombobaseDecoder<R>} right
* @returns {ComposedDecoder<L|R>}
*/
export const or = (left, right) => new ComposedDecoder(/** @type {Decoders<L|R>} */({
...(left.decoders || { [/** @type UnibaseDecoder<L> */(left).prefix]: left }),
...(right.decoders || { [/** @type UnibaseDecoder<R> */(right).prefix]: right })
...(left.decoders || { [/** @type API.UnibaseDecoder<L> */(left).prefix]: left }),
...(right.decoders || { [/** @type API.UnibaseDecoder<R> */(right).prefix]: right })
}))

/**
* @template T
* @typedef {import('./interface').MultibaseCodec<T>} MultibaseCodec
*/

/**
* @class
* @template {string} Base
* @template {string} Prefix
* @implements {MultibaseCodec<Prefix>}
* @implements {MultibaseEncoder<Prefix>}
* @implements {MultibaseDecoder<Prefix>}
* @implements {BaseCodec}
* @implements {BaseEncoder}
* @implements {BaseDecoder}
* @implements {API.MultibaseCodec<Prefix>}
* @implements {API.MultibaseEncoder<Prefix>}
* @implements {API.MultibaseDecoder<Prefix>}
* @implements {API.BaseCodec}
* @implements {API.BaseEncoder}
* @implements {API.BaseDecoder}
*/
export class Codec {
/**
Expand Down
1 change: 1 addition & 0 deletions src/bases/interface.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// this is dummy module overlayed by interface.ts
1 change: 1 addition & 0 deletions src/bases/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,4 @@ export interface UnibaseDecoder<Prefix extends string> extends MultibaseDecoder<
export interface CombobaseDecoder<Prefix extends string> extends MultibaseDecoder<Prefix> {
readonly decoders: Record<Prefix, UnibaseDecoder<Prefix>>
}

Loading

0 comments on commit 88e29ea

Please sign in to comment.