()
| 233 | } |
| 234 | |
| 235 | private async createServer() { |
| 236 | const path = `/${randomBytes(20).toString('hex')}`; |
| 237 | const server = await acquireTrackedWebSocketServer(this.portTracker, { |
| 238 | perMessageDeflate: true, |
| 239 | path, |
| 240 | }); |
| 241 | |
| 242 | this.logger.info(LogTag.ProxyActivity, 'activated cdp proxy'); |
| 243 | |
| 244 | server.on('connection', client => { |
| 245 | const clientHandle = new ClientHandle(client, this.logger); |
| 246 | this.logger.info(LogTag.ProxyActivity, 'accepted proxy connection', { |
| 247 | id: clientHandle.id, |
| 248 | }); |
| 249 | |
| 250 | client.on('close', () => { |
| 251 | this.logger.verbose(LogTag.ProxyActivity, 'closed proxy connection', { |
| 252 | id: clientHandle.id, |
| 253 | }); |
| 254 | this.disposables.disposeObject(clientHandle); |
| 255 | }); |
| 256 | |
| 257 | client.on('message', async d => { |
| 258 | let message: CdpProtocol.ICommand; |
| 259 | try { |
| 260 | message = JSON.parse(d.toString()); |
| 261 | } catch (e) { |
| 262 | return clientHandle.send({ |
| 263 | id: 0, |
| 264 | error: { code: ProxyErrors.ParseError, message: e.message }, |
| 265 | }); |
| 266 | } |
| 267 | |
| 268 | this.logger.verbose(LogTag.ProxyActivity, 'received proxy message', message); |
| 269 | |
| 270 | const { method, params, id = 0 } = message; |
| 271 | const [domain, fn] = method.split('.'); |
| 272 | try { |
| 273 | const result = domain === jsDebugDomain |
| 274 | ? await this.invokeJsDebugDomainMethod(clientHandle, fn, params) |
| 275 | : await this.invokeCdpMethod(clientHandle, domain, fn, params); |
| 276 | clientHandle.send({ id, result }); |
| 277 | } catch (e) { |
| 278 | const error = e instanceof ProtocolError && e.cause |
| 279 | ? e.cause |
| 280 | : { code: 0, message: e.message }; |
| 281 | clientHandle.send({ id, error }); |
| 282 | } |
| 283 | }); |
| 284 | }); |
| 285 | |
| 286 | return { server, path }; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * @inheritdoc |
no test coverage detected