cdp-tunnel 2.5.14 → 2.5.16
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.
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
var LocalHandler = (function() {
|
|
2
2
|
function browserGetVersion() {
|
|
3
3
|
var userAgent = navigator.userAgent || '';
|
|
4
|
-
var
|
|
4
|
+
var match = userAgent.match(/Chrome\/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/);
|
|
5
|
+
var product = match ? 'Chrome/' + match[1] : 'Chrome';
|
|
5
6
|
return {
|
|
6
7
|
protocolVersion: '1.3',
|
|
7
|
-
product:
|
|
8
|
+
product: product,
|
|
8
9
|
revision: '',
|
|
9
10
|
userAgent: userAgent,
|
|
10
11
|
jsVersion: ''
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cdp-tunnel",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.16",
|
|
4
4
|
"description": "Bridge Chrome's debugger API to WebSocket — control your existing browser with Playwright/Puppeteer via CDP",
|
|
5
5
|
"main": "server/proxy-server.js",
|
|
6
6
|
"bin": "./cli/index.js",
|
|
@@ -15,7 +15,8 @@
|
|
|
15
15
|
"benchmark:native": "node benchmark/run-all.js --native",
|
|
16
16
|
"benchmark:proxy": "node benchmark/run-all.js --proxy",
|
|
17
17
|
"demo": "node demo/server.js",
|
|
18
|
-
"test:compare": "node tests/cdp-compare/cdp-compare-runner.js"
|
|
18
|
+
"test:compare": "node tests/cdp-compare/cdp-compare-runner.js",
|
|
19
|
+
"prepare": "husky"
|
|
19
20
|
},
|
|
20
21
|
"keywords": [
|
|
21
22
|
"cdp",
|
|
@@ -52,10 +53,12 @@
|
|
|
52
53
|
"LICENSE"
|
|
53
54
|
],
|
|
54
55
|
"dependencies": {
|
|
56
|
+
"cdp-tunnel": "^2.5.15",
|
|
55
57
|
"commander": "^12.0.0",
|
|
56
58
|
"ws": "^8.16.0"
|
|
57
59
|
},
|
|
58
60
|
"devDependencies": {
|
|
61
|
+
"husky": "^9.1.7",
|
|
59
62
|
"lz-string": "^1.5.0",
|
|
60
63
|
"pako": "^2.1.0",
|
|
61
64
|
"playwright": "^1.58.2",
|
package/server/proxy-server.js
CHANGED
|
@@ -159,6 +159,7 @@ const { version: PKG_VERSION } = require('../package.json');
|
|
|
159
159
|
|
|
160
160
|
let cachedTargets = [];
|
|
161
161
|
let lastTargetsUpdate = 0;
|
|
162
|
+
let cachedBrowserVersion = null;
|
|
162
163
|
|
|
163
164
|
console.log('='.repeat(60));
|
|
164
165
|
console.log(` WebSocket CDP Proxy Server v${PKG_VERSION}`);
|
|
@@ -187,6 +188,39 @@ function buildTargetWebSocketUrl(req, targetId) {
|
|
|
187
188
|
return `ws://${getHost(req)}/devtools/page/${targetId}`;
|
|
188
189
|
}
|
|
189
190
|
|
|
191
|
+
function invalidateTargetsCache() {
|
|
192
|
+
lastTargetsUpdate = 0;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async function requestVersionFromPlugin() {
|
|
196
|
+
if (cachedBrowserVersion) return cachedBrowserVersion;
|
|
197
|
+
|
|
198
|
+
const plugin = pluginConnections.values().next().value;
|
|
199
|
+
if (!plugin || plugin.readyState !== WebSocket.OPEN) {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return new Promise((resolve) => {
|
|
204
|
+
const requestId = `version_${Date.now()}`;
|
|
205
|
+
const timeout = setTimeout(() => resolve(null), 2000);
|
|
206
|
+
|
|
207
|
+
const handler = (data) => {
|
|
208
|
+
try {
|
|
209
|
+
const msg = JSON.parse(data.toString());
|
|
210
|
+
if (msg.id === requestId && msg.result) {
|
|
211
|
+
clearTimeout(timeout);
|
|
212
|
+
plugin.off('message', handler);
|
|
213
|
+
cachedBrowserVersion = msg.result;
|
|
214
|
+
resolve(cachedBrowserVersion);
|
|
215
|
+
}
|
|
216
|
+
} catch (e) {}
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
plugin.on('message', handler);
|
|
220
|
+
plugin.send(JSON.stringify({ id: requestId, method: 'Browser.getVersion' }));
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
190
224
|
async function requestTargetsFromPlugin() {
|
|
191
225
|
const now = Date.now();
|
|
192
226
|
if (now - lastTargetsUpdate < CONFIG.TARGETS_CACHE_TTL && cachedTargets.length > 0) {
|
|
@@ -229,11 +263,14 @@ async function handleHttpRequest(req, res) {
|
|
|
229
263
|
const url = new URL(req.url, `http://localhost:${PORT}`);
|
|
230
264
|
|
|
231
265
|
if (url.pathname === '/json/version' || url.pathname === '/json/version/') {
|
|
266
|
+
const ver = await requestVersionFromPlugin();
|
|
267
|
+
const userAgent = ver?.userAgent || 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.86 Safari/537.36';
|
|
268
|
+
const product = ver?.product || 'Chrome/131.0.6778.86';
|
|
232
269
|
const payload = {
|
|
233
|
-
Browser:
|
|
234
|
-
'Protocol-Version': '1.3',
|
|
235
|
-
'User-Agent':
|
|
236
|
-
'V8-Version': '
|
|
270
|
+
Browser: product,
|
|
271
|
+
'Protocol-Version': ver?.protocolVersion || '1.3',
|
|
272
|
+
'User-Agent': userAgent,
|
|
273
|
+
'V8-Version': ver?.jsVersion || '',
|
|
237
274
|
'WebKit-Version': '537.36',
|
|
238
275
|
webSocketDebuggerUrl: buildWebSocketDebuggerUrl(req)
|
|
239
276
|
};
|
|
@@ -745,6 +782,7 @@ function handlePluginConnection(ws, clientInfo) {
|
|
|
745
782
|
clientWs.send(msgStr);
|
|
746
783
|
console.log(`[ATTACHED EVENT] Sent cached event to client: ${mapping.clientId}`);
|
|
747
784
|
}
|
|
785
|
+
invalidateTargetsCache();
|
|
748
786
|
}
|
|
749
787
|
// 过滤 Target.getTargets 响应,只返回该客户端拥有的 target
|
|
750
788
|
if (mapping.isGetTargets && parsed.result && parsed.result.targetInfos) {
|
|
@@ -762,6 +800,7 @@ function handlePluginConnection(ws, clientInfo) {
|
|
|
762
800
|
targetIdToClientId.delete(mapping.closeTargetId);
|
|
763
801
|
console.log(`[CLOSE TARGET CLEANUP] removed targetId=${mapping.closeTargetId?.substring(0,8)} from mapping`);
|
|
764
802
|
}
|
|
803
|
+
invalidateTargetsCache();
|
|
765
804
|
}
|
|
766
805
|
|
|
767
806
|
// 然后发送响应给客户端
|