-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
307 lines (292 loc) · 8.59 KB
/
index.js
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* eslint-env node */
/* global window */
const fs = require("fs");
const {URL} = require("url");
const isCI = require("is-ci");
const createServer = require("css-to-js-sourcemap-fixture-app");
const puppeteer = require("puppeteer");
const getPort = require("get-port");
const test = require("tape");
const {getConsumer} = require("./mapper.js");
const fixtures = {
clientSource: fs.readFileSync(
require.resolve("css-to-js-sourcemap-fixture-app/client.js"),
"utf-8",
),
clientNoMapRaw: fs.readFileSync(
require.resolve("css-to-js-sourcemap-fixture-app/public/_static/no-map.js"),
"utf-8",
),
clientExternalMapRaw: fs.readFileSync(
require.resolve(
"css-to-js-sourcemap-fixture-app/public/_static/external-map.js",
),
"utf-8",
),
};
testSingleMap("/external-map");
testSingleMap("/inline-map");
test(`single mapped class works on /no-map`, async t => {
const {page, browser, server} = await setup(
"/no-map",
async msg => {
if (msg.css) {
const lines = msg.css.split("\n");
t.equal(lines[0], ".__debug-1 {}", "has expected class on line 1");
const consumer = await getConsumer(msg.css);
const pos = consumer.originalPositionFor({line: 1, column: 0});
const lineNumber =
fixtures.clientNoMapRaw
.split("\n")
.indexOf(`const err1 = new Error("Line 5");`) + 1;
t.equal(pos.line, lineNumber, "mapped line number matches expected");
t.equal(pos.column, 0, "mapped column matches expected");
const {hostname, pathname, protocol} = new URL(pos.source);
t.equal(hostname, "localhost");
t.equal(pathname, "/_static/no-map.js");
t.equal(protocol, "http:");
const content = consumer.sourceContentFor(pos.source);
t.equal(
content,
fixtures.clientNoMapRaw,
"mapped source content matches expected",
);
await browser.close();
server.close();
t.end();
}
},
() => {
t.fail("recieved error");
},
);
page.evaluate(() => {
window.worker.postMessage({
id: "init_wasm",
url: "/mappings.wasm",
});
window.worker.postMessage({
id: "add_mapped_class",
stackInfo: window.error1,
className: "__debug-1",
stackIndex: 0,
});
window.worker.postMessage({
id: "set_render_interval",
interval: 60,
});
});
});
test(`replaying requests after invalidation`, async t => {
const {page, browser, server} = await setup(
"/external-map",
async msg => {
if (msg.css) {
const lines = msg.css.split("\n");
t.equal(lines[0], ".__debug-1 {}", "has expected class on line 1");
const consumer = await getConsumer(msg.css);
const pos = consumer.originalPositionFor({line: 1, column: 0});
t.equal(pos.line, 7, "mapped line number matches expected");
t.equal(pos.column, 0, "mapped column matches expected");
t.equal(
pos.source,
"webpack:///client.js?n=1",
"mapped source matches expected",
);
const content = consumer.sourceContentFor("webpack:///client.js?n=1");
t.equal(
content,
fixtures.clientSource,
"mapped source content matches expected",
);
await browser.close();
server.close();
t.end();
}
},
() => {
t.fail("recieved error");
},
);
await page.setRequestInterception(true);
let invalidated = false;
page.on("request", req => {
req.continue();
if (req._url.endsWith(".js.map")) {
// invalidate after first request
if (!invalidated) {
page.evaluate(() => {
window.worker.postMessage({
id: "invalidate",
});
});
invalidated = true;
} else {
t.pass("sourcemap fetched twice");
// end test
page.evaluate(() => {
window.worker.postMessage({
id: "set_render_interval",
interval: 60,
});
});
}
}
});
await page.evaluate(() => {
window.worker.postMessage({
id: "init_wasm",
url: "/mappings.wasm",
});
window.worker.postMessage({
id: "add_mapped_class",
stackInfo: window.error1,
className: "__debug-1",
stackIndex: 0,
});
});
});
test(`fallback if sourcemap request is 404`, async t => {
const {page, browser, server} = await setup(
"/external-map",
async msg => {
if (msg.css) {
const lines = msg.css.split("\n");
t.equal(lines[0], ".__debug-1 {}", "has expected class on line 1");
const consumer = await getConsumer(msg.css);
const pos = consumer.originalPositionFor({line: 1, column: 0});
const lineNumber =
fixtures.clientExternalMapRaw
.split("\n")
.indexOf(`const err1 = new Error("Line 5");`) + 1;
t.equal(pos.line, lineNumber, "mapped line number matches expected");
t.equal(pos.column, 0, "mapped column matches expected");
const {hostname, pathname, protocol} = new URL(pos.source);
t.equal(hostname, "localhost");
t.equal(pathname, "/_static/external-map.js");
t.equal(protocol, "http:");
const content = consumer.sourceContentFor(pos.source);
t.equal(
content,
fixtures.clientExternalMapRaw,
"mapped source content matches expected",
);
await browser.close();
server.close();
t.end();
}
},
() => {
t.fail("recieved error");
},
);
await page.setRequestInterception(true);
page.on("request", req => {
if (req._url.endsWith(".js.map")) {
req.respond({
status: 404,
});
} else {
req.continue();
}
});
await page.evaluate(() => {
window.worker.postMessage({
id: "init_wasm",
url: "/mappings.wasm",
});
window.worker.postMessage({
id: "add_mapped_class",
stackInfo: window.error1,
className: "__debug-1",
stackIndex: 0,
});
window.worker.postMessage({
id: "set_render_interval",
interval: 60,
});
});
});
function testSingleMap(route) {
test(`single mapped class works on ${route}`, async t => {
const {page, browser, server} = await setup(
route,
async msg => {
if (msg.css) {
const lines = msg.css.split("\n");
t.equal(lines[0], ".__debug-1 {}", "has expected class on line 1");
const consumer = await getConsumer(msg.css);
const pos = consumer.originalPositionFor({line: 1, column: 0});
t.equal(pos.line, 7, "mapped line number matches expected");
t.equal(pos.column, 0, "mapped column matches expected");
t.equal(
pos.source,
"webpack:///client.js?n=0",
"mapped source matches expected",
);
const content = consumer.sourceContentFor("webpack:///client.js?n=0");
t.equal(
content,
fixtures.clientSource,
"mapped source content matches expected",
);
await browser.close();
server.close();
t.end();
}
},
() => {
t.fail("Recieved error");
},
);
page.evaluate(() => {
window.worker.postMessage({
id: "init_wasm",
url: "/mappings.wasm",
});
window.worker.postMessage({
id: "add_mapped_class",
stackInfo: window.error1,
className: "__debug-1",
stackIndex: 0,
});
window.worker.postMessage({
id: "set_render_interval",
interval: 60,
});
});
});
}
function startServer() {
const server = createServer();
return new Promise(resolve => {
getPort(49348).then(port => {
server.listen(port, () => resolve({port, server}));
});
});
}
async function setup(route, msgHandler, errHandler) {
const {port, server} = await startServer();
const browser = await puppeteer.launch(
isCI ? {args: ["--no-sandbox", "--disable-dev-shm-usage"]} : {},
);
const page = await browser.newPage();
let url = `http://localhost:${port}${route}`;
await page.goto(url);
page.on("console", msg => {
Promise.all(msg.args().map(a => a.jsonValue())).then(async args => {
if (args[0] === "__on_message__") {
msgHandler(args[1]);
} else if (args[0] === "Debug worker error") {
errHandler(args[1]);
}
});
});
await page.evaluate(() => {
window.worker.onmessage = msg => {
/* eslint-disable-next-line no-console */
console.log("__on_message__", msg.data);
};
});
return {page, browser, server};
}