filecoin-pin
Version:
Bridge IPFS content to Filecoin Onchain Cloud using familiar tools
62 lines • 2.13 kB
JavaScript
/**
* Browser-compatible in-memory storage backend for CAR files.
* Collects CAR chunks in memory for later retrieval.
*/
import { CarWriter } from '@ipld/car';
import toBuffer from 'it-to-buffer';
/**
* Memory-based storage backend that collects CAR chunks
*/
export class CARMemoryBackend {
carWriter = null;
carChunks = [];
async initialize(rootCID) {
// Create CAR writer channel
const { writer, out } = CarWriter.create([rootCID]);
this.carWriter = writer;
(async () => {
for await (const chunk of out) {
this.carChunks.push(chunk);
}
})().catch(() => {
// Ignore errors during collection
});
// Wait for the header to be written
await this.carWriter._mutex;
// Calculate header size from what's been written so far
const headerSize = this.carChunks.reduce((sum, chunk) => sum + chunk.length, 0);
return { headerSize };
}
async writeBlock(cid, block, _offset) {
// Write block to CAR
await this.carWriter?.put({ cid, bytes: block });
}
// biome-ignore lint/correctness/useYield: This method throws immediately and intentionally never yields
async *readBlock(_cid, _offset) {
throw new Error('Not implemented for CAR blockstore in the browser.');
}
async finalize() {
// Close the CAR writer to signal no more data
if (this.carWriter != null) {
await this.carWriter.close();
this.carWriter = null;
}
// Wait a tick for any pending chunks to be collected
await new Promise((resolve) => setTimeout(resolve, 0));
}
async cleanup() {
if (this.carWriter != null) {
await this.carWriter.close();
}
// Clear chunks to free memory
this.carChunks.length = 0;
}
/**
* Get the complete CAR file as Uint8Array
* Browser-specific method for retrieving the in-memory CAR
*/
getCarBytes() {
return toBuffer(this.carChunks);
}
}
//# sourceMappingURL=car-memory-backend.js.map