Skip to content

Commit

Permalink
chore: add code type info
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Sep 18, 2020
1 parent 783bce4 commit 59449e7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/bases/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class ComposedDecoder {
/** @type {Object<string, UnibaseDecoder<Prefix>>} */
this.decoders = decoders
// so that we can distinguish between unibase and multibase
/** @type {void} */
/** @type {null} */
this.prefix = null
}

Expand Down
52 changes: 31 additions & 21 deletions src/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import CID from './cid.js'

/**
* @template {number} Code
* @template T
* @class
*/
export default class Block {
/**
* @param {CID|null} cid
* @param {number} code
* @param {Code} code
* @param {T} data
* @param {Uint8Array} bytes
* @param {BlockConfig} config
Expand Down Expand Up @@ -56,30 +57,33 @@ export default class Block {
}

/**
* @template {number} Code
* @template T
* @param {Encoder<T>} codec
* @param {Encoder<Code, T>} codec
* @param {BlockConfig} options
*/
static encoder (codec, options) {
return new BlockEncoder(codec, options)
}

/**
* @template {number} Code
* @template T
* @param {Decoder<T>} codec
* @param {Decoder<Code, T>} codec
* @param {BlockConfig} options
*/
static decoder (codec, options) {
return new BlockDecoder(codec, options)
}

/**
* @template {number} Code
* @template T
* @param {Object} codec
* @param {Encoder<T>} codec.encoder
* @param {Decoder<T>} codec.decoder
* @param {Object} [options]
* @returns {BlockCodec<T>}
* @param {Encoder<Code, T>} codec.encoder
* @param {Decoder<Code, T>} codec.decoder
* @param {BlockConfig} options
* @returns {BlockCodec<Code, T>}
*/

static codec ({ encoder, decoder }, options) {
Expand Down Expand Up @@ -178,12 +182,13 @@ const createCID = async (hasher, bytes, code) => {
}

/**
* @template {number} Code
* @template T
*/
class BlockCodec {
/**
* @param {Encoder<T>} encoder
* @param {Decoder<T>} decoder
* @param {Encoder<Code, T>} encoder
* @param {Decoder<Code, T>} decoder
* @param {BlockConfig} config
*/

Expand All @@ -195,17 +200,17 @@ class BlockCodec {

/**
* @param {Uint8Array} bytes
* @param {BlockConfig} [options]
* @returns {Block<T>}
* @param {Partial<BlockConfig>} [options]
* @returns {Block<Code, T>}
*/
decode (bytes, options) {
return this.decoder.decode(bytes, { ...this.config, ...options })
}

/**
* @param {T} data
* @param {BlockConfig} [options]
* @returns {Block<T>}
* @param {Partial<BlockConfig>} [options]
* @returns {Block<Code, T>}
*/
encode (data, options) {
return this.encoder.encode(data, { ...this.config, ...options })
Expand All @@ -214,11 +219,12 @@ class BlockCodec {

/**
* @class
* @template {number} Code
* @template T
*/
class BlockEncoder {
/**
* @param {Encoder<T>} codec
* @param {Encoder<Code, T>} codec
* @param {BlockConfig} config
*/
constructor (codec, config) {
Expand All @@ -228,8 +234,8 @@ class BlockEncoder {

/**
* @param {T} data
* @param {BlockConfig} [options]
* @returns {Block}
* @param {Partial<BlockConfig>} [options]
* @returns {Block<Code, T>}
*/
encode (data, options) {
const { codec } = this
Expand All @@ -240,11 +246,12 @@ class BlockEncoder {

/**
* @class
* @template {number} Code
* @template T
*/
class BlockDecoder {
/**
* @param {Decoder<T>} codec
* @param {Decoder<Code, T>} codec
* @param {BlockConfig} config
*/
constructor (codec, config) {
Expand All @@ -255,7 +262,7 @@ class BlockDecoder {
/**
* @param {Uint8Array} bytes
* @param {Partial<BlockConfig>} [options]
* @returns {Block}
* @returns {Block<Code, T>}
*/
decode (bytes, options) {
const data = this.codec.decode(bytes)
Expand Down Expand Up @@ -283,16 +290,19 @@ class BlockDecoder {
*/

/**
* @template {number} Code
* @template T
* @typedef {import('./codecs/interface').BlockEncoder<T>} Encoder
* @typedef {import('./codecs/interface').BlockEncoder<Code, T>} Encoder
*/

/**
* @template {number} Code
* @template T
* @typedef {import('./codecs/interface').BlockDecoder} Decoder
* @typedef {import('./codecs/interface').BlockDecoder<Code, T>} Decoder
*/

/**
* @template {number} Code
* @template T
* @typedef {import('./codecs/interface').BlockCodec} Codec
* @typedef {import('./codecs/interface').BlockCodec<Code, T>} Codec
*/
25 changes: 16 additions & 9 deletions src/codecs/codec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ export const codec = ({ name, code, decode, encode }) =>
new Codec(name, code, encode, decode)

/**
* @template {number} Code
* @template T
* @typedef {import('./interface').BlockEncoder<T>} BlockEncoder
* @typedef {import('./interface').BlockEncoder<Code, T>} BlockEncoder
*/

/**
* @class
* @template T
* @template {string} Name
* @template {number} Code
* @implements {BlockEncoder<T>}
* @implements {BlockEncoder<Code, T>}
*/
export class Encoder {
/**
Expand All @@ -40,36 +41,42 @@ export class Encoder {
}

/**
* @template {number} Code
* @template T
* @typedef {import('./interface').BlockDecoder<T>} BlockDecoder
* @typedef {import('./interface').BlockDecoder<Code, T>} BlockDecoder
*/

/**
* @class
* @template {number} Code
* @template T
* @implements {BlockDecoder<T>}
* @implements {BlockDecoder<Code, T>}
*/
export class Decoder {
/**
* @param {string} name
* @param {Code} code
* @param {(bytes:Uint8Array) => T} decode
*/
constructor (code, decode) {
constructor (name, code, decode) {
this.name = name
this.code = code
this.decode = decode
}
}

/**
* @template {number} Code
* @template T
* @typedef {import('./interface').BlockCodec<T>} BlockCodec
* @typedef {import('./interface').BlockCodec<Code, T>} BlockCodec
*/

/**
* @class
* @template {string} Name
* @template {number} Code
* @template T
* @implements {BlockCodec<T>}
* @implements {BlockCodec<Code, T>}
*/
export class Codec {
/**
Expand All @@ -86,8 +93,8 @@ export class Codec {
}

get decoder () {
const { name, decode } = this
const decoder = new Decoder(name, decode)
const { name, code, decode } = this
const decoder = new Decoder(name, code, decode)
Object.defineProperty(this, 'decoder', { value: decoder })
return decoder
}
Expand Down
3 changes: 2 additions & 1 deletion src/legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const legacy = (codec, { hashes }) => {
}

if (bytes.isBinary(obj)) {
// @ts-ignore
return Buffer.from(obj)
}

Expand Down Expand Up @@ -93,7 +94,7 @@ const legacy = (codec, { hashes }) => {
let value = codec.decode(buff)
const entries = path.split('/').filter(x => x)
while (entries.length) {
value = value[entries.shift()]
value = value[/** @type {string} */(entries.shift())]
if (typeof value === 'undefined') throw new Error('Not found')
if (OldCID.isCID(value)) {
return { value, remainderPath: entries.join('/') }
Expand Down

0 comments on commit 59449e7

Please sign in to comment.