@s2-dev/resumable-stream
TypeScript icon, indicating that this package has built-in type declarations

1.6.0 • Public • Published

Resumable stream

This package is inspired by Vercel's take on Resumable Streams used in the Chat SDK, except instead of Redis, this relies on S2 providing a basic implementation to create and resume streams.

Usage

To use this package, you need to:

  1. Create an Access token for S2 and a Basin to store all your streams. You can do so by signing up here. Set the created token as S2_ACCESS_TOKEN in your env.

  2. Create a new basin from the basins tab with appropriate retention age for streams and the create-on-append option turned on, and set it as S2_BASIN in your env.

The incoming stream is batched and the batch size can be changed by setting S2_BATCH_SIZE.

To integrate this package with the Chat SDK, checkout the following changes here.

import { createResumableStreamContext } from "resumable-stream";
import { after } from "next/server";

const streamContext = createResumableStreamContext({
  waitUntil: after,  
});

export async function POST(req: NextRequest, { params }: { params: Promise<{ streamId: string }> }) {
  const { streamId } = await params;
  const inputStream = makeTestStream();
  const stream = await streamContext.createNewResumableStream(
    streamId,
    inputStream,
  );
  return new Response(stream, {
    headers: {
      "Content-Type": "text/event-stream",
    },
  });
}

export async function GET(req: NextRequest, { params }: { params: Promise<{ streamId: string }> }) {
  const { streamId } = await params;  
  const stream = await streamContext.resumeExistingStream(
    streamId    
  );
  if (!stream) {
    return new Response("Stream is already done", {
      status: 422,
    });
  }
  return new Response(stream, {
    headers: {
      "Content-Type": "text/event-stream",
    },
  });
}

Type docs

Type docs

Readme

Keywords

none

Package Sidebar

Install

npm i @s2-dev/resumable-stream

Weekly Downloads

1

Version

1.6.0

License

ISC

Unpacked Size

14.8 kB

Total Files

6

Last publish

Collaborators

  • s2-dev