-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!: simplify the codec interface, remove helper methods & classes
BREAKING CHANGE: * Removes the codecs/codec export - there is no longer a helper function to building codecs, they should instead assert that they conform to the `BlockCodec` type, see json and raw examples. The `codec()` helper and `Encoder` and `Decoder` classes have been removed. Use type assertions to conform to simple signatures for now. In the future we may introduce more composable codec functionality which may require codecs to lean on additional helpers and classes. * Fix bases/base64 type export to be usable
- Loading branch information
Showing
11 changed files
with
68 additions
and
175 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
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
// @ts-check | ||
|
||
import { codec } from './codec.js' | ||
/** | ||
* @template {number} Code | ||
* @template T | ||
* @typedef {import('./interface').BlockCodec<Code, T>} BlockCodec | ||
*/ | ||
|
||
export const { name, code, decode, encode, decoder, encoder } = codec({ | ||
/** | ||
* @template T | ||
* @type {BlockCodec<0x0200, T>} | ||
*/ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,26 @@ | ||
// @ts-check | ||
|
||
import { coerce } from '../bytes.js' | ||
import { codec } from './codec.js' | ||
|
||
export const { name, code, decode, encode, decoder, encoder } = codec({ | ||
/** | ||
* @template {number} Code | ||
* @template T | ||
* @typedef {import('./interface').BlockCodec<Code, T>} BlockCodec | ||
*/ | ||
|
||
/** | ||
* @param {Uint8Array} bytes | ||
* @returns {Uint8Array} | ||
*/ | ||
const raw = (bytes) => coerce(bytes) | ||
|
||
/** | ||
* @template T | ||
* @type {BlockCodec<0x55, Uint8Array>} | ||
*/ | ||
export const { name, code, encode, decode } = { | ||
name: 'raw', | ||
code: 85, | ||
decode: coerce, | ||
encode: coerce | ||
}) | ||
code: 0x55, | ||
decode: raw, | ||
encode: raw | ||
} |
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
Oops, something went wrong.