@intuned/runtime-dev 1.3.18-interface.10 → 1.3.18-interface.12

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.
@@ -0,0 +1,354 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __copyProps = (to, from, except, desc) => {
8
+ if (from && typeof from === "object" || typeof from === "function") {
9
+ for (let key of __getOwnPropNames(from))
10
+ if (!__hasOwnProp.call(to, key) && key !== except)
11
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
12
+ }
13
+ return to;
14
+ };
15
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
16
+ // If the importer is in node compatibility mode or this is not an ESM
17
+ // file that has been converted to a CommonJS file using a Babel-
18
+ // compatible transform (i.e. "__esModule" has not been set), then set
19
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
+ mod
22
+ ));
23
+
24
+ // src/commands/get-headless-user-agent.ts
25
+ var import_commander = require("commander");
26
+ var import_dotenv2 = __toESM(require("dotenv"));
27
+
28
+ // src/common/launchBrowser.ts
29
+ var playwright = __toESM(require("playwright"));
30
+ var import_fs_extra = require("fs-extra");
31
+ var fs2 = __toESM(require("fs-extra"));
32
+ var import_wait_on = __toESM(require("wait-on"));
33
+ var import_child_process = require("child_process");
34
+
35
+ // src/commands/common/utils/fileUtils.ts
36
+ var fs = __toESM(require("fs-extra"));
37
+ var import_dotenv = __toESM(require("dotenv"));
38
+ import_dotenv.default.config();
39
+
40
+ // src/common/settingsSchema.ts
41
+ var z = __toESM(require("zod"));
42
+ var baseCaptchaSchema = z.object({
43
+ enabled: z.boolean()
44
+ });
45
+ var customCaptchaSchema = baseCaptchaSchema.extend({
46
+ imageLocators: z.array(z.string()).min(1, "At least one image locator is required"),
47
+ submitLocators: z.array(z.string()).min(1, "At least one submit locator is required"),
48
+ inputLocators: z.array(z.string()).min(1, "At least one input locator is required")
49
+ });
50
+ var textCaptchaSchema = baseCaptchaSchema.extend({
51
+ labelLocators: z.array(z.string()).min(1, "At least one image locator is required"),
52
+ submitLocators: z.array(z.string()).min(1, "At least one submit locator is required"),
53
+ inputLocators: z.array(z.string()).min(1, "At least one input locator is required")
54
+ });
55
+ var captchaSolverSolveSettingsSchema = z.object({
56
+ autoSolve: z.boolean().default(true),
57
+ solveDelay: z.number().min(0).default(2e3),
58
+ maxRetries: z.number().min(0).default(3),
59
+ timeout: z.number().min(0).default(3e4)
60
+ });
61
+ var captchaSolverSettingsSchema = z.object({
62
+ enabled: z.boolean().default(false),
63
+ port: z.number().int().min(1).max(65535).optional(),
64
+ cloudflare: baseCaptchaSchema.optional(),
65
+ googleRecaptchaV2: baseCaptchaSchema.optional(),
66
+ googleRecaptchaV3: baseCaptchaSchema.optional(),
67
+ awscaptcha: baseCaptchaSchema.optional(),
68
+ hcaptcha: baseCaptchaSchema.optional(),
69
+ funcaptcha: baseCaptchaSchema.optional(),
70
+ geetest: baseCaptchaSchema.optional(),
71
+ lemin: baseCaptchaSchema.optional(),
72
+ customCaptcha: customCaptchaSchema.optional(),
73
+ text: textCaptchaSchema.optional(),
74
+ settings: captchaSolverSolveSettingsSchema.default(
75
+ captchaSolverSolveSettingsSchema.parse({})
76
+ )
77
+ }).default({});
78
+ var authSessionsSchema = z.object({
79
+ enabled: z.boolean()
80
+ }).optional().default({
81
+ enabled: false
82
+ });
83
+ var stealthModeSchema = z.object({
84
+ enabled: z.boolean()
85
+ }).optional().default({
86
+ enabled: false
87
+ });
88
+ var settingsSchema = z.object({
89
+ authSessions: authSessionsSchema,
90
+ stealthMode: stealthModeSchema,
91
+ captchaSolver: captchaSolverSettingsSchema.optional()
92
+ });
93
+
94
+ // src/common/constants.ts
95
+ var WORKSPACE_ID_ENV_VAR_KEY = "INTUNED_WORKSPACE_ID";
96
+ var PROJECT_ID_ENV_VAR_KEY = "INTUNED_PROJECT_ID";
97
+ var API_KEY_ENV_VAR_KEY = "INTUNED_API_KEY";
98
+ var AUTH_TOKEN_ENV_VAR_KEY = "INTUNED_AUTH_TOKEN";
99
+ var API_KEY_HEADER_NAME = "x-api-key";
100
+ var CLI_ENV_VAR_KEY = "INTUNED_CLI";
101
+
102
+ // src/common/jwtTokenManager.ts
103
+ var import_cross_fetch = __toESM(require("cross-fetch"));
104
+ var jwt = __toESM(require("jsonwebtoken"));
105
+ var import_neverthrow = require("neverthrow");
106
+ var JwtTokenManager = class {
107
+ constructor(refreshTokenPath) {
108
+ this.refreshTokenPath = refreshTokenPath;
109
+ this._token = void 0;
110
+ }
111
+ get token() {
112
+ return this._token;
113
+ }
114
+ // When the token is set, the schedule for renewal is issued automatically
115
+ // This is currently being set it two places:
116
+ // 1. Whenever the runner starts, initializes it from the environment variable (set whenever the api is run from the authoring IDE)
117
+ // 2. Whenever a published api is called to run (/api/run/*), it is set to the token received in the run request.
118
+ set token(newToken) {
119
+ if (this._token != newToken) {
120
+ this._token = newToken;
121
+ void this.scheduleTokenRefresh();
122
+ }
123
+ }
124
+ get timeToRefresh() {
125
+ if (!this._token) return;
126
+ const payload = jwt.decode(this._token);
127
+ if (!payload || typeof payload == "string") return;
128
+ const expiry = payload.expiry;
129
+ if (!expiry || typeof expiry !== "number") return;
130
+ const timeWindow = 60 * 1e3;
131
+ const timeToRefresh = expiry - Date.now() - timeWindow;
132
+ return Math.max(timeToRefresh, timeWindow);
133
+ }
134
+ async scheduleTokenRefresh() {
135
+ if (process.env.RUN_ENVIRONMENT?.toLowerCase() !== "authoring") return;
136
+ const timeToRefresh = this.timeToRefresh;
137
+ if (timeToRefresh === void 0) return;
138
+ if (this.tokenRefreshTimeout) clearTimeout(this.tokenRefreshTimeout);
139
+ this.tokenRefreshTimeout = setTimeout(async () => {
140
+ const result = await this.refreshToken();
141
+ if (result && result.isErr()) {
142
+ console.error(`[Internal Error] ${result.error}`);
143
+ return;
144
+ }
145
+ await this.scheduleTokenRefresh();
146
+ }, timeToRefresh);
147
+ }
148
+ async refreshToken() {
149
+ if (process.env.RUN_ENVIRONMENT?.toLowerCase() !== "authoring") return;
150
+ const res = await this.fetchWithToken(
151
+ `${this.backendFunctionsBaseUrl}/${this.refreshTokenPath}`,
152
+ {
153
+ method: "GET"
154
+ }
155
+ );
156
+ if (res.status === 401) {
157
+ return (0, import_neverthrow.err)("Unauthorized");
158
+ }
159
+ const jsonResult = await import_neverthrow.ResultAsync.fromPromise(
160
+ res.json(),
161
+ () => "not json"
162
+ );
163
+ if (jsonResult.isErr()) return;
164
+ const newToken = jsonResult.value.token;
165
+ if (newToken) this._token = newToken;
166
+ }
167
+ async fetchWithToken(...[input, init]) {
168
+ const headers = new Headers(init?.headers);
169
+ const apiKey = process.env[API_KEY_ENV_VAR_KEY];
170
+ if (apiKey) {
171
+ headers.set(API_KEY_HEADER_NAME, apiKey);
172
+ }
173
+ const token = process.env[AUTH_TOKEN_ENV_VAR_KEY];
174
+ if (token) {
175
+ headers.set("Authorization", `Bearer ${token}`);
176
+ }
177
+ if (this.token !== void 0) {
178
+ headers.set("Authorization", `Bearer ${this.token}`);
179
+ }
180
+ const result = await (0, import_cross_fetch.default)(input, {
181
+ ...init,
182
+ headers
183
+ });
184
+ if (result.status === 401 && process.env[CLI_ENV_VAR_KEY] === "true") {
185
+ console.warn(
186
+ "Unauthorized backend function call - make sure to provision your project to Intuned to set up the correct API credentials.\nRun 'intuned provision' or see https://docs.intunedhq.com/docs/05-references/cli#provision-project for more information."
187
+ );
188
+ }
189
+ return result;
190
+ }
191
+ get backendFunctionsBaseUrl() {
192
+ try {
193
+ if (!process.env.FUNCTIONS_DOMAIN) {
194
+ throw new Error(
195
+ `Cannot call backend function - FUNCTIONS_DOMAIN not set`
196
+ );
197
+ }
198
+ const domain = process.env.FUNCTIONS_DOMAIN;
199
+ if (!process.env[WORKSPACE_ID_ENV_VAR_KEY]) {
200
+ throw new Error(
201
+ `Cannot call backend function - ${WORKSPACE_ID_ENV_VAR_KEY} not set`
202
+ );
203
+ }
204
+ const workspaceId = process.env[WORKSPACE_ID_ENV_VAR_KEY];
205
+ if (!process.env[PROJECT_ID_ENV_VAR_KEY] && !process.env.INTUNED_INTEGRATION_ID) {
206
+ throw new Error(
207
+ `Cannot call backend function - ${PROJECT_ID_ENV_VAR_KEY} or INTUNED_INTEGRATION_ID not set`
208
+ );
209
+ }
210
+ const projectId = process.env.INTUNED_INTEGRATION_ID ?? process.env[PROJECT_ID_ENV_VAR_KEY];
211
+ return `${domain}/api/${workspaceId}/functions/${projectId}`;
212
+ } catch (e) {
213
+ if (process.env[CLI_ENV_VAR_KEY] === "true") {
214
+ throw new Error(
215
+ `API credentials not set - make sure to provision your project to Intuned to set up the correct API credentials.
216
+ Run 'intuned provision' or see https://docs.intunedhq.com/docs/05-references/cli#provision-project for more information.
217
+ Original error: ${e.message}`
218
+ );
219
+ }
220
+ }
221
+ }
222
+ };
223
+ var backendFunctionsTokenManager = new JwtTokenManager(
224
+ `refreshBackendFunctionsToken`
225
+ );
226
+ backendFunctionsTokenManager.token = process.env.INTUNED_AUTHORING_SESSION_BACKEND_FUNCTIONS_TOKEN;
227
+
228
+ // src/common/extension/intunedExtensionServer.ts
229
+ var import_fastify = __toESM(require("fastify"));
230
+
231
+ // src/common/extension/types.ts
232
+ var import_zod = require("zod");
233
+ var captchaTypeSchema = import_zod.z.enum([
234
+ "aws",
235
+ "cloudflare",
236
+ "customcaptcha",
237
+ "funcaptcha",
238
+ "geetest",
239
+ "hcaptcha",
240
+ "lemincaptcha",
241
+ "recaptcha",
242
+ "textcaptcha"
243
+ ]);
244
+ var captchaStatusSchema = import_zod.z.enum([
245
+ "attached",
246
+ "solving",
247
+ "solved",
248
+ "error",
249
+ "detached"
250
+ ]);
251
+ var captchaErrorCodeSchema = import_zod.z.enum([
252
+ "HIT_LIMIT",
253
+ "MAX_RETRIES",
254
+ "UNEXPECTED_SERVER_RESPONSE",
255
+ "UNEXPECTED_ERROR"
256
+ ]);
257
+ var captchaErrorSchema = import_zod.z.object({
258
+ code: captchaErrorCodeSchema,
259
+ error: import_zod.z.unknown().optional()
260
+ });
261
+ var captchaBaseSchema = import_zod.z.object({
262
+ id: import_zod.z.string().min(1),
263
+ tabId: import_zod.z.number().int().positive(),
264
+ type: captchaTypeSchema,
265
+ retryCount: import_zod.z.number().int().nonnegative().optional()
266
+ });
267
+ var captchaNonErrorSchema = captchaBaseSchema.extend({
268
+ status: captchaStatusSchema.exclude(["error"])
269
+ });
270
+ var captchaErrorStatusSchema = captchaBaseSchema.extend({
271
+ status: import_zod.z.literal("error"),
272
+ error: captchaErrorSchema
273
+ });
274
+ var captchaSchema = import_zod.z.discriminatedUnion("status", [
275
+ captchaBaseSchema.extend({
276
+ status: import_zod.z.literal("attached")
277
+ }),
278
+ captchaBaseSchema.extend({
279
+ status: import_zod.z.literal("solving")
280
+ }),
281
+ captchaBaseSchema.extend({
282
+ status: import_zod.z.literal("solved")
283
+ }),
284
+ captchaBaseSchema.extend({
285
+ status: import_zod.z.literal("detached")
286
+ }),
287
+ captchaErrorStatusSchema
288
+ ]);
289
+
290
+ // src/common/extension/extensionsHelpers.ts
291
+ var import_portfinder = require("portfinder");
292
+
293
+ // src/common/launchBrowser.ts
294
+ var import_util = require("util");
295
+ var import_neverthrow2 = require("neverthrow");
296
+ var import_zod2 = require("zod");
297
+ var execAsync = (0, import_util.promisify)(import_child_process.exec);
298
+ async function getBrowserExecutablePath() {
299
+ const browserType = getBrowserType();
300
+ if (browserType === "brave") {
301
+ return await getBraveExecutablePath();
302
+ }
303
+ }
304
+ function getBrowserType() {
305
+ if (process.env.BROWSER_TYPE === "brave") {
306
+ return "brave";
307
+ }
308
+ return "chromium";
309
+ }
310
+ async function getBraveExecutablePath() {
311
+ const { stdout } = await execAsync("which brave-browser-stable");
312
+ const bravePath = stdout.trim();
313
+ if (bravePath.length === 0) {
314
+ throw new Error("Brave browser not found");
315
+ }
316
+ return bravePath;
317
+ }
318
+ async function getHeadlessUserAgent({
319
+ executablePath,
320
+ args,
321
+ ignoreDefaultArgs
322
+ }) {
323
+ const browser = await playwright.chromium.launch({
324
+ headless: true,
325
+ executablePath,
326
+ args,
327
+ ignoreDefaultArgs
328
+ });
329
+ const context = await browser.newContext();
330
+ const page = await context.newPage();
331
+ let userAgent = await page.evaluate(() => navigator.userAgent);
332
+ await browser.close();
333
+ if (!userAgent || typeof userAgent !== "string") {
334
+ return void 0;
335
+ }
336
+ userAgent = userAgent.replace("HeadlessChrome", "Chrome");
337
+ return userAgent;
338
+ }
339
+
340
+ // src/commands/get-headless-user-agent.ts
341
+ import_dotenv2.default.config();
342
+ import_commander.program.description("get headless user agent").action(async () => {
343
+ try {
344
+ console.log(
345
+ await getHeadlessUserAgent({
346
+ executablePath: await getBrowserExecutablePath()
347
+ })
348
+ );
349
+ } catch (error) {
350
+ console.error("Error getting headless user agent:", error);
351
+ process.exit(1);
352
+ }
353
+ });
354
+ import_commander.program.parse(process.argv);
@@ -398,8 +398,8 @@ var require_interfaceClient = __commonJS({
398
398
  exports2.SocketClient = SocketClient;
399
399
  SocketClient.LENGTH_HEADER_LENGTH = 4;
400
400
  var UnixSocketClient2 = class extends SocketClient {
401
- constructor(path4) {
402
- super(net.createConnection(path4));
401
+ constructor(path3) {
402
+ super(net.createConnection(path3));
403
403
  }
404
404
  };
405
405
  exports2.UnixSocketClient = UnixSocketClient2;
@@ -647,9 +647,9 @@ var backendFunctionsTokenManager = new JwtTokenManager(
647
647
  `refreshBackendFunctionsToken`
648
648
  );
649
649
  backendFunctionsTokenManager.token = process.env.INTUNED_AUTHORING_SESSION_BACKEND_FUNCTIONS_TOKEN;
650
- function callBackendFunctionWithToken(path4, init) {
650
+ function callBackendFunctionWithToken(path3, init) {
651
651
  return backendFunctionsTokenManager.fetchWithToken(
652
- `${backendFunctionsTokenManager.backendFunctionsBaseUrl}/${path4}`,
652
+ `${backendFunctionsTokenManager.backendFunctionsBaseUrl}/${path3}`,
653
653
  init
654
654
  );
655
655
  }
@@ -887,8 +887,8 @@ function getIntunedExtensionPath() {
887
887
  return process.env.INTUNED_EXTENSION_PATH;
888
888
  }
889
889
  async function isIntunedExtensionEnabled() {
890
- const path4 = getIntunedExtensionPath();
891
- if (!path4) {
890
+ const path3 = getIntunedExtensionPath();
891
+ if (!path3) {
892
892
  return false;
893
893
  }
894
894
  const captchaSolverSettings2 = await getIntunedCaptchaSolverSettings();
@@ -1144,11 +1144,11 @@ function getDownloadDirectoryPath() {
1144
1144
  if (!context) {
1145
1145
  throw new Error("ExecutionContext not found");
1146
1146
  }
1147
- const path4 = `/tmp/downloads/${context.runId}`;
1148
- (0, import_fs_extra.ensureDirSync)(path4, {
1147
+ const path3 = `/tmp/downloads/${context.runId}`;
1148
+ (0, import_fs_extra.ensureDirSync)(path3, {
1149
1149
  mode: 1533
1150
1150
  });
1151
- return path4;
1151
+ return path3;
1152
1152
  }
1153
1153
 
1154
1154
  // src/common/runApi/index.ts
@@ -1237,7 +1237,6 @@ async function getStorageState(context) {
1237
1237
  }
1238
1238
 
1239
1239
  // src/common/playwrightContext.ts
1240
- var import_path3 = __toESM(require("path"));
1241
1240
  var fs3 = __toESM(require("fs-extra"));
1242
1241
  var import_neverthrow5 = require("neverthrow");
1243
1242
 
@@ -1248,27 +1247,27 @@ var import_neverthrow3 = require("neverthrow");
1248
1247
  var import_neverthrow2 = require("neverthrow");
1249
1248
  var import_runtime_interface = __toESM(require_dist());
1250
1249
  async function importUsingImportFunction({
1251
- path: path4,
1250
+ path: path3,
1252
1251
  allowGenerators = true,
1253
1252
  importFunction
1254
1253
  }) {
1255
1254
  try {
1256
- const importedResult = await importFunction(path4);
1255
+ const importedResult = await importFunction(path3);
1257
1256
  if (importedResult.isErr()) {
1258
1257
  if (importedResult.error.type === "not_found") {
1259
- return (0, import_neverthrow2.err)(new import_runtime_interface.ApiNotFoundError(path4));
1258
+ return (0, import_neverthrow2.err)(new import_runtime_interface.ApiNotFoundError(path3));
1260
1259
  }
1261
1260
  return (0, import_neverthrow2.err)(new import_runtime_interface.AutomationError(importedResult.error.error));
1262
1261
  }
1263
1262
  const imported = importedResult.value;
1264
1263
  if (!imported || !imported.default || !imported.default.constructor) {
1265
- return (0, import_neverthrow2.err)(new import_runtime_interface.InvalidApiError(`${path4} does not have a default export`));
1264
+ return (0, import_neverthrow2.err)(new import_runtime_interface.InvalidApiError(`${path3} does not have a default export`));
1266
1265
  }
1267
1266
  if (imported.default.constructor.name === "AsyncGeneratorFunction") {
1268
1267
  if (!allowGenerators) {
1269
1268
  return (0, import_neverthrow2.err)(
1270
1269
  new import_runtime_interface.InvalidApiError(
1271
- `${path4} default export must be an async function`
1270
+ `${path3} default export must be an async function`
1272
1271
  )
1273
1272
  );
1274
1273
  }
@@ -1287,7 +1286,7 @@ async function importUsingImportFunction({
1287
1286
  return (0, import_neverthrow2.ok)(imported.default);
1288
1287
  }
1289
1288
  return (0, import_neverthrow2.err)(
1290
- new import_runtime_interface.InvalidApiError(`${path4} default export must be an async function`)
1289
+ new import_runtime_interface.InvalidApiError(`${path3} default export must be an async function`)
1291
1290
  );
1292
1291
  } catch (error) {
1293
1292
  return (0, import_neverthrow2.err)(new import_runtime_interface.AutomationError(error));
@@ -1560,10 +1559,6 @@ async function getHeadlessUserAgent({
1560
1559
 
1561
1560
  // src/common/playwrightContext.ts
1562
1561
  var import_runtime_interface3 = __toESM(require_dist());
1563
- var browserScriptsFile = import_path3.default.join(
1564
- __dirname,
1565
- "./assets/browser_scripts.js"
1566
- );
1567
1562
  async function withPlaywrightContext({
1568
1563
  cdpAddress,
1569
1564
  cdpTargetId,
@@ -1674,11 +1669,11 @@ async function loadSessionToContext({
1674
1669
  // src/common/formatZodError.ts
1675
1670
  function formatZodError(zodError) {
1676
1671
  const formattedErrors = zodError.errors.map((error) => {
1677
- const path4 = error.path.map((segment) => {
1672
+ const path3 = error.path.map((segment) => {
1678
1673
  return typeof segment === "number" ? `[${segment}]` : segment;
1679
1674
  }).join(".");
1680
- if (path4) {
1681
- return `${path4} is invalid - ${error.message}`;
1675
+ if (path3) {
1676
+ return `${path3} is invalid - ${error.message}`;
1682
1677
  }
1683
1678
  return error.message;
1684
1679
  });
@@ -1779,13 +1774,6 @@ async function runApi({
1779
1774
  intunedContext.getAuthSessionParameters = async () => auth.parameters;
1780
1775
  }
1781
1776
  }
1782
- const scriptContent = await fs4.readFile(browserScriptsFile, "utf-8");
1783
- await context.addInitScript({
1784
- content: scriptContent
1785
- });
1786
- for (const page2 of context.pages()) {
1787
- await page2.evaluate(scriptContent);
1788
- }
1789
1777
  if (tracing.enabled) {
1790
1778
  await context.tracing.start({
1791
1779
  screenshots: true,
@@ -2057,9 +2045,9 @@ function getProxyUrlFromRunOptions(runOptions) {
2057
2045
  url.password = proxy.password;
2058
2046
  return url.toString();
2059
2047
  }
2060
- var _defaultImport = async (path4) => {
2048
+ var _defaultImport = async (path3) => {
2061
2049
  try {
2062
- const imported = await Promise.resolve().then(() => require(`${process.cwd()}/${path4}`));
2050
+ const imported = await Promise.resolve().then(() => require(`${process.cwd()}/${path3}`));
2063
2051
  return (0, import_neverthrow7.ok)(imported);
2064
2052
  } catch (e) {
2065
2053
  if ("code" in e && e.code === "MODULE_NOT_FOUND" || e.code === "ERR_MODULE_NOT_FOUND") {
@@ -0,0 +1,2 @@
1
+
2
+ export { }