-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
80 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
src/main.js | ||
tsconfig.tsbuildinfo |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "ts-use", | ||
"private": true, | ||
"dependencies": { | ||
"@ipld/dag-cbor": "file:../../dist/", | ||
"multiformats": "file:../../node_modules/multiformats" | ||
}, | ||
"scripts": { | ||
"test": "npm install && npx -p typescript tsc && node src/main.js" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { deepStrictEqual } from 'assert' | ||
|
||
import { BlockEncoder, BlockDecoder, BlockCodec, CodecFeature } from 'multiformats/codecs/interface' | ||
import * as dagCbor from '@ipld/dag-cbor' | ||
|
||
const main = () => { | ||
// make sure we have a full CodecFeature | ||
useCodecFeature(dagCbor) | ||
} | ||
|
||
function useCodecFeature (codec: CodecFeature<0x71, any>) { | ||
// use only as a BlockEncoder | ||
const { encoder } = codec | ||
useEncoder(encoder) | ||
|
||
// use only as a BlockDecoder | ||
const { decoder } = codec | ||
useDecoder(decoder) | ||
|
||
// use as a full BlockCodec which does both BlockEncoder & BlockDecoder | ||
useBlockCodec(codec) | ||
|
||
// use BlockEncoder & BlockDecoder as a full BlockCodec | ||
// (demonstrates and tests BlockEncoder & BlockDecoder composability) | ||
useBlockCodec(Object.assign({}, encoder, decoder)) | ||
} | ||
|
||
function useEncoder<Codec extends number> (encoder: BlockEncoder<Codec, string>) { | ||
deepStrictEqual(encoder.code, 0x71) | ||
deepStrictEqual(encoder.name, 'dag-cbor') | ||
deepStrictEqual(Array.from(encoder.encode('blip')), [100, 98, 108, 105, 112]) | ||
console.log('[TS] ✓ { encoder: BlockEncoder }') | ||
} | ||
|
||
function useDecoder<Codec extends number> (decoder: BlockDecoder<Codec, Uint8Array>) { | ||
deepStrictEqual(decoder.code, 0x71) | ||
deepStrictEqual(decoder.decode(Uint8Array.from([100, 98, 108, 105, 112])), 'blip') | ||
console.log('[TS] ✓ { decoder: BlockDecoder }') | ||
} | ||
|
||
function useBlockCodec<Codec extends number> (blockCodec: BlockCodec<Codec, string>) { | ||
deepStrictEqual(blockCodec.code, 0x71) | ||
deepStrictEqual(blockCodec.name, 'dag-cbor') | ||
deepStrictEqual(Array.from(blockCodec.encode('blip')), [100, 98, 108, 105, 112]) | ||
deepStrictEqual(blockCodec.decode(Uint8Array.from([100, 98, 108, 105, 112])), 'blip') | ||
console.log('[TS] ✓ {}:BlockCodec') | ||
} | ||
|
||
main() | ||
|
||
export default main |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true, | ||
"moduleResolution": "node", | ||
"noImplicitAny": true, | ||
"skipLibCheck": true, | ||
"incremental": true | ||
} | ||
} |