multiblob-http

1.2.1 · maintenance · verified Wed Apr 22

multiblob-http is a Node.js library designed to serve content-addressed blobs over HTTP. It provides an HTTP handler, compatible with frameworks like Express, that exposes endpoints for retrieving blobs by hash (`GET /blobs/get/{id}`) and adding new blobs (`POST /blobs/add`). The library ensures efficient content delivery by setting appropriate HTTP headers, including `ETag` (based on the blob hash) and `Expires` (a year in the future), and supports HTTP range requests (RFC 7233) crucial for streaming media and partial content retrieval. The current stable version is 1.2.1, with releases appearing to follow a maintenance cadence, focusing on stability rather than frequent new features. Its primary differentiator is its tight integration with the `multiblob` package for backend storage and its robust handling of HTTP caching mechanisms for immutable, content-addressed data.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart sets up a basic `multiblob-http` server to serve content-addressed blobs from a local directory, demonstrating how to initialize the server and programmatically add a blob.

import MultiBlob from 'multiblob';
import MultiBlobHttp from 'multiblob-http';
import http from 'http';
import path from 'path';
import fs from 'fs';

// Ensure a directory exists for blobs
const dir = path.join(process.cwd(), 'my-blobs');
if (!fs.existsSync(dir)) {
  fs.mkdirSync(dir, { recursive: true });
}

const blobs = MultiBlob(dir);
const port = process.env.PORT || 8000;

http.createServer(MultiBlobHttp(blobs, '/blobs')).listen(port, () => {
  console.log(`multiblob-http server listening on http://localhost:${port}`);
  console.log(`GET blobs: http://localhost:${port}/blobs/get/{hash}`);
  console.log(`POST to add: http://localhost:${port}/blobs/add`);
});

// Example of adding a blob programmatically
async function addExampleBlob() {
  const content = Buffer.from('Hello, multiblob-http!');
  const ws = blobs.createWriteStream();
  ws.end(content);
  const hash = await new Promise((resolve, reject) => {
    ws.on('error', reject);
    ws.on('finish', () => resolve(ws.hash));
  });
  console.log(`Added example blob with hash: ${hash}`);
  console.log(`Try to fetch it: curl http://localhost:${port}/blobs/get/${hash}`);
}

addExampleBlob();

view raw JSON →