-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
107 lines (96 loc) · 3.27 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
//@ts-check
const cdp = require('chrome-remote-interface');
async function wait(n) {
return new Promise(resolve => setTimeout(resolve, n));
}
async function connectWithRetry(host, port, tries = 10, retryWait = 50, errors = [], target) {
if (typeof target === 'undefined') {
target = function (targets) {
const target = targets.find(target => {
if (target.webSocketDebuggerUrl) {
if (target.type === 'page') {
return target.url.indexOf('bootstrap/index.html') > 0
} else {
return true;
}
}
});
if (!target) {
throw new class extends Error {
constructor() {
super('no target');
this.code = 'ECONNREFUSED';
}
};
}
return target;
};
}
try {
return await cdp({
host,
port,
target,
local: true,
});
} catch (e) {
errors.push(e);
if (tries <= 1) {
throw new class extends Error {
constructor() {
super('failed to connect');
this.errors = errors;
}
}
}
await wait(retryWait);
return connectWithRetry(host, port, tries - 1, retryWait, errors, target);
}
}
async function startProfiling(options) {
const client = await connectWithRetry(options.host, options.port, options.tries, options.retryWait, [], options.target);
const { Runtime, Profiler } = client;
if (options.checkForPaused) {
// ensure the runtime isn't being debugged
let { Debugger } = client;
let isPaused = false;
client.on('event', message => {
if (message.method === 'Debugger.paused') {
isPaused = true;
}
})
await Debugger.enable();
if (isPaused) {
// client.close();
// ⬆︎ this leaks the connection but there is an issue in
// chrome that it will resume the runtime whenever a client
// disconnects. Because things are relatively short-lived
// we trade the leakage for being able to debug
return Promise.reject('runtime is paused');
}
} else {
// resume form inspect-brk
await Runtime.runIfWaitingForDebugger();
}
// now start profiling
await Profiler.enable();
await Profiler.setSamplingInterval({ interval: 100 });
await Profiler.start();
return {
stop: async function (n = 0) {
if (n > 0) {
await wait(n);
}
const data = await Profiler.stop();
await client.close();
return data;
}
}
}
module.exports = {
startProfiling
}