From 50210f03c4032a96e0b95df0f5189711c97ac5ca Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Thu, 3 Feb 2022 10:34:30 +0000 Subject: [PATCH] fix: client close blockstore on store directory and store blob (#1198) --- packages/client/package.json | 2 +- packages/client/src/lib.js | 61 +++++++++++++++++++++++++++--------- yarn.lock | 10 ++++++ 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/packages/client/package.json b/packages/client/package.json index 42ad881adf..28ab8821ac 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -49,7 +49,7 @@ "@ipld/car": "^3.1.20", "@ipld/dag-cbor": "^6.0.13", "@web-std/blob": "^3.0.1", - "@web-std/fetch": "^3.0.0", + "@web-std/fetch": "^3.0.3", "@web-std/file": "^3.0.0", "@web-std/form-data": "^3.0.0", "carbites": "^1.0.6", diff --git a/packages/client/src/lib.js b/packages/client/src/lib.js index 035f589601..d25ae4a913 100644 --- a/packages/client/src/lib.js +++ b/packages/client/src/lib.js @@ -34,6 +34,7 @@ const MAX_CHUNK_SIZE = 1024 * 1024 * 10 // chunk to ~10MB CARs * @typedef {import('./lib/interface.js').Deal} Deal * @typedef {import('./lib/interface.js').Pin} Pin * @typedef {import('./lib/interface.js').CarReader} CarReader + * @typedef {import('ipfs-car/blockstore').Blockstore} BlockstoreI */ /** @@ -99,9 +100,18 @@ class NFTStorage { * @returns {Promise} */ static async storeBlob(service, blob) { - const { cid, car } = await NFTStorage.encodeBlob(blob) - await NFTStorage.storeCar(service, car) - return cid.toString() + const blockstore = new Blockstore() + let cidString + + try { + const { cid, car } = await NFTStorage.encodeBlob(blob, { blockstore }) + await NFTStorage.storeCar(service, car) + cidString = cid.toString() + } finally { + await blockstore.close() + } + + return cidString } /** @@ -177,9 +187,20 @@ class NFTStorage { * @returns {Promise} */ static async storeDirectory(service, files) { - const { cid, car } = await NFTStorage.encodeDirectory(files) - await NFTStorage.storeCar(service, car) - return cid.toString() + const blockstore = new Blockstore() + let cidString + + try { + const { cid, car } = await NFTStorage.encodeDirectory(files, { + blockstore, + }) + await NFTStorage.storeCar(service, car) + cidString = cid.toString() + } finally { + await blockstore.close() + } + + return cidString } /** @@ -347,14 +368,18 @@ class NFTStorage { * ``` * * @param {Blob} blob + * @param {object} [options] + * @param {BlockstoreI} [options.blockstore] * @returns {Promise<{ cid: CID, car: CarReader }>} */ - static async encodeBlob(blob) { + static async encodeBlob(blob, { blockstore } = {}) { if (blob.size === 0) { throw new Error('Content size is 0, make sure to provide some content') } - - return packCar([{ path: 'blob', content: blob.stream() }], false) + return packCar([{ path: 'blob', content: blob.stream() }], { + blockstore, + wrapWithDirectory: false, + }) } /** @@ -378,9 +403,11 @@ class NFTStorage { * ``` * * @param {Iterable} files + * @param {object} [options] + * @param {BlockstoreI} [options.blockstore] * @returns {Promise<{ cid: CID, car: CarReader }>} */ - static async encodeDirectory(files) { + static async encodeDirectory(files, { blockstore } = {}) { const input = [] let size = 0 for (const file of files) { @@ -394,7 +421,10 @@ class NFTStorage { ) } - return packCar(input, true) + return packCar(input, { + blockstore, + wrapWithDirectory: true, + }) } // Just a sugar so you don't have to pass around endpoint and token around. @@ -604,10 +634,13 @@ For more context please see ERC-721 specification https://eips.ethereum.org/EIPS /** * @param {Array<{ path: string, content: import('./platform.js').ReadableStream }>} input - * @param {boolean} wrapWithDirectory + * @param {object} [options] + * @param {BlockstoreI} [options.blockstore] + * @param {boolean} [options.wrapWithDirectory] */ -const packCar = async (input, wrapWithDirectory) => { - const blockstore = new Blockstore() +const packCar = async (input, { blockstore, wrapWithDirectory } = {}) => { + /* c8 ignore next 1 */ + blockstore = blockstore || new Blockstore() const { root: cid } = await pack({ input, blockstore, wrapWithDirectory }) const car = new BlockstoreCarReader(1, [cid], blockstore) return { cid, car } diff --git a/yarn.lock b/yarn.lock index cac93c41e2..989855549c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4055,6 +4055,16 @@ "@web3-storage/multipart-parser" "^1.0.0" data-uri-to-buffer "^3.0.1" +"@web-std/fetch@^3.0.3": + version "3.0.3" + resolved "https://registry.yarnpkg.com/@web-std/fetch/-/fetch-3.0.3.tgz#507e1371825298aae61172b0da439570437d3982" + integrity sha512-PtaKr6qvw2AmKChugzhQWuTa12dpbogHRBxwcleAZ35UhWucnfD4N+g3f7qYK2OeioSWTK3yMf6n/kOOfqxHaQ== + dependencies: + "@web-std/blob" "^3.0.3" + "@web-std/form-data" "^3.0.2" + "@web3-storage/multipart-parser" "^1.0.0" + data-uri-to-buffer "^3.0.1" + "@web-std/file-url@^1.0.1": version "1.0.1" resolved "https://registry.yarnpkg.com/@web-std/file-url/-/file-url-1.0.1.tgz#41209ec581ee7c97b19222b5daf47f2992f6fdd8"