Skip to content

Commit 7ee02ca

Browse files
feat(streams): stabilize toBytes (#7043)
1 parent 260f5b8 commit 7ee02ca

12 files changed

Lines changed: 45 additions & 28 deletions

encoding/unstable_base32_stream.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export class Base32EncoderStream<T extends "string" | "bytes">
134134
* Base32DecoderStream,
135135
* Base32EncoderStream,
136136
* } from "@std/encoding/unstable-base32-stream";
137-
* import { toBytes } from "@std/streams/unstable-to-bytes";
137+
* import { toBytes } from "@std/streams/to-bytes";
138138
*
139139
* const readable = (await Deno.open("./deno.lock"))
140140
* .readable

encoding/unstable_base32_stream_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { assertEquals } from "@std/assert";
44
import { toText } from "@std/streams";
5-
import { toBytes } from "@std/streams/unstable-to-bytes";
5+
import { toBytes } from "@std/streams/to-bytes";
66
import { FixedChunkStream } from "@std/streams/unstable-fixed-chunk-stream";
77
import { encodeBase32 } from "./unstable_base32.ts";
88
import {

encoding/unstable_base64_stream.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export class Base64EncoderStream<T extends "string" | "bytes">
138138
* Base64DecoderStream,
139139
* Base64EncoderStream,
140140
* } from "@std/encoding/unstable-base64-stream";
141-
* import { toBytes } from "@std/streams/unstable-to-bytes";
141+
* import { toBytes } from "@std/streams/to-bytes";
142142
*
143143
* const readable = (await Deno.open("./deno.lock"))
144144
* .readable

encoding/unstable_base64_stream_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { assertEquals } from "@std/assert";
44
import { toText } from "@std/streams";
5-
import { toBytes } from "@std/streams/unstable-to-bytes";
5+
import { toBytes } from "@std/streams/to-bytes";
66
import { FixedChunkStream } from "@std/streams/unstable-fixed-chunk-stream";
77
import { encodeBase64 } from "./unstable_base64.ts";
88
import {

encoding/unstable_hex_stream.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class HexEncoderStream<T extends "string" | "bytes">
104104
* HexDecoderStream,
105105
* HexEncoderStream,
106106
* } from "@std/encoding/unstable-hex-stream";
107-
* import { toBytes } from "@std/streams/unstable-to-bytes";
107+
* import { toBytes } from "@std/streams/to-bytes";
108108
*
109109
* const readable = (await Deno.open("./deno.lock"))
110110
* .readable

encoding/unstable_hex_stream_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { assertEquals } from "@std/assert";
44
import { toText } from "@std/streams";
5-
import { toBytes } from "@std/streams/unstable-to-bytes";
5+
import { toBytes } from "@std/streams/to-bytes";
66
import { FixedChunkStream } from "@std/streams/unstable-fixed-chunk-stream";
77
import { encodeHex } from "./unstable_hex.ts";
88
import { HexDecoderStream, HexEncoderStream } from "./unstable_hex_stream.ts";

streams/deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"./to-array-buffer": "./to_array_buffer.ts",
2020
"./to-blob": "./to_blob.ts",
2121
"./unstable-to-byte-stream": "./unstable_to_byte_stream.ts",
22-
"./unstable-to-bytes": "./unstable_to_bytes.ts",
22+
"./to-bytes": "./to_bytes.ts",
2323
"./to-json": "./to_json.ts",
2424
"./unstable-to-lines": "./unstable_to_lines.ts",
2525
"./to-text": "./to_text.ts",

streams/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export * from "./text_delimiter_stream.ts";
3030
export * from "./text_line_stream.ts";
3131
export * from "./to_array_buffer.ts";
3232
export * from "./to_blob.ts";
33+
export * from "./to_bytes.ts";
3334
export * from "./to_json.ts";
3435
export * from "./to_text.ts";
3536
export * from "./to_transform_stream.ts";
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
* Converts a {@linkcode ReadableStream} of {@linkcode Uint8Array}s to a
66
* {@linkcode Uint8Array}. Works the same as {@linkcode Response.bytes}.
77
*
8-
* @experimental **UNSTABLE**: New API, yet to be vetted.
9-
*
108
* @param stream A `ReadableStream` of `Uint8Array`s to convert into a `Uint8Array`.
119
* @returns A `Promise` that resolves to the `Uint8Array`.
1210
*
1311
* @example Basic usage
1412
* ```ts
15-
* import { toBytes } from "@std/streams/unstable-to-bytes";
13+
* import { toBytes } from "@std/streams/to-bytes";
1614
* import { assertEquals } from "@std/assert";
1715
*
1816
* const stream = ReadableStream.from([

streams/to_bytes_test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2018-2026 the Deno authors. MIT license.
2+
3+
import { assertEquals, assertRejects } from "@std/assert";
4+
import { toBytes } from "./to_bytes.ts";
5+
6+
Deno.test("toBytes() concatenates multiple chunks", async () => {
7+
const stream = ReadableStream.from([
8+
new Uint8Array([1, 2, 3, 4, 5]),
9+
new Uint8Array([6, 7]),
10+
new Uint8Array([8, 9]),
11+
]);
12+
13+
const bytes = await toBytes(stream);
14+
assertEquals(bytes, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9]));
15+
});
16+
17+
Deno.test("toBytes() returns empty Uint8Array for empty stream", async () => {
18+
const stream = ReadableStream.from([]);
19+
const bytes = await toBytes(stream);
20+
assertEquals(bytes, new Uint8Array());
21+
});
22+
23+
Deno.test("toBytes() rejects when stream errors", async () => {
24+
const stream = new ReadableStream({
25+
start(controller) {
26+
controller.error(new Error("boom"));
27+
},
28+
});
29+
30+
await assertRejects(
31+
() => toBytes(stream),
32+
Error,
33+
"boom",
34+
);
35+
});

0 commit comments

Comments
 (0)