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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const ChromeUtils = require("ChromeUtils");
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
const {
getAdHocFrontOrPrimitiveGrip,
} = require("devtools/client/fronts/object");
const CHROME_PREFIX = "chrome://mochitests/content/browser/";
const STUBS_FOLDER = "devtools/client/webconsole/test/node/fixtures/stubs/";
const STUBS_UPDATE_ENV = "WEBCONSOLE_STUBS_UPDATE";
async function createResourceWatcherForTab(tab) {
const { TargetFactory } = require("devtools/client/framework/target");
const target = await TargetFactory.forTab(tab);
const resourceWatcher = await createResourceWatcherForTarget(target);
return resourceWatcher;
}
async function createResourceWatcherForTarget(target) {
// Avoid mocha to try to load these module and fail while doing it when running node tests
const {
ResourceWatcher,
} = require("devtools/shared/resources/resource-watcher");
const { TargetList } = require("devtools/shared/resources/target-list");
const targetList = new TargetList(target.client.mainRoot, target);
await targetList.startListening();
return new ResourceWatcher(targetList);
}
// eslint-disable-next-line complexity
function getCleanedPacket(key, packet) {
const { stubPackets } = require(CHROME_PREFIX + STUBS_FOLDER + "index");
// Strip escaped characters.
const safeKey = key
.replace(/\\n/g, "\n")
.replace(/\\r/g, "\r")
.replace(/\\\"/g, `\"`)
.replace(/\\\'/g, `\'`);
cleanTimeStamp(packet);
// Remove the targetFront property that has a cyclical reference and that we don't need
// in our node tests.
delete packet.targetFront;
if (!stubPackets.has(safeKey)) {
return packet;
}
// If the stub already exist, we want to ignore irrelevant properties (actor, timer, …)
// that might changed and "pollute" the diff resulting from this stub generation.
const existingPacket = stubPackets.get(safeKey);
const res = Object.assign({}, packet, {
from: existingPacket.from,
});
if (res.innerWindowID) {
res.innerWindowID = existingPacket.innerWindowID;
}
if (res.startedDateTime) {
res.startedDateTime = existingPacket.startedDateTime;
}
if (res.actor) {
res.actor = existingPacket.actor;
}
if (res.channelId) {
res.channelId = existingPacket.channelId;
}
if (res.resultID) {
res.resultID = existingPacket.resultID;
}
if (res.message) {
if (res.message.timer) {
// Clean timer properties on the message.
// Those properties are found on console.time, timeLog and timeEnd calls,
// and those time can vary, which is why we need to clean them.
if ("duration" in res.message.timer) {
res.message.timer.duration = existingPacket.message.timer.duration;
}
}
// Clean innerWindowId on the message prop.
if (existingPacket.message.innerWindowID) {
res.message.innerWindowID = existingPacket.message.innerWindowID;
}
if (Array.isArray(res.message.arguments)) {
res.message.arguments = res.message.arguments.map((argument, i) => {
if (!argument || typeof argument !== "object") {
return argument;
}
const newArgument = Object.assign({}, argument);
const existingArgument = existingPacket.message.arguments[i];
if (existingArgument && newArgument._grip) {
// Clean actor ids on each message.arguments item.
copyExistingActor(newArgument, existingArgument);
// `window`'s properties count can vary from OS to OS, so we
// clean the `ownPropertyLength` property from the grip.
if (newArgument._grip.class === "Window") {
newArgument._grip.ownPropertyLength =
existingArgument._grip.ownPropertyLength;
}
}
return newArgument;
});
}
if (res.message.actor && existingPacket?.message?.actor) {
res.message.actor = existingPacket.message.actor;
}
if (res.message.sourceId) {
res.message.sourceId = existingPacket.message.sourceId;
}
if (Array.isArray(res.message.stacktrace)) {
res.message.stacktrace = res.message.stacktrace.map((frame, i) => {
const existingFrame = existingPacket.message.stacktrace[i];
if (frame && existingFrame && frame.sourceId) {
frame.sourceId = existingFrame.sourceId;
}
return frame;
});
}
}
if (res?.exception?.actor && existingPacket.exception.actor) {
// Clean actor ids on evaluation exception
copyExistingActor(res.exception, existingPacket.exception);
}
if (res.result && res.result._grip && existingPacket.result) {
// Clean actor ids on evaluation result messages.
copyExistingActor(res.result, existingPacket.result);
}
if (
res?.result?._grip?.preview?.ownProperties?.["<value>"]?.value &&
existingPacket?.result?._grip?.preview?.ownProperties?.["<value>"]?.value
) {
// Clean actor ids on evaluation promise result messages.
copyExistingActor(
res.result._grip.preview.ownProperties["<value>"].value,
existingPacket.result._grip.preview.ownProperties["<value>"].value
);
}
if (
res?.result?._grip?.preview?.ownProperties?.["<reason>"]?.value &&
existingPacket?.result?._grip?.preview?.ownProperties?.["<reason>"]?.value
) {
// Clean actor ids on evaluation promise result messages.
copyExistingActor(
res.result._grip.preview.ownProperties["<reason>"].value,
existingPacket.result._grip.preview.ownProperties["<reason>"].value
);
}
if (res.exception && existingPacket.exception) {
// Clean actor ids on exception messages.
copyExistingActor(res.exception, existingPacket.exception);
if (
res.exception._grip &&
res.exception._grip.preview &&
existingPacket.exception._grip &&
existingPacket.exception._grip.preview
) {
if (
typeof res.exception._grip.preview.message === "object" &&
res.exception._grip.preview.message._grip.type === "longString" &&
typeof existingPacket.exception._grip.preview.message === "object" &&
existingPacket.exception._grip.preview.message._grip.type ===
"longString"
) {
copyExistingActor(
res.exception._grip.preview.message,
existingPacket.exception._grip.preview.message
);
}
}
if (
typeof res.exceptionMessage === "object" &&
res.exceptionMessage._grip &&
res.exceptionMessage._grip.type === "longString"
) {
copyExistingActor(res.exceptionMessage, existingPacket.exceptionMessage);
}
}
if (res.eventActor) {
// Clean actor ids and startedDateTime on network messages.
res.eventActor.actor = existingPacket.actor;
res.eventActor.startedDateTime = existingPacket.startedDateTime;
}
if (res.pageError) {
// Clean innerWindowID on pageError messages.
res.pageError.innerWindowID = existingPacket.pageError.innerWindowID;
if (
typeof res.pageError.errorMessage === "object" &&
res.pageError.errorMessage._grip &&
res.pageError.errorMessage._grip.type === "longString"
) {
copyExistingActor(
res.pageError.errorMessage,
existingPacket.pageError.errorMessage
);
}
if (
res.pageError.exception?._grip?.preview?.message?._grip &&
existingPacket.pageError.exception?._grip?.preview?.message?._grip
) {
copyExistingActor(
res.pageError.exception._grip.preview.message,
existingPacket.pageError.exception._grip.preview.message
);
}
if (res.pageError.exception && existingPacket.pageError.exception) {
copyExistingActor(
res.pageError.exception,
existingPacket.pageError.exception
);
}
if (res.pageError.sourceId) {
res.pageError.sourceId = existingPacket.pageError.sourceId;
}
if (
Array.isArray(res.pageError.stacktrace) &&
Array.isArray(existingPacket.pageError.stacktrace)
) {
res.pageError.stacktrace = res.pageError.stacktrace.map((frame, i) => {
const existingFrame = existingPacket.pageError.stacktrace[i];
if (frame && existingFrame && frame.sourceId) {
frame.sourceId = existingFrame.sourceId;
}
return frame;
});
}
}
if (Array.isArray(res.exceptionStack)) {
res.exceptionStack = res.exceptionStack.map((frame, i) => {
const existingFrame = existingPacket.exceptionStack[i];
if (frame && existingFrame && frame.sourceId) {
frame.sourceId = existingFrame.sourceId;
}
return frame;
});
}
if (res.frame && existingPacket.frame) {
res.frame.sourceId = existingPacket.frame.sourceId;
}
if (res.packet) {
const override = {};
const keys = ["totalTime", "from", "contentSize", "transferredSize"];
keys.forEach(x => {
if (res.packet[x] !== undefined) {
override[x] = existingPacket.packet[key];
}
});
res.packet = Object.assign({}, res.packet, override);
}
if (res.startedDateTime) {
res.startedDateTime = existingPacket.startedDateTime;
}
if (res.totalTime && existingPacket.totalTime) {
res.totalTime = existingPacket.totalTime;
}
if (res.securityState && existingPacket.securityState) {
res.securityState = existingPacket.securityState;
}
if (res.actor && existingPacket.actor) {
res.actor = existingPacket.actor;
}
if (res.waitingTime && existingPacket.waitingTime) {
res.waitingTime = existingPacket.waitingTime;
}
if (res.helperResult) {
copyExistingActor(
res.helperResult.object,
existingPacket.helperResult.object
);
}
return res;
}
function cleanTimeStamp(packet) {
// We want to have the same timestamp for every stub, so they won't be re-sorted when
// adding them to the store.
const uniqueTimeStamp = 1572867483805;
// lowercased timestamp
if (packet.timestamp) {
packet.timestamp = uniqueTimeStamp;
}
// camelcased timestamp
if (packet.timeStamp) {
packet.timeStamp = uniqueTimeStamp;
}
if (packet.startTime) {
packet.startTime = uniqueTimeStamp;
}
if (packet?.message?.timeStamp) {
packet.message.timeStamp = uniqueTimeStamp;
}
if (packet?.result?._grip?.preview?.timestamp) {
packet.result._grip.preview.timestamp = uniqueTimeStamp;
}
if (packet?.result?._grip?.promiseState?.creationTimestamp) {
packet.result._grip.promiseState.creationTimestamp = uniqueTimeStamp;
}
if (packet?.exception?._grip?.preview?.timestamp) {
packet.exception._grip.preview.timestamp = uniqueTimeStamp;
}
if (packet?.eventActor?.timeStamp) {
packet.eventActor.timeStamp = uniqueTimeStamp;
}
if (packet?.pageError?.timeStamp) {
packet.pageError.timeStamp = uniqueTimeStamp;
}
}
function copyExistingActor(a, b) {
if (!a || !b) {
return;
}
if (a.actorID && b.actorID) {
a.actorID = b.actorID;
}
if (a.actor && b.actor) {
a.actor = b.actor;
}
if (a._grip && b._grip && a._grip.actor && b._grip.actor) {
a._grip.actor = b._grip.actor;
}
}
/**
* Write stubs to a given file
*
* @param {Object} env
* @param {String} fileName: The file to write the stubs in.
* @param {Map} packets: A Map of the packets.
* @param {Boolean} isNetworkMessage: Is the packets are networkMessage packets
*/
async function writeStubsToFile(env, fileName, packets, isNetworkMessage) {
const mozRepo = env.get("MOZ_DEVELOPER_REPO_DIR");
const filePath = `${mozRepo}/${STUBS_FOLDER + fileName}`;
const serializedPackets = Array.from(packets.entries()).map(
([key, packet]) => {
const stringifiedPacket = getSerializedPacket(packet);
return `rawPackets.set(\`${key}\`, ${stringifiedPacket});`;
}
);
const fileContent = `/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
/* eslint-disable max-len */
"use strict";
/*
* THIS FILE IS AUTOGENERATED. DO NOT MODIFY BY HAND. RUN TESTS IN FIXTURES/ TO UPDATE.
*/
const {
parsePacketsWithFronts,
} = require("chrome://mochitests/content/browser/devtools/client/webconsole/test/browser/stub-generator-helpers");
const { prepareMessage } = require("devtools/client/webconsole/utils/messages");
const {
ConsoleMessage,
NetworkEventMessage,
} = require("devtools/client/webconsole/types");
const rawPackets = new Map();
${serializedPackets.join("\n\n")}
const stubPackets = parsePacketsWithFronts(rawPackets);
const stubPreparedMessages = new Map();
for (const [key, packet] of Array.from(stubPackets.entries())) {
const transformedPacket = prepareMessage(${"packet"}, {
getNextId: () => "1",
});
const message = ${
isNetworkMessage
? "NetworkEventMessage(transformedPacket);"
: "ConsoleMessage(transformedPacket);"
}
stubPreparedMessages.set(key, message);
}
module.exports = {
rawPackets,
stubPreparedMessages,
stubPackets,
};
`;
await OS.File.writeAtomic(filePath, fileContent);
}
function getStubFile(fileName) {
return require(CHROME_PREFIX + STUBS_FOLDER + fileName);
}
function sortObjectKeys(obj) {
const isArray = Array.isArray(obj);
const isObject = Object.prototype.toString.call(obj) === "[object Object]";
const isFront = obj?._grip;
if (isObject && !isFront) {
// Reorder keys for objects, but skip fronts to avoid infinite recursion.
const sortedKeys = Object.keys(obj).sort((k1, k2) => k1.localeCompare(k2));
const withSortedKeys = {};
sortedKeys.forEach(k => {
withSortedKeys[k] = k !== "stacktrace" ? sortObjectKeys(obj[k]) : obj[k];
});
return withSortedKeys;
} else if (isArray) {
return obj.map(item => sortObjectKeys(item));
}
return obj;
}
/**
* @param {Object} packet
* The packet to serialize.
* @param {Object}
* - {Boolean} sortKeys: pass true to sort all keys alphabetically in the
* packet before serialization. For instance stub comparison should not
* fail if the order of properties changed.
*/
function getSerializedPacket(packet, { sortKeys = false } = {}) {
if (sortKeys) {
packet = sortObjectKeys(packet);
}
return JSON.stringify(
packet,
function(_, value) {
// The message can have fronts that we need to serialize
if (value && value._grip) {
return { _grip: value._grip, actorID: value.actorID };
}
return value;
},
2
);
}
/**
*
* @param {Map} rawPackets
*/
function parsePacketsWithFronts(rawPackets) {
const packets = new Map();
for (const [key, packet] of rawPackets.entries()) {
const newPacket = parsePacketAndCreateFronts(packet);
packets.set(key, newPacket);
}
return packets;
}
function parsePacketAndCreateFronts(packet) {
if (!packet) {
return packet;
}
if (Array.isArray(packet)) {
packet.forEach(parsePacketAndCreateFronts);
}
if (typeof packet === "object") {
for (const [key, value] of Object.entries(packet)) {
if (value?._grip) {
// The message of an error grip might be a longString.
if (value._grip?.preview?.message?._grip) {
value._grip.preview.message = value._grip.preview.message._grip;
}
packet[key] = getAdHocFrontOrPrimitiveGrip(value._grip, {
conn: {
poolFor: () => {},
addActorPool: () => {},
getFrontByID: () => {},
},
manage: () => {},
});
} else {
packet[key] = parsePacketAndCreateFronts(value);
}
}
}
return packet;
}
module.exports = {
STUBS_UPDATE_ENV,
createResourceWatcherForTab,
createResourceWatcherForTarget,
getStubFile,
getCleanedPacket,
getSerializedPacket,
parsePacketsWithFronts,
parsePacketAndCreateFronts,
writeStubsToFile,
};
|