@intuned/runtime-dev 1.3.17-ws.0 → 1.3.17

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.
Files changed (119) hide show
  1. package/.babelrc +6 -0
  2. package/InterfaceTemplate/{utils.ts → __utils.ts} +3 -1
  3. package/InterfaceTemplate/index.playwright.ts +1 -1
  4. package/bin/intuned +0 -0
  5. package/bin/intuned-interface +7 -0
  6. package/dist/commands/api/run.js +7 -11
  7. package/dist/commands/auth-sessions/load.js +2 -2
  8. package/dist/commands/auth-sessions/run-check.js +8 -8
  9. package/dist/commands/auth-sessions/run-create.js +6 -6
  10. package/dist/commands/build.js +2 -3
  11. package/dist/commands/common/browserUtils.d.ts +3 -3
  12. package/dist/commands/common/browserUtils.js +3 -4
  13. package/dist/commands/common/getFirstLineNumber.test.js +1 -2
  14. package/dist/commands/common/projectExclusions.js +1 -1
  15. package/dist/commands/common/tsNodeImport.d.ts +1 -1
  16. package/dist/commands/common/tsNodeImport.js +10 -2
  17. package/dist/commands/common/utils/fileUtils.js +1 -2
  18. package/dist/commands/common/utils/{unixSocket.d.ts → interfaceClient.d.ts} +10 -2
  19. package/dist/commands/common/utils/{unixSocket.js → interfaceClient.js} +16 -5
  20. package/dist/commands/common/utils/template.js +1 -2
  21. package/dist/commands/interface/run.js +23 -49
  22. package/dist/commands/intuned-cli/commands/attempt_api.command.js +1 -1
  23. package/dist/commands/intuned-cli/commands/attempt_authsession.command.js +1 -1
  24. package/dist/commands/intuned-cli/commands/attempt_authsession_check.command.js +2 -2
  25. package/dist/commands/intuned-cli/commands/attempt_authsession_create.command.js +1 -1
  26. package/dist/commands/intuned-cli/commands/authsession.command.js +1 -1
  27. package/dist/commands/intuned-cli/commands/authsession_record.command.js +1 -1
  28. package/dist/commands/intuned-cli/commands/authsession_scaffold.command.d.ts +1 -0
  29. package/dist/commands/intuned-cli/commands/authsession_scaffold.command.js +16 -0
  30. package/dist/commands/intuned-cli/commands/deploy.command.js +13 -9
  31. package/dist/commands/intuned-cli/commands/index.d.ts +2 -2
  32. package/dist/commands/intuned-cli/commands/index.js +15 -15
  33. package/dist/commands/intuned-cli/commands/{save.command.d.ts → provision.command.d.ts} +3 -3
  34. package/dist/commands/intuned-cli/commands/provision.command.js +50 -0
  35. package/dist/commands/intuned-cli/commands/run_api.command.js +1 -1
  36. package/dist/commands/intuned-cli/commands/run_authsession.command.d.ts +5 -1
  37. package/dist/commands/intuned-cli/commands/run_authsession.command.js +2 -2
  38. package/dist/commands/intuned-cli/commands/run_authsession_create.command.js +1 -1
  39. package/dist/commands/intuned-cli/commands/run_authsession_update.command.js +2 -2
  40. package/dist/commands/intuned-cli/commands/run_authsession_validate.command.js +2 -2
  41. package/dist/commands/intuned-cli/commands/types.d.ts +5 -1
  42. package/dist/commands/intuned-cli/commands/types.js +7 -5
  43. package/dist/commands/intuned-cli/controller/__test__/api.test.js +12 -10
  44. package/dist/commands/intuned-cli/controller/__test__/authSession.test.js +18 -16
  45. package/dist/commands/intuned-cli/controller/api.d.ts +2 -2
  46. package/dist/commands/intuned-cli/controller/api.js +6 -3
  47. package/dist/commands/intuned-cli/controller/authSession.d.ts +13 -13
  48. package/dist/commands/intuned-cli/controller/authSession.js +31 -30
  49. package/dist/commands/intuned-cli/controller/build.js +1 -2
  50. package/dist/commands/intuned-cli/controller/deploy.js +53 -12
  51. package/dist/commands/intuned-cli/controller/index.js +2 -3
  52. package/dist/commands/intuned-cli/controller/{save.d.ts → provision.d.ts} +5 -1
  53. package/dist/commands/intuned-cli/controller/provision.js +299 -0
  54. package/dist/commands/intuned-cli/controller/scaffold.d.ts +1 -0
  55. package/dist/commands/intuned-cli/controller/scaffold.js +77 -0
  56. package/dist/commands/intuned-cli/helpers/__test__/browser.test.js +40 -2
  57. package/dist/commands/intuned-cli/helpers/__test__/tracing.test.js +4 -3
  58. package/dist/commands/intuned-cli/helpers/api.js +4 -7
  59. package/dist/commands/intuned-cli/helpers/auth.d.ts +4 -4
  60. package/dist/commands/intuned-cli/helpers/auth.js +24 -21
  61. package/dist/commands/intuned-cli/helpers/backend.js +12 -4
  62. package/dist/commands/intuned-cli/helpers/browser.d.ts +3 -4
  63. package/dist/commands/intuned-cli/helpers/browser.js +31 -2
  64. package/dist/commands/intuned-cli/helpers/context.js +2 -2
  65. package/dist/commands/intuned-cli/helpers/errors.d.ts +1 -1
  66. package/dist/commands/intuned-cli/helpers/errors.js +2 -2
  67. package/dist/commands/intuned-cli/helpers/intunedJson.d.ts +6 -5
  68. package/dist/commands/intuned-cli/helpers/prompts.d.ts +3 -0
  69. package/dist/commands/intuned-cli/helpers/prompts.js +71 -0
  70. package/dist/commands/intuned-cli/helpers/timeout.js +2 -2
  71. package/dist/commands/intuned-cli/helpers/traces.d.ts +1 -1
  72. package/dist/commands/intuned-cli/helpers/wrapper.js +14 -4
  73. package/dist/commands/intuned-cli/main.js +1 -2
  74. package/dist/commands/intuned-cli/types.d.ts +41 -12
  75. package/dist/commands/intuned-cli/types.js +12 -2
  76. package/dist/commands/ts-check.js +1 -2
  77. package/dist/common/backendFunctions/getAuthSessionParameters.js +1 -1
  78. package/dist/common/binStartupScript.js +16 -7
  79. package/dist/common/browserTabs.d.ts +72 -0
  80. package/dist/common/browserTabs.js +74 -0
  81. package/dist/common/constants.d.ts +1 -0
  82. package/dist/common/constants.js +2 -1
  83. package/dist/common/contextStorageStateHelpers.d.ts +4 -3
  84. package/dist/common/contextStorageStateHelpers.js +4 -1
  85. package/dist/common/extension/extensionsHelpers.d.ts +1 -1
  86. package/dist/common/extension/types.d.ts +14 -7
  87. package/dist/common/formatZodError.d.ts +1 -1
  88. package/dist/common/intunedJson.d.ts +29 -14
  89. package/dist/common/intunedJson.js +26 -5
  90. package/dist/common/jwtTokenManager.js +10 -6
  91. package/dist/common/launchBrowser.d.ts +3 -0
  92. package/dist/common/launchBrowser.js +60 -5
  93. package/dist/common/playwrightContext.d.ts +3 -5
  94. package/dist/common/playwrightContext.js +16 -12
  95. package/dist/common/runApi/importUsingImportFunction.d.ts +1 -3
  96. package/dist/common/runApi/importUsingImportFunction.js +7 -7
  97. package/dist/common/runApi/index.d.ts +3 -6
  98. package/dist/common/runApi/index.js +26 -52
  99. package/dist/common/settingsSchema.d.ts +49 -45
  100. package/dist/common/settingsSchema.js +1 -2
  101. package/dist/common/setupContextHook.d.ts +1 -2
  102. package/dist/common/setupContextHook.js +2 -2
  103. package/dist/common/telemetry.js +1 -2
  104. package/dist/index.d.ts +0 -1
  105. package/dist/index.js +0 -7
  106. package/dist/runtime/downloadDirectory.js +2 -2
  107. package/dist/vendor/runtime-interface.d.ts +1 -0
  108. package/dist/vendor/runtime-interface.js +491 -0
  109. package/package.json +19 -11
  110. package/tsup.config.ts +12 -0
  111. package/WebTemplate.zip +0 -0
  112. package/dist/commands/intuned-cli/commands/init.command.d.ts +0 -1
  113. package/dist/commands/intuned-cli/commands/init.command.js +0 -13
  114. package/dist/commands/intuned-cli/commands/save.command.js +0 -42
  115. package/dist/commands/intuned-cli/controller/save.js +0 -357
  116. package/dist/common/runApi/errors.d.ts +0 -72
  117. package/dist/common/runApi/errors.js +0 -169
  118. package/dist/common/runApi/types.d.ts +0 -830
  119. package/dist/common/runApi/types.js +0 -73
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.getBrowserExecutablePath = getBrowserExecutablePath;
7
+ exports.getCdpWebSocketUrl = getCdpWebSocketUrl;
7
8
  exports.getHeadlessUserAgent = getHeadlessUserAgent;
8
9
  exports.getLocalCdpAddress = getLocalCdpAddress;
9
10
  exports.launchBrowser = launchBrowser;
@@ -17,9 +18,11 @@ var _child_process = require("child_process");
17
18
  var _extensionsHelpers = require("./extension/extensionsHelpers");
18
19
  var _intunedExtensionServer = require("./extension/intunedExtensionServer");
19
20
  var _util = require("util");
21
+ var _neverthrow = require("neverthrow");
22
+ var _zod = require("zod");
23
+ var _intunedJson = require("./intunedJson");
20
24
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
21
- function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
22
- function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
25
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
23
26
  const execAsync = (0, _util.promisify)(_child_process.exec);
24
27
  async function createUserDirWithPreferences() {
25
28
  const playwrightTempDir = await (0, _fsExtra.mkdtemp)("/tmp/pw-");
@@ -46,9 +49,22 @@ async function launchChromium(options) {
46
49
  throw new Error("No browser contexts found in the connected browser");
47
50
  }
48
51
  const context = browser.contexts()[0];
49
- let page = context.pages().at(0);
50
- if (!page) {
51
- page = await context.newPage();
52
+ let page = context.pages().at(0) ?? (await context.newPage());
53
+ const targetId = options.cdpTargetId;
54
+ if (targetId) {
55
+ for (const p of context.pages()) {
56
+ let cdp = null;
57
+ try {
58
+ cdp = await context.newCDPSession(p);
59
+ const result = await cdp.send("Target.getTargetInfo");
60
+ if (result.targetInfo.targetId === targetId) {
61
+ page = p;
62
+ break;
63
+ }
64
+ } catch (error) {} finally {
65
+ await cdp?.detach();
66
+ }
67
+ }
52
68
  }
53
69
  return {
54
70
  page,
@@ -139,7 +155,21 @@ async function launchChromium(options) {
139
155
  context
140
156
  };
141
157
  }
158
+ async function getIntunedBrowserExecutablePath() {
159
+ const intunedPath = process.env.INTUNED_BROWSER_PATH;
160
+ if (intunedPath && (await fs.exists(intunedPath))) {
161
+ return intunedPath;
162
+ }
163
+ return undefined;
164
+ }
142
165
  async function getBrowserExecutablePath() {
166
+ const stealthConfig = await (0, _intunedJson.getStealthModeConfig)();
167
+ if (stealthConfig.enabled && stealthConfig.type === "intunedBrowser") {
168
+ const intunedPath = await getIntunedBrowserExecutablePath();
169
+ if (intunedPath) {
170
+ return intunedPath;
171
+ }
172
+ }
143
173
  const browserType = getBrowserType();
144
174
  if (browserType === "brave") {
145
175
  return await getBraveExecutablePath();
@@ -173,6 +203,31 @@ async function getBraveExecutablePath() {
173
203
  function getLocalCdpAddress(port) {
174
204
  return `http://localhost:${port}`;
175
205
  }
206
+ async function getCdpWebSocketUrl(cdpAddress) {
207
+ let response;
208
+ try {
209
+ response = await fetch(`${cdpAddress}/json/version`);
210
+ } catch (error) {
211
+ return (0, _neverthrow.err)(`Failed to fetch CDP version from ${cdpAddress}: ${error instanceof Error ? error.message : String(error)}`);
212
+ }
213
+ if (!response.ok) {
214
+ return (0, _neverthrow.err)(`Failed to get CDP WebSocket URL from ${cdpAddress}: ${response.status} ${response.statusText}`);
215
+ }
216
+ let data;
217
+ try {
218
+ data = await response.json();
219
+ } catch (error) {
220
+ return (0, _neverthrow.err)(`Invalid CDP version response from ${cdpAddress}: ${error instanceof Error ? error.message : String(error)}`);
221
+ }
222
+ const cdpJsonVersionResponseSchema = _zod.z.object({
223
+ webSocketDebuggerUrl: _zod.z.string().url()
224
+ });
225
+ const parseResult = cdpJsonVersionResponseSchema.safeParse(data);
226
+ if (!parseResult.success) {
227
+ return (0, _neverthrow.err)(`Invalid CDP version response from ${cdpAddress}: ${JSON.stringify(parseResult.error.format())}`);
228
+ }
229
+ return (0, _neverthrow.ok)(parseResult.data.webSocketDebuggerUrl);
230
+ }
176
231
  async function waitOnCdpAddress(cdpAddress) {
177
232
  const cdpAddressWithoutProtocol = cdpAddress.replace("http://", "").replace("https://", "").replace("localhost", "127.0.0.1");
178
233
  await (0, _waitOn.default)({
@@ -1,10 +1,7 @@
1
- import * as playwright from "playwright";
2
- import { RunAutomationError } from "./runApi/errors";
3
- import type { RunApiSession } from "./runApi/types";
1
+ import type * as playwright from "playwright";
4
2
  import { Err, Ok } from "neverthrow";
5
- import { type ImportFunction } from "./runApi/importUsingImportFunction";
6
3
  import { type Proxy } from "./launchBrowser";
7
- export declare const browserScriptsFile: string;
4
+ import { ImportFunction, RunAutomationError, RunApiSession } from "@intuned/runtime-interface";
8
5
  type WithPlaywrightContextParameters = {
9
6
  importFunction: ImportFunction;
10
7
  apiName: string;
@@ -23,6 +20,7 @@ export declare function withPlaywrightContext<R>(options: {
23
20
  } & WithPlaywrightContextParameters, fn: WithPlaywrightContextWrappedFunction<R>): Promise<Ok<R, any> | Err<any, RunAutomationError>>;
24
21
  export declare function withPlaywrightContext<R>(options: {
25
22
  cdpAddress: string;
23
+ cdpTargetId?: string;
26
24
  } & WithPlaywrightContextParameters, fn: WithPlaywrightContextWrappedFunction<R>): Promise<Ok<R, any> | Err<any, RunAutomationError>>;
27
25
  export declare function loadSessionToContext({ context, session, }: {
28
26
  context: playwright.BrowserContext;
@@ -3,24 +3,20 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.browserScriptsFile = void 0;
7
6
  exports.loadSessionToContext = loadSessionToContext;
8
7
  exports.withPlaywrightContext = withPlaywrightContext;
9
8
  var _contextStorageStateHelpers = require("./contextStorageStateHelpers");
10
- var _path = _interopRequireDefault(require("path"));
11
9
  var fs = _interopRequireWildcard(require("fs-extra"));
12
10
  var _fileUtils = require("../commands/common/utils/fileUtils");
13
- var _errors = require("./runApi/errors");
14
11
  var _neverthrow = require("neverthrow");
15
12
  var _setupContextHook = require("./setupContextHook");
16
13
  var _portfinder = require("portfinder");
17
14
  var _launchBrowser = require("./launchBrowser");
18
- function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
19
- function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
20
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
21
- const browserScriptsFile = exports.browserScriptsFile = _path.default.join(__dirname, "./assets/browser_scripts.js");
15
+ var _runtimeInterface = require("../vendor/runtime-interface");
16
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
22
17
  async function withPlaywrightContext({
23
18
  cdpAddress,
19
+ cdpTargetId,
24
20
  proxy,
25
21
  headless = true,
26
22
  downloadsPath,
@@ -44,7 +40,8 @@ async function withPlaywrightContext({
44
40
  page,
45
41
  context
46
42
  } = await (0, _launchBrowser.launchBrowser)({
47
- cdpAddress
43
+ cdpAddress,
44
+ cdpTargetId
48
45
  }));
49
46
  } else {
50
47
  ({
@@ -65,13 +62,13 @@ async function withPlaywrightContext({
65
62
  context,
66
63
  page
67
64
  } = await (0, _launchBrowser.launchBrowser)({
68
- cdpAddress
65
+ cdpAddress,
66
+ cdpTargetId
69
67
  }));
70
68
  } else {
71
69
  const port = await (0, _portfinder.getPort)({
72
70
  port: 9222
73
71
  });
74
- hookCdpUrl = (0, _launchBrowser.getLocalCdpAddress)(port);
75
72
  ({
76
73
  context,
77
74
  page
@@ -81,16 +78,23 @@ async function withPlaywrightContext({
81
78
  downloadsPath,
82
79
  cdpPort: port
83
80
  }));
81
+ hookCdpUrl = (0, _launchBrowser.getLocalCdpAddress)(port);
84
82
  }
85
83
  let hookResult;
86
84
  try {
85
+ const wsUrlResult = await (0, _launchBrowser.getCdpWebSocketUrl)(hookCdpUrl);
86
+ if (wsUrlResult.isOk()) {
87
+ hookCdpUrl = wsUrlResult.value;
88
+ } else {
89
+ throw new Error(wsUrlResult.error);
90
+ }
87
91
  hookResult = await setupContextHook({
88
92
  apiName: apiName,
89
93
  apiParameters: apiParameters,
90
94
  cdpUrl: hookCdpUrl
91
95
  });
92
96
  } catch (error) {
93
- return (0, _neverthrow.err)(new _errors.AutomationError(error));
97
+ return (0, _neverthrow.err)(new _runtimeInterface.AutomationError(error));
94
98
  }
95
99
  if (!hookResult) {
96
100
  return await fn(context, page);
@@ -114,7 +118,7 @@ async function withPlaywrightContext({
114
118
  await cleanup?.();
115
119
  } catch (e) {
116
120
  result = {
117
- return: (0, _neverthrow.err)(new _errors.AutomationError(e))
121
+ return: (0, _neverthrow.err)(new _runtimeInterface.AutomationError(e))
118
122
  };
119
123
  }
120
124
  if ("throw" in result) {
@@ -1,7 +1,5 @@
1
1
  import { Result } from "neverthrow";
2
- import { RunAutomationError } from "./errors";
3
- import { ExtendedRunApiParameters } from "./types";
4
- export type ImportFunction = ExtendedRunApiParameters["importFunction"];
2
+ import { ImportFunction, RunAutomationError } from "@intuned/runtime-interface";
5
3
  export declare function importUsingImportFunction<_ReturnType = any>({ path, allowGenerators, importFunction, }: {
6
4
  path: string;
7
5
  importFunction: ImportFunction;
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.importUsingImportFunction = importUsingImportFunction;
7
7
  var _neverthrow = require("neverthrow");
8
- var _errors = require("./errors");
8
+ var _runtimeInterface = require("../../vendor/runtime-interface");
9
9
  async function importUsingImportFunction({
10
10
  path,
11
11
  allowGenerators = true,
@@ -15,17 +15,17 @@ async function importUsingImportFunction({
15
15
  const importedResult = await importFunction(path);
16
16
  if (importedResult.isErr()) {
17
17
  if (importedResult.error.type === "not_found") {
18
- return (0, _neverthrow.err)(new _errors.ApiNotFoundError(path));
18
+ return (0, _neverthrow.err)(new _runtimeInterface.ApiNotFoundError(path));
19
19
  }
20
- return (0, _neverthrow.err)(new _errors.AutomationError(importedResult.error.error));
20
+ return (0, _neverthrow.err)(new _runtimeInterface.AutomationError(importedResult.error.error));
21
21
  }
22
22
  const imported = importedResult.value;
23
23
  if (!imported || !imported.default || !imported.default.constructor) {
24
- return (0, _neverthrow.err)(new _errors.InvalidApiError(`${path} does not have a default export`));
24
+ return (0, _neverthrow.err)(new _runtimeInterface.InvalidApiError(`${path} does not have a default export`));
25
25
  }
26
26
  if (imported.default.constructor.name === "AsyncGeneratorFunction") {
27
27
  if (!allowGenerators) {
28
- return (0, _neverthrow.err)(new _errors.InvalidApiError(`${path} default export must be an async function`));
28
+ return (0, _neverthrow.err)(new _runtimeInterface.InvalidApiError(`${path} default export must be an async function`));
29
29
  }
30
30
  return (0, _neverthrow.ok)(async (...args) => {
31
31
  const generator = imported.default(...args);
@@ -39,8 +39,8 @@ async function importUsingImportFunction({
39
39
  if (imported.default.constructor.name === "AsyncFunction") {
40
40
  return (0, _neverthrow.ok)(imported.default);
41
41
  }
42
- return (0, _neverthrow.err)(new _errors.InvalidApiError(`${path} default export must be an async function`));
42
+ return (0, _neverthrow.err)(new _runtimeInterface.InvalidApiError(`${path} default export must be an async function`));
43
43
  } catch (error) {
44
- return (0, _neverthrow.err)(new _errors.AutomationError(error));
44
+ return (0, _neverthrow.err)(new _runtimeInterface.AutomationError(error));
45
45
  }
46
46
  }
@@ -1,11 +1,8 @@
1
1
  import { Result } from "neverthrow";
2
- import { RunAutomationError } from "./errors";
3
- import { Page, BrowserContext } from "playwright";
4
- import { ExtendedRunApiParameters, RunApiResult, RunApiResultWithSessionOk } from "./types";
5
- export * from "./types";
6
- export * from "./errors";
2
+ import type { Page, BrowserContext } from "playwright";
3
+ import { ExtendedRunApiParameters, RunApiResult, RunApiResultWithSessionOk, RunAutomationError } from "@intuned/runtime-interface";
7
4
  export declare function runApi<ResultType = any>(input: ExtendedRunApiParameters & {
8
5
  retrieveSession: true;
9
- }): Promise<RunApiResult<ResultType, RunApiResultWithSessionOk>>;
6
+ }): Promise<RunApiResult<ResultType, RunApiResultWithSessionOk<ResultType>>>;
10
7
  export declare function runApi<ResultType = any>(input: ExtendedRunApiParameters): Promise<RunApiResult<ResultType>>;
11
8
  export declare function checkAuthSessionWithRetries(page: Page, context: BrowserContext, checkFn: (..._: any) => Promise<boolean>, retries?: number): Promise<Result<boolean, RunAutomationError>>;
@@ -3,49 +3,21 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- var _exportNames = {
7
- runApi: true,
8
- checkAuthSessionWithRetries: true
9
- };
10
6
  exports.checkAuthSessionWithRetries = checkAuthSessionWithRetries;
11
7
  exports.runApi = runApi;
12
8
  var _downloadDirectory = require("../../runtime/downloadDirectory");
13
9
  var _asyncLocalStorage = require("../asyncLocalStorage");
14
10
  var _fsExtra = _interopRequireWildcard(require("fs-extra"));
15
11
  var fs = _fsExtra;
16
- var _contextStorageStateHelpers = require("../contextStorageStateHelpers");
17
12
  var _neverthrow = require("neverthrow");
18
- var _errors = require("./errors");
19
- Object.keys(_errors).forEach(function (key) {
20
- if (key === "default" || key === "__esModule") return;
21
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
22
- if (key in exports && exports[key] === _errors[key]) return;
23
- Object.defineProperty(exports, key, {
24
- enumerable: true,
25
- get: function () {
26
- return _errors[key];
27
- }
28
- });
29
- });
30
13
  var _constants = require("../constants");
31
14
  var _playwrightContext = require("../playwrightContext");
32
- var _types = require("./types");
33
- Object.keys(_types).forEach(function (key) {
34
- if (key === "default" || key === "__esModule") return;
35
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
36
- if (key in exports && exports[key] === _types[key]) return;
37
- Object.defineProperty(exports, key, {
38
- enumerable: true,
39
- get: function () {
40
- return _types[key];
41
- }
42
- });
43
- });
44
15
  var _formatZodError = require("../formatZodError");
45
16
  var _cleanEnvironmentVariables = require("../cleanEnvironmentVariables");
46
17
  var _importUsingImportFunction = require("./importUsingImportFunction");
47
- function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
48
- function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
18
+ var _runtimeInterface = require("../../vendor/runtime-interface");
19
+ var _contextStorageStateHelpers = require("../contextStorageStateHelpers");
20
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
49
21
  function getObjectSizeInBytes(obj) {
50
22
  try {
51
23
  return new TextEncoder().encode(JSON.stringify(obj)).length;
@@ -60,9 +32,9 @@ async function runApi({
60
32
  ...input
61
33
  }) {
62
34
  let traceStarted = false;
63
- const inputParseResult = _types.runApiParametersSchema.safeParse(input);
35
+ const inputParseResult = _runtimeInterface.runApiParametersSchema.safeParse(input);
64
36
  if (!inputParseResult.success) {
65
- return (0, _neverthrow.err)(new _errors.InternalInvalidInputError("Input validation failed", (0, _formatZodError.formatZodError)(inputParseResult.error)));
37
+ return (0, _neverthrow.err)(new _runtimeInterface.InternalInvalidInputError("Input validation failed", (0, _formatZodError.formatZodError)(inputParseResult.error)));
66
38
  }
67
39
  const {
68
40
  automationFunction: {
@@ -92,7 +64,7 @@ async function runApi({
92
64
  if (auth && auth.session.type === "state") {
93
65
  const state = auth.session.state;
94
66
  if (state === undefined || state === null) {
95
- return (0, _neverthrow.err)(new _errors.AuthRequiredError());
67
+ return (0, _neverthrow.err)(new _runtimeInterface.AuthRequiredError());
96
68
  }
97
69
  }
98
70
  const playwrightContextParameters = {
@@ -122,13 +94,9 @@ async function runApi({
122
94
  context,
123
95
  session: auth.session
124
96
  });
125
- }
126
- const scriptContent = await fs.readFile(_playwrightContext.browserScriptsFile, "utf-8");
127
- await context.addInitScript({
128
- content: scriptContent
129
- });
130
- for (const page of context.pages()) {
131
- await page.evaluate(scriptContent);
97
+ if (intunedContext && auth.parameters) {
98
+ intunedContext.getAuthSessionParameters = async () => auth.parameters;
99
+ }
132
100
  }
133
101
  if (tracing.enabled) {
134
102
  await context.tracing.start({
@@ -141,19 +109,28 @@ async function runApi({
141
109
  (0, _cleanEnvironmentVariables.cleanEnvironmentVariables)();
142
110
  const automationFunctionParameters = [...(params !== undefined ? [params] : []), page, context];
143
111
  try {
144
- const automationFunctionResult = await automationFunction(...automationFunctionParameters);
112
+ const automationFunctionResult = await Promise.race([automationFunction(...automationFunctionParameters), abortPromise]);
113
+ if (automationFunctionResult === abortSymbol) {
114
+ return (0, _neverthrow.err)(new _runtimeInterface.AbortedError());
115
+ }
145
116
  const MAX_RESULT_SIZE_BYTES = 2 * 1024 * 1024;
146
117
  const resultSizeInBytes = getObjectSizeInBytes(automationFunctionResult);
147
118
  if (resultSizeInBytes > MAX_RESULT_SIZE_BYTES) {
148
- return (0, _neverthrow.err)(new _errors.ResultTooBigError(resultSizeInBytes, MAX_RESULT_SIZE_BYTES));
119
+ return (0, _neverthrow.err)(new _runtimeInterface.ResultTooBigError(resultSizeInBytes, MAX_RESULT_SIZE_BYTES));
120
+ }
121
+ if (retrieveSession) {
122
+ return (0, _neverthrow.ok)({
123
+ result: automationFunctionResult,
124
+ extendedPayloads: intunedContext?.extendedPayloads,
125
+ session: await (0, _contextStorageStateHelpers.getStorageState)(context)
126
+ });
149
127
  }
150
128
  return (0, _neverthrow.ok)({
151
129
  result: automationFunctionResult,
152
- extendedPayloads: intunedContext?.extendedPayloads,
153
- session: retrieveSession ? await (0, _contextStorageStateHelpers.getStorageState)(context) : undefined
130
+ extendedPayloads: intunedContext?.extendedPayloads
154
131
  });
155
132
  } catch (error) {
156
- return (0, _neverthrow.err)(new _errors.AutomationError(error));
133
+ return (0, _neverthrow.err)(new _runtimeInterface.AutomationError(error));
157
134
  } finally {
158
135
  await saveTraceIfNeeded({
159
136
  errorMessage: "failed to save trace"
@@ -178,22 +155,19 @@ async function runApi({
178
155
  } else {
179
156
  return await (0, _playwrightContext.withPlaywrightContext)({
180
157
  cdpAddress: runOptions.cdpAddress,
158
+ cdpTargetId: runOptions.cdpTargetId,
181
159
  ...playwrightContextParameters
182
160
  }, runAutomationWithContext);
183
161
  }
184
162
  }
185
- const result = await Promise.race([await runAutomation(), abortPromise]);
186
- if (result === abortSymbol) {
187
- return (0, _neverthrow.err)(new _errors.AbortedError());
188
- }
189
- return result;
163
+ return await runAutomation();
190
164
  }
191
165
  async function checkAuthSessionWithRetries(page, context, checkFn, retries = 3) {
192
166
  if (retries === 0) {
193
167
  return (0, _neverthrow.ok)(false);
194
168
  }
195
169
  let tryNumber = 0;
196
- console.log("Checking auth session with retries", `${_constants.AUTH_SESSIONS_FOLDER_NAME}/check`);
170
+ console.log("Checking AuthSession with retries", `${_constants.AUTH_SESSIONS_FOLDER_NAME}/check`);
197
171
  while (retries > tryNumber) {
198
172
  const result = await checkFn(page, context);
199
173
  if (result) return (0, _neverthrow.ok)(true);