@bewa/cdp-cli 0.1.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/AGENT.md +97 -0
- package/LICENSE +190 -0
- package/README.md +259 -0
- package/bin/cdp-cli.js +66 -0
- package/lib/browser.js +96 -0
- package/lib/cdp.js +116 -0
- package/lib/commands/eval.js +56 -0
- package/lib/commands/init.js +73 -0
- package/lib/commands/launch.js +44 -0
- package/lib/commands/navigate.js +22 -0
- package/lib/commands/reload.js +32 -0
- package/lib/commands/targets.js +20 -0
- package/lib/config.js +94 -0
- package/lib/output.js +38 -0
- package/lib/ws.js +166 -0
- package/package.json +34 -0
package/lib/ws.js
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { createConnection } = require('node:net');
|
|
4
|
+
const { randomBytes } = require('node:crypto');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Minimal WebSocket client for CDP. No dependencies.
|
|
8
|
+
* Only supports text frames (sufficient for CDP JSON protocol).
|
|
9
|
+
*/
|
|
10
|
+
class MinimalWebSocket {
|
|
11
|
+
constructor(url) {
|
|
12
|
+
this._url = new URL(url);
|
|
13
|
+
this._socket = null;
|
|
14
|
+
this._onMessage = null;
|
|
15
|
+
this._buffer = Buffer.alloc(0);
|
|
16
|
+
this._connected = false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
connect() {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
const port = this._url.port || 80;
|
|
22
|
+
const host = this._url.hostname;
|
|
23
|
+
const path = this._url.pathname + this._url.search;
|
|
24
|
+
|
|
25
|
+
this._socket = createConnection({ host, port }, () => {
|
|
26
|
+
const key = randomBytes(16).toString('base64');
|
|
27
|
+
const req = [
|
|
28
|
+
`GET ${path} HTTP/1.1`,
|
|
29
|
+
`Host: ${host}:${port}`,
|
|
30
|
+
`Upgrade: websocket`,
|
|
31
|
+
`Connection: Upgrade`,
|
|
32
|
+
`Sec-WebSocket-Key: ${key}`,
|
|
33
|
+
`Sec-WebSocket-Version: 13`,
|
|
34
|
+
``,
|
|
35
|
+
``,
|
|
36
|
+
].join('\r\n');
|
|
37
|
+
this._socket.write(req);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
this._socket.on('error', reject);
|
|
41
|
+
|
|
42
|
+
let handshakeDone = false;
|
|
43
|
+
this._socket.on('data', chunk => {
|
|
44
|
+
this._buffer = Buffer.concat([this._buffer, chunk]);
|
|
45
|
+
|
|
46
|
+
if (!handshakeDone) {
|
|
47
|
+
const idx = this._buffer.indexOf('\r\n\r\n');
|
|
48
|
+
if (idx === -1) return;
|
|
49
|
+
const header = this._buffer.subarray(0, idx).toString();
|
|
50
|
+
if (!header.includes('101')) {
|
|
51
|
+
reject(new Error('WebSocket handshake failed: ' + header.split('\r\n')[0]));
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
this._buffer = this._buffer.subarray(idx + 4);
|
|
55
|
+
handshakeDone = true;
|
|
56
|
+
this._connected = true;
|
|
57
|
+
resolve();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
this._processFrames();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
this._socket.on('close', () => { this._connected = false; });
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
_processFrames() {
|
|
68
|
+
while (this._buffer.length >= 2) {
|
|
69
|
+
const secondByte = this._buffer[1];
|
|
70
|
+
const masked = (secondByte & 0x80) !== 0;
|
|
71
|
+
let payloadLen = secondByte & 0x7f;
|
|
72
|
+
let offset = 2;
|
|
73
|
+
|
|
74
|
+
if (payloadLen === 126) {
|
|
75
|
+
if (this._buffer.length < 4) return;
|
|
76
|
+
payloadLen = this._buffer.readUInt16BE(2);
|
|
77
|
+
offset = 4;
|
|
78
|
+
} else if (payloadLen === 127) {
|
|
79
|
+
if (this._buffer.length < 10) return;
|
|
80
|
+
payloadLen = Number(this._buffer.readBigUInt64BE(2));
|
|
81
|
+
offset = 10;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (masked) offset += 4; // skip mask key (server shouldn't mask, but handle it)
|
|
85
|
+
if (this._buffer.length < offset + payloadLen) return;
|
|
86
|
+
|
|
87
|
+
const opcode = this._buffer[0] & 0x0f;
|
|
88
|
+
let payload = this._buffer.subarray(offset, offset + payloadLen);
|
|
89
|
+
|
|
90
|
+
if (masked) {
|
|
91
|
+
const maskKey = this._buffer.subarray(offset - 4, offset);
|
|
92
|
+
payload = Buffer.from(payload);
|
|
93
|
+
for (let i = 0; i < payload.length; i++) {
|
|
94
|
+
payload[i] ^= maskKey[i % 4];
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
this._buffer = this._buffer.subarray(offset + payloadLen);
|
|
99
|
+
|
|
100
|
+
if (opcode === 0x01 && this._onMessage) {
|
|
101
|
+
// Text frame
|
|
102
|
+
this._onMessage(payload.toString('utf-8'));
|
|
103
|
+
} else if (opcode === 0x08) {
|
|
104
|
+
// Close frame
|
|
105
|
+
this.close();
|
|
106
|
+
return;
|
|
107
|
+
} else if (opcode === 0x09) {
|
|
108
|
+
// Ping — respond with pong
|
|
109
|
+
this._sendFrame(0x0a, payload);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
send(data) {
|
|
115
|
+
const payload = Buffer.from(data, 'utf-8');
|
|
116
|
+
this._sendFrame(0x01, payload, true);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
_sendFrame(opcode, payload, mask = false) {
|
|
120
|
+
if (!this._socket || !this._connected) return;
|
|
121
|
+
|
|
122
|
+
const len = payload.length;
|
|
123
|
+
let header;
|
|
124
|
+
|
|
125
|
+
if (len < 126) {
|
|
126
|
+
header = Buffer.alloc(2);
|
|
127
|
+
header[0] = 0x80 | opcode; // FIN + opcode
|
|
128
|
+
header[1] = (mask ? 0x80 : 0) | len;
|
|
129
|
+
} else if (len < 65536) {
|
|
130
|
+
header = Buffer.alloc(4);
|
|
131
|
+
header[0] = 0x80 | opcode;
|
|
132
|
+
header[1] = (mask ? 0x80 : 0) | 126;
|
|
133
|
+
header.writeUInt16BE(len, 2);
|
|
134
|
+
} else {
|
|
135
|
+
header = Buffer.alloc(10);
|
|
136
|
+
header[0] = 0x80 | opcode;
|
|
137
|
+
header[1] = (mask ? 0x80 : 0) | 127;
|
|
138
|
+
header.writeBigUInt64BE(BigInt(len), 2);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (mask) {
|
|
142
|
+
const maskKey = randomBytes(4);
|
|
143
|
+
const masked = Buffer.from(payload);
|
|
144
|
+
for (let i = 0; i < masked.length; i++) {
|
|
145
|
+
masked[i] ^= maskKey[i % 4];
|
|
146
|
+
}
|
|
147
|
+
this._socket.write(Buffer.concat([header, maskKey, masked]));
|
|
148
|
+
} else {
|
|
149
|
+
this._socket.write(Buffer.concat([header, payload]));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
onMessage(fn) {
|
|
154
|
+
this._onMessage = fn;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
close() {
|
|
158
|
+
if (this._socket) {
|
|
159
|
+
try { this._sendFrame(0x08, Buffer.alloc(0), true); } catch {}
|
|
160
|
+
this._socket.destroy();
|
|
161
|
+
this._connected = false;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
module.exports = { MinimalWebSocket };
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bewa/cdp-cli",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Browser DevTools bridge for AI coding assistants",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"bin": {
|
|
7
|
+
"cdp-cli": "./bin/cdp-cli.js"
|
|
8
|
+
},
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=18"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"bin/",
|
|
14
|
+
"lib/",
|
|
15
|
+
"AGENT.md"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"test": "node --test test/*.test.js",
|
|
19
|
+
"test:watch": "node --test --watch test/*.test.js",
|
|
20
|
+
"prepublishOnly": "npm test"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"chrome",
|
|
24
|
+
"devtools",
|
|
25
|
+
"cdp",
|
|
26
|
+
"debugging",
|
|
27
|
+
"ai",
|
|
28
|
+
"cli"
|
|
29
|
+
],
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "https://github.com/bewa/cdp-cli.git"
|
|
33
|
+
}
|
|
34
|
+
}
|