Repository navigation
Expand file tree
/
Copy pathreport_diff_json_test.go
More file actions
64 lines (60 loc) · 2.66 KB
/
Copy pathreport_diff_json_test.go
File metadata and controls
64 lines (60 loc) · 2.66 KB
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
package main
import (
"os/exec"
"strings"
"testing"
)
func TestDiffJSONRuntimeStreamsAndValidates(t *testing.T) {
node, err := exec.LookPath("node")
if err != nil {
t.Skip("node is unavailable")
}
if !strings.Contains(diffReportTemplate, "data = await parseDiffJSONStream(stream)") || strings.Contains(diffReportTemplate, "new Response(stream).text()") {
t.Fatal("diff report boot must parse the decompressed stream incrementally")
}
harness := `
const assert = require('node:assert/strict');
function streamOf(chunks) {
let i = 0;
return new ReadableStream({ pull(controller) { if (i < chunks.length) controller.enqueue(chunks[i++]); else controller.close(); } });
}
(async () => {
const source = '{"s":"hi \\"quoted\\" \\\\ \\u263A 🐈","arr":[-1.2e+3,true,false,null,{"__proto__":{"polluted":1},"x":1}],"dup":1,"dup":2,"lone":"\\uD800"}';
const bytes = new TextEncoder().encode(source);
const oneByte = Array.from(bytes, b => Uint8Array.of(b));
const actual = await parseDiffJSONStream(streamOf(oneByte));
const expected = JSON.parse(source);
assert.deepEqual(actual, expected);
assert.equal(Object.hasOwn(actual.arr[4], '__proto__'), true);
assert.equal({}.polluted, undefined);
assert.equal(actual.dup, 2);
for (const bad of ['{"a":', '1 2', '01', '[1,]', '"\\x"', 'truex', '{"a":1']) {
await assert.rejects(parseDiffJSONStream(streamOf([new TextEncoder().encode(bad)])));
}
// Produce a multi-megabyte report in bounded chunks; reject accidental
// whole-report JSON.parse while still permitting token parsing.
const originalParse = JSON.parse;
JSON.parse = text => { if (text.length > 65536) throw new Error('whole-report JSON.parse'); return originalParse(text); };
const count = 150000;
let emitted = 0;
let opened = false;
const large = new ReadableStream({
pull(controller) {
if (!opened) { opened = true; controller.enqueue(new TextEncoder().encode('{"nodes":[')); return; }
const end = Math.min(emitted + 512, count);
const part = [];
for (; emitted < end; emitted++) part.push((emitted ? ',' : '') + '{"name":"node","size":123}');
controller.enqueue(new TextEncoder().encode(part.join('')));
if (emitted === count) { controller.enqueue(new TextEncoder().encode(']}')); controller.close(); }
}
});
const big = await parseDiffJSONStream(large);
assert.equal(big.nodes.length, count);
assert.equal(big.nodes[count - 1].size, 123);
})().catch(error => { console.error(error); process.exitCode = 1; });
`
command := exec.Command(node, "-e", diffJSONRuntime+"\n"+harness)
if output, err := command.CombinedOutput(); err != nil {
t.Fatalf("incremental JSON runtime failed: %v\n%s", err, output)
}
}