Skip to content

Commit

Permalink
fixup! fix: more generic BlockCodec, remove encoder & decoder props
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Apr 10, 2021
1 parent 7cbce18 commit 2ea0393
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/codecs/codec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
/**
* @template {number} Code
* @template T
* @typedef {import('./interface').BlockCodec<Code, T>} BlockCodec
* @typedef {import('./interface').CodecFeature<Code, T>} CodecFeature
*/

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

/**
* @template {number} Code
* @template T
Expand All @@ -25,7 +27,7 @@
* @param {Code} options.code
* @param {(data:T) => Uint8Array} options.encode
* @param {(bytes:Uint8Array) => T} options.decode
* @returns {BlockCodec<Code, T> & { encoder: BlockEncoder<Code, T>, decoder: BlockDecoder<Code, T> }}
* @returns {CodecFeature<Code, T>}
*/
export const codec = ({ name, code, decode, encode }) => {
const decoder = new Decoder(name, code, decode)
Expand Down
9 changes: 9 additions & 0 deletions src/codecs/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ export interface BlockDecoder<Code extends number, T> {
*/
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
Expand Down
10 changes: 10 additions & 0 deletions src/codecs/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

import { codec } from './codec.js'

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

/**
* @template T
* @type {CodecFeature<0x0200, T>}
*/
export const { name, code, decode, encode, decoder, encoder } = codec({
name: 'json',
code: 0x0200,
Expand Down
22 changes: 19 additions & 3 deletions src/codecs/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,25 @@
import { coerce } from '../bytes.js'
import { codec } from './codec.js'

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

/**
* @param {Uint8Array} bytes
* @returns {Uint8Array}
*/
const raw = (bytes) => coerce(bytes)

/**
* @template T
* @type {CodecFeature<0x55, Uint8Array>}
*/
export const { name, code, decode, encode, decoder, encoder } = codec({
name: 'raw',
code: 85,
decode: coerce,
encode: coerce
code: 0x55,
decode: raw,
encode: raw
})

0 comments on commit 2ea0393

Please sign in to comment.