feat(run-server): expose --host (#28723)

This commit is contained in:
Max Schmitt 2023-12-19 23:19:46 +01:00 committed by GitHub
parent dcaa5758f0
commit b88b194e9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 4 deletions

View File

@ -53,6 +53,7 @@ export function runDriver() {
export type RunServerOptions = { export type RunServerOptions = {
port?: number, port?: number,
host?: string,
path?: string, path?: string,
extension?: boolean, extension?: boolean,
maxConnections?: number, maxConnections?: number,
@ -63,12 +64,13 @@ export type RunServerOptions = {
export async function runServer(options: RunServerOptions) { export async function runServer(options: RunServerOptions) {
const { const {
port, port,
host,
path = '/', path = '/',
maxConnections = Infinity, maxConnections = Infinity,
extension, extension,
} = options; } = options;
const server = new PlaywrightServer({ mode: extension ? 'extension' : 'default', path, maxConnections }); const server = new PlaywrightServer({ mode: extension ? 'extension' : 'default', path, maxConnections });
const wsEndpoint = await server.listen(port); const wsEndpoint = await server.listen(port, host);
process.on('exit', () => server.close().catch(console.error)); process.on('exit', () => server.close().catch(console.error));
console.log('Listening on ' + wsEndpoint); console.log('Listening on ' + wsEndpoint);
process.stdin.on('close', () => gracefullyProcessExitDoNotHang(0)); process.stdin.on('close', () => gracefullyProcessExitDoNotHang(0));

View File

@ -257,12 +257,14 @@ program
program program
.command('run-server', { hidden: true }) .command('run-server', { hidden: true })
.option('--port <port>', 'Server port') .option('--port <port>', 'Server port')
.option('--host <host>', 'Server host')
.option('--path <path>', 'Endpoint Path', '/') .option('--path <path>', 'Endpoint Path', '/')
.option('--max-clients <maxClients>', 'Maximum clients') .option('--max-clients <maxClients>', 'Maximum clients')
.option('--mode <mode>', 'Server mode, either "default" or "extension"') .option('--mode <mode>', 'Server mode, either "default" or "extension"')
.action(function(options) { .action(function(options) {
runServer({ runServer({
port: options.port ? +options.port : undefined, port: options.port ? +options.port : undefined,
host: options.host,
path: options.path, path: options.path,
maxConnections: options.maxClients ? +options.maxClients : Infinity, maxConnections: options.maxClients ? +options.maxClients : Infinity,
extension: options.mode === 'extension' || !!process.env.PW_EXTENSION_MODE, extension: options.mode === 'extension' || !!process.env.PW_EXTENSION_MODE,

View File

@ -56,7 +56,7 @@ export class PlaywrightServer {
this._preLaunchedPlaywright = options.preLaunchedAndroidDevice._android.attribution.playwright; this._preLaunchedPlaywright = options.preLaunchedAndroidDevice._android.attribution.playwright;
} }
async listen(port: number = 0): Promise<string> { async listen(port: number = 0, hostname?: string): Promise<string> {
debugLogger.log('server', `Server started at ${new Date()}`); debugLogger.log('server', `Server started at ${new Date()}`);
const server = createHttpServer((request: http.IncomingMessage, response: http.ServerResponse) => { const server = createHttpServer((request: http.IncomingMessage, response: http.ServerResponse) => {
@ -73,13 +73,13 @@ export class PlaywrightServer {
this._server = server; this._server = server;
const wsEndpoint = await new Promise<string>((resolve, reject) => { const wsEndpoint = await new Promise<string>((resolve, reject) => {
server.listen(port, () => { server.listen(port, hostname, () => {
const address = server.address(); const address = server.address();
if (!address) { if (!address) {
reject(new Error('Could not bind server socket')); reject(new Error('Could not bind server socket'));
return; return;
} }
const wsEndpoint = typeof address === 'string' ? `${address}${this._options.path}` : `ws://127.0.0.1:${address.port}${this._options.path}`; const wsEndpoint = typeof address === 'string' ? `${address}${this._options.path}` : `ws://${hostname || 'localhost'}:${address.port}${this._options.path}`;
resolve(wsEndpoint); resolve(wsEndpoint);
}).on('error', reject); }).on('error', reject);
}); });