Skip to content

Commit 4e37f8e

Browse files
author
Alan Shaw
authored
feat: custom chunk size (#2396)
Since we upped the default chunk size, folks uploading directories with thousands of small files are sometimes finding the worker exceeds resource limits 🤦‍♂️. This allows them to `encodeDirectory` and then `storeCar` with a smaller chunk size to allow the directory to upload successfully. e.g. ```js import { NFTStorage } from 'nft.storage' import { filesFromPaths } from 'files-from-path' const token = 'API_KEY' // your API key from https://nft.storage/manage async function main() { const storage = new NFTStorage({ token }) const files = await filesFromPaths(['/path/to/files']) const { cid, car } = await NFTStorage.encodeDirectory(files) console.log(`File CID: ${cid}`) console.log('Sending file...') await storage.storeCar(car, { maxChunkSize: 1024 * 1024 * 10, // 10MB onStoredChunk: (size) => console.log(`Stored a chunk of ${size} bytes`) }) console.log('✅ Done') } main() ```
1 parent a9e0a24 commit 4e37f8e

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

packages/client/src/lib.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ class NFTStorage {
169169
static async storeCar(
170170
{ endpoint, rateLimiter = globalRateLimiter, ...token },
171171
car,
172-
{ onStoredChunk, maxRetries, decoders, signal } = {}
172+
{ onStoredChunk, maxRetries, maxChunkSize, decoders, signal } = {}
173173
) {
174174
const url = new URL('upload/', endpoint)
175175
const headers = NFTStorage.auth(token)
176-
const targetSize = MAX_CHUNK_SIZE
176+
const targetSize = maxChunkSize || MAX_CHUNK_SIZE
177177
const splitter =
178178
car instanceof Blob
179179
? await TreewalkCarSplitter.fromBlob(car, targetSize, { decoders })

packages/client/src/lib/interface.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ export interface CarStorerOptions extends RequestOptions {
150150
* Maximum times to retry a failed upload. Default: 5
151151
*/
152152
maxRetries?: number
153+
/**
154+
* Maximum chunk size to upload in bytes. Default: 52,428,800
155+
*/
156+
maxChunkSize?: number
153157
/**
154158
* Additional IPLD block decoders. Used to interpret the data in the CAR
155159
* file and split it into multiple chunks. Note these are only required if

packages/client/test/lib.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,29 @@ describe('client', () => {
195195
assert.equal(cid, expectedCid)
196196
})
197197

198+
it('upload CAR with custom chunk size', async function () {
199+
let uploadedChunks = 0
200+
201+
const client = new NFTStorage({ token, endpoint })
202+
203+
const targetSize = 1024 * 1024 * 20
204+
const carReader = await CarReader.fromIterable(
205+
await randomCar(targetSize)
206+
)
207+
208+
const roots = await carReader.getRoots()
209+
const expectedCid = roots[0]?.toString()
210+
211+
const cid = await client.storeCar(carReader, {
212+
maxChunkSize: 1024 * 1024 * 15,
213+
onStoredChunk: () => {
214+
uploadedChunks++
215+
},
216+
})
217+
assert.equal(uploadedChunks, 2)
218+
assert.equal(cid, expectedCid)
219+
})
220+
198221
it('upload CAR with non-default decoder', async () => {
199222
const client = new NFTStorage({ token, endpoint })
200223
const block = await encode({

0 commit comments

Comments
 (0)