Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maraisr/piecemeal
🪀 Send your data piecemeal
https://github.com/maraisr/piecemeal
cloudflare incremental incremental-delivery multipart serviceworker workers
Last synced: 3 months ago
JSON representation
🪀 Send your data piecemeal
- Host: GitHub
- URL: https://github.com/maraisr/piecemeal
- Owner: maraisr
- License: mit
- Created: 2021-10-26T03:26:11.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-01-19T02:08:10.000Z (about 1 year ago)
- Last Synced: 2024-10-12T05:28:20.756Z (4 months ago)
- Topics: cloudflare, incremental, incremental-delivery, multipart, serviceworker, workers
- Language: TypeScript
- Homepage:
- Size: 113 KB
- Stars: 24
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
## ⚡ Features
- **Lightweight** — _Does **not** include any dependencies [see](https://npm.anvaka.com/#/view/2d/piecemeal)_.
- **Familiar** — _plugs into any `node:http` or `workers` based environment._
- **Incredible DX** — _passing only an `AsyncIterable | Iterable`._
## ⚙️ Install
```sh
npm add piecemeal
```## 🚀 Usage
> Visit [/examples](/examples) for more info!
#### _Workers_
```ts
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_
```ts
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
#### Module: [`piecemeal/worker`](./src/worker.ts)
The main module used by [Cloudflare Workers](https://workers.cloudflare.com/) — or any
[Service Worker API](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API).> Example over at [/examples/workers](/examples/workers)
#### Module: [`piecemeal/node`](./src/node.ts)
The main module used for a `node` runtime and plugs directly into `node:http` modules.
> Example over at [/examples/polka](/examples/polka)
#### Module: [`piecemeal/message`](./src/message.ts)
A module used to construct messages. Messages are the partial _bits-of-data_ flushed in increments.
#### Module: [`piecemeal`](./src/index.ts)
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](https://github.com/maraisr/meros) — makes reading multipart responses simple
## License
MIT © [Marais Rossouw](https://marais.io)