@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,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @template {any} T
|
|
3
|
+
* @typedef {import('./coding').IteratorChannel<T>} IteratorChannel
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
function noop () {}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @template {any} T
|
|
10
|
+
* @returns {IteratorChannel<T>}
|
|
11
|
+
*/
|
|
12
|
+
export function create () {
|
|
13
|
+
/** @type {T[]} */
|
|
14
|
+
const chunkQueue = []
|
|
15
|
+
/** @type {Promise<void> | null} */
|
|
16
|
+
let drainer = null
|
|
17
|
+
let drainerResolver = noop
|
|
18
|
+
let ended = false
|
|
19
|
+
/** @type {Promise<IteratorResult<T>> | null} */
|
|
20
|
+
let outWait = null
|
|
21
|
+
let outWaitResolver = noop
|
|
22
|
+
|
|
23
|
+
const makeDrainer = () => {
|
|
24
|
+
if (!drainer) {
|
|
25
|
+
drainer = new Promise((resolve) => {
|
|
26
|
+
drainerResolver = () => {
|
|
27
|
+
drainer = null
|
|
28
|
+
drainerResolver = noop
|
|
29
|
+
resolve()
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
return drainer
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @returns {IteratorChannel<T>}
|
|
38
|
+
*/
|
|
39
|
+
const writer = {
|
|
40
|
+
/**
|
|
41
|
+
* @param {T} chunk
|
|
42
|
+
* @returns {Promise<void>}
|
|
43
|
+
*/
|
|
44
|
+
write (chunk) {
|
|
45
|
+
chunkQueue.push(chunk)
|
|
46
|
+
const drainer = makeDrainer()
|
|
47
|
+
outWaitResolver()
|
|
48
|
+
return drainer
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
async end () {
|
|
52
|
+
ended = true
|
|
53
|
+
const drainer = makeDrainer()
|
|
54
|
+
outWaitResolver()
|
|
55
|
+
await drainer
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** @type {AsyncIterator<T>} */
|
|
60
|
+
const iterator = {
|
|
61
|
+
/** @returns {Promise<IteratorResult<T>>} */
|
|
62
|
+
async next () {
|
|
63
|
+
const chunk = chunkQueue.shift()
|
|
64
|
+
if (chunk) {
|
|
65
|
+
if (chunkQueue.length === 0) {
|
|
66
|
+
drainerResolver()
|
|
67
|
+
}
|
|
68
|
+
return { done: false, value: chunk }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (ended) {
|
|
72
|
+
drainerResolver()
|
|
73
|
+
return { done: true, value: undefined }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (!outWait) {
|
|
77
|
+
outWait = new Promise((resolve) => {
|
|
78
|
+
outWaitResolver = () => {
|
|
79
|
+
outWait = null
|
|
80
|
+
outWaitResolver = noop
|
|
81
|
+
return resolve(iterator.next())
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return outWait
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return { writer, iterator }
|
|
91
|
+
}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import {
|
|
2
|
+
asyncIterableReader,
|
|
3
|
+
bytesReader,
|
|
4
|
+
createDecoder
|
|
5
|
+
} from './decoder.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {import('multiformats').CID} CID
|
|
9
|
+
* @typedef {import('./api').Block} Block
|
|
10
|
+
* @typedef {import('./api').RootsReader} RootsReader
|
|
11
|
+
* @typedef {import('./coding').BytesReader} BytesReader
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @class
|
|
16
|
+
* @implements {RootsReader}
|
|
17
|
+
* @property {number} version The version number of the CAR referenced by this reader (should be `1`).
|
|
18
|
+
*/
|
|
19
|
+
export class CarIteratorBase {
|
|
20
|
+
/**
|
|
21
|
+
* @param {number} version
|
|
22
|
+
* @param {CID[]} roots
|
|
23
|
+
* @param {AsyncIterable<Block>|void} iterable
|
|
24
|
+
*/
|
|
25
|
+
constructor (version, roots, iterable) {
|
|
26
|
+
this._version = version
|
|
27
|
+
this._roots = roots
|
|
28
|
+
this._iterable = iterable
|
|
29
|
+
this._decoded = false
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
get version () {
|
|
33
|
+
return this._version
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @returns {Promise<CID[]>}
|
|
38
|
+
*/
|
|
39
|
+
async getRoots () {
|
|
40
|
+
return this._roots
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Provides an iterator over all of the `Block`s in a CAR. Implements a
|
|
46
|
+
* `BlockIterator` interface, or `AsyncIterable<Block>`. Where a `Block` is
|
|
47
|
+
* a `{ cid:CID, bytes:Uint8Array }` pair.
|
|
48
|
+
*
|
|
49
|
+
* As an implementer of `AsyncIterable`, this class can be used directly in a
|
|
50
|
+
* `for await (const block of iterator) {}` loop. Where the `iterator` is
|
|
51
|
+
* constructed using {@link CarBlockiterator.fromBytes} or
|
|
52
|
+
* {@link CarBlockiterator.fromIterable}.
|
|
53
|
+
*
|
|
54
|
+
* An iteration can only be performce _once_ per instantiation.
|
|
55
|
+
*
|
|
56
|
+
* `CarBlockIterator` also implements the `RootsReader` interface and provides
|
|
57
|
+
* the {@link CarBlockiterator.getRoots `getRoots()`} method.
|
|
58
|
+
*
|
|
59
|
+
* Load this class with either
|
|
60
|
+
* `import { CarBlockIterator } from '@ipld/car/iterator'`
|
|
61
|
+
* (`const { CarBlockIterator } = require('@ipld/car/iterator')`). Or
|
|
62
|
+
* `import { CarBlockIterator } from '@ipld/car'`
|
|
63
|
+
* (`const { CarBlockIterator } = require('@ipld/car')`).
|
|
64
|
+
*
|
|
65
|
+
* @name CarBlockIterator
|
|
66
|
+
* @class
|
|
67
|
+
* @implements {RootsReader}
|
|
68
|
+
* @implements {AsyncIterable<Block>}
|
|
69
|
+
* @property {number} version The version number of the CAR referenced by this
|
|
70
|
+
* iterator (should be `1`).
|
|
71
|
+
*/
|
|
72
|
+
export class CarBlockIterator extends CarIteratorBase {
|
|
73
|
+
// inherited method
|
|
74
|
+
/**
|
|
75
|
+
* Get the list of roots defined by the CAR referenced by this iterator. May be
|
|
76
|
+
* zero or more `CID`s.
|
|
77
|
+
*
|
|
78
|
+
* @function getRoots
|
|
79
|
+
* @memberof CarBlockIterator
|
|
80
|
+
* @instance
|
|
81
|
+
* @async
|
|
82
|
+
* @returns {Promise<CID[]>}
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @returns {AsyncIterator<Block>}
|
|
87
|
+
*/
|
|
88
|
+
[Symbol.asyncIterator] () {
|
|
89
|
+
if (this._decoded) {
|
|
90
|
+
throw new Error('Cannot decode more than once')
|
|
91
|
+
}
|
|
92
|
+
/* c8 ignore next 3 */
|
|
93
|
+
if (!this._iterable) {
|
|
94
|
+
throw new Error('Block iterable not found')
|
|
95
|
+
}
|
|
96
|
+
this._decoded = true
|
|
97
|
+
return this._iterable[Symbol.asyncIterator]()
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Instantiate a {@link CarBlockIterator} from a `Uint8Array` blob. Rather
|
|
102
|
+
* than decoding the entire byte array prior to returning the iterator, as in
|
|
103
|
+
* {@link CarReader.fromBytes}, only the header is decoded and the remainder
|
|
104
|
+
* of the CAR is parsed as the `Block`s as yielded.
|
|
105
|
+
*
|
|
106
|
+
* @async
|
|
107
|
+
* @static
|
|
108
|
+
* @memberof CarBlockIterator
|
|
109
|
+
* @param {Uint8Array} bytes
|
|
110
|
+
* @returns {Promise<CarBlockIterator>}
|
|
111
|
+
*/
|
|
112
|
+
static async fromBytes (bytes) {
|
|
113
|
+
const { version, roots, iterator } = await fromBytes(bytes)
|
|
114
|
+
return new CarBlockIterator(version, roots, iterator)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Instantiate a {@link CarBlockIterator} from a `AsyncIterable<Uint8Array>`,
|
|
119
|
+
* such as a [modern Node.js stream](https://nodejs.org/api/stream.html#stream_streams_compatibility_with_async_generators_and_async_iterators).
|
|
120
|
+
* Rather than decoding the entire byte array prior to returning the iterator,
|
|
121
|
+
* as in {@link CarReader.fromIterable}, only the header is decoded and the
|
|
122
|
+
* remainder of the CAR is parsed as the `Block`s as yielded.
|
|
123
|
+
*
|
|
124
|
+
* @async
|
|
125
|
+
* @static
|
|
126
|
+
* @param {AsyncIterable<Uint8Array>} asyncIterable
|
|
127
|
+
* @returns {Promise<CarBlockIterator>}
|
|
128
|
+
*/
|
|
129
|
+
static async fromIterable (asyncIterable) {
|
|
130
|
+
const { version, roots, iterator } = await fromIterable(asyncIterable)
|
|
131
|
+
return new CarBlockIterator(version, roots, iterator)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Provides an iterator over all of the `CID`s in a CAR. Implements a
|
|
137
|
+
* `CIDIterator` interface, or `AsyncIterable<CID>`. Similar to
|
|
138
|
+
* {@link CarBlockIterator} but only yields the CIDs in the CAR.
|
|
139
|
+
*
|
|
140
|
+
* As an implementer of `AsyncIterable`, this class can be used directly in a
|
|
141
|
+
* `for await (const cid of iterator) {}` loop. Where the `iterator` is
|
|
142
|
+
* constructed using {@link CarCIDiterator.fromBytes} or
|
|
143
|
+
* {@link CarCIDiterator.fromIterable}.
|
|
144
|
+
*
|
|
145
|
+
* An iteration can only be performce _once_ per instantiation.
|
|
146
|
+
*
|
|
147
|
+
* `CarCIDIterator` also implements the `RootsReader` interface and provides
|
|
148
|
+
* the {@link CarCIDiterator.getRoots `getRoots()`} method.
|
|
149
|
+
*
|
|
150
|
+
* Load this class with either
|
|
151
|
+
* `import { CarCIDIterator } from '@ipld/car/iterator'`
|
|
152
|
+
* (`const { CarCIDIterator } = require('@ipld/car/iterator')`). Or
|
|
153
|
+
* `import { CarCIDIterator } from '@ipld/car'`
|
|
154
|
+
* (`const { CarCIDIterator } = require('@ipld/car')`).
|
|
155
|
+
*
|
|
156
|
+
* @name CarCIDIterator
|
|
157
|
+
* @class
|
|
158
|
+
* @implements {RootsReader}
|
|
159
|
+
* @implements {AsyncIterable<CID>}
|
|
160
|
+
* @property {number} version The version number of the CAR referenced by this
|
|
161
|
+
* iterator (should be `1`).
|
|
162
|
+
*/
|
|
163
|
+
export class CarCIDIterator extends CarIteratorBase {
|
|
164
|
+
// inherited method
|
|
165
|
+
/**
|
|
166
|
+
* Get the list of roots defined by the CAR referenced by this iterator. May be
|
|
167
|
+
* zero or more `CID`s.
|
|
168
|
+
*
|
|
169
|
+
* @function getRoots
|
|
170
|
+
* @memberof CarCIDIterator
|
|
171
|
+
* @instance
|
|
172
|
+
* @async
|
|
173
|
+
* @returns {Promise<CID[]>}
|
|
174
|
+
*/
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* @returns {AsyncIterator<CID>}
|
|
178
|
+
*/
|
|
179
|
+
[Symbol.asyncIterator] () {
|
|
180
|
+
if (this._decoded) {
|
|
181
|
+
throw new Error('Cannot decode more than once')
|
|
182
|
+
}
|
|
183
|
+
/* c8 ignore next 3 */
|
|
184
|
+
if (!this._iterable) {
|
|
185
|
+
throw new Error('Block iterable not found')
|
|
186
|
+
}
|
|
187
|
+
this._decoded = true
|
|
188
|
+
const iterable = this._iterable[Symbol.asyncIterator]()
|
|
189
|
+
return {
|
|
190
|
+
async next () {
|
|
191
|
+
const next = await iterable.next()
|
|
192
|
+
if (next.done) {
|
|
193
|
+
return next
|
|
194
|
+
}
|
|
195
|
+
return { done: false, value: next.value.cid }
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Instantiate a {@link CarCIDIterator} from a `Uint8Array` blob. Rather
|
|
202
|
+
* than decoding the entire byte array prior to returning the iterator, as in
|
|
203
|
+
* {@link CarReader.fromBytes}, only the header is decoded and the remainder
|
|
204
|
+
* of the CAR is parsed as the `CID`s as yielded.
|
|
205
|
+
*
|
|
206
|
+
* @async
|
|
207
|
+
* @static
|
|
208
|
+
* @memberof CarCIDIterator
|
|
209
|
+
* @param {Uint8Array} bytes
|
|
210
|
+
* @returns {Promise<CarCIDIterator>}
|
|
211
|
+
*/
|
|
212
|
+
static async fromBytes (bytes) {
|
|
213
|
+
const { version, roots, iterator } = await fromBytes(bytes)
|
|
214
|
+
return new CarCIDIterator(version, roots, iterator)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Instantiate a {@link CarCIDIterator} from a `AsyncIterable<Uint8Array>`,
|
|
219
|
+
* such as a [modern Node.js stream](https://nodejs.org/api/stream.html#stream_streams_compatibility_with_async_generators_and_async_iterators).
|
|
220
|
+
* Rather than decoding the entire byte array prior to returning the iterator,
|
|
221
|
+
* as in {@link CarReader.fromIterable}, only the header is decoded and the
|
|
222
|
+
* remainder of the CAR is parsed as the `CID`s as yielded.
|
|
223
|
+
*
|
|
224
|
+
* @async
|
|
225
|
+
* @static
|
|
226
|
+
* @memberof CarCIDIterator
|
|
227
|
+
* @param {AsyncIterable<Uint8Array>} asyncIterable
|
|
228
|
+
* @returns {Promise<CarCIDIterator>}
|
|
229
|
+
*/
|
|
230
|
+
static async fromIterable (asyncIterable) {
|
|
231
|
+
const { version, roots, iterator } = await fromIterable(asyncIterable)
|
|
232
|
+
return new CarCIDIterator(version, roots, iterator)
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* @param {Uint8Array} bytes
|
|
238
|
+
* @returns {Promise<{ version:number, roots:CID[], iterator:AsyncIterable<Block>}>}
|
|
239
|
+
*/
|
|
240
|
+
async function fromBytes (bytes) {
|
|
241
|
+
if (!(bytes instanceof Uint8Array)) {
|
|
242
|
+
throw new TypeError('fromBytes() requires a Uint8Array')
|
|
243
|
+
}
|
|
244
|
+
return decodeIterator(bytesReader(bytes))
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* @param {AsyncIterable<Uint8Array>} asyncIterable
|
|
249
|
+
* @returns {Promise<{ version:number, roots:CID[], iterator:AsyncIterable<Block>}>}
|
|
250
|
+
*/
|
|
251
|
+
async function fromIterable (asyncIterable) {
|
|
252
|
+
if (!asyncIterable || !(typeof asyncIterable[Symbol.asyncIterator] === 'function')) {
|
|
253
|
+
throw new TypeError('fromIterable() requires an async iterable')
|
|
254
|
+
}
|
|
255
|
+
return decodeIterator(asyncIterableReader(asyncIterable))
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* @private
|
|
260
|
+
* @param {BytesReader} reader
|
|
261
|
+
* @returns {Promise<{ version:number, roots:CID[], iterator:AsyncIterable<Block>}>}
|
|
262
|
+
*/
|
|
263
|
+
async function decodeIterator (reader) {
|
|
264
|
+
const decoder = createDecoder(reader)
|
|
265
|
+
const { version, roots } = await decoder.header()
|
|
266
|
+
return { version, roots, iterator: decoder.blocks() }
|
|
267
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import { promisify } from 'util'
|
|
3
|
+
|
|
4
|
+
const hasFS = Boolean(fs)
|
|
5
|
+
|
|
6
|
+
export { hasFS }
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @type {any}
|
|
10
|
+
*/
|
|
11
|
+
let _fsReadFn
|
|
12
|
+
/**
|
|
13
|
+
* @description This function is needed not to initialize the `fs.read` on load time. To run in cf workers without polyfill.
|
|
14
|
+
* @param {number} fd
|
|
15
|
+
* @param {Uint8Array} buffer
|
|
16
|
+
* @param {number} offset
|
|
17
|
+
* @param {number} length
|
|
18
|
+
* @param {number} position
|
|
19
|
+
* @returns {Promise<{ bytesRead: number, buffer: Uint8Array }>}
|
|
20
|
+
*/
|
|
21
|
+
export function fsread (fd, buffer, offset, length, position) {
|
|
22
|
+
if (!_fsReadFn) {
|
|
23
|
+
_fsReadFn = promisify(fs.read)
|
|
24
|
+
}
|
|
25
|
+
return _fsReadFn(fd, buffer, offset, length, position)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @type {any}
|
|
30
|
+
*/
|
|
31
|
+
let _fsWriteFn
|
|
32
|
+
/**
|
|
33
|
+
* @description This function is needed not to initialize the `fs.write` on load time. To run in cf workers without polyfill.
|
|
34
|
+
* @param {number} fd
|
|
35
|
+
* @param {Uint8Array} buffer
|
|
36
|
+
* @param {number} offset
|
|
37
|
+
* @param {number} length
|
|
38
|
+
* @param {number} position
|
|
39
|
+
* @returns {Promise<{ bytesRead: number, buffer: Uint8Array }>}
|
|
40
|
+
*/
|
|
41
|
+
export function fswrite (fd, buffer, offset, length, position) {
|
|
42
|
+
if (!_fsWriteFn) {
|
|
43
|
+
_fsWriteFn = promisify(fs.write)
|
|
44
|
+
}
|
|
45
|
+
return _fsWriteFn(fd, buffer, offset, length, position)
|
|
46
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { asyncIterableReader, bytesReader, createDecoder } from './decoder.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {import('multiformats').CID} CID
|
|
5
|
+
* @typedef {import('./api').Block} Block
|
|
6
|
+
* @typedef {import('./api').CarReader} CarReaderIface
|
|
7
|
+
* @typedef {import('./coding').BytesReader} BytesReader
|
|
8
|
+
* @typedef {import('./coding').CarHeader} CarHeader
|
|
9
|
+
* @typedef {import('./coding').CarV2Header} CarV2Header
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Provides blockstore-like access to a CAR.
|
|
14
|
+
*
|
|
15
|
+
* Implements the `RootsReader` interface:
|
|
16
|
+
* {@link CarReader.getRoots `getRoots()`}. And the `BlockReader` interface:
|
|
17
|
+
* {@link CarReader.get `get()`}, {@link CarReader.has `has()`},
|
|
18
|
+
* {@link CarReader.blocks `blocks()`} (defined as a `BlockIterator`) and
|
|
19
|
+
* {@link CarReader.cids `cids()`} (defined as a `CIDIterator`).
|
|
20
|
+
*
|
|
21
|
+
* Load this class with either `import { CarReader } from '@ipld/car/reader'`
|
|
22
|
+
* (`const { CarReader } = require('@ipld/car/reader')`). Or
|
|
23
|
+
* `import { CarReader } from '@ipld/car'` (`const { CarReader } = require('@ipld/car')`).
|
|
24
|
+
* The former will likely result in smaller bundle sizes where this is
|
|
25
|
+
* important.
|
|
26
|
+
*
|
|
27
|
+
* @name CarReader
|
|
28
|
+
* @class
|
|
29
|
+
* @implements {CarReaderIface}
|
|
30
|
+
* @property {number} version The version number of the CAR referenced by this
|
|
31
|
+
* reader (should be `1` or `2`).
|
|
32
|
+
*/
|
|
33
|
+
export class CarReader {
|
|
34
|
+
/**
|
|
35
|
+
* @constructs CarReader
|
|
36
|
+
* @param {CarHeader|CarV2Header} header
|
|
37
|
+
* @param {Block[]} blocks
|
|
38
|
+
*/
|
|
39
|
+
constructor (header, blocks) {
|
|
40
|
+
this._header = header
|
|
41
|
+
this._blocks = blocks
|
|
42
|
+
this._keys = blocks.map((b) => b.cid.toString())
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @property
|
|
47
|
+
* @memberof CarReader
|
|
48
|
+
* @instance
|
|
49
|
+
*/
|
|
50
|
+
get version () {
|
|
51
|
+
return this._header.version
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Get the list of roots defined by the CAR referenced by this reader. May be
|
|
56
|
+
* zero or more `CID`s.
|
|
57
|
+
*
|
|
58
|
+
* @function
|
|
59
|
+
* @memberof CarReader
|
|
60
|
+
* @instance
|
|
61
|
+
* @async
|
|
62
|
+
* @returns {Promise<CID[]>}
|
|
63
|
+
*/
|
|
64
|
+
async getRoots () {
|
|
65
|
+
return this._header.roots
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Check whether a given `CID` exists within the CAR referenced by this
|
|
70
|
+
* reader.
|
|
71
|
+
*
|
|
72
|
+
* @function
|
|
73
|
+
* @memberof CarReader
|
|
74
|
+
* @instance
|
|
75
|
+
* @async
|
|
76
|
+
* @param {CID} key
|
|
77
|
+
* @returns {Promise<boolean>}
|
|
78
|
+
*/
|
|
79
|
+
async has (key) {
|
|
80
|
+
return this._keys.indexOf(key.toString()) > -1
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Fetch a `Block` (a `{ cid:CID, bytes:Uint8Array }` pair) from the CAR
|
|
85
|
+
* referenced by this reader matching the provided `CID`. In the case where
|
|
86
|
+
* the provided `CID` doesn't exist within the CAR, `undefined` will be
|
|
87
|
+
* returned.
|
|
88
|
+
*
|
|
89
|
+
* @function
|
|
90
|
+
* @memberof CarReader
|
|
91
|
+
* @instance
|
|
92
|
+
* @async
|
|
93
|
+
* @param {CID} key
|
|
94
|
+
* @returns {Promise<Block | undefined>}
|
|
95
|
+
*/
|
|
96
|
+
async get (key) {
|
|
97
|
+
const index = this._keys.indexOf(key.toString())
|
|
98
|
+
return index > -1 ? this._blocks[index] : undefined
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Returns a `BlockIterator` (`AsyncIterable<Block>`) that iterates over all
|
|
103
|
+
* of the `Block`s (`{ cid:CID, bytes:Uint8Array }` pairs) contained within
|
|
104
|
+
* the CAR referenced by this reader.
|
|
105
|
+
*
|
|
106
|
+
* @function
|
|
107
|
+
* @memberof CarReader
|
|
108
|
+
* @instance
|
|
109
|
+
* @async
|
|
110
|
+
* @generator
|
|
111
|
+
* @returns {AsyncGenerator<Block>}
|
|
112
|
+
*/
|
|
113
|
+
async * blocks () {
|
|
114
|
+
for (const block of this._blocks) {
|
|
115
|
+
yield block
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Returns a `CIDIterator` (`AsyncIterable<CID>`) that iterates over all of
|
|
121
|
+
* the `CID`s contained within the CAR referenced by this reader.
|
|
122
|
+
*
|
|
123
|
+
* @function
|
|
124
|
+
* @memberof CarReader
|
|
125
|
+
* @instance
|
|
126
|
+
* @async
|
|
127
|
+
* @generator
|
|
128
|
+
* @returns {AsyncGenerator<CID>}
|
|
129
|
+
*/
|
|
130
|
+
async * cids () {
|
|
131
|
+
for (const block of this._blocks) {
|
|
132
|
+
yield block.cid
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Instantiate a {@link CarReader} from a `Uint8Array` blob. This performs a
|
|
138
|
+
* decode fully in memory and maintains the decoded state in memory for full
|
|
139
|
+
* access to the data via the `CarReader` API.
|
|
140
|
+
*
|
|
141
|
+
* @async
|
|
142
|
+
* @static
|
|
143
|
+
* @memberof CarReader
|
|
144
|
+
* @param {Uint8Array} bytes
|
|
145
|
+
* @returns {Promise<CarReader>}
|
|
146
|
+
*/
|
|
147
|
+
static async fromBytes (bytes) {
|
|
148
|
+
if (!(bytes instanceof Uint8Array)) {
|
|
149
|
+
throw new TypeError('fromBytes() requires a Uint8Array')
|
|
150
|
+
}
|
|
151
|
+
return decodeReaderComplete(bytesReader(bytes))
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Instantiate a {@link CarReader} from a `AsyncIterable<Uint8Array>`, such as
|
|
156
|
+
* a [modern Node.js stream](https://nodejs.org/api/stream.html#stream_streams_compatibility_with_async_generators_and_async_iterators).
|
|
157
|
+
* This performs a decode fully in memory and maintains the decoded state in
|
|
158
|
+
* memory for full access to the data via the `CarReader` API.
|
|
159
|
+
*
|
|
160
|
+
* Care should be taken for large archives; this API may not be appropriate
|
|
161
|
+
* where memory is a concern or the archive is potentially larger than the
|
|
162
|
+
* amount of memory that the runtime can handle.
|
|
163
|
+
*
|
|
164
|
+
* @async
|
|
165
|
+
* @static
|
|
166
|
+
* @memberof CarReader
|
|
167
|
+
* @param {AsyncIterable<Uint8Array>} asyncIterable
|
|
168
|
+
* @returns {Promise<CarReader>}
|
|
169
|
+
*/
|
|
170
|
+
static async fromIterable (asyncIterable) {
|
|
171
|
+
if (!asyncIterable || !(typeof asyncIterable[Symbol.asyncIterator] === 'function')) {
|
|
172
|
+
throw new TypeError('fromIterable() requires an async iterable')
|
|
173
|
+
}
|
|
174
|
+
return decodeReaderComplete(asyncIterableReader(asyncIterable))
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @private
|
|
180
|
+
* @param {BytesReader} reader
|
|
181
|
+
* @returns {Promise<CarReader>}
|
|
182
|
+
*/
|
|
183
|
+
export async function decodeReaderComplete (reader) {
|
|
184
|
+
const decoder = createDecoder(reader)
|
|
185
|
+
const header = await decoder.header()
|
|
186
|
+
const blocks = []
|
|
187
|
+
for await (const block of decoder.blocks()) {
|
|
188
|
+
blocks.push(block)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return new CarReader(header, blocks)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export const __browser = true
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { fsread, hasFS } from './promise-fs-opts.js'
|
|
2
|
+
import { CarReader as BrowserCarReader } from './reader-browser.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {import('./api').Block} Block
|
|
6
|
+
* @typedef {import('./api').BlockIndex} BlockIndex
|
|
7
|
+
* @typedef {import('./api').CarReader} CarReaderIface
|
|
8
|
+
* @typedef {import('fs').promises.FileHandle} FileHandle
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @class
|
|
13
|
+
* @implements {CarReaderIface}
|
|
14
|
+
*/
|
|
15
|
+
export class CarReader extends BrowserCarReader {
|
|
16
|
+
/**
|
|
17
|
+
* Reads a block directly from a file descriptor for an open CAR file. This
|
|
18
|
+
* function is **only available in Node.js** and not a browser environment.
|
|
19
|
+
*
|
|
20
|
+
* This function can be used in connection with {@link CarIndexer} which emits
|
|
21
|
+
* the `BlockIndex` objects that are required by this function.
|
|
22
|
+
*
|
|
23
|
+
* The user is responsible for opening and closing the file used in this call.
|
|
24
|
+
*
|
|
25
|
+
* @async
|
|
26
|
+
* @static
|
|
27
|
+
* @memberof CarReader
|
|
28
|
+
* @param {FileHandle | number} fd - A file descriptor from the
|
|
29
|
+
* Node.js `fs` module. Either an integer, from `fs.open()` or a `FileHandle`
|
|
30
|
+
* from `fs.promises.open()`.
|
|
31
|
+
* @param {BlockIndex} blockIndex - An index pointing to the location of the
|
|
32
|
+
* Block required. This `BlockIndex` should take the form:
|
|
33
|
+
* `{cid:CID, blockLength:number, blockOffset:number}`.
|
|
34
|
+
* @returns {Promise<Block>} A `{ cid:CID, bytes:Uint8Array }` pair.
|
|
35
|
+
*/
|
|
36
|
+
static async readRaw (fd, blockIndex) {
|
|
37
|
+
const { cid, blockLength, blockOffset } = blockIndex
|
|
38
|
+
const bytes = new Uint8Array(blockLength)
|
|
39
|
+
let read
|
|
40
|
+
if (typeof fd === 'number') {
|
|
41
|
+
read = (await fsread(fd, bytes, 0, blockLength, blockOffset)).bytesRead
|
|
42
|
+
} else if (typeof fd === 'object' && typeof fd.read === 'function') { // FileDescriptor
|
|
43
|
+
read = (await fd.read(bytes, 0, blockLength, blockOffset)).bytesRead
|
|
44
|
+
} else {
|
|
45
|
+
throw new TypeError('Bad fd')
|
|
46
|
+
}
|
|
47
|
+
if (read !== blockLength) {
|
|
48
|
+
throw new Error(`Failed to read entire block (${read} instead of ${blockLength})`)
|
|
49
|
+
}
|
|
50
|
+
return { cid, bytes }
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const __browser = !hasFS
|