2020-11-20 15:19:39 -08:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-02-11 06:36:15 -08:00
|
|
|
import debug from 'debug';
|
2020-11-20 15:19:39 -08:00
|
|
|
import * as http from 'http';
|
2021-04-12 11:14:54 -07:00
|
|
|
import * as ws from 'ws';
|
|
|
|
import { DispatcherConnection, DispatcherScope } from '../dispatchers/dispatcher';
|
2020-11-20 15:19:39 -08:00
|
|
|
import { PlaywrightDispatcher } from '../dispatchers/playwrightDispatcher';
|
2021-01-29 16:00:56 -08:00
|
|
|
import { createPlaywright } from '../server/playwright';
|
2021-07-07 21:14:16 +02:00
|
|
|
import { gracefullyCloseAll } from '../utils/processLauncher';
|
2020-11-20 15:19:39 -08:00
|
|
|
|
|
|
|
const debugLog = debug('pw:server');
|
|
|
|
|
2021-04-12 11:14:54 -07:00
|
|
|
export interface PlaywrightServerDelegate {
|
|
|
|
path: string;
|
|
|
|
allowMultipleClients: boolean;
|
2021-06-02 14:35:17 -07:00
|
|
|
onConnect(rootScope: DispatcherScope, forceDisconnect: () => void): Promise<() => any>;
|
2021-04-12 11:14:54 -07:00
|
|
|
onClose: () => any;
|
|
|
|
}
|
|
|
|
|
2021-06-02 14:35:17 -07:00
|
|
|
export type PlaywrightServerOptions = {
|
|
|
|
acceptForwardedPorts?: boolean
|
2021-08-04 19:45:33 +02:00
|
|
|
onDisconnect?: () => void;
|
2021-06-02 14:35:17 -07:00
|
|
|
};
|
|
|
|
|
2020-11-20 15:19:39 -08:00
|
|
|
export class PlaywrightServer {
|
2021-04-16 17:07:56 -07:00
|
|
|
private _wsServer: ws.Server | undefined;
|
2021-04-12 11:14:54 -07:00
|
|
|
private _clientsCount = 0;
|
|
|
|
private _delegate: PlaywrightServerDelegate;
|
|
|
|
|
2021-08-04 19:45:33 +02:00
|
|
|
static async startDefault({ acceptForwardedPorts, onDisconnect }: PlaywrightServerOptions = {}): Promise<PlaywrightServer> {
|
2021-04-16 11:14:57 -07:00
|
|
|
const cleanup = async () => {
|
|
|
|
await gracefullyCloseAll().catch(e => {});
|
|
|
|
};
|
2021-04-12 11:14:54 -07:00
|
|
|
const delegate: PlaywrightServerDelegate = {
|
|
|
|
path: '/ws',
|
|
|
|
allowMultipleClients: false,
|
2021-04-16 11:14:57 -07:00
|
|
|
onClose: cleanup,
|
2021-06-02 14:35:17 -07:00
|
|
|
onConnect: async (rootScope: DispatcherScope) => {
|
|
|
|
const playwright = createPlaywright();
|
|
|
|
if (acceptForwardedPorts)
|
|
|
|
await playwright._enablePortForwarding();
|
|
|
|
new PlaywrightDispatcher(rootScope, playwright);
|
|
|
|
return () => {
|
|
|
|
cleanup();
|
|
|
|
playwright._disablePortForwarding();
|
2021-06-15 14:56:29 -07:00
|
|
|
playwright.selectors.unregisterAll();
|
2021-08-04 19:45:33 +02:00
|
|
|
onDisconnect?.();
|
2021-06-02 14:35:17 -07:00
|
|
|
};
|
2021-04-12 11:14:54 -07:00
|
|
|
},
|
|
|
|
};
|
2021-06-02 17:19:01 -07:00
|
|
|
return new PlaywrightServer(delegate);
|
2021-04-12 11:14:54 -07:00
|
|
|
}
|
2020-11-20 15:19:39 -08:00
|
|
|
|
2021-04-12 11:14:54 -07:00
|
|
|
constructor(delegate: PlaywrightServerDelegate) {
|
|
|
|
this._delegate = delegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
async listen(port: number = 0): Promise<string> {
|
|
|
|
const server = http.createServer((request, response) => {
|
2020-11-20 15:19:39 -08:00
|
|
|
response.end('Running');
|
|
|
|
});
|
2021-04-12 11:14:54 -07:00
|
|
|
server.on('error', error => debugLog(error));
|
|
|
|
|
|
|
|
const path = this._delegate.path;
|
2021-06-02 14:35:17 -07:00
|
|
|
const wsEndpoint = await new Promise<string>((resolve, reject) => {
|
2021-04-12 11:14:54 -07:00
|
|
|
server.listen(port, () => {
|
|
|
|
const address = server.address();
|
|
|
|
const wsEndpoint = typeof address === 'string' ? `${address}${path}` : `ws://127.0.0.1:${address.port}${path}`;
|
|
|
|
resolve(wsEndpoint);
|
2021-06-02 14:35:17 -07:00
|
|
|
}).on('error', reject);
|
2021-04-12 11:14:54 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
debugLog('Listening at ' + wsEndpoint);
|
2020-11-20 15:19:39 -08:00
|
|
|
|
2021-04-16 17:07:56 -07:00
|
|
|
this._wsServer = new ws.Server({ server, path });
|
|
|
|
this._wsServer.on('connection', async socket => {
|
2021-04-12 11:14:54 -07:00
|
|
|
if (this._clientsCount && !this._delegate.allowMultipleClients) {
|
|
|
|
socket.close();
|
2020-11-20 15:19:39 -08:00
|
|
|
return;
|
|
|
|
}
|
2021-04-12 11:14:54 -07:00
|
|
|
this._clientsCount++;
|
2020-11-20 15:19:39 -08:00
|
|
|
debugLog('Incoming connection');
|
2021-04-12 11:14:54 -07:00
|
|
|
|
|
|
|
const connection = new DispatcherConnection();
|
|
|
|
connection.onmessage = message => {
|
|
|
|
if (socket.readyState !== ws.CLOSING)
|
|
|
|
socket.send(JSON.stringify(message));
|
|
|
|
};
|
|
|
|
socket.on('message', (message: string) => {
|
|
|
|
connection.dispatch(JSON.parse(Buffer.from(message).toString()));
|
|
|
|
});
|
|
|
|
|
2021-05-06 09:34:06 -07:00
|
|
|
const forceDisconnect = () => socket.close();
|
2021-04-12 11:14:54 -07:00
|
|
|
const scope = connection.rootDispatcher();
|
2021-06-02 14:35:17 -07:00
|
|
|
let onDisconnect = () => {};
|
2021-05-06 09:34:06 -07:00
|
|
|
const disconnected = () => {
|
2021-04-12 11:14:54 -07:00
|
|
|
this._clientsCount--;
|
|
|
|
// Avoid sending any more messages over closed socket.
|
|
|
|
connection.onmessage = () => {};
|
|
|
|
onDisconnect();
|
|
|
|
};
|
|
|
|
socket.on('close', () => {
|
2020-11-20 15:19:39 -08:00
|
|
|
debugLog('Client closed');
|
2021-05-06 09:34:06 -07:00
|
|
|
disconnected();
|
2020-11-20 15:19:39 -08:00
|
|
|
});
|
2021-04-12 11:14:54 -07:00
|
|
|
socket.on('error', error => {
|
2020-11-20 15:19:39 -08:00
|
|
|
debugLog('Client error ' + error);
|
2021-05-06 09:34:06 -07:00
|
|
|
disconnected();
|
2020-11-20 15:19:39 -08:00
|
|
|
});
|
2021-06-02 14:35:17 -07:00
|
|
|
onDisconnect = await this._delegate.onConnect(scope, forceDisconnect);
|
2020-11-20 15:19:39 -08:00
|
|
|
});
|
2021-04-12 11:14:54 -07:00
|
|
|
|
|
|
|
return wsEndpoint;
|
2020-11-20 15:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async close() {
|
2021-04-16 17:07:56 -07:00
|
|
|
if (!this._wsServer)
|
2020-11-20 15:19:39 -08:00
|
|
|
return;
|
|
|
|
debugLog('Closing server');
|
2021-04-16 17:07:56 -07:00
|
|
|
// First disconnect all remaining clients.
|
|
|
|
await new Promise(f => this._wsServer!.close(f));
|
|
|
|
await new Promise(f => this._wsServer!.options.server!.close(f));
|
2021-05-06 09:34:06 -07:00
|
|
|
this._wsServer = undefined;
|
2021-04-12 11:14:54 -07:00
|
|
|
await this._delegate.onClose();
|
2020-11-20 15:19:39 -08:00
|
|
|
}
|
|
|
|
}
|