Skip to content

createPieceBatcher

createPieceBatcher(client, options): PieceBatcher

Defined in: packages/synapse-core/src/sp/create-piece-batcher.ts:232

Create a stateful piece batcher that parks/pulls immediately and coalesces addPieces.

upload runs per-piece I/O right away (streaming CommP-last protocol for bytes and streams). pull calls are coalesced into pull requests of up to SIZE_CONSTANTS.PULL_BATCH_SIZE pieces, each with one signature and one polling loop; each pull promise still settles with its own piece’s status, and a full SP pull queue (429) is waited out. The tumbling window batches the on-chain addPieces (or createDataSetAndAddPieces) call. Pull authorization matches the operation for the pieces in that pull request; flush signs new extraData for the whole window.

Invalid metadata or an out-of-bounds PieceCID size rejects only that piece when it is passed to upload/pull/enqueue, before any network work; it never joins a pull request or a window. A failed flush (signing, RPC, SP or chain errors) rejects every piece in the window with AddPiecesFlushError.

For crash recovery, persist each window from onSubmitted and pass it to resume() after a restart instead of adding the same pieces again.

Parameter Type Description
client Client<Transport, Chain, Account> Wallet client used to sign and submit.
options OptionsType createPieceBatcher.OptionsType

PieceBatcher

Batcher createPieceBatcher.ReturnType

import { createPieceBatcher } from '@filoz/synapse-core/sp'

const batcher = createPieceBatcher(client, { dataSet })
await Promise.all([
  batcher.upload({ data: fileA }),
  batcher.pull({ pieceCid, sourceUrl }),
])
await batcher.close()