@lynx-js/devtool-connector 0.1.1 → 0.9.5
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/CHANGELOG.md +13 -0
- package/LICENSE +1 -1
- package/README.md +152 -0
- package/dist/187.js +272 -0
- package/dist/390.js +102 -0
- package/dist/43.js +20 -0
- package/dist/556.js +253 -0
- package/dist/63.js +93 -0
- package/dist/677.js +19 -0
- package/dist/712.js +180 -0
- package/dist/872.js +771 -0
- package/dist/962.js +12150 -0
- package/dist/client-id.d.ts +7 -0
- package/dist/daemon/device-connection.d.ts +36 -0
- package/dist/daemon/entry.d.ts +1 -0
- package/dist/daemon/entry.js +46 -0
- package/dist/daemon/index.d.ts +1 -0
- package/dist/daemon/index.js +1 -0
- package/dist/daemon/manager.d.ts +14 -0
- package/dist/daemon/protocol.d.ts +73 -0
- package/dist/daemon/server.d.ts +14 -0
- package/dist/daemon/static-server.d.ts +5 -0
- package/dist/daemon/tarball-cache.d.ts +16 -0
- package/dist/daemon/version.d.ts +1 -0
- package/dist/index.d.ts +47 -19
- package/dist/index.js +188 -349
- package/dist/rslib-runtime.js +0 -20
- package/dist/streams/cdp.d.ts +2 -4
- package/dist/streams/customized.d.ts +5 -4
- package/dist/streams/index.d.ts +3 -0
- package/dist/streams/index.js +1 -0
- package/dist/streams/utils.d.ts +11 -0
- package/dist/transport/android.d.ts +4 -3
- package/dist/transport/base.d.ts +9 -0
- package/dist/transport/daemon.d.ts +20 -0
- package/dist/transport/desktop.d.ts +4 -3
- package/dist/transport/index.d.ts +5 -0
- package/dist/transport/index.js +3 -11253
- package/dist/transport/ios.d.ts +3 -4
- package/dist/transport/transport.d.ts +7 -5
- package/dist/transport/usbmux.d.ts +49 -0
- package/dist/transport/ws-stream.d.ts +28 -0
- package/dist/types.d.ts +73 -19
- package/package.json +33 -15
- package/public/inspector-wrapper.html +539 -0
- package/dist/844.js +0 -632
- /package/dist/{streams → transport}/peertalk.d.ts +0 -0
package/dist/556.js
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { randomInt } from "node:crypto";
|
|
2
|
+
import { TransformStream, WritableStream } from "node:stream/web";
|
|
3
|
+
import { DaemonManager } from "./390.js";
|
|
4
|
+
import { node_createDebug } from "./187.js";
|
|
5
|
+
const debug = node_createDebug('devtool-mcp-server:daemon:transport');
|
|
6
|
+
class DaemonTransport {
|
|
7
|
+
#port;
|
|
8
|
+
constructor(port = 21783){
|
|
9
|
+
this.#port = port;
|
|
10
|
+
}
|
|
11
|
+
async close() {}
|
|
12
|
+
async listDevices() {
|
|
13
|
+
const result = await this.#controlRequest('listDevices');
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
async listAvailableApps(deviceId) {
|
|
17
|
+
const result = await this.#controlRequest('listAvailableApps', {
|
|
18
|
+
deviceId
|
|
19
|
+
});
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
async openApp(deviceId, packageName, options) {
|
|
23
|
+
await this.#controlRequest('openApp', {
|
|
24
|
+
deviceId,
|
|
25
|
+
packageName,
|
|
26
|
+
withDataCleared: options?.withDataCleared
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
async listClients() {
|
|
30
|
+
const entries = await this.#controlRequest('listClients');
|
|
31
|
+
debug('received ClientList with %d entries', entries.length);
|
|
32
|
+
return entries.map(({ id, info })=>({
|
|
33
|
+
id,
|
|
34
|
+
info
|
|
35
|
+
}));
|
|
36
|
+
}
|
|
37
|
+
async connect(options) {
|
|
38
|
+
const { deviceId, port, signal } = options;
|
|
39
|
+
debug('connect to %s:%d via daemon', deviceId, port);
|
|
40
|
+
await DaemonManager.ensureRunning(this.#port);
|
|
41
|
+
const conn = await this.#createWebSocketConnection(signal);
|
|
42
|
+
try {
|
|
43
|
+
await this.#controlRequestOnConn(conn, 'subscribe', {
|
|
44
|
+
deviceId,
|
|
45
|
+
port
|
|
46
|
+
});
|
|
47
|
+
} catch (error) {
|
|
48
|
+
await conn[Symbol.asyncDispose]();
|
|
49
|
+
throw new Error(`Failed to subscribe to ${deviceId}:${port}`, {
|
|
50
|
+
cause: error
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
const writable = new WritableStream({
|
|
54
|
+
async write (chunk) {
|
|
55
|
+
const message = routeDaemonMessage(chunk, conn.assignedId, port);
|
|
56
|
+
await writeMessage(conn.writable, stringifyMessage(message), signal);
|
|
57
|
+
},
|
|
58
|
+
async abort () {
|
|
59
|
+
await conn[Symbol.asyncDispose]();
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
const outputReadable = conn.readable.pipeThrough(new JSONStringToObjectStream());
|
|
63
|
+
return {
|
|
64
|
+
readable: outputReadable,
|
|
65
|
+
writable,
|
|
66
|
+
async [Symbol.asyncDispose] () {
|
|
67
|
+
await conn[Symbol.asyncDispose]();
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
async #controlRequest(method, params) {
|
|
72
|
+
await DaemonManager.ensureRunning(this.#port);
|
|
73
|
+
const conn = await this.#createWebSocketConnection();
|
|
74
|
+
try {
|
|
75
|
+
return await this.#controlRequestOnConn(conn, method, params);
|
|
76
|
+
} finally{
|
|
77
|
+
await conn[Symbol.asyncDispose]();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
async #controlRequestOnConn(conn, method, params) {
|
|
81
|
+
const id = randomInt(10000, 50000);
|
|
82
|
+
const signal = AbortSignal.timeout(10000);
|
|
83
|
+
await this.#writeMessage(conn.writable, JSON.stringify({
|
|
84
|
+
event: 'Control',
|
|
85
|
+
data: {
|
|
86
|
+
id,
|
|
87
|
+
method,
|
|
88
|
+
params
|
|
89
|
+
}
|
|
90
|
+
}), signal);
|
|
91
|
+
for await (const value of this.#readMessages(conn.readable, signal)){
|
|
92
|
+
const msg = value;
|
|
93
|
+
if ('ControlResponse' === msg.event) {
|
|
94
|
+
const resp = msg.data;
|
|
95
|
+
if (resp.id === id) {
|
|
96
|
+
if (resp.error) throw new Error(resp.error);
|
|
97
|
+
return resp.result;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
throw new Error(`No response for control request: ${method}`);
|
|
102
|
+
}
|
|
103
|
+
async #createWebSocketConnection(signal) {
|
|
104
|
+
const url = await DaemonManager.ensureRunning(this.#port);
|
|
105
|
+
signal?.throwIfAborted();
|
|
106
|
+
const { wsStreams } = await import("./63.js");
|
|
107
|
+
const wss = wsStreams.create(url);
|
|
108
|
+
const abortHandler = ()=>{
|
|
109
|
+
wss.close();
|
|
110
|
+
};
|
|
111
|
+
signal?.addEventListener('abort', abortHandler, {
|
|
112
|
+
once: true
|
|
113
|
+
});
|
|
114
|
+
wss.closed.catch(()=>{
|
|
115
|
+
debug('WebSocket to daemon closed');
|
|
116
|
+
});
|
|
117
|
+
try {
|
|
118
|
+
const { readable, writable } = await this.#withAbortSignal(wss.opened, signal);
|
|
119
|
+
const reader = readable.getReader();
|
|
120
|
+
const { value, done } = await reader.read();
|
|
121
|
+
reader.releaseLock();
|
|
122
|
+
if (done) throw new Error('WebSocket closed before initialization.');
|
|
123
|
+
const initMsg = JSON.parse(value);
|
|
124
|
+
if ('Initialize' !== initMsg.event) throw new Error(`Expected Initialize, got ${initMsg.event}`);
|
|
125
|
+
const assignedId = initMsg.data;
|
|
126
|
+
await this.#writeMessage(writable, JSON.stringify({
|
|
127
|
+
event: 'Register',
|
|
128
|
+
data: {
|
|
129
|
+
id: assignedId,
|
|
130
|
+
type: 'Driver'
|
|
131
|
+
}
|
|
132
|
+
}), signal);
|
|
133
|
+
return {
|
|
134
|
+
assignedId,
|
|
135
|
+
readable,
|
|
136
|
+
writable,
|
|
137
|
+
async [Symbol.asyncDispose] () {
|
|
138
|
+
signal?.removeEventListener('abort', abortHandler);
|
|
139
|
+
wss.close();
|
|
140
|
+
await wss.closed.catch(()=>{});
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
} catch (err) {
|
|
144
|
+
signal?.removeEventListener('abort', abortHandler);
|
|
145
|
+
try {
|
|
146
|
+
wss.close();
|
|
147
|
+
} catch {}
|
|
148
|
+
try {
|
|
149
|
+
await wss.closed;
|
|
150
|
+
} catch {}
|
|
151
|
+
throw err;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
async *#readMessages(readable, signal) {
|
|
155
|
+
const reader = readable.getReader();
|
|
156
|
+
const abortHandler = ()=>{
|
|
157
|
+
reader.cancel(signal.reason);
|
|
158
|
+
};
|
|
159
|
+
signal.addEventListener('abort', abortHandler, {
|
|
160
|
+
once: true
|
|
161
|
+
});
|
|
162
|
+
try {
|
|
163
|
+
while(!signal.aborted){
|
|
164
|
+
const { value, done } = await reader.read();
|
|
165
|
+
if (done) break;
|
|
166
|
+
try {
|
|
167
|
+
yield JSON.parse(value);
|
|
168
|
+
} catch {}
|
|
169
|
+
}
|
|
170
|
+
} finally{
|
|
171
|
+
signal.removeEventListener('abort', abortHandler);
|
|
172
|
+
reader.releaseLock();
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
async #writeMessage(writable, chunk, signal) {
|
|
176
|
+
await writeMessage(writable, chunk, signal);
|
|
177
|
+
}
|
|
178
|
+
async #withAbortSignal(promise, signal) {
|
|
179
|
+
if (!signal) return promise;
|
|
180
|
+
signal.throwIfAborted();
|
|
181
|
+
return new Promise((resolve, reject)=>{
|
|
182
|
+
const abortHandler = ()=>{
|
|
183
|
+
reject(signal.reason);
|
|
184
|
+
};
|
|
185
|
+
signal.addEventListener('abort', abortHandler, {
|
|
186
|
+
once: true
|
|
187
|
+
});
|
|
188
|
+
promise.then((value)=>{
|
|
189
|
+
signal.removeEventListener('abort', abortHandler);
|
|
190
|
+
resolve(value);
|
|
191
|
+
}, (error)=>{
|
|
192
|
+
signal.removeEventListener('abort', abortHandler);
|
|
193
|
+
reject(error);
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function routeDaemonMessage(chunk, sender, port) {
|
|
199
|
+
if (isRecord(chunk) && 'Customized' === chunk['event']) {
|
|
200
|
+
const data = isRecord(chunk['data']) ? chunk['data'] : {};
|
|
201
|
+
return {
|
|
202
|
+
...chunk,
|
|
203
|
+
data: {
|
|
204
|
+
...data,
|
|
205
|
+
sender
|
|
206
|
+
},
|
|
207
|
+
to: port
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
return chunk;
|
|
211
|
+
}
|
|
212
|
+
function stringifyMessage(message) {
|
|
213
|
+
try {
|
|
214
|
+
return JSON.stringify(message);
|
|
215
|
+
} catch (err) {
|
|
216
|
+
throw new Error(`Failed to stringify object: ${err instanceof Error ? err.message : String(err)}`, {
|
|
217
|
+
cause: err
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
function isRecord(value) {
|
|
222
|
+
return 'object' == typeof value && null !== value;
|
|
223
|
+
}
|
|
224
|
+
async function writeMessage(writable, chunk, signal) {
|
|
225
|
+
signal?.throwIfAborted();
|
|
226
|
+
const writer = writable.getWriter();
|
|
227
|
+
const abortHandler = ()=>{
|
|
228
|
+
writer.abort(signal?.reason);
|
|
229
|
+
};
|
|
230
|
+
signal?.addEventListener('abort', abortHandler, {
|
|
231
|
+
once: true
|
|
232
|
+
});
|
|
233
|
+
try {
|
|
234
|
+
await writer.write(chunk);
|
|
235
|
+
} finally{
|
|
236
|
+
signal?.removeEventListener('abort', abortHandler);
|
|
237
|
+
writer.releaseLock();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
class JSONStringToObjectStream extends TransformStream {
|
|
241
|
+
constructor(){
|
|
242
|
+
super({
|
|
243
|
+
transform (chunk, controller) {
|
|
244
|
+
try {
|
|
245
|
+
controller.enqueue(JSON.parse(chunk));
|
|
246
|
+
} catch (err) {
|
|
247
|
+
controller.error(new Error(`Failed to parse JSON: ${err instanceof Error ? err.message : String(err)}`));
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
export { DaemonTransport };
|
package/dist/63.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { ReadableStream, WritableStream } from "node:stream/web";
|
|
2
|
+
import { WebSocket } from "ws";
|
|
3
|
+
class WsWebSocketStream {
|
|
4
|
+
#ws;
|
|
5
|
+
opened;
|
|
6
|
+
closed;
|
|
7
|
+
#resolveClosed;
|
|
8
|
+
#rejectClosed;
|
|
9
|
+
constructor(url){
|
|
10
|
+
this.#ws = new WebSocket(url);
|
|
11
|
+
this.closed = new Promise((resolve, reject)=>{
|
|
12
|
+
this.#resolveClosed = resolve;
|
|
13
|
+
this.#rejectClosed = reject;
|
|
14
|
+
});
|
|
15
|
+
this.opened = new Promise((resolve, reject)=>{
|
|
16
|
+
const ws = this.#ws;
|
|
17
|
+
const onError = (err)=>{
|
|
18
|
+
cleanup();
|
|
19
|
+
reject(err);
|
|
20
|
+
this.#rejectClosed(err);
|
|
21
|
+
};
|
|
22
|
+
const onClose = ()=>{
|
|
23
|
+
cleanup();
|
|
24
|
+
reject(new Error('WebSocket closed before opening.'));
|
|
25
|
+
this.#resolveClosed();
|
|
26
|
+
};
|
|
27
|
+
const cleanup = ()=>{
|
|
28
|
+
ws.removeListener('error', onError);
|
|
29
|
+
ws.removeListener('close', onClose);
|
|
30
|
+
};
|
|
31
|
+
ws.once('open', ()=>{
|
|
32
|
+
cleanup();
|
|
33
|
+
const readable = new ReadableStream({
|
|
34
|
+
start (controller) {
|
|
35
|
+
ws.on('message', (data)=>{
|
|
36
|
+
controller.enqueue('string' == typeof data ? data : data.toString());
|
|
37
|
+
});
|
|
38
|
+
ws.on('close', ()=>{
|
|
39
|
+
try {
|
|
40
|
+
controller.close();
|
|
41
|
+
} catch {}
|
|
42
|
+
});
|
|
43
|
+
ws.on('error', (err)=>{
|
|
44
|
+
try {
|
|
45
|
+
controller.error(err);
|
|
46
|
+
} catch {}
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
cancel () {
|
|
50
|
+
ws.close();
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
const writable = new WritableStream({
|
|
54
|
+
write (chunk) {
|
|
55
|
+
return new Promise((res, rej)=>{
|
|
56
|
+
ws.send(chunk, (err)=>{
|
|
57
|
+
if (err) rej(err);
|
|
58
|
+
else res();
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
close () {
|
|
63
|
+
ws.close();
|
|
64
|
+
},
|
|
65
|
+
abort () {
|
|
66
|
+
ws.close();
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
resolve({
|
|
70
|
+
readable,
|
|
71
|
+
writable
|
|
72
|
+
});
|
|
73
|
+
ws.on('close', ()=>{
|
|
74
|
+
this.#resolveClosed();
|
|
75
|
+
});
|
|
76
|
+
ws.on('error', (err)=>{
|
|
77
|
+
this.#rejectClosed(err);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
ws.once('error', onError);
|
|
81
|
+
ws.once('close', onClose);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
close() {
|
|
85
|
+
this.#ws.close();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const wsStreams = {
|
|
89
|
+
create (url) {
|
|
90
|
+
return new WsWebSocketStream(url);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
export { WsWebSocketStream, wsStreams };
|
package/dist/677.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
function isInitializeResponse(response) {
|
|
2
|
+
return 'Register' === response.event;
|
|
3
|
+
}
|
|
4
|
+
function isHeadlessPrepareResponse(response) {
|
|
5
|
+
return 'Customized' === response.event && 'HeadlessPrepare' === response.data.type;
|
|
6
|
+
}
|
|
7
|
+
function isListSessionResponse(response) {
|
|
8
|
+
return 'Customized' === response.event && 'SessionList' === response.data.type;
|
|
9
|
+
}
|
|
10
|
+
function isGetGlobalSwitchResponse(response) {
|
|
11
|
+
return 'Customized' === response.event && 'GetGlobalSwitch' === response.data.type;
|
|
12
|
+
}
|
|
13
|
+
function isSetGlobalSwitchResponse(response) {
|
|
14
|
+
return 'Customized' === response.event && 'SetGlobalSwitch' === response.data.type;
|
|
15
|
+
}
|
|
16
|
+
function isCustomizedResponseWithType(response, type) {
|
|
17
|
+
return 'Customized' === response.event && response.data.type === type;
|
|
18
|
+
}
|
|
19
|
+
export { isCustomizedResponseWithType, isGetGlobalSwitchResponse, isHeadlessPrepareResponse, isInitializeResponse, isListSessionResponse, isSetGlobalSwitchResponse };
|
package/dist/712.js
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { randomInt } from "node:crypto";
|
|
2
|
+
import { TransformStream } from "node:stream/web";
|
|
3
|
+
import { isListSessionResponse, isCustomizedResponseWithType } from "./677.js";
|
|
4
|
+
class CustomizedClientIdTransformStream extends TransformStream {
|
|
5
|
+
constructor(clientId){
|
|
6
|
+
super({
|
|
7
|
+
transform (chunk, controller) {
|
|
8
|
+
if ('Customized' === chunk.event) controller.enqueue({
|
|
9
|
+
...chunk,
|
|
10
|
+
data: {
|
|
11
|
+
...chunk?.data,
|
|
12
|
+
data: {
|
|
13
|
+
...chunk?.data?.data,
|
|
14
|
+
client_id: clientId
|
|
15
|
+
},
|
|
16
|
+
sender: clientId
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
else controller.enqueue(chunk);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
class CustomizedRequestTransformStream extends TransformStream {
|
|
25
|
+
constructor(options){
|
|
26
|
+
const { type, sessionId = -1, messageBuilder } = options;
|
|
27
|
+
super({
|
|
28
|
+
transform (chunk, controller) {
|
|
29
|
+
const sid = 'function' == typeof sessionId ? sessionId(chunk) : sessionId;
|
|
30
|
+
controller.enqueue({
|
|
31
|
+
event: 'Customized',
|
|
32
|
+
data: {
|
|
33
|
+
type,
|
|
34
|
+
data: {
|
|
35
|
+
session_id: sid,
|
|
36
|
+
message: messageBuilder(chunk)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
class CustomizedResponseTransformStream extends TransformStream {
|
|
45
|
+
constructor(type, id){
|
|
46
|
+
super({
|
|
47
|
+
transform (response, controller) {
|
|
48
|
+
if (!isCustomizedResponseWithType(response, type)) return;
|
|
49
|
+
try {
|
|
50
|
+
const message = JSON.parse(response.data.data.message);
|
|
51
|
+
if (void 0 === id || message?.id === id) controller.enqueue(message);
|
|
52
|
+
} catch (err) {
|
|
53
|
+
controller.error(new Error(`Failed to parse response for type ${type}`, {
|
|
54
|
+
cause: err
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
class ResponseParserTransformStream extends TransformStream {
|
|
62
|
+
constructor(options){
|
|
63
|
+
const { parseResult, checkError } = options;
|
|
64
|
+
super({
|
|
65
|
+
transform (chunk, controller) {
|
|
66
|
+
const error = checkError(chunk);
|
|
67
|
+
if (error) return void controller.error(error);
|
|
68
|
+
try {
|
|
69
|
+
controller.enqueue(parseResult(chunk));
|
|
70
|
+
} catch (err) {
|
|
71
|
+
controller.error(err);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
class AppResponseTransformStream extends ResponseParserTransformStream {
|
|
78
|
+
constructor(method){
|
|
79
|
+
super({
|
|
80
|
+
checkError: (message)=>{
|
|
81
|
+
try {
|
|
82
|
+
const result = JSON.parse(message.result);
|
|
83
|
+
if (0 !== result.code && '0' !== result.code) return new Error(`App request ${method} error: ${result.message}`, {
|
|
84
|
+
cause: message
|
|
85
|
+
});
|
|
86
|
+
return null;
|
|
87
|
+
} catch (err) {
|
|
88
|
+
return new Error('Failed to parse App response message', {
|
|
89
|
+
cause: err
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
parseResult: (message)=>JSON.parse(message.result)
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
class GlobalSwitchRequestTransformStream extends CustomizedRequestTransformStream {
|
|
98
|
+
constructor(type){
|
|
99
|
+
super({
|
|
100
|
+
type,
|
|
101
|
+
sessionId: -1,
|
|
102
|
+
messageBuilder: ({ key, value })=>({
|
|
103
|
+
global_key: key,
|
|
104
|
+
global_value: value
|
|
105
|
+
})
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
class CDPRequestTransformStream extends CustomizedRequestTransformStream {
|
|
110
|
+
constructor(sessionId, fixedId){
|
|
111
|
+
super({
|
|
112
|
+
type: 'CDP',
|
|
113
|
+
sessionId,
|
|
114
|
+
messageBuilder: (chunk)=>{
|
|
115
|
+
const id = fixedId ?? randomInt(10000, 50000);
|
|
116
|
+
return {
|
|
117
|
+
id,
|
|
118
|
+
...chunk
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
class CDPResponseTransformStream extends CustomizedResponseTransformStream {
|
|
125
|
+
constructor(id){
|
|
126
|
+
super('CDP', id);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
class CDPOutputTransformStream extends ResponseParserTransformStream {
|
|
130
|
+
constructor(){
|
|
131
|
+
super({
|
|
132
|
+
checkError: (message)=>{
|
|
133
|
+
if ('error' in message) return new Error(`CDP request error: ${message.error.message}`, {
|
|
134
|
+
cause: message
|
|
135
|
+
});
|
|
136
|
+
return null;
|
|
137
|
+
},
|
|
138
|
+
parseResult: (message)=>{
|
|
139
|
+
if ('result' in message) return message.result;
|
|
140
|
+
throw new Error('No result in CDP response message', {
|
|
141
|
+
cause: message
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
class FilterTransformStream extends TransformStream {
|
|
148
|
+
constructor(filter){
|
|
149
|
+
super({
|
|
150
|
+
transform (chunk, controller) {
|
|
151
|
+
if (filter(chunk)) controller.enqueue(chunk);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
class InspectStream extends TransformStream {
|
|
157
|
+
constructor(callback){
|
|
158
|
+
super({
|
|
159
|
+
transform (chunk, controller) {
|
|
160
|
+
callback(chunk);
|
|
161
|
+
controller.enqueue(chunk);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
class SessionGuardTransformStream extends TransformStream {
|
|
167
|
+
constructor(sessionId){
|
|
168
|
+
super({
|
|
169
|
+
transform (chunk, controller) {
|
|
170
|
+
if (isListSessionResponse(chunk)) {
|
|
171
|
+
const sessions = chunk.data.data;
|
|
172
|
+
if (!Array.isArray(sessions)) return void controller.enqueue(chunk);
|
|
173
|
+
if (!sessions.some((s)=>s?.session_id === sessionId)) return void controller.terminate();
|
|
174
|
+
}
|
|
175
|
+
controller.enqueue(chunk);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
export { AppResponseTransformStream, CDPOutputTransformStream, CDPRequestTransformStream, CDPResponseTransformStream, CustomizedClientIdTransformStream, CustomizedRequestTransformStream, CustomizedResponseTransformStream, FilterTransformStream, GlobalSwitchRequestTransformStream, InspectStream, ResponseParserTransformStream, SessionGuardTransformStream };
|