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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
import { join } from "path";
import { openSync } from "fs";
describe("structured clone", () => {
let primitives_tests = [
{ description: "primitive undefined", value: undefined },
{ description: "primitive null", value: null },
{ description: "primitive true", value: true },
{ description: "primitive false", value: false },
{ description: "primitive string, empty string", value: "" },
{ description: "primitive string, lone high surrogate", value: "\uD800" },
{ description: "primitive string, lone low surrogate", value: "\uDC00" },
{ description: "primitive string, NUL", value: "\u0000" },
{ description: "primitive string, astral character", value: "\uDBFF\uDFFD" },
{ description: "primitive number, 0.2", value: 0.2 },
{ description: "primitive number, 0", value: 0 },
{ description: "primitive number, -0", value: -0 },
{ description: "primitive number, NaN", value: NaN },
{ description: "primitive number, Infinity", value: Infinity },
{ description: "primitive number, -Infinity", value: -Infinity },
{ description: "primitive number, 9007199254740992", value: 9007199254740992 },
{ description: "primitive number, -9007199254740992", value: -9007199254740992 },
{ description: "primitive number, 9007199254740994", value: 9007199254740994 },
{ description: "primitive number, -9007199254740994", value: -9007199254740994 },
{ description: "primitive BigInt, 0n", value: 0n },
{ description: "primitive BigInt, -0n", value: -0n },
{ description: "primitive BigInt, -9007199254740994000n", value: -9007199254740994000n },
{
description: "primitive BigInt, -9007199254740994000900719925474099400090071992547409940009007199254740994000n",
value: -9007199254740994000900719925474099400090071992547409940009007199254740994000n,
},
];
for (let { description, value } of primitives_tests) {
test(description, () => {
var cloned = structuredClone(value);
expect(cloned).toBe(value);
});
}
test("Array with primitives", () => {
var input = [
undefined,
null,
true,
false,
"",
"\uD800",
"\uDC00",
"\u0000",
"\uDBFF\uDFFD",
0.2,
0,
-0,
NaN,
Infinity,
-Infinity,
9007199254740992,
-9007199254740992,
9007199254740994,
-9007199254740994,
-12n,
-0n,
0n,
];
var cloned = structuredClone(input);
expect(cloned).toBeInstanceOf(Array);
expect(cloned).not.toBe(input);
expect(cloned.length).toEqual(input.length);
for (const x in input) {
expect(cloned[x]).toBe(input[x]);
}
});
test("Object with primitives", () => {
var input: any = {
undefined: undefined,
null: null,
true: true,
false: false,
empty: "",
"high surrogate": "\uD800",
"low surrogate": "\uDC00",
nul: "\u0000",
astral: "\uDBFF\uDFFD",
"0.2": 0.2,
"0": 0,
"-0": -0,
NaN: NaN,
Infinity: Infinity,
"-Infinity": -Infinity,
"9007199254740992": 9007199254740992,
"-9007199254740992": -9007199254740992,
"9007199254740994": 9007199254740994,
"-9007199254740994": -9007199254740994,
"-12n": -12n,
"-0n": -0n,
"0n": 0n,
};
var cloned = structuredClone(input);
expect(cloned).toBeInstanceOf(Object);
expect(cloned).not.toBeInstanceOf(Array);
expect(cloned).not.toBe(input);
for (const x in input) {
expect(cloned[x]).toBe(input[x]);
}
});
test("map", () => {
var input = new Map();
input.set("a", 1);
input.set("b", 2);
input.set("c", 3);
var cloned = structuredClone(input);
expect(cloned).toBeInstanceOf(Map);
expect(cloned).not.toBe(input);
expect(cloned.size).toEqual(input.size);
for (const [key, value] of input) {
expect(cloned.get(key)).toBe(value);
}
});
test("set", () => {
var input = new Set();
input.add("a");
input.add("b");
input.add("c");
var cloned = structuredClone(input);
expect(cloned).toBeInstanceOf(Set);
expect(cloned).not.toBe(input);
expect(cloned.size).toEqual(input.size);
for (const value of input) {
expect(cloned.has(value)).toBe(true);
}
});
describe("bun blobs work", () => {
test("simple", async () => {
const blob = new Blob(["hello"], { type: "application/octet-stream" });
const cloned = structuredClone(blob);
await compareBlobs(blob, cloned);
});
test("empty", async () => {
const emptyBlob = new Blob([], { type: "" });
const clonedEmpty = structuredClone(emptyBlob);
await compareBlobs(emptyBlob, clonedEmpty);
});
test("empty with type", async () => {
const emptyBlob = new Blob([], { type: "application/octet-stream" });
const clonedEmpty = structuredClone(emptyBlob);
await compareBlobs(emptyBlob, clonedEmpty);
});
test("unknown type", async () => {
const blob = new Blob(["hello type"], { type: "this is type" });
const cloned = structuredClone(blob);
await compareBlobs(blob, cloned);
});
test("file from path", async () => {
const blob = Bun.file(join(import.meta.dir, "example.txt"));
const cloned = structuredClone(blob);
expect(cloned.lastModified).toBe(blob.lastModified);
expect(cloned.name).toBe(blob.name);
});
test("file from fd", async () => {
const fd = openSync(join(import.meta.dir, "example.txt"), "r");
const blob = Bun.file(fd);
const cloned = structuredClone(blob);
expect(cloned.lastModified).toBe(blob.lastModified);
expect(cloned.name).toBe(blob.name);
});
test("unpaired high surrogate (invalid utf-8)", async () => {
const blob = createBlob(encode_cesu8([0xd800]));
const cloned = structuredClone(blob);
await compareBlobs(blob, cloned);
});
test("unpaired low surrogate (invalid utf-8)", async () => {
const blob = createBlob(encode_cesu8([0xdc00]));
const cloned = structuredClone(blob);
await compareBlobs(blob, cloned);
});
test("paired surrogates (invalid utf-8)", async () => {
const blob = createBlob(encode_cesu8([0xd800, 0xdc00]));
const cloned = structuredClone(blob);
await compareBlobs(blob, cloned);
});
});
describe("transferables", () => {
test("ArrayBuffer", () => {
const buffer = Uint8Array.from([1]).buffer;
const cloned = structuredClone(buffer, { transfer: [buffer] });
expect(buffer.byteLength).toBe(0);
expect(cloned.byteLength).toBe(1);
});
test("A detached ArrayBuffer cannot be transferred", () => {
const buffer = new ArrayBuffer(2);
const cloned = structuredClone(buffer, { transfer: [buffer] });
expect(() => {
structuredClone(buffer, { transfer: [buffer] });
}).toThrow(DOMException);
});
test("Transferring a non-transferable platform object fails", () => {
const blob = new Blob();
expect(() => {
structuredClone(blob, { transfer: [blob] });
}).toThrow(DOMException);
});
});
});
async function compareBlobs(original: Blob, cloned: Blob) {
expect(cloned).toBeInstanceOf(Blob);
expect(cloned).not.toBe(original);
expect(cloned.size).toBe(original.size);
expect(cloned.type).toBe(original.type);
const ab1 = await new Response(cloned).arrayBuffer();
const ab2 = await new Response(original).arrayBuffer();
expect(ab1.byteLength).toBe(ab2.byteLength);
const ta1 = new Uint8Array(ab1);
const ta2 = new Uint8Array(ab2);
for (let i = 0; i < ta1.length; i++) {
expect(ta1[i]).toBe(ta2[i]);
}
}
function encode_cesu8(codeunits: number[]): number[] {
// http://www.unicode.org/reports/tr26/ section 2.2
// only the 3-byte form is supported
const rv: number[] = [];
codeunits.forEach(function (codeunit) {
rv.push(b("11100000") + ((codeunit & b("1111000000000000")) >> 12));
rv.push(b("10000000") + ((codeunit & b("0000111111000000")) >> 6));
rv.push(b("10000000") + (codeunit & b("0000000000111111")));
});
return rv;
}
function b(s: string): number {
return parseInt(s, 2);
}
function createBlob(arr: number[]): Blob {
const buffer = new ArrayBuffer(arr.length);
const view = new DataView(buffer);
for (let i = 0; i < arr.length; i++) {
view.setUint8(i, arr[i]);
}
return new Blob([view]);
}
|