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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
import { file, gc } from "bun";
import { expect, it } from "bun:test";
import { writeFileSync } from "node:fs";
it("exists globally", () => {
expect(typeof ReadableStream).toBe("function");
expect(typeof ReadableStreamBYOBReader).toBe("function");
expect(typeof ReadableStreamBYOBRequest).toBe("function");
expect(typeof ReadableStreamDefaultController).toBe("function");
expect(typeof ReadableStreamDefaultReader).toBe("function");
expect(typeof TransformStream).toBe("function");
expect(typeof TransformStreamDefaultController).toBe("function");
expect(typeof WritableStream).toBe("function");
expect(typeof WritableStreamDefaultController).toBe("function");
expect(typeof WritableStreamDefaultWriter).toBe("function");
expect(typeof ByteLengthQueuingStrategy).toBe("function");
expect(typeof CountQueuingStrategy).toBe("function");
});
it("ReadableStream", async () => {
var stream = new ReadableStream({
start(controller) {
controller.enqueue(Buffer.from("abdefgh"));
},
pull(controller) {},
cancel() {},
type: "bytes",
});
const chunks = [];
const chunk = await stream.getReader().read();
chunks.push(chunk.value);
expect(chunks[0].join("")).toBe(Buffer.from("abdefgh").join(""));
});
it("ReadableStream for Blob", async () => {
var blob = new Blob(["abdefgh", "ijklmnop"]);
expect(await blob.text()).toBe("abdefghijklmnop");
var stream = blob.stream();
const chunks = [];
var reader = stream.getReader();
while (true) {
const chunk = await reader.read();
if (chunk.done) break;
chunks.push(new TextDecoder().decode(chunk.value));
}
expect(chunks.join("")).toBe(
new TextDecoder().decode(Buffer.from("abdefghijklmnop"))
);
});
it("ReadableStream for File", async () => {
var blob = file(import.meta.dir + "/fetch.js.txt");
var stream = blob.stream(24);
const chunks = [];
var reader = stream.getReader();
stream = undefined;
while (true) {
const chunk = await reader.read();
gc(true);
if (chunk.done) break;
chunks.push(chunk.value);
expect(chunk.value.byteLength <= 24).toBe(true);
gc(true);
}
reader = undefined;
const output = new Uint8Array(await blob.arrayBuffer()).join("");
const input = chunks.map((a) => a.join("")).join("");
expect(output).toBe(input);
gc(true);
});
it("ReadableStream for File errors", async () => {
try {
var blob = file(import.meta.dir + "/fetch.js.txt.notfound");
blob.stream().getReader();
throw new Error("should not reach here");
} catch (e) {
expect(e.code).toBe("ENOENT");
expect(e.syscall).toBe("open");
}
});
it("ReadableStream for empty blob closes immediately", async () => {
var blob = new Blob([]);
var stream = blob.stream();
const chunks = [];
var reader = stream.getReader();
while (true) {
const chunk = await reader.read();
if (chunk.done) break;
chunks.push(chunk.value);
}
expect(chunks.length).toBe(0);
});
it("ReadableStream for empty file closes immediately", async () => {
writeFileSync("/tmp/bun-empty-file-123456", "");
var blob = file("/tmp/bun-empty-file-123456");
var stream = blob.stream();
const chunks = [];
var reader = stream.getReader();
while (true) {
const chunk = await reader.read();
if (chunk.done) break;
chunks.push(chunk.value);
}
expect(chunks.length).toBe(0);
});
|