@qontinui/ui-bridge-wrapper 0.2.0 → 0.4.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 +43 -0
- package/dist/capture-specs-cli.cjs +1023 -0
- package/dist/capture-specs-cli.cjs.map +1 -0
- package/dist/capture-specs-cli.d.ts +90 -0
- package/dist/capture-specs-cli.d.ts.map +1 -0
- package/dist/create-transport.d.ts.map +1 -1
- package/dist/index.cjs +206 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +205 -2
- package/dist/index.js.map +1 -1
- package/dist/inject-cli.cjs +1156 -0
- package/dist/inject-cli.cjs.map +1 -0
- package/dist/inject-cli.d.ts +85 -0
- package/dist/inject-cli.d.ts.map +1 -0
- package/dist/login-web-cli.cjs +1004 -0
- package/dist/login-web-cli.cjs.map +1 -0
- package/dist/login-web-cli.d.ts +116 -0
- package/dist/login-web-cli.d.ts.map +1 -0
- package/dist/transports/headless.d.ts +20 -2
- package/dist/transports/headless.d.ts.map +1 -1
- package/dist/transports/injected.d.ts +88 -0
- package/dist/transports/injected.d.ts.map +1 -0
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +9 -3
|
@@ -0,0 +1,1004 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var url = require('url');
|
|
5
|
+
var module$1 = require('module');
|
|
6
|
+
var fs = require('fs');
|
|
7
|
+
|
|
8
|
+
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
9
|
+
// src/types.ts
|
|
10
|
+
var WrapperTransportError = class extends Error {
|
|
11
|
+
code;
|
|
12
|
+
retryable;
|
|
13
|
+
cause;
|
|
14
|
+
constructor(code, message, options = {}) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "WrapperTransportError";
|
|
17
|
+
this.code = code;
|
|
18
|
+
this.retryable = options.retryable ?? false;
|
|
19
|
+
if (options.cause !== void 0) {
|
|
20
|
+
this.cause = options.cause;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// src/handler-registry.ts
|
|
26
|
+
var HandlerRegistry = class {
|
|
27
|
+
handlers = /* @__PURE__ */ new Map();
|
|
28
|
+
/**
|
|
29
|
+
* Register (or replace) a handler for `actionId`. Replacement is explicit
|
|
30
|
+
* rather than throwing because live reloading / hot module scenarios need
|
|
31
|
+
* to update handlers without tearing the whole transport down.
|
|
32
|
+
*/
|
|
33
|
+
register(actionId, fn) {
|
|
34
|
+
this.handlers.set(actionId, fn);
|
|
35
|
+
}
|
|
36
|
+
/** Remove a handler. Returns `true` if a handler was removed. */
|
|
37
|
+
unregister(actionId) {
|
|
38
|
+
return this.handlers.delete(actionId);
|
|
39
|
+
}
|
|
40
|
+
/** All registered action ids. Stable iteration order (insertion order). */
|
|
41
|
+
list() {
|
|
42
|
+
return Array.from(this.handlers.keys());
|
|
43
|
+
}
|
|
44
|
+
/** Check whether a handler is registered for `actionId`. */
|
|
45
|
+
has(actionId) {
|
|
46
|
+
return this.handlers.has(actionId);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Invoke the handler registered under `actionId`.
|
|
50
|
+
*
|
|
51
|
+
* Throws `WrapperTransportError("NO_HANDLER")` if no handler is registered.
|
|
52
|
+
* Propagates handler errors wrapped as `WrapperTransportError("HANDLER_ERROR")`
|
|
53
|
+
* so callers can uniformly check `err.code`.
|
|
54
|
+
*/
|
|
55
|
+
async dispatch(actionId, params, ctx) {
|
|
56
|
+
const handler = this.handlers.get(actionId);
|
|
57
|
+
if (!handler) {
|
|
58
|
+
throw new WrapperTransportError(
|
|
59
|
+
"NO_HANDLER",
|
|
60
|
+
`No handler registered for action '${actionId}'`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
const result = await handler(params, ctx);
|
|
65
|
+
return result;
|
|
66
|
+
} catch (err) {
|
|
67
|
+
if (err instanceof WrapperTransportError) throw err;
|
|
68
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
69
|
+
throw new WrapperTransportError("HANDLER_ERROR", message, { cause: err });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// src/base-transport.ts
|
|
75
|
+
var BaseTransport = class {
|
|
76
|
+
_status = "idle";
|
|
77
|
+
listeners = /* @__PURE__ */ new Set();
|
|
78
|
+
registry;
|
|
79
|
+
readyPromise = null;
|
|
80
|
+
constructor(registry) {
|
|
81
|
+
this.registry = registry ?? new HandlerRegistry();
|
|
82
|
+
}
|
|
83
|
+
get status() {
|
|
84
|
+
return this._status;
|
|
85
|
+
}
|
|
86
|
+
/** Expose the underlying registry so wrappers can register handlers. */
|
|
87
|
+
get handlerRegistry() {
|
|
88
|
+
return this.registry;
|
|
89
|
+
}
|
|
90
|
+
/** Convenience: register a handler on the transport's registry. */
|
|
91
|
+
register(actionId, fn) {
|
|
92
|
+
this.registry.register(actionId, fn);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Idempotent async setup. First call runs `onReady()`; subsequent calls
|
|
96
|
+
* share the same promise until it settles, after which further calls
|
|
97
|
+
* are no-ops if `status === 'ready'`, or re-run setup if it errored.
|
|
98
|
+
*/
|
|
99
|
+
async ready() {
|
|
100
|
+
if (this._status === "ready") return;
|
|
101
|
+
if (this._status === "closed") {
|
|
102
|
+
throw new WrapperTransportError("TRANSPORT_CLOSED", `Transport '${this.kind}' is closed`);
|
|
103
|
+
}
|
|
104
|
+
if (this.readyPromise) return this.readyPromise;
|
|
105
|
+
this.setStatus("connecting");
|
|
106
|
+
this.readyPromise = this.onReady().then(() => {
|
|
107
|
+
this.setStatus("ready");
|
|
108
|
+
}).catch((err) => {
|
|
109
|
+
this.setStatus("error");
|
|
110
|
+
this.readyPromise = null;
|
|
111
|
+
throw err;
|
|
112
|
+
});
|
|
113
|
+
return this.readyPromise;
|
|
114
|
+
}
|
|
115
|
+
/** Default dispatch — waits for ready, builds ctx, delegates to registry. */
|
|
116
|
+
async dispatch(actionId, params) {
|
|
117
|
+
await this.ready();
|
|
118
|
+
const ctx = await this.buildContext(actionId, params);
|
|
119
|
+
return this.registry.dispatch(actionId, params, ctx);
|
|
120
|
+
}
|
|
121
|
+
async close() {
|
|
122
|
+
if (this._status === "closed") return;
|
|
123
|
+
try {
|
|
124
|
+
await this.onClose();
|
|
125
|
+
} finally {
|
|
126
|
+
this.setStatus("closed");
|
|
127
|
+
this.listeners.clear();
|
|
128
|
+
this.readyPromise = null;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
onStatusChange(listener) {
|
|
132
|
+
this.listeners.add(listener);
|
|
133
|
+
return () => {
|
|
134
|
+
this.listeners.delete(listener);
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
/** Update status and notify listeners. Safe to call from subclasses. */
|
|
138
|
+
setStatus(next) {
|
|
139
|
+
if (this._status === next) return;
|
|
140
|
+
this._status = next;
|
|
141
|
+
for (const l of this.listeners) {
|
|
142
|
+
try {
|
|
143
|
+
l(next);
|
|
144
|
+
} catch {
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Subclass hook: build the context object passed to handlers. Default is
|
|
150
|
+
* `undefined` (api transport). Headless/headed override to provide the
|
|
151
|
+
* Playwright `page`; live overrides to provide a response helper.
|
|
152
|
+
*/
|
|
153
|
+
async buildContext(_actionId, _params) {
|
|
154
|
+
return void 0;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
// src/transports/api.ts
|
|
159
|
+
var ApiTransport = class extends BaseTransport {
|
|
160
|
+
kind = "api";
|
|
161
|
+
fallback;
|
|
162
|
+
constructor(config = { kind: "api" }, registry) {
|
|
163
|
+
super(registry);
|
|
164
|
+
const handler = config.options?.handler;
|
|
165
|
+
if (typeof handler === "function") {
|
|
166
|
+
this.fallback = handler;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
async onReady() {
|
|
170
|
+
}
|
|
171
|
+
async onClose() {
|
|
172
|
+
}
|
|
173
|
+
async buildContext() {
|
|
174
|
+
return { kind: "api" };
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Override dispatch to fall back to `options.handler` when no explicit
|
|
178
|
+
* registration exists for `actionId`.
|
|
179
|
+
*/
|
|
180
|
+
async dispatch(actionId, params) {
|
|
181
|
+
await this.ready();
|
|
182
|
+
if (this.registry.has(actionId)) {
|
|
183
|
+
const ctx = await this.buildContext();
|
|
184
|
+
return this.registry.dispatch(actionId, params, ctx);
|
|
185
|
+
}
|
|
186
|
+
if (this.fallback) {
|
|
187
|
+
try {
|
|
188
|
+
return await this.fallback(actionId, params);
|
|
189
|
+
} catch (err) {
|
|
190
|
+
if (err instanceof WrapperTransportError) throw err;
|
|
191
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
192
|
+
throw new WrapperTransportError("HANDLER_ERROR", message, { cause: err });
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
throw new WrapperTransportError("NO_HANDLER", `No handler registered for action '${actionId}'`);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
// src/transports/headless.ts
|
|
200
|
+
var HeadlessTransport = class extends BaseTransport {
|
|
201
|
+
kind;
|
|
202
|
+
options;
|
|
203
|
+
headless;
|
|
204
|
+
tab = null;
|
|
205
|
+
constructor(config, registry, overrides = {}) {
|
|
206
|
+
super(registry);
|
|
207
|
+
this.kind = overrides.kind ?? "headless";
|
|
208
|
+
this.headless = overrides.headless ?? true;
|
|
209
|
+
this.options = this.parseOptions(config);
|
|
210
|
+
}
|
|
211
|
+
parseOptions(config) {
|
|
212
|
+
const raw = config.options ?? {};
|
|
213
|
+
const targetUrl = raw["targetUrl"];
|
|
214
|
+
if (typeof targetUrl !== "string" || targetUrl.length === 0) {
|
|
215
|
+
throw new WrapperTransportError(
|
|
216
|
+
"INVALID_CONFIG",
|
|
217
|
+
`Transport kind '${this.kind}' requires options.targetUrl (string)`
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
return {
|
|
221
|
+
targetUrl,
|
|
222
|
+
viewportWidth: typeof raw["viewportWidth"] === "number" ? raw["viewportWidth"] : void 0,
|
|
223
|
+
viewportHeight: typeof raw["viewportHeight"] === "number" ? raw["viewportHeight"] : void 0,
|
|
224
|
+
uiBridgeBase: typeof raw["uiBridgeBase"] === "string" ? raw["uiBridgeBase"] : void 0,
|
|
225
|
+
waitForUiBridgeMs: typeof raw["waitForUiBridgeMs"] === "number" ? raw["waitForUiBridgeMs"] : void 0,
|
|
226
|
+
userAgent: typeof raw["userAgent"] === "string" ? raw["userAgent"] : void 0,
|
|
227
|
+
forwardConsole: typeof raw["forwardConsole"] === "boolean" ? raw["forwardConsole"] : void 0
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
async onReady() {
|
|
231
|
+
const { launchHeadlessTab } = await import('@qontinui/ui-bridge-headless');
|
|
232
|
+
const initScripts = await this.buildInitScripts();
|
|
233
|
+
const tab = await launchHeadlessTab({
|
|
234
|
+
url: this.options.targetUrl,
|
|
235
|
+
headless: this.headless,
|
|
236
|
+
uiBridgeBase: this.options.uiBridgeBase,
|
|
237
|
+
waitForUiBridgeMs: this.options.waitForUiBridgeMs,
|
|
238
|
+
viewportWidth: this.options.viewportWidth,
|
|
239
|
+
viewportHeight: this.options.viewportHeight,
|
|
240
|
+
userAgent: this.options.userAgent,
|
|
241
|
+
forwardConsole: this.options.forwardConsole,
|
|
242
|
+
initScripts: initScripts.length > 0 ? initScripts : void 0
|
|
243
|
+
});
|
|
244
|
+
this.tab = {
|
|
245
|
+
browser: tab.browser,
|
|
246
|
+
context: tab.context,
|
|
247
|
+
page: tab.page,
|
|
248
|
+
uiBridgeRegistered: tab.uiBridgeRegistered,
|
|
249
|
+
tabId: tab.tabId,
|
|
250
|
+
close: tab.close
|
|
251
|
+
};
|
|
252
|
+
await this.afterLaunch(tab.page);
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Subclass hook: init-scripts to register on the browser context before the
|
|
256
|
+
* first navigation (via `context.addInitScript`). Default: none. Injected
|
|
257
|
+
* mode overrides this to inject the config + runtime bundle pre-paint.
|
|
258
|
+
*/
|
|
259
|
+
async buildInitScripts() {
|
|
260
|
+
return [];
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Subclass hook: run after launch + navigation, before `ready()` resolves.
|
|
264
|
+
* Default: no-op. Injected mode overrides it to await the in-page runtime.
|
|
265
|
+
*/
|
|
266
|
+
async afterLaunch(_page) {
|
|
267
|
+
}
|
|
268
|
+
async onClose() {
|
|
269
|
+
if (!this.tab) return;
|
|
270
|
+
const t = this.tab;
|
|
271
|
+
this.tab = null;
|
|
272
|
+
await t.close();
|
|
273
|
+
}
|
|
274
|
+
async buildContext() {
|
|
275
|
+
if (!this.tab) {
|
|
276
|
+
throw new WrapperTransportError(
|
|
277
|
+
"NOT_READY",
|
|
278
|
+
`Transport '${this.kind}' is not ready; call ready() before dispatch`
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
return {
|
|
282
|
+
kind: this.kind,
|
|
283
|
+
page: this.tab.page,
|
|
284
|
+
browserContext: this.tab.context,
|
|
285
|
+
browser: this.tab.browser,
|
|
286
|
+
uiBridgeRegistered: this.tab.uiBridgeRegistered,
|
|
287
|
+
tabId: this.tab.tabId
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
// src/transports/headed.ts
|
|
293
|
+
var HeadedTransport = class extends HeadlessTransport {
|
|
294
|
+
constructor(config, registry) {
|
|
295
|
+
super(config, registry, { kind: "headed", headless: false });
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
var require2 = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('login-web-cli.cjs', document.baseURI).href)));
|
|
299
|
+
var cachedBundle = null;
|
|
300
|
+
function loadInjectedBundle() {
|
|
301
|
+
if (cachedBundle !== null) return cachedBundle;
|
|
302
|
+
let bundlePath;
|
|
303
|
+
try {
|
|
304
|
+
bundlePath = require2.resolve("@qontinui/ui-bridge/injected/bundle.global.js");
|
|
305
|
+
} catch {
|
|
306
|
+
const pkgJson = require2.resolve("@qontinui/ui-bridge/package.json");
|
|
307
|
+
bundlePath = pkgJson.replace(/package\.json$/, "dist/injected/bundle.global.js");
|
|
308
|
+
}
|
|
309
|
+
try {
|
|
310
|
+
cachedBundle = fs.readFileSync(bundlePath, "utf8");
|
|
311
|
+
} catch (cause) {
|
|
312
|
+
throw new WrapperTransportError(
|
|
313
|
+
"INJECTED_BUNDLE_MISSING",
|
|
314
|
+
`Could not read the UI Bridge injected runtime bundle at '${bundlePath}'. Ensure @qontinui/ui-bridge is built (the IIFE is emitted by its tsup build).`,
|
|
315
|
+
{ cause }
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
return cachedBundle;
|
|
319
|
+
}
|
|
320
|
+
var InjectedTransport = class extends HeadlessTransport {
|
|
321
|
+
injected;
|
|
322
|
+
constructor(config, registry) {
|
|
323
|
+
const raw = config.options ?? {};
|
|
324
|
+
const headed = raw["headed"] === true;
|
|
325
|
+
super(config, registry, { kind: "injected", headless: !headed });
|
|
326
|
+
this.injected = {
|
|
327
|
+
headed,
|
|
328
|
+
readyTimeoutMs: typeof raw["readyTimeoutMs"] === "number" ? raw["readyTimeoutMs"] : 3e4,
|
|
329
|
+
waitForSettle: raw["waitForSettle"] !== false,
|
|
330
|
+
settleQuietMs: typeof raw["settleQuietMs"] === "number" ? raw["settleQuietMs"] : void 0,
|
|
331
|
+
settleTimeoutMs: typeof raw["settleTimeoutMs"] === "number" ? raw["settleTimeoutMs"] : void 0,
|
|
332
|
+
expectSelector: typeof raw["expectSelector"] === "string" && raw["expectSelector"].length > 0 ? raw["expectSelector"] : void 0,
|
|
333
|
+
authToken: typeof raw["authToken"] === "string" ? raw["authToken"] : void 0,
|
|
334
|
+
registrationMetadata: isRegistrationMetadata(raw["registrationMetadata"]) ? raw["registrationMetadata"] : void 0,
|
|
335
|
+
appId: typeof raw["appId"] === "string" ? raw["appId"] : void 0,
|
|
336
|
+
appName: typeof raw["appName"] === "string" ? raw["appName"] : void 0,
|
|
337
|
+
tabId: typeof raw["tabId"] === "string" ? raw["tabId"] : void 0
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Build the `__uiBridgeInjectedConfig` injected ahead of the bundle, or null
|
|
342
|
+
* if there's nothing to configure. Carries the relay fields (Variant B only,
|
|
343
|
+
* when `uiBridgeBase` is set) AND the settle-tuning fields (both variants).
|
|
344
|
+
*/
|
|
345
|
+
injectedConfig() {
|
|
346
|
+
const cfg = {};
|
|
347
|
+
const base = this.options.uiBridgeBase;
|
|
348
|
+
if (base) {
|
|
349
|
+
cfg.uiBridgeBase = base;
|
|
350
|
+
if (this.injected.authToken) cfg.authToken = this.injected.authToken;
|
|
351
|
+
if (this.injected.registrationMetadata)
|
|
352
|
+
cfg.registrationMetadata = this.injected.registrationMetadata;
|
|
353
|
+
if (this.injected.appId) cfg.appId = this.injected.appId;
|
|
354
|
+
if (this.injected.appName) cfg.appName = this.injected.appName;
|
|
355
|
+
if (this.injected.tabId) cfg.tabId = this.injected.tabId;
|
|
356
|
+
}
|
|
357
|
+
if (this.injected.settleQuietMs !== void 0)
|
|
358
|
+
cfg.settleQuietMs = this.injected.settleQuietMs;
|
|
359
|
+
if (this.injected.settleTimeoutMs !== void 0)
|
|
360
|
+
cfg.settleTimeoutMs = this.injected.settleTimeoutMs;
|
|
361
|
+
if (this.injected.expectSelector !== void 0)
|
|
362
|
+
cfg.expectSelector = this.injected.expectSelector;
|
|
363
|
+
return Object.keys(cfg).length > 0 ? cfg : null;
|
|
364
|
+
}
|
|
365
|
+
async buildInitScripts() {
|
|
366
|
+
const scripts = [];
|
|
367
|
+
const cfg = this.injectedConfig();
|
|
368
|
+
if (cfg) {
|
|
369
|
+
scripts.push(`window.__uiBridgeInjectedConfig = ${JSON.stringify(cfg)};`);
|
|
370
|
+
}
|
|
371
|
+
scripts.push(loadInjectedBundle());
|
|
372
|
+
return scripts;
|
|
373
|
+
}
|
|
374
|
+
async afterLaunch(page) {
|
|
375
|
+
try {
|
|
376
|
+
await page.waitForFunction(
|
|
377
|
+
() => window.__uiBridgeInjected?.ready === true,
|
|
378
|
+
void 0,
|
|
379
|
+
{ timeout: this.injected.readyTimeoutMs }
|
|
380
|
+
);
|
|
381
|
+
} catch (cause) {
|
|
382
|
+
throw new WrapperTransportError(
|
|
383
|
+
"INJECTED_RUNTIME_NOT_READY",
|
|
384
|
+
`The injected UI Bridge runtime did not report ready within ${this.injected.readyTimeoutMs}ms. The bundle may have failed to evaluate (check page console / CSP) or the page never reached DOMContentLoaded.`,
|
|
385
|
+
{ cause, retryable: true }
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
if (this.injected.waitForSettle) {
|
|
389
|
+
let state;
|
|
390
|
+
try {
|
|
391
|
+
await page.waitForFunction(
|
|
392
|
+
() => window.__uiBridgeInjected?.settled === true,
|
|
393
|
+
void 0,
|
|
394
|
+
{ timeout: this.injected.readyTimeoutMs }
|
|
395
|
+
);
|
|
396
|
+
state = await page.evaluate(() => {
|
|
397
|
+
const api = window.__uiBridgeInjected;
|
|
398
|
+
return {
|
|
399
|
+
settled: api?.settled === true,
|
|
400
|
+
settledByTimeout: api?.settledByTimeout === true,
|
|
401
|
+
expectSatisfied: api?.expectSatisfied === true,
|
|
402
|
+
elementCount: api?.elementCount ?? 0
|
|
403
|
+
};
|
|
404
|
+
});
|
|
405
|
+
} catch (cause) {
|
|
406
|
+
throw new WrapperTransportError(
|
|
407
|
+
"INJECTED_RUNTIME_NOT_SETTLED",
|
|
408
|
+
`The injected UI Bridge runtime did not report settled within ${this.injected.readyTimeoutMs}ms. The page kept mutating past its settle cap, or the realm was torn down. Pass options.waitForSettle=false to gate on ready only, or raise options.settleTimeoutMs / readyTimeoutMs.`,
|
|
409
|
+
{ cause, retryable: true }
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
if (state.settledByTimeout && !state.expectSatisfied) {
|
|
413
|
+
const sel = this.injected.expectSelector;
|
|
414
|
+
throw new WrapperTransportError(
|
|
415
|
+
"INJECTED_EXPECT_SELECTOR_UNMET",
|
|
416
|
+
sel ? `The injected runtime settled at its cap without the expected control (selector '${sel}') appearing \u2014 ${state.elementCount} element(s) were registered, but none matched. The control may mount slower than the settle cap (raise options.settleTimeoutMs), the selector may be wrong, or the inject failed. Treat as BLOCKED/UNVERIFIED, not a clean page.` : `The injected runtime settled at its cap with an empty registry (0 elements). The page never rendered interactive content within the settle cap, or the inject failed. Raise options.settleTimeoutMs for a slow page; otherwise treat as BLOCKED/UNVERIFIED.`,
|
|
417
|
+
{ retryable: true }
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
async buildContext() {
|
|
423
|
+
if (!this.tab) {
|
|
424
|
+
throw new WrapperTransportError(
|
|
425
|
+
"NOT_READY",
|
|
426
|
+
`Transport 'injected' is not ready; call ready() before dispatch`
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
const { page, context: browserContext, browser, uiBridgeRegistered, tabId } = this.tab;
|
|
430
|
+
const execute = (action, payload) => page.evaluate(
|
|
431
|
+
(args) => window.__uiBridgeInjected.execute(args[0], args[1]),
|
|
432
|
+
[action, payload]
|
|
433
|
+
);
|
|
434
|
+
const settleState = () => page.evaluate(() => {
|
|
435
|
+
const api = window.__uiBridgeInjected;
|
|
436
|
+
return {
|
|
437
|
+
settled: api?.settled === true,
|
|
438
|
+
elementCount: api?.elementCount ?? 0,
|
|
439
|
+
settledByTimeout: api?.settledByTimeout === true,
|
|
440
|
+
expectSatisfied: api?.expectSatisfied === true
|
|
441
|
+
};
|
|
442
|
+
});
|
|
443
|
+
const whenSettled = (timeoutMs) => page.evaluate(
|
|
444
|
+
(ms) => {
|
|
445
|
+
const api = window.__uiBridgeInjected;
|
|
446
|
+
if (api?.whenSettled) return api.whenSettled(ms ?? void 0);
|
|
447
|
+
return Promise.resolve({
|
|
448
|
+
settled: api?.settled === true,
|
|
449
|
+
elementCount: api?.elementCount ?? 0,
|
|
450
|
+
settledByTimeout: api?.settledByTimeout === true,
|
|
451
|
+
expectSatisfied: api?.expectSatisfied === true
|
|
452
|
+
});
|
|
453
|
+
},
|
|
454
|
+
timeoutMs ?? null
|
|
455
|
+
);
|
|
456
|
+
return {
|
|
457
|
+
kind: "injected",
|
|
458
|
+
page,
|
|
459
|
+
browserContext,
|
|
460
|
+
browser,
|
|
461
|
+
uiBridgeRegistered,
|
|
462
|
+
ready: true,
|
|
463
|
+
tabId,
|
|
464
|
+
settleState,
|
|
465
|
+
whenSettled,
|
|
466
|
+
execute,
|
|
467
|
+
find: (criteria) => execute("find", criteria ?? {}),
|
|
468
|
+
act: (id, request) => execute("executeElementAction", { id, request }),
|
|
469
|
+
snapshot: (options) => execute("getControlSnapshot", options ?? {}),
|
|
470
|
+
assert: (spec) => execute("aiAssert", spec)
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
function isRegistrationMetadata(value) {
|
|
475
|
+
if (!value || typeof value !== "object") return false;
|
|
476
|
+
const v = value;
|
|
477
|
+
return typeof v.userId === "string" && typeof v.sessionId === "string";
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// src/transports/live.ts
|
|
481
|
+
var LiveSessionTransport = class extends BaseTransport {
|
|
482
|
+
kind = "live";
|
|
483
|
+
url;
|
|
484
|
+
appId;
|
|
485
|
+
appName;
|
|
486
|
+
opts;
|
|
487
|
+
wsCtor;
|
|
488
|
+
ws = null;
|
|
489
|
+
ackResolvers = null;
|
|
490
|
+
constructor(config, registry, injected) {
|
|
491
|
+
super(registry);
|
|
492
|
+
if (!config.runnerUrl) {
|
|
493
|
+
throw new WrapperTransportError("INVALID_CONFIG", `Transport kind 'live' requires runnerUrl`);
|
|
494
|
+
}
|
|
495
|
+
this.url = config.runnerUrl;
|
|
496
|
+
this.appId = config.appId;
|
|
497
|
+
this.appName = config.appName;
|
|
498
|
+
this.opts = config.options ?? {};
|
|
499
|
+
const fromInjection = injected?.webSocketCtor;
|
|
500
|
+
const fromGlobal = typeof globalThis.WebSocket === "function" ? globalThis.WebSocket : void 0;
|
|
501
|
+
const resolved = fromInjection ?? fromGlobal;
|
|
502
|
+
if (!resolved) {
|
|
503
|
+
throw new WrapperTransportError(
|
|
504
|
+
"NO_WEBSOCKET",
|
|
505
|
+
`No WebSocket constructor available. Provide one via the second constructor arg or run in an environment with global WebSocket.`
|
|
506
|
+
);
|
|
507
|
+
}
|
|
508
|
+
this.wsCtor = resolved;
|
|
509
|
+
}
|
|
510
|
+
async onReady() {
|
|
511
|
+
const ws = new this.wsCtor(this.url);
|
|
512
|
+
this.ws = ws;
|
|
513
|
+
ws.addEventListener("message", (ev) => this.handleMessage(ev.data));
|
|
514
|
+
ws.addEventListener("close", () => this.handleClose());
|
|
515
|
+
ws.addEventListener("error", (ev) => this.handleError(ev));
|
|
516
|
+
await new Promise((resolve, reject) => {
|
|
517
|
+
const onOpen = () => {
|
|
518
|
+
try {
|
|
519
|
+
const frame = {
|
|
520
|
+
type: "register",
|
|
521
|
+
transport: "websocket",
|
|
522
|
+
appId: this.appId,
|
|
523
|
+
appName: this.appName,
|
|
524
|
+
appType: this.opts.appType ?? "wrapper"
|
|
525
|
+
};
|
|
526
|
+
if (this.opts.framework !== void 0) frame.framework = this.opts.framework;
|
|
527
|
+
if (this.opts.version !== void 0) frame.version = this.opts.version;
|
|
528
|
+
if (this.opts.capabilities !== void 0) frame.capabilities = this.opts.capabilities;
|
|
529
|
+
if (this.opts.origin !== void 0) frame.origin = this.opts.origin;
|
|
530
|
+
if (this.opts.pageUrl !== void 0) frame.pageUrl = this.opts.pageUrl;
|
|
531
|
+
ws.send(JSON.stringify(frame));
|
|
532
|
+
} catch (err) {
|
|
533
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
const timeoutMs = this.opts.handshakeTimeoutMs ?? 1e4;
|
|
537
|
+
const timer = setTimeout(() => {
|
|
538
|
+
this.ackResolvers = null;
|
|
539
|
+
reject(
|
|
540
|
+
new WrapperTransportError(
|
|
541
|
+
"HANDSHAKE_TIMEOUT",
|
|
542
|
+
`Did not receive register ack within ${timeoutMs}ms`
|
|
543
|
+
)
|
|
544
|
+
);
|
|
545
|
+
}, timeoutMs);
|
|
546
|
+
this.ackResolvers = { resolve, reject, timer };
|
|
547
|
+
};
|
|
548
|
+
ws.addEventListener("open", onOpen);
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
async onClose() {
|
|
552
|
+
const ws = this.ws;
|
|
553
|
+
this.ws = null;
|
|
554
|
+
if (this.ackResolvers) {
|
|
555
|
+
clearTimeout(this.ackResolvers.timer);
|
|
556
|
+
this.ackResolvers.reject(
|
|
557
|
+
new WrapperTransportError("TRANSPORT_CLOSED", "Transport closed during handshake")
|
|
558
|
+
);
|
|
559
|
+
this.ackResolvers = null;
|
|
560
|
+
}
|
|
561
|
+
if (ws) {
|
|
562
|
+
try {
|
|
563
|
+
ws.close();
|
|
564
|
+
} catch {
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
async buildContext() {
|
|
569
|
+
return { kind: "live", commandId: null };
|
|
570
|
+
}
|
|
571
|
+
handleMessage(data) {
|
|
572
|
+
let frame;
|
|
573
|
+
try {
|
|
574
|
+
const text = typeof data === "string" ? data : String(data);
|
|
575
|
+
frame = JSON.parse(text);
|
|
576
|
+
} catch {
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
switch (frame.type) {
|
|
580
|
+
case "ack":
|
|
581
|
+
case "registered": {
|
|
582
|
+
const f = frame;
|
|
583
|
+
if (!this.ackResolvers) return;
|
|
584
|
+
clearTimeout(this.ackResolvers.timer);
|
|
585
|
+
if (f.ok === false) {
|
|
586
|
+
const err = new WrapperTransportError(
|
|
587
|
+
f.error?.code ?? "REGISTER_FAILED",
|
|
588
|
+
f.error?.message ?? "Runner refused registration"
|
|
589
|
+
);
|
|
590
|
+
this.ackResolvers.reject(err);
|
|
591
|
+
} else {
|
|
592
|
+
this.ackResolvers.resolve();
|
|
593
|
+
}
|
|
594
|
+
this.ackResolvers = null;
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
case "command": {
|
|
598
|
+
const f = frame;
|
|
599
|
+
void this.handleCommand(f);
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
default:
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
async handleCommand(frame) {
|
|
607
|
+
const ctx = { kind: "live", commandId: frame.commandId };
|
|
608
|
+
let response;
|
|
609
|
+
try {
|
|
610
|
+
const result = await this.registry.dispatch(frame.action, frame.payload, ctx);
|
|
611
|
+
response = {
|
|
612
|
+
type: "response",
|
|
613
|
+
commandId: frame.commandId,
|
|
614
|
+
success: true,
|
|
615
|
+
result
|
|
616
|
+
};
|
|
617
|
+
} catch (err) {
|
|
618
|
+
const code = err instanceof WrapperTransportError ? err.code : "HANDLER_ERROR";
|
|
619
|
+
const retryable = err instanceof WrapperTransportError ? err.retryable : false;
|
|
620
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
621
|
+
response = {
|
|
622
|
+
type: "response",
|
|
623
|
+
commandId: frame.commandId,
|
|
624
|
+
success: false,
|
|
625
|
+
error: { code, message, retryable }
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
try {
|
|
629
|
+
this.ws?.send(JSON.stringify(response));
|
|
630
|
+
} catch {
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
handleClose() {
|
|
634
|
+
if (this._status === "closed") return;
|
|
635
|
+
this.setStatus("closed");
|
|
636
|
+
if (this.ackResolvers) {
|
|
637
|
+
clearTimeout(this.ackResolvers.timer);
|
|
638
|
+
this.ackResolvers.reject(
|
|
639
|
+
new WrapperTransportError("DISCONNECTED", "WebSocket closed before ack")
|
|
640
|
+
);
|
|
641
|
+
this.ackResolvers = null;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
handleError(ev) {
|
|
645
|
+
if (this.ackResolvers) {
|
|
646
|
+
clearTimeout(this.ackResolvers.timer);
|
|
647
|
+
const message = this.extractErrorMessage(ev);
|
|
648
|
+
this.ackResolvers.reject(
|
|
649
|
+
new WrapperTransportError("WEBSOCKET_ERROR", message, { retryable: true })
|
|
650
|
+
);
|
|
651
|
+
this.ackResolvers = null;
|
|
652
|
+
} else {
|
|
653
|
+
this.setStatus("error");
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
extractErrorMessage(ev) {
|
|
657
|
+
if (ev && typeof ev === "object" && "message" in ev) {
|
|
658
|
+
const m = ev.message;
|
|
659
|
+
if (typeof m === "string") return m;
|
|
660
|
+
}
|
|
661
|
+
if (ev instanceof Error) return ev.message;
|
|
662
|
+
return "WebSocket error";
|
|
663
|
+
}
|
|
664
|
+
};
|
|
665
|
+
|
|
666
|
+
// src/create-transport.ts
|
|
667
|
+
function createTransport(config) {
|
|
668
|
+
switch (config.kind) {
|
|
669
|
+
case "api":
|
|
670
|
+
return new ApiTransport(config);
|
|
671
|
+
case "headless":
|
|
672
|
+
return new HeadlessTransport(config);
|
|
673
|
+
case "headed":
|
|
674
|
+
return new HeadedTransport(config);
|
|
675
|
+
case "injected":
|
|
676
|
+
return new InjectedTransport(config);
|
|
677
|
+
case "live": {
|
|
678
|
+
const opts = config.options ?? {};
|
|
679
|
+
const injected = typeof opts.webSocketCtor === "function" ? { webSocketCtor: opts.webSocketCtor } : void 0;
|
|
680
|
+
return new LiveSessionTransport(config, void 0, injected);
|
|
681
|
+
}
|
|
682
|
+
default: {
|
|
683
|
+
const kind = config.kind ?? "<missing>";
|
|
684
|
+
throw new WrapperTransportError("UNKNOWN_TRANSPORT", `Unknown transport kind '${kind}'`);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
// src/login-web-cli.ts
|
|
690
|
+
var USAGE = `ui-bridge-login-web \u2014 automated web login via the UI Bridge injected transport
|
|
691
|
+
|
|
692
|
+
Required (one of):
|
|
693
|
+
--email <e> / --password <p> Credentials. Falls back to env
|
|
694
|
+
UIB_LOGIN_EMAIL / UIB_LOGIN_PASSWORD.
|
|
695
|
+
|
|
696
|
+
Options:
|
|
697
|
+
--url <pageUrl> App login URL (default https://qontinui.io/login).
|
|
698
|
+
Prefer a ?next=<urlencoded-path> form so the
|
|
699
|
+
authed landing is deterministic.
|
|
700
|
+
--success <pathSubstring> Pathname substring the authed landing must
|
|
701
|
+
contain (default /dashboard). Matched against the
|
|
702
|
+
PATHNAME only \u2014 query/fragment excluded.
|
|
703
|
+
--timeout <ms> Per-redirect-hop wait budget (default 60000).
|
|
704
|
+
--headed Launch a visible window (default headless).
|
|
705
|
+
--post-login-click <css> Click this selector ONCE on the authed landing
|
|
706
|
+
(e.g. [data-testid='co-pilot-consent-allow']).
|
|
707
|
+
Non-fatal when the target never appears.
|
|
708
|
+
--keep-open On success, park the authed session until SIGINT/
|
|
709
|
+
SIGTERM instead of closing the browser.
|
|
710
|
+
--help, -h Print this help and exit.
|
|
711
|
+
|
|
712
|
+
Prerequisites (packaged bin): the optional @qontinui/ui-bridge-headless peer must
|
|
713
|
+
be installed AND Chromium available (run \`npx playwright install chromium\` once,
|
|
714
|
+
or set PLAYWRIGHT_BROWSERS_PATH). Git Bash callers: prefix MSYS_NO_PATHCONV=1 so
|
|
715
|
+
a leading-slash --success value is not rewritten into a Windows path.
|
|
716
|
+
|
|
717
|
+
Examples:
|
|
718
|
+
ui-bridge-login-web --url "https://qontinui.io/login?next=%2Fbuild%2Fworkflows" \\
|
|
719
|
+
--success /build/workflows
|
|
720
|
+
ui-bridge-login-web --url "https://qontinui.io/login?next=%2Fco-pilot" \\
|
|
721
|
+
--success /co-pilot --post-login-click "[data-testid='co-pilot-consent-allow']" --keep-open
|
|
722
|
+
`;
|
|
723
|
+
var LoginCliArgError = class extends Error {
|
|
724
|
+
constructor(message) {
|
|
725
|
+
super(message);
|
|
726
|
+
this.name = "LoginCliArgError";
|
|
727
|
+
}
|
|
728
|
+
};
|
|
729
|
+
var isCognito = (u) => /auth\.qontinui\.io|amazoncognito\.com|\/oauth2\//i.test(u);
|
|
730
|
+
var pathOf = (u) => {
|
|
731
|
+
try {
|
|
732
|
+
return new URL(u).pathname;
|
|
733
|
+
} catch {
|
|
734
|
+
return "";
|
|
735
|
+
}
|
|
736
|
+
};
|
|
737
|
+
var isSuccessPath = (u, success) => pathOf(u).includes(success);
|
|
738
|
+
var isAppLoginPath = (u) => /\/login(\b|\/|$)/.test(pathOf(u)) && !isCognito(u);
|
|
739
|
+
function parseArgs(argv) {
|
|
740
|
+
const valueOf = (name) => {
|
|
741
|
+
const i = argv.indexOf(name);
|
|
742
|
+
return i >= 0 && i + 1 < argv.length ? argv[i + 1] ?? null : null;
|
|
743
|
+
};
|
|
744
|
+
const has = (name) => argv.includes(name);
|
|
745
|
+
for (const arg of argv) {
|
|
746
|
+
if (arg.startsWith("--") && !KNOWN_FLAGS.has(arg)) {
|
|
747
|
+
throw new LoginCliArgError(`Unknown flag: ${arg}`);
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
const help = has("--help") || has("-h");
|
|
751
|
+
const timeoutRaw = valueOf("--timeout");
|
|
752
|
+
const timeoutMs = timeoutRaw === null ? 6e4 : Number.parseInt(timeoutRaw, 10);
|
|
753
|
+
if (!help && (!Number.isFinite(timeoutMs) || timeoutMs <= 0)) {
|
|
754
|
+
throw new LoginCliArgError(`--timeout expects a positive integer (got ${timeoutRaw})`);
|
|
755
|
+
}
|
|
756
|
+
const args = {
|
|
757
|
+
url: valueOf("--url") ?? "https://qontinui.io/login",
|
|
758
|
+
email: valueOf("--email") ?? process.env.UIB_LOGIN_EMAIL ?? "",
|
|
759
|
+
password: valueOf("--password") ?? process.env.UIB_LOGIN_PASSWORD ?? "",
|
|
760
|
+
success: valueOf("--success") ?? "/dashboard",
|
|
761
|
+
timeoutMs,
|
|
762
|
+
headed: has("--headed"),
|
|
763
|
+
postClick: valueOf("--post-login-click"),
|
|
764
|
+
keepOpen: has("--keep-open"),
|
|
765
|
+
help
|
|
766
|
+
};
|
|
767
|
+
validateArgs(args);
|
|
768
|
+
return args;
|
|
769
|
+
}
|
|
770
|
+
var KNOWN_FLAGS = /* @__PURE__ */ new Set([
|
|
771
|
+
"--url",
|
|
772
|
+
"--email",
|
|
773
|
+
"--password",
|
|
774
|
+
"--success",
|
|
775
|
+
"--timeout",
|
|
776
|
+
"--headed",
|
|
777
|
+
"--post-login-click",
|
|
778
|
+
"--keep-open",
|
|
779
|
+
"--help",
|
|
780
|
+
"-h"
|
|
781
|
+
]);
|
|
782
|
+
function validateArgs(args) {
|
|
783
|
+
if (args.help) return;
|
|
784
|
+
if (!/^https?:\/\//i.test(args.url)) {
|
|
785
|
+
throw new LoginCliArgError(`--url must start with http:// or https:// (got ${args.url})`);
|
|
786
|
+
}
|
|
787
|
+
if (!args.email || !args.password) {
|
|
788
|
+
throw new LoginCliArgError(
|
|
789
|
+
"need --email/--password or env UIB_LOGIN_EMAIL / UIB_LOGIN_PASSWORD"
|
|
790
|
+
);
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
function log(m) {
|
|
794
|
+
process.stderr.write(`[login-web] ${m}
|
|
795
|
+
`);
|
|
796
|
+
}
|
|
797
|
+
async function main() {
|
|
798
|
+
let args;
|
|
799
|
+
try {
|
|
800
|
+
args = parseArgs(process.argv.slice(2));
|
|
801
|
+
} catch (err) {
|
|
802
|
+
if (err instanceof LoginCliArgError) {
|
|
803
|
+
process.stderr.write(`${err.message}
|
|
804
|
+
|
|
805
|
+
${USAGE}`);
|
|
806
|
+
process.exit(2);
|
|
807
|
+
}
|
|
808
|
+
throw err;
|
|
809
|
+
}
|
|
810
|
+
if (args.help) {
|
|
811
|
+
process.stdout.write(USAGE);
|
|
812
|
+
process.exit(0);
|
|
813
|
+
}
|
|
814
|
+
const { url, email, password, success, timeoutMs, headed, postClick } = args;
|
|
815
|
+
const transport = createTransport({
|
|
816
|
+
kind: "injected",
|
|
817
|
+
options: { targetUrl: url, waitForSettle: false, readyTimeoutMs: 45e3, headed }
|
|
818
|
+
});
|
|
819
|
+
transport.register("drive", async (_p, ctx) => {
|
|
820
|
+
const page = ctx.page;
|
|
821
|
+
log(`landed ${page.url()}`);
|
|
822
|
+
const emailBtn = page.getByRole("button", { name: /continue with email/i }).or(page.getByText(/continue with email/i));
|
|
823
|
+
await emailBtn.first().waitFor({ state: "visible", timeout: 15e3 });
|
|
824
|
+
await emailBtn.first().click();
|
|
825
|
+
await page.waitForURL((u) => isCognito(u.toString()) || isSuccessPath(u.toString(), success), {
|
|
826
|
+
timeout: timeoutMs
|
|
827
|
+
});
|
|
828
|
+
if (isCognito(page.url())) {
|
|
829
|
+
await page.waitForLoadState("domcontentloaded");
|
|
830
|
+
log(`cognito ${page.url()}`);
|
|
831
|
+
const fillFirst = async (sels, val, what) => {
|
|
832
|
+
for (const s of sels) {
|
|
833
|
+
const loc = page.locator(s);
|
|
834
|
+
const n = await loc.count().catch(() => 0);
|
|
835
|
+
for (let i = 0; i < n; i++) {
|
|
836
|
+
const el = loc.nth(i);
|
|
837
|
+
if (await el.isVisible().catch(() => false)) {
|
|
838
|
+
await el.fill(val, { timeout: 1e4 });
|
|
839
|
+
log(`filled ${what} via ${s} [visible #${i}]`);
|
|
840
|
+
return;
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
throw new Error(`Cognito ${what} field (visible) not found (selectors: ${sels.join(", ")})`);
|
|
845
|
+
};
|
|
846
|
+
await fillFirst(
|
|
847
|
+
[
|
|
848
|
+
"#signInFormUsername",
|
|
849
|
+
'input[name="username"]',
|
|
850
|
+
'input[type="email"]',
|
|
851
|
+
'input[autocomplete="username"]'
|
|
852
|
+
],
|
|
853
|
+
email,
|
|
854
|
+
"email"
|
|
855
|
+
);
|
|
856
|
+
await fillFirst(
|
|
857
|
+
[
|
|
858
|
+
"#signInFormPassword",
|
|
859
|
+
'input[name="password"]',
|
|
860
|
+
'input[type="password"]',
|
|
861
|
+
'input[autocomplete="current-password"]'
|
|
862
|
+
],
|
|
863
|
+
password,
|
|
864
|
+
"password"
|
|
865
|
+
);
|
|
866
|
+
const submitLoc = page.locator(
|
|
867
|
+
'input[name="signInSubmitButton"], button[type="submit"], input[type="submit"], [name="signInSubmitButton"]'
|
|
868
|
+
);
|
|
869
|
+
let clicked = false;
|
|
870
|
+
const sn = await submitLoc.count().catch(() => 0);
|
|
871
|
+
for (let i = 0; i < sn; i++) {
|
|
872
|
+
const el = submitLoc.nth(i);
|
|
873
|
+
if (await el.isVisible().catch(() => false)) {
|
|
874
|
+
await el.click();
|
|
875
|
+
clicked = true;
|
|
876
|
+
log(`clicked submit [visible #${i}]`);
|
|
877
|
+
break;
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
if (!clicked) {
|
|
881
|
+
await page.getByRole("button", { name: /sign in|log in|continue|submit/i }).first().click();
|
|
882
|
+
log("clicked submit [role fallback]");
|
|
883
|
+
}
|
|
884
|
+
await page.waitForURL(
|
|
885
|
+
(u) => isSuccessPath(u.toString(), success) || /\/auth\/callback/.test(u.toString()),
|
|
886
|
+
{ timeout: timeoutMs }
|
|
887
|
+
);
|
|
888
|
+
if (/\/auth\/callback/.test(page.url())) {
|
|
889
|
+
await page.waitForURL((u) => isSuccessPath(u.toString(), success), { timeout: timeoutMs }).catch(() => {
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
await page.waitForLoadState("networkidle").catch(() => {
|
|
894
|
+
});
|
|
895
|
+
const finalUrl = page.url();
|
|
896
|
+
log(`final ${finalUrl}`);
|
|
897
|
+
await page.waitForFunction(() => window.__uiBridgeInjected?.ready === true, {
|
|
898
|
+
timeout: 15e3
|
|
899
|
+
}).catch(() => {
|
|
900
|
+
});
|
|
901
|
+
let snap = null;
|
|
902
|
+
try {
|
|
903
|
+
snap = await ctx.snapshot();
|
|
904
|
+
} catch {
|
|
905
|
+
}
|
|
906
|
+
const uiBridgeRoute = snap?.route ?? null;
|
|
907
|
+
const landingPath = uiBridgeRoute ?? pathOf(finalUrl);
|
|
908
|
+
const ok = !isAppLoginPath(finalUrl) && !isCognito(finalUrl) && landingPath.includes(success);
|
|
909
|
+
let errorText = null;
|
|
910
|
+
if (!ok) {
|
|
911
|
+
try {
|
|
912
|
+
const alerts = page.locator(
|
|
913
|
+
'[role="alert"], #loginErrorMessage, .error-message, [class*="error" i], [data-testid*="error" i]'
|
|
914
|
+
);
|
|
915
|
+
const an = await alerts.count();
|
|
916
|
+
for (let i = 0; i < an && !errorText; i++) {
|
|
917
|
+
const el = alerts.nth(i);
|
|
918
|
+
if (await el.isVisible().catch(() => false)) {
|
|
919
|
+
const t = (await el.innerText().catch(() => "")).trim();
|
|
920
|
+
if (t) errorText = t.slice(0, 500);
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
} catch {
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
let postLoginClicked = null;
|
|
927
|
+
if (ok && postClick) {
|
|
928
|
+
postLoginClicked = false;
|
|
929
|
+
try {
|
|
930
|
+
const target = page.locator(postClick).first();
|
|
931
|
+
await target.waitFor({ state: "visible", timeout: 2e4 });
|
|
932
|
+
await target.click();
|
|
933
|
+
postLoginClicked = true;
|
|
934
|
+
log(`post-login click: ${postClick}`);
|
|
935
|
+
} catch {
|
|
936
|
+
log(`post-login click target never visible (non-fatal): ${postClick}`);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
return {
|
|
940
|
+
ok,
|
|
941
|
+
finalUrl,
|
|
942
|
+
landingPath,
|
|
943
|
+
onAppLoginPage: isAppLoginPath(finalUrl),
|
|
944
|
+
stillOnCognito: isCognito(finalUrl),
|
|
945
|
+
errorText,
|
|
946
|
+
uiBridgeRoute,
|
|
947
|
+
uiBridgeRegistered: snap?.registration?.totalRegistered ?? null,
|
|
948
|
+
postLoginClicked
|
|
949
|
+
};
|
|
950
|
+
});
|
|
951
|
+
let result;
|
|
952
|
+
let code;
|
|
953
|
+
try {
|
|
954
|
+
await transport.ready();
|
|
955
|
+
result = await transport.dispatch("drive");
|
|
956
|
+
code = result && result.ok ? 0 : 1;
|
|
957
|
+
} catch (err) {
|
|
958
|
+
result = { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
959
|
+
code = 1;
|
|
960
|
+
}
|
|
961
|
+
process.stdout.write(JSON.stringify(result) + "\n");
|
|
962
|
+
if (args.keepOpen && result && result.ok) {
|
|
963
|
+
log("--keep-open: authed session parked; press Ctrl+C to exit");
|
|
964
|
+
const stop = async () => {
|
|
965
|
+
await transport.close().catch(() => {
|
|
966
|
+
});
|
|
967
|
+
process.exit(0);
|
|
968
|
+
};
|
|
969
|
+
process.on("SIGINT", () => void stop());
|
|
970
|
+
process.on("SIGTERM", () => void stop());
|
|
971
|
+
setInterval(() => {
|
|
972
|
+
}, 1 << 30);
|
|
973
|
+
return;
|
|
974
|
+
}
|
|
975
|
+
await transport.close().catch(() => {
|
|
976
|
+
});
|
|
977
|
+
process.exit(code);
|
|
978
|
+
}
|
|
979
|
+
var isMain = (() => {
|
|
980
|
+
try {
|
|
981
|
+
return process.argv[1] === url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('login-web-cli.cjs', document.baseURI).href)));
|
|
982
|
+
} catch {
|
|
983
|
+
return false;
|
|
984
|
+
}
|
|
985
|
+
})();
|
|
986
|
+
if (isMain) {
|
|
987
|
+
void main().catch((err) => {
|
|
988
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
989
|
+
process.stderr.write(`[login-web] fatal: ${msg}
|
|
990
|
+
`);
|
|
991
|
+
process.exit(1);
|
|
992
|
+
});
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
exports.LoginCliArgError = LoginCliArgError;
|
|
996
|
+
exports.USAGE = USAGE;
|
|
997
|
+
exports.isAppLoginPath = isAppLoginPath;
|
|
998
|
+
exports.isCognito = isCognito;
|
|
999
|
+
exports.isSuccessPath = isSuccessPath;
|
|
1000
|
+
exports.parseArgs = parseArgs;
|
|
1001
|
+
exports.pathOf = pathOf;
|
|
1002
|
+
exports.validateArgs = validateArgs;
|
|
1003
|
+
//# sourceMappingURL=login-web-cli.cjs.map
|
|
1004
|
+
//# sourceMappingURL=login-web-cli.cjs.map
|