filecoin-pin
Version:
Bridge IPFS content to Filecoin Onchain Cloud using familiar tools
51 lines • 1.55 kB
JavaScript
/**
* Browser-compatible CAR Blockstore
* Writes blocks to an in-memory CAR structure instead of a file
*/
import { CARBlockstoreBase } from './car-blockstore-base.js';
import { CARMemoryBackend } from './car-memory-backend.js';
/**
* A blockstore that writes blocks to an in-memory CAR structure
* This eliminates the need for redundant storage during IPFS operations in the browser
*
* @example
* ```ts
* import { CARWritingBlockstore } from './browser-car-blockstore.js'
* import { CID } from 'multiformats/cid'
*
* // Create with a placeholder or actual root CID
* const blockstore = new CARWritingBlockstore({
* rootCID: someCID,
* })
*
* await blockstore.initialize()
*
* // Add blocks (same as Node.js version)
* await blockstore.put(cid, blockData)
*
* // Finalize when done
* await blockstore.finalize()
*
* // Get the complete CAR file
* const carBytes = blockstore.getCarBytes() // Uint8Array ready for upload
* ```
*/
export class CARWritingBlockstore extends CARBlockstoreBase {
memoryBackend;
constructor(options) {
const backend = new CARMemoryBackend();
super(options.rootCID, backend);
this.memoryBackend = backend;
}
/**
* Get the complete CAR file as Uint8Array
* Can only be called after finalize()
*/
getCarBytes() {
if (!this.finalized) {
throw new Error('Cannot get CAR bytes before finalization');
}
return this.memoryBackend.getCarBytes();
}
}
//# sourceMappingURL=browser-car-blockstore.js.map