| 29 | export type ResponseSchema = CDPJSONPayload; |
| 30 | |
| 31 | export default class ChromiumJSONNewPutRoute extends HTTPRoute { |
| 32 | name = BrowserlessRoutes.ChromiumJSONNewPutRoute; |
| 33 | accepts = [contentTypes.any]; |
| 34 | auth = true; |
| 35 | browser = null; |
| 36 | concurrency = false; |
| 37 | contentTypes = [contentTypes.json]; |
| 38 | description = dedent(` |
| 39 | Returns a JSON payload that acts as a pass-through to the DevTools /json/new HTTP API in Chromium. |
| 40 | Browserless mocks this payload so that remote clients can connect to the underlying "webSocketDebuggerUrl" |
| 41 | which will cause Browserless to start the browser and proxy that request into a blank page. |
| 42 | `); |
| 43 | method = Methods.put; |
| 44 | path = HTTPRoutes.jsonNew; |
| 45 | tags = [APITags.browserAPI]; |
| 46 | |
| 47 | async handler(_req: Request, res: Response): Promise<void> { |
| 48 | const config = this.config(); |
| 49 | const externalAddress = config.getExternalWebSocketAddress(); |
| 50 | const id = pageID(); |
| 51 | const { protocol, host, pathname, href } = new URL( |
| 52 | `/devtools/page/${id}`, |
| 53 | externalAddress, |
| 54 | ); |
| 55 | const param = protocol.includes('wss') ? 'wss' : 'ws'; |
| 56 | const value = path.join(host, pathname); |
| 57 | |
| 58 | return jsonResponse(res, 200, { |
| 59 | description: '', |
| 60 | devtoolsFrontendUrl: `/devtools/inspector.html?${param}=${value}`, |
| 61 | id, |
| 62 | title: 'New Tab', |
| 63 | type: 'page', |
| 64 | url: 'about:blank', |
| 65 | webSocketDebuggerUrl: href, |
| 66 | }); |
| 67 | } |
| 68 | } |