Skip to content

Stop AsyncIterator/ReadableStream when streaming serialization stops? #76

@yume-chan

Description

@yume-chan

I'm using Cloudflare Workers, where API can return a ReadableStream (via new Response(new ReadableStream(...), { status: 200 }). But I want to return more complex objects containing ReadableStreams, so I used seroval to serialize them:

import { toCrossJSONStream } from "seroval";
import { ReadableStreamPlugin } from "seroval-plugins/web";

export function serializeStream(object: unknown) {
  let cancel!: () => void;
  return new ReadableStream<string>({
    start(controller) {
      cancel = toCrossJSONStream(object, {
        plugins: [ReadableStreamPlugin],
        onParse(node, initial) {
          const line = (initial ? "" : "\n") + JSON.stringify(node);
          // console.log("[server-function]", "onParse", line);
          controller.enqueue(line);
        },
        onDone() {
          // console.log("[server-function]", "onDone");
          controller.close();
        },
      });
    },
    cancel() {
      cancel();
    },
  }).pipeThrough(new TextEncoderStream());
}

However, one thing I lost is, when client disconnects before receiving the full response, the cancel method above will be called, but it doesn't actually cancel the ReadableStream being serialized:

import { toCrossJSONStream } from "seroval";
import { ReadableStreamPlugin } from "seroval-plugins/web";

const cancel = toCrossJSONStream(
  new ReadableStream({
    async start(controller) {
      while (true) {
        await new Promise((resolve) => {
          setTimeout(resolve, 1000);
        });
        console.log("ReadableStream enqueue");
        controller.enqueue(0);
      }
    },
    cancel() {
      console.log("ReadableStream cancel");
    },
  }),
  {
    plugins: [ReadableStreamPlugin],
    onParse(node, initial) {
      console.log("onParse", JSON.stringify(node), initial);
    },
  },
);

setTimeout(() => {
  console.log("call cancel");
  cancel();
}, 5000);

After 5 seconds, it stops printing onParse, but there is no "ReadableStream cancel", and "ReadableStream enqueue" continues indefinitely.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions