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

feat(streams): to ArrayBuffer/Blob/Json/Text #3631

Merged
merged 4 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions streams/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export * from "./reader_from_iterable.ts";
export * from "./reader_from_stream_reader.ts";
export * from "./text_delimiter_stream.ts";
export * from "./text_line_stream.ts";
export * from "./to_array_buffer.ts";
export * from "./to_blob.ts";
export * from "./to_json.ts";
export * from "./to_text.ts";
export * from "./to_transform_stream.ts";
export * from "./writable_stream_from_writer.ts";
export * from "./write_all.ts";
Expand Down
23 changes: 23 additions & 0 deletions streams/to_array_buffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import { concat } from "../bytes/concat.ts";

export async function toArrayBuffer(
readableStream: ReadableStream<Uint8Array>,
): Promise<ArrayBuffer> {
const reader = readableStream.getReader();
const chunks: Uint8Array[] = [];

while (true) {
const { done, value } = await reader.read();

if (done) {
break;
}

chunks.push(value);
}

return concat(...chunks).buffer;
}
18 changes: 18 additions & 0 deletions streams/to_array_buffer_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { assertEquals } from "../assert/assert_equals.ts";
import { toArrayBuffer } from "./to_array_buffer.ts";

Deno.test("[streams] toArrayBuffer", async () => {
const stream = new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(Uint8Array.of(1, 2, 3, 4, 5));
controller.enqueue(Uint8Array.of(6, 7));
controller.enqueue(Uint8Array.of(8, 9));
controller.close();
},
});

const buf = await toArrayBuffer(stream);
assertEquals(buf, Uint8Array.of(1, 2, 3, 4, 5, 6, 7, 8, 9).buffer);
});
21 changes: 21 additions & 0 deletions streams/to_blob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

export async function toBlob(
readableStream: ReadableStream,
): Promise<Blob> {
const reader = readableStream.getReader();
const chunks: Uint8Array[] = [];

while (true) {
const { done, value } = await reader.read();

if (done) {
break;
}

chunks.push(value);
}

return new Blob(chunks);
}
23 changes: 23 additions & 0 deletions streams/to_blob_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { assert } from "../assert/assert.ts";
import { assertEquals } from "../assert/assert_equals.ts";
import { toBlob } from "./to_blob.ts";

Deno.test("[streams] toBlob", async () => {
const stream = new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(Uint8Array.of(1, 2, 3, 4, 5));
controller.enqueue(Uint8Array.of(6, 7));
controller.enqueue(Uint8Array.of(8, 9));
controller.close();
},
});

const blob = await toBlob(stream);
assert(blob instanceof Blob);
assertEquals(
await blob.arrayBuffer(),
Uint8Array.of(1, 2, 3, 4, 5, 6, 7, 8, 9).buffer,
);
});
10 changes: 10 additions & 0 deletions streams/to_json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import { toText } from "./to_text.ts";

export function toJson(
readableStream: ReadableStream,
): Promise<unknown> {
return toText(readableStream).then(JSON.parse);
}
34 changes: 34 additions & 0 deletions streams/to_json_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { assertEquals } from "../assert/assert_equals.ts";
import { toJson } from "./to_json.ts";

const textEncoder = new TextEncoder();

Deno.test("[streams] toJson", async () => {
const byteStream = new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(textEncoder.encode("["));
controller.enqueue(textEncoder.encode("1, 2, 3, 4"));
controller.enqueue(textEncoder.encode("]"));
controller.close();
},
});

assertEquals(await toJson(byteStream), [1, 2, 3, 4]);

const stringStream = new ReadableStream<string>({
start(controller) {
controller.enqueue('{ "a": 2,');
controller.enqueue(' "b": 3,');
controller.enqueue(' "c": 4 }');
controller.close();
},
});

assertEquals(await toJson(stringStream), {
a: 2,
b: 3,
c: 4,
});
});
23 changes: 23 additions & 0 deletions streams/to_text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

const textDecoder = new TextDecoder();

export async function toText(
readableStream: ReadableStream,
): Promise<string> {
const reader = readableStream.getReader();
let result = "";

while (true) {
const { done, value } = await reader.read();

if (done) {
break;
}

result += typeof value === "string" ? value : textDecoder.decode(value);
}

return result;
}
30 changes: 30 additions & 0 deletions streams/to_text_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { assertEquals } from "../assert/assert_equals.ts";
import { toText } from "./to_text.ts";

const textEncoder = new TextEncoder();

Deno.test("[streams] toText", async () => {
const byteStream = new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(textEncoder.encode("hello"));
controller.enqueue(textEncoder.encode(" js "));
controller.enqueue(textEncoder.encode("fans"));
controller.close();
},
});

assertEquals(await toText(byteStream), "hello js fans");

const stringStream = new ReadableStream<string>({
start(controller) {
controller.enqueue("hello");
controller.enqueue(" deno ");
controller.enqueue("world");
controller.close();
},
});

assertEquals(await toText(stringStream), "hello deno world");
});