multiblob-http
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
-
TypeError: MultiBlob is not a function
cause The `multiblob` dependency was not installed or incorrectly imported/initialized before being passed to `MultiBlobHttp`.fixInstall `multiblob` (`npm install multiblob`) and ensure it's imported correctly: `const MultiBlob = require('multiblob')` or `import MultiBlob from 'multiblob'`. -
Error: listen EADDRINUSE: address already in use :::8000
cause The specified port (e.g., 8000) is already being used by another process on your system.fixChoose a different port for your HTTP server, or identify and terminate the process currently using the port. -
Cannot GET /blobs/get/{hash} (404 Not Found)cause The requested blob with the specified hash does not exist in the configured `multiblob` store, or the URL path/prefix is incorrect.fixVerify the hash is correct and corresponds to an existing blob. Check that the `prefix` argument passed to `MultiBlobHttp(blobs, '/blobs')` matches the URL path you are requesting (e.g., `/blobs`). -
ReferenceError: require is not defined
cause Attempting to use `require()` in an ES Module (ESM) context without proper configuration for CommonJS interop.fixConvert `require('multiblob-http')` to `import MultiBlobHttp from 'multiblob-http'` (and similarly for `multiblob`) if your project is ESM-first. Ensure your `package.json` has `"type": "module"` if using `.js` files as ESM.
Warnings
- gotcha The README and most examples use CommonJS `require()`. While Node.js supports CommonJS in ESM projects, explicit ESM import paths (`import MultiBlobHttp from 'multiblob-http'`) might be preferred for modern applications. Ensure your build system or Node.js configuration handles CJS interop correctly.
- breaking The package explicitly depends on `multiblob`. If `multiblob` is not installed or incorrectly configured, `multiblob-http` will fail to initialize or serve content. Ensure `multiblob` is a direct dependency and properly instantiated.
- gotcha The `engines` field in `package.json` specifies `"node": ">=12"`. While the library might function on newer Node.js versions, it indicates a lack of explicit testing or consideration for potential breaking changes in very recent Node.js releases beyond version 12.
- gotcha The `POST /add` endpoint currently responds with the hash but does not validate if the received content matches a hash specified in the URL (e.g., `POST /add/{id}`). This 'TODO' in the README implies a potential for mismatch if the client provides a hash that doesn't match the content.
Install
-
npm install multiblob-http -
yarn add multiblob-http -
pnpm add multiblob-http
Imports
- MultiBlobHttp
const { MultiBlobHttp } = require('multiblob-http')const MultiBlobHttp = require('multiblob-http') - MultiBlobHttp (ESM)
import MultiBlobHttp from 'multiblob-http'
- HTTP Handler
http.createServer(MultiBlobHttp).listen(8000)
http.createServer(MultiBlobHttp(blobs, '/blobs')).listen(8000)
Quickstart
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();