diff --git a/packages/playwright-core/src/cli/cli.ts b/packages/playwright-core/src/cli/cli.ts index 2db65f09e6..c0eeb2b130 100755 --- a/packages/playwright-core/src/cli/cli.ts +++ b/packages/playwright-core/src/cli/cli.ts @@ -240,10 +240,11 @@ Examples: program .command('experimental-grid-server', { hidden: true }) .option('--port ', 'grid port; defaults to 3333') + .option('--address
', 'address of the server') .option('--agent-factory ', 'path to grid agent factory or npm package') .option('--auth-token ', '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 ', 'specify browser viewport size in pixels, for example "1280, 720"'); } -async function launchGridServer(factoryPathOrPackageName: string, port: number, authToken: string|undefined): Promise { +async function launchGridServer(factoryPathOrPackageName: string, port: number, address: string | undefined, authToken: string | undefined): Promise { 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()); } diff --git a/packages/playwright-core/src/grid/gridServer.ts b/packages/playwright-core/src/grid/gridServer.ts index e0aa3f6277..1247cda680 100644 --- a/packages/playwright-core/src/grid/gridServer.ts +++ b/packages/playwright-core/src/grid/gridServer.ts @@ -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', () => { diff --git a/packages/playwright-core/src/utils/httpServer.ts b/packages/playwright-core/src/utils/httpServer.ts index d413fec142..a4e4dc17b3 100644 --- a/packages/playwright-core/src/utils/httpServer.ts +++ b/packages/playwright-core/src/utils/httpServer.ts @@ -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(); - 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 { - 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; }