@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,147 @@
|
|
|
1
|
+
import * as cborg from "@fireproof/vendor/cborg"
|
|
2
|
+
import { CID } from 'multiformats/cid'
|
|
3
|
+
|
|
4
|
+
// https://github.com/ipfs/go-ipfs/issues/3570#issuecomment-273931692
|
|
5
|
+
const CID_CBOR_TAG = 42
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @template T
|
|
9
|
+
* @typedef {import('multiformats/codecs/interface').ByteView<T>} ByteView
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @template T
|
|
14
|
+
* @typedef {import('multiformats/codecs/interface').ArrayBufferView<T>} ArrayBufferView
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @template T
|
|
19
|
+
* @param {ByteView<T> | ArrayBufferView<T>} buf
|
|
20
|
+
* @returns {ByteView<T>}
|
|
21
|
+
*/
|
|
22
|
+
export function toByteView (buf) {
|
|
23
|
+
if (buf instanceof ArrayBuffer) {
|
|
24
|
+
return new Uint8Array(buf, 0, buf.byteLength)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return buf
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* cidEncoder will receive all Objects during encode, it needs to filter out
|
|
32
|
+
* anything that's not a CID and return `null` for that so it's encoded as
|
|
33
|
+
* normal.
|
|
34
|
+
*
|
|
35
|
+
* @param {any} obj
|
|
36
|
+
* @returns {cborg.Token[]|null}
|
|
37
|
+
*/
|
|
38
|
+
function cidEncoder (obj) {
|
|
39
|
+
if (obj.asCID !== obj && obj['/'] !== obj.bytes) {
|
|
40
|
+
return null // any other kind of object
|
|
41
|
+
}
|
|
42
|
+
const cid = CID.asCID(obj)
|
|
43
|
+
/* c8 ignore next 4 */
|
|
44
|
+
// very unlikely case, and it'll probably throw a recursion error in cborg
|
|
45
|
+
if (!cid) {
|
|
46
|
+
return null
|
|
47
|
+
}
|
|
48
|
+
const bytes = new Uint8Array(cid.bytes.byteLength + 1)
|
|
49
|
+
bytes.set(cid.bytes, 1) // prefix is 0x00, for historical reasons
|
|
50
|
+
return [
|
|
51
|
+
new cborg.Token(cborg.Type.tag, CID_CBOR_TAG),
|
|
52
|
+
new cborg.Token(cborg.Type.bytes, bytes)
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// eslint-disable-next-line jsdoc/require-returns-check
|
|
57
|
+
/**
|
|
58
|
+
* Intercept all `undefined` values from an object walk and reject the entire
|
|
59
|
+
* object if we find one.
|
|
60
|
+
*
|
|
61
|
+
* @returns {null}
|
|
62
|
+
*/
|
|
63
|
+
function undefinedEncoder () {
|
|
64
|
+
throw new Error('`undefined` is not supported by the IPLD Data Model and cannot be encoded')
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Intercept all `number` values from an object walk and reject the entire
|
|
69
|
+
* object if we find something that doesn't fit the IPLD data model (NaN &
|
|
70
|
+
* Infinity).
|
|
71
|
+
*
|
|
72
|
+
* @param {number} num
|
|
73
|
+
* @returns {null}
|
|
74
|
+
*/
|
|
75
|
+
function numberEncoder (num) {
|
|
76
|
+
if (Number.isNaN(num)) {
|
|
77
|
+
throw new Error('`NaN` is not supported by the IPLD Data Model and cannot be encoded')
|
|
78
|
+
}
|
|
79
|
+
if (num === Infinity || num === -Infinity) {
|
|
80
|
+
throw new Error('`Infinity` and `-Infinity` is not supported by the IPLD Data Model and cannot be encoded')
|
|
81
|
+
}
|
|
82
|
+
return null
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const _encodeOptions = {
|
|
86
|
+
float64: true,
|
|
87
|
+
typeEncoders: {
|
|
88
|
+
Object: cidEncoder,
|
|
89
|
+
undefined: undefinedEncoder,
|
|
90
|
+
number: numberEncoder
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export const encodeOptions = {
|
|
95
|
+
..._encodeOptions,
|
|
96
|
+
typeEncoders: {
|
|
97
|
+
..._encodeOptions.typeEncoders
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @param {Uint8Array} bytes
|
|
103
|
+
* @returns {CID}
|
|
104
|
+
*/
|
|
105
|
+
function cidDecoder (bytes) {
|
|
106
|
+
if (bytes[0] !== 0) {
|
|
107
|
+
throw new Error('Invalid CID for CBOR tag 42; expected leading 0x00')
|
|
108
|
+
}
|
|
109
|
+
return CID.decode(bytes.subarray(1)) // ignore leading 0x00
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const _decodeOptions = {
|
|
113
|
+
allowIndefinite: false,
|
|
114
|
+
coerceUndefinedToNull: true,
|
|
115
|
+
allowNaN: false,
|
|
116
|
+
allowInfinity: false,
|
|
117
|
+
allowBigInt: true, // this will lead to BigInt for ints outside of
|
|
118
|
+
// safe-integer range, which may surprise users
|
|
119
|
+
strict: true,
|
|
120
|
+
useMaps: false,
|
|
121
|
+
rejectDuplicateMapKeys: true,
|
|
122
|
+
/** @type {import('cborg').TagDecoder[]} */
|
|
123
|
+
tags: []
|
|
124
|
+
}
|
|
125
|
+
_decodeOptions.tags[CID_CBOR_TAG] = cidDecoder
|
|
126
|
+
|
|
127
|
+
export const decodeOptions = {
|
|
128
|
+
..._decodeOptions,
|
|
129
|
+
tags: _decodeOptions.tags.slice()
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export const name = 'dag-cbor'
|
|
133
|
+
export const code = 0x71
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @template T
|
|
137
|
+
* @param {T} node
|
|
138
|
+
* @returns {ByteView<T>}
|
|
139
|
+
*/
|
|
140
|
+
export const encode = (node) => cborg.encode(node, _encodeOptions)
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* @template T
|
|
144
|
+
* @param {ByteView<T> | ArrayBufferView<T>} data
|
|
145
|
+
* @returns {T}
|
|
146
|
+
*/
|
|
147
|
+
export const decode = (data) => cborg.decode(toByteView(data), _decodeOptions)
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# @ipld/dag-json <!-- omit in toc -->
|
|
2
|
+
|
|
3
|
+
[](https://codecov.io/gh/ipld/js-dag-json)
|
|
4
|
+
[](https://github.com/ipld/js-dag-json/actions/workflows/js-test-and-release.yml)
|
|
5
|
+
|
|
6
|
+
> JS implementation of DAG-JSON
|
|
7
|
+
|
|
8
|
+
## Table of contents <!-- omit in toc -->
|
|
9
|
+
|
|
10
|
+
- [Install](#install)
|
|
11
|
+
- [Example](#example)
|
|
12
|
+
- [Usage](#usage)
|
|
13
|
+
- [License](#license)
|
|
14
|
+
- [Contribute](#contribute)
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```console
|
|
19
|
+
$ npm i @ipld/dag-json
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Example
|
|
23
|
+
|
|
24
|
+
```javascript
|
|
25
|
+
import { encode, decode } from '@ipld/dag-json'
|
|
26
|
+
import { CID } from 'multiformats'
|
|
27
|
+
|
|
28
|
+
const obj = {
|
|
29
|
+
x: 1,
|
|
30
|
+
/* CID instances are encoded as links */
|
|
31
|
+
y: [2, 3, CID.parse('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4')],
|
|
32
|
+
z: {
|
|
33
|
+
a: CID.parse('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4'),
|
|
34
|
+
b: null,
|
|
35
|
+
c: 'string'
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let data = encode(obj)
|
|
40
|
+
let decoded = decode(data)
|
|
41
|
+
decoded.y[0] // 2
|
|
42
|
+
CID.asCID(decoded.z.a) // cid instance
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
`@ipld/dag-json` is designed to be used within multiformats but can be used separately. `encode()`, `decode()` are available as exports, as are `name` and `code` to match with the corresponding DAG-JSON [multicodec](https://github.com/multiformats/multicodec/).
|
|
48
|
+
|
|
49
|
+
## License
|
|
50
|
+
|
|
51
|
+
Licensed under either of
|
|
52
|
+
|
|
53
|
+
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
|
|
54
|
+
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
|
|
55
|
+
|
|
56
|
+
## Contribute
|
|
57
|
+
|
|
58
|
+
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.IpldDagJson = factory()}(typeof self !== 'undefined' ? self : this, function () {
|
|
2
|
+
"use strict";var IpldDagJson=(()=>{var Ie=Object.defineProperty;var wr=Object.getOwnPropertyDescriptor;var br=Object.getOwnPropertyNames;var kr=Object.prototype.hasOwnProperty;var Er=(e,t)=>{for(var r in t)Ie(e,r,{get:t[r],enumerable:!0})},_r=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of br(t))!kr.call(e,o)&&o!==r&&Ie(e,o,{get:()=>t[o],enumerable:!(n=wr(t,o))||n.enumerable});return e};var vr=e=>_r(Ie({},"__esModule",{value:!0}),e);var Rn={};Er(Rn,{code:()=>On,decode:()=>yr,encode:()=>xr,format:()=>Fn,name:()=>zn,parse:()=>jn,stringify:()=>Fn});var Ar=["string","number","bigint","symbol"],Ur=["Function","Generator","AsyncGenerator","GeneratorFunction","AsyncGeneratorFunction","AsyncFunction","Observable","Array","Buffer","Object","RegExp","Date","Error","Map","Set","WeakMap","WeakSet","ArrayBuffer","SharedArrayBuffer","DataView","Promise","URL","HTMLElement","Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array","BigInt64Array","BigUint64Array"];function rt(e){if(e===null)return"null";if(e===void 0)return"undefined";if(e===!0||e===!1)return"boolean";let t=typeof e;if(Ar.includes(t))return t;if(t==="function")return"Function";if(Array.isArray(e))return"Array";if(Sr(e))return"Buffer";let r=Tr(e);return r||"Object"}function Sr(e){return e&&e.constructor&&e.constructor.isBuffer&&e.constructor.isBuffer.call(null,e)}function Tr(e){let t=Object.prototype.toString.call(e).slice(8,-1);if(Ur.includes(t))return t}var i=class{constructor(t,r,n){this.major=t,this.majorEncoded=t<<5,this.name=r,this.terminal=n}toString(){return`Type[${this.major}].${this.name}`}compare(t){return this.major<t.major?-1:this.major>t.major?1:0}};i.uint=new i(0,"uint",!0);i.negint=new i(1,"negint",!0);i.bytes=new i(2,"bytes",!0);i.string=new i(3,"string",!0);i.array=new i(4,"array",!1);i.map=new i(5,"map",!1);i.tag=new i(6,"tag",!1);i.float=new i(7,"float",!0);i.false=new i(7,"false",!0);i.true=new i(7,"true",!0);i.null=new i(7,"null",!0);i.undefined=new i(7,"undefined",!0);i.break=new i(7,"break",!0);var c=class{constructor(t,r,n){this.type=t,this.value=r,this.encodedLength=n,this.encodedBytes=void 0,this.byteValue=void 0}toString(){return`Token[${this.type}].${this.value}`}};var G=globalThis.process&&!globalThis.process.browser&&globalThis.Buffer&&typeof globalThis.Buffer.isBuffer=="function",Br=new TextDecoder,Cr=new TextEncoder;function ye(e){return G&&globalThis.Buffer.isBuffer(e)}function ie(e){return e instanceof Uint8Array?ye(e)?new Uint8Array(e.buffer,e.byteOffset,e.byteLength):e:Uint8Array.from(e)}var st=G?(e,t,r)=>r-t>64?globalThis.Buffer.from(e.subarray(t,r)).toString("utf8"):ot(e,t,r):(e,t,r)=>r-t>64?Br.decode(e.subarray(t,r)):ot(e,t,r),ge=G?e=>e.length>64?globalThis.Buffer.from(e):nt(e):e=>e.length>64?Cr.encode(e):nt(e);var J=G?(e,t,r)=>ye(e)?new Uint8Array(e.subarray(t,r)):e.slice(t,r):(e,t,r)=>e.slice(t,r),at=G?(e,t)=>(e=e.map(r=>r instanceof Uint8Array?r:globalThis.Buffer.from(r)),ie(globalThis.Buffer.concat(e,t))):(e,t)=>{let r=new Uint8Array(t),n=0;for(let o of e)n+o.length>r.length&&(o=o.subarray(0,r.length-n)),r.set(o,n),n+=o.length;return r},ct=G?e=>globalThis.Buffer.allocUnsafe(e):e=>new Uint8Array(e);function ut(e,t){if(ye(e)&&ye(t))return e.compare(t);for(let r=0;r<e.length;r++)if(e[r]!==t[r])return e[r]<t[r]?-1:1;return 0}function nt(e){let t=[],r=0;for(let n=0;n<e.length;n++){let o=e.charCodeAt(n);o<128?t[r++]=o:o<2048?(t[r++]=o>>6|192,t[r++]=o&63|128):(o&64512)===55296&&n+1<e.length&&(e.charCodeAt(n+1)&64512)===56320?(o=65536+((o&1023)<<10)+(e.charCodeAt(++n)&1023),t[r++]=o>>18|240,t[r++]=o>>12&63|128,t[r++]=o>>6&63|128,t[r++]=o&63|128):(t[r++]=o>>12|224,t[r++]=o>>6&63|128,t[r++]=o&63|128)}return t}function ot(e,t,r){let n=[];for(;t<r;){let o=e[t],s=null,a=o>239?4:o>223?3:o>191?2:1;if(t+a<=r){let u,p,x,l;switch(a){case 1:o<128&&(s=o);break;case 2:u=e[t+1],(u&192)===128&&(l=(o&31)<<6|u&63,l>127&&(s=l));break;case 3:u=e[t+1],p=e[t+2],(u&192)===128&&(p&192)===128&&(l=(o&15)<<12|(u&63)<<6|p&63,l>2047&&(l<55296||l>57343)&&(s=l));break;case 4:u=e[t+1],p=e[t+2],x=e[t+3],(u&192)===128&&(p&192)===128&&(x&192)===128&&(l=(o&15)<<18|(u&63)<<12|(p&63)<<6|x&63,l>65535&&l<1114112&&(s=l))}}s===null?(s=65533,a=1):s>65535&&(s-=65536,n.push(s>>>10&1023|55296),s=56320|s&1023),n.push(s),t+=a}return Ne(n)}var it=4096;function Ne(e){let t=e.length;if(t<=it)return String.fromCharCode.apply(String,e);let r="",n=0;for(;n<t;)r+=String.fromCharCode.apply(String,e.slice(n,n+=it));return r}var $r=256,se=class{constructor(t=$r){this.chunkSize=t,this.cursor=0,this.maxCursor=-1,this.chunks=[],this._initReuseChunk=null}reset(){this.cursor=0,this.maxCursor=-1,this.chunks.length&&(this.chunks=[]),this._initReuseChunk!==null&&(this.chunks.push(this._initReuseChunk),this.maxCursor=this._initReuseChunk.length-1)}push(t){let r=this.chunks[this.chunks.length-1];if(this.cursor+t.length<=this.maxCursor+1){let o=r.length-(this.maxCursor-this.cursor)-1;r.set(t,o)}else{if(r){let o=r.length-(this.maxCursor-this.cursor)-1;o<r.length&&(this.chunks[this.chunks.length-1]=r.subarray(0,o),this.maxCursor=this.cursor-1)}t.length<64&&t.length<this.chunkSize?(r=ct(this.chunkSize),this.chunks.push(r),this.maxCursor+=r.length,this._initReuseChunk===null&&(this._initReuseChunk=r),r.set(t,0)):(this.chunks.push(t),this.maxCursor+=t.length)}this.cursor+=t.length}toBytes(t=!1){let r;if(this.chunks.length===1){let n=this.chunks[0];t&&this.cursor>n.length/2?(r=this.cursor===n.length?n:n.subarray(0,this.cursor),this._initReuseChunk=null,this.chunks=[]):r=J(n,0,this.cursor)}else r=at(this.chunks,this.cursor);return t&&this.reset(),r}};var d="CBOR decode error:",F="CBOR encode error:",ae=[];ae[23]=1;ae[24]=2;ae[25]=3;ae[26]=5;ae[27]=9;function V(e,t,r){if(e.length-t<r)throw new Error(`${d} not enough data for type`)}var g=[24,256,65536,4294967296,BigInt("18446744073709551616")];function E(e,t,r){V(e,t,1);let n=e[t];if(r.strict===!0&&n<g[0])throw new Error(`${d} integer encoded in more bytes than necessary (strict decode)`);return n}function _(e,t,r){V(e,t,2);let n=e[t]<<8|e[t+1];if(r.strict===!0&&n<g[1])throw new Error(`${d} integer encoded in more bytes than necessary (strict decode)`);return n}function v(e,t,r){V(e,t,4);let n=e[t]*16777216+(e[t+1]<<16)+(e[t+2]<<8)+e[t+3];if(r.strict===!0&&n<g[2])throw new Error(`${d} integer encoded in more bytes than necessary (strict decode)`);return n}function A(e,t,r){V(e,t,8);let n=e[t]*16777216+(e[t+1]<<16)+(e[t+2]<<8)+e[t+3],o=e[t+4]*16777216+(e[t+5]<<16)+(e[t+6]<<8)+e[t+7],s=(BigInt(n)<<BigInt(32))+BigInt(o);if(r.strict===!0&&s<g[3])throw new Error(`${d} integer encoded in more bytes than necessary (strict decode)`);if(s<=Number.MAX_SAFE_INTEGER)return Number(s);if(r.allowBigInt===!0)return s;throw new Error(`${d} integers outside of the safe integer range are not supported`)}function ft(e,t,r,n){return new c(i.uint,E(e,t+1,n),2)}function dt(e,t,r,n){return new c(i.uint,_(e,t+1,n),3)}function pt(e,t,r,n){return new c(i.uint,v(e,t+1,n),5)}function ht(e,t,r,n){return new c(i.uint,A(e,t+1,n),9)}function S(e,t){return w(e,0,t.value)}function w(e,t,r){if(r<g[0]){let n=Number(r);e.push([t|n])}else if(r<g[1]){let n=Number(r);e.push([t|24,n])}else if(r<g[2]){let n=Number(r);e.push([t|25,n>>>8,n&255])}else if(r<g[3]){let n=Number(r);e.push([t|26,n>>>24&255,n>>>16&255,n>>>8&255,n&255])}else{let n=BigInt(r);if(n<g[4]){let o=[t|27,0,0,0,0,0,0,0],s=Number(n&BigInt(4294967295)),a=Number(n>>BigInt(32)&BigInt(4294967295));o[8]=s&255,s=s>>8,o[7]=s&255,s=s>>8,o[6]=s&255,s=s>>8,o[5]=s&255,o[4]=a&255,a=a>>8,o[3]=a&255,a=a>>8,o[2]=a&255,a=a>>8,o[1]=a&255,e.push(o)}else throw new Error(`${d} encountered BigInt larger than allowable range`)}}S.encodedSize=function(t){return w.encodedSize(t.value)};w.encodedSize=function(t){return t<g[0]?1:t<g[1]?2:t<g[2]?3:t<g[3]?5:9};S.compareTokens=function(t,r){return t.value<r.value?-1:t.value>r.value?1:0};function lt(e,t,r,n){return new c(i.negint,-1-E(e,t+1,n),2)}function mt(e,t,r,n){return new c(i.negint,-1-_(e,t+1,n),3)}function xt(e,t,r,n){return new c(i.negint,-1-v(e,t+1,n),5)}var Me=BigInt(-1),yt=BigInt(1);function gt(e,t,r,n){let o=A(e,t+1,n);if(typeof o!="bigint"){let s=-1-o;if(s>=Number.MIN_SAFE_INTEGER)return new c(i.negint,s,9)}if(n.allowBigInt!==!0)throw new Error(`${d} integers outside of the safe integer range are not supported`);return new c(i.negint,Me-BigInt(o),9)}function we(e,t){let r=t.value,n=typeof r=="bigint"?r*Me-yt:r*-1-1;w(e,t.type.majorEncoded,n)}we.encodedSize=function(t){let r=t.value,n=typeof r=="bigint"?r*Me-yt:r*-1-1;return n<g[0]?1:n<g[1]?2:n<g[2]?3:n<g[3]?5:9};we.compareTokens=function(t,r){return t.value<r.value?1:t.value>r.value?-1:0};function ce(e,t,r,n){V(e,t,r+n);let o=J(e,t+r,t+r+n);return new c(i.bytes,o,r+n)}function wt(e,t,r,n){return ce(e,t,1,r)}function bt(e,t,r,n){return ce(e,t,2,E(e,t+1,n))}function kt(e,t,r,n){return ce(e,t,3,_(e,t+1,n))}function Et(e,t,r,n){return ce(e,t,5,v(e,t+1,n))}function _t(e,t,r,n){let o=A(e,t+1,n);if(typeof o=="bigint")throw new Error(`${d} 64-bit integer bytes lengths not supported`);return ce(e,t,9,o)}function be(e){return e.encodedBytes===void 0&&(e.encodedBytes=e.type===i.string?ge(e.value):e.value),e.encodedBytes}function K(e,t){let r=be(t);w(e,t.type.majorEncoded,r.length),e.push(r)}K.encodedSize=function(t){let r=be(t);return w.encodedSize(r.length)+r.length};K.compareTokens=function(t,r){return Nr(be(t),be(r))};function Nr(e,t){return e.length<t.length?-1:e.length>t.length?1:ut(e,t)}function ue(e,t,r,n,o){let s=r+n;V(e,t,s);let a=new c(i.string,st(e,t+r,t+s),s);return o.retainStringBytes===!0&&(a.byteValue=J(e,t+r,t+s)),a}function vt(e,t,r,n){return ue(e,t,1,r,n)}function At(e,t,r,n){return ue(e,t,2,E(e,t+1,n),n)}function Ut(e,t,r,n){return ue(e,t,3,_(e,t+1,n),n)}function St(e,t,r,n){return ue(e,t,5,v(e,t+1,n),n)}function Tt(e,t,r,n){let o=A(e,t+1,n);if(typeof o=="bigint")throw new Error(`${d} 64-bit integer string lengths not supported`);return ue(e,t,9,o,n)}var Bt=K;function X(e,t,r,n){return new c(i.array,n,r)}function Ct(e,t,r,n){return X(e,t,1,r)}function $t(e,t,r,n){return X(e,t,2,E(e,t+1,n))}function It(e,t,r,n){return X(e,t,3,_(e,t+1,n))}function Nt(e,t,r,n){return X(e,t,5,v(e,t+1,n))}function Mt(e,t,r,n){let o=A(e,t+1,n);if(typeof o=="bigint")throw new Error(`${d} 64-bit integer array lengths not supported`);return X(e,t,9,o)}function Dt(e,t,r,n){if(n.allowIndefinite===!1)throw new Error(`${d} indefinite length items not allowed`);return X(e,t,1,1/0)}function ke(e,t){w(e,i.array.majorEncoded,t.value)}ke.compareTokens=S.compareTokens;ke.encodedSize=function(t){return w.encodedSize(t.value)};function Q(e,t,r,n){return new c(i.map,n,r)}function zt(e,t,r,n){return Q(e,t,1,r)}function Ot(e,t,r,n){return Q(e,t,2,E(e,t+1,n))}function Ft(e,t,r,n){return Q(e,t,3,_(e,t+1,n))}function Vt(e,t,r,n){return Q(e,t,5,v(e,t+1,n))}function jt(e,t,r,n){let o=A(e,t+1,n);if(typeof o=="bigint")throw new Error(`${d} 64-bit integer map lengths not supported`);return Q(e,t,9,o)}function Pt(e,t,r,n){if(n.allowIndefinite===!1)throw new Error(`${d} indefinite length items not allowed`);return Q(e,t,1,1/0)}function Ee(e,t){w(e,i.map.majorEncoded,t.value)}Ee.compareTokens=S.compareTokens;Ee.encodedSize=function(t){return w.encodedSize(t.value)};function Rt(e,t,r,n){return new c(i.tag,r,1)}function Lt(e,t,r,n){return new c(i.tag,E(e,t+1,n),2)}function qt(e,t,r,n){return new c(i.tag,_(e,t+1,n),3)}function Wt(e,t,r,n){return new c(i.tag,v(e,t+1,n),5)}function Gt(e,t,r,n){return new c(i.tag,A(e,t+1,n),9)}function _e(e,t){w(e,i.tag.majorEncoded,t.value)}_e.compareTokens=S.compareTokens;_e.encodedSize=function(t){return w.encodedSize(t.value)};var Vr=20,jr=21,Pr=22,Rr=23;function Jt(e,t,r,n){if(n.allowUndefined===!1)throw new Error(`${d} undefined values are not supported`);return n.coerceUndefinedToNull===!0?new c(i.null,null,1):new c(i.undefined,void 0,1)}function Kt(e,t,r,n){if(n.allowIndefinite===!1)throw new Error(`${d} indefinite length items not allowed`);return new c(i.break,void 0,1)}function De(e,t,r){if(r){if(r.allowNaN===!1&&Number.isNaN(e))throw new Error(`${d} NaN values are not supported`);if(r.allowInfinity===!1&&(e===1/0||e===-1/0))throw new Error(`${d} Infinity values are not supported`)}return new c(i.float,e,t)}function Xt(e,t,r,n){return De(ze(e,t+1),3,n)}function Qt(e,t,r,n){return De(Oe(e,t+1),5,n)}function Ht(e,t,r,n){return De(tr(e,t+1),9,n)}function ve(e,t,r){let n=t.value;if(n===!1)e.push([i.float.majorEncoded|Vr]);else if(n===!0)e.push([i.float.majorEncoded|jr]);else if(n===null)e.push([i.float.majorEncoded|Pr]);else if(n===void 0)e.push([i.float.majorEncoded|Rr]);else{let o,s=!1;(!r||r.float64!==!0)&&(Yt(n),o=ze($,1),n===o||Number.isNaN(n)?($[0]=249,e.push($.slice(0,3)),s=!0):(er(n),o=Oe($,1),n===o&&($[0]=250,e.push($.slice(0,5)),s=!0))),s||(Lr(n),o=tr($,1),$[0]=251,e.push($.slice(0,9)))}}ve.encodedSize=function(t,r){let n=t.value;if(n===!1||n===!0||n===null||n===void 0)return 1;if(!r||r.float64!==!0){Yt(n);let o=ze($,1);if(n===o||Number.isNaN(n))return 3;if(er(n),o=Oe($,1),n===o)return 5}return 9};var Zt=new ArrayBuffer(9),T=new DataView(Zt,1),$=new Uint8Array(Zt,0);function Yt(e){if(e===1/0)T.setUint16(0,31744,!1);else if(e===-1/0)T.setUint16(0,64512,!1);else if(Number.isNaN(e))T.setUint16(0,32256,!1);else{T.setFloat32(0,e);let t=T.getUint32(0),r=(t&2139095040)>>23,n=t&8388607;if(r===255)T.setUint16(0,31744,!1);else if(r===0)T.setUint16(0,(e&2147483648)>>16|n>>13,!1);else{let o=r-127;o<-24?T.setUint16(0,0):o<-14?T.setUint16(0,(t&2147483648)>>16|1<<24+o,!1):T.setUint16(0,(t&2147483648)>>16|o+15<<10|n>>13,!1)}}}function ze(e,t){if(e.length-t<2)throw new Error(`${d} not enough data for float16`);let r=(e[t]<<8)+e[t+1];if(r===31744)return 1/0;if(r===64512)return-1/0;if(r===32256)return NaN;let n=r>>10&31,o=r&1023,s;return n===0?s=o*2**-24:n!==31?s=(o+1024)*2**(n-25):s=o===0?1/0:NaN,r&32768?-s:s}function er(e){T.setFloat32(0,e,!1)}function Oe(e,t){if(e.length-t<4)throw new Error(`${d} not enough data for float32`);let r=(e.byteOffset||0)+t;return new DataView(e.buffer,r,4).getFloat32(0,!1)}function Lr(e){T.setFloat64(0,e,!1)}function tr(e,t){if(e.length-t<8)throw new Error(`${d} not enough data for float64`);let r=(e.byteOffset||0)+t;return new DataView(e.buffer,r,8).getFloat64(0,!1)}ve.compareTokens=S.compareTokens;function h(e,t,r){throw new Error(`${d} encountered invalid minor (${r}) for major ${e[t]>>>5}`)}function Ae(e){return()=>{throw new Error(`${d} ${e}`)}}var f=[];for(let e=0;e<=23;e++)f[e]=h;f[24]=ft;f[25]=dt;f[26]=pt;f[27]=ht;f[28]=h;f[29]=h;f[30]=h;f[31]=h;for(let e=32;e<=55;e++)f[e]=h;f[56]=lt;f[57]=mt;f[58]=xt;f[59]=gt;f[60]=h;f[61]=h;f[62]=h;f[63]=h;for(let e=64;e<=87;e++)f[e]=wt;f[88]=bt;f[89]=kt;f[90]=Et;f[91]=_t;f[92]=h;f[93]=h;f[94]=h;f[95]=Ae("indefinite length bytes/strings are not supported");for(let e=96;e<=119;e++)f[e]=vt;f[120]=At;f[121]=Ut;f[122]=St;f[123]=Tt;f[124]=h;f[125]=h;f[126]=h;f[127]=Ae("indefinite length bytes/strings are not supported");for(let e=128;e<=151;e++)f[e]=Ct;f[152]=$t;f[153]=It;f[154]=Nt;f[155]=Mt;f[156]=h;f[157]=h;f[158]=h;f[159]=Dt;for(let e=160;e<=183;e++)f[e]=zt;f[184]=Ot;f[185]=Ft;f[186]=Vt;f[187]=jt;f[188]=h;f[189]=h;f[190]=h;f[191]=Pt;for(let e=192;e<=215;e++)f[e]=Rt;f[216]=Lt;f[217]=qt;f[218]=Wt;f[219]=Gt;f[220]=h;f[221]=h;f[222]=h;f[223]=h;for(let e=224;e<=243;e++)f[e]=Ae("simple values are not supported");f[244]=h;f[245]=h;f[246]=h;f[247]=Jt;f[248]=Ae("simple values are not supported");f[249]=Xt;f[250]=Qt;f[251]=Ht;f[252]=h;f[253]=h;f[254]=h;f[255]=Kt;var I=[];for(let e=0;e<24;e++)I[e]=new c(i.uint,e,1);for(let e=-1;e>=-24;e--)I[31-e]=new c(i.negint,e,1);I[64]=new c(i.bytes,new Uint8Array(0),1);I[96]=new c(i.string,"",1);I[128]=new c(i.array,0,1);I[160]=new c(i.map,0,1);I[244]=new c(i.false,!1,1);I[245]=new c(i.true,!0,1);I[246]=new c(i.null,null,1);function Wr(){let e=[];return e[i.uint.major]=S,e[i.negint.major]=we,e[i.bytes.major]=K,e[i.string.major]=Bt,e[i.array.major]=ke,e[i.map.major]=Ee,e[i.tag.major]=_e,e[i.float.major]=ve,e}var zo=Wr(),Fe=new se,Se=class e{constructor(t,r){this.obj=t,this.parent=r}includes(t){let r=this;do if(r.obj===t)return!0;while(r=r.parent);return!1}static createCheck(t,r){if(t&&t.includes(r))throw new Error(`${F} object contains circular references`);return new e(r,t)}},R={null:new c(i.null,null),undefined:new c(i.undefined,void 0),true:new c(i.true,!0),false:new c(i.false,!1),emptyArray:new c(i.array,0),emptyMap:new c(i.map,0)},L={number(e,t,r,n){return!Number.isInteger(e)||!Number.isSafeInteger(e)?new c(i.float,e):e>=0?new c(i.uint,e):new c(i.negint,e)},bigint(e,t,r,n){return e>=BigInt(0)?new c(i.uint,e):new c(i.negint,e)},Uint8Array(e,t,r,n){return new c(i.bytes,e)},string(e,t,r,n){return new c(i.string,e)},boolean(e,t,r,n){return e?R.true:R.false},null(e,t,r,n){return R.null},undefined(e,t,r,n){return R.undefined},ArrayBuffer(e,t,r,n){return new c(i.bytes,new Uint8Array(e))},DataView(e,t,r,n){return new c(i.bytes,new Uint8Array(e.buffer,e.byteOffset,e.byteLength))},Array(e,t,r,n){if(!e.length)return r.addBreakTokens===!0?[R.emptyArray,new c(i.break)]:R.emptyArray;n=Se.createCheck(n,e);let o=[],s=0;for(let a of e)o[s++]=Ue(a,r,n);return r.addBreakTokens?[new c(i.array,e.length),o,new c(i.break)]:[new c(i.array,e.length),o]},Object(e,t,r,n){let o=t!=="Object",s=o?e.keys():Object.keys(e),a=o?e.size:s.length;if(!a)return r.addBreakTokens===!0?[R.emptyMap,new c(i.break)]:R.emptyMap;n=Se.createCheck(n,e);let u=[],p=0;for(let x of s)u[p++]=[Ue(x,r,n),Ue(o?e.get(x):e[x],r,n)];return Gr(u,r),r.addBreakTokens?[new c(i.map,a),u,new c(i.break)]:[new c(i.map,a),u]}};L.Map=L.Object;L.Buffer=L.Uint8Array;for(let e of"Uint8Clamped Uint16 Uint32 Int8 Int16 Int32 BigUint64 BigInt64 Float32 Float64".split(" "))L[`${e}Array`]=L.DataView;function Ue(e,t={},r){let n=rt(e),o=t&&t.typeEncoders&&t.typeEncoders[n]||L[n];if(typeof o=="function"){let a=o(e,n,t,r);if(a!=null)return a}let s=L[n];if(!s)throw new Error(`${F} unsupported type: ${n}`);return s(e,n,t,r)}function Gr(e,t){t.mapSorter&&e.sort(t.mapSorter)}function rr(e,t,r,n){if(Array.isArray(t))for(let o of t)rr(e,o,r,n);else r[t.type.major](e,t,n)}function nr(e,t,r){let n=Ue(e,r);if(!Array.isArray(n)&&r.quickEncodeToken){let o=r.quickEncodeToken(n);if(o)return o;let s=t[n.type.major];if(s.encodedSize){let a=s.encodedSize(n,r),u=new se(a);if(s(u,n,r),u.chunks.length!==1)throw new Error(`Unexpected error: pre-calculated length for ${n} was wrong`);return ie(u.chunks[0])}}return Fe.reset(),rr(Fe,n,t,r),Fe.toBytes(!0)}var Jr={strict:!1,allowIndefinite:!0,allowUndefined:!0,allowBigInt:!0},Te=class{constructor(t,r={}){this._pos=0,this.data=t,this.options=r}pos(){return this._pos}done(){return this._pos>=this.data.length}next(){let t=this.data[this._pos],r=I[t];if(r===void 0){let n=f[t];if(!n)throw new Error(`${d} no decoder for major type ${t>>>5} (byte 0x${t.toString(16).padStart(2,"0")})`);let o=t&31;r=n(this.data,this._pos,o,this.options)}return this._pos+=r.encodedLength,r}},fe=Symbol.for("DONE"),Be=Symbol.for("BREAK");function Kr(e,t,r){let n=[];for(let o=0;o<e.value;o++){let s=H(t,r);if(s===Be){if(e.value===1/0)break;throw new Error(`${d} got unexpected break to lengthed array`)}if(s===fe)throw new Error(`${d} found array but not enough entries (got ${o}, expected ${e.value})`);n[o]=s}return n}function Xr(e,t,r){let n=r.useMaps===!0,o=n?void 0:{},s=n?new Map:void 0;for(let a=0;a<e.value;a++){let u=H(t,r);if(u===Be){if(e.value===1/0)break;throw new Error(`${d} got unexpected break to lengthed map`)}if(u===fe)throw new Error(`${d} found map but not enough entries (got ${a} [no key], expected ${e.value})`);if(n!==!0&&typeof u!="string")throw new Error(`${d} non-string keys not supported (got ${typeof u})`);if(r.rejectDuplicateMapKeys===!0&&(n&&s.has(u)||!n&&u in o))throw new Error(`${d} found repeat map key "${u}"`);let p=H(t,r);if(p===fe)throw new Error(`${d} found map but not enough entries (got ${a} [no value], expected ${e.value})`);n?s.set(u,p):o[u]=p}return n?s:o}function H(e,t){if(e.done())return fe;let r=e.next();if(r.type===i.break)return Be;if(r.type.terminal)return r.value;if(r.type===i.array)return Kr(r,e,t);if(r.type===i.map)return Xr(r,e,t);if(r.type===i.tag){if(t.tags&&typeof t.tags[r.value]=="function"){let n=H(e,t);return t.tags[r.value](n)}throw new Error(`${d} tag not supported (${r.value})`)}throw new Error("unsupported")}function Ve(e,t){if(!(e instanceof Uint8Array))throw new Error(`${d} data to decode must be a Uint8Array`);t=Object.assign({},Jr,t);let r=t.tokenizer||new Te(e,t),n=H(r,t);if(n===fe)throw new Error(`${d} did not find any content to decode`);if(n===Be)throw new Error(`${d} got unexpected break`);return[n,e.subarray(r.pos())]}function je(e,t){let[r,n]=Ve(e,t);if(n.length>0)throw new Error(`${d} too many terminals, data makes no sense`);return r}var Pe=class extends Array{constructor(){super(),this.inRecursive=[]}prefix(t){let r=this.inRecursive[this.inRecursive.length-1];r&&(r.type===i.array&&(r.elements++,r.elements!==1&&t.push([44])),r.type===i.map&&(r.elements++,r.elements!==1&&(r.elements%2===1?t.push([44]):t.push([58]))))}[i.uint.major](t,r){this.prefix(t);let n=String(r.value),o=[];for(let s=0;s<n.length;s++)o[s]=n.charCodeAt(s);t.push(o)}[i.negint.major](t,r){this[i.uint.major](t,r)}[i.bytes.major](t,r){throw new Error(`${F} unsupported type: Uint8Array`)}[i.string.major](t,r){this.prefix(t);let n=ge(JSON.stringify(r.value));t.push(n.length>32?ie(n):n)}[i.array.major](t,r){this.prefix(t),this.inRecursive.push({type:i.array,elements:0}),t.push([91])}[i.map.major](t,r){this.prefix(t),this.inRecursive.push({type:i.map,elements:0}),t.push([123])}[i.tag.major](t,r){}[i.float.major](t,r){if(r.type.name==="break"){let a=this.inRecursive.pop();if(a){if(a.type===i.array)t.push([93]);else if(a.type===i.map)t.push([125]);else throw new Error("Unexpected recursive type; this should not happen!");return}throw new Error("Unexpected break; this should not happen!")}if(r.value===void 0)throw new Error(`${F} unsupported type: undefined`);if(this.prefix(t),r.type.name==="true"){t.push([116,114,117,101]);return}else if(r.type.name==="false"){t.push([102,97,108,115,101]);return}else if(r.type.name==="null"){t.push([110,117,108,108]);return}let n=String(r.value),o=[],s=!1;for(let a=0;a<n.length;a++)o[a]=n.charCodeAt(a),!s&&(o[a]===46||o[a]===101||o[a]===69)&&(s=!0);s||(o.push(46),o.push(48)),t.push(o)}};function Qr(e,t){if(Array.isArray(e[0])||Array.isArray(t[0]))throw new Error(`${F} complex map keys are not supported`);let r=e[0],n=t[0];if(r.type!==i.string||n.type!==i.string)throw new Error(`${F} non-string map keys are not supported`);if(r<n)return-1;if(r>n)return 1;throw new Error(`${F} unexpected duplicate map keys, this is not supported`)}var Hr={addBreakTokens:!0,mapSorter:Qr};function Re(e,t){return t=Object.assign({},Hr,t),nr(e,new Pe,t)}var Z=class{constructor(t,r={}){this._pos=0,this.data=t,this.options=r,this.modeStack=["value"],this.lastToken=""}pos(){return this._pos}done(){return this._pos>=this.data.length}ch(){return this.data[this._pos]}currentMode(){return this.modeStack[this.modeStack.length-1]}skipWhitespace(){let t=this.ch();for(;t===32||t===9||t===13||t===10;)t=this.data[++this._pos]}expect(t){if(this.data.length-this._pos<t.length)throw new Error(`${d} unexpected end of input at position ${this._pos}`);for(let r=0;r<t.length;r++)if(this.data[this._pos++]!==t[r])throw new Error(`${d} unexpected token at position ${this._pos}, expected to find '${String.fromCharCode(...t)}'`)}parseNumber(){let t=this._pos,r=!1,n=!1,o=u=>{for(;!this.done();){let p=this.ch();if(u.includes(p))this._pos++;else break}};if(this.ch()===45&&(r=!0,this._pos++),this.ch()===48)if(this._pos++,this.ch()===46)this._pos++,n=!0;else return new c(i.uint,0,this._pos-t);if(o([48,49,50,51,52,53,54,55,56,57]),r&&this._pos===t+1)throw new Error(`${d} unexpected token at position ${this._pos}`);if(!this.done()&&this.ch()===46){if(n)throw new Error(`${d} unexpected token at position ${this._pos}`);n=!0,this._pos++,o([48,49,50,51,52,53,54,55,56,57])}!this.done()&&(this.ch()===101||this.ch()===69)&&(n=!0,this._pos++,!this.done()&&(this.ch()===43||this.ch()===45)&&this._pos++,o([48,49,50,51,52,53,54,55,56,57]));let s=String.fromCharCode.apply(null,this.data.subarray(t,this._pos)),a=parseFloat(s);return n?new c(i.float,a,this._pos-t):this.options.allowBigInt!==!0||Number.isSafeInteger(a)?new c(a>=0?i.uint:i.negint,a,this._pos-t):new c(a>=0?i.uint:i.negint,BigInt(s),this._pos-t)}parseString(){if(this.ch()!==34)throw new Error(`${d} unexpected character at position ${this._pos}; this shouldn't happen`);this._pos++;for(let s=this._pos,a=0;s<this.data.length&&a<65536;s++,a++){let u=this.data[s];if(u===92||u<32||u>=128)break;if(u===34){let p=String.fromCharCode.apply(null,this.data.subarray(this._pos,s));return this._pos=s+1,new c(i.string,p,a)}}let t=this._pos,r=[],n=()=>{if(this._pos+4>=this.data.length)throw new Error(`${d} unexpected end of unicode escape sequence at position ${this._pos}`);let s=0;for(let a=0;a<4;a++){let u=this.ch();if(u>=48&&u<=57)u-=48;else if(u>=97&&u<=102)u=u-97+10;else if(u>=65&&u<=70)u=u-65+10;else throw new Error(`${d} unexpected unicode escape character at position ${this._pos}`);s=s*16+u,this._pos++}return s},o=()=>{let s=this.ch(),a=null,u=s>239?4:s>223?3:s>191?2:1;if(this._pos+u>this.data.length)throw new Error(`${d} unexpected unicode sequence at position ${this._pos}`);let p,x,l,y;switch(u){case 1:s<128&&(a=s);break;case 2:p=this.data[this._pos+1],(p&192)===128&&(y=(s&31)<<6|p&63,y>127&&(a=y));break;case 3:p=this.data[this._pos+1],x=this.data[this._pos+2],(p&192)===128&&(x&192)===128&&(y=(s&15)<<12|(p&63)<<6|x&63,y>2047&&(y<55296||y>57343)&&(a=y));break;case 4:p=this.data[this._pos+1],x=this.data[this._pos+2],l=this.data[this._pos+3],(p&192)===128&&(x&192)===128&&(l&192)===128&&(y=(s&15)<<18|(p&63)<<12|(x&63)<<6|l&63,y>65535&&y<1114112&&(a=y))}a===null?(a=65533,u=1):a>65535&&(a-=65536,r.push(a>>>10&1023|55296),a=56320|a&1023),r.push(a),this._pos+=u};for(;!this.done();){let s=this.ch(),a;switch(s){case 92:if(this._pos++,this.done())throw new Error(`${d} unexpected string termination at position ${this._pos}`);switch(a=this.ch(),this._pos++,a){case 34:case 39:case 92:case 47:r.push(a);break;case 98:r.push(8);break;case 116:r.push(9);break;case 110:r.push(10);break;case 102:r.push(12);break;case 114:r.push(13);break;case 117:r.push(n());break;default:throw new Error(`${d} unexpected string escape character at position ${this._pos}`)}break;case 34:return this._pos++,new c(i.string,Ne(r),this._pos-t);default:if(s<32)throw new Error(`${d} invalid control character at position ${this._pos}`);s<128?(r.push(s),this._pos++):o()}}throw new Error(`${d} unexpected end of string at position ${this._pos}`)}parseValue(){switch(this.ch()){case 123:return this.modeStack.push("obj-start"),this._pos++,new c(i.map,1/0,1);case 91:return this.modeStack.push("array-start"),this._pos++,new c(i.array,1/0,1);case 34:return this.parseString();case 110:return this.expect([110,117,108,108]),new c(i.null,null,4);case 102:return this.expect([102,97,108,115,101]),new c(i.false,!1,5);case 116:return this.expect([116,114,117,101]),new c(i.true,!0,4);case 45:case 48:case 49:case 50:case 51:case 52:case 53:case 54:case 55:case 56:case 57:return this.parseNumber();default:throw new Error(`${d} unexpected character at position ${this._pos}`)}}next(){switch(this.skipWhitespace(),this.currentMode()){case"value":return this.modeStack.pop(),this.parseValue();case"array-value":{if(this.modeStack.pop(),this.ch()===93)return this._pos++,this.skipWhitespace(),new c(i.break,void 0,1);if(this.ch()!==44)throw new Error(`${d} unexpected character at position ${this._pos}, was expecting array delimiter but found '${String.fromCharCode(this.ch())}'`);return this._pos++,this.modeStack.push("array-value"),this.skipWhitespace(),this.parseValue()}case"array-start":return this.modeStack.pop(),this.ch()===93?(this._pos++,this.skipWhitespace(),new c(i.break,void 0,1)):(this.modeStack.push("array-value"),this.skipWhitespace(),this.parseValue());case"obj-key":if(this.ch()===125)return this.modeStack.pop(),this._pos++,this.skipWhitespace(),new c(i.break,void 0,1);if(this.ch()!==44)throw new Error(`${d} unexpected character at position ${this._pos}, was expecting object delimiter but found '${String.fromCharCode(this.ch())}'`);this._pos++,this.skipWhitespace();case"obj-start":{if(this.modeStack.pop(),this.ch()===125)return this._pos++,this.skipWhitespace(),new c(i.break,void 0,1);let t=this.parseString();if(this.skipWhitespace(),this.ch()!==58)throw new Error(`${d} unexpected character at position ${this._pos}, was expecting key/value delimiter ':' but found '${String.fromCharCode(this.ch())}'`);return this._pos++,this.modeStack.push("obj-value"),t}case"obj-value":return this.modeStack.pop(),this.modeStack.push("obj-key"),this.skipWhitespace(),this.parseValue();default:throw new Error(`${d} unexpected parse state at position ${this._pos}; this shouldn't happen`)}}};function Le(e,t){return t=Object.assign({tokenizer:new Z(e,t)},t),je(e,t)}var si=new Uint8Array(0);function or(e,t){if(e===t)return!0;if(e.byteLength!==t.byteLength)return!1;for(let r=0;r<e.byteLength;r++)if(e[r]!==t[r])return!1;return!0}function Y(e){if(e instanceof Uint8Array&&e.constructor.name==="Uint8Array")return e;if(e instanceof ArrayBuffer)return new Uint8Array(e);if(ArrayBuffer.isView(e))return new Uint8Array(e.buffer,e.byteOffset,e.byteLength);throw new Error("Unknown type, must be binary type")}function en(e,t){if(e.length>=255)throw new TypeError("Alphabet too long");for(var r=new Uint8Array(256),n=0;n<r.length;n++)r[n]=255;for(var o=0;o<e.length;o++){var s=e.charAt(o),a=s.charCodeAt(0);if(r[a]!==255)throw new TypeError(s+" is ambiguous");r[a]=o}var u=e.length,p=e.charAt(0),x=Math.log(u)/Math.log(256),l=Math.log(256)/Math.log(u);function y(m){if(m instanceof Uint8Array||(ArrayBuffer.isView(m)?m=new Uint8Array(m.buffer,m.byteOffset,m.byteLength):Array.isArray(m)&&(m=Uint8Array.from(m))),!(m instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(m.length===0)return"";for(var k=0,q=0,B=0,M=m.length;B!==M&&m[B]===0;)B++,k++;for(var D=(M-B)*l+1>>>0,U=new Uint8Array(D);B!==M;){for(var z=m[B],P=0,C=D-1;(z!==0||P<q)&&C!==-1;C--,P++)z+=256*U[C]>>>0,U[C]=z%u>>>0,z=z/u>>>0;if(z!==0)throw new Error("Non-zero carry");q=P,B++}for(var O=D-q;O!==D&&U[O]===0;)O++;for(var xe=p.repeat(k);O<D;++O)xe+=e.charAt(U[O]);return xe}function me(m){if(typeof m!="string")throw new TypeError("Expected String");if(m.length===0)return new Uint8Array;var k=0;if(m[k]!==" "){for(var q=0,B=0;m[k]===p;)q++,k++;for(var M=(m.length-k)*x+1>>>0,D=new Uint8Array(M);m[k];){var U=r[m.charCodeAt(k)];if(U===255)return;for(var z=0,P=M-1;(U!==0||z<B)&&P!==-1;P--,z++)U+=u*D[P]>>>0,D[P]=U%256>>>0,U=U/256>>>0;if(U!==0)throw new Error("Non-zero carry");B=z,k++}if(m[k]!==" "){for(var C=M-B;C!==M&&D[C]===0;)C++;for(var O=new Uint8Array(q+(M-C)),xe=q;C!==M;)O[xe++]=D[C++];return O}}}function gr(m){var k=me(m);if(k)return k;throw new Error(`Non-${t} character`)}return{encode:y,decodeUnsafe:me,decode:gr}}var tn=en,rn=tn,ir=rn;var qe=class{name;prefix;baseEncode;constructor(t,r,n){this.name=t,this.prefix=r,this.baseEncode=n}encode(t){if(t instanceof Uint8Array)return`${this.prefix}${this.baseEncode(t)}`;throw Error("Unknown type, must be binary type")}},We=class{name;prefix;baseDecode;prefixCodePoint;constructor(t,r,n){this.name=t,this.prefix=r;let o=r.codePointAt(0);if(o===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=o,this.baseDecode=n}decode(t){if(typeof t=="string"){if(t.codePointAt(0)!==this.prefixCodePoint)throw Error(`Unable to decode multibase string ${JSON.stringify(t)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);return this.baseDecode(t.slice(this.prefix.length))}else throw Error("Can only multibase decode strings")}or(t){return sr(this,t)}},Ge=class{decoders;constructor(t){this.decoders=t}or(t){return sr(this,t)}decode(t){let r=t[0],n=this.decoders[r];if(n!=null)return n.decode(t);throw RangeError(`Unable to decode multibase string ${JSON.stringify(t)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}};function sr(e,t){return new Ge({...(e.decoders ?? {[e.prefix]:e}),...(t.decoders ?? {[t.prefix]:t})});}var Je=class{name;prefix;baseEncode;baseDecode;encoder;decoder;constructor(t,r,n,o){this.name=t,this.prefix=r,this.baseEncode=n,this.baseDecode=o,this.encoder=new qe(t,r,n),this.decoder=new We(t,r,o)}encode(t){return this.encoder.encode(t)}decode(t){return this.decoder.decode(t)}};function ar({name:e,prefix:t,encode:r,decode:n}){return new Je(e,t,r,n)}function ee({name:e,prefix:t,alphabet:r}){let{encode:n,decode:o}=ir(r,e);return ar({prefix:t,name:e,encode:n,decode:s=>Y(o(s))})}function nn(e,t,r,n){let o={};for(let l=0;l<t.length;++l)o[t[l]]=l;let s=e.length;for(;e[s-1]==="=";)--s;let a=new Uint8Array(s*r/8|0),u=0,p=0,x=0;for(let l=0;l<s;++l){let y=o[e[l]];if(y===void 0)throw new SyntaxError(`Non-${n} character`);p=p<<r|y,u+=r,u>=8&&(u-=8,a[x++]=255&p>>u)}if(u>=r||(255&p<<8-u)!==0)throw new SyntaxError("Unexpected end of data");return a}function on(e,t,r){let n=t[t.length-1]==="=",o=(1<<r)-1,s="",a=0,u=0;for(let p=0;p<e.length;++p)for(u=u<<8|e[p],a+=8;a>r;)a-=r,s+=t[o&u>>a];if(a!==0&&(s+=t[o&u<<r-a]),n)for(;(s.length*r&7)!==0;)s+="=";return s}function b({name:e,prefix:t,bitsPerChar:r,alphabet:n}){return ar({prefix:t,name:e,encode(o){return on(o,n,r)},decode(o){return nn(o,n,r,e)}})}var de=b({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),pi=b({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),hi=b({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),li=b({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),mi=b({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),xi=b({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),yi=b({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),gi=b({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),wi=b({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var Ce=ee({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),Ei=ee({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"});var j=ee({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),Ai=ee({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var sn=fr,cr=128,an=127,cn=~an,un=Math.pow(2,31);function fr(e,t,r){t=t||[],r=r||0;for(var n=r;e>=un;)t[r++]=e&255|cr,e/=128;for(;e&cn;)t[r++]=e&255|cr,e>>>=7;return t[r]=e|0,fr.bytes=r-n+1,t}var fn=Ke,dn=128,ur=127;function Ke(e,n){var r=0,n=n||0,o=0,s=n,a,u=e.length;do{if(s>=u)throw (Ke.bytes=0, new RangeError("Could not decode varint"));a=e[s++],r+=o<28?(a&ur)<<o:(a&ur)*Math.pow(2,o),o+=7}while(a>=dn);return Ke.bytes=s-n,r}var pn=Math.pow(2,7),hn=Math.pow(2,14),ln=Math.pow(2,21),mn=Math.pow(2,28),xn=Math.pow(2,35),yn=Math.pow(2,42),gn=Math.pow(2,49),wn=Math.pow(2,56),bn=Math.pow(2,63),kn=function(e){return e<pn?1:e<hn?2:e<ln?3:e<mn?4:e<xn?5:e<yn?6:e<gn?7:e<wn?8:e<bn?9:10},En={encode:sn,decode:fn,encodingLength:kn},_n=En,pe=_n;function he(e,t=0){return[pe.decode(e,t),pe.decode.bytes]}function te(e,t,r=0){return pe.encode(e,t,r),t}function re(e){return pe.encodingLength(e)}function Qe(e,t){let r=t.byteLength,n=re(e),o=n+re(r),s=new Uint8Array(o+r);return te(e,s,0),te(r,s,n),s.set(t,o),new ne(e,r,t,s)}function dr(e){let t=Y(e),[r,n]=he(t),[o,s]=he(t.subarray(n)),a=t.subarray(n+s);if(a.byteLength!==o)throw new Error("Incorrect length");return new ne(r,o,a,t)}function pr(e,t){if(e===t)return!0;{let r=t;return e.code===r.code&&e.size===r.size&&r.bytes instanceof Uint8Array&&or(e.bytes,r.bytes)}}var ne=class{code;size;digest;bytes;constructor(t,r,n,o){this.code=t,this.size=r,this.digest=n,this.bytes=o}};function hr(e,t){let{bytes:r,version:n}=e;switch(n){case 0:return An(r,Ze(e),t??j.encoder);default:return Un(r,Ze(e),t??de.encoder)}}var lr=new WeakMap;function Ze(e){let t=lr.get(e);if(t==null){let r=new Map;return lr.set(e,r),r}return t}var oe=class e{code;version;multihash;bytes;"/";constructor(t,r,n,o){this.code=r,this.version=t,this.multihash=n,this.bytes=o,this["/"]=o}get asCID(){return this}get byteOffset(){return this.bytes.byteOffset}get byteLength(){return this.bytes.byteLength}toV0(){switch(this.version){case 0:return this;case 1:{let{code:t,multihash:r}=this;if(t!==le)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(r.code!==Sn)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return e.createV0(r)}default:throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`)}}toV1(){switch(this.version){case 0:{let{code:t,digest:r}=this.multihash,n=Qe(t,r);return e.createV1(this.code,n)}case 1:return this;default:throw Error(`Can not convert CID version ${this.version} to version 1. This is a bug please report`)}}equals(t){return e.equals(this,t)}static equals(t,r){let n=r;return n!=null&&t.code===n.code&&t.version===n.version&&pr(t.multihash,n.multihash)}toString(t){return hr(this,t)}toJSON(){return{"/":hr(this)}}link(){return this}[Symbol.toStringTag]="CID";[Symbol.for("nodejs.util.inspect.custom")](){return`CID(${this.toString()})`}static asCID(t){if(t==null)return null;let r=t;if(r instanceof e)return r;if(r["/"]!=null&&r["/"]===r.bytes||r.asCID===r){let{version:n,code:o,multihash:s,bytes:a}=r;return new e(n,o,s,a??mr(n,o,s.bytes))}else if(r[Tn]===!0){let{version:n,multihash:o,code:s}=r,a=dr(o);return e.create(n,s,a)}else return null}static create(t,r,n){if(typeof r!="number")throw new Error("String codecs are no longer supported");if(!(n.bytes instanceof Uint8Array))throw new Error("Invalid digest");switch(t){case 0:{if(r!==le)throw new Error(`Version 0 CID must use dag-pb (code: ${le}) block encoding`);return new e(t,r,n,n.bytes)}case 1:{let o=mr(t,r,n.bytes);return new e(t,r,n,o)}default:throw new Error("Invalid version")}}static createV0(t){return e.create(0,le,t)}static createV1(t,r){return e.create(1,t,r)}static decode(t){let[r,n]=e.decodeFirst(t);if(n.length!==0)throw new Error("Incorrect length");return r}static decodeFirst(t){let r=e.inspectBytes(t),n=r.size-r.multihashSize,o=Y(t.subarray(n,n+r.multihashSize));if(o.byteLength!==r.multihashSize)throw new Error("Incorrect length");let s=o.subarray(r.multihashSize-r.digestSize),a=new ne(r.multihashCode,r.digestSize,s,o);return[r.version===0?e.createV0(a):e.createV1(r.codec,a),t.subarray(r.size)]}static inspectBytes(t){let r=0,n=()=>{let[y,me]=he(t.subarray(r));return r+=me,y},o=n(),s=le;if(o===18?(o=0,r=0):s=n(),o!==0&&o!==1)throw new RangeError(`Invalid CID version ${o}`);let a=r,u=n(),p=n(),x=r+p,l=x-a;return{version:o,codec:s,multihashCode:u,digestSize:p,multihashSize:l,size:x}}static parse(t,r){let[n,o]=vn(t,r),s=e.decode(o);if(s.version===0&&t[0]!=="Q")throw Error("Version 0 CID string must not include multibase prefix");return Ze(s).set(n,t),s}};function vn(e,t){switch(e[0]){case"Q":{let r=t??j;return[j.prefix,r.decode(`${j.prefix}${e}`)]}case j.prefix:{let r=t??j;return[j.prefix,r.decode(e)]}case de.prefix:{let r=t??de;return[de.prefix,r.decode(e)]}case Ce.prefix:{let r=t??Ce;return[Ce.prefix,r.decode(e)]}default:{if(t==null)throw Error("To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}}function An(e,t,r){let{prefix:n}=r;if(n!==j.prefix)throw Error(`Cannot string encode V0 in ${r.name} encoding`);let o=t.get(n);if(o==null){let s=r.encode(e).slice(1);return t.set(n,s),s}else return o}function Un(e,t,r){let{prefix:n}=r,o=t.get(n);if(o==null){let s=r.encode(e);return t.set(n,s),s}else return o}var le=112,Sn=18;function mr(e,t,r){let n=re(e),o=n+re(t),s=new Uint8Array(o+r.byteLength);return te(e,s,0),te(t,s,n),s.set(r,o),s}var Tn=Symbol.for("@ipld/js-cid/CID");var Ye=b({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),Wi=b({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),Gi=b({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),Ji=b({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});function Cn(e){return e instanceof ArrayBuffer?new Uint8Array(e,0,e.byteLength):e}function $n(e){if(e.asCID!==e&&e["/"]!==e.bytes)return null;let t=oe.asCID(e);if(!t)return null;let r=t.toString();return[new c(i.map,1/0,1),new c(i.string,"/",1),new c(i.string,r,r.length),new c(i.break,void 0,1)]}function $e(e){let t=Ye.encode(e).slice(1);return[new c(i.map,1/0,1),new c(i.string,"/",1),new c(i.map,1/0,1),new c(i.string,"bytes",5),new c(i.string,t,t.length),new c(i.break,void 0,1),new c(i.break,void 0,1)]}function N(e){return $e(new Uint8Array(e.buffer,e.byteOffset,e.byteLength))}function In(e){return $e(new Uint8Array(e))}function Nn(){throw new Error("`undefined` is not supported by the IPLD Data Model and cannot be encoded")}function Mn(e){if(Number.isNaN(e))throw new Error("`NaN` is not supported by the IPLD Data Model and cannot be encoded");if(e===1/0||e===-1/0)throw new Error("`Infinity` and `-Infinity` is not supported by the IPLD Data Model and cannot be encoded");return null}var Dn={typeEncoders:{Object:$n,Buffer:$e,Uint8Array:$e,Int8Array:N,Uint16Array:N,Int16Array:N,Uint32Array:N,Int32Array:N,Float32Array:N,Float64Array:N,Uint8ClampedArray:N,BigInt64Array:N,BigUint64Array:N,DataView:N,ArrayBuffer:In,undefined:Nn,number:Mn}},et=class extends Z{constructor(t,r){super(t,r),this.tokenBuffer=[]}done(){return this.tokenBuffer.length===0&&super.done()}_next(){return this.tokenBuffer.length>0?this.tokenBuffer.pop():super.next()}next(){let t=this._next();if(t.type.compare(i.map)===0){let r=this._next();if(r.type.compare(i.string)===0&&r.value==="/"){let n=this._next();if(n.type.compare(i.string)===0){if(this._next().type.compare(i.break))throw new Error("Invalid encoded CID form");return this.tokenBuffer.push(n),new c(i.tag,42,0)}if(n.type.compare(i.map)===0){let o=this._next();if(o.type.compare(i.string)===0&&o.value==="bytes"){let s=this._next();if(s.type.compare(i.string)===0){for(let u=0;u<2;u++)if(this._next().type.compare(i.break))throw new Error("Invalid encoded Bytes form");let a=Ye.decode(`m${s.value}`);return new c(i.bytes,a,s.value.length)}this.tokenBuffer.push(s)}this.tokenBuffer.push(o)}this.tokenBuffer.push(n)}this.tokenBuffer.push(r)}return t}},tt={allowIndefinite:!1,allowUndefined:!1,allowNaN:!1,allowInfinity:!1,allowBigInt:!0,strict:!0,useMaps:!1,rejectDuplicateMapKeys:!0,tags:[]};tt.tags[42]=oe.parse;var zn="dag-json",On=297,xr=e=>Re(e,Dn),yr=e=>{let t=Cn(e),r=Object.assign(tt,{tokenizer:new et(t,tt)});return Le(t,r)},Fn=e=>Vn.decode(xr(e));var Vn=new TextDecoder,jn=e=>yr(Pn.encode(e)),Pn=new TextEncoder;return vr(Rn);})();
|
|
3
|
+
return IpldDagJson}));
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const name: "dag-json";
|
|
2
|
+
export const code: 297;
|
|
3
|
+
export function encode<T>(node: T): import("multiformats").ByteView<T>;
|
|
4
|
+
export function decode<T>(data: import("multiformats").ByteView<T> | import("multiformats").ArrayBufferView<T>): T;
|
|
5
|
+
export function format<T>(node: T): import("multiformats").ToString<T, string>;
|
|
6
|
+
export { format as stringify };
|
|
7
|
+
export function parse<T>(data: import("multiformats").ToString<T, string>): T;
|
|
8
|
+
export type ByteView<T> = import('multiformats/codecs/interface').ByteView<T>;
|
|
9
|
+
export type ArrayBufferView<T> = import('multiformats/codecs/interface').ArrayBufferView<T>;
|
|
10
|
+
export type ToString<T> = import('multiformats').ToString<T>;
|
|
11
|
+
export type DecodeTokenizer = import('cborg/interface').DecodeTokenizer;
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.js"],"names":[],"mappings":"AA6PA,8BAA8B;AAC9B,uBAA0B;AAOnB,uEAA8D;AAO9D,mHAKN;AAOM,+EAAyD;;AASzD,8EAAwD;0BAzRlD,OAAO,+BAA+B,EAAE,QAAQ,CAAC,CAAC,CAAC;iCAInD,OAAO,+BAA+B,EAAE,eAAe,CAAC,CAAC,CAAC;0BAI1D,OAAO,cAAc,EAAE,QAAQ,CAAC,CAAC,CAAC;8BAGlC,OAAO,iBAAiB,EAAE,eAAe"}
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/* eslint max-depth: ["error", 7] */
|
|
2
|
+
import { Token, Type } from "@fireproof/vendor/cborg"
|
|
3
|
+
import * as cborgJson from "@fireproof/vendor/cborg/json"
|
|
4
|
+
import { CID } from 'multiformats'
|
|
5
|
+
import { base64 } from 'multiformats/bases/base64'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @template T
|
|
9
|
+
* @typedef {import('multiformats/codecs/interface').ByteView<T>} ByteView
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* @template T
|
|
13
|
+
* @typedef {import('multiformats/codecs/interface').ArrayBufferView<T>} ArrayBufferView
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @template T
|
|
17
|
+
* @typedef {import('multiformats').ToString<T>} ToString
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* @typedef {import('cborg/interface').DecodeTokenizer} DecodeTokenizer
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @template T
|
|
25
|
+
* @param {ByteView<T> | ArrayBufferView<T>} buf
|
|
26
|
+
* @returns {ByteView<T>}
|
|
27
|
+
*/
|
|
28
|
+
function toByteView (buf) {
|
|
29
|
+
if (buf instanceof ArrayBuffer) {
|
|
30
|
+
return new Uint8Array(buf, 0, buf.byteLength)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return buf
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* cidEncoder will receive all Objects during encode, it needs to filter out
|
|
38
|
+
* anything that's not a CID and return `null` for that so it's encoded as
|
|
39
|
+
* normal. Encoding a CID means replacing it with a `{"/":"<CidString>}`
|
|
40
|
+
* object as per the DAG-JSON spec.
|
|
41
|
+
*
|
|
42
|
+
* @param {any} obj
|
|
43
|
+
* @returns {Token[]|null}
|
|
44
|
+
*/
|
|
45
|
+
function cidEncoder (obj) {
|
|
46
|
+
if (obj.asCID !== obj && obj['/'] !== obj.bytes) {
|
|
47
|
+
return null // any other kind of object
|
|
48
|
+
}
|
|
49
|
+
const cid = CID.asCID(obj)
|
|
50
|
+
/* c8 ignore next 4 */
|
|
51
|
+
// very unlikely case, and it'll probably throw a recursion error in cborg
|
|
52
|
+
if (!cid) {
|
|
53
|
+
return null
|
|
54
|
+
}
|
|
55
|
+
const cidString = cid.toString()
|
|
56
|
+
|
|
57
|
+
return [
|
|
58
|
+
new Token(Type.map, Infinity, 1),
|
|
59
|
+
new Token(Type.string, '/', 1), // key
|
|
60
|
+
new Token(Type.string, cidString, cidString.length), // value
|
|
61
|
+
new Token(Type.break, undefined, 1)
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* bytesEncoder will receive all Uint8Arrays (and friends) during encode, it
|
|
67
|
+
* needs to replace it with a `{"/":{"bytes":"Base64ByteString"}}` object as
|
|
68
|
+
* per the DAG-JSON spec.
|
|
69
|
+
*
|
|
70
|
+
* @param {Uint8Array} bytes
|
|
71
|
+
* @returns {Token[]|null}
|
|
72
|
+
*/
|
|
73
|
+
function bytesEncoder (bytes) {
|
|
74
|
+
const bytesString = base64.encode(bytes).slice(1) // no mbase prefix
|
|
75
|
+
return [
|
|
76
|
+
new Token(Type.map, Infinity, 1),
|
|
77
|
+
new Token(Type.string, '/', 1), // key
|
|
78
|
+
new Token(Type.map, Infinity, 1), // value
|
|
79
|
+
new Token(Type.string, 'bytes', 5), // inner key
|
|
80
|
+
new Token(Type.string, bytesString, bytesString.length), // inner value
|
|
81
|
+
new Token(Type.break, undefined, 1),
|
|
82
|
+
new Token(Type.break, undefined, 1)
|
|
83
|
+
]
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* taBytesEncoder wraps bytesEncoder() but for the more exotic typed arrays so
|
|
88
|
+
* that we access the underlying ArrayBuffer data
|
|
89
|
+
*
|
|
90
|
+
* @param {Int8Array|Uint16Array|Int16Array|Uint32Array|Int32Array|Float32Array|Float64Array|Uint8ClampedArray|BigInt64Array|BigUint64Array} obj
|
|
91
|
+
* @returns {Token[]|null}
|
|
92
|
+
*/
|
|
93
|
+
function taBytesEncoder (obj) {
|
|
94
|
+
return bytesEncoder(new Uint8Array(obj.buffer, obj.byteOffset, obj.byteLength))
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* abBytesEncoder wraps bytesEncoder() but for plain ArrayBuffers
|
|
99
|
+
*
|
|
100
|
+
* @param {ArrayBuffer} ab
|
|
101
|
+
* @returns {Token[]|null}
|
|
102
|
+
*/
|
|
103
|
+
function abBytesEncoder (ab) {
|
|
104
|
+
return bytesEncoder(new Uint8Array(ab))
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// eslint-disable-next-line jsdoc/require-returns-check
|
|
108
|
+
/**
|
|
109
|
+
* Intercept all `undefined` values from an object walk and reject the entire
|
|
110
|
+
* object if we find one.
|
|
111
|
+
*
|
|
112
|
+
* @returns {null}
|
|
113
|
+
*/
|
|
114
|
+
function undefinedEncoder () {
|
|
115
|
+
throw new Error('`undefined` is not supported by the IPLD Data Model and cannot be encoded')
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Intercept all `number` values from an object walk and reject the entire
|
|
120
|
+
* object if we find something that doesn't fit the IPLD data model (NaN &
|
|
121
|
+
* Infinity).
|
|
122
|
+
*
|
|
123
|
+
* @param {number} num
|
|
124
|
+
* @returns {null}
|
|
125
|
+
*/
|
|
126
|
+
function numberEncoder (num) {
|
|
127
|
+
if (Number.isNaN(num)) {
|
|
128
|
+
throw new Error('`NaN` is not supported by the IPLD Data Model and cannot be encoded')
|
|
129
|
+
}
|
|
130
|
+
if (num === Infinity || num === -Infinity) {
|
|
131
|
+
throw new Error('`Infinity` and `-Infinity` is not supported by the IPLD Data Model and cannot be encoded')
|
|
132
|
+
}
|
|
133
|
+
return null // process with standard number encoder
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const encodeOptions = {
|
|
137
|
+
typeEncoders: {
|
|
138
|
+
Object: cidEncoder,
|
|
139
|
+
Buffer: bytesEncoder,
|
|
140
|
+
Uint8Array: bytesEncoder,
|
|
141
|
+
Int8Array: taBytesEncoder,
|
|
142
|
+
Uint16Array: taBytesEncoder,
|
|
143
|
+
Int16Array: taBytesEncoder,
|
|
144
|
+
Uint32Array: taBytesEncoder,
|
|
145
|
+
Int32Array: taBytesEncoder,
|
|
146
|
+
Float32Array: taBytesEncoder,
|
|
147
|
+
Float64Array: taBytesEncoder,
|
|
148
|
+
Uint8ClampedArray: taBytesEncoder,
|
|
149
|
+
BigInt64Array: taBytesEncoder,
|
|
150
|
+
BigUint64Array: taBytesEncoder,
|
|
151
|
+
DataView: taBytesEncoder,
|
|
152
|
+
ArrayBuffer: abBytesEncoder,
|
|
153
|
+
undefined: undefinedEncoder,
|
|
154
|
+
number: numberEncoder
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* @implements {DecodeTokenizer}
|
|
160
|
+
*/
|
|
161
|
+
class DagJsonTokenizer extends cborgJson.Tokenizer {
|
|
162
|
+
/**
|
|
163
|
+
* @param {Uint8Array} data
|
|
164
|
+
* @param {object} [options]
|
|
165
|
+
*/
|
|
166
|
+
constructor (data, options) {
|
|
167
|
+
super(data, options)
|
|
168
|
+
/** @type {Token[]} */
|
|
169
|
+
this.tokenBuffer = []
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* @returns {boolean}
|
|
174
|
+
*/
|
|
175
|
+
done () {
|
|
176
|
+
return this.tokenBuffer.length === 0 && super.done()
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* @returns {Token}
|
|
181
|
+
*/
|
|
182
|
+
_next () {
|
|
183
|
+
if (this.tokenBuffer.length > 0) {
|
|
184
|
+
// @ts-ignore https://github.com/Microsoft/TypeScript/issues/30406
|
|
185
|
+
return this.tokenBuffer.pop()
|
|
186
|
+
}
|
|
187
|
+
return super.next()
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Implements rules outlined in https://github.com/ipld/specs/pull/356
|
|
192
|
+
*
|
|
193
|
+
* @returns {Token}
|
|
194
|
+
*/
|
|
195
|
+
next () {
|
|
196
|
+
const token = this._next()
|
|
197
|
+
|
|
198
|
+
if (token.type.compare(Type.map) === 0) {
|
|
199
|
+
const keyToken = this._next()
|
|
200
|
+
if (keyToken.type.compare(Type.string) === 0 && keyToken.value === '/') {
|
|
201
|
+
const valueToken = this._next()
|
|
202
|
+
if (valueToken.type.compare(Type.string) === 0) { // *must* be a CID
|
|
203
|
+
const breakToken = this._next() // swallow the end-of-map token
|
|
204
|
+
if (breakToken.type.compare(Type.break)) {
|
|
205
|
+
throw new Error('Invalid encoded CID form')
|
|
206
|
+
}
|
|
207
|
+
this.tokenBuffer.push(valueToken) // CID.parse will pick this up after our tag token
|
|
208
|
+
return new Token(Type.tag, 42, 0)
|
|
209
|
+
}
|
|
210
|
+
if (valueToken.type.compare(Type.map) === 0) {
|
|
211
|
+
const innerKeyToken = this._next()
|
|
212
|
+
if (innerKeyToken.type.compare(Type.string) === 0 && innerKeyToken.value === 'bytes') {
|
|
213
|
+
const innerValueToken = this._next()
|
|
214
|
+
if (innerValueToken.type.compare(Type.string) === 0) { // *must* be Bytes
|
|
215
|
+
for (let i = 0; i < 2; i++) {
|
|
216
|
+
const breakToken = this._next() // swallow two end-of-map tokens
|
|
217
|
+
if (breakToken.type.compare(Type.break)) {
|
|
218
|
+
throw new Error('Invalid encoded Bytes form')
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
const bytes = base64.decode(`m${innerValueToken.value}`)
|
|
222
|
+
return new Token(Type.bytes, bytes, innerValueToken.value.length)
|
|
223
|
+
}
|
|
224
|
+
this.tokenBuffer.push(innerValueToken) // bail
|
|
225
|
+
}
|
|
226
|
+
this.tokenBuffer.push(innerKeyToken) // bail
|
|
227
|
+
}
|
|
228
|
+
this.tokenBuffer.push(valueToken) // bail
|
|
229
|
+
}
|
|
230
|
+
this.tokenBuffer.push(keyToken) // bail
|
|
231
|
+
}
|
|
232
|
+
return token
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const decodeOptions = {
|
|
237
|
+
allowIndefinite: false,
|
|
238
|
+
allowUndefined: false,
|
|
239
|
+
allowNaN: false,
|
|
240
|
+
allowInfinity: false,
|
|
241
|
+
allowBigInt: true, // this will lead to BigInt for ints outside of
|
|
242
|
+
// safe-integer range, which may surprise users
|
|
243
|
+
strict: true,
|
|
244
|
+
useMaps: false,
|
|
245
|
+
rejectDuplicateMapKeys: true,
|
|
246
|
+
/** @type {import('cborg').TagDecoder[]} */
|
|
247
|
+
tags: []
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// we're going to get TAG(42)STRING("bafy...") from the tokenizer so we only need
|
|
251
|
+
// to deal with the STRING("bafy...") at this point
|
|
252
|
+
decodeOptions.tags[42] = CID.parse
|
|
253
|
+
|
|
254
|
+
export const name = 'dag-json'
|
|
255
|
+
export const code = 0x0129
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* @template T
|
|
259
|
+
* @param {T} node
|
|
260
|
+
* @returns {ByteView<T>}
|
|
261
|
+
*/
|
|
262
|
+
export const encode = (node) => cborgJson.encode(node, encodeOptions)
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* @template T
|
|
266
|
+
* @param {ByteView<T> | ArrayBufferView<T>} data
|
|
267
|
+
* @returns {T}
|
|
268
|
+
*/
|
|
269
|
+
export const decode = (data) => {
|
|
270
|
+
const buf = toByteView(data)
|
|
271
|
+
// the tokenizer is stateful so we need a single instance of it
|
|
272
|
+
const options = Object.assign(decodeOptions, { tokenizer: new DagJsonTokenizer(buf, decodeOptions) })
|
|
273
|
+
return cborgJson.decode(buf, options)
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* @template T
|
|
278
|
+
* @param {T} node
|
|
279
|
+
* @returns {ToString<T>}
|
|
280
|
+
*/
|
|
281
|
+
export const format = (node) => utf8Decoder.decode(encode(node))
|
|
282
|
+
export { format as stringify }
|
|
283
|
+
const utf8Decoder = new TextDecoder()
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* @template T
|
|
287
|
+
* @param {ToString<T>} data
|
|
288
|
+
* @returns {T}
|
|
289
|
+
*/
|
|
290
|
+
export const parse = (data) => decode(utf8Encoder.encode(data))
|
|
291
|
+
const utf8Encoder = new TextEncoder()
|