@intuned/runtime 1.3.14 → 1.3.16

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.
@@ -1,29 +1,29 @@
1
1
  import { Command } from "commander";
2
2
  export declare const baseRunAuthSessionCommandOptionsSchema: import("zod").ZodObject<{
3
3
  proxy: import("zod").ZodOptional<import("zod").ZodString>;
4
- headless: import("zod").ZodDefault<import("zod").ZodBoolean>;
5
4
  timeout: import("zod").ZodEffects<import("zod").ZodEffects<import("zod").ZodDefault<import("zod").ZodString>, import("ms").StringValue, string | undefined>, number, string | undefined>;
5
+ trace: import("zod").ZodDefault<import("zod").ZodBoolean>;
6
+ headless: import("zod").ZodDefault<import("zod").ZodBoolean>;
6
7
  keepBrowserOpen: import("zod").ZodDefault<import("zod").ZodBoolean>;
7
8
  cdpUrl: import("zod").ZodOptional<import("zod").ZodString>;
8
- trace: import("zod").ZodDefault<import("zod").ZodBoolean>;
9
9
  checkAttempts: import("zod").ZodDefault<import("zod").ZodNumber>;
10
10
  createAttempts: import("zod").ZodDefault<import("zod").ZodNumber>;
11
11
  }, "strip", import("zod").ZodTypeAny, {
12
- headless: boolean;
13
12
  timeout: number;
14
- keepBrowserOpen: boolean;
15
13
  trace: boolean;
14
+ headless: boolean;
15
+ keepBrowserOpen: boolean;
16
16
  checkAttempts: number;
17
17
  createAttempts: number;
18
18
  proxy?: string | undefined;
19
19
  cdpUrl?: string | undefined;
20
20
  }, {
21
21
  proxy?: string | undefined;
22
- headless?: boolean | undefined;
23
22
  timeout?: string | undefined;
23
+ trace?: boolean | undefined;
24
+ headless?: boolean | undefined;
24
25
  keepBrowserOpen?: boolean | undefined;
25
26
  cdpUrl?: string | undefined;
26
- trace?: boolean | undefined;
27
27
  checkAttempts?: number | undefined;
28
28
  createAttempts?: number | undefined;
29
29
  }>;
@@ -17,10 +17,10 @@ export declare const baseCommandOptionsSchema: z.ZodObject<{
17
17
  keepBrowserOpen: z.ZodDefault<z.ZodBoolean>;
18
18
  cdpUrl: z.ZodOptional<z.ZodString>;
19
19
  }, "strip", z.ZodTypeAny, {
20
- headless: boolean;
21
20
  timeout: number;
22
- keepBrowserOpen: boolean;
23
21
  trace: boolean;
22
+ headless: boolean;
23
+ keepBrowserOpen: boolean;
24
24
  proxy?: string | undefined;
25
25
  cdpUrl?: string | undefined;
26
26
  }, {
@@ -18,8 +18,8 @@ export declare function executeRunCreateAuthSessionCLI({ id, input, checkRetries
18
18
  cookies: {
19
19
  path: string;
20
20
  value: string;
21
- name: string;
22
21
  domain: string;
22
+ name: string;
23
23
  expires: number;
24
24
  httpOnly: boolean;
25
25
  secure: boolean;
@@ -49,8 +49,8 @@ export declare function executeRunUpdateAuthSessionCLI({ id, input, checkRetries
49
49
  cookies: {
50
50
  path: string;
51
51
  value: string;
52
- name: string;
53
52
  domain: string;
53
+ name: string;
54
54
  expires: number;
55
55
  httpOnly: boolean;
56
56
  secure: boolean;
@@ -78,8 +78,8 @@ export declare function executeAttemptCreateAuthSessionCLI({ id, input, ...rest
78
78
  cookies: {
79
79
  path: string;
80
80
  value: string;
81
- name: string;
82
81
  domain: string;
82
+ name: string;
83
83
  expires: number;
84
84
  httpOnly: boolean;
85
85
  secure: boolean;
@@ -106,8 +106,8 @@ export declare function executeAttemptCheckAuthSessionCLI({ id, ...rest }: {
106
106
  cookies: {
107
107
  path: string;
108
108
  value: string;
109
- name: string;
110
109
  domain: string;
110
+ name: string;
111
111
  expires: number;
112
112
  httpOnly: boolean;
113
113
  secure: boolean;
@@ -140,8 +140,8 @@ declare function runCreate({ authSessionInput, proxy, headless, timeout, traceId
140
140
  cookies: {
141
141
  path: string;
142
142
  value: string;
143
- name: string;
144
143
  domain: string;
144
+ name: string;
145
145
  expires: number;
146
146
  httpOnly: boolean;
147
147
  secure: boolean;
@@ -177,8 +177,8 @@ declare function runCreateWithRetries({ authSessionId, authSessionInput, retries
177
177
  cookies: {
178
178
  path: string;
179
179
  value: string;
180
- name: string;
181
180
  domain: string;
181
+ name: string;
182
182
  expires: number;
183
183
  httpOnly: boolean;
184
184
  secure: boolean;
@@ -8,8 +8,8 @@ export declare function loadAuthSessionInstance(authSessionId: string): Promise<
8
8
  cookies: {
9
9
  path: string;
10
10
  value: string;
11
- name: string;
12
11
  domain: string;
12
+ name: string;
13
13
  expires: number;
14
14
  httpOnly: boolean;
15
15
  secure: boolean;
@@ -0,0 +1,11 @@
1
+ import * as playwright from "playwright";
2
+ import { CaptchaSolverSettings, CaptchaSolverSettingsWithRunContext } from "../settingsSchema";
3
+ export declare function resolveCaptchaSolverSettings(input?: unknown): Promise<CaptchaSolverSettings>;
4
+ export declare function isIntunedExtensionLoaded(): boolean;
5
+ export declare function buildExtensionsList(): string[];
6
+ export declare function getIntunedExtensionPath(): string;
7
+ export declare function isIntunedExtensionEnabled(): Promise<boolean>;
8
+ export declare function getIntunedExtensionWorker(context: playwright.BrowserContext): Promise<playwright.Worker | null>;
9
+ export declare function getIntunedExtensionSettings(captchaSolverSettings: CaptchaSolverSettings): Promise<CaptchaSolverSettingsWithRunContext>;
10
+ export declare function getIntunedCaptchaSolverSettings(): Promise<CaptchaSolverSettings>;
11
+ export declare function setupIntunedExtension(): Promise<void>;
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.buildExtensionsList = buildExtensionsList;
7
+ exports.getIntunedCaptchaSolverSettings = getIntunedCaptchaSolverSettings;
8
+ exports.getIntunedExtensionPath = getIntunedExtensionPath;
9
+ exports.getIntunedExtensionSettings = getIntunedExtensionSettings;
10
+ exports.getIntunedExtensionWorker = getIntunedExtensionWorker;
11
+ exports.isIntunedExtensionEnabled = isIntunedExtensionEnabled;
12
+ exports.isIntunedExtensionLoaded = isIntunedExtensionLoaded;
13
+ exports.resolveCaptchaSolverSettings = resolveCaptchaSolverSettings;
14
+ exports.setupIntunedExtension = setupIntunedExtension;
15
+ var _settings = require("../../commands/common/utils/settings");
16
+ var _settingsSchema = require("../settingsSchema");
17
+ var _constants = require("../constants");
18
+ var _jwtTokenManager = require("../jwtTokenManager");
19
+ var _path = _interopRequireDefault(require("path"));
20
+ var _promises = require("fs/promises");
21
+ var _intunedExtensionServer = require("./intunedExtensionServer");
22
+ var _portfinder = require("portfinder");
23
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
24
+ const INTUNED_WORKER = "intunedWorker.js";
25
+ const INTUNED_EXTENSION_SETTINGS_FILE_NAME = "intunedSettings.json";
26
+ const INTUNED_CAPTCHA_EXTENSION_PORT_ENV_VAR = "INTUNED_CAPTCHA_EXTENSION_PORT";
27
+ let captchaSolverSettings = null;
28
+ async function getIntunedCaptchaExtensionPort() {
29
+ if (process.env.INTUNED_CAPTCHA_EXTENSION_PORT) {
30
+ return parseInt(process.env.INTUNED_CAPTCHA_EXTENSION_PORT, 10);
31
+ }
32
+ return await (0, _portfinder.getPort)({});
33
+ }
34
+ async function resolveCaptchaSolverSettings(input) {
35
+ const parsed = _settingsSchema.captchaSolverSettingsSchema.parse(input || {});
36
+ if (!parsed.port) {
37
+ parsed.port = await getIntunedCaptchaExtensionPort();
38
+ }
39
+ return parsed;
40
+ }
41
+ function getIntunedCaptchaExtensionPortFromEnv() {
42
+ const raw = process.env[INTUNED_CAPTCHA_EXTENSION_PORT_ENV_VAR];
43
+ if (!raw) return null;
44
+ const parsed = Number(raw);
45
+ if (!Number.isFinite(parsed) || parsed <= 0 || parsed > 65535) {
46
+ throw new Error(`Invalid ${INTUNED_CAPTCHA_EXTENSION_PORT_ENV_VAR}: ${raw}`);
47
+ }
48
+ return parsed;
49
+ }
50
+ function isIntunedExtensionLoaded() {
51
+ return !!getIntunedExtensionPath();
52
+ }
53
+ function buildExtensionsList() {
54
+ const extensionsList = [];
55
+ if (isIntunedExtensionLoaded()) {
56
+ const intunedExtensionPath = getIntunedExtensionPath();
57
+ extensionsList.push(intunedExtensionPath);
58
+ }
59
+ return extensionsList;
60
+ }
61
+ function getIntunedExtensionPath() {
62
+ return process.env.INTUNED_EXTENSION_PATH;
63
+ }
64
+ async function isIntunedExtensionEnabled() {
65
+ const path = getIntunedExtensionPath();
66
+ if (!path) {
67
+ return false;
68
+ }
69
+ const captchaSolverSettings = await getIntunedCaptchaSolverSettings();
70
+ return captchaSolverSettings.enabled;
71
+ }
72
+ async function getIntunedExtensionWorker(context) {
73
+ if (!(await isIntunedExtensionEnabled())) {
74
+ return null;
75
+ }
76
+ let attemptCount = 0;
77
+ while (attemptCount < 4) {
78
+ const intunedServiceWorker = context.serviceWorkers().find(serviceWorker => serviceWorker.url().includes(INTUNED_WORKER));
79
+ if (intunedServiceWorker) {
80
+ return intunedServiceWorker;
81
+ }
82
+ try {
83
+ await context.waitForEvent("serviceworker", {
84
+ timeout: 3000
85
+ });
86
+ } catch (err) {
87
+ console.log(`Error accessing service workers (attempt ${attemptCount})`);
88
+ }
89
+ attemptCount++;
90
+ }
91
+ console.error("Failed to get intuned worker after 5 attmepts");
92
+ return null;
93
+ }
94
+ async function getIntunedExtensionSettings(captchaSolverSettings) {
95
+ const [domain, workspaceId, projectId] = [process.env.FUNCTIONS_DOMAIN, process.env[_constants.WORKSPACE_ID_ENV_VAR_KEY], process.env.INTUNED_INTEGRATION_ID ?? process.env[_constants.PROJECT_ID_ENV_VAR_KEY]];
96
+ if (!domain || !workspaceId || !projectId) {
97
+ const missingEnvVars = [domain && "FUNCTIONS_DOMAIN", workspaceId && _constants.WORKSPACE_ID_ENV_VAR_KEY, projectId && `INTUNED_INTEGRATION_ID OR ${_constants.PROJECT_ID_ENV_VAR_KEY}`];
98
+ throw new Error(`Missing required environment variables: ${missingEnvVars}`);
99
+ }
100
+ const authentication = (() => {
101
+ if (process.env.INTUNED_API_KEY) {
102
+ return {
103
+ type: "apiKey",
104
+ apiKey: process.env.INTUNED_API_KEY
105
+ };
106
+ }
107
+ if (process.env.INTUNED_BASIC_AUTH_USERNAME && process.env.INTUNED_BASIC_AUTH_PASSWORD) {
108
+ const credentials = `${process.env.INTUNED_BASIC_AUTH_USERNAME}:${process.env.INTUNED_BASIC_AUTH_PASSWORD}`;
109
+ const token = Buffer.from(credentials, "utf-8").toString("base64");
110
+ return {
111
+ type: "basic",
112
+ token
113
+ };
114
+ }
115
+ return {
116
+ type: "bearer",
117
+ token: _jwtTokenManager.backendFunctionsTokenManager.token
118
+ };
119
+ })();
120
+ const baseUrl = process.env.INTUNED_API_BASE_URL ?? domain;
121
+ return {
122
+ ...captchaSolverSettings,
123
+ workspaceId,
124
+ projectId,
125
+ baseUrl,
126
+ authentication
127
+ };
128
+ }
129
+ async function getIntunedCaptchaSolverSettings() {
130
+ if (captchaSolverSettings) {
131
+ return captchaSolverSettings;
132
+ }
133
+ const settings = await (0, _settings.getSettings)();
134
+ captchaSolverSettings = await resolveCaptchaSolverSettings(settings.captchaSolver);
135
+ return captchaSolverSettings;
136
+ }
137
+ async function setupIntunedExtension() {
138
+ if (!(await isIntunedExtensionEnabled())) {
139
+ return;
140
+ }
141
+ const intunedExtensionPath = getIntunedExtensionPath();
142
+ const intunedExtensionSettingsPath = _path.default.join(intunedExtensionPath, INTUNED_EXTENSION_SETTINGS_FILE_NAME);
143
+ const captchaSolverSettings = await getIntunedCaptchaSolverSettings();
144
+ await (0, _intunedExtensionServer.setupIntunedExtensionServer)(captchaSolverSettings);
145
+ const captchaSolverSettingsWithRunContext = await getIntunedExtensionSettings(captchaSolverSettings);
146
+ await (0, _promises.writeFile)(intunedExtensionSettingsPath, JSON.stringify(captchaSolverSettingsWithRunContext));
147
+ }
@@ -0,0 +1,24 @@
1
+ import type * as playwright from "playwright";
2
+ import { Captcha, CaptchaCallback, CaptchaStatus } from "./types";
3
+ import { CaptchaSolverSettings } from "../settingsSchema";
4
+ export declare class ExtensionServer {
5
+ private tabs;
6
+ private app;
7
+ constructor();
8
+ private getOrCreateTab;
9
+ handleUpsertCaptcha(captcha: Captcha): Promise<void>;
10
+ start({ port, host, }: {
11
+ port: number;
12
+ host?: string;
13
+ }): Promise<void>;
14
+ stop(): Promise<void>;
15
+ getCaptchas(page: playwright.Page, status?: CaptchaStatus): Promise<Captcha[]>;
16
+ subscribe(page: playwright.Page, handler: CaptchaCallback, status?: CaptchaStatus): Promise<void>;
17
+ unsubscribe(page: playwright.Page, handler: CaptchaCallback, status?: CaptchaStatus): Promise<void>;
18
+ removeTab(tabId: number): void;
19
+ getTabId(page: playwright.Page): Promise<number>;
20
+ }
21
+ export declare function getIntunedExtensionServer(): ExtensionServer;
22
+ export declare function setupIntunedExtensionServer(captchaSolverSettings?: CaptchaSolverSettings): Promise<void>;
23
+ export declare function cleanIntunedExtensionServer(): Promise<void>;
24
+ export declare function getTabId(page: playwright.Page): Promise<number>;
@@ -0,0 +1,178 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ExtensionServer = void 0;
7
+ exports.cleanIntunedExtensionServer = cleanIntunedExtensionServer;
8
+ exports.getIntunedExtensionServer = getIntunedExtensionServer;
9
+ exports.getTabId = getTabId;
10
+ exports.setupIntunedExtensionServer = setupIntunedExtensionServer;
11
+ var _fastify = _interopRequireDefault(require("fastify"));
12
+ var _types = require("./types");
13
+ var _extensionsHelpers = require("./extensionsHelpers");
14
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
15
+ class TabCaptchaState {
16
+ constructor(tabId) {
17
+ this.tabId = tabId;
18
+ this.captchasById = new Map();
19
+ this.subscribers = new Array();
20
+ }
21
+ subscribe(handler) {
22
+ this.subscribers.push(handler);
23
+ }
24
+ unsubscribe(handler, status) {
25
+ const index = this.subscribers.findIndex(subscriber => subscriber.handler === handler && (subscriber.status === status || !status));
26
+ if (index !== -1) {
27
+ this.subscribers.splice(index, 1);
28
+ }
29
+ }
30
+ getCaptchas() {
31
+ return [...this.captchasById.values()];
32
+ }
33
+ async upsertCaptcha(captcha) {
34
+ this.captchasById.set(captcha.id, captcha);
35
+ for (const subscriber of this.subscribers) {
36
+ if (!subscriber.status || subscriber.status === captcha.status) {
37
+ await subscriber.handler(captcha);
38
+ }
39
+ }
40
+ }
41
+ }
42
+ class ExtensionServer {
43
+ app = null;
44
+ constructor() {
45
+ this.tabs = new Map();
46
+ }
47
+ getOrCreateTab(tabId) {
48
+ const existing = this.tabs.get(tabId);
49
+ if (existing) return existing;
50
+ const created = new TabCaptchaState(tabId);
51
+ this.tabs.set(tabId, created);
52
+ return created;
53
+ }
54
+ async handleUpsertCaptcha(captcha) {
55
+ const tab = this.getOrCreateTab(captcha.tabId);
56
+ await tab.upsertCaptcha(captcha);
57
+ }
58
+ async start({
59
+ port,
60
+ host = "0.0.0.0"
61
+ }) {
62
+ if (this.app) {
63
+ return;
64
+ }
65
+ this.app = (0, _fastify.default)({
66
+ logger: false,
67
+ bodyLimit: 1_000_000
68
+ });
69
+ this.app.post("/state", async (request, reply) => {
70
+ try {
71
+ const result = _types.captchaSchema.safeParse(request.body);
72
+ if (!result.success) {
73
+ return reply.code(400).send({
74
+ error: "Invalid captcha payload",
75
+ details: result.error.flatten().fieldErrors
76
+ });
77
+ }
78
+ await this.handleUpsertCaptcha(result.data);
79
+ return reply.code(200).send({});
80
+ } catch (error) {
81
+ console.error("Error processing captcha state update:", error);
82
+ return reply.code(500).send({
83
+ error: "Internal server error",
84
+ message: error?.message ?? String(error)
85
+ });
86
+ }
87
+ });
88
+ this.app.setNotFoundHandler((_request, reply) => {
89
+ return reply.code(404).send({
90
+ error: "Not found"
91
+ });
92
+ });
93
+ await this.app.listen({
94
+ port,
95
+ host
96
+ });
97
+ }
98
+ async stop() {
99
+ if (!this.app) return;
100
+ const toClose = this.app;
101
+ this.app = null;
102
+ await toClose.close();
103
+ }
104
+ async getCaptchas(page, status) {
105
+ const tabId = await getTabId(page);
106
+ const tab = this.tabs.get(tabId);
107
+ if (!tab) return [];
108
+ const captchas = tab.getCaptchas();
109
+ if (!status) return captchas;
110
+ return captchas.filter(c => c.status === status && c.tabId === tabId);
111
+ }
112
+ async subscribe(page, handler, status) {
113
+ const tabId = await getTabId(page);
114
+ const tab = this.getOrCreateTab(tabId);
115
+ tab.subscribe({
116
+ handler,
117
+ status
118
+ });
119
+ }
120
+ async unsubscribe(page, handler, status) {
121
+ const tabId = await getTabId(page);
122
+ const tab = this.tabs.get(tabId);
123
+ if (!tab) return;
124
+ tab.unsubscribe(handler, status);
125
+ }
126
+ removeTab(tabId) {
127
+ this.tabs.delete(tabId);
128
+ }
129
+ async getTabId(page) {
130
+ return await getTabId(page);
131
+ }
132
+ }
133
+ exports.ExtensionServer = ExtensionServer;
134
+ let extensionServerSingleton = null;
135
+ function getIntunedExtensionServer() {
136
+ if (!extensionServerSingleton) {
137
+ throw new Error("Intuned Extension Server is not initialized.");
138
+ }
139
+ return extensionServerSingleton;
140
+ }
141
+ async function setupIntunedExtensionServer(captchaSolverSettings) {
142
+ if (!captchaSolverSettings) {
143
+ captchaSolverSettings = await (0, _extensionsHelpers.resolveCaptchaSolverSettings)();
144
+ }
145
+ if (!extensionServerSingleton) {
146
+ extensionServerSingleton = new ExtensionServer();
147
+ }
148
+ await extensionServerSingleton.start({
149
+ port: captchaSolverSettings.port ?? 9000
150
+ });
151
+ }
152
+ async function cleanIntunedExtensionServer() {
153
+ if (extensionServerSingleton) {
154
+ await extensionServerSingleton.stop();
155
+ extensionServerSingleton = null;
156
+ }
157
+ }
158
+ const TAB_ID_CACHE_KEY = "__INTUNED_CACHED_TAB_ID__";
159
+ async function getTabId(page) {
160
+ const cached = page[TAB_ID_CACHE_KEY];
161
+ if (cached !== undefined && typeof cached === "number") {
162
+ return cached;
163
+ }
164
+ let tabId;
165
+ try {
166
+ tabId = await page.evaluate("window.__INTUNED_TAB_ID__", {
167
+ timeout: 100
168
+ });
169
+ } catch {
170
+ await page.waitForFunction("window.__INTUNED_TAB_ID__ !== undefined", {
171
+ timeout: 15_000
172
+ });
173
+ tabId = await page.evaluate("window.__INTUNED_TAB_ID__");
174
+ }
175
+ const numericTabId = Number(tabId);
176
+ page[TAB_ID_CACHE_KEY] = numericTabId;
177
+ return numericTabId;
178
+ }