Hypercore Blob Server

1.12.0 · active · verified Sun Apr 19

hypercore-blob-server provides an HTTP server specifically designed for streaming data (blobs and files) from Hypercore and Hyperdrive instances. It allows applications to expose content from the peer-to-peer Holepunch ecosystem over standard HTTP, making it accessible to traditional web clients or tools. Currently at version 1.12.0, it is an actively developed part of the Holepunch (formerly Dat Project) ecosystem, offering a more flexible successor to 'serve-drive'. The package differentiates itself by tightly integrating with Corestore for data management, supporting partial content delivery via HTTP Range headers, and generating direct links for both raw Hypercore blobs and Hyperdrive files, facilitating interoperability between decentralized data structures and conventional web infrastructure.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the full flow of initializing a Corestore, creating a Hypercore, appending data, starting a BlobServer, and generating a shareable link to the content.

const BlobServer = require('hypercore-blob-server')
const Corestore = require('corestore')
const Hypercore = require('hypercore')

async function startServer () {
  // Initialize a Corestore for data management and persistence
  const store = new Corestore('./my-hypercore-data')
  await store.ready()

  // Create a Hypercore instance and append some data to it
  const core = store.get({ name: 'my-test-feed' })
  await core.ready()
  await core.append(Buffer.from('Hello, Hypercore blob server!'))

  // Instantiate the BlobServer with the initialized corestore
  const server = new BlobServer(store, {
    port: 49833,
    host: '127.0.0.1',
    // For production deployments, consider adding a 'token' for security:
    // token: process.env.BLOB_SERVER_TOKEN ?? ''
  })

  // Start the HTTP server to listen for incoming requests
  await server.listen()
  const address = server.server.address()
  console.log(`BlobServer listening on http://${address.address}:${address.port}`)

  // Generate a public URL link to the appended blob data
  // The 'blob' option requires precise byte offsets and lengths.
  const link = server.getLink(core.key, {
    blob: { blockOffset: 0, blockLength: 1, byteOffset: 0, byteLength: 29 },
    type: 'text/plain'
  })
  console.log('Access your Hypercore blob data here:', link)

  // To gracefully stop the server and close the corestore:
  // await server.suspend()
  // await store.close()
}

startServer().catch(console.error)

view raw JSON →