Skip to content

dozyio/js-blockstore-opfs

Repository files navigation

OPFS Blockstore

Origin Private File System TS/JS blockstore implementation for IPFS / Helia - for use in the browser.

Install

npm install blockstore-opfs

or

yarn add blockstore-opfs

Helia Usage

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);
  }
})();

Standalone Usage

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);
  }
})();

Implementation Details

  • Uses a web worker implementation for compatability with webkit browsers.
  • See src-worker for the web worker wrapper - this is inlined during the build, see src/opfs-worker.ts
  • Conforms to interface-blockstore

View storage quota

navigator.storage.estimate()

Todo

  • Benchmarks
  • Sharding

OPFS Links

License

MIT License

About

OPFS Blockstore for IPFS / Helia

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published