chromium-bidi 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +245 -0
- package/package.json +58 -0
- package/src/.build/bidiMapper/mapper.js +26071 -0
- package/src/.build/bidiMapper/mapper.js.map +1 -0
- package/src/.build/bidiServer/bidiServerRunner.js +148 -0
- package/src/.build/bidiServer/bidiServerRunner.js.map +1 -0
- package/src/.build/bidiServer/cdp/cdpClient.js +122 -0
- package/src/.build/bidiServer/cdp/cdpClient.js.map +1 -0
- package/src/.build/bidiServer/cdp/cdpClient.spec.js +194 -0
- package/src/.build/bidiServer/cdp/cdpClient.spec.js.map +1 -0
- package/src/.build/bidiServer/cdp/cdpConnection.js +120 -0
- package/src/.build/bidiServer/cdp/cdpConnection.js.map +1 -0
- package/src/.build/bidiServer/cdp/cdpConnection.spec.js +154 -0
- package/src/.build/bidiServer/cdp/cdpConnection.spec.js.map +1 -0
- package/src/.build/bidiServer/cdp/cdpMessage.js +19 -0
- package/src/.build/bidiServer/cdp/cdpMessage.js.map +1 -0
- package/src/.build/bidiServer/cdp/index.js +24 -0
- package/src/.build/bidiServer/cdp/index.js.map +1 -0
- package/src/.build/bidiServer/cdp/websocketTransport.js +43 -0
- package/src/.build/bidiServer/cdp/websocketTransport.js.map +1 -0
- package/src/.build/bidiServer/index.js +95 -0
- package/src/.build/bidiServer/index.js.map +1 -0
- package/src/.build/bidiServer/mapperReader.js +28 -0
- package/src/.build/bidiServer/mapperReader.js.map +1 -0
- package/src/.build/bidiServer/mapperServer.js +137 -0
- package/src/.build/bidiServer/mapperServer.js.map +1 -0
- package/src/.build/bidiServer/tests/stubTransport.spec.js +44 -0
- package/src/.build/bidiServer/tests/stubTransport.spec.js.map +1 -0
- package/src/.build/bidiServer/utils/log.js +44 -0
- package/src/.build/bidiServer/utils/log.js.map +1 -0
- package/src/.build/bidiServer/utils/transport.js +19 -0
- package/src/.build/bidiServer/utils/transport.js.map +1 -0
- package/src/index.ts +110 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright 2021 Google LLC.
|
|
4
|
+
* Copyright (c) Microsoft Corporation.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
19
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
const argparse_1 = __importDefault(require("argparse"));
|
|
23
|
+
const puppeteer_1 = __importDefault(require("puppeteer"));
|
|
24
|
+
const mapperReader_1 = __importDefault(require("./mapperReader"));
|
|
25
|
+
const mapperServer_1 = require("./mapperServer");
|
|
26
|
+
const bidiServerRunner_1 = require("./bidiServerRunner");
|
|
27
|
+
function parseArguments() {
|
|
28
|
+
var parser = new argparse_1.default.ArgumentParser({
|
|
29
|
+
add_help: true,
|
|
30
|
+
exit_on_error: false,
|
|
31
|
+
});
|
|
32
|
+
parser.add_argument('-p', '--port', {
|
|
33
|
+
help: 'Port that BiDi server should listen to. Default is 8080.',
|
|
34
|
+
type: 'int',
|
|
35
|
+
default: process.env.PORT || 8080,
|
|
36
|
+
});
|
|
37
|
+
parser.add_argument('-hl', '--headless', {
|
|
38
|
+
help: 'Sets if browser should run in headless or headful mode. Default is `--headless=true`.',
|
|
39
|
+
default: true,
|
|
40
|
+
});
|
|
41
|
+
// `parse_known_args` puts known args in the first element of the result.
|
|
42
|
+
const args = parser.parse_known_args();
|
|
43
|
+
return args[0];
|
|
44
|
+
}
|
|
45
|
+
(async () => {
|
|
46
|
+
try {
|
|
47
|
+
console.log('Launching BiDi server.');
|
|
48
|
+
const args = parseArguments();
|
|
49
|
+
const bidiPort = args.port;
|
|
50
|
+
const headless = args.headless !== 'false';
|
|
51
|
+
bidiServerRunner_1.BidiServerRunner.run(bidiPort, (bidiServer) => {
|
|
52
|
+
return _onNewBidiConnectionOpen(headless, bidiServer);
|
|
53
|
+
});
|
|
54
|
+
console.log('BiDi server launched.');
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
console.log('Error', e);
|
|
58
|
+
}
|
|
59
|
+
})();
|
|
60
|
+
/**
|
|
61
|
+
* On each new BiDi connection:
|
|
62
|
+
* 1. Launch Chromium (using Puppeteer for now).
|
|
63
|
+
* 2. Get `BiDi-CDP` mapper JS binaries using `mapperReader`.
|
|
64
|
+
* 3. Run `BiDi-CDP` mapper in launched browser.
|
|
65
|
+
* 4. Bind `BiDi-CDP` mapper to the `BiDi server`.
|
|
66
|
+
*
|
|
67
|
+
* @returns delegate to be called when the connection is closed
|
|
68
|
+
*/
|
|
69
|
+
async function _onNewBidiConnectionOpen(headless, bidiTransport) {
|
|
70
|
+
// 1. Launch Chromium (using Puppeteer for now).
|
|
71
|
+
// Puppeteer should have downloaded Chromium during the installation.
|
|
72
|
+
// Use Puppeteer's logic of launching browser as well.
|
|
73
|
+
const browser = await puppeteer_1.default.launch({
|
|
74
|
+
headless,
|
|
75
|
+
});
|
|
76
|
+
// 2. Get `BiDi-CDP` mapper JS binaries using `mapperReader`.
|
|
77
|
+
const bidiMapperScript = await (0, mapperReader_1.default)();
|
|
78
|
+
// 3. Run `BiDi-CDP` mapper in launched browser.
|
|
79
|
+
const mapperServer = await mapperServer_1.MapperServer.create(browser.wsEndpoint(), bidiMapperScript);
|
|
80
|
+
// 4. Bind `BiDi-CDP` mapper to the `BiDi server`.
|
|
81
|
+
// Forward messages from BiDi Mapper to the client.
|
|
82
|
+
mapperServer.setOnMessage(async (message) => {
|
|
83
|
+
await bidiTransport.sendMessage(message);
|
|
84
|
+
});
|
|
85
|
+
// Forward messages from the client to BiDi Mapper.
|
|
86
|
+
bidiTransport.setOnMessage(async (message) => {
|
|
87
|
+
await mapperServer.sendMessage(message);
|
|
88
|
+
});
|
|
89
|
+
// Return delegate to be called when the connection is closed.
|
|
90
|
+
return async () => {
|
|
91
|
+
// Client disconnected. Close browser.
|
|
92
|
+
await browser.close();
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;;;AAEH,wDAAgC;AAChC,0DAAqD;AAErD,kEAA0C;AAC1C,iDAA8C;AAC9C,yDAAsD;AAGtD,SAAS,cAAc;IACrB,IAAI,MAAM,GAAG,IAAI,kBAAQ,CAAC,cAAc,CAAC;QACvC,QAAQ,EAAE,IAAI;QACd,aAAa,EAAE,KAAK;KACrB,CAAC,CAAC;IAEH,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,QAAQ,EAAE;QAClC,IAAI,EAAE,0DAA0D;QAChE,IAAI,EAAE,KAAK;QACX,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,IAAI;KAClC,CAAC,CAAC;IAEH,MAAM,CAAC,YAAY,CAAC,KAAK,EAAE,YAAY,EAAE;QACvC,IAAI,EAAE,uFAAuF;QAC7F,OAAO,EAAE,IAAI;KACd,CAAC,CAAC;IAEH,yEAAyE;IACzE,MAAM,IAAI,GAAG,MAAM,CAAC,gBAAgB,EAAE,CAAC;IACvC,OAAO,IAAI,CAAC,CAAC,CAAC,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AAE;QAC3C,MAAM,YAAY,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IAC1C,CAAC,CAAC,CAAC;IAEH,8DAA8D;IAC9D,OAAO,KAAK,IAAI,EAAE;QAChB,sCAAsC;QACtC,MAAM,OAAO,CAAC,KAAK,EAAE,CAAC;IACxB,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright 2021 Google LLC.
|
|
4
|
+
* Copyright (c) Microsoft Corporation.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
19
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
const promises_1 = __importDefault(require("fs/promises"));
|
|
23
|
+
const path_1 = __importDefault(require("path"));
|
|
24
|
+
async function read() {
|
|
25
|
+
return await promises_1.default.readFile(path_1.default.join(__dirname, '../bidiMapper/mapper.js'), 'utf8');
|
|
26
|
+
}
|
|
27
|
+
exports.default = read;
|
|
28
|
+
//# sourceMappingURL=mapperReader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mapperReader.js","sourceRoot":"","sources":["../../mapperReader.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;;;AAEH,2DAA6B;AAC7B,gDAAwB;AAET,KAAK,UAAU,IAAI;IAChC,OAAO,MAAM,kBAAE,CAAC,QAAQ,CACtB,cAAI,CAAC,IAAI,CAAC,SAAS,EAAE,yBAAyB,CAAC,EAC/C,MAAM,CACP,CAAC;AACJ,CAAC;AALD,uBAKC"}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright 2021 Google LLC.
|
|
4
|
+
* Copyright (c) Microsoft Corporation.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
19
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.MapperServer = void 0;
|
|
23
|
+
const debug_1 = __importDefault(require("debug"));
|
|
24
|
+
const debugInternal = (0, debug_1.default)('bidiMapper:internal');
|
|
25
|
+
const debugLog = (0, debug_1.default)('bidiMapper:log');
|
|
26
|
+
const ws_1 = __importDefault(require("ws"));
|
|
27
|
+
const cdp_1 = require("./cdp");
|
|
28
|
+
class MapperServer {
|
|
29
|
+
_cdpConnection;
|
|
30
|
+
_mapperCdpClient;
|
|
31
|
+
_handlers = new Array();
|
|
32
|
+
static async create(cdpUrl, mapperContent) {
|
|
33
|
+
const cdpConnection = await this._establishCdpConnection(cdpUrl);
|
|
34
|
+
try {
|
|
35
|
+
const mapperCdpClient = await this._initMapper(cdpConnection, mapperContent);
|
|
36
|
+
return new MapperServer(cdpConnection, mapperCdpClient);
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
cdpConnection.close();
|
|
40
|
+
throw e;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
constructor(_cdpConnection, _mapperCdpClient) {
|
|
44
|
+
this._cdpConnection = _cdpConnection;
|
|
45
|
+
this._mapperCdpClient = _mapperCdpClient;
|
|
46
|
+
this._mapperCdpClient.Runtime.on('bindingCalled', this._onBindingCalled);
|
|
47
|
+
this._mapperCdpClient.Runtime.on('consoleAPICalled', this._onConsoleAPICalled);
|
|
48
|
+
}
|
|
49
|
+
setOnMessage(handler) {
|
|
50
|
+
this._handlers.push(handler);
|
|
51
|
+
}
|
|
52
|
+
sendMessage(messageJson) {
|
|
53
|
+
return this._sendBidiMessage(messageJson);
|
|
54
|
+
}
|
|
55
|
+
close() {
|
|
56
|
+
this._cdpConnection.close();
|
|
57
|
+
}
|
|
58
|
+
static async _establishCdpConnection(cdpUrl) {
|
|
59
|
+
return new Promise((resolve, reject) => {
|
|
60
|
+
debugInternal('Establishing session with cdpUrl: ', cdpUrl);
|
|
61
|
+
const ws = new ws_1.default(cdpUrl);
|
|
62
|
+
ws.once('error', reject);
|
|
63
|
+
ws.on('open', () => {
|
|
64
|
+
debugInternal('Session established.');
|
|
65
|
+
const transport = new cdp_1.WebSocketTransport(ws);
|
|
66
|
+
const connection = new cdp_1.CdpConnection(transport);
|
|
67
|
+
resolve(connection);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
async _sendBidiMessage(bidiMessageJson) {
|
|
72
|
+
await this._mapperCdpClient.Runtime.evaluate({
|
|
73
|
+
expression: 'onBidiMessage(' + JSON.stringify(bidiMessageJson) + ')',
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
_onBidiMessage(bidiMessage) {
|
|
77
|
+
for (let handler of this._handlers)
|
|
78
|
+
handler(bidiMessage);
|
|
79
|
+
}
|
|
80
|
+
_onBindingCalled = async (params) => {
|
|
81
|
+
if (params.name === 'sendBidiResponse') {
|
|
82
|
+
this._onBidiMessage(params.payload);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
_onConsoleAPICalled = async (params) => {
|
|
86
|
+
debugLog('consoleAPICalled %s %O', params.type, params.args.map((arg) => arg.value));
|
|
87
|
+
};
|
|
88
|
+
static async _initMapper(cdpConnection, mapperContent) {
|
|
89
|
+
debugInternal('Connection opened.');
|
|
90
|
+
// await browserClient.Log.enable();
|
|
91
|
+
const browserClient = cdpConnection.browserClient();
|
|
92
|
+
const { targetId } = await browserClient.Target.createTarget({
|
|
93
|
+
url: 'about:blank',
|
|
94
|
+
});
|
|
95
|
+
const { sessionId: mapperSessionId } = await browserClient.Target.attachToTarget({ targetId, flatten: true });
|
|
96
|
+
const mapperCdpClient = cdpConnection.getCdpClient(mapperSessionId);
|
|
97
|
+
if (!mapperCdpClient) {
|
|
98
|
+
throw new Error('Unable to connect to mapper CDP target');
|
|
99
|
+
}
|
|
100
|
+
await mapperCdpClient.Runtime.enable();
|
|
101
|
+
await browserClient.Target.exposeDevToolsProtocol({
|
|
102
|
+
bindingName: 'cdp',
|
|
103
|
+
targetId,
|
|
104
|
+
});
|
|
105
|
+
await mapperCdpClient.Runtime.addBinding({
|
|
106
|
+
name: 'sendBidiResponse',
|
|
107
|
+
});
|
|
108
|
+
const launchedPromise = new Promise((resolve, reject) => {
|
|
109
|
+
const onBindingCalled = ({ name, payload, }) => {
|
|
110
|
+
// Needed to check when Mapper is launched on the frontend.
|
|
111
|
+
if (name === 'sendBidiResponse') {
|
|
112
|
+
try {
|
|
113
|
+
const parsed = JSON.parse(payload);
|
|
114
|
+
if (parsed.launched) {
|
|
115
|
+
mapperCdpClient.Runtime.removeListener('bindingCalled', onBindingCalled);
|
|
116
|
+
resolve();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
catch (e) {
|
|
120
|
+
reject(new Error('Could not parse initial bidi response as JSON'));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
mapperCdpClient.Runtime.on('bindingCalled', onBindingCalled);
|
|
125
|
+
});
|
|
126
|
+
await mapperCdpClient.Runtime.evaluate({ expression: mapperContent });
|
|
127
|
+
// Let Mapper know what is it's TargetId to filter out related targets.
|
|
128
|
+
await mapperCdpClient.Runtime.evaluate({
|
|
129
|
+
expression: 'window.setSelfTargetId(' + JSON.stringify(targetId) + ')',
|
|
130
|
+
});
|
|
131
|
+
await launchedPromise;
|
|
132
|
+
debugInternal('Launched!');
|
|
133
|
+
return mapperCdpClient;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
exports.MapperServer = MapperServer;
|
|
137
|
+
//# sourceMappingURL=mapperServer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mapperServer.js","sourceRoot":"","sources":["../../mapperServer.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;;;;AAEH,kDAA0B;AAE1B,MAAM,aAAa,GAAG,IAAA,eAAK,EAAC,qBAAqB,CAAC,CAAC;AACnD,MAAM,QAAQ,GAAG,IAAA,eAAK,EAAC,gBAAgB,CAAC,CAAC;AAEzC,4CAA2B;AAG3B,+BAAqE;AAErE,MAAa,YAAY;IAqBb;IACA;IArBF,SAAS,GAAkC,IAAI,KAAK,EAAE,CAAC;IAE/D,MAAM,CAAC,KAAK,CAAC,MAAM,CACjB,MAAc,EACd,aAAqB;QAErB,MAAM,aAAa,GAAG,MAAM,IAAI,CAAC,uBAAuB,CAAC,MAAM,CAAC,CAAC;QACjE,IAAI;YACF,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,WAAW,CAC5C,aAAa,EACb,aAAa,CACd,CAAC;YACF,OAAO,IAA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uEAAuE;QACvE,MAAM,eAAe,CAAC,OAAO,CAAC,QAAQ,CAAC;YACrC,UAAU,EAAE,yBAAyB,GAAG,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,GAAG,GAAG;SACvE,CAAC,CAAC;QAEH,MAAM,eAAe,CAAC;QACtB,aAAa,CAAC,WAAW,CAAC,CAAC;QAC3B,OAAO,eAAe,CAAC;IACzB,CAAC;CACF;AA7JD,oCA6JC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright 2021 Google LLC.
|
|
4
|
+
* Copyright (c) Microsoft Corporation.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.StubTransport = void 0;
|
|
20
|
+
const sinon_1 = require("sinon");
|
|
21
|
+
function typedSpy() {
|
|
22
|
+
return (0, sinon_1.spy)();
|
|
23
|
+
}
|
|
24
|
+
class StubTransport {
|
|
25
|
+
setOnMessage;
|
|
26
|
+
sendMessage;
|
|
27
|
+
close;
|
|
28
|
+
_getOnMessage() {
|
|
29
|
+
sinon_1.assert.called(this.setOnMessage);
|
|
30
|
+
return this.setOnMessage.getCall(0).args[0];
|
|
31
|
+
}
|
|
32
|
+
async emulateIncomingMessage(messageObject) {
|
|
33
|
+
this._getOnMessage()(JSON.stringify(messageObject));
|
|
34
|
+
// `setTimeout` allows the message to be processed.
|
|
35
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
36
|
+
}
|
|
37
|
+
constructor() {
|
|
38
|
+
this.sendMessage = typedSpy();
|
|
39
|
+
this.setOnMessage = typedSpy();
|
|
40
|
+
this.close = typedSpy();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.StubTransport = StubTransport;
|
|
44
|
+
//# sourceMappingURL=stubTransport.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stubTransport.spec.js","sourceRoot":"","sources":["../../../tests/stubTransport.spec.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;AAEH,iCAA8C;AAQ9C,SAAS,QAAQ;IACf,OAAO,IAAA,WAAG,GAAO,CAAC;AACpB,CAAC;AAED,MAAa,aAAa;IACxB,YAAY,CAAuC;IACnD,WAAW,CAAsC;IACjD,KAAK,CAAgC;IAE7B,aAAa;QACnB,cAAM,CAAC,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;QACjC,OAAO,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC9C,CAAC;IAEM,KAAK,CAAC,sBAAsB,CAAC,aAAsB;QACxD,IAAI,CAAC,aAAa,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC,CAAC;QACpD,mDAAmD;QACnD,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC;IACzD,CAAC;IAED;QACE,IAAI,CAAC,WAAW,GAAG,QAAQ,EAAE,CAAC;QAC9B,IAAI,CAAC,YAAY,GAAG,QAAQ,EAAE,CAAC;QAC/B,IAAI,CAAC,KAAK,GAAG,QAAQ,EAAE,CAAC;IAC1B,CAAC;CACF;AArBD,sCAqBC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright 2021 Google LLC.
|
|
4
|
+
* Copyright (c) Microsoft Corporation.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.log = void 0;
|
|
20
|
+
function log(type) {
|
|
21
|
+
return (...messages) => {
|
|
22
|
+
// If run in browser, add debug message to the page.
|
|
23
|
+
if (globalThis.document?.documentElement) {
|
|
24
|
+
console.log(type, ...messages);
|
|
25
|
+
const typeLogContainer = findOrCreateTypeLogContainer(type);
|
|
26
|
+
const pre = document.createElement('pre');
|
|
27
|
+
pre.textContent = messages.join(', ');
|
|
28
|
+
typeLogContainer.appendChild(pre);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
exports.log = log;
|
|
33
|
+
function findOrCreateTypeLogContainer(type) {
|
|
34
|
+
const elementId = type + '_log';
|
|
35
|
+
const existingContainer = document.getElementById(elementId);
|
|
36
|
+
if (existingContainer)
|
|
37
|
+
return existingContainer;
|
|
38
|
+
const newContainer = document.createElement('div');
|
|
39
|
+
newContainer.id = elementId;
|
|
40
|
+
newContainer.innerHTML = `<h3>${type}:</h3>`;
|
|
41
|
+
document.body.appendChild(newContainer);
|
|
42
|
+
return newContainer;
|
|
43
|
+
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
|
4
|
+
* Copyright (c) Microsoft Corporation.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
//# sourceMappingURL=transport.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.js","sourceRoot":"","sources":["../../../utils/transport.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG"}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2021 Google LLC.
|
|
3
|
+
* Copyright (c) Microsoft Corporation.
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import argparse from 'argparse';
|
|
19
|
+
import puppeteer, { PuppeteerNode } from 'puppeteer';
|
|
20
|
+
|
|
21
|
+
import mapperReader from './mapperReader';
|
|
22
|
+
import { MapperServer } from './mapperServer';
|
|
23
|
+
import { BidiServerRunner } from './bidiServerRunner';
|
|
24
|
+
import { ITransport } from './utils/transport';
|
|
25
|
+
|
|
26
|
+
function parseArguments() {
|
|
27
|
+
var parser = new argparse.ArgumentParser({
|
|
28
|
+
add_help: true,
|
|
29
|
+
exit_on_error: false,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
parser.add_argument('-p', '--port', {
|
|
33
|
+
help: 'Port that BiDi server should listen to. Default is 8080.',
|
|
34
|
+
type: 'int',
|
|
35
|
+
default: process.env.PORT || 8080,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
parser.add_argument('-hl', '--headless', {
|
|
39
|
+
help: 'Sets if browser should run in headless or headful mode. Default is `--headless=true`.',
|
|
40
|
+
default: true,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// `parse_known_args` puts known args in the first element of the result.
|
|
44
|
+
const args = parser.parse_known_args();
|
|
45
|
+
return args[0];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
(async () => {
|
|
49
|
+
try {
|
|
50
|
+
console.log('Launching BiDi server.');
|
|
51
|
+
|
|
52
|
+
const args = parseArguments();
|
|
53
|
+
const bidiPort = args.port;
|
|
54
|
+
const headless = args.headless !== 'false';
|
|
55
|
+
|
|
56
|
+
BidiServerRunner.run(bidiPort, (bidiServer) => {
|
|
57
|
+
return _onNewBidiConnectionOpen(headless, bidiServer);
|
|
58
|
+
});
|
|
59
|
+
console.log('BiDi server launched.');
|
|
60
|
+
} catch (e) {
|
|
61
|
+
console.log('Error', e);
|
|
62
|
+
}
|
|
63
|
+
})();
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* On each new BiDi connection:
|
|
67
|
+
* 1. Launch Chromium (using Puppeteer for now).
|
|
68
|
+
* 2. Get `BiDi-CDP` mapper JS binaries using `mapperReader`.
|
|
69
|
+
* 3. Run `BiDi-CDP` mapper in launched browser.
|
|
70
|
+
* 4. Bind `BiDi-CDP` mapper to the `BiDi server`.
|
|
71
|
+
*
|
|
72
|
+
* @returns delegate to be called when the connection is closed
|
|
73
|
+
*/
|
|
74
|
+
async function _onNewBidiConnectionOpen(
|
|
75
|
+
headless: boolean,
|
|
76
|
+
bidiTransport: ITransport
|
|
77
|
+
): Promise<() => void> {
|
|
78
|
+
// 1. Launch Chromium (using Puppeteer for now).
|
|
79
|
+
// Puppeteer should have downloaded Chromium during the installation.
|
|
80
|
+
// Use Puppeteer's logic of launching browser as well.
|
|
81
|
+
const browser = await (puppeteer as any as PuppeteerNode).launch({
|
|
82
|
+
headless,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// 2. Get `BiDi-CDP` mapper JS binaries using `mapperReader`.
|
|
86
|
+
const bidiMapperScript = await mapperReader();
|
|
87
|
+
|
|
88
|
+
// 3. Run `BiDi-CDP` mapper in launched browser.
|
|
89
|
+
const mapperServer = await MapperServer.create(
|
|
90
|
+
browser.wsEndpoint(),
|
|
91
|
+
bidiMapperScript
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
// 4. Bind `BiDi-CDP` mapper to the `BiDi server`.
|
|
95
|
+
// Forward messages from BiDi Mapper to the client.
|
|
96
|
+
mapperServer.setOnMessage(async (message) => {
|
|
97
|
+
await bidiTransport.sendMessage(message);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// Forward messages from the client to BiDi Mapper.
|
|
101
|
+
bidiTransport.setOnMessage(async (message) => {
|
|
102
|
+
await mapperServer.sendMessage(message);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Return delegate to be called when the connection is closed.
|
|
106
|
+
return async () => {
|
|
107
|
+
// Client disconnected. Close browser.
|
|
108
|
+
await browser.close();
|
|
109
|
+
};
|
|
110
|
+
}
|