Skip to content

maraisr/piecemeal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

40 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

piecemeal

npm add piecemeal makes incremental delivery possible


⚑ Features

  • Lightweight β€” Does not include any dependencies see.

  • Familiar β€” plugs into any node:http or workers based environment.

  • Incredible DX β€” passing only an AsyncIterable | Iterable.

βš™οΈ Install

npm add piecemeal

πŸš€ Usage

Visit /examples for more info!

Workers

import * as Piecemeal from 'piecemeal/worker';

// Some sort of data access
// ~> here we read from KV, but can be anything
async function* get_data(binding: KV.Namespace) {
  const list = await DATA.list();

  for await (let item of list.keys) {
    yield await DATA.get(item);
  }
}

// A handler you'd typically give a Module or Service Worker
const handler = (request, env, context) => {
  // Notice we're not awaiting or spreading this iterable
  const data = get_data(context.bindings.DATA);

  // Sets up our stream and constructs the Response
  const { pipe, response } = Piecemeal.stream(data);

  // Defers the execution of the iterable, so we respond super quick
  context.waitUntil(pipe());

  return response;
};

Node

import { createServer } from 'node:http';

import * as Piecemeal from 'piecemeal/node';

// An example of some method to retreive some database data
async function* get_data() {
  const keys = await db.fetchAllKeys();

  for await (let key of keys) {
    yield await db.read(key);
  }
}

createServer((req, res) => {
  // Notice we're not awaiting or spreading this iterable
  const data = get_data();

  // Creates a streams β€” and kicks off the iterable.
  // assumes JSON (can override)
  const stream = Piecemeal.stream(data);

  // Pipes the stream directly to a ServerResponse
  stream.pipe(res);
}).listen(8080);

πŸ”Ž API

The main module used by Cloudflare Workers β€” or any Service Worker API.

Example over at /examples/workers

The main module used for a node runtime and plugs directly into node:http modules.

Example over at /examples/polka

A module used to construct messages. Messages are the partial bits-of-data flushed in increments.

Module: piecemeal

A main module one can use to build out custom runtimes β€” exposes all the building blocks to generate a stream supplying the Iterable and a write method.

πŸ™Š Caveats

  • Workers doesn't abort any iterables if connection is dropped. πŸ˜”

Related

  • meros β€” makes reading multipart responses simple

License

MIT Β© Marais Rossouw