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/unstable): toBytes() #6011

Merged
merged 1 commit into from
Sep 18, 2024
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
1 change: 1 addition & 0 deletions _tools/check_docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions streams/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 30 additions & 0 deletions streams/unstable_to_bytes.ts
Original file line number Diff line number Diff line change
@@ -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<Uint8Array>,
): Promise<Uint8Array> {
return new Response(stream).bytes();
}
17 changes: 17 additions & 0 deletions streams/unstable_to_bytes_test.ts
Original file line number Diff line number Diff line change
@@ -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,
);
});