-
Notifications
You must be signed in to change notification settings - Fork 10
/
example.ts
66 lines (56 loc) · 1.83 KB
/
example.ts
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
import * as fs from 'fs'
import * as WebSocket from 'ws'
import WebSocketServer = WebSocket.Server
import * as http from 'http'
import Crdp from 'chrome-remote-debug-protocol'
import DevToolsProtocol from 'devtools-protocol'
import * as rpc from '../lib/noice-json-rpc'
async function setupClient() {
try {
const rpcClient = new rpc.Client(new WebSocket('ws://localhost:8080'), {logConsole: true})
const api: DevToolsProtocol.ProtocolApi = rpcClient.api()
await Promise.all([
api.Runtime.enable(),
api.Debugger.enable(),
api.Profiler.enable(),
])
await api.Profiler.start()
await new Promise((resolve) => api.Runtime.on('executionContextDestroyed', resolve)); // Wait for event
const result = await api.Profiler.stop()
console.log('Result', result)
process.exit(0)
} catch (e) {
console.error(e)
}
}
function setupServer() {
const wssServer = new WebSocketServer({port: 8080});
const api: Crdp.CrdpServer = new rpc.Server(wssServer).api();
const enable = () => Promise.resolve()
api.Debugger.expose({enable})
api.Profiler.expose({enable})
api.Runtime.expose({
enable,
})
api.Profiler.expose({
enable,
start() {
setTimeout(() => {
api.Runtime.emitExecutionContextDestroyed({executionContextId: 1})
}, 1000)
return Promise.resolve()
},
stop() {
const response: Crdp.Profiler.StopResponse = {
profile: {
nodes: [],
startTime: 0,
endTime: 100
}
}
return Promise.resolve(response)
}
})
}
setupServer()
setupClient()