Origin Private File System TS/JS blockstore implementation for IPFS / Helia - for use in the browser.
npm install blockstore-opfs
or
yarn add blockstore-opfs
import { unixfs } from '@helia/unixfs';
import { OPFSBlockstore } from 'blockstore-opfs';
import { createHelia } from 'helia';
(async () => {
try {
const store = new OPFSBlockstore('bs')
await store.open();
const helia = await createHelia({
blockstore: store
})
const fs = unixfs(helia)
const encoder = new TextEncoder()
const cid = await fs.addBytes(encoder.encode('Hello World'))
console.log('Added file:', cid.toString())
const decoder = new TextDecoder()
let text = ''
for await (const chunk of fs.cat(cid)) {
text += decoder.decode(chunk, {
stream: true
})
}
console.log('Added file contents:', text)
} catch (err) {
console.error(err);
}
})();
import { OPFSBlockstore } from 'blockstore-opfs';
import { CID } from 'multiformats/cid';
(async () => {
try {
const store = new OPFSBlockstore('bs')
await store.open();
// Use the store as you would use any Blockstore
const someCid = CID.parse('bafkreigh2akiscaildc6en5ynpwp45fucjk64o4uqa5fmsrzc4i4vqveae')
const someData = new Uint8Array([1, 2, 3, 4, 5]);
await store.put(someCid, someData);
const data = await store.get(someCid);
console.log('Retrieved data:', data);
store.close()
} catch (err) {
console.error(err);
}
})();
- Uses a web worker implementation for compatability with webkit browsers.
- See
src-worker
for the web worker wrapper - this is inlined during the build, seesrc/opfs-worker.ts
- Conforms to interface-blockstore
navigator.storage.estimate()
- Benchmarks
- Sharding
MIT License