Streams

Create and fork

Create a stream with an optional lifecycle, fork a child stream from a source, and remove a stream.

Create

import { Effect } from "effect";
import { Notes } from "./streams.ts";

const ensure = Effect.gen(function* () {
  const notes = yield* Notes;
  const result = yield* notes.create({}, { ttlSeconds: 3600 });
  return result.nextOffset;
});
ResultMeaning
CreatedThe stream is new
ExistsA stream with this id and the same configuration was there

Both results carry nextOffset, contentType, and closed. A different configuration on the same id fails with CreateConflict.

OptionEffect
ttlSecondsTime to live. Each append and each read restarts it
expiresAtA fixed expiry date
closedCreates the stream closed
initialDataWrites the first messages in the same operation

Set ttlSeconds or expiresAt, not both.

Expiry

An expired stream answers StreamNotFound on the next access. On Cloudflare, the object alarm also sweeps expired streams. The Bun host has no sweeper.

A projection that stops reading can lose an expired input. It then stops with history-unavailable. See Faults.

Fork

A fork is a child stream that starts with a prefix of a source stream. Appends to the child do not change the source.

import { Effect } from "effect";
import { Drafts } from "./streams.ts";

const branch = Effect.gen(function* () {
  const drafts = yield* Drafts;
  const head = yield* drafts.head({ draftId: "main" });
  return yield* drafts.fork(
    { draftId: "copy" },
    { draftId: "main" },
    { forkOffset: head.nextOffset },
  );
});

fork(params, source, options?) takes the child parameters, then the source parameters. Both are addresses of one stream class. The child gets every source message up to and including forkOffset. With no forkOffset, the child starts empty.

BackendFork
MemoryShares the prefix
Constrained memoryCopies the prefix
SQLiteShares the prefix
CloudflareSource and child on the same object

A fork never crosses two storage Layers.

ErrorWhen
ForkSourceNotFoundThe source is missing or outside the stream class
CreateConflictThe child id has a different configuration
NotSupportedThe backend has no fork support

See the upstream fork page.

Remove

remove(params) deletes the stream. Later calls on that id answer StreamGone. A source with live forks is kept until its last child is removed.

Over HTTP

MethodOperationAccess
PUTCreatefull
PUT with Stream-Forked-FromForkfull
DELETERemovefull

See Protocol routes.

On this page