@fireproof/vendor 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +97 -4
- package/src/@ipld/car/LICENSE +4 -0
- package/src/@ipld/car/README.md +1132 -0
- package/src/@ipld/car/dist/index.min.js +5 -0
- package/src/@ipld/car/dist/src/api.d.ts +72 -0
- package/src/@ipld/car/dist/src/api.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/buffer-decoder.d.ts +67 -0
- package/src/@ipld/car/dist/src/buffer-decoder.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/buffer-reader-browser.d.ts +116 -0
- package/src/@ipld/car/dist/src/buffer-reader-browser.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/buffer-reader.d.ts +31 -0
- package/src/@ipld/car/dist/src/buffer-reader.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/buffer-writer.d.ts +86 -0
- package/src/@ipld/car/dist/src/buffer-writer.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/coding.d.ts +49 -0
- package/src/@ipld/car/dist/src/coding.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/decoder-common.d.ts +43 -0
- package/src/@ipld/car/dist/src/decoder-common.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/decoder.d.ts +87 -0
- package/src/@ipld/car/dist/src/decoder.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/encoder.d.ts +17 -0
- package/src/@ipld/car/dist/src/encoder.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/header-validator.d.ts +5 -0
- package/src/@ipld/car/dist/src/header-validator.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/index-browser.d.ts +10 -0
- package/src/@ipld/car/dist/src/index-browser.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/index.d.ts +11 -0
- package/src/@ipld/car/dist/src/index.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/indexed-reader-browser.d.ts +5 -0
- package/src/@ipld/car/dist/src/indexed-reader-browser.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/indexed-reader.d.ts +152 -0
- package/src/@ipld/car/dist/src/indexed-reader.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/indexer.d.ts +95 -0
- package/src/@ipld/car/dist/src/indexer.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/iterator-channel.d.ts +7 -0
- package/src/@ipld/car/dist/src/iterator-channel.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/iterator.d.ts +174 -0
- package/src/@ipld/car/dist/src/iterator.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/promise-fs-opts.d.ts +28 -0
- package/src/@ipld/car/dist/src/promise-fs-opts.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/reader-browser.d.ts +151 -0
- package/src/@ipld/car/dist/src/reader-browser.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/reader.d.ts +40 -0
- package/src/@ipld/car/dist/src/reader.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/writer-browser.d.ts +155 -0
- package/src/@ipld/car/dist/src/writer-browser.d.ts.map +1 -0
- package/src/@ipld/car/dist/src/writer.d.ts +45 -0
- package/src/@ipld/car/dist/src/writer.d.ts.map +1 -0
- package/src/@ipld/car/src/api.ts +90 -0
- package/src/@ipld/car/src/buffer-decoder.js +213 -0
- package/src/@ipld/car/src/buffer-reader-browser.js +144 -0
- package/src/@ipld/car/src/buffer-reader.js +51 -0
- package/src/@ipld/car/src/buffer-writer.js +286 -0
- package/src/@ipld/car/src/coding.ts +68 -0
- package/src/@ipld/car/src/decoder-common.js +82 -0
- package/src/@ipld/car/src/decoder.js +347 -0
- package/src/@ipld/car/src/encoder.js +76 -0
- package/src/@ipld/car/src/header-validator.js +214 -0
- package/src/@ipld/car/src/header.ipldsch +18 -0
- package/src/@ipld/car/src/index-browser.js +18 -0
- package/src/@ipld/car/src/index.js +21 -0
- package/src/@ipld/car/src/indexed-reader-browser.js +7 -0
- package/src/@ipld/car/src/indexed-reader.js +211 -0
- package/src/@ipld/car/src/indexer.js +130 -0
- package/src/@ipld/car/src/iterator-channel.js +91 -0
- package/src/@ipld/car/src/iterator.js +267 -0
- package/src/@ipld/car/src/promise-fs-opts.js +46 -0
- package/src/@ipld/car/src/reader-browser.js +194 -0
- package/src/@ipld/car/src/reader.js +54 -0
- package/src/@ipld/car/src/writer-browser.js +250 -0
- package/src/@ipld/car/src/writer.js +81 -0
- package/src/@ipld/dag-cbor/LICENSE +4 -0
- package/src/@ipld/dag-cbor/README.md +80 -0
- package/src/@ipld/dag-cbor/dist/index.min.js +3 -0
- package/src/@ipld/dag-cbor/dist/src/index.d.ts +67 -0
- package/src/@ipld/dag-cbor/dist/src/index.d.ts.map +1 -0
- package/src/@ipld/dag-cbor/src/index.js +147 -0
- package/src/@ipld/dag-json/LICENSE +4 -0
- package/src/@ipld/dag-json/README.md +58 -0
- package/src/@ipld/dag-json/dist/index.min.js +3 -0
- package/src/@ipld/dag-json/dist/src/index.d.ts +12 -0
- package/src/@ipld/dag-json/dist/src/index.d.ts.map +1 -0
- package/src/@ipld/dag-json/src/index.js +291 -0
- package/src/@web3-storage/pail/dist/src/batch/index.d.ts +53 -1
- package/src/@web3-storage/pail/dist/src/batch/index.d.ts.map +1 -1
- package/src/@web3-storage/pail/dist/src/batch/shard.d.ts +1 -1
- package/src/@web3-storage/pail/dist/src/batch/shard.d.ts.map +1 -1
- package/src/@web3-storage/pail/dist/src/block.d.ts +2 -2
- package/src/@web3-storage/pail/dist/src/block.d.ts.map +1 -1
- package/src/@web3-storage/pail/dist/src/clock/index.d.ts +2 -2
- package/src/@web3-storage/pail/dist/src/clock/index.d.ts.map +1 -1
- package/src/@web3-storage/pail/dist/src/crdt/batch/index.d.ts.map +1 -1
- package/src/@web3-storage/pail/dist/src/crdt/index.d.ts +2 -2
- package/src/@web3-storage/pail/dist/src/crdt/index.d.ts.map +1 -1
- package/src/@web3-storage/pail/dist/src/diff.d.ts +3 -3
- package/src/@web3-storage/pail/dist/src/diff.d.ts.map +1 -1
- package/src/@web3-storage/pail/dist/src/index.d.ts +1 -1
- package/src/@web3-storage/pail/dist/src/index.d.ts.map +1 -1
- package/src/@web3-storage/pail/dist/src/merge.d.ts.map +1 -1
- package/src/@web3-storage/pail/dist/src/shard.d.ts +4 -4
- package/src/@web3-storage/pail/dist/src/shard.d.ts.map +1 -1
- package/src/@web3-storage/pail/dist/tsconfig.tsbuildinfo +1 -1
- package/src/@web3-storage/pail/src/api.ts +1 -1
- package/src/@web3-storage/pail/src/clock/index.js +1 -1
- package/src/@web3-storage/pail/src/shard.js +1 -1
- package/src/cborg/.github/dependabot.yml +16 -0
- package/src/cborg/.github/workflows/test-and-release.yml +52 -0
- package/src/cborg/CHANGELOG.md +513 -0
- package/src/cborg/LICENSE +13 -0
- package/src/cborg/README.md +515 -0
- package/src/cborg/bench/bench.js +117 -0
- package/src/cborg/bench/json.js +124 -0
- package/src/cborg/bench/package.json +8 -0
- package/src/cborg/cborg/bin.js +189 -0
- package/src/cborg/cborg/common.js +28 -0
- package/src/cborg/cborg/decode.js +211 -0
- package/src/cborg/cborg/diagnostic.js +158 -0
- package/src/cborg/cborg/diagnostic_test.js +117 -0
- package/src/cborg/cborg/encode.js +466 -0
- package/src/cborg/cborg/index.js +33 -0
- package/src/cborg/cborg/is.js +106 -0
- package/src/cborg/cborg/length.js +62 -0
- package/src/cborg/example-bytestrings.js +180 -0
- package/src/cborg/example-json.js +6 -0
- package/src/cborg/example.js +5 -0
- package/src/cborg/interface.ts +59 -0
- package/src/cborg/json/decode.js +462 -0
- package/src/cborg/json/encode.js +302 -0
- package/src/cborg/json/json.js +4 -0
- package/src/cborg/taglib.js +75 -0
- package/src/cborg/test/appendix_a.js +647 -0
- package/src/cborg/test/common.js +18 -0
- package/src/cborg/test/node-test-bin.js +402 -0
- package/src/cborg/test/noop-bin-test.js +3 -0
- package/src/cborg/test/test-0uint.js +103 -0
- package/src/cborg/test/test-1negint.js +96 -0
- package/src/cborg/test/test-2bytes.js +198 -0
- package/src/cborg/test/test-3string.js +136 -0
- package/src/cborg/test/test-4array.js +93 -0
- package/src/cborg/test/test-5map.js +284 -0
- package/src/cborg/test/test-6tag.js +84 -0
- package/src/cborg/test/test-7float.js +131 -0
- package/src/cborg/test/test-bl.js +37 -0
- package/src/cborg/test/test-cbor-vectors.js +107 -0
- package/src/cborg/test/test-decode-errors.js +65 -0
- package/src/cborg/test/test-fuzz.js +42 -0
- package/src/cborg/test/test-json.js +219 -0
- package/src/cborg/test/test-length.js +65 -0
- package/src/cborg/test/test-partial.js +111 -0
- package/src/cborg/tsconfig.json +48 -0
- package/src/cborg/types/cborg/decode.d.ts +43 -0
- package/src/cborg/types/cborg/decode.d.ts.map +1 -0
- package/src/cborg/types/cborg/encode.d.ts +51 -0
- package/src/cborg/types/cborg/encode.d.ts.map +1 -0
- package/src/cborg/types/cborg/index.d.ts +26 -0
- package/src/cborg/types/cborg/index.d.ts.map +1 -0
- package/src/cborg/types/cborg/is.d.ts +6 -0
- package/src/cborg/types/cborg/is.d.ts.map +1 -0
- package/src/cborg/types/example.d.ts +2 -0
- package/src/cborg/types/example.d.ts.map +1 -0
- package/src/cborg/types/interface.d.ts +49 -0
- package/src/cborg/types/interface.d.ts.map +1 -0
- package/src/cborg/types/json/decode.d.ts +67 -0
- package/src/cborg/types/json/decode.d.ts.map +1 -0
- package/src/cborg/types/json/encode.d.ts +11 -0
- package/src/cborg/types/json/encode.d.ts.map +1 -0
- package/src/cborg/types/json/json.d.ts +6 -0
- package/src/cborg/types/json/json.d.ts.map +1 -0
- package/src/cborg/types/taglib.d.ts +18 -0
- package/src/cborg/types/taglib.d.ts.map +1 -0
- package/src/cborg/types/tsconfig.tsbuildinfo +1 -0
- package/src/cborg/types/utils/0uint.d.ts +102 -0
- package/src/cborg/types/utils/0uint.d.ts.map +1 -0
- package/src/cborg/types/utils/1negint.d.ts +59 -0
- package/src/cborg/types/utils/1negint.d.ts.map +1 -0
- package/src/cborg/types/utils/2bytes.d.ts +69 -0
- package/src/cborg/types/utils/2bytes.d.ts.map +1 -0
- package/src/cborg/types/utils/3string.d.ts +46 -0
- package/src/cborg/types/utils/3string.d.ts.map +1 -0
- package/src/cborg/types/utils/4array.d.ts +66 -0
- package/src/cborg/types/utils/4array.d.ts.map +1 -0
- package/src/cborg/types/utils/5map.d.ts +66 -0
- package/src/cborg/types/utils/5map.d.ts.map +1 -0
- package/src/cborg/types/utils/6tag.d.ts +62 -0
- package/src/cborg/types/utils/6tag.d.ts.map +1 -0
- package/src/cborg/types/utils/7float.d.ts +60 -0
- package/src/cborg/types/utils/7float.d.ts.map +1 -0
- package/src/cborg/types/utils/bl.d.ts +26 -0
- package/src/cborg/types/utils/bl.d.ts.map +1 -0
- package/src/cborg/types/utils/byte-utils.d.ts +53 -0
- package/src/cborg/types/utils/byte-utils.d.ts.map +1 -0
- package/src/cborg/types/utils/common.d.ts +8 -0
- package/src/cborg/types/utils/common.d.ts.map +1 -0
- package/src/cborg/types/utils/index.d.ts +13 -0
- package/src/cborg/types/utils/index.d.ts.map +1 -0
- package/src/cborg/types/utils/jump.d.ts +16 -0
- package/src/cborg/types/utils/jump.d.ts.map +1 -0
- package/src/cborg/types/utils/token.d.ts +59 -0
- package/src/cborg/types/utils/token.d.ts.map +1 -0
- package/src/cborg/utils/0uint.js +229 -0
- package/src/cborg/utils/1negint.js +111 -0
- package/src/cborg/utils/2bytes.js +135 -0
- package/src/cborg/utils/3string.js +90 -0
- package/src/cborg/utils/4array.js +114 -0
- package/src/cborg/utils/5map.js +113 -0
- package/src/cborg/utils/6tag.js +81 -0
- package/src/cborg/utils/7float.js +310 -0
- package/src/cborg/utils/bl.js +124 -0
- package/src/cborg/utils/byte-utils.js +417 -0
- package/src/cborg/utils/common.js +11 -0
- package/src/cborg/utils/index.js +12 -0
- package/src/cborg/utils/jump.js +222 -0
- package/src/cborg/utils/token.js +76 -0
- package/src/ipfs-unixfs-exporter/LICENSE +4 -0
- package/src/ipfs-unixfs-exporter/README.md +105 -0
- package/src/ipfs-unixfs-exporter/dist/index.min.js +3 -0
- package/src/ipfs-unixfs-exporter/dist/src/errors.d.ts +57 -0
- package/src/ipfs-unixfs-exporter/dist/src/errors.d.ts.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/errors.js +73 -0
- package/src/ipfs-unixfs-exporter/dist/src/errors.js.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/index.d.ts +355 -0
- package/src/ipfs-unixfs-exporter/dist/src/index.d.ts.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/index.js +197 -0
- package/src/ipfs-unixfs-exporter/dist/src/index.js.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/dag-cbor.d.ts +4 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/dag-cbor.d.ts.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/dag-cbor.js +9 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/dag-cbor.js.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/dag-json.d.ts +4 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/dag-json.d.ts.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/dag-json.js +9 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/dag-json.js.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/identity.d.ts +4 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/identity.d.ts.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/identity.js +38 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/identity.js.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/index.d.ts +4 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/index.d.ts.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/index.js +30 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/index.js.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/json.d.ts +4 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/json.d.ts.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/json.js +9 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/json.js.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/raw.d.ts +4 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/raw.d.ts.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/raw.js +37 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/raw.js.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/directory.d.ts +4 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/directory.d.ts.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/directory.js +29 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/directory.js.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/file.d.ts +4 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/file.d.ts.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/file.js +153 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/file.js.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/hamt-sharded-directory.d.ts +4 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/hamt-sharded-directory.d.ts.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/hamt-sharded-directory.js +59 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/hamt-sharded-directory.js.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/raw.d.ts +4 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/raw.d.ts.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/raw.js +25 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/raw.js.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/index.d.ts +4 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/index.d.ts.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/index.js +104 -0
- package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/index.js.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/utils/extract-data-from-block.d.ts +3 -0
- package/src/ipfs-unixfs-exporter/dist/src/utils/extract-data-from-block.d.ts.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/utils/extract-data-from-block.js +20 -0
- package/src/ipfs-unixfs-exporter/dist/src/utils/extract-data-from-block.js.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/utils/find-cid-in-shard.d.ts +6 -0
- package/src/ipfs-unixfs-exporter/dist/src/utils/find-cid-in-shard.d.ts.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/utils/find-cid-in-shard.js +116 -0
- package/src/ipfs-unixfs-exporter/dist/src/utils/find-cid-in-shard.js.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/utils/resolve-object-path.d.ts +4 -0
- package/src/ipfs-unixfs-exporter/dist/src/utils/resolve-object-path.d.ts.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/utils/resolve-object-path.js +57 -0
- package/src/ipfs-unixfs-exporter/dist/src/utils/resolve-object-path.js.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/utils/validate-offset-and-length.d.ts +6 -0
- package/src/ipfs-unixfs-exporter/dist/src/utils/validate-offset-and-length.d.ts.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/src/utils/validate-offset-and-length.js +30 -0
- package/src/ipfs-unixfs-exporter/dist/src/utils/validate-offset-and-length.js.map +1 -0
- package/src/ipfs-unixfs-exporter/dist/typedoc-urls.json +58 -0
- package/src/ipfs-unixfs-exporter/src/errors.ts +87 -0
- package/src/ipfs-unixfs-exporter/src/index.ts +498 -0
- package/src/ipfs-unixfs-exporter/src/resolvers/dag-cbor.ts +12 -0
- package/src/ipfs-unixfs-exporter/src/resolvers/dag-json.ts +12 -0
- package/src/ipfs-unixfs-exporter/src/resolvers/identity.ts +49 -0
- package/src/ipfs-unixfs-exporter/src/resolvers/index.ts +35 -0
- package/src/ipfs-unixfs-exporter/src/resolvers/json.ts +12 -0
- package/src/ipfs-unixfs-exporter/src/resolvers/raw.ts +49 -0
- package/src/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/directory.ts +39 -0
- package/src/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/file.ts +198 -0
- package/src/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/hamt-sharded-directory.ts +76 -0
- package/src/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/raw.ts +37 -0
- package/src/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/index.ts +121 -0
- package/src/ipfs-unixfs-exporter/src/utils/extract-data-from-block.ts +24 -0
- package/src/ipfs-unixfs-exporter/src/utils/find-cid-in-shard.ts +149 -0
- package/src/ipfs-unixfs-exporter/src/utils/resolve-object-path.ts +62 -0
- package/src/ipfs-unixfs-exporter/src/utils/validate-offset-and-length.ts +38 -0
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
/* eslint-env mocha */
|
|
2
|
+
|
|
3
|
+
import * as chai from 'chai'
|
|
4
|
+
|
|
5
|
+
import { decode, encode } from "@fireproof/vendor/cborg"
|
|
6
|
+
import { fromHex, toHex } from "@fireproof/vendor/cborg/utils"
|
|
7
|
+
|
|
8
|
+
const { assert } = chai
|
|
9
|
+
|
|
10
|
+
// TODO: reject duplicate keys from encoded form
|
|
11
|
+
|
|
12
|
+
const fixtures = [
|
|
13
|
+
{ data: 'a0', expected: {}, type: 'map empty' },
|
|
14
|
+
{ data: 'a0', expected: new Map(), type: 'map empty (useMaps)', useMaps: true },
|
|
15
|
+
{ data: 'a1616101', expected: { a: 1 }, type: 'map 1 pair' },
|
|
16
|
+
{ data: 'a161316161', expected: { 1: 'a' }, type: 'map 1 pair (rev)' },
|
|
17
|
+
{
|
|
18
|
+
data: 'a1016161',
|
|
19
|
+
expected: toMap([[1, 'a']]),
|
|
20
|
+
type: 'map 1 pair (int key as Map w/ useMaps)',
|
|
21
|
+
useMaps: true
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
data: 'a243010203633132334302030463323334',
|
|
25
|
+
expected: toMap([[Uint8Array.from([1, 2, 3]), '123'], [Uint8Array.from([2, 3, 4]), '234']]),
|
|
26
|
+
type: 'map 2 pair (bytes keys Map w/ useMaps)',
|
|
27
|
+
useMaps: true
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
data: 'a1666f626a656374a16477697468a26134666e6573746564676f626a65637473a161216121',
|
|
31
|
+
expected: { object: { with: { 4: 'nested', objects: { '!': '!' } } } },
|
|
32
|
+
type: 'map nested'
|
|
33
|
+
},
|
|
34
|
+
{ // same as previous but as a Map and an int key
|
|
35
|
+
data: 'a1666f626a656374a16477697468a204666e6573746564676f626a65637473a161216121',
|
|
36
|
+
expected: toMap([['object', toMap([['with', toMap([[4, 'nested'], ['objects', toMap([['!', '!']])]])]])]]),
|
|
37
|
+
type: 'map nested w/ useMaps',
|
|
38
|
+
useMaps: true
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
data: 'ae636f6e651b0016db6db6db6db763736978206374656e3b0016db6db6db6db76374776f1a0001000064666976650064666f757202646e696e653aa5f702b365656967687438ff65736576656e226574687265651901f466656c6576656e426131667477656c76656fc48c6175657320c39f76c49b74652168666f75727465656ea4616664666f7572616f016174026274680368746869727465656e840203046466697665',
|
|
42
|
+
encode: {
|
|
43
|
+
one: Number.MAX_SAFE_INTEGER / 1.4,
|
|
44
|
+
two: 65536,
|
|
45
|
+
three: 500,
|
|
46
|
+
four: 2,
|
|
47
|
+
five: 0,
|
|
48
|
+
six: -1,
|
|
49
|
+
seven: -3,
|
|
50
|
+
eight: -256,
|
|
51
|
+
nine: -2784428724,
|
|
52
|
+
ten: Number.MIN_SAFE_INTEGER / 1.4 - 1,
|
|
53
|
+
eleven: new TextEncoder().encode('a1'),
|
|
54
|
+
twelve: 'Čaues ßvěte!',
|
|
55
|
+
thirteen: [2, 3, 4, 'five'],
|
|
56
|
+
fourteen: { o: 1, t: 2, th: 3, f: 'four' }
|
|
57
|
+
},
|
|
58
|
+
expected: {
|
|
59
|
+
one: Number.MAX_SAFE_INTEGER / 1.4,
|
|
60
|
+
six: -1,
|
|
61
|
+
ten: Number.MIN_SAFE_INTEGER / 1.4 - 1,
|
|
62
|
+
two: 65536,
|
|
63
|
+
five: 0,
|
|
64
|
+
four: 2,
|
|
65
|
+
nine: -2784428724,
|
|
66
|
+
eight: -256,
|
|
67
|
+
seven: -3,
|
|
68
|
+
three: 500,
|
|
69
|
+
eleven: new TextEncoder().encode('a1'),
|
|
70
|
+
twelve: 'Čaues ßvěte!',
|
|
71
|
+
fourteen: { f: 'four', o: 1, t: 2, th: 3 },
|
|
72
|
+
thirteen: [2, 3, 4, 'five']
|
|
73
|
+
},
|
|
74
|
+
type: 'map with complex entries',
|
|
75
|
+
label: '{}'
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
data: 'ad01636f6e65026374776f1901f46c666976652068756e647265641902586b7369782068756e647265641a00010000636269671b0016db6db6db6db76662696767657220696d696e7573206f6e6521696d696e75732074776f38ff781f6d696e75732074776f2068756e6472656420616e64206669667479207369783901f4781a6d696e757820666976652068756e6472656420616e64206f6e653901f5781a6d696e757820666976652068756e6472656420616e642074776f3aa5f702b367626967206e65673b0016db6db6db6db76a626967676572206e6567',
|
|
79
|
+
encode: toMap([
|
|
80
|
+
[2, 'two'],
|
|
81
|
+
[1, 'one'],
|
|
82
|
+
[-2, 'minus two'],
|
|
83
|
+
[-1, 'minus one'],
|
|
84
|
+
[600, 'six hundred'],
|
|
85
|
+
[500, 'five hundred'],
|
|
86
|
+
[-256, 'minus two hundred and fifty six'],
|
|
87
|
+
[-502, 'minux five hundred and two'],
|
|
88
|
+
[-501, 'minux five hundred and one'],
|
|
89
|
+
[65536, 'big'],
|
|
90
|
+
[-2784428724, 'big neg'],
|
|
91
|
+
[6433713753386423, 'bigger'],
|
|
92
|
+
[-6433713753386424, 'bigger neg']
|
|
93
|
+
]),
|
|
94
|
+
expected: toMap([
|
|
95
|
+
[1, 'one'],
|
|
96
|
+
[2, 'two'],
|
|
97
|
+
[500, 'five hundred'],
|
|
98
|
+
[600, 'six hundred'],
|
|
99
|
+
[65536, 'big'],
|
|
100
|
+
[6433713753386423, 'bigger'],
|
|
101
|
+
[-1, 'minus one'],
|
|
102
|
+
[-2, 'minus two'],
|
|
103
|
+
[-256, 'minus two hundred and fifty six'],
|
|
104
|
+
[-501, 'minux five hundred and one'],
|
|
105
|
+
[-502, 'minux five hundred and two'],
|
|
106
|
+
[-2784428724, 'big neg'],
|
|
107
|
+
[-6433713753386424, 'bigger neg']
|
|
108
|
+
]),
|
|
109
|
+
type: 'map with ints and negints',
|
|
110
|
+
useMaps: true
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
data: 'a44104636f6e65430102026374776f430102036574687265654301020464666f7572',
|
|
114
|
+
encode: toMap([
|
|
115
|
+
[Uint8Array.from([1, 2, 3]), 'three'],
|
|
116
|
+
[Uint8Array.from([4]), 'one'],
|
|
117
|
+
[Uint8Array.from([1, 2, 4]), 'four'],
|
|
118
|
+
[Uint8Array.from([1, 2, 2]), 'two']
|
|
119
|
+
]),
|
|
120
|
+
expected: toMap([
|
|
121
|
+
[Uint8Array.from([4]), 'one'],
|
|
122
|
+
[Uint8Array.from([1, 2, 2]), 'two'],
|
|
123
|
+
[Uint8Array.from([1, 2, 3]), 'three'],
|
|
124
|
+
[Uint8Array.from([1, 2, 4]), 'four']
|
|
125
|
+
]),
|
|
126
|
+
type: 'map with bytes keys',
|
|
127
|
+
useMaps: true
|
|
128
|
+
},
|
|
129
|
+
// testing lengths encoded as too-large ints
|
|
130
|
+
{ data: 'b801616101', expected: { a: 1 }, type: 'map 1 pair, length8', strict: false },
|
|
131
|
+
{ data: 'b90001616101', expected: { a: 1 }, type: 'map 1 pair, length16', strict: false },
|
|
132
|
+
{ data: 'ba00000001616101', expected: { a: 1 }, type: 'map 1 pair, length32', strict: false },
|
|
133
|
+
{ data: 'bb0000000000000001616101', expected: { a: 1 }, type: 'map 1 pair, length64', strict: false }
|
|
134
|
+
]
|
|
135
|
+
|
|
136
|
+
function toMap (arr) {
|
|
137
|
+
const m = new Map()
|
|
138
|
+
for (const [key, value] of arr) {
|
|
139
|
+
m.set(key, value)
|
|
140
|
+
}
|
|
141
|
+
return m
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function entries (map) {
|
|
145
|
+
function nest (a) {
|
|
146
|
+
for (const e of a) {
|
|
147
|
+
e[0] = entries(e[0])
|
|
148
|
+
e[1] = entries(e[1])
|
|
149
|
+
}
|
|
150
|
+
return a
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (Object.getPrototypeOf(map) === Map.prototype) {
|
|
154
|
+
return nest([...map.entries()])
|
|
155
|
+
}
|
|
156
|
+
if (typeof map === 'object') {
|
|
157
|
+
return nest([...Object.entries(map)])
|
|
158
|
+
}
|
|
159
|
+
return map
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
describe('map', () => {
|
|
163
|
+
describe('decode', () => {
|
|
164
|
+
for (const fixture of fixtures) {
|
|
165
|
+
const data = fromHex(fixture.data)
|
|
166
|
+
it(`should decode ${fixture.type}=${fixture.label || JSON.stringify(fixture.expected)}`, () => {
|
|
167
|
+
let options = fixture.useMaps ? { useMaps: true } : undefined
|
|
168
|
+
const decoded = decode(data, options)
|
|
169
|
+
|
|
170
|
+
if (fixture.useMaps) {
|
|
171
|
+
assert.strictEqual(Object.getPrototypeOf(decoded), Map.prototype, 'is Map')
|
|
172
|
+
} else {
|
|
173
|
+
assert.isObject(decoded, 'is object')
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
assert.deepStrictEqual(entries(decoded), entries(fixture.expected), `decode ${fixture.type}`)
|
|
177
|
+
|
|
178
|
+
options = Object.assign({ strict: true }, options)
|
|
179
|
+
if (fixture.strict === false) {
|
|
180
|
+
assert.throws(() => decode(data, options), Error, 'CBOR decode error: integer encoded in more bytes than necessary (strict decode)')
|
|
181
|
+
} else {
|
|
182
|
+
assert.deepStrictEqual(
|
|
183
|
+
entries(decode(data, options)),
|
|
184
|
+
entries(fixture.expected),
|
|
185
|
+
`decode ${fixture.type}`)
|
|
186
|
+
}
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
it('should fail to decode very large length', () => {
|
|
190
|
+
assert.throws(
|
|
191
|
+
() => decode(fromHex('bba5f702b3a5f70201616101')),
|
|
192
|
+
/CBOR decode error: 64-bit integer map lengths not supported/)
|
|
193
|
+
})
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
it('errors', () => {
|
|
197
|
+
assert.throws(() => decode(fromHex('a1016161')), /non-string keys not supported \(got number\)/)
|
|
198
|
+
})
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
describe('encode', () => {
|
|
202
|
+
for (const fixture of fixtures) {
|
|
203
|
+
it(`should encode ${fixture.type}=${fixture.label || JSON.stringify(fixture.expected)}`, () => {
|
|
204
|
+
const toEncode = fixture.encode || fixture.expected
|
|
205
|
+
if (fixture.unsafe) {
|
|
206
|
+
assert.throws(encode.bind(null, toEncode), Error, /^CBOR encode error: number too large to encode \(\d+\)$/)
|
|
207
|
+
} else if (fixture.strict === false || fixture.roundtrip === false) {
|
|
208
|
+
assert.notDeepEqual(toHex(encode(toEncode)), fixture.data, `encode ${fixture.type} !strict`)
|
|
209
|
+
} else {
|
|
210
|
+
assert.strictEqual(toHex(encode(toEncode)), fixture.data, `encode ${fixture.type}`)
|
|
211
|
+
}
|
|
212
|
+
})
|
|
213
|
+
}
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
// mostly unnecessary, but feels good
|
|
217
|
+
describe('roundtrip', () => {
|
|
218
|
+
for (const fixture of fixtures) {
|
|
219
|
+
if (!fixture.unsafe && fixture.strict !== false && fixture.roundtrip !== false) {
|
|
220
|
+
it(`should roundtrip ${fixture.type}=${fixture.label || JSON.stringify(fixture.expected)}`, () => {
|
|
221
|
+
const toEncode = fixture.encode || fixture.expected
|
|
222
|
+
const options = fixture.useMaps ? { useMaps: true } : undefined
|
|
223
|
+
const rt = decode(encode(toEncode), options)
|
|
224
|
+
|
|
225
|
+
if (fixture.useMaps) {
|
|
226
|
+
assert.strictEqual(Object.getPrototypeOf(rt), Map.prototype, 'is Map')
|
|
227
|
+
} else {
|
|
228
|
+
assert.isObject(rt, 'is object')
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
assert.deepStrictEqual(entries(rt), entries(fixture.expected), `roundtrip ${fixture.type}`)
|
|
232
|
+
})
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
describe('specials', () => {
|
|
238
|
+
it('can decode indefinite length items', () => {
|
|
239
|
+
assert.deepStrictEqual(decode(fromHex('bf616f01617402ff')), { o: 1, t: 2 })
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
it('can switch off indefinite length support', () => {
|
|
243
|
+
assert.throws(() => decode(fromHex('bf616f01617402ff'), { allowIndefinite: false }), /indefinite/)
|
|
244
|
+
})
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
describe('sorting', () => {
|
|
248
|
+
it('sorts int map keys', () => {
|
|
249
|
+
assert.strictEqual(toHex(encode(new Map([[1, 1], [2, 2]]))), 'a201010202')
|
|
250
|
+
assert.strictEqual(toHex(encode(new Map([[2, 1], [1, 2]]))), 'a201020201')
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
it('sorts negint map keys', () => {
|
|
254
|
+
assert.strictEqual(toHex(encode(new Map([[-1, 1], [-2, 2]]))), 'a220012102')
|
|
255
|
+
assert.strictEqual(toHex(encode(new Map([[-2, 1], [-1, 2]]))), 'a220022101')
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
it('sorts bytes map keys', () => {
|
|
259
|
+
assert.strictEqual(toHex(encode(new Map([[Uint8Array.from([1, 2]), 1], [Uint8Array.from([2, 1]), 2]]))), 'a24201020142020102')
|
|
260
|
+
assert.strictEqual(toHex(encode(new Map([[Uint8Array.from([2, 1]), 1], [Uint8Array.from([1, 2]), 2]]))), 'a24201020242020101')
|
|
261
|
+
// shortest first
|
|
262
|
+
assert.strictEqual(toHex(encode(new Map([[Uint8Array.from([1, 2]), 1], [Uint8Array.from([2, 1]), 2], [Uint8Array.from([200]), 3]]))), 'a341c8034201020142020102')
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
it('sorts bytes map keys', () => {
|
|
266
|
+
assert.strictEqual(toHex(encode(new Map([[Uint8Array.from([1, 2]), 1], [Uint8Array.from([2, 1]), 2]]))), 'a24201020142020102')
|
|
267
|
+
assert.strictEqual(toHex(encode(new Map([[Uint8Array.from([2, 1]), 1], [Uint8Array.from([1, 2]), 2]]))), 'a24201020242020101')
|
|
268
|
+
// shortest first
|
|
269
|
+
assert.strictEqual(toHex(encode(new Map([[Uint8Array.from([1, 2]), 1], [Uint8Array.from([2, 1]), 2], [Uint8Array.from([200]), 3]]))), 'a341c8034201020142020102')
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
it('sorts array map keys (length only)', () => {
|
|
273
|
+
assert.strictEqual(toHex(encode(new Map([[[1], 1], [[1, 1], 2]]))), 'a281010182010102')
|
|
274
|
+
assert.strictEqual(toHex(encode(new Map([[[1, 1], 1], [[1], 2]]))), 'a281010282010101')
|
|
275
|
+
})
|
|
276
|
+
|
|
277
|
+
it('sorts map map keys (length only)', () => {
|
|
278
|
+
assert.strictEqual(toHex(encode(new Map([[{ a: 1 }, 1], [{ a: 1, b: 1 }, 2]]))), 'a2a161610101a261610161620102')
|
|
279
|
+
assert.strictEqual(toHex(encode(new Map([[{ a: 1, b: 1 }, 1], [{ a: 1 }, 2]]))), 'a2a161610102a261610161620101')
|
|
280
|
+
})
|
|
281
|
+
|
|
282
|
+
// TODO: tag keys .. but why would you do this!?
|
|
283
|
+
})
|
|
284
|
+
})
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/* eslint-env mocha */
|
|
2
|
+
|
|
3
|
+
import * as chai from 'chai'
|
|
4
|
+
|
|
5
|
+
import { decode, encode } from "@fireproof/vendor/cborg"
|
|
6
|
+
import { fromHex, toHex, Token, Type } from "@fireproof/vendor/cborg/utils"
|
|
7
|
+
import { dateDecoder, dateEncoder } from './common.js'
|
|
8
|
+
|
|
9
|
+
const { assert } = chai
|
|
10
|
+
|
|
11
|
+
function Uint16ArrayDecoder (obj) {
|
|
12
|
+
if (typeof obj !== 'string') {
|
|
13
|
+
throw new Error('expected string for tag 23')
|
|
14
|
+
}
|
|
15
|
+
const u8a = fromHex(obj)
|
|
16
|
+
return new Uint16Array(u8a.buffer, u8a.byteOffset, u8a.length / 2)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function Uint16ArrayEncoder (obj) {
|
|
20
|
+
if (!(obj instanceof Uint16Array)) {
|
|
21
|
+
throw new Error('expected Uint16Array for "Uint16Array" encoder')
|
|
22
|
+
}
|
|
23
|
+
return [
|
|
24
|
+
new Token(Type.tag, 23),
|
|
25
|
+
new Token(Type.string, toHex(obj))
|
|
26
|
+
]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
describe('tag', () => {
|
|
30
|
+
it('date', () => {
|
|
31
|
+
assert.throws(() => encode({ d: new Date() }), /unsupported type: Date/)
|
|
32
|
+
|
|
33
|
+
assert.equal(
|
|
34
|
+
toHex(encode(new Date('2013-03-21T20:04:00Z'), { typeEncoders: { Date: dateEncoder } })),
|
|
35
|
+
'c074323031332d30332d32315432303a30343a30305a' // from appendix_a
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
const decodedDate = decode(fromHex('c074323031332d30332d32315432303a30343a30305a'), { tags: { 0: dateDecoder } })
|
|
39
|
+
assert.instanceOf(decodedDate, Date)
|
|
40
|
+
assert.equal(decodedDate.toISOString(), new Date('2013-03-21T20:04:00Z').toISOString())
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('Uint16Array as hex/23 (overide existing type)', () => {
|
|
44
|
+
assert.equal(
|
|
45
|
+
toHex(encode(Uint16Array.from([1, 2, 3]), { typeEncoders: { Uint16Array: Uint16ArrayEncoder } })),
|
|
46
|
+
'd76c303130303032303030333030' // tag(23) + string('010002000300')
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
const decoded = decode(fromHex('d76c303130303032303030333030'), { tags: { 23: Uint16ArrayDecoder } })
|
|
50
|
+
assert.instanceOf(decoded, Uint16Array)
|
|
51
|
+
assert.equal(toHex(decoded), toHex(Uint16Array.from([1, 2, 3])))
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('tag int too large', () => {
|
|
55
|
+
const verify = (hex, strict) => {
|
|
56
|
+
if (!strict) {
|
|
57
|
+
assert.throws(
|
|
58
|
+
() => decode(fromHex(hex), { tags: { 8: dateDecoder }, strict: true }),
|
|
59
|
+
/integer encoded in more bytes than necessary/)
|
|
60
|
+
}
|
|
61
|
+
const decodedDate = decode(fromHex(hex), { tags: { 8: dateDecoder }, strict })
|
|
62
|
+
assert.instanceOf(decodedDate, Date)
|
|
63
|
+
assert.equal(decodedDate.toISOString(), new Date('2013-03-21T20:04:00Z').toISOString())
|
|
64
|
+
}
|
|
65
|
+
// compact
|
|
66
|
+
verify('c874323031332d30332d32315432303a30343a30305a', true)
|
|
67
|
+
// int8
|
|
68
|
+
verify('d80874323031332d30332d32315432303a30343a30305a', false)
|
|
69
|
+
// int16
|
|
70
|
+
verify('d9000874323031332d30332d32315432303a30343a30305a', false)
|
|
71
|
+
// int32
|
|
72
|
+
verify('da0000000874323031332d30332d32315432303a30343a30305a', false)
|
|
73
|
+
// int64
|
|
74
|
+
verify('db000000000000000874323031332d30332d32315432303a30343a30305a', false)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
/*
|
|
78
|
+
describe('taglib', () => {
|
|
79
|
+
it('bigint', () => {
|
|
80
|
+
const v = BigInt(2) ** BigInt(80)
|
|
81
|
+
})
|
|
82
|
+
})
|
|
83
|
+
*/
|
|
84
|
+
})
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/* eslint-env mocha */
|
|
2
|
+
|
|
3
|
+
import * as chai from 'chai'
|
|
4
|
+
|
|
5
|
+
import { decode, encode } from "@fireproof/vendor/cborg"
|
|
6
|
+
import { fromHex, toHex } from "@fireproof/vendor/cborg/utils"
|
|
7
|
+
|
|
8
|
+
const { assert } = chai
|
|
9
|
+
|
|
10
|
+
const fixtures = [
|
|
11
|
+
{ data: '8601f5f4f6f720', expected: [1, true, false, null, undefined, -1], type: 'array of float specials' },
|
|
12
|
+
{ data: 'f93800', expected: 0.5, type: 'float16' },
|
|
13
|
+
{ data: 'f9b800', expected: -0.5, type: 'float16' },
|
|
14
|
+
{ data: 'fa33c00000', expected: 8.940696716308594e-08, type: 'float32' },
|
|
15
|
+
{ data: 'fab3c00000', expected: -8.940696716308594e-08, type: 'float32' },
|
|
16
|
+
{ data: 'fb3ff199999999999a', expected: 1.1, type: 'float64' },
|
|
17
|
+
{ data: 'fbbff199999999999a', expected: -1.1, type: 'float64' },
|
|
18
|
+
{ data: 'fb3ff1c71c71c71c72', expected: 1.11111111111111111111111111111, type: 'float64' }, // eslint-disable-line
|
|
19
|
+
{ data: 'fb0000000000000002', expected: 1e-323, type: 'float64' },
|
|
20
|
+
{ data: 'fb8000000000000002', expected: -1e-323, type: 'float64' },
|
|
21
|
+
{ data: 'fb3fefffffffffffff', expected: 0.9999999999999999, type: 'float64' },
|
|
22
|
+
{ data: 'fbbfefffffffffffff', expected: -0.9999999999999999, type: 'float64' },
|
|
23
|
+
{ data: 'f97c00', expected: Infinity, type: 'Infinity' }, // special CBOR token for -Infinity
|
|
24
|
+
{ data: 'fb7ff0000000000000', expected: Infinity, type: 'Infinity', strict: false }, // an IEEE 754 representation of Infinity
|
|
25
|
+
{ data: 'f9fc00', expected: -Infinity, type: '-Infinity' }, // special CBOR token for -Infinity
|
|
26
|
+
{ data: 'fbfff0000000000000', expected: -Infinity, type: '-Infinity', strict: false }, // an IEEE 754 representation of Infinity
|
|
27
|
+
{ data: 'f97e00', expected: NaN, type: 'NaN' }, // special CBOR token for NaN
|
|
28
|
+
{ data: 'f97ff8', expected: NaN, type: 'NaN', strict: false }, // one of the many IEEE 754 representations of NaN
|
|
29
|
+
{ data: 'fa7ff80000', expected: NaN, type: 'NaN', strict: false },
|
|
30
|
+
{ data: 'fb7ff8000000000000', expected: NaN, type: 'NaN', strict: false },
|
|
31
|
+
{ data: 'fb7ff8cafedeadbeef', expected: NaN, type: 'NaN', strict: false }, // yep, that's NaN too
|
|
32
|
+
{ data: 'fb40f4241a31a5a515', expected: 82497.63712086187, type: 'float64' }
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
describe('float', () => {
|
|
36
|
+
describe('decode', () => {
|
|
37
|
+
for (const fixture of fixtures) {
|
|
38
|
+
const data = fromHex(fixture.data)
|
|
39
|
+
it(`should decode ${fixture.type}=${fixture.expected}`, () => {
|
|
40
|
+
assert.deepStrictEqual(decode(data), fixture.expected, `decode ${fixture.type}`)
|
|
41
|
+
assert.deepStrictEqual(decode(data, { strict: true }), fixture.expected, `decode ${fixture.type}`)
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('error', () => {
|
|
47
|
+
// minor number 28, too high for uint
|
|
48
|
+
assert.throws(() => decode(fromHex('f80000')), Error, 'simple values are not supported')
|
|
49
|
+
assert.throws(() => decode(fromHex('f900')), Error, 'not enough data for float16')
|
|
50
|
+
assert.throws(() => decode(fromHex('fa0000')), Error, 'not enough data for float32')
|
|
51
|
+
assert.throws(() => decode(fromHex('fb00000000')), Error, 'not enough data for float64')
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
describe('encode', () => {
|
|
55
|
+
for (const fixture of fixtures) {
|
|
56
|
+
if (fixture.strict !== false) {
|
|
57
|
+
it(`should encode ${fixture.type}=${fixture.expected}`, () => {
|
|
58
|
+
assert.strictEqual(toHex(encode(fixture.expected)), fixture.data, `encode ${fixture.type}`)
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
describe('encode float64', () => {
|
|
65
|
+
for (const fixture of fixtures) {
|
|
66
|
+
if (fixture.type.startsWith('float')) {
|
|
67
|
+
it(`should encode ${fixture.type}=${fixture.expected}`, () => {
|
|
68
|
+
const encoded = encode(fixture.expected, { float64: true })
|
|
69
|
+
assert.strictEqual(encoded.length, 9) // always encode as 9 bytes, regardless of size
|
|
70
|
+
assert.strictEqual(encoded[0], 0xfb)
|
|
71
|
+
assert.strictEqual(decode(encoded), fixture.expected, `encode float64 ${fixture.type}`)
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
describe('roundtrip', () => {
|
|
78
|
+
for (const fixture of fixtures) {
|
|
79
|
+
if (!fixture.unsafe && fixture.strict !== false) {
|
|
80
|
+
it(`should roundtrip ${fixture.type}=${fixture.expected}`, () => {
|
|
81
|
+
assert.deepStrictEqual(decode(encode(fixture.expected)), fixture.expected, `roundtrip ${fixture.type}`)
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
describe('specials', () => {
|
|
88
|
+
// This is a bit of a hack, the CBOR is invalid because it's a standard fixed-length array
|
|
89
|
+
// followed by a BREAK, which should normally error ("too many terminals"), but we want to
|
|
90
|
+
// exercise the allowIndefinite switch in the major-7 decode and it should error before it
|
|
91
|
+
// even gets to looking at terminals and whether the tokens make sense.
|
|
92
|
+
it('indefinite length switch fails on BREAK', () => {
|
|
93
|
+
// sanity check, BREAK doesn't belong there
|
|
94
|
+
assert.throws(() => decode(Uint8Array.from([131, 1, 2, 0xff])), /unexpected break to lengthed array/)
|
|
95
|
+
// throw earlier because we're disallowing BREAK entirely
|
|
96
|
+
assert.throws(() => decode(Uint8Array.from([131, 1, 2, 0xff]), { allowIndefinite: false }), /indefinite/)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('can switch off undefined support', () => {
|
|
100
|
+
assert.deepStrictEqual(decode(fromHex('f7')), undefined)
|
|
101
|
+
assert.throws(() => decode(fromHex('f7'), { allowUndefined: false }), /undefined/)
|
|
102
|
+
assert.deepStrictEqual(decode(fromHex('830102f7')), [1, 2, undefined])
|
|
103
|
+
assert.throws(() => decode(fromHex('830102f7'), { allowUndefined: false }), /undefined/)
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('can coerce undefined to null', () => {
|
|
107
|
+
assert.deepStrictEqual(decode(fromHex('f7'), { coerceUndefinedToNull: false }), undefined)
|
|
108
|
+
assert.deepStrictEqual(decode(fromHex('f7'), { coerceUndefinedToNull: true }), null)
|
|
109
|
+
assert.deepStrictEqual(decode(fromHex('830102f7'), { coerceUndefinedToNull: false }), [1, 2, undefined])
|
|
110
|
+
assert.deepStrictEqual(decode(fromHex('830102f7'), { coerceUndefinedToNull: true }), [1, 2, null])
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
it('can switch off Infinity support', () => {
|
|
114
|
+
assert.deepStrictEqual(decode(fromHex('830102f97c00')), [1, 2, Infinity])
|
|
115
|
+
assert.deepStrictEqual(decode(fromHex('830102f9fc00')), [1, 2, -Infinity])
|
|
116
|
+
assert.throws(() => decode(fromHex('830102f97c00'), { allowInfinity: false }), /Infinity/)
|
|
117
|
+
assert.throws(() => decode(fromHex('830102f9fc00'), { allowInfinity: false }), /Infinity/)
|
|
118
|
+
for (const fixture of fixtures.filter((f) => f.type.endsWith('Infinity'))) {
|
|
119
|
+
assert.throws(() => decode(fromHex(fixture.data), { allowInfinity: false }), /Infinity/)
|
|
120
|
+
}
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it('can switch off NaN support', () => {
|
|
124
|
+
assert.deepStrictEqual(decode(fromHex('830102f97e00')), [1, 2, NaN])
|
|
125
|
+
assert.throws(() => decode(fromHex('830102f97e00'), { allowNaN: false }), /NaN/)
|
|
126
|
+
for (const fixture of fixtures.filter((f) => f.type === 'NaN')) {
|
|
127
|
+
assert.throws(() => decode(fromHex(fixture.data), { allowNaN: false }), /NaN/)
|
|
128
|
+
}
|
|
129
|
+
})
|
|
130
|
+
})
|
|
131
|
+
})
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/* eslint-env mocha */
|
|
2
|
+
|
|
3
|
+
import * as chai from 'chai'
|
|
4
|
+
import { Bl } from "@fireproof/vendor/cborg/utils"
|
|
5
|
+
|
|
6
|
+
const { assert } = chai
|
|
7
|
+
|
|
8
|
+
describe('Internal bytes list', () => {
|
|
9
|
+
describe('push', () => {
|
|
10
|
+
it('push bits', () => {
|
|
11
|
+
const bl = new Bl(10)
|
|
12
|
+
const expected = []
|
|
13
|
+
for (let i = 0; i < 25; i++) {
|
|
14
|
+
bl.push([i + 1])
|
|
15
|
+
expected.push(i + 1)
|
|
16
|
+
}
|
|
17
|
+
assert.deepEqual([...bl.toBytes()], expected)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
for (let i = 4; i < 21; i++) {
|
|
21
|
+
it(`push Bl(${i})`, () => {
|
|
22
|
+
const bl = new Bl(i)
|
|
23
|
+
const expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 110, 120, 11, 12, 130, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
|
|
24
|
+
for (let i = 0; i < 5; i++) {
|
|
25
|
+
bl.push([i + 1])
|
|
26
|
+
}
|
|
27
|
+
bl.push(Uint8Array.from([6, 7, 8, 9, 10]))
|
|
28
|
+
bl.push([100])
|
|
29
|
+
bl.push(Uint8Array.from([110, 120]))
|
|
30
|
+
bl.push(Uint8Array.from([11, 12]))
|
|
31
|
+
bl.push([130])
|
|
32
|
+
bl.push(Uint8Array.from([13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]))
|
|
33
|
+
assert.deepEqual([...bl.toBytes()], expected)
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
})
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/* eslint-env mocha,es2020 */
|
|
2
|
+
|
|
3
|
+
import * as chai from 'chai'
|
|
4
|
+
|
|
5
|
+
import { decode, encode } from "@fireproof/vendor/cborg"
|
|
6
|
+
import * as taglib from "@fireproof/vendor/cborg/taglib"
|
|
7
|
+
import { fromHex, toHex } from "@fireproof/vendor/cborg/utils"
|
|
8
|
+
// fixtures from https://github.com/cbor/test-vectors
|
|
9
|
+
import { fixtures } from './appendix_a.js'
|
|
10
|
+
|
|
11
|
+
const { assert } = chai
|
|
12
|
+
|
|
13
|
+
const tags = []
|
|
14
|
+
const typeEncoders = {}
|
|
15
|
+
|
|
16
|
+
tags[0] = function (obj) {
|
|
17
|
+
if (typeof obj !== 'string') {
|
|
18
|
+
throw new Error('expected string for tag 1')
|
|
19
|
+
}
|
|
20
|
+
return `0("${new Date(obj).toISOString().replace(/\.000Z$/, 'Z')}")`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
tags[1] = function (obj) {
|
|
24
|
+
if (typeof obj !== 'number') {
|
|
25
|
+
throw new Error('expected number for tag 1')
|
|
26
|
+
}
|
|
27
|
+
return `1(${obj})`
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
tags[2] = taglib.bigIntDecoder
|
|
31
|
+
typeEncoders.bigint = taglib.bigIntEncoder
|
|
32
|
+
tags[3] = taglib.bigNegIntDecoder
|
|
33
|
+
|
|
34
|
+
tags[23] = function (obj) {
|
|
35
|
+
// expected conversion to base16
|
|
36
|
+
if (!(obj instanceof Uint8Array)) {
|
|
37
|
+
throw new Error('expected byte array for tag 23')
|
|
38
|
+
}
|
|
39
|
+
return `23(h'${toHex(obj)}')`
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
tags[24] = function (obj) { // embedded cbor, oh my
|
|
43
|
+
return tags[23](obj).replace(/^23/, '24');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
tags[32] = function (obj) { // url
|
|
47
|
+
if (typeof obj !== 'string') {
|
|
48
|
+
throw new Error('expected string for tag 32')
|
|
49
|
+
}
|
|
50
|
+
;(() => new URL(obj))() // will throw if not a url
|
|
51
|
+
return `32("${obj}")`
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
describe('cbor/test-vectors', () => {
|
|
55
|
+
let i = 0
|
|
56
|
+
for (const fixture of fixtures) {
|
|
57
|
+
const u8a = fromHex(fixture.hex)
|
|
58
|
+
let expected = fixture.decoded !== undefined ? fixture.decoded : fixture.diagnostic
|
|
59
|
+
|
|
60
|
+
if (typeof expected === 'string' && expected.startsWith('h\'')) {
|
|
61
|
+
expected = fromHex(expected.replace(/(^h)'|('$)/g, ''))
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
it(`test vector #${i}: ${inspect(expected).replace(/\n\s*/g, '')}`, () => {
|
|
65
|
+
if (fixture.error) {
|
|
66
|
+
assert.throws(() => decode(u8a, { tags }), fixture.error)
|
|
67
|
+
} else {
|
|
68
|
+
if (fixture.noTagDecodeError) {
|
|
69
|
+
assert.throws(() => decode(u8a), fixture.noTagDecodeError)
|
|
70
|
+
}
|
|
71
|
+
let actual = decode(u8a, { tags })
|
|
72
|
+
if (typeof actual === 'bigint') {
|
|
73
|
+
actual = inspect(actual)
|
|
74
|
+
}
|
|
75
|
+
if (typeof expected === 'bigint') {
|
|
76
|
+
expected = inspect(expected)
|
|
77
|
+
}
|
|
78
|
+
assert.deepEqual(actual, expected)
|
|
79
|
+
|
|
80
|
+
if (fixture.roundtrip) {
|
|
81
|
+
if (fixture.noTagEncodeError) {
|
|
82
|
+
assert.throws(() => encode(decode(u8a, { tags })), fixture.noTagEncodeError)
|
|
83
|
+
}
|
|
84
|
+
const reencoded = encode(decode(u8a, { tags }), { typeEncoders })
|
|
85
|
+
assert.equal(toHex(reencoded), fixture.hex)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
i++
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
it.skip('encode w/ tags', () => {
|
|
93
|
+
})
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
function inspect (o) {
|
|
97
|
+
if (typeof o === 'string') {
|
|
98
|
+
return `'${o}'`
|
|
99
|
+
}
|
|
100
|
+
if (o instanceof Uint8Array) {
|
|
101
|
+
return `Uint8Array<${o.join(',')}>`
|
|
102
|
+
}
|
|
103
|
+
if (o == null || typeof o !== 'object') {
|
|
104
|
+
return String(o)
|
|
105
|
+
}
|
|
106
|
+
return JSON.stringify(o)
|
|
107
|
+
}
|