Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream ReadableStream instances #51

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/flatten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
TYPE_NULL_OBJECT,
TYPE_PREVIOUS_RESOLVED,
TYPE_PROMISE,
TYPE_STREAM,
TYPE_REGEXP,
TYPE_SET,
TYPE_SYMBOL,
Expand All @@ -39,7 +40,7 @@ export function flatten(this: ThisEncode, input: unknown): number | [number] {
}

function stringify(this: ThisEncode, input: unknown, index: number) {
const { deferred, plugins, postPlugins } = this;
const { deferred, plugins, postPlugins, streams } = this;
const str = this.stringified;

const stack: [unknown, number][] = [[input, index]];
Expand Down Expand Up @@ -136,6 +137,22 @@ function stringify(this: ThisEncode, input: unknown, index: number) {
} else if (input instanceof Promise) {
str[index] = `["${TYPE_PROMISE}",${index}]`;
deferred[index] = input;
} else if (input instanceof ReadableStream) {
str[index] = `["${TYPE_STREAM}",${index}]`;
// during this, the stream will be read, which is destructive
// so we tee it, and replace the methods on the original stream
// with methods forwarding to the left teed stream, while
// processing the right teed stream ourselves
const [left, right] = input.tee();
input.getReader = left.getReader.bind(left);
input.cancel = left.cancel.bind(left);
input.pipeThrough = left.pipeThrough.bind(left);
input.pipeTo = left.pipeTo.bind(left);
input.tee = left.tee.bind(left);
Object.defineProperty(input, "locked", {
get: () => left.locked,
});
streams[index] = right;
Comment on lines +146 to +155
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be omitted if there were a consensus that encoding is a destructive action - React does so - but I honestly felt a bit more comfortable with this tee approach.

Suggested change
const [left, right] = input.tee();
input.getReader = left.getReader.bind(left);
input.cancel = left.cancel.bind(left);
input.pipeThrough = left.pipeThrough.bind(left);
input.pipeTo = left.pipeTo.bind(left);
input.tee = left.tee.bind(left);
Object.defineProperty(input, "locked", {
get: () => left.locked,
});
streams[index] = right;
streams[index] = input;

} else if (input instanceof Error) {
str[index] = `["${TYPE_ERROR}",${JSON.stringify(input.message)}`;
if (input.name !== "Error") {
Expand Down
193 changes: 193 additions & 0 deletions src/turbo-stream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,3 +572,196 @@ test("should allow many nested promises without a memory leak", async () => {
expect(currentDecoded.i).toBe(depth - 1);
await decoded.done;
});

test.describe("ReadableStream", () => {
test("basic usage", async () => {
const input = new ReadableStream({
start(controller) {
controller.enqueue("foo");
controller.enqueue("bar");
setTimeout(() => {
controller.enqueue("foo");
controller.enqueue({ loba: "boba" });
controller.close();
}, 10);
},
});

const decoded = await decode(encode(input));

const inputReader = input.getReader();
const decodedReader = (decoded.value as typeof input).getReader();

{
const inputValue = await inputReader.read();
const decodedValue = await decodedReader.read();
expect(inputValue).toEqual(decodedValue);
expect(inputValue).toEqual({ value: "foo", done: false });
}
{
const inputValue = await inputReader.read();
const decodedValue = await decodedReader.read();
expect(inputValue).toEqual(decodedValue);
expect(inputValue).toEqual({ value: "bar", done: false });
}
{
const inputValue = await inputReader.read();
const decodedValue = await decodedReader.read();
expect(inputValue).toEqual(decodedValue);
expect(inputValue).toEqual({ value: "foo", done: false });
}
{
const inputValue = await inputReader.read();
const decodedValue = await decodedReader.read();
expect(inputValue).toEqual(decodedValue);
expect(inputValue).toEqual({ value: { loba: "boba" }, done: false });
}
{
const inputValue = await inputReader.read();
const decodedValue = await decodedReader.read();
expect(inputValue).toEqual(decodedValue);
expect(inputValue).toEqual({ value: undefined, done: true });
}
{
const inputValue = await inputReader.closed;
const decodedValue = await decodedReader.closed;
expect(inputValue).toEqual(decodedValue);
expect(inputValue).toEqual(undefined);
}
await decoded.done;
});

test("errors", async () => {
const input = new ReadableStream({
start(controller) {
controller.enqueue("foo");
controller.enqueue("bar");
setTimeout(() => {
controller.enqueue("foo");
controller.enqueue({ loba: "boba" });
setTimeout(() => {
controller.error(new Error("baz"));
}, 1);
}, 10);
},
});

const decoded = await decode(encode(input));

const inputReader = input.getReader();
const decodedReader = (decoded.value as typeof input).getReader();

{
const inputValue = await inputReader.read();
const decodedValue = await decodedReader.read();
Comment on lines +655 to +656
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests currently test the "non-destructive" approach with tee, which makes it possible to still consume the original input stream.

expect(inputValue).toEqual(decodedValue);
expect(inputValue).toEqual({ value: "foo", done: false });
}
{
const inputValue = await inputReader.read();
const decodedValue = await decodedReader.read();
expect(inputValue).toEqual(decodedValue);
expect(inputValue).toEqual({ value: "bar", done: false });
}
{
const inputValue = await inputReader.read();
const decodedValue = await decodedReader.read();
expect(inputValue).toEqual(decodedValue);
expect(inputValue).toEqual({ value: "foo", done: false });
}
{
const inputValue = await inputReader.read();
const decodedValue = await decodedReader.read();
expect(inputValue).toEqual(decodedValue);
expect(inputValue).toEqual({ value: { loba: "boba" }, done: false });
}
{
const expected = new Error("baz");
await expect(inputReader.closed).rejects.toEqual(expected);
await expect(decodedReader.closed).rejects.toEqual(expected);
}
await decoded.done;
});

test("streams in a stream", async () => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this works now 🤣

const nested1 = new ReadableStream({
start(controller) {
controller.enqueue("a");
controller.enqueue("b");
controller.close();
},
});
const nested2 = new ReadableStream({
start(controller) {
controller.enqueue("1");
controller.enqueue("2");
controller.close();
},
});
const input = new ReadableStream({
start(controller) {
controller.enqueue(nested1);
controller.enqueue(nested2);
controller.close();
},
});

const decoded = await decode(encode(input));

const inputReader = input.getReader();
const decodedReader = (decoded.value as typeof input).getReader();
{
const inputValue = (await inputReader.read()).value as ReadableStream;
const decodedValue = (await decodedReader.read()).value as ReadableStream;
expect(inputValue).toBe(nested1);
const inputNestedReader = inputValue.getReader();
const decodedNestedReader = decodedValue.getReader();
{
const inputValue = await inputNestedReader.read();
const decodedValue = await decodedNestedReader.read();
expect(inputValue).toEqual(decodedValue);
expect(inputValue).toEqual({ value: "a", done: false });
}
{
const inputValue = await inputNestedReader.read();
const decodedValue = await decodedNestedReader.read();
expect(inputValue).toEqual(decodedValue);
expect(inputValue).toEqual({ value: "b", done: false });
}
{
const inputValue = await inputNestedReader.read();
const decodedValue = await decodedNestedReader.read();
expect(inputValue).toEqual(decodedValue);
expect(inputValue).toEqual({ value: undefined, done: true });
}
}

{
const inputValue = (await inputReader.read()).value as ReadableStream;
const decodedValue = (await decodedReader.read()).value as ReadableStream;
expect(inputValue).toBe(nested2);
const inputNestedReader = inputValue.getReader();
const decodedNestedReader = decodedValue.getReader();
{
const inputValue = await inputNestedReader.read();
const decodedValue = await decodedNestedReader.read();
expect(inputValue).toEqual(decodedValue);
expect(inputValue).toEqual({ value: "1", done: false });
}
{
const inputValue = await inputNestedReader.read();
const decodedValue = await decodedNestedReader.read();
expect(inputValue).toEqual(decodedValue);
expect(inputValue).toEqual({ value: "2", done: false });
}
{
const inputValue = await inputNestedReader.read();
const decodedValue = await decodedNestedReader.read();
expect(inputValue).toEqual(decodedValue);
expect(inputValue).toEqual({ value: undefined, done: true });
}
}

await decoded.done;
});
});
Loading