From a550998a6f0dabb8b5377aea23bad139f2295482 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Wed, 18 Sep 2024 19:57:38 +1000 Subject: [PATCH] feat(streams/unstable): `toBytes()` (#6011) --- _tools/check_docs.ts | 1 + streams/deno.json | 1 + streams/unstable_to_bytes.ts | 30 ++++++++++++++++++++++++++++++ streams/unstable_to_bytes_test.ts | 17 +++++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 streams/unstable_to_bytes.ts create mode 100644 streams/unstable_to_bytes_test.ts diff --git a/_tools/check_docs.ts b/_tools/check_docs.ts index b0649e5aa..d8f7cbb5a 100644 --- a/_tools/check_docs.ts +++ b/_tools/check_docs.ts @@ -89,6 +89,7 @@ const ENTRY_POINTS = [ "../streams/mod.ts", "../streams/unstable_fixed_chunk_stream.ts", "../streams/unstable_to_lines.ts", + "../streams/unstable_to_bytes.ts", "../tar/mod.ts", "../text/mod.ts", "../text/unstable_slugify.ts", diff --git a/streams/deno.json b/streams/deno.json index 7d76fc8ce..1ea18df26 100644 --- a/streams/deno.json +++ b/streams/deno.json @@ -16,6 +16,7 @@ "./text-line-stream": "./text_line_stream.ts", "./to-array-buffer": "./to_array_buffer.ts", "./to-blob": "./to_blob.ts", + "./unstable-to-bytes": "./unstable_to_bytes.ts", "./to-json": "./to_json.ts", "./unstable-to-lines": "./unstable_to_lines.ts", "./to-text": "./to_text.ts", diff --git a/streams/unstable_to_bytes.ts b/streams/unstable_to_bytes.ts new file mode 100644 index 000000000..71ec5cae2 --- /dev/null +++ b/streams/unstable_to_bytes.ts @@ -0,0 +1,30 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +// This module is browser compatible. + +/** + * Converts a {@linkcode ReadableStream} of {@linkcode Uint8Array}s to a + * {@linkcode Uint8Array}. Works the same as {@linkcode Response.bytes}. + * + * @experimental **UNSTABLE**: New API, yet to be vetted. + * + * @param stream A `ReadableStream` of `Uint8Array`s to convert into a `Uint8Array`. + * @returns A `Promise` that resolves to the `Uint8Array`. + * + * @example Basic usage + * ```ts + * import { toBytes } from "@std/streams/unstable-to-bytes"; + * import { assertEquals } from "@std/assert"; + * + * const stream = ReadableStream.from([ + * new Uint8Array([1, 2]), + * new Uint8Array([3, 4, 5]), + * ]); + * const bytes = await toBytes(stream); + * assertEquals(bytes, new Uint8Array([1, 2, 3, 4, 5])); + * ``` + */ +export function toBytes( + stream: ReadableStream, +): Promise { + return new Response(stream).bytes(); +} diff --git a/streams/unstable_to_bytes_test.ts b/streams/unstable_to_bytes_test.ts new file mode 100644 index 000000000..fd89e2089 --- /dev/null +++ b/streams/unstable_to_bytes_test.ts @@ -0,0 +1,17 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +import { assertEquals } from "@std/assert"; +import { toBytes } from "./unstable_to_bytes.ts"; + +Deno.test("toBytes()", async () => { + const stream = ReadableStream.from([ + new Uint8Array([1, 2, 3, 4, 5]), + new Uint8Array([6, 7]), + new Uint8Array([8, 9]), + ]); + + const bytes = await toBytes(stream); + assertEquals( + await bytes.buffer, + new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9]).buffer, + ); +});