Skip to content
UniKVS
Esc
↑↓navigate↵open⌘Jpreview
On this page

Using Streams

How to read and write large values in chunks with ReadableStream.

For large byte sequences, read and write them in chunks as streams.

Define a Stream Key

Define stream keys with StreamValue or Value. stream() cannot be used on PlainValue keys.

import { Memory } from "@unikvs/memory";
import * as unikvs from "unikvs";

const kvs = new unikvs.Builder<{
  logs: unikvs.StreamValue<Uint8Array>;
}>()
  .persist(new Memory())
  .build();

await kvs.open();

Write a Stream

Pass a ReadableStream to set(). The memory storage chunk type is Uint8Array.

const stream = new ReadableStream<Uint8Array>({
  start(controller) {
    controller.enqueue(new Uint8Array([0x01, 0x02]));
    controller.enqueue(new Uint8Array([0x03, 0x04]));
    controller.close();
  },
});

await kvs.set("logs", stream);

Read a Stream

stream() returns a ValueStream. Process chunks in order with for await...of.

for await (const chunk of await kvs.stream("logs")) {
  console.log(chunk); // Uint8Array
}

Dispose of streams when done. Use await using for automatic disposal or call dispose().

await using vs = await kvs.stream("logs");
const reader = vs.getReader();
const { value } = await reader.read();
console.log(value);

Check Support

Stream support and chunk types vary by backend. For example, Local Storage, Cookie, Memcached, and Cloudflare KV backends do not support streams. Check the data types in each package reference before use.

Was this page helpful?