Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement link interface & module #197

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
553bdee
feat: define sync multihash hasher interface
Gozala Jan 19, 2022
b677a65
chore: enable composite code path
Gozala Jan 19, 2022
655028b
feat: define CID as interface
Gozala Jan 19, 2022
5a976d0
Merge remote-tracking branch 'origin/master' into feat/cid-interface
Gozala Jan 19, 2022
9947c49
chore: add missing type params
Gozala Jan 19, 2022
3f2fafa
chore: remove unecessary exports
Gozala Jan 19, 2022
e841c28
chore: add clarifying comments
Gozala Jan 19, 2022
a306d52
fix: type errors
Gozala Jan 19, 2022
6da6273
chore: cleanup block module
Gozala Jan 19, 2022
88182b6
chore: remove uintented changes
Gozala Jan 19, 2022
99fcb43
chore: rename Algorithm to Alg
Gozala Jan 19, 2022
f624c80
chore: undo unintended change
Gozala Jan 19, 2022
aa6a478
chore: fix import
Gozala Jan 19, 2022
3c835a0
fix: revert CID class
Gozala Mar 30, 2022
6c92773
fix: add test to improve coverage
Gozala Mar 30, 2022
6e8b914
chore: merge master
Gozala Mar 30, 2022
680bd77
fix: add missing files
Gozala Mar 31, 2022
22d0fc0
fix: type errors
Gozala Mar 31, 2022
f21a1ba
Apply suggestions from code review
Gozala Mar 31, 2022
91f780e
merge upsteam changes
Gozala Jun 10, 2022
ec5e3d9
retain CID type info in Block impl
Gozala Jun 10, 2022
6023831
fix: type errors
Gozala Jun 10, 2022
7b36f00
try to fix typecheck
Gozala Jun 14, 2022
4c18aca
feat: add link module & interface
Gozala Aug 26, 2022
ec4827c
fix: remaining failures
Gozala Aug 26, 2022
c41785d
fix: type error
Gozala Aug 27, 2022
b3ef77a
Update src/block.js
Gozala Aug 29, 2022
1c620f5
Apply suggestions from code review
Gozala Aug 29, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -98,6 +101,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 @@ -107,7 +84,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 @@ -117,30 +94,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 @@ -165,30 +137,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