Drive Bundler

2.6.0 · active · verified Tue Apr 21

drive-bundler is a JavaScript module designed for extracting bundles of files, code, and assets from Hyperdrives or Localdrives within the Holepunch peer-to-peer ecosystem. As of version 2.6.0, it provides programmatic access to traverse a 'drive' (a distributed or local file system abstraction) and gather specified entrypoints and their dependencies into a bundled output. This library is a foundational component for building serverless, local-first, and offline-capable applications that leverage Hypercore Protocol's decentralized data structures. It differs from traditional web bundlers (like Webpack or Parcel) by operating directly on a distributed file system interface, rather than a local file system, making it crucial for distributing P2P applications and their updates efficiently. While there's no explicit fixed release cadence, the broader Holepunch ecosystem is under active development, implying regular updates and maintenance for its core modules.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to create a temporary Localdrive, populate it with some files, and then use `DriveBundler` to extract a bundle based on a specified entrypoint, showing the resolved paths and source content.

import Localdrive from 'localdrive';
import DriveBundler from 'drive-bundler';
import RAM from 'random-access-memory';
import { promises as fs } from 'fs';
import path from 'path';

async function createTempLocaldrive() {
  const root = await fs.mkdtemp(path.join(process.cwd(), 'temp-drive-'));
  const drive = new Localdrive(root, { 
    storage: (file) => new RAM()
  });

  // Add some dummy files to the localdrive
  await fs.writeFile(path.join(root, 'index.js'), 'export * from "./lib";');
  await fs.mkdir(path.join(root, 'lib'));
  await fs.writeFile(path.join(root, 'lib', 'main.js'), 'export const hello = "world";');
  await fs.writeFile(path.join(root, 'package.json'), JSON.stringify({
    name: 'my-project',
    main: 'index.js'
  }));

  console.log(`Created temporary localdrive at ${root}`);
  return drive;
}

async function bundleExample() {
  const drive = await createTempLocaldrive();

  try {
    const b = new DriveBundler(drive);

    // Bundle a specific entrypoint, e.g., 'index.js'
    const { entrypoint, resolutions, sources } = await b.bundle('/index.js');

    console.log('Bundling successful!');
    console.log('Entrypoint:', entrypoint);
    console.log('Resolved Paths:', Object.keys(resolutions));
    console.log('Source files bundled:', Object.keys(sources));

    // Example: Accessing a bundled source
    if (sources['/lib/main.js']) {
      console.log('Content of /lib/main.js:', sources['/lib/main.js'].toString());
    }

  } finally {
    // Clean up the temporary drive resources
    // In a real app, ensure proper drive closing and resource management.
    console.log('Finished bundling example.');
  }
}

bundleExample().catch(console.error);

view raw JSON →