-
Notifications
You must be signed in to change notification settings - Fork 16
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
Changes from 2 commits
b3e208c
956ff5e
c98a2ca
02592c1
3fa62cb
d24f66e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests currently test the "non-destructive" approach with |
||
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 () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}); | ||
}); |
There was a problem hiding this comment.
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.