diff --git a/package.json b/package.json index 33f223f..1811570 100644 --- a/package.json +++ b/package.json @@ -6,17 +6,18 @@ "types": "./types/index.d.ts", "type": "module", "scripts": { - "lint": "standard", + "lint": "standard *.js test/*.js", "build": "npm run build:js && npm run build:types", "build:js": "npm_config_yes=true ipjs build --tests --main && npm run build:copy", - "build:copy": "cp -a tsconfig.json index.js test/ dist/", + "build:copy": "cp -a tsconfig.json index.js dist/ && mkdir -p dist/test && cp test/*.js dist/test/", "build:types": "npm run build:copy && cd dist && tsc --build", "check": "tsc --build --noErrorTruncation", "publish": "npm_config_yes=true ipjs publish", - "test:cjs": "npm run build && mocha dist/cjs/node-test/test-*.js", + "test:cjs": "npm run build && mocha dist/cjs/node-test/test-*.js && npm run test:cjs:browser", "test:node": "hundreds mocha test/test-*.js", - "test:browser": "polendina --cleanup dist/cjs/node-test/test-*.js", - "test": "npm run lint && npm run test:node && npm run test:cjs && npm run test:browser", + "test:cjs:browser": "polendina --cleanup dist/cjs/node-test/test-*.js", + "test:ts": "npm run build:types && npm run test --prefix test/ts-use", + "test": "npm run lint && npm run test:node && npm run test:cjs && npm run test:ts", "coverage": "c8 --reporter=html mocha test/test-*.js && npx st -d coverage -p 8080" }, "exports": { diff --git a/test/ts-use/.gitignore b/test/ts-use/.gitignore new file mode 100644 index 0000000..6881777 --- /dev/null +++ b/test/ts-use/.gitignore @@ -0,0 +1,3 @@ +node_modules +src/main.js +tsconfig.tsbuildinfo diff --git a/test/ts-use/package.json b/test/ts-use/package.json new file mode 100644 index 0000000..7456b05 --- /dev/null +++ b/test/ts-use/package.json @@ -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" + } +} diff --git a/test/ts-use/src/main.ts b/test/ts-use/src/main.ts new file mode 100644 index 0000000..6cf48cf --- /dev/null +++ b/test/ts-use/src/main.ts @@ -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 (encoder: BlockEncoder) { + 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 (decoder: BlockDecoder) { + deepStrictEqual(decoder.code, 0x71) + deepStrictEqual(decoder.decode(Uint8Array.from([100, 98, 108, 105, 112])), 'blip') + console.log('[TS] ✓ { decoder: BlockDecoder }') +} + +function useBlockCodec (blockCodec: BlockCodec) { + 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 diff --git a/test/ts-use/tsconfig.json b/test/ts-use/tsconfig.json new file mode 100644 index 0000000..ff14759 --- /dev/null +++ b/test/ts-use/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "strict": true, + "moduleResolution": "node", + "noImplicitAny": true, + "skipLibCheck": true, + "incremental": true + } +}