-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! fix: more generic BlockCodec, remove encoder & decoder props
- Loading branch information
Showing
8 changed files
with
46 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,5 @@ | ||
// @ts-check | ||
|
||
/** | ||
* @template {number} Code | ||
* @template T | ||
* @typedef {import('./interface').CodecFeature<Code, T>} CodecFeature | ||
*/ | ||
|
||
/** | ||
* @template {number} Code | ||
* @template T | ||
|
@@ -19,33 +13,20 @@ | |
*/ | ||
|
||
/** | ||
* @template {string} Name | ||
* @template {number} Code | ||
* @template T | ||
* @param {Object} options | ||
* @param {Name} options.name | ||
* @param {Code} options.code | ||
* @param {(data:T) => Uint8Array} options.encode | ||
* @param {(bytes:Uint8Array) => T} options.decode | ||
* @returns {CodecFeature<Code, T>} | ||
* @typedef {import('./interface').BlockCodec<Code, T>} BlockCodec | ||
*/ | ||
export const codec = ({ name, code, decode, encode }) => { | ||
const decoder = new Decoder(name, code, decode) | ||
const encoder = new Encoder(name, code, encode) | ||
|
||
return { name, code, decode, encode, decoder, encoder } | ||
} | ||
|
||
/** | ||
* @class | ||
* @template T | ||
* @template {string} Name | ||
* @template {number} Code | ||
* @template T | ||
* @implements {BlockEncoder<Code, T>} | ||
*/ | ||
export class Encoder { | ||
/** | ||
* @param {Name} name | ||
* @param {string} name | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
rvagg
Author
Member
|
||
* @param {Code} code | ||
* @param {(data:T) => Uint8Array} encode | ||
*/ | ||
|
@@ -54,6 +35,17 @@ export class Encoder { | |
this.code = code | ||
this.encode = encode | ||
} | ||
|
||
/** | ||
* @template {number} Code | ||
* @template T | ||
* @param {BlockCodec<Code, T>} codec | ||
* @returns {BlockEncoder<Code, T>} | ||
*/ | ||
static fromCodec (codec) { | ||
const { name, code, encode } = codec | ||
return new Encoder(name, code, encode) | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -73,4 +65,15 @@ export class Decoder { | |
this.code = code | ||
this.decode = decode | ||
} | ||
|
||
/** | ||
* @template {number} Code | ||
* @template T | ||
* @param {BlockCodec<Code, T>} codec | ||
* @returns {BlockDecoder<Code, T>} | ||
*/ | ||
static fromCodec (codec) { | ||
const { name, code, decode } = codec | ||
return new Decoder(name, code, decode) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,27 +11,16 @@ export interface BlockEncoder<Code extends number, T> { | |
* IPLD decoder part of the codec. | ||
*/ | ||
export interface BlockDecoder<Code extends number, T> { | ||
name: string | ||
This comment has been minimized.
Sorry, something went wrong.
Gozala
Contributor
|
||
code: Code | ||
decode(bytes: ByteView<T>): T | ||
} | ||
|
||
/** | ||
* IPLD codec that is just Encoder + Decoder however it is | ||
* separate those capabilties as sender requires encoder and receiver | ||
* requires decoder. | ||
* An IPLD codec is a combination of both encoder and decoder. | ||
*/ | ||
export interface BlockCodec<Code extends number, T> extends BlockEncoder<Code, T>, BlockDecoder<Code, T> {} | ||
|
||
/** | ||
* A roll-up type from which you can derive any of `BlockCodec`, | ||
* `BlockEncoder` or `BlockDecoder`. Intended as the interface exported | ||
* by codec implementations. | ||
*/ | ||
export interface CodecFeature<Code extends number, T> extends BlockCodec<Code, T> { | ||
encoder: BlockEncoder<Code, T>, | ||
decoder: BlockDecoder<Code, T> | ||
} | ||
|
||
// This just a hack to retain type information about the data that | ||
// is encoded `T` Because it's a union `data` field is never going | ||
// to be usable anyway. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,18 @@ | ||
// @ts-check | ||
|
||
import { codec } from './codec.js' | ||
|
||
/** | ||
* @template {number} Code | ||
* @template T | ||
* @typedef {import('./interface').CodecFeature<Code, T>} CodecFeature | ||
* @typedef {import('./interface').BlockCodec<Code, T>} BlockCodec | ||
*/ | ||
|
||
/** | ||
* @template T | ||
* @type {CodecFeature<0x0200, T>} | ||
* @type {BlockCodec<0x0200, T>} | ||
*/ | ||
export const { name, code, decode, encode, decoder, encoder } = codec({ | ||
export const { name, code, encode, decode } = { | ||
name: 'json', | ||
code: 0x0200, | ||
encode: json => new TextEncoder().encode(JSON.stringify(json)), | ||
decode: bytes => JSON.parse(new TextDecoder().decode(bytes)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The reason name used generic type as opposed to string is because that leads TS to do a precise inference
As per image above when generic is used TS retains actual literal in the type and subsequently generates better docs, does more useful tooltips in vscode etc..
Link to the live version of that screenshot