feat: pass server address as argument (#13412)

This commit is contained in:
Yury Semikhatsky 2022-04-07 17:22:52 -07:00 committed by GitHub
parent eaab6406cd
commit f803a929e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 15 deletions

View File

@ -240,10 +240,11 @@ Examples:
program
.command('experimental-grid-server', { hidden: true })
.option('--port <port>', 'grid port; defaults to 3333')
.option('--address <address>', 'address of the server')
.option('--agent-factory <factory>', 'path to grid agent factory or npm package')
.option('--auth-token <authToken>', 'optional authentication token')
.action(function(options) {
launchGridServer(options.agentFactory, options.port || 3333, options.authToken);
launchGridServer(options.agentFactory, options.port || 3333, options.address, options.authToken);
});
program
@ -641,7 +642,7 @@ function commandWithOpenOptions(command: string, description: string, options: a
.option('--viewport-size <size>', 'specify browser viewport size in pixels, for example "1280, 720"');
}
async function launchGridServer(factoryPathOrPackageName: string, port: number, authToken: string|undefined): Promise<void> {
async function launchGridServer(factoryPathOrPackageName: string, port: number, address: string | undefined, authToken: string | undefined): Promise<void> {
if (!factoryPathOrPackageName)
factoryPathOrPackageName = path.join('..', 'grid', 'simpleGridFactory');
let factory;
@ -655,7 +656,7 @@ async function launchGridServer(factoryPathOrPackageName: string, port: number,
if (!factory || !factory.launch || typeof factory.launch !== 'function')
throw new Error('factory does not export `launch` method');
factory.name = factory.name || factoryPathOrPackageName;
const gridServer = new GridServer(factory as GridFactory, authToken);
const gridServer = new GridServer(factory as GridFactory, authToken, address);
await gridServer.start(port);
console.log('Grid server is running at ' + gridServer.urlPrefix());
}

View File

@ -220,11 +220,11 @@ export class GridServer {
private _factory: GridFactory;
private _pwVersion: string;
constructor(factory: GridFactory, authToken: string = '') {
constructor(factory: GridFactory, authToken: string = '', address: string = '') {
this._log = debug(`pw:grid:server`);
this._log(`using factory ${factory.name}`);
this._authToken = authToken || '';
this._server = new HttpServer();
this._server = new HttpServer(address);
this._factory = factory;
this._pwVersion = getPlaywrightVersion(true /* majorMinorOnly */);
@ -323,7 +323,7 @@ export class GridServer {
return await initPromise;
}
private _createAgent(): {agent: GridAgent, initPromise: Promise<{ error: any }>} {
private _createAgent(): { agent: GridAgent, initPromise: Promise<{ error: any }> } {
const agent = new GridAgent(this._factory.capacity, this._factory.launchTimeout, this._factory.retireTimeout);
this._agents.set(agent.agentId, agent);
agent.on('close', () => {

View File

@ -27,10 +27,11 @@ export class HttpServer {
private _server: http.Server;
private _urlPrefix: string;
private _port: number = 0;
private _started = false;
private _routes: { prefix?: string, exact?: string, handler: ServerRouteHandler }[] = [];
private _activeSockets = new Set<import('net').Socket>();
constructor() {
this._urlPrefix = '';
constructor(address: string = '') {
this._urlPrefix = address;
this._server = http.createServer(this._onRequest.bind(this));
}
@ -51,7 +52,8 @@ export class HttpServer {
}
async start(port?: number): Promise<string> {
assert(!this._urlPrefix, 'server already started');
assert(!this._started, 'server already started');
this._started = true;
this._server.on('connection', socket => {
this._activeSockets.add(socket);
socket.once('close', () => this._activeSockets.delete(socket));
@ -59,12 +61,14 @@ export class HttpServer {
this._server.listen(port);
await new Promise(cb => this._server!.once('listening', cb));
const address = this._server.address();
if (typeof address === 'string') {
this._urlPrefix = address;
} else {
assert(address, 'Could not bind server socket');
this._port = address.port;
this._urlPrefix = `http://127.0.0.1:${address.port}`;
assert(address, 'Could not bind server socket');
if (!this._urlPrefix) {
if (typeof address === 'string') {
this._urlPrefix = address;
} else {
this._port = address.port;
this._urlPrefix = `http://127.0.0.1:${address.port}`;
}
}
return this._urlPrefix;
}