-
Notifications
You must be signed in to change notification settings - Fork 646
/
Copy pathto_readable_stream_test.ts
90 lines (82 loc) · 2.81 KB
/
to_readable_stream_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2018-2025 the Deno authors. MIT license.
import { assertEquals } from "@std/assert";
import { toReadableStream } from "./to_readable_stream.ts";
import { Buffer } from "./buffer.ts";
import { concat } from "@std/bytes/concat";
import { copy } from "@std/bytes/copy";
import type { Closer, Reader } from "./types.ts";
class MockReaderCloser implements Reader, Closer {
chunks: Uint8Array[] = [];
closeCall = 0;
read(p: Uint8Array): Promise<number | null> {
if (this.closeCall) {
throw new Error("Already closed");
}
if (p.length === 0) {
return Promise.resolve(0);
}
const chunk = this.chunks.shift();
if (chunk) {
const copied = copy(chunk, p);
if (copied < chunk.length) {
this.chunks.unshift(chunk.subarray(copied));
}
return Promise.resolve(copied);
}
return Promise.resolve(null);
}
close() {
this.closeCall++;
}
}
Deno.test("toReadableStream()", async function () {
const encoder = new TextEncoder();
const reader = new Buffer(encoder.encode("hello deno land"));
const stream = toReadableStream(reader);
const actual = await Array.fromAsync(stream);
const decoder = new TextDecoder();
assertEquals(decoder.decode(concat(actual)), "hello deno land");
});
Deno.test("toReadableStream() handles close", async function () {
const encoder = new TextEncoder();
const reader = new MockReaderCloser();
reader.chunks = [
encoder.encode("hello "),
encoder.encode("deno "),
encoder.encode("land"),
];
const stream = toReadableStream(reader);
const actual = await Array.fromAsync(stream);
const decoder = new TextDecoder();
assertEquals(decoder.decode(concat(actual)), "hello deno land");
assertEquals(reader.closeCall, 1);
});
Deno.test("toReadableStream() doesn't call close with `autoClose` is false", async function () {
const encoder = new TextEncoder();
const reader = new MockReaderCloser();
reader.chunks = [
encoder.encode("hello "),
encoder.encode("deno "),
encoder.encode("land"),
];
const stream = toReadableStream(reader, { autoClose: false });
const actual = await Array.fromAsync(stream);
const decoder = new TextDecoder();
assertEquals(decoder.decode(concat(actual)), "hello deno land");
assertEquals(reader.closeCall, 0);
});
Deno.test("toReadableStream() handles `chunkSize` option", async function () {
const encoder = new TextEncoder();
const reader = new MockReaderCloser();
reader.chunks = [
encoder.encode("hello "),
encoder.encode("deno "),
encoder.encode("land"),
];
const stream = toReadableStream(reader, { chunkSize: 2 });
const actual = await Array.fromAsync(stream);
const decoder = new TextDecoder();
assertEquals(actual.length, 8);
assertEquals(decoder.decode(concat(actual)), "hello deno land");
assertEquals(reader.closeCall, 1);
});