LogoPear Docs

bare-stow

Module bundling and harness generation for Bare

stable

bare-stow — Module bundling and harness generation for Bare.

npm i bare-stow

Usage

Given an entry module core.js that exports a start function receiving the IPC stream and an optional ready callback. The function may optionally return (or resolve to) a function that runs before the bundle exits:

module.exports = async function start(ipc, ready) {
  ipc.on('data', (data) => {
    // Handle messages from the host.
  })

  // Optionally call `ready()` to signal readiness early. Otherwise it is
  // signaled automatically when `start()` resolves.
  ready()

  return async function stop() {
    // Clean up before the bundle exits.
  }
}

The ipc argument is a duplex byte stream. Anything written to it is delivered to the host, and 'data' events fire when the host writes back.

Stow it for the bare-sidecar target:

const stow = require('bare-stow')

const entry = new URL('file:///app/core.js')
const out = new URL('file:///app/out/index.js')

for await (const artifact of stow(entry, 'bare-sidecar', out)) {
  console.log(artifact.url.href)
}

Or equivalently from the command line:

$ bare-stow --target bare-sidecar --out ./out/index.js ./core.js

This writes the harness to out and the bundle alongside it. The harness can then be required from the host and booted with start(). The harness awaits the worker's ready signal before resolving, so by the time start() returns the bundle is up:

const harness = require('/app/out/index.js')

const { ipc } = await harness.start()

ipc.write(Buffer.from('hello from the host'))

ipc.on('exit', (code) => {
  console.log('bundle exited with', code)
})

// Request a graceful shutdown.
ipc.destroy()

API

Functions

stow

stow(entry: URL | string, target: Target | TargetName, out: URL | string, opts?: StowOptions): AsyncGenerator<StowArtifact>
Source

Bundle the module graph rooted at entry for target, writing a harness plus bundle to out.

Parameters

ParameterTypeDefaultDescription
entryURL | stringThe entry module to bundle, as a file: URL or path string.
targetTarget | TargetNameThe bundling target: a Target object, or a target name resolved to one (the built-in bare-sidecar and bare-worker, or a bare-stow-target-<name> package).
outURL | stringThe path to write the harness to, as a file: URL or path string; the bundle is written alongside it.
opts?StowOptionsOptions; see StowOptions.

Returns AsyncGenerator<StowArtifact> — An async generator that yields each written artifact's url as it is produced — the harness first, then the bundle, then any offloaded addon or asset files.

Throws

  • The target argument is missing.
  • The out argument is missing.
  • A host in opts.hosts is not supported by the resolved target.

Types

Target

interface Target {
  name: string
  linked: boolean
  offload: boolean | { addons?: boolean; assets?: boolean }
  format: 'bundle' | 'bundle.cjs' | 'bundle.mjs' | 'bundle.json'
  encoding: string | null
  extension: string
  module?: 'esm' | 'cjs'
  hosts: string[]
  generate(context: TargetContext): Artifact[]
}
Source

A bundling target, such as bare-sidecar or bare-worker, describing how to package and boot a bundle on a given host runtime.

TargetContext

interface TargetContext {
  bundleSpecifier: string
  ipc: string
  rpc: string
  module: 'esm' | 'cjs'
  client: RPCClient | null
}
Source

The context passed to a target's generate(), describing the bundle to embed and the RPC wiring to splice in.

RPC

interface RPC {
  name: string
  generate(context: RPCContext): Artifact[]
}
Source

An RPC library adapter that generates the wiring code spliced into a stowed bundle's harness or entry shim.

RPCContext

interface RPCContext {
  ipc: string
  rpc: string
  module: 'esm' | 'cjs'
  role: 'client' | 'server'
}
Source

The context passed to an RPC adapter's generate().

RPCClient

interface RPCClient {
  source: string
  type: string
}
Source

The resolved client RPC wiring, carrying both the runtime source to splice into the harness and the type declaration to splice into the harness's .d.ts.

Artifact

interface Artifact {
  extension?: string
  source: string
}
Source

A single generated source artifact, such as a harness or a type declaration.

StowOptions

interface StowOptions extends Omit<PackOptions, 'offload' | 'linked' | 'resolve' | 'aliases'> {
  client?: RPC | RPCName
  server?: RPC | RPCName
  resolveTarget?(name: string): Target
  resolveRPC?(name: string): RPC
}
Source

Options for stow().

StowArtifact

interface StowArtifact {
  url: URL
}
Source

An artifact written by stow(), yielded once its file has been written.

bare-stow/protocol

Protocol

new Protocol(stream: Duplex)

Source

Attach a Protocol to the given underlying duplex byte stream.

Parameters

ParameterTypeDefaultDescription
streamDuplexThe underlying duplex byte stream to multiplex the control and user-data channels over.

send(type: string, payload?: object): Promise<void>

Source

Send a control frame of type with an optional JSON-serializable payload.

Parameters

ParameterTypeDefaultDescription
typestringThe control frame type, e.g. 'ready', 'exit', 'error', or 'terminate'.
payload?objectAn optional JSON-serializable payload carried with the frame.

Functions

attach(stream: Duplex): Protocol

Source

Attach a Protocol to stream, an underlying duplex byte stream.

Parameters

ParameterTypeDefaultDescription
streamDuplexAny duplex byte stream to attach the protocol to.

Returns Protocol — A Protocol multiplexing control and user-data frames over stream.

Constants and variables

CONTROL: number

Source

The frame type marker identifying a control-channel frame.

USER: number

Source

The frame type marker identifying a user-data-channel frame.

Types

ProtocolEvents

interface ProtocolEvents extends DuplexEvents {
  ready: []
  terminate: []
  exit: [code: number]
}
Source

The events emitted by a Protocol, in addition to the standard duplex stream events.

bare-stow/host

IPC

new IPC(stream: Duplex)

Source

Wrap the host side of a stowed bundle's transport stream.

Parameters

ParameterTypeDefaultDescription
streamDuplexThe host side of a stowed bundle's transport (any duplex byte stream).

ready: Promise<void>

Source

A promise that resolves once the worker has signaled ready, and rejects if the worker errors before then.

terminate(): Promise<number | undefined>

Source

Send a terminate control frame to the worker and resolve with its exit code once it exits.

Returns Promise<number | undefined> — The worker's exit code once it exits.

Functions

wrap(stream: Duplex): IPC

Source

Wrap the host side of a stowed bundle's transport stream, returning an IPC handle with lifecycle helpers layered on top of the Protocol.

Parameters

ParameterTypeDefaultDescription
streamDuplexThe host side of a stowed bundle's transport (any duplex byte stream).

Returns IPC — An IPC handle wrapping stream, with the ready promise and terminate() layered on top of Protocol.

See also

On this page