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

Add readable byte streams #1362

Merged
merged 13 commits into from
Aug 4, 2022
2 changes: 1 addition & 1 deletion inputfiles/addedTypes.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,7 @@
},
"autoAllocateChunkSize": {
"name": "autoAllocateChunkSize",
"overrideType": "number"
"type": "unsigned long long"
},
"start": {
"name": "start",
Expand Down
2 changes: 1 addition & 1 deletion inputfiles/overridingTypes.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -2353,7 +2353,7 @@
{
"name": "underlyingSource",
"optional": false,
"overrideType": "UnderlyingByteSource"
"type": "UnderlyingByteSource"
},
{
"name": "strategy",
Expand Down
59 changes: 59 additions & 0 deletions unittests/files/streams.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
function assertType<T>(_x: T) {}

const readableStream = new ReadableStream<string>({
start(controller) {
assertType<ReadableStreamDefaultController<string>>(controller);
controller.desiredSize;
controller.enqueue("a");
},
pull(controller) {
assertType<ReadableStreamDefaultController<string>>(controller);
controller.enqueue("b");
controller.close();
},
cancel(reason) {
assertType<any>(reason);
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this really do anything? 😄 (But probably good to be explicit)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

¯\_(ツ)_/¯

},
});

const defaultReader1 = readableStream.getReader();
assertType<ReadableStreamDefaultReader<string>>(defaultReader1);
defaultReader1.read().then((result) => {
assertType<ReadableStreamReadResult<string>>(result);
if (!result.done) {
result.value.charAt(0);
}
Expand All @@ -19,11 +28,14 @@ defaultReader1.read().then((result) => {
const readableByteStream = new ReadableStream({
type: "bytes",
start(controller) {
assertType<ReadableByteStreamController>(controller);
controller.desiredSize;
controller.byobRequest;
},
pull(controller) {
assertType<ReadableByteStreamController>(controller);
if (controller.byobRequest) {
assertType<ReadableStreamBYOBRequest>(controller.byobRequest);
controller.byobRequest.view;
controller.byobRequest.respond(1);
controller.byobRequest.respondWithNewView(new Uint32Array(1));
Expand All @@ -32,20 +44,67 @@ const readableByteStream = new ReadableStream({
}
controller.close();
},
cancel(reason) {
assertType<any>(reason);
},
});

const defaultReader2 = readableByteStream.getReader();
assertType<ReadableStreamDefaultReader<Uint8Array>>(defaultReader2);
defaultReader2.read().then((result) => {
assertType<ReadableStreamReadResult<Uint8Array>>(result);
if (!result.done) {
result.value.buffer;
result.value[0];
}
});

const byobReader = readableByteStream.getReader({ mode: "byob" });
assertType<ReadableStreamBYOBReader>(byobReader);
byobReader.read(new Uint8Array(1));
byobReader.read(new DataView(new ArrayBuffer(1))).then((result) => {
assertType<ReadableStreamReadResult<DataView>>(result);
if (!result.done) {
result.value.getUint8(0);
}
});

const writableStream = new WritableStream<number>({
start(controller) {
assertType<WritableStreamDefaultController>(controller);
},
write(chunk, controller) {
assertType<number>(chunk);
assertType<WritableStreamDefaultController>(controller);
chunk.toFixed(3);
controller.error("boom!");
},
close() {},
abort(reason) {
assertType<any>(reason);
},
});

const defaultWriter = writableStream.getWriter();
assertType<WritableStreamDefaultWriter<number>>(defaultWriter);
defaultWriter.write(42);
defaultWriter.close();
defaultWriter.abort("boom!");

const transformStream = new TransformStream<string, number>({
start(controller) {
assertType<TransformStreamDefaultController<number>>(controller);
},
transform(chunk, controller) {
assertType<string>(chunk);
assertType<TransformStreamDefaultController<number>>(controller);
controller.enqueue(chunk.length);
controller.error("boom!");
},
flush(controller) {
assertType<TransformStreamDefaultController<number>>(controller);
controller.terminate();
},
});
assertType<ReadableStream<number>>(transformStream.readable);
assertType<WritableStream<string>>(transformStream.writable);