2021-01-28 15:09:20 -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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as http from 'http';
|
2021-02-11 06:36:15 -08:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
feat: introduce experimental general-purpose grid (#8941)
This patch adds a general-purpose grid framework to parallelize
Playwright across multiple agents.
This patch adds two CLI commands to manage grid:
- `npx playwright experimental-grid-server` - to launch grid
- `npx playwrigth experimental-grid-agent` - to launch agent in a host
environment.
Grid server accepts an `--agent-factory` argument. A simple
`factory.js` might look like this:
```js
const child_process = require('child_process');
module.exports = {
name: 'My Simple Factory',
capacity: Infinity, // How many workers launch per agent
timeout: 10_000, // 10 seconds timeout to create agent
launch: ({agentId, gridURL, playwrightVersion}) => child_process.spawn(`npx`, [
'playwright'
'experimental-grid-agent',
'--grid-url', gridURL,
'--agent-id', agentId,
], {
cwd: __dirname,
shell: true,
stdio: 'inherit',
}),
};
```
With this `factory.js`, grid server could be launched like this:
```bash
npx playwright experimental-grid-server --factory=./factory.js
```
Once launched, it could be used with Playwright Test using env variable:
```bash
PW_GRID=http://localhost:3000 npx playwright test
```
2021-09-16 01:20:36 -07:00
|
|
|
import { Server as WebSocketServer } from 'ws';
|
2021-08-25 11:18:35 -07:00
|
|
|
import * as mime from 'mime';
|
2021-10-01 19:40:47 -07:00
|
|
|
import { assert } from './utils';
|
2021-10-07 14:49:30 -08:00
|
|
|
import { VirtualFileSystem } from './vfs';
|
2021-01-28 15:09:20 -08:00
|
|
|
|
|
|
|
export type ServerRouteHandler = (request: http.IncomingMessage, response: http.ServerResponse) => boolean;
|
|
|
|
|
2021-02-25 08:25:52 -08:00
|
|
|
export class HttpServer {
|
feat: introduce experimental general-purpose grid (#8941)
This patch adds a general-purpose grid framework to parallelize
Playwright across multiple agents.
This patch adds two CLI commands to manage grid:
- `npx playwright experimental-grid-server` - to launch grid
- `npx playwrigth experimental-grid-agent` - to launch agent in a host
environment.
Grid server accepts an `--agent-factory` argument. A simple
`factory.js` might look like this:
```js
const child_process = require('child_process');
module.exports = {
name: 'My Simple Factory',
capacity: Infinity, // How many workers launch per agent
timeout: 10_000, // 10 seconds timeout to create agent
launch: ({agentId, gridURL, playwrightVersion}) => child_process.spawn(`npx`, [
'playwright'
'experimental-grid-agent',
'--grid-url', gridURL,
'--agent-id', agentId,
], {
cwd: __dirname,
shell: true,
stdio: 'inherit',
}),
};
```
With this `factory.js`, grid server could be launched like this:
```bash
npx playwright experimental-grid-server --factory=./factory.js
```
Once launched, it could be used with Playwright Test using env variable:
```bash
PW_GRID=http://localhost:3000 npx playwright test
```
2021-09-16 01:20:36 -07:00
|
|
|
private _server: http.Server;
|
2021-01-28 15:09:20 -08:00
|
|
|
private _urlPrefix: string;
|
feat: introduce experimental general-purpose grid (#8941)
This patch adds a general-purpose grid framework to parallelize
Playwright across multiple agents.
This patch adds two CLI commands to manage grid:
- `npx playwright experimental-grid-server` - to launch grid
- `npx playwrigth experimental-grid-agent` - to launch agent in a host
environment.
Grid server accepts an `--agent-factory` argument. A simple
`factory.js` might look like this:
```js
const child_process = require('child_process');
module.exports = {
name: 'My Simple Factory',
capacity: Infinity, // How many workers launch per agent
timeout: 10_000, // 10 seconds timeout to create agent
launch: ({agentId, gridURL, playwrightVersion}) => child_process.spawn(`npx`, [
'playwright'
'experimental-grid-agent',
'--grid-url', gridURL,
'--agent-id', agentId,
], {
cwd: __dirname,
shell: true,
stdio: 'inherit',
}),
};
```
With this `factory.js`, grid server could be launched like this:
```bash
npx playwright experimental-grid-server --factory=./factory.js
```
Once launched, it could be used with Playwright Test using env variable:
```bash
PW_GRID=http://localhost:3000 npx playwright test
```
2021-09-16 01:20:36 -07:00
|
|
|
private _port: number = 0;
|
2021-02-26 14:16:32 -08:00
|
|
|
private _routes: { prefix?: string, exact?: string, handler: ServerRouteHandler }[] = [];
|
2021-09-15 11:40:54 -04:00
|
|
|
private _activeSockets = new Set<import('net').Socket>();
|
2021-02-25 08:25:52 -08:00
|
|
|
constructor() {
|
2021-01-28 15:09:20 -08:00
|
|
|
this._urlPrefix = '';
|
feat: introduce experimental general-purpose grid (#8941)
This patch adds a general-purpose grid framework to parallelize
Playwright across multiple agents.
This patch adds two CLI commands to manage grid:
- `npx playwright experimental-grid-server` - to launch grid
- `npx playwrigth experimental-grid-agent` - to launch agent in a host
environment.
Grid server accepts an `--agent-factory` argument. A simple
`factory.js` might look like this:
```js
const child_process = require('child_process');
module.exports = {
name: 'My Simple Factory',
capacity: Infinity, // How many workers launch per agent
timeout: 10_000, // 10 seconds timeout to create agent
launch: ({agentId, gridURL, playwrightVersion}) => child_process.spawn(`npx`, [
'playwright'
'experimental-grid-agent',
'--grid-url', gridURL,
'--agent-id', agentId,
], {
cwd: __dirname,
shell: true,
stdio: 'inherit',
}),
};
```
With this `factory.js`, grid server could be launched like this:
```bash
npx playwright experimental-grid-server --factory=./factory.js
```
Once launched, it could be used with Playwright Test using env variable:
```bash
PW_GRID=http://localhost:3000 npx playwright test
```
2021-09-16 01:20:36 -07:00
|
|
|
this._server = http.createServer(this._onRequest.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
createWebSocketServer(): WebSocketServer {
|
|
|
|
return new WebSocketServer({ server: this._server });
|
2021-01-28 15:09:20 -08:00
|
|
|
}
|
|
|
|
|
2021-02-26 14:16:32 -08:00
|
|
|
routePrefix(prefix: string, handler: ServerRouteHandler) {
|
|
|
|
this._routes.push({ prefix, handler });
|
2021-01-28 15:09:20 -08:00
|
|
|
}
|
|
|
|
|
2021-02-26 14:16:32 -08:00
|
|
|
routePath(path: string, handler: ServerRouteHandler) {
|
|
|
|
this._routes.push({ exact: path, handler });
|
2021-01-28 15:09:20 -08:00
|
|
|
}
|
|
|
|
|
feat: introduce experimental general-purpose grid (#8941)
This patch adds a general-purpose grid framework to parallelize
Playwright across multiple agents.
This patch adds two CLI commands to manage grid:
- `npx playwright experimental-grid-server` - to launch grid
- `npx playwrigth experimental-grid-agent` - to launch agent in a host
environment.
Grid server accepts an `--agent-factory` argument. A simple
`factory.js` might look like this:
```js
const child_process = require('child_process');
module.exports = {
name: 'My Simple Factory',
capacity: Infinity, // How many workers launch per agent
timeout: 10_000, // 10 seconds timeout to create agent
launch: ({agentId, gridURL, playwrightVersion}) => child_process.spawn(`npx`, [
'playwright'
'experimental-grid-agent',
'--grid-url', gridURL,
'--agent-id', agentId,
], {
cwd: __dirname,
shell: true,
stdio: 'inherit',
}),
};
```
With this `factory.js`, grid server could be launched like this:
```bash
npx playwright experimental-grid-server --factory=./factory.js
```
Once launched, it could be used with Playwright Test using env variable:
```bash
PW_GRID=http://localhost:3000 npx playwright test
```
2021-09-16 01:20:36 -07:00
|
|
|
port(): number {
|
|
|
|
return this._port;
|
|
|
|
}
|
|
|
|
|
2021-02-26 14:16:32 -08:00
|
|
|
async start(port?: number): Promise<string> {
|
feat: introduce experimental general-purpose grid (#8941)
This patch adds a general-purpose grid framework to parallelize
Playwright across multiple agents.
This patch adds two CLI commands to manage grid:
- `npx playwright experimental-grid-server` - to launch grid
- `npx playwrigth experimental-grid-agent` - to launch agent in a host
environment.
Grid server accepts an `--agent-factory` argument. A simple
`factory.js` might look like this:
```js
const child_process = require('child_process');
module.exports = {
name: 'My Simple Factory',
capacity: Infinity, // How many workers launch per agent
timeout: 10_000, // 10 seconds timeout to create agent
launch: ({agentId, gridURL, playwrightVersion}) => child_process.spawn(`npx`, [
'playwright'
'experimental-grid-agent',
'--grid-url', gridURL,
'--agent-id', agentId,
], {
cwd: __dirname,
shell: true,
stdio: 'inherit',
}),
};
```
With this `factory.js`, grid server could be launched like this:
```bash
npx playwright experimental-grid-server --factory=./factory.js
```
Once launched, it could be used with Playwright Test using env variable:
```bash
PW_GRID=http://localhost:3000 npx playwright test
```
2021-09-16 01:20:36 -07:00
|
|
|
console.assert(!this._urlPrefix, 'server already started');
|
2021-09-15 11:40:54 -04:00
|
|
|
this._server.on('connection', socket => {
|
|
|
|
this._activeSockets.add(socket);
|
|
|
|
socket.once('close', () => this._activeSockets.delete(socket));
|
|
|
|
});
|
2021-02-26 14:16:32 -08:00
|
|
|
this._server.listen(port);
|
2021-01-28 15:09:20 -08:00
|
|
|
await new Promise(cb => this._server!.once('listening', cb));
|
|
|
|
const address = this._server.address();
|
feat: introduce experimental general-purpose grid (#8941)
This patch adds a general-purpose grid framework to parallelize
Playwright across multiple agents.
This patch adds two CLI commands to manage grid:
- `npx playwright experimental-grid-server` - to launch grid
- `npx playwrigth experimental-grid-agent` - to launch agent in a host
environment.
Grid server accepts an `--agent-factory` argument. A simple
`factory.js` might look like this:
```js
const child_process = require('child_process');
module.exports = {
name: 'My Simple Factory',
capacity: Infinity, // How many workers launch per agent
timeout: 10_000, // 10 seconds timeout to create agent
launch: ({agentId, gridURL, playwrightVersion}) => child_process.spawn(`npx`, [
'playwright'
'experimental-grid-agent',
'--grid-url', gridURL,
'--agent-id', agentId,
], {
cwd: __dirname,
shell: true,
stdio: 'inherit',
}),
};
```
With this `factory.js`, grid server could be launched like this:
```bash
npx playwright experimental-grid-server --factory=./factory.js
```
Once launched, it could be used with Playwright Test using env variable:
```bash
PW_GRID=http://localhost:3000 npx playwright test
```
2021-09-16 01:20:36 -07:00
|
|
|
if (typeof address === 'string') {
|
|
|
|
this._urlPrefix = address;
|
|
|
|
} else {
|
2021-10-01 19:40:47 -07:00
|
|
|
assert(address, 'Could not bind server socket');
|
feat: introduce experimental general-purpose grid (#8941)
This patch adds a general-purpose grid framework to parallelize
Playwright across multiple agents.
This patch adds two CLI commands to manage grid:
- `npx playwright experimental-grid-server` - to launch grid
- `npx playwrigth experimental-grid-agent` - to launch agent in a host
environment.
Grid server accepts an `--agent-factory` argument. A simple
`factory.js` might look like this:
```js
const child_process = require('child_process');
module.exports = {
name: 'My Simple Factory',
capacity: Infinity, // How many workers launch per agent
timeout: 10_000, // 10 seconds timeout to create agent
launch: ({agentId, gridURL, playwrightVersion}) => child_process.spawn(`npx`, [
'playwright'
'experimental-grid-agent',
'--grid-url', gridURL,
'--agent-id', agentId,
], {
cwd: __dirname,
shell: true,
stdio: 'inherit',
}),
};
```
With this `factory.js`, grid server could be launched like this:
```bash
npx playwright experimental-grid-server --factory=./factory.js
```
Once launched, it could be used with Playwright Test using env variable:
```bash
PW_GRID=http://localhost:3000 npx playwright test
```
2021-09-16 01:20:36 -07:00
|
|
|
this._port = address.port;
|
|
|
|
this._urlPrefix = `http://127.0.0.1:${address.port}`;
|
|
|
|
}
|
2021-01-28 15:09:20 -08:00
|
|
|
return this._urlPrefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
async stop() {
|
2021-09-15 11:40:54 -04:00
|
|
|
for (const socket of this._activeSockets)
|
|
|
|
socket.destroy();
|
2021-01-28 15:09:20 -08:00
|
|
|
await new Promise(cb => this._server!.close(cb));
|
|
|
|
}
|
|
|
|
|
feat: introduce experimental general-purpose grid (#8941)
This patch adds a general-purpose grid framework to parallelize
Playwright across multiple agents.
This patch adds two CLI commands to manage grid:
- `npx playwright experimental-grid-server` - to launch grid
- `npx playwrigth experimental-grid-agent` - to launch agent in a host
environment.
Grid server accepts an `--agent-factory` argument. A simple
`factory.js` might look like this:
```js
const child_process = require('child_process');
module.exports = {
name: 'My Simple Factory',
capacity: Infinity, // How many workers launch per agent
timeout: 10_000, // 10 seconds timeout to create agent
launch: ({agentId, gridURL, playwrightVersion}) => child_process.spawn(`npx`, [
'playwright'
'experimental-grid-agent',
'--grid-url', gridURL,
'--agent-id', agentId,
], {
cwd: __dirname,
shell: true,
stdio: 'inherit',
}),
};
```
With this `factory.js`, grid server could be launched like this:
```bash
npx playwright experimental-grid-server --factory=./factory.js
```
Once launched, it could be used with Playwright Test using env variable:
```bash
PW_GRID=http://localhost:3000 npx playwright test
```
2021-09-16 01:20:36 -07:00
|
|
|
urlPrefix(): string {
|
2021-01-28 15:09:20 -08:00
|
|
|
return this._urlPrefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
serveFile(response: http.ServerResponse, absoluteFilePath: string, headers?: { [name: string]: string }): boolean {
|
|
|
|
try {
|
|
|
|
const content = fs.readFileSync(absoluteFilePath);
|
|
|
|
response.statusCode = 200;
|
2021-08-25 11:18:35 -07:00
|
|
|
const contentType = mime.getType(path.extname(absoluteFilePath)) || 'application/octet-stream';
|
2021-01-28 15:09:20 -08:00
|
|
|
response.setHeader('Content-Type', contentType);
|
|
|
|
response.setHeader('Content-Length', content.byteLength);
|
|
|
|
for (const [name, value] of Object.entries(headers || {}))
|
|
|
|
response.setHeader(name, value);
|
|
|
|
response.end(content);
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 14:49:30 -08:00
|
|
|
async serveVirtualFile(response: http.ServerResponse, vfs: VirtualFileSystem, entry: string, headers?: { [name: string]: string }) {
|
|
|
|
try {
|
|
|
|
const content = await vfs.read(entry);
|
|
|
|
response.statusCode = 200;
|
|
|
|
const contentType = mime.getType(path.extname(entry)) || 'application/octet-stream';
|
|
|
|
response.setHeader('Content-Type', contentType);
|
|
|
|
response.setHeader('Content-Length', content.byteLength);
|
|
|
|
for (const [name, value] of Object.entries(headers || {}))
|
|
|
|
response.setHeader(name, value);
|
|
|
|
response.end(content);
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 15:09:20 -08:00
|
|
|
private _onRequest(request: http.IncomingMessage, response: http.ServerResponse) {
|
2021-10-12 20:21:06 -08:00
|
|
|
response.setHeader('Access-Control-Allow-Origin', '*');
|
|
|
|
response.setHeader('Access-Control-Request-Method', '*');
|
|
|
|
response.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
|
|
|
|
if (request.headers.origin)
|
|
|
|
response.setHeader('Access-Control-Allow-Headers', request.headers.origin);
|
|
|
|
|
|
|
|
if (request.method === 'OPTIONS') {
|
|
|
|
response.writeHead(200);
|
|
|
|
response.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-28 15:09:20 -08:00
|
|
|
request.on('error', () => response.end());
|
|
|
|
try {
|
|
|
|
if (!request.url) {
|
|
|
|
response.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const url = new URL('http://localhost' + request.url);
|
|
|
|
for (const route of this._routes) {
|
|
|
|
if (route.exact && url.pathname === route.exact && route.handler(request, response))
|
|
|
|
return;
|
|
|
|
if (route.prefix && url.pathname.startsWith(route.prefix) && route.handler(request, response))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
response.statusCode = 404;
|
|
|
|
response.end();
|
|
|
|
} catch (e) {
|
|
|
|
response.end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|