This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
mapperServer.ts
155 lines (131 loc) · 4.75 KB
/
mapperServer.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
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
import debug from 'debug';
const debugInternal = debug('bidiMapper:internal');
const debugSend = debug('bidiMapper:SEND ►');
const debugRecv = debug('bidiMapper:RECV ◀');
const debugLog = debug('bidiMapper:log');
import { IServer } from './iServer';
import WebSocket from 'ws';
export class MapperServer implements IServer {
private _handlers: ((messageObj: any) => void)[] = new Array()
private _commandCallbacks: ((messageObj: any) => void)[] = new Array()
private _ws: WebSocket
private _mapperSessionId;
static async create(cdpUrl: string, mapperContent: string): Promise<MapperServer> {
const mapper = new MapperServer;
await mapper._establishCdpSession(cdpUrl);
await mapper._initMapper(mapperContent);
return mapper;
}
setOnMessage(handler: (messageObj: any) => void): void {
this._handlers.push(handler);
}
sendMessage(messageObj: string): Promise<void> {
return this._sendBidiMessage(messageObj);
}
private _establishCdpSession: (cdpUrl: string) => Promise<void> = async function (cdpUrl: string) {
return new Promise(resolve => {
debugInternal("Establishing session with cdpUrl: ", cdpUrl);
this._ws = new WebSocket(cdpUrl);
this._ws.on('message', (dataStr: string) => {
this._onCdpMessage(dataStr);
});
this._ws.on('open', () => {
debugInternal("Session established.");
resolve();
});
});
};
private _sendBidiMessage(bidiMessageObj: any): Promise<void> {
return this._sendCdpCommand({
method: "Runtime.evaluate",
sessionId: this._mapperSessionId,
params: {
expression: "onBidiMessage(" + JSON.stringify(bidiMessageObj) + ")"
}
});
}
private _onBidiMessage(bidiMessageObj: any): void {
for (let handler of this._handlers)
handler(bidiMessageObj);
}
private _onCdpMessage(dataStr: string): void {
const data = JSON.parse(dataStr);
debugRecv(data);
if (this._commandCallbacks.hasOwnProperty(data.id)) {
this._commandCallbacks[data.id](data.result);
return;
} else {
if (data.method === "Runtime.bindingCalled" && data.params && data.params.name === "sendBidiResponse") {
this._onBidiMessage(data.params.payload);
return;
}
if (data.method === "Runtime.consoleAPICalled") {
debugLog.apply(null, data.params.args.map(arg=>arg.value));
return;
}
}
}
private async _sendCdpCommand(command: any): Promise<any> {
return new Promise((resolve) => {
const id = this._commandCallbacks.length;
this._commandCallbacks[id] = resolve;
command.id = id;
debugSend(command);
this._ws.send(JSON.stringify(command));
});
};
private async _initMapper(mapperContent: string): Promise<void> {
debugInternal("Connection opened.");
// await this._sendCdpCommand({
// method: "Log.enable"
// })
const targetId = (
await this._sendCdpCommand({
method: "Target.createTarget",
params: {
url: "about:blank"
}
})).targetId;
this._mapperSessionId = (await this._sendCdpCommand({
"method": "Target.attachToTarget",
"params": {
"targetId": targetId,
"flatten": true
}
})).sessionId;
await this._sendCdpCommand({
method: "Runtime.enable",
sessionId: this._mapperSessionId
})
await this._sendCdpCommand({
method: "Target.exposeDevToolsProtocol",
params: {
bindingName: "cdp",
targetId
}
});
await this._sendCdpCommand({
method: "Runtime.addBinding",
sessionId: this._mapperSessionId,
params: {
name: "sendBidiResponse"
}
});
await this._sendCdpCommand({
method: "Runtime.evaluate",
sessionId: this._mapperSessionId,
params: {
expression: mapperContent
}
})
// Let Mapper know what is it's TargetId to filter out related targets.
await this._sendCdpCommand({
method: "Runtime.evaluate",
sessionId: this._mapperSessionId,
params: {
expression: "window.setCurrentTargetId(" + JSON.stringify(targetId) + ")"
}
});
debugInternal("Launched!");
}
}