dsh-embedded-browser-next 0.0.0-stage → 1.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/lib/ws.js ADDED
@@ -0,0 +1,253 @@
1
+ /**
2
+ * Minimal RFC 6455 server-side WebSocket implementation.
3
+ *
4
+ * The plugin needs exactly one upgrade route carrying small JSON control frames
5
+ * in and JPEG screencast frames out, so it ships its own ~200-line server
6
+ * instead of a dependency: fewer moving parts for a package that is linked into
7
+ * a running DSH host and published to npm.
8
+ *
9
+ * @module dsh-embedded-browser/ws
10
+ */
11
+
12
+ import { createHash } from 'node:crypto'
13
+
14
+ /** RFC 6455 handshake magic string. */
15
+ const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
16
+
17
+ /** Opcodes this server understands. */
18
+ const OPCODE = {
19
+ continuation: 0x0,
20
+ text: 0x1,
21
+ binary: 0x2,
22
+ close: 0x8,
23
+ ping: 0x9,
24
+ pong: 0xa,
25
+ }
26
+
27
+ /** Compute the `Sec-WebSocket-Accept` value for a client key. */
28
+ function acceptKey(key) {
29
+ return createHash('sha1').update(key + GUID).digest('base64')
30
+ }
31
+
32
+ /** Encode one server->client frame (never masked). */
33
+ export function encodeFrame(opcode, payload) {
34
+ const body = Buffer.isBuffer(payload) ? payload : Buffer.from(String(payload), 'utf8')
35
+ const length = body.length
36
+ let header
37
+ if (length < 126) {
38
+ header = Buffer.alloc(2)
39
+ header[1] = length
40
+ } else if (length < 65_536) {
41
+ header = Buffer.alloc(4)
42
+ header[1] = 126
43
+ header.writeUInt16BE(length, 2)
44
+ } else {
45
+ header = Buffer.alloc(10)
46
+ header[1] = 127
47
+ header.writeBigUInt64BE(BigInt(length), 2)
48
+ }
49
+ header[0] = 0x80 | opcode
50
+ return Buffer.concat([header, body])
51
+ }
52
+
53
+ /**
54
+ * One accepted WebSocket connection.
55
+ *
56
+ * Messages surface as `{ type: 'text' | 'binary', data: string | Buffer }`.
57
+ */
58
+ export class WebSocketConnection {
59
+ constructor(socket) {
60
+ this.socket = socket
61
+ this.handlers = new Map()
62
+ this.buffer = Buffer.alloc(0)
63
+ this.fragments = []
64
+ this.fragmentOpcode = undefined
65
+ this.closed = false
66
+ this.socket.on('data', (chunk) => this.consume(chunk))
67
+ this.socket.on('close', () => this.finish())
68
+ this.socket.on('error', () => this.finish())
69
+ }
70
+
71
+ /** Subscribe to `message`, `close`, or `error`. */
72
+ on(event, handler) {
73
+ let set = this.handlers.get(event)
74
+ if (set === undefined) {
75
+ set = new Set()
76
+ this.handlers.set(event, set)
77
+ }
78
+ set.add(handler)
79
+ return () => set.delete(handler)
80
+ }
81
+
82
+ emit(event, payload) {
83
+ const set = this.handlers.get(event)
84
+ if (set === undefined) return
85
+ for (const handler of [...set]) {
86
+ try {
87
+ handler(payload)
88
+ } catch {
89
+ /* a listener must not break the socket */
90
+ }
91
+ }
92
+ }
93
+
94
+ /** Send a JSON control message. */
95
+ sendJson(value) {
96
+ return this.sendText(JSON.stringify(value))
97
+ }
98
+
99
+ sendText(text) {
100
+ return this.write(encodeFrame(OPCODE.text, text))
101
+ }
102
+
103
+ /** Send one binary frame (screencast image). */
104
+ sendBinary(buffer) {
105
+ return this.write(encodeFrame(OPCODE.binary, buffer))
106
+ }
107
+
108
+ write(frame) {
109
+ if (this.closed) return false
110
+ try {
111
+ this.socket.write(frame)
112
+ return true
113
+ } catch {
114
+ this.finish()
115
+ return false
116
+ }
117
+ }
118
+
119
+ /** Bytes queued but not yet flushed — used to drop frames under back-pressure. */
120
+ get bufferedAmount() {
121
+ return this.socket.writableLength ?? 0
122
+ }
123
+
124
+ close(code = 1000, reason = '') {
125
+ if (this.closed) return
126
+ const body = Buffer.alloc(2 + Buffer.byteLength(reason))
127
+ body.writeUInt16BE(code, 0)
128
+ body.write(reason, 2)
129
+ this.write(encodeFrame(OPCODE.close, body))
130
+ this.closed = true
131
+ this.socket.end()
132
+ }
133
+
134
+ finish() {
135
+ if (this.closed && this.handlers.size === 0) return
136
+ this.closed = true
137
+ this.emit('close')
138
+ this.socket.destroy()
139
+ }
140
+
141
+ consume(chunk) {
142
+ this.buffer = this.buffer.length === 0 ? chunk : Buffer.concat([this.buffer, chunk])
143
+ for (;;) {
144
+ const frame = this.readFrame()
145
+ if (frame === undefined) return
146
+ if (frame.opcode === OPCODE.close) {
147
+ this.closed = true
148
+ this.write(encodeFrame(OPCODE.close, Buffer.alloc(0)))
149
+ this.socket.end()
150
+ return
151
+ }
152
+ if (frame.opcode === OPCODE.ping) {
153
+ this.write(encodeFrame(OPCODE.pong, frame.payload))
154
+ continue
155
+ }
156
+ if (frame.opcode === OPCODE.pong) continue
157
+ if (frame.opcode === OPCODE.continuation) {
158
+ this.fragments.push(frame.payload)
159
+ if (!frame.fin) continue
160
+ const payload = Buffer.concat(this.fragments)
161
+ const opcode = this.fragmentOpcode
162
+ this.fragments = []
163
+ this.fragmentOpcode = undefined
164
+ this.deliver(opcode, payload)
165
+ continue
166
+ }
167
+ if (!frame.fin) {
168
+ this.fragmentOpcode = frame.opcode
169
+ this.fragments = [frame.payload]
170
+ continue
171
+ }
172
+ this.deliver(frame.opcode, frame.payload)
173
+ }
174
+ }
175
+
176
+ deliver(opcode, payload) {
177
+ if (opcode === OPCODE.text) this.emit('message', { type: 'text', data: payload.toString('utf8') })
178
+ else if (opcode === OPCODE.binary) this.emit('message', { type: 'binary', data: payload })
179
+ }
180
+
181
+ /** Parse one frame, returning undefined when the buffer holds only a fragment of it. */
182
+ readFrame() {
183
+ const buffer = this.buffer
184
+ if (buffer.length < 2) return undefined
185
+ const fin = (buffer[0] & 0x80) !== 0
186
+ const opcode = buffer[0] & 0x0f
187
+ const masked = (buffer[1] & 0x80) !== 0
188
+ let length = buffer[1] & 0x7f
189
+ let offset = 2
190
+ if (length === 126) {
191
+ if (buffer.length < offset + 2) return undefined
192
+ length = buffer.readUInt16BE(offset)
193
+ offset += 2
194
+ } else if (length === 127) {
195
+ if (buffer.length < offset + 8) return undefined
196
+ const big = buffer.readBigUInt64BE(offset)
197
+ if (big > BigInt(64 * 1024 * 1024)) {
198
+ this.close(1009, 'frame too large')
199
+ return undefined
200
+ }
201
+ length = Number(big)
202
+ offset += 8
203
+ }
204
+ let mask
205
+ if (masked) {
206
+ if (buffer.length < offset + 4) return undefined
207
+ mask = buffer.subarray(offset, offset + 4)
208
+ offset += 4
209
+ } else {
210
+ // RFC 6455 §5.1: a client MUST mask every frame. Tolerating an unmasked one
211
+ // means a malformed or hostile client can drive the session unverified.
212
+ this.close(1002, 'client frames must be masked')
213
+ return undefined
214
+ }
215
+ if (buffer.length < offset + length) return undefined
216
+ const payload = Buffer.from(buffer.subarray(offset, offset + length))
217
+ if (mask !== undefined) {
218
+ for (let index = 0; index < payload.length; index += 1) payload[index] ^= mask[index % 4]
219
+ }
220
+ this.buffer = buffer.subarray(offset + length)
221
+ return { fin, opcode, payload }
222
+ }
223
+ }
224
+
225
+ /**
226
+ * Complete the upgrade handshake and hand the connection to `onConnection`.
227
+ *
228
+ * @param req - the HTTP upgrade request.
229
+ * @param socket - the raw socket (ownership transfers to the connection).
230
+ * @param head - bytes already read past the request headers.
231
+ * @param onConnection - receives the live {@link WebSocketConnection}.
232
+ * @returns whether the upgrade was accepted.
233
+ */
234
+ export function acceptUpgrade(req, socket, head, onConnection) {
235
+ const key = req.headers['sec-websocket-key']
236
+ if (typeof key !== 'string' || key === '') {
237
+ socket.write('HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n')
238
+ socket.destroy()
239
+ return false
240
+ }
241
+ const headers = [
242
+ 'HTTP/1.1 101 Switching Protocols',
243
+ 'Upgrade: websocket',
244
+ 'Connection: Upgrade',
245
+ `Sec-WebSocket-Accept: ${acceptKey(key)}`,
246
+ '\r\n',
247
+ ]
248
+ socket.write(headers.join('\r\n'))
249
+ const connection = new WebSocketConnection(socket)
250
+ if (head !== undefined && head.length > 0) connection.consume(head)
251
+ onConnection(connection)
252
+ return true
253
+ }
package/package.json CHANGED
@@ -1,6 +1,62 @@
1
1
  {
2
2
  "name": "dsh-embedded-browser-next",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "A browser living inside the DSH host: one tab per session, 26 browser_embedded_* tools, and on-demand Sidebar / Float / Real Window human takeover — logins, 2FA, QR codes, and CAPTCHAs included.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "lib/index.js",
8
+ "exports": {
9
+ ".": "./lib/index.js",
10
+ "./client": "./lib/client.js",
11
+ "./package.json": "./package.json"
12
+ },
13
+ "files": [
14
+ "lib",
15
+ "cordis.patch.yml",
16
+ "README.md",
17
+ "README.en.md"
18
+ ],
19
+ "engines": {
20
+ "node": ">=22.19"
21
+ },
22
+ "peerDependencies": {
23
+ "@deepseek-ai/dsh-tools": "*",
24
+ "@deepseek-ai/schemastery": "*"
25
+ },
26
+ "dsh": {
27
+ "engines": {
28
+ "dsh": ">=0.1.5-rc.2"
29
+ },
30
+ "bundle": {
31
+ "patch": "./cordis.patch.yml"
32
+ },
33
+ "client": {
34
+ "platform": "web"
35
+ }
36
+ },
37
+ "scripts": {
38
+ "test": "node --test test/adaptive.test.mjs test/click-text.test.mjs test/click.test.mjs test/client-input.test.mjs test/client-load.test.mjs test/diagnostics.test.mjs test/dialog-guard.test.mjs test/downloads.test.mjs test/external.test.mjs test/frame.test.mjs test/guest-scripts.test.mjs test/handover.test.mjs test/human.test.mjs test/idle-guard.test.mjs test/launch-tab.test.mjs test/layer.test.mjs test/lazy.test.mjs test/live-url.test.mjs test/mjpeg.test.mjs test/native-view.test.mjs test/panel-messages.test.mjs test/path-guard.test.mjs test/present.test.mjs test/recover.test.mjs test/residency.test.mjs test/restore.test.mjs test/router.test.mjs test/routes.test.mjs test/same-tab.test.mjs test/snapshot-window.test.mjs test/stream-health.test.mjs test/target.test.mjs test/tools.surface.test.mjs test/version.test.mjs test/viewport-fit.test.mjs test/window.test.mjs test/ws.test.mjs"
39
+ },
40
+ "keywords": [
41
+ "deepseek-harness",
42
+ "dsh",
43
+ "dsh-plugin",
44
+ "browser",
45
+ "embedded-browser",
46
+ "headless-browser",
47
+ "lazy-tools",
48
+ "cdp",
49
+ "chrome-devtools-protocol",
50
+ "agent-tools",
51
+ "human-in-the-loop"
52
+ ],
53
+ "author": "LXYCWDMGS <280239685+LXYCWDMGS@users.noreply.github.com>",
54
+ "repository": {
55
+ "type": "git",
56
+ "url": "git+https://github.com/LXYCWDMGS/dsh-embedded-browser-next.git"
57
+ },
58
+ "homepage": "https://github.com/LXYCWDMGS/dsh-embedded-browser-next#readme",
59
+ "bugs": {
60
+ "url": "https://github.com/LXYCWDMGS/dsh-embedded-browser-next/issues"
61
+ }
62
+ }