@qontinui/ui-bridge-wrapper 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/README.md +52 -0
- package/dist/base-transport.d.ts +54 -0
- package/dist/base-transport.d.ts.map +1 -0
- package/dist/create-transport.d.ts +12 -0
- package/dist/create-transport.d.ts.map +1 -0
- package/dist/handler-registry.d.ts +42 -0
- package/dist/handler-registry.d.ts.map +1 -0
- package/dist/helpers/auth.d.ts +18 -0
- package/dist/helpers/auth.d.ts.map +1 -0
- package/dist/helpers/retry.d.ts +28 -0
- package/dist/helpers/retry.d.ts.map +1 -0
- package/dist/helpers/schema.d.ts +35 -0
- package/dist/helpers/schema.d.ts.map +1 -0
- package/dist/index.cjs +655 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +641 -0
- package/dist/index.js.map +1 -0
- package/dist/manifest-only.d.ts +100 -0
- package/dist/manifest-only.d.ts.map +1 -0
- package/dist/react/WrapperAppShell.d.ts +25 -0
- package/dist/react/WrapperAppShell.d.ts.map +1 -0
- package/dist/react/index.cjs +63 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.ts +13 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +59 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/useTransportSelector.d.ts +30 -0
- package/dist/react/useTransportSelector.d.ts.map +1 -0
- package/dist/react/useWrapperStatus.d.ts +11 -0
- package/dist/react/useWrapperStatus.d.ts.map +1 -0
- package/dist/transports/api.d.ts +39 -0
- package/dist/transports/api.d.ts.map +1 -0
- package/dist/transports/headed.d.ts +15 -0
- package/dist/transports/headed.d.ts.map +1 -0
- package/dist/transports/headless.d.ts +69 -0
- package/dist/transports/headless.d.ts.map +1 -0
- package/dist/transports/live.d.ts +97 -0
- package/dist/transports/live.d.ts.map +1 -0
- package/dist/types.d.ts +102 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +70 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,655 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/types.ts
|
|
4
|
+
var WrapperTransportError = class extends Error {
|
|
5
|
+
code;
|
|
6
|
+
retryable;
|
|
7
|
+
cause;
|
|
8
|
+
constructor(code, message, options = {}) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "WrapperTransportError";
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.retryable = options.retryable ?? false;
|
|
13
|
+
if (options.cause !== void 0) {
|
|
14
|
+
this.cause = options.cause;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// src/handler-registry.ts
|
|
20
|
+
var HandlerRegistry = class {
|
|
21
|
+
handlers = /* @__PURE__ */ new Map();
|
|
22
|
+
/**
|
|
23
|
+
* Register (or replace) a handler for `actionId`. Replacement is explicit
|
|
24
|
+
* rather than throwing because live reloading / hot module scenarios need
|
|
25
|
+
* to update handlers without tearing the whole transport down.
|
|
26
|
+
*/
|
|
27
|
+
register(actionId, fn) {
|
|
28
|
+
this.handlers.set(actionId, fn);
|
|
29
|
+
}
|
|
30
|
+
/** Remove a handler. Returns `true` if a handler was removed. */
|
|
31
|
+
unregister(actionId) {
|
|
32
|
+
return this.handlers.delete(actionId);
|
|
33
|
+
}
|
|
34
|
+
/** All registered action ids. Stable iteration order (insertion order). */
|
|
35
|
+
list() {
|
|
36
|
+
return Array.from(this.handlers.keys());
|
|
37
|
+
}
|
|
38
|
+
/** Check whether a handler is registered for `actionId`. */
|
|
39
|
+
has(actionId) {
|
|
40
|
+
return this.handlers.has(actionId);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Invoke the handler registered under `actionId`.
|
|
44
|
+
*
|
|
45
|
+
* Throws `WrapperTransportError("NO_HANDLER")` if no handler is registered.
|
|
46
|
+
* Propagates handler errors wrapped as `WrapperTransportError("HANDLER_ERROR")`
|
|
47
|
+
* so callers can uniformly check `err.code`.
|
|
48
|
+
*/
|
|
49
|
+
async dispatch(actionId, params, ctx) {
|
|
50
|
+
const handler = this.handlers.get(actionId);
|
|
51
|
+
if (!handler) {
|
|
52
|
+
throw new WrapperTransportError(
|
|
53
|
+
"NO_HANDLER",
|
|
54
|
+
`No handler registered for action '${actionId}'`
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
const result = await handler(params, ctx);
|
|
59
|
+
return result;
|
|
60
|
+
} catch (err) {
|
|
61
|
+
if (err instanceof WrapperTransportError) throw err;
|
|
62
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
63
|
+
throw new WrapperTransportError("HANDLER_ERROR", message, { cause: err });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// src/base-transport.ts
|
|
69
|
+
var BaseTransport = class {
|
|
70
|
+
_status = "idle";
|
|
71
|
+
listeners = /* @__PURE__ */ new Set();
|
|
72
|
+
registry;
|
|
73
|
+
readyPromise = null;
|
|
74
|
+
constructor(registry) {
|
|
75
|
+
this.registry = registry ?? new HandlerRegistry();
|
|
76
|
+
}
|
|
77
|
+
get status() {
|
|
78
|
+
return this._status;
|
|
79
|
+
}
|
|
80
|
+
/** Expose the underlying registry so wrappers can register handlers. */
|
|
81
|
+
get handlerRegistry() {
|
|
82
|
+
return this.registry;
|
|
83
|
+
}
|
|
84
|
+
/** Convenience: register a handler on the transport's registry. */
|
|
85
|
+
register(actionId, fn) {
|
|
86
|
+
this.registry.register(actionId, fn);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Idempotent async setup. First call runs `onReady()`; subsequent calls
|
|
90
|
+
* share the same promise until it settles, after which further calls
|
|
91
|
+
* are no-ops if `status === 'ready'`, or re-run setup if it errored.
|
|
92
|
+
*/
|
|
93
|
+
async ready() {
|
|
94
|
+
if (this._status === "ready") return;
|
|
95
|
+
if (this._status === "closed") {
|
|
96
|
+
throw new WrapperTransportError("TRANSPORT_CLOSED", `Transport '${this.kind}' is closed`);
|
|
97
|
+
}
|
|
98
|
+
if (this.readyPromise) return this.readyPromise;
|
|
99
|
+
this.setStatus("connecting");
|
|
100
|
+
this.readyPromise = this.onReady().then(() => {
|
|
101
|
+
this.setStatus("ready");
|
|
102
|
+
}).catch((err) => {
|
|
103
|
+
this.setStatus("error");
|
|
104
|
+
this.readyPromise = null;
|
|
105
|
+
throw err;
|
|
106
|
+
});
|
|
107
|
+
return this.readyPromise;
|
|
108
|
+
}
|
|
109
|
+
/** Default dispatch — waits for ready, builds ctx, delegates to registry. */
|
|
110
|
+
async dispatch(actionId, params) {
|
|
111
|
+
await this.ready();
|
|
112
|
+
const ctx = await this.buildContext(actionId, params);
|
|
113
|
+
return this.registry.dispatch(actionId, params, ctx);
|
|
114
|
+
}
|
|
115
|
+
async close() {
|
|
116
|
+
if (this._status === "closed") return;
|
|
117
|
+
try {
|
|
118
|
+
await this.onClose();
|
|
119
|
+
} finally {
|
|
120
|
+
this.setStatus("closed");
|
|
121
|
+
this.listeners.clear();
|
|
122
|
+
this.readyPromise = null;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
onStatusChange(listener) {
|
|
126
|
+
this.listeners.add(listener);
|
|
127
|
+
return () => {
|
|
128
|
+
this.listeners.delete(listener);
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/** Update status and notify listeners. Safe to call from subclasses. */
|
|
132
|
+
setStatus(next) {
|
|
133
|
+
if (this._status === next) return;
|
|
134
|
+
this._status = next;
|
|
135
|
+
for (const l of this.listeners) {
|
|
136
|
+
try {
|
|
137
|
+
l(next);
|
|
138
|
+
} catch {
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Subclass hook: build the context object passed to handlers. Default is
|
|
144
|
+
* `undefined` (api transport). Headless/headed override to provide the
|
|
145
|
+
* Playwright `page`; live overrides to provide a response helper.
|
|
146
|
+
*/
|
|
147
|
+
async buildContext(_actionId, _params) {
|
|
148
|
+
return void 0;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
// src/transports/api.ts
|
|
153
|
+
var ApiTransport = class extends BaseTransport {
|
|
154
|
+
kind = "api";
|
|
155
|
+
fallback;
|
|
156
|
+
constructor(config = { kind: "api" }, registry) {
|
|
157
|
+
super(registry);
|
|
158
|
+
const handler = config.options?.handler;
|
|
159
|
+
if (typeof handler === "function") {
|
|
160
|
+
this.fallback = handler;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
async onReady() {
|
|
164
|
+
}
|
|
165
|
+
async onClose() {
|
|
166
|
+
}
|
|
167
|
+
async buildContext() {
|
|
168
|
+
return { kind: "api" };
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Override dispatch to fall back to `options.handler` when no explicit
|
|
172
|
+
* registration exists for `actionId`.
|
|
173
|
+
*/
|
|
174
|
+
async dispatch(actionId, params) {
|
|
175
|
+
await this.ready();
|
|
176
|
+
if (this.registry.has(actionId)) {
|
|
177
|
+
const ctx = await this.buildContext();
|
|
178
|
+
return this.registry.dispatch(actionId, params, ctx);
|
|
179
|
+
}
|
|
180
|
+
if (this.fallback) {
|
|
181
|
+
try {
|
|
182
|
+
return await this.fallback(actionId, params);
|
|
183
|
+
} catch (err) {
|
|
184
|
+
if (err instanceof WrapperTransportError) throw err;
|
|
185
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
186
|
+
throw new WrapperTransportError("HANDLER_ERROR", message, { cause: err });
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
throw new WrapperTransportError("NO_HANDLER", `No handler registered for action '${actionId}'`);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
// src/transports/headless.ts
|
|
194
|
+
var HeadlessTransport = class extends BaseTransport {
|
|
195
|
+
kind;
|
|
196
|
+
options;
|
|
197
|
+
headless;
|
|
198
|
+
tab = null;
|
|
199
|
+
constructor(config, registry, overrides = {}) {
|
|
200
|
+
super(registry);
|
|
201
|
+
this.kind = overrides.kind ?? "headless";
|
|
202
|
+
this.headless = overrides.headless ?? true;
|
|
203
|
+
this.options = this.parseOptions(config);
|
|
204
|
+
}
|
|
205
|
+
parseOptions(config) {
|
|
206
|
+
const raw = config.options ?? {};
|
|
207
|
+
const targetUrl = raw["targetUrl"];
|
|
208
|
+
if (typeof targetUrl !== "string" || targetUrl.length === 0) {
|
|
209
|
+
throw new WrapperTransportError(
|
|
210
|
+
"INVALID_CONFIG",
|
|
211
|
+
`Transport kind '${this.kind}' requires options.targetUrl (string)`
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
return {
|
|
215
|
+
targetUrl,
|
|
216
|
+
viewportWidth: typeof raw["viewportWidth"] === "number" ? raw["viewportWidth"] : void 0,
|
|
217
|
+
viewportHeight: typeof raw["viewportHeight"] === "number" ? raw["viewportHeight"] : void 0,
|
|
218
|
+
uiBridgeBase: typeof raw["uiBridgeBase"] === "string" ? raw["uiBridgeBase"] : void 0,
|
|
219
|
+
waitForUiBridgeMs: typeof raw["waitForUiBridgeMs"] === "number" ? raw["waitForUiBridgeMs"] : void 0,
|
|
220
|
+
userAgent: typeof raw["userAgent"] === "string" ? raw["userAgent"] : void 0,
|
|
221
|
+
forwardConsole: typeof raw["forwardConsole"] === "boolean" ? raw["forwardConsole"] : void 0
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
async onReady() {
|
|
225
|
+
const { launchHeadlessTab } = await import('@qontinui/ui-bridge-headless');
|
|
226
|
+
const tab = await launchHeadlessTab({
|
|
227
|
+
url: this.options.targetUrl,
|
|
228
|
+
headless: this.headless,
|
|
229
|
+
uiBridgeBase: this.options.uiBridgeBase,
|
|
230
|
+
waitForUiBridgeMs: this.options.waitForUiBridgeMs,
|
|
231
|
+
viewportWidth: this.options.viewportWidth,
|
|
232
|
+
viewportHeight: this.options.viewportHeight,
|
|
233
|
+
userAgent: this.options.userAgent,
|
|
234
|
+
forwardConsole: this.options.forwardConsole
|
|
235
|
+
});
|
|
236
|
+
this.tab = {
|
|
237
|
+
browser: tab.browser,
|
|
238
|
+
context: tab.context,
|
|
239
|
+
page: tab.page,
|
|
240
|
+
uiBridgeRegistered: tab.uiBridgeRegistered,
|
|
241
|
+
tabId: tab.tabId,
|
|
242
|
+
close: tab.close
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
async onClose() {
|
|
246
|
+
if (!this.tab) return;
|
|
247
|
+
const t = this.tab;
|
|
248
|
+
this.tab = null;
|
|
249
|
+
await t.close();
|
|
250
|
+
}
|
|
251
|
+
async buildContext() {
|
|
252
|
+
if (!this.tab) {
|
|
253
|
+
throw new WrapperTransportError(
|
|
254
|
+
"NOT_READY",
|
|
255
|
+
`Transport '${this.kind}' is not ready; call ready() before dispatch`
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
return {
|
|
259
|
+
kind: this.kind,
|
|
260
|
+
page: this.tab.page,
|
|
261
|
+
browserContext: this.tab.context,
|
|
262
|
+
browser: this.tab.browser,
|
|
263
|
+
uiBridgeRegistered: this.tab.uiBridgeRegistered,
|
|
264
|
+
tabId: this.tab.tabId
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
// src/transports/headed.ts
|
|
270
|
+
var HeadedTransport = class extends HeadlessTransport {
|
|
271
|
+
constructor(config, registry) {
|
|
272
|
+
super(config, registry, { kind: "headed", headless: false });
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
// src/transports/live.ts
|
|
277
|
+
var LiveSessionTransport = class extends BaseTransport {
|
|
278
|
+
kind = "live";
|
|
279
|
+
url;
|
|
280
|
+
appId;
|
|
281
|
+
appName;
|
|
282
|
+
opts;
|
|
283
|
+
wsCtor;
|
|
284
|
+
ws = null;
|
|
285
|
+
ackResolvers = null;
|
|
286
|
+
constructor(config, registry, injected) {
|
|
287
|
+
super(registry);
|
|
288
|
+
if (!config.runnerUrl) {
|
|
289
|
+
throw new WrapperTransportError("INVALID_CONFIG", `Transport kind 'live' requires runnerUrl`);
|
|
290
|
+
}
|
|
291
|
+
this.url = config.runnerUrl;
|
|
292
|
+
this.appId = config.appId;
|
|
293
|
+
this.appName = config.appName;
|
|
294
|
+
this.opts = config.options ?? {};
|
|
295
|
+
const fromInjection = injected?.webSocketCtor;
|
|
296
|
+
const fromGlobal = typeof globalThis.WebSocket === "function" ? globalThis.WebSocket : void 0;
|
|
297
|
+
const resolved = fromInjection ?? fromGlobal;
|
|
298
|
+
if (!resolved) {
|
|
299
|
+
throw new WrapperTransportError(
|
|
300
|
+
"NO_WEBSOCKET",
|
|
301
|
+
`No WebSocket constructor available. Provide one via the second constructor arg or run in an environment with global WebSocket.`
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
this.wsCtor = resolved;
|
|
305
|
+
}
|
|
306
|
+
async onReady() {
|
|
307
|
+
const ws = new this.wsCtor(this.url);
|
|
308
|
+
this.ws = ws;
|
|
309
|
+
ws.addEventListener("message", (ev) => this.handleMessage(ev.data));
|
|
310
|
+
ws.addEventListener("close", () => this.handleClose());
|
|
311
|
+
ws.addEventListener("error", (ev) => this.handleError(ev));
|
|
312
|
+
await new Promise((resolve, reject) => {
|
|
313
|
+
const onOpen = () => {
|
|
314
|
+
try {
|
|
315
|
+
const frame = {
|
|
316
|
+
type: "register",
|
|
317
|
+
transport: "websocket",
|
|
318
|
+
appId: this.appId,
|
|
319
|
+
appName: this.appName,
|
|
320
|
+
appType: this.opts.appType ?? "wrapper"
|
|
321
|
+
};
|
|
322
|
+
if (this.opts.framework !== void 0) frame.framework = this.opts.framework;
|
|
323
|
+
if (this.opts.version !== void 0) frame.version = this.opts.version;
|
|
324
|
+
if (this.opts.capabilities !== void 0) frame.capabilities = this.opts.capabilities;
|
|
325
|
+
if (this.opts.origin !== void 0) frame.origin = this.opts.origin;
|
|
326
|
+
if (this.opts.pageUrl !== void 0) frame.pageUrl = this.opts.pageUrl;
|
|
327
|
+
ws.send(JSON.stringify(frame));
|
|
328
|
+
} catch (err) {
|
|
329
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
const timeoutMs = this.opts.handshakeTimeoutMs ?? 1e4;
|
|
333
|
+
const timer = setTimeout(() => {
|
|
334
|
+
this.ackResolvers = null;
|
|
335
|
+
reject(
|
|
336
|
+
new WrapperTransportError(
|
|
337
|
+
"HANDSHAKE_TIMEOUT",
|
|
338
|
+
`Did not receive register ack within ${timeoutMs}ms`
|
|
339
|
+
)
|
|
340
|
+
);
|
|
341
|
+
}, timeoutMs);
|
|
342
|
+
this.ackResolvers = { resolve, reject, timer };
|
|
343
|
+
};
|
|
344
|
+
ws.addEventListener("open", onOpen);
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
async onClose() {
|
|
348
|
+
const ws = this.ws;
|
|
349
|
+
this.ws = null;
|
|
350
|
+
if (this.ackResolvers) {
|
|
351
|
+
clearTimeout(this.ackResolvers.timer);
|
|
352
|
+
this.ackResolvers.reject(
|
|
353
|
+
new WrapperTransportError("TRANSPORT_CLOSED", "Transport closed during handshake")
|
|
354
|
+
);
|
|
355
|
+
this.ackResolvers = null;
|
|
356
|
+
}
|
|
357
|
+
if (ws) {
|
|
358
|
+
try {
|
|
359
|
+
ws.close();
|
|
360
|
+
} catch {
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
async buildContext() {
|
|
365
|
+
return { kind: "live", commandId: null };
|
|
366
|
+
}
|
|
367
|
+
handleMessage(data) {
|
|
368
|
+
let frame;
|
|
369
|
+
try {
|
|
370
|
+
const text = typeof data === "string" ? data : String(data);
|
|
371
|
+
frame = JSON.parse(text);
|
|
372
|
+
} catch {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
switch (frame.type) {
|
|
376
|
+
case "ack":
|
|
377
|
+
case "registered": {
|
|
378
|
+
const f = frame;
|
|
379
|
+
if (!this.ackResolvers) return;
|
|
380
|
+
clearTimeout(this.ackResolvers.timer);
|
|
381
|
+
if (f.ok === false) {
|
|
382
|
+
const err = new WrapperTransportError(
|
|
383
|
+
f.error?.code ?? "REGISTER_FAILED",
|
|
384
|
+
f.error?.message ?? "Runner refused registration"
|
|
385
|
+
);
|
|
386
|
+
this.ackResolvers.reject(err);
|
|
387
|
+
} else {
|
|
388
|
+
this.ackResolvers.resolve();
|
|
389
|
+
}
|
|
390
|
+
this.ackResolvers = null;
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
case "command": {
|
|
394
|
+
const f = frame;
|
|
395
|
+
void this.handleCommand(f);
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
default:
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
async handleCommand(frame) {
|
|
403
|
+
const ctx = { kind: "live", commandId: frame.commandId };
|
|
404
|
+
let response;
|
|
405
|
+
try {
|
|
406
|
+
const result = await this.registry.dispatch(frame.action, frame.payload, ctx);
|
|
407
|
+
response = {
|
|
408
|
+
type: "response",
|
|
409
|
+
commandId: frame.commandId,
|
|
410
|
+
success: true,
|
|
411
|
+
result
|
|
412
|
+
};
|
|
413
|
+
} catch (err) {
|
|
414
|
+
const code = err instanceof WrapperTransportError ? err.code : "HANDLER_ERROR";
|
|
415
|
+
const retryable = err instanceof WrapperTransportError ? err.retryable : false;
|
|
416
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
417
|
+
response = {
|
|
418
|
+
type: "response",
|
|
419
|
+
commandId: frame.commandId,
|
|
420
|
+
success: false,
|
|
421
|
+
error: { code, message, retryable }
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
try {
|
|
425
|
+
this.ws?.send(JSON.stringify(response));
|
|
426
|
+
} catch {
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
handleClose() {
|
|
430
|
+
if (this._status === "closed") return;
|
|
431
|
+
this.setStatus("closed");
|
|
432
|
+
if (this.ackResolvers) {
|
|
433
|
+
clearTimeout(this.ackResolvers.timer);
|
|
434
|
+
this.ackResolvers.reject(
|
|
435
|
+
new WrapperTransportError("DISCONNECTED", "WebSocket closed before ack")
|
|
436
|
+
);
|
|
437
|
+
this.ackResolvers = null;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
handleError(ev) {
|
|
441
|
+
if (this.ackResolvers) {
|
|
442
|
+
clearTimeout(this.ackResolvers.timer);
|
|
443
|
+
const message = this.extractErrorMessage(ev);
|
|
444
|
+
this.ackResolvers.reject(
|
|
445
|
+
new WrapperTransportError("WEBSOCKET_ERROR", message, { retryable: true })
|
|
446
|
+
);
|
|
447
|
+
this.ackResolvers = null;
|
|
448
|
+
} else {
|
|
449
|
+
this.setStatus("error");
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
extractErrorMessage(ev) {
|
|
453
|
+
if (ev && typeof ev === "object" && "message" in ev) {
|
|
454
|
+
const m = ev.message;
|
|
455
|
+
if (typeof m === "string") return m;
|
|
456
|
+
}
|
|
457
|
+
if (ev instanceof Error) return ev.message;
|
|
458
|
+
return "WebSocket error";
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
// src/create-transport.ts
|
|
463
|
+
function createTransport(config) {
|
|
464
|
+
switch (config.kind) {
|
|
465
|
+
case "api":
|
|
466
|
+
return new ApiTransport(config);
|
|
467
|
+
case "headless":
|
|
468
|
+
return new HeadlessTransport(config);
|
|
469
|
+
case "headed":
|
|
470
|
+
return new HeadedTransport(config);
|
|
471
|
+
case "live": {
|
|
472
|
+
const opts = config.options ?? {};
|
|
473
|
+
const injected = typeof opts.webSocketCtor === "function" ? { webSocketCtor: opts.webSocketCtor } : void 0;
|
|
474
|
+
return new LiveSessionTransport(config, void 0, injected);
|
|
475
|
+
}
|
|
476
|
+
default: {
|
|
477
|
+
const kind = config.kind ?? "<missing>";
|
|
478
|
+
throw new WrapperTransportError("UNKNOWN_TRANSPORT", `Unknown transport kind '${kind}'`);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// src/helpers/retry.ts
|
|
484
|
+
var defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
485
|
+
var defaultShouldRetry = (err) => {
|
|
486
|
+
if (err instanceof WrapperTransportError) {
|
|
487
|
+
return err.retryable;
|
|
488
|
+
}
|
|
489
|
+
return true;
|
|
490
|
+
};
|
|
491
|
+
async function withRetry(fn, opts = {}) {
|
|
492
|
+
const attempts = Math.max(1, opts.attempts ?? 3);
|
|
493
|
+
const base = Math.max(0, opts.baseMs ?? 200);
|
|
494
|
+
const cap = Math.max(base, opts.maxDelayMs ?? 5e3);
|
|
495
|
+
const shouldRetry = opts.shouldRetry ?? defaultShouldRetry;
|
|
496
|
+
const sleep = opts.sleep ?? defaultSleep;
|
|
497
|
+
let lastErr;
|
|
498
|
+
for (let attempt = 1; attempt <= attempts; attempt++) {
|
|
499
|
+
try {
|
|
500
|
+
return await fn();
|
|
501
|
+
} catch (err) {
|
|
502
|
+
lastErr = err;
|
|
503
|
+
if (attempt === attempts || !shouldRetry(err, attempt)) {
|
|
504
|
+
throw err;
|
|
505
|
+
}
|
|
506
|
+
const delay = Math.min(cap, base * 2 ** (attempt - 1));
|
|
507
|
+
await sleep(delay);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
throw lastErr;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// src/helpers/auth.ts
|
|
514
|
+
var defaultIsAuthError = (err) => {
|
|
515
|
+
if (err instanceof WrapperTransportError) {
|
|
516
|
+
return /^(AUTH|UNAUTH|401)/i.test(err.code);
|
|
517
|
+
}
|
|
518
|
+
const status = err?.status;
|
|
519
|
+
if (status === 401) return true;
|
|
520
|
+
const code = err?.code;
|
|
521
|
+
if (typeof code === "string" && /^(AUTH|UNAUTH|401)/i.test(code)) return true;
|
|
522
|
+
return false;
|
|
523
|
+
};
|
|
524
|
+
async function withAuthRefresh(fn, refresh, opts = {}) {
|
|
525
|
+
const isAuthError = opts.isAuthError ?? defaultIsAuthError;
|
|
526
|
+
try {
|
|
527
|
+
return await fn();
|
|
528
|
+
} catch (err) {
|
|
529
|
+
if (!isAuthError(err)) throw err;
|
|
530
|
+
await refresh();
|
|
531
|
+
return fn();
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
// src/helpers/schema.ts
|
|
536
|
+
var PRIMITIVE_TYPES = /* @__PURE__ */ new Set([
|
|
537
|
+
"string",
|
|
538
|
+
"number",
|
|
539
|
+
"integer",
|
|
540
|
+
"boolean",
|
|
541
|
+
"object",
|
|
542
|
+
"array",
|
|
543
|
+
"null"
|
|
544
|
+
]);
|
|
545
|
+
function normalize(value) {
|
|
546
|
+
if (value === null || value === void 0) return null;
|
|
547
|
+
if (typeof value === "string") {
|
|
548
|
+
if (!PRIMITIVE_TYPES.has(value)) {
|
|
549
|
+
return { type: value };
|
|
550
|
+
}
|
|
551
|
+
return { type: value };
|
|
552
|
+
}
|
|
553
|
+
if (typeof value === "object") {
|
|
554
|
+
return { ...value };
|
|
555
|
+
}
|
|
556
|
+
return null;
|
|
557
|
+
}
|
|
558
|
+
function paramSchemaOf(shape) {
|
|
559
|
+
const properties = {};
|
|
560
|
+
const required = [];
|
|
561
|
+
for (const [key, raw] of Object.entries(shape)) {
|
|
562
|
+
const normalized = normalize(raw);
|
|
563
|
+
if (!normalized) continue;
|
|
564
|
+
const { optional, ...rest } = normalized;
|
|
565
|
+
properties[key] = rest;
|
|
566
|
+
if (optional !== true) {
|
|
567
|
+
required.push(key);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
const schema = {
|
|
571
|
+
type: "object",
|
|
572
|
+
properties,
|
|
573
|
+
additionalProperties: false
|
|
574
|
+
};
|
|
575
|
+
if (required.length > 0) {
|
|
576
|
+
schema["required"] = required;
|
|
577
|
+
}
|
|
578
|
+
return schema;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// src/manifest-only.ts
|
|
582
|
+
function hasManifestOnlyFlag(argv) {
|
|
583
|
+
return argv.includes("--manifest-only");
|
|
584
|
+
}
|
|
585
|
+
async function emitManifestOnly(opts) {
|
|
586
|
+
const envelope = {
|
|
587
|
+
manifestVersion: 1,
|
|
588
|
+
manifest: opts.manifest,
|
|
589
|
+
actions: opts.actions.map((a) => ({
|
|
590
|
+
id: a.id,
|
|
591
|
+
paramSchema: a.paramSchema,
|
|
592
|
+
exclusive: a.exclusive ?? false
|
|
593
|
+
}))
|
|
594
|
+
};
|
|
595
|
+
process.stdout.write(`${JSON.stringify(envelope)}
|
|
596
|
+
`);
|
|
597
|
+
try {
|
|
598
|
+
await opts.transport.close();
|
|
599
|
+
} catch {
|
|
600
|
+
}
|
|
601
|
+
process.exit(0);
|
|
602
|
+
}
|
|
603
|
+
async function runWrapperEntrypoint(opts) {
|
|
604
|
+
const argv = opts.argv ?? process.argv.slice(2);
|
|
605
|
+
const logName = opts.logName ?? opts.manifest.id;
|
|
606
|
+
if (hasManifestOnlyFlag(argv)) {
|
|
607
|
+
await emitManifestOnly({
|
|
608
|
+
transport: opts.transport,
|
|
609
|
+
manifest: opts.manifest,
|
|
610
|
+
actions: opts.actions
|
|
611
|
+
});
|
|
612
|
+
throw new Error("unreachable");
|
|
613
|
+
}
|
|
614
|
+
await opts.transport.ready();
|
|
615
|
+
process.stdout.write(
|
|
616
|
+
`[${logName}] ready (transport=${opts.transport.kind}); press Ctrl+C to exit.
|
|
617
|
+
`
|
|
618
|
+
);
|
|
619
|
+
const shutdown = async (signal) => {
|
|
620
|
+
process.stdout.write(`[${logName}] received ${signal}, closing transport...
|
|
621
|
+
`);
|
|
622
|
+
try {
|
|
623
|
+
await opts.transport.close();
|
|
624
|
+
} catch (err) {
|
|
625
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
626
|
+
process.stderr.write(`[${logName}] close error: ${msg}
|
|
627
|
+
`);
|
|
628
|
+
}
|
|
629
|
+
process.exit(0);
|
|
630
|
+
};
|
|
631
|
+
process.on("SIGINT", (s) => {
|
|
632
|
+
void shutdown(s);
|
|
633
|
+
});
|
|
634
|
+
process.on("SIGTERM", (s) => {
|
|
635
|
+
void shutdown(s);
|
|
636
|
+
});
|
|
637
|
+
return new Promise(() => {
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
exports.ApiTransport = ApiTransport;
|
|
642
|
+
exports.BaseTransport = BaseTransport;
|
|
643
|
+
exports.HandlerRegistry = HandlerRegistry;
|
|
644
|
+
exports.HeadedTransport = HeadedTransport;
|
|
645
|
+
exports.HeadlessTransport = HeadlessTransport;
|
|
646
|
+
exports.LiveSessionTransport = LiveSessionTransport;
|
|
647
|
+
exports.WrapperTransportError = WrapperTransportError;
|
|
648
|
+
exports.createTransport = createTransport;
|
|
649
|
+
exports.emitManifestOnly = emitManifestOnly;
|
|
650
|
+
exports.paramSchemaOf = paramSchemaOf;
|
|
651
|
+
exports.runWrapperEntrypoint = runWrapperEntrypoint;
|
|
652
|
+
exports.withAuthRefresh = withAuthRefresh;
|
|
653
|
+
exports.withRetry = withRetry;
|
|
654
|
+
//# sourceMappingURL=index.cjs.map
|
|
655
|
+
//# sourceMappingURL=index.cjs.map
|