npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@s2-dev/streamstore-patterns

v2.1.0

Published

Add-on patterns built on top of the S2 StreamStore TypeScript SDK.

Readme

s2-typescript-patterns

Add-on patterns and higher-level helpers built on top of the @s2-dev/streamstore TypeScript SDK.

Installation

npm install @s2-dev/streamstore-patterns @s2-dev/streamstore
# or: pnpm add @s2-dev/streamstore-patterns @s2-dev/streamstore
# or: yarn add @s2-dev/streamstore-patterns @s2-dev/streamstore

Structure

  • src/patterns: Reusable patterns/add-ons that wrap or compose types from @s2-dev/streamstore.
  • src/index.ts: Package entrypoint that re-exports everything from src/patterns.
  • examples: Small, focused usage examples that can combine any of the patterns.

Try the examples

In a loop, synchronously write a message to a stream and read it back.

export DEBUG="patterns:*"
export S2_ACCESS_TOKEN="token"
export S2_BASIN="your-basin"
S2_STREAM="test/read-write/0001" npx tsx packages/patterns/examples/read-write.ts

Use the AI SDK to generate a response to a prompt. Tee the resulting textStream to the console and an s2 stream. Then, read through the s2 stream to replay the stream.

export DEBUG="patterns:*"
export S2_ACCESS_TOKEN="token"
export S2_BASIN="your-basin"

S2_STREAM="agent/session/0001" npx tsx packages/patterns/examples/ai-sdk.ts

Typed append/read sessions

The main @s2-dev/streamstore SDK provides idiomatic access to the core S2 APIs, as well as functionality like retries.

This package, on the other hand, provides some basic implementations of advanced functionality that can be built off of the core SDK. These may be useful out of the box, or could serve simply as examples for building a system on top of S2.

What type of functionality?

The core S2 API essentially provides durable streams of binary records, each of which may not exceed 1MiB. Furthermore, while retries can be configured on the core SDK, retrying failed appends (without first inspecting the stream) can result in duplicate records.

The SerializingAppendSession<Message> and DeserializingReadSession<Message> patterns provide an opinionated way to address all of these issues, by:

  • Being strongly typed around a Message
  • Splitting large messages into < 1MiB chunks, and "framing" those messages across multiple S2 records
  • Injecting deduplication headers (_dedupe_seq, _writer_id), to act as idempotency keys and allow readers to efficiently detect and filter repeated records caused by retried appends

Write path (append):

  • MessageUint8Array – you supply a serializer (e.g. JSON, MessagePack).
  • Uint8ArrayUint8Array[]chunkBytes splits into bounded-size chunks (MAX_CHUNK_BODY_BYTES) so each S2 record stays under the per-record limit.
  • Uint8Array[]AppendRecord[]frameChunksToRecords adds framing headers (_frame_bytes, _frame_records) to the first record to describe the whole message.
  • AppendRecord[]AppendRecord[] with _dedupe_seq and _writer_idinjectDedupeHeaders adds a monotonically increasing dedupe sequence per record, scoped to a unique writer id.
  • AppendRecord[] → sent to S2 – SerializingAppendSession submits each record, optionally using matchSeqNum sequencing.

Read path (consume):

  • S2 records → ReadSession<"bytes"> – provided by the @s2-dev/streamstore SDK.
  • Records → filtered records – DedupeFilter drops duplicates based on the pair (_writer_id, _dedupe_seq).
  • Filtered records → CompletedFrameFrameAssembler uses _frame_bytes / _frame_records to reassemble full payloads.
  • CompletedFrame.payload (Uint8Array) → Message – you supply a deserializer.
  • Messages → ReadableStream<Message>DeserializingReadSession wraps the read session and yields typed messages.

All of these are grouped under the serialization namespace.

Usage (library)

import { serialization } from "@s2-dev/streamstore-patterns";
import { S2 } from "@s2-dev/streamstore";
// Serializer/deserializer (bring your own; MessagePack shown here)
import { encode, decode } from "@msgpack/msgpack";

type ChatMessage = {
  userId: string;
  text: string;
};

async function main() {
  const s2 = new S2({ accessToken: process.env.S2_ACCESS_TOKEN! });
  const stream = s2.basin("your-basin").stream("your-stream");

  // Write: serialize, chunk, frame, dedupe, and append.
  const appendSession = new serialization.SerializingAppendSession<ChatMessage>(
    await stream.appendSession(),
    (msg) => encode(msg),
    { dedupeSeq: 0n },
  );

  await appendSession.submit({ userId: "alice", text: "hello" });

  // Read: dedupe, reassemble frames, and deserialize.
  const readSession = new serialization.DeserializingReadSession<ChatMessage>(
    await stream.readSession({ tail_offset: 0, as: "bytes" }),
    (bytes) => decode(bytes) as ChatMessage,
  );

  for await (const msg of readSession) {
    console.log(`[${msg.userId}] ${msg.text}`);
  }
}

main().catch((error) => {
  console.error(error);
  process.exit(1);
});

Using WritableStream for streaming sources

SerializingAppendSession is also a WritableStream<Message>, so you can pipe any ReadableStream<Message> into S2 with backpressure:

const append = new serialization.SerializingAppendSession<ChatMessage>(
  await stream.appendSession({ maxInflightBatches: 10 }),
  (msg) => encode(msg),
  { dedupeSeq: 0n },
);

// readableMessages: ReadableStream<ChatMessage>
await readableMessages.pipeTo(append);
// If this resolves without throwing, the entire stream was durably appended.