-
-
Notifications
You must be signed in to change notification settings - Fork 503
Expand file tree
/
Copy pathviewer.js
More file actions
130 lines (115 loc) · 3.6 KB
/
viewer.js
File metadata and controls
130 lines (115 loc) · 3.6 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
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
const crypto = require("node:crypto");
const net = require("node:net");
const Logger = require("../src/Logger");
const { getEntrypoints, startServer } = require("../src/viewer");
describe("WebSocket server", () => {
it("should not crash when an error is emitted on the websocket", (done) => {
const bundleStats = {
assets: [{ name: "bundle.js", chunks: [0] }],
};
const options = {
openBrowser: false,
logger: new Logger("silent"),
port: 0,
analyzerUrl: () => "",
};
startServer(bundleStats, options)
.then(({ http: server }) => {
// The GUID constant defined in WebSocket protocol
// https://tools.ietf.org/html/rfc6455#section-1.3
const GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
// The client-generated "Sec-WebSocket-Key" header field value.
const key = crypto.randomBytes(16).toString("base64");
// The server-generated "Sec-WebSocket-Accept" header field value.
const accept = crypto
.createHash("sha1")
.update(key + GUID)
.digest("base64");
const socket = net.createConnection(server.address().port, () => {
socket.write(
[
"GET / HTTP/1.1",
"Host: localhost",
"Upgrade: websocket",
"Connection: Upgrade",
`Sec-WebSocket-Key: ${key}`,
"Sec-WebSocket-Version: 13",
"",
"",
].join("\r\n"),
);
});
socket.on("close", () => {
server.close(done);
});
let count = 0;
socket.on("data", (chunk) => {
++count;
const expected = Buffer.from(
[
"HTTP/1.1 101 Switching Protocols",
"Upgrade: websocket",
"Connection: Upgrade",
`Sec-WebSocket-Accept: ${accept}`,
"",
"",
].join("\r\n"),
);
// Because data may be received in multiple chunks, only check the first one
if (count === 1) {
expect(chunk.equals(expected)).toBe(true);
}
// Send a WebSocket frame with a reserved opcode (5) to trigger an error
// to be emitted on the server.
socket.write(Buffer.from([0x85, 0x00]));
});
})
.catch(done);
});
});
describe("getEntrypoints", () => {
it("should get all entrypoints", () => {
const bundleStats = {
entrypoints: {
A: {
name: "A",
assets: [
{
name: "chunkA.js",
},
],
},
B: {
name: "B",
assets: [
{
name: "chunkA.js",
},
{
name: "chunkB.js",
},
],
},
},
};
expect(JSON.stringify(getEntrypoints(bundleStats))).toBe(
JSON.stringify(["A", "B"]),
);
});
it("should handle when bundlestats is null or undefined", () => {
expect(JSON.stringify(getEntrypoints(null))).toBe(JSON.stringify([]));
expect(JSON.stringify(getEntrypoints(undefined))).toBe(JSON.stringify([]));
});
it("should handle when bundlestats is empty", () => {
const bundleStatsWithoutEntryPoints = {};
expect(JSON.stringify(getEntrypoints(bundleStatsWithoutEntryPoints))).toBe(
JSON.stringify([]),
);
});
it("should handle when entrypoints is empty", () => {
const bundleStatsEmptyEntryPoint = { entrypoints: {} };
expect(JSON.stringify(getEntrypoints(bundleStatsEmptyEntryPoint))).toBe(
JSON.stringify([]),
);
});
});