@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,417 @@
|
|
|
1
|
+
// Use Uint8Array directly in the browser, use Buffer in Node.js but don't
|
|
2
|
+
// speak its name directly to avoid bundlers pulling in the `Buffer` polyfill
|
|
3
|
+
|
|
4
|
+
// @ts-ignore
|
|
5
|
+
export const useBuffer = globalThis.process &&
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
!globalThis.process.browser &&
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
globalThis.Buffer &&
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
typeof globalThis.Buffer.isBuffer === 'function'
|
|
12
|
+
|
|
13
|
+
const textDecoder = new TextDecoder()
|
|
14
|
+
const textEncoder = new TextEncoder()
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {Uint8Array} buf
|
|
18
|
+
* @returns {boolean}
|
|
19
|
+
*/
|
|
20
|
+
function isBuffer (buf) {
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
return useBuffer && globalThis.Buffer.isBuffer(buf)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {Uint8Array|number[]} buf
|
|
27
|
+
* @returns {Uint8Array}
|
|
28
|
+
*/
|
|
29
|
+
export function asU8A (buf) {
|
|
30
|
+
/* c8 ignore next */
|
|
31
|
+
if (!(buf instanceof Uint8Array)) {
|
|
32
|
+
return Uint8Array.from(buf)
|
|
33
|
+
}
|
|
34
|
+
return isBuffer(buf) ? new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) : buf
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const toString = useBuffer
|
|
38
|
+
? // eslint-disable-line operator-linebreak
|
|
39
|
+
/**
|
|
40
|
+
* @param {Uint8Array} bytes
|
|
41
|
+
* @param {number} start
|
|
42
|
+
* @param {number} end
|
|
43
|
+
*/
|
|
44
|
+
(bytes, start, end) => {
|
|
45
|
+
return end - start > 64
|
|
46
|
+
? // eslint-disable-line operator-linebreak
|
|
47
|
+
// @ts-ignore
|
|
48
|
+
globalThis.Buffer.from(bytes.subarray(start, end)).toString('utf8')
|
|
49
|
+
: utf8Slice(bytes, start, end)
|
|
50
|
+
}
|
|
51
|
+
/* c8 ignore next 11 */
|
|
52
|
+
: // eslint-disable-line operator-linebreak
|
|
53
|
+
/**
|
|
54
|
+
* @param {Uint8Array} bytes
|
|
55
|
+
* @param {number} start
|
|
56
|
+
* @param {number} end
|
|
57
|
+
*/
|
|
58
|
+
(bytes, start, end) => {
|
|
59
|
+
return end - start > 64
|
|
60
|
+
? textDecoder.decode(bytes.subarray(start, end))
|
|
61
|
+
: utf8Slice(bytes, start, end)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const fromString = useBuffer
|
|
65
|
+
? // eslint-disable-line operator-linebreak
|
|
66
|
+
/**
|
|
67
|
+
* @param {string} string
|
|
68
|
+
*/
|
|
69
|
+
(string) => {
|
|
70
|
+
return string.length > 64
|
|
71
|
+
? // eslint-disable-line operator-linebreak
|
|
72
|
+
// @ts-ignore
|
|
73
|
+
globalThis.Buffer.from(string)
|
|
74
|
+
: utf8ToBytes(string)
|
|
75
|
+
}
|
|
76
|
+
/* c8 ignore next 7 */
|
|
77
|
+
: // eslint-disable-line operator-linebreak
|
|
78
|
+
/**
|
|
79
|
+
* @param {string} string
|
|
80
|
+
*/
|
|
81
|
+
(string) => {
|
|
82
|
+
return string.length > 64 ? textEncoder.encode(string) : utf8ToBytes(string)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Buffer variant not fast enough for what we need
|
|
87
|
+
* @param {number[]} arr
|
|
88
|
+
* @returns {Uint8Array}
|
|
89
|
+
*/
|
|
90
|
+
export const fromArray = (arr) => {
|
|
91
|
+
return Uint8Array.from(arr)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export const slice = useBuffer
|
|
95
|
+
? // eslint-disable-line operator-linebreak
|
|
96
|
+
/**
|
|
97
|
+
* @param {Uint8Array} bytes
|
|
98
|
+
* @param {number} start
|
|
99
|
+
* @param {number} end
|
|
100
|
+
*/
|
|
101
|
+
(bytes, start, end) => {
|
|
102
|
+
if (isBuffer(bytes)) {
|
|
103
|
+
return new Uint8Array(bytes.subarray(start, end))
|
|
104
|
+
}
|
|
105
|
+
return bytes.slice(start, end)
|
|
106
|
+
}
|
|
107
|
+
/* c8 ignore next 9 */
|
|
108
|
+
: // eslint-disable-line operator-linebreak
|
|
109
|
+
/**
|
|
110
|
+
* @param {Uint8Array} bytes
|
|
111
|
+
* @param {number} start
|
|
112
|
+
* @param {number} end
|
|
113
|
+
*/
|
|
114
|
+
(bytes, start, end) => {
|
|
115
|
+
return bytes.slice(start, end)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export const concat = useBuffer
|
|
119
|
+
? // eslint-disable-line operator-linebreak
|
|
120
|
+
/**
|
|
121
|
+
* @param {Uint8Array[]} chunks
|
|
122
|
+
* @param {number} length
|
|
123
|
+
* @returns {Uint8Array}
|
|
124
|
+
*/
|
|
125
|
+
(chunks, length) => {
|
|
126
|
+
// might get a stray plain Array here
|
|
127
|
+
/* c8 ignore next 1 */
|
|
128
|
+
chunks = chunks.map((c) => c instanceof Uint8Array
|
|
129
|
+
? c
|
|
130
|
+
// this case is occasionally missed during test runs so becomes coverage-flaky
|
|
131
|
+
/* c8 ignore next 4 */
|
|
132
|
+
: // eslint-disable-line operator-linebreak
|
|
133
|
+
// @ts-ignore
|
|
134
|
+
globalThis.Buffer.from(c))
|
|
135
|
+
// @ts-ignore
|
|
136
|
+
return asU8A(globalThis.Buffer.concat(chunks, length))
|
|
137
|
+
}
|
|
138
|
+
/* c8 ignore next 19 */
|
|
139
|
+
: // eslint-disable-line operator-linebreak
|
|
140
|
+
/**
|
|
141
|
+
* @param {Uint8Array[]} chunks
|
|
142
|
+
* @param {number} length
|
|
143
|
+
* @returns {Uint8Array}
|
|
144
|
+
*/
|
|
145
|
+
(chunks, length) => {
|
|
146
|
+
const out = new Uint8Array(length)
|
|
147
|
+
let off = 0
|
|
148
|
+
for (let b of chunks) {
|
|
149
|
+
if (off + b.length > out.length) {
|
|
150
|
+
// final chunk that's bigger than we need
|
|
151
|
+
b = b.subarray(0, out.length - off)
|
|
152
|
+
}
|
|
153
|
+
out.set(b, off)
|
|
154
|
+
off += b.length
|
|
155
|
+
}
|
|
156
|
+
return out
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export const alloc = useBuffer
|
|
160
|
+
? // eslint-disable-line operator-linebreak
|
|
161
|
+
/**
|
|
162
|
+
* @param {number} size
|
|
163
|
+
* @returns {Uint8Array}
|
|
164
|
+
*/
|
|
165
|
+
(size) => {
|
|
166
|
+
// we always write over the contents we expose so this should be safe
|
|
167
|
+
// @ts-ignore
|
|
168
|
+
return globalThis.Buffer.allocUnsafe(size)
|
|
169
|
+
}
|
|
170
|
+
/* c8 ignore next 8 */
|
|
171
|
+
: // eslint-disable-line operator-linebreak
|
|
172
|
+
/**
|
|
173
|
+
* @param {number} size
|
|
174
|
+
* @returns {Uint8Array}
|
|
175
|
+
*/
|
|
176
|
+
(size) => {
|
|
177
|
+
return new Uint8Array(size)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export const toHex = useBuffer
|
|
181
|
+
? // eslint-disable-line operator-linebreak
|
|
182
|
+
/**
|
|
183
|
+
* @param {Uint8Array} d
|
|
184
|
+
* @returns {string}
|
|
185
|
+
*/
|
|
186
|
+
(d) => {
|
|
187
|
+
if (typeof d === 'string') {
|
|
188
|
+
return d
|
|
189
|
+
}
|
|
190
|
+
// @ts-ignore
|
|
191
|
+
return globalThis.Buffer.from(toBytes(d)).toString('hex')
|
|
192
|
+
}
|
|
193
|
+
/* c8 ignore next 12 */
|
|
194
|
+
: // eslint-disable-line operator-linebreak
|
|
195
|
+
/**
|
|
196
|
+
* @param {Uint8Array} d
|
|
197
|
+
* @returns {string}
|
|
198
|
+
*/
|
|
199
|
+
(d) => {
|
|
200
|
+
if (typeof d === 'string') {
|
|
201
|
+
return d
|
|
202
|
+
}
|
|
203
|
+
// @ts-ignore not smart enough to figure this out
|
|
204
|
+
return Array.prototype.reduce.call(toBytes(d), (p, c) => `${p}${c.toString(16).padStart(2, '0')}`, '')
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export const fromHex = useBuffer
|
|
208
|
+
? // eslint-disable-line operator-linebreak
|
|
209
|
+
/**
|
|
210
|
+
* @param {string|Uint8Array} hex
|
|
211
|
+
* @returns {Uint8Array}
|
|
212
|
+
*/
|
|
213
|
+
(hex) => {
|
|
214
|
+
if (hex instanceof Uint8Array) {
|
|
215
|
+
return hex
|
|
216
|
+
}
|
|
217
|
+
// @ts-ignore
|
|
218
|
+
return globalThis.Buffer.from(hex, 'hex')
|
|
219
|
+
}
|
|
220
|
+
/* c8 ignore next 17 */
|
|
221
|
+
: // eslint-disable-line operator-linebreak
|
|
222
|
+
/**
|
|
223
|
+
* @param {string|Uint8Array} hex
|
|
224
|
+
* @returns {Uint8Array}
|
|
225
|
+
*/
|
|
226
|
+
(hex) => {
|
|
227
|
+
if (hex instanceof Uint8Array) {
|
|
228
|
+
return hex
|
|
229
|
+
}
|
|
230
|
+
if (!hex.length) {
|
|
231
|
+
return new Uint8Array(0)
|
|
232
|
+
}
|
|
233
|
+
return new Uint8Array(hex.split('')
|
|
234
|
+
.map((/** @type {string} */ c, /** @type {number} */ i, /** @type {string[]} */ d) => i % 2 === 0 ? `0x${c}${d[i + 1]}` : '')
|
|
235
|
+
.filter(Boolean)
|
|
236
|
+
.map((/** @type {string} */ e) => parseInt(e, 16)))
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* @param {Uint8Array|ArrayBuffer|ArrayBufferView} obj
|
|
241
|
+
* @returns {Uint8Array}
|
|
242
|
+
*/
|
|
243
|
+
function toBytes (obj) {
|
|
244
|
+
if (obj instanceof Uint8Array && obj.constructor.name === 'Uint8Array') {
|
|
245
|
+
return obj
|
|
246
|
+
}
|
|
247
|
+
if (obj instanceof ArrayBuffer) {
|
|
248
|
+
return new Uint8Array(obj)
|
|
249
|
+
}
|
|
250
|
+
if (ArrayBuffer.isView(obj)) {
|
|
251
|
+
return new Uint8Array(obj.buffer, obj.byteOffset, obj.byteLength)
|
|
252
|
+
}
|
|
253
|
+
/* c8 ignore next */
|
|
254
|
+
throw new Error('Unknown type, must be binary type')
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* @param {Uint8Array} b1
|
|
259
|
+
* @param {Uint8Array} b2
|
|
260
|
+
* @returns {number}
|
|
261
|
+
*/
|
|
262
|
+
export function compare (b1, b2) {
|
|
263
|
+
/* c8 ignore next 5 */
|
|
264
|
+
if (isBuffer(b1) && isBuffer(b2)) {
|
|
265
|
+
// probably not possible to get here in the current API
|
|
266
|
+
// @ts-ignore Buffer
|
|
267
|
+
return b1.compare(b2)
|
|
268
|
+
}
|
|
269
|
+
for (let i = 0; i < b1.length; i++) {
|
|
270
|
+
if (b1[i] === b2[i]) {
|
|
271
|
+
continue
|
|
272
|
+
}
|
|
273
|
+
return b1[i] < b2[i] ? -1 : 1
|
|
274
|
+
} /* c8 ignore next 3 */
|
|
275
|
+
return 0
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// The below code is taken from https://github.com/google/closure-library/blob/8598d87242af59aac233270742c8984e2b2bdbe0/closure/goog/crypt/crypt.js#L117-L143
|
|
279
|
+
// Licensed Apache-2.0.
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* @param {string} str
|
|
283
|
+
* @returns {number[]}
|
|
284
|
+
*/
|
|
285
|
+
function utf8ToBytes (str) {
|
|
286
|
+
const out = []
|
|
287
|
+
let p = 0
|
|
288
|
+
for (let i = 0; i < str.length; i++) {
|
|
289
|
+
let c = str.charCodeAt(i)
|
|
290
|
+
if (c < 128) {
|
|
291
|
+
out[p++] = c
|
|
292
|
+
} else if (c < 2048) {
|
|
293
|
+
out[p++] = (c >> 6) | 192
|
|
294
|
+
out[p++] = (c & 63) | 128
|
|
295
|
+
} else if (
|
|
296
|
+
((c & 0xFC00) === 0xD800) && (i + 1) < str.length &&
|
|
297
|
+
((str.charCodeAt(i + 1) & 0xFC00) === 0xDC00)) {
|
|
298
|
+
// Surrogate Pair
|
|
299
|
+
c = 0x10000 + ((c & 0x03FF) << 10) + (str.charCodeAt(++i) & 0x03FF)
|
|
300
|
+
out[p++] = (c >> 18) | 240
|
|
301
|
+
out[p++] = ((c >> 12) & 63) | 128
|
|
302
|
+
out[p++] = ((c >> 6) & 63) | 128
|
|
303
|
+
out[p++] = (c & 63) | 128
|
|
304
|
+
} else {
|
|
305
|
+
out[p++] = (c >> 12) | 224
|
|
306
|
+
out[p++] = ((c >> 6) & 63) | 128
|
|
307
|
+
out[p++] = (c & 63) | 128
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return out
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// The below code is mostly taken from https://github.com/feross/buffer
|
|
314
|
+
// Licensed MIT. Copyright (c) Feross Aboukhadijeh
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* @param {Uint8Array} buf
|
|
318
|
+
* @param {number} offset
|
|
319
|
+
* @param {number} end
|
|
320
|
+
* @returns {string}
|
|
321
|
+
*/
|
|
322
|
+
function utf8Slice (buf, offset, end) {
|
|
323
|
+
const res = []
|
|
324
|
+
|
|
325
|
+
while (offset < end) {
|
|
326
|
+
const firstByte = buf[offset]
|
|
327
|
+
let codePoint = null
|
|
328
|
+
let bytesPerSequence = (firstByte > 0xef) ? 4 : (firstByte > 0xdf) ? 3 : (firstByte > 0xbf) ? 2 : 1
|
|
329
|
+
|
|
330
|
+
if (offset + bytesPerSequence <= end) {
|
|
331
|
+
let secondByte, thirdByte, fourthByte, tempCodePoint
|
|
332
|
+
|
|
333
|
+
switch (bytesPerSequence) {
|
|
334
|
+
case 1:
|
|
335
|
+
if (firstByte < 0x80) {
|
|
336
|
+
codePoint = firstByte
|
|
337
|
+
}
|
|
338
|
+
break
|
|
339
|
+
case 2:
|
|
340
|
+
secondByte = buf[offset + 1]
|
|
341
|
+
if ((secondByte & 0xc0) === 0x80) {
|
|
342
|
+
tempCodePoint = (firstByte & 0x1f) << 0x6 | (secondByte & 0x3f)
|
|
343
|
+
if (tempCodePoint > 0x7f) {
|
|
344
|
+
codePoint = tempCodePoint
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
break
|
|
348
|
+
case 3:
|
|
349
|
+
secondByte = buf[offset + 1]
|
|
350
|
+
thirdByte = buf[offset + 2]
|
|
351
|
+
if ((secondByte & 0xc0) === 0x80 && (thirdByte & 0xc0) === 0x80) {
|
|
352
|
+
tempCodePoint = (firstByte & 0xf) << 0xc | (secondByte & 0x3f) << 0x6 | (thirdByte & 0x3f)
|
|
353
|
+
/* c8 ignore next 3 */
|
|
354
|
+
if (tempCodePoint > 0x7ff && (tempCodePoint < 0xd800 || tempCodePoint > 0xdfff)) {
|
|
355
|
+
codePoint = tempCodePoint
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
break
|
|
359
|
+
case 4:
|
|
360
|
+
secondByte = buf[offset + 1]
|
|
361
|
+
thirdByte = buf[offset + 2]
|
|
362
|
+
fourthByte = buf[offset + 3]
|
|
363
|
+
if ((secondByte & 0xc0) === 0x80 && (thirdByte & 0xc0) === 0x80 && (fourthByte & 0xc0) === 0x80) {
|
|
364
|
+
tempCodePoint = (firstByte & 0xf) << 0x12 | (secondByte & 0x3f) << 0xc | (thirdByte & 0x3f) << 0x6 | (fourthByte & 0x3f)
|
|
365
|
+
if (tempCodePoint > 0xffff && tempCodePoint < 0x110000) {
|
|
366
|
+
codePoint = tempCodePoint
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/* c8 ignore next 5 */
|
|
373
|
+
if (codePoint === null) {
|
|
374
|
+
// we did not generate a valid codePoint so insert a
|
|
375
|
+
// replacement char (U+FFFD) and advance only 1 byte
|
|
376
|
+
codePoint = 0xfffd
|
|
377
|
+
bytesPerSequence = 1
|
|
378
|
+
} else if (codePoint > 0xffff) {
|
|
379
|
+
// encode to utf16 (surrogate pair dance)
|
|
380
|
+
codePoint -= 0x10000
|
|
381
|
+
res.push(codePoint >>> 10 & 0x3ff | 0xd800)
|
|
382
|
+
codePoint = 0xdc00 | codePoint & 0x3ff
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
res.push(codePoint)
|
|
386
|
+
offset += bytesPerSequence
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
return decodeCodePointsArray(res)
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// Based on http://stackoverflow.com/a/22747272/680742, the browser with
|
|
393
|
+
// the lowest limit is Chrome, with 0x10000 args.
|
|
394
|
+
// We go 1 magnitude less, for safety
|
|
395
|
+
const MAX_ARGUMENTS_LENGTH = 0x1000
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* @param {number[]} codePoints
|
|
399
|
+
* @returns {string}
|
|
400
|
+
*/
|
|
401
|
+
export function decodeCodePointsArray (codePoints) {
|
|
402
|
+
const len = codePoints.length
|
|
403
|
+
if (len <= MAX_ARGUMENTS_LENGTH) {
|
|
404
|
+
return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
|
|
405
|
+
}
|
|
406
|
+
/* c8 ignore next 10 */
|
|
407
|
+
// Decode in chunks to avoid "call stack size exceeded".
|
|
408
|
+
let res = ''
|
|
409
|
+
let i = 0
|
|
410
|
+
while (i < len) {
|
|
411
|
+
res += String.fromCharCode.apply(
|
|
412
|
+
String,
|
|
413
|
+
codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
|
|
414
|
+
)
|
|
415
|
+
}
|
|
416
|
+
return res
|
|
417
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {Uint8Array} data
|
|
3
|
+
* @param {number} pos
|
|
4
|
+
* @param {number} need
|
|
5
|
+
* @param {string} decodeErrPrefix
|
|
6
|
+
*/
|
|
7
|
+
export function assertEnoughData (data, pos, need, decodeErrPrefix) {
|
|
8
|
+
if (data.length - pos < need) {
|
|
9
|
+
throw new Error(`${decodeErrPrefix} not enough data for type`)
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from './byte-utils.js'
|
|
2
|
+
export { encodeUint, uintBoundaries } from './0uint.js'
|
|
3
|
+
export { encodeNegint } from './1negint.js'
|
|
4
|
+
export { encodeBytes } from './2bytes.js'
|
|
5
|
+
export { encodeString } from './3string.js'
|
|
6
|
+
export { encodeArray } from './4array.js'
|
|
7
|
+
export { encodeMap } from './5map.js'
|
|
8
|
+
export { encodeTag } from './6tag.js'
|
|
9
|
+
export { encodeFloat } from './7float.js'
|
|
10
|
+
export { Bl } from './bl.js'
|
|
11
|
+
export { Token, Type } from './token.js'
|
|
12
|
+
export * from './jump.js'
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { Token, Type } from './token.js'
|
|
2
|
+
import * as uint from './0uint.js'
|
|
3
|
+
import * as negint from './1negint.js'
|
|
4
|
+
import * as bytes from './2bytes.js'
|
|
5
|
+
import * as string from './3string.js'
|
|
6
|
+
import * as array from './4array.js'
|
|
7
|
+
import * as map from './5map.js'
|
|
8
|
+
import * as tag from './6tag.js'
|
|
9
|
+
import * as float from './7float.js'
|
|
10
|
+
import { fromArray } from './byte-utils.js'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
14
|
+
* @typedef {import('../interface.js').DecodeFunction} DecodeFunction
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {string} decodeErrPrefix
|
|
19
|
+
* @returns {DecodeFunction}
|
|
20
|
+
*/
|
|
21
|
+
function invalidMinorWithPrefix (decodeErrPrefix) {
|
|
22
|
+
/**
|
|
23
|
+
* @param {Uint8Array} data
|
|
24
|
+
* @param {number} pos
|
|
25
|
+
* @param {number} minor
|
|
26
|
+
*/
|
|
27
|
+
return (data, pos, minor) => {
|
|
28
|
+
throw new Error(`${decodeErrPrefix} encountered invalid minor (${minor}) for major ${data[pos] >>> 5}`)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {string} msg
|
|
34
|
+
* @param {string} decodeErrPrefix
|
|
35
|
+
* @returns {()=>any}
|
|
36
|
+
*/
|
|
37
|
+
function errorer (msg, decodeErrPrefix) {
|
|
38
|
+
return () => { throw new Error(`${decodeErrPrefix} ${msg}`) }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @param {string} decodeErrPrefix
|
|
43
|
+
* @return {DecodeFunction[]}
|
|
44
|
+
*/
|
|
45
|
+
export function jump (decodeErrPrefix) {
|
|
46
|
+
const invalidMinor = invalidMinorWithPrefix(decodeErrPrefix)
|
|
47
|
+
const jump = Array(256).fill(invalidMinor)
|
|
48
|
+
// unsigned integer, 0x00..0x17 (0..23)
|
|
49
|
+
// for (let i = 0; i <= 0x17; i++) {
|
|
50
|
+
// jump[i] = invalidMinor // uint.decodeUintCompact, handled by quick[]
|
|
51
|
+
// }
|
|
52
|
+
jump[0x18] = uint.decodeUint8 // unsigned integer, one-byte uint8_t follows
|
|
53
|
+
jump[0x19] = uint.decodeUint16 // unsigned integer, two-byte uint16_t follows
|
|
54
|
+
jump[0x1a] = uint.decodeUint32 // unsigned integer, four-byte uint32_t follows
|
|
55
|
+
jump[0x1b] = uint.decodeUint64 // unsigned integer, eight-byte uint64_t follows
|
|
56
|
+
// jump[0x1c] = invalidMinor
|
|
57
|
+
// jump[0x1d] = invalidMinor
|
|
58
|
+
// jump[0x1e] = invalidMinor
|
|
59
|
+
// jump[0x1f] = invalidMinor
|
|
60
|
+
// negative integer, -1-0x00..-1-0x17 (-1..-24)
|
|
61
|
+
// for (let i = 0x20; i <= 0x37; i++) {
|
|
62
|
+
// jump[i] = invalidMinor // negintDecode, handled by quick[]
|
|
63
|
+
// }
|
|
64
|
+
jump[0x38] = negint.decodeNegint8 // negative integer, -1-n one-byte uint8_t for n follows
|
|
65
|
+
jump[0x39] = negint.decodeNegint16 // negative integer, -1-n two-byte uint16_t for n follows
|
|
66
|
+
jump[0x3a] = negint.decodeNegint32 // negative integer, -1-n four-byte uint32_t for follows
|
|
67
|
+
jump[0x3b] = negint.decodeNegint64 // negative integer, -1-n eight-byte uint64_t for follows
|
|
68
|
+
// jump[0x3c] = invalidMinor
|
|
69
|
+
// jump[0x3d] = invalidMinor
|
|
70
|
+
// jump[0x3e] = invalidMinor
|
|
71
|
+
// jump[0x3f] = invalidMinor
|
|
72
|
+
// byte string, 0x00..0x17 bytes follow
|
|
73
|
+
for (let i = 0x40; i <= 0x57; i++) {
|
|
74
|
+
jump[i] = bytes.decodeBytesCompact
|
|
75
|
+
}
|
|
76
|
+
jump[0x58] = bytes.decodeBytes8 // byte string, one-byte uint8_t for n, and then n bytes follow
|
|
77
|
+
jump[0x59] = bytes.decodeBytes16 // byte string, two-byte uint16_t for n, and then n bytes follow
|
|
78
|
+
jump[0x5a] = bytes.decodeBytes32 // byte string, four-byte uint32_t for n, and then n bytes follow
|
|
79
|
+
jump[0x5b] = bytes.decodeBytes64 // byte string, eight-byte uint64_t for n, and then n bytes follow
|
|
80
|
+
// jump[0x5c] = invalidMinor
|
|
81
|
+
// jump[0x5d] = invalidMinor
|
|
82
|
+
// jump[0x5e] = invalidMinor
|
|
83
|
+
jump[0x5f] = errorer('indefinite length bytes/strings are not supported', decodeErrPrefix) // byte string, byte strings follow, terminated by "break"
|
|
84
|
+
// UTF-8 string 0x00..0x17 bytes follow
|
|
85
|
+
for (let i = 0x60; i <= 0x77; i++) {
|
|
86
|
+
jump[i] = string.decodeStringCompact
|
|
87
|
+
}
|
|
88
|
+
jump[0x78] = string.decodeString8 // UTF-8 string, one-byte uint8_t for n, and then n bytes follow
|
|
89
|
+
jump[0x79] = string.decodeString16 // UTF-8 string, two-byte uint16_t for n, and then n bytes follow
|
|
90
|
+
jump[0x7a] = string.decodeString32 // UTF-8 string, four-byte uint32_t for n, and then n bytes follow
|
|
91
|
+
jump[0x7b] = string.decodeString64 // UTF-8 string, eight-byte uint64_t for n, and then n bytes follow
|
|
92
|
+
// jump[0x7c] = invalidMinor
|
|
93
|
+
// jump[0x7d] = invalidMinor
|
|
94
|
+
// jump[0x7e] = invalidMinor
|
|
95
|
+
jump[0x7f] = errorer('indefinite length bytes/strings are not supported', decodeErrPrefix) // UTF-8 strings follow, terminated by "break"
|
|
96
|
+
// array, 0x00..0x17 data items follow
|
|
97
|
+
for (let i = 0x80; i <= 0x97; i++) {
|
|
98
|
+
jump[i] = array.decodeArrayCompact
|
|
99
|
+
}
|
|
100
|
+
jump[0x98] = array.decodeArray8 // array, one-byte uint8_t for n, and then n data items follow
|
|
101
|
+
jump[0x99] = array.decodeArray16 // array, two-byte uint16_t for n, and then n data items follow
|
|
102
|
+
jump[0x9a] = array.decodeArray32 // array, four-byte uint32_t for n, and then n data items follow
|
|
103
|
+
jump[0x9b] = array.decodeArray64 // array, eight-byte uint64_t for n, and then n data items follow
|
|
104
|
+
// jump[0x9c] = invalidMinor
|
|
105
|
+
// jump[0x9d] = invalidMinor
|
|
106
|
+
// jump[0x9e] = invalidMinor
|
|
107
|
+
jump[0x9f] = array.decodeArrayIndefinite // array, data items follow, terminated by "break"
|
|
108
|
+
// map, 0x00..0x17 pairs of data items follow
|
|
109
|
+
for (let i = 0xa0; i <= 0xb7; i++) {
|
|
110
|
+
jump[i] = map.decodeMapCompact
|
|
111
|
+
}
|
|
112
|
+
jump[0xb8] = map.decodeMap8 // map, one-byte uint8_t for n, and then n pairs of data items follow
|
|
113
|
+
jump[0xb9] = map.decodeMap16 // map, two-byte uint16_t for n, and then n pairs of data items follow
|
|
114
|
+
jump[0xba] = map.decodeMap32 // map, four-byte uint32_t for n, and then n pairs of data items follow
|
|
115
|
+
jump[0xbb] = map.decodeMap64 // map, eight-byte uint64_t for n, and then n pairs of data items follow
|
|
116
|
+
// jump[0xbc] = invalidMinor
|
|
117
|
+
// jump[0xbd] = invalidMinor
|
|
118
|
+
// jump[0xbe] = invalidMinor
|
|
119
|
+
jump[0xbf] = map.decodeMapIndefinite // map, pairs of data items follow, terminated by "break"
|
|
120
|
+
// tags
|
|
121
|
+
for (let i = 0xc0; i <= 0xd7; i++) {
|
|
122
|
+
jump[i] = tag.decodeTagCompact
|
|
123
|
+
}
|
|
124
|
+
jump[0xd8] = tag.decodeTag8
|
|
125
|
+
jump[0xd9] = tag.decodeTag16
|
|
126
|
+
jump[0xda] = tag.decodeTag32
|
|
127
|
+
jump[0xdb] = tag.decodeTag64
|
|
128
|
+
// jump[0xdc] = invalidMinor
|
|
129
|
+
// jump[0xdd] = invalidMinor
|
|
130
|
+
// jump[0xde] = invalidMinor
|
|
131
|
+
// jump[0xdf] = invalidMinor
|
|
132
|
+
// 0xe0..0xf3 simple values, unsupported
|
|
133
|
+
for (let i = 0xe0; i <= 0xf3; i++) {
|
|
134
|
+
jump[i] = errorer('simple values are not supported', decodeErrPrefix)
|
|
135
|
+
}
|
|
136
|
+
// jump[0xf4] = invalidMinor // false, handled by quick[]
|
|
137
|
+
// jump[0xf5] = invalidMinor // true, handled by quick[]
|
|
138
|
+
// jump[0xf6] = invalidMinor // null, handled by quick[]
|
|
139
|
+
jump[0xf7] = float.decodeUndefined // undefined
|
|
140
|
+
jump[0xf8] = errorer('simple values are not supported', decodeErrPrefix) // simple value, one byte follows, unsupported
|
|
141
|
+
jump[0xf9] = float.decodeFloat16 // half-precision float (two-byte IEEE 754)
|
|
142
|
+
jump[0xfa] = float.decodeFloat32 // single-precision float (four-byte IEEE 754)
|
|
143
|
+
jump[0xfb] = float.decodeFloat64 // double-precision float (eight-byte IEEE 754)
|
|
144
|
+
// jump[0xfc] = invalidMinor
|
|
145
|
+
// jump[0xfd] = invalidMinor
|
|
146
|
+
// jump[0xfe] = invalidMinor
|
|
147
|
+
jump[0xff] = float.decodeBreak // "break" stop code
|
|
148
|
+
return jump
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** @type {Token[]} */
|
|
152
|
+
export const quick = []
|
|
153
|
+
// ints <24
|
|
154
|
+
for (let i = 0; i < 24; i++) {
|
|
155
|
+
quick[i] = new Token(Type.uint, i, 1)
|
|
156
|
+
}
|
|
157
|
+
// negints >= -24
|
|
158
|
+
for (let i = -1; i >= -24; i--) {
|
|
159
|
+
quick[31 - i] = new Token(Type.negint, i, 1)
|
|
160
|
+
}
|
|
161
|
+
// empty bytes
|
|
162
|
+
quick[0x40] = new Token(Type.bytes, new Uint8Array(0), 1)
|
|
163
|
+
// empty string
|
|
164
|
+
quick[0x60] = new Token(Type.string, '', 1)
|
|
165
|
+
// empty list
|
|
166
|
+
quick[0x80] = new Token(Type.array, 0, 1)
|
|
167
|
+
// empty map
|
|
168
|
+
quick[0xa0] = new Token(Type.map, 0, 1)
|
|
169
|
+
// false
|
|
170
|
+
quick[0xf4] = new Token(Type.false, false, 1)
|
|
171
|
+
// true
|
|
172
|
+
quick[0xf5] = new Token(Type.true, true, 1)
|
|
173
|
+
// null
|
|
174
|
+
quick[0xf6] = new Token(Type.null, null, 1)
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* @param {Token} token
|
|
178
|
+
* @returns {Uint8Array|undefined}
|
|
179
|
+
*/
|
|
180
|
+
export function quickEncodeToken (token) {
|
|
181
|
+
switch (token.type) {
|
|
182
|
+
case Type.false:
|
|
183
|
+
return fromArray([0xf4])
|
|
184
|
+
case Type.true:
|
|
185
|
+
return fromArray([0xf5])
|
|
186
|
+
case Type.null:
|
|
187
|
+
return fromArray([0xf6])
|
|
188
|
+
case Type.bytes:
|
|
189
|
+
if (!token.value.length) {
|
|
190
|
+
return fromArray([0x40])
|
|
191
|
+
}
|
|
192
|
+
return
|
|
193
|
+
case Type.string:
|
|
194
|
+
if (token.value === '') {
|
|
195
|
+
return fromArray([0x60])
|
|
196
|
+
}
|
|
197
|
+
return
|
|
198
|
+
case Type.array:
|
|
199
|
+
if (token.value === 0) {
|
|
200
|
+
return fromArray([0x80])
|
|
201
|
+
}
|
|
202
|
+
/* c8 ignore next 2 */
|
|
203
|
+
// shouldn't be possible if this were called when there was only one token
|
|
204
|
+
return
|
|
205
|
+
case Type.map:
|
|
206
|
+
if (token.value === 0) {
|
|
207
|
+
return fromArray([0xa0])
|
|
208
|
+
}
|
|
209
|
+
/* c8 ignore next 2 */
|
|
210
|
+
// shouldn't be possible if this were called when there was only one token
|
|
211
|
+
return
|
|
212
|
+
case Type.uint:
|
|
213
|
+
if (token.value < 24) {
|
|
214
|
+
return fromArray([Number(token.value)])
|
|
215
|
+
}
|
|
216
|
+
return
|
|
217
|
+
case Type.negint:
|
|
218
|
+
if (token.value >= -24) {
|
|
219
|
+
return fromArray([31 - Number(token.value)])
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|