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

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 (120) hide show
  1. package/.babelrc +6 -0
  2. package/CHANGELOG.md +1 -1
  3. package/InterfaceTemplate/{utils.ts → __utils.ts} +3 -1
  4. package/InterfaceTemplate/index.playwright.ts +1 -1
  5. package/bin/intuned +0 -0
  6. package/bin/intuned-interface +7 -0
  7. package/dist/commands/api/run.js +7 -11
  8. package/dist/commands/auth-sessions/load.js +2 -2
  9. package/dist/commands/auth-sessions/run-check.js +8 -8
  10. package/dist/commands/auth-sessions/run-create.js +6 -6
  11. package/dist/commands/build.js +2 -3
  12. package/dist/commands/common/browserUtils.d.ts +3 -3
  13. package/dist/commands/common/browserUtils.js +3 -4
  14. package/dist/commands/common/getFirstLineNumber.test.js +1 -2
  15. package/dist/commands/common/projectExclusions.js +1 -1
  16. package/dist/commands/common/tsNodeImport.d.ts +1 -1
  17. package/dist/commands/common/tsNodeImport.js +10 -2
  18. package/dist/commands/common/utils/fileUtils.js +1 -2
  19. package/dist/commands/common/utils/{unixSocket.d.ts → interfaceClient.d.ts} +10 -2
  20. package/dist/commands/common/utils/{unixSocket.js → interfaceClient.js} +16 -5
  21. package/dist/commands/common/utils/template.js +1 -2
  22. package/dist/commands/interface/run.js +23 -49
  23. package/dist/commands/intuned-cli/commands/attempt_api.command.js +1 -1
  24. package/dist/commands/intuned-cli/commands/attempt_authsession.command.js +1 -1
  25. package/dist/commands/intuned-cli/commands/attempt_authsession_check.command.js +2 -2
  26. package/dist/commands/intuned-cli/commands/attempt_authsession_create.command.js +1 -1
  27. package/dist/commands/intuned-cli/commands/authsession.command.js +1 -1
  28. package/dist/commands/intuned-cli/commands/authsession_record.command.js +1 -1
  29. package/dist/commands/intuned-cli/commands/authsession_scaffold.command.d.ts +1 -0
  30. package/dist/commands/intuned-cli/commands/authsession_scaffold.command.js +16 -0
  31. package/dist/commands/intuned-cli/commands/deploy.command.js +13 -9
  32. package/dist/commands/intuned-cli/commands/index.d.ts +2 -2
  33. package/dist/commands/intuned-cli/commands/index.js +15 -15
  34. package/dist/commands/intuned-cli/commands/{save.command.d.ts → provision.command.d.ts} +3 -3
  35. package/dist/commands/intuned-cli/commands/provision.command.js +50 -0
  36. package/dist/commands/intuned-cli/commands/run_api.command.js +1 -1
  37. package/dist/commands/intuned-cli/commands/run_authsession.command.d.ts +8 -1
  38. package/dist/commands/intuned-cli/commands/run_authsession.command.js +2 -2
  39. package/dist/commands/intuned-cli/commands/run_authsession_create.command.js +1 -1
  40. package/dist/commands/intuned-cli/commands/run_authsession_update.command.js +2 -2
  41. package/dist/commands/intuned-cli/commands/run_authsession_validate.command.js +2 -2
  42. package/dist/commands/intuned-cli/commands/types.d.ts +9 -1
  43. package/dist/commands/intuned-cli/commands/types.js +9 -5
  44. package/dist/commands/intuned-cli/controller/__test__/api.test.js +30 -19
  45. package/dist/commands/intuned-cli/controller/__test__/authSession.test.js +140 -77
  46. package/dist/commands/intuned-cli/controller/api.d.ts +2 -2
  47. package/dist/commands/intuned-cli/controller/api.js +6 -3
  48. package/dist/commands/intuned-cli/controller/authSession.d.ts +16 -16
  49. package/dist/commands/intuned-cli/controller/authSession.js +50 -39
  50. package/dist/commands/intuned-cli/controller/build.js +1 -2
  51. package/dist/commands/intuned-cli/controller/deploy.js +53 -12
  52. package/dist/commands/intuned-cli/controller/index.js +2 -3
  53. package/dist/commands/intuned-cli/controller/{save.d.ts → provision.d.ts} +5 -1
  54. package/dist/commands/intuned-cli/controller/provision.js +299 -0
  55. package/dist/commands/intuned-cli/controller/scaffold.d.ts +1 -0
  56. package/dist/commands/intuned-cli/controller/scaffold.js +77 -0
  57. package/dist/commands/intuned-cli/helpers/__test__/browser.test.js +45 -5
  58. package/dist/commands/intuned-cli/helpers/__test__/tracing.test.js +4 -3
  59. package/dist/commands/intuned-cli/helpers/api.js +4 -7
  60. package/dist/commands/intuned-cli/helpers/auth.d.ts +4 -4
  61. package/dist/commands/intuned-cli/helpers/auth.js +24 -21
  62. package/dist/commands/intuned-cli/helpers/backend.js +12 -4
  63. package/dist/commands/intuned-cli/helpers/browser.d.ts +4 -4
  64. package/dist/commands/intuned-cli/helpers/browser.js +40 -5
  65. package/dist/commands/intuned-cli/helpers/context.js +2 -2
  66. package/dist/commands/intuned-cli/helpers/errors.d.ts +1 -1
  67. package/dist/commands/intuned-cli/helpers/errors.js +2 -2
  68. package/dist/commands/intuned-cli/helpers/intunedJson.d.ts +6 -5
  69. package/dist/commands/intuned-cli/helpers/prompts.d.ts +3 -0
  70. package/dist/commands/intuned-cli/helpers/prompts.js +71 -0
  71. package/dist/commands/intuned-cli/helpers/timeout.js +2 -2
  72. package/dist/commands/intuned-cli/helpers/traces.d.ts +1 -1
  73. package/dist/commands/intuned-cli/helpers/wrapper.js +14 -4
  74. package/dist/commands/intuned-cli/main.js +1 -2
  75. package/dist/commands/intuned-cli/types.d.ts +41 -12
  76. package/dist/commands/intuned-cli/types.js +12 -2
  77. package/dist/commands/ts-check.js +1 -2
  78. package/dist/common/backendFunctions/getAuthSessionParameters.js +1 -1
  79. package/dist/common/binStartupScript.js +1 -2
  80. package/dist/common/browserTabs.d.ts +72 -0
  81. package/dist/common/browserTabs.js +74 -0
  82. package/dist/common/constants.d.ts +1 -0
  83. package/dist/common/constants.js +2 -1
  84. package/dist/common/contextStorageStateHelpers.d.ts +4 -3
  85. package/dist/common/contextStorageStateHelpers.js +4 -1
  86. package/dist/common/extension/extensionsHelpers.d.ts +1 -1
  87. package/dist/common/extension/types.d.ts +14 -7
  88. package/dist/common/formatZodError.d.ts +1 -1
  89. package/dist/common/intunedJson.d.ts +19 -14
  90. package/dist/common/intunedJson.js +4 -4
  91. package/dist/common/jwtTokenManager.js +10 -6
  92. package/dist/common/launchBrowser.d.ts +10 -0
  93. package/dist/common/launchBrowser.js +67 -7
  94. package/dist/common/playwrightContext.d.ts +5 -5
  95. package/dist/common/playwrightContext.js +24 -14
  96. package/dist/common/runApi/importUsingImportFunction.d.ts +1 -3
  97. package/dist/common/runApi/importUsingImportFunction.js +7 -7
  98. package/dist/common/runApi/index.d.ts +3 -6
  99. package/dist/common/runApi/index.js +28 -52
  100. package/dist/common/settingsSchema.d.ts +52 -45
  101. package/dist/common/settingsSchema.js +3 -3
  102. package/dist/common/setupContextHook.d.ts +1 -2
  103. package/dist/common/setupContextHook.js +2 -2
  104. package/dist/common/telemetry.js +1 -2
  105. package/dist/index.d.ts +0 -1
  106. package/dist/index.js +0 -7
  107. package/dist/runtime/downloadDirectory.js +2 -2
  108. package/dist/vendor/runtime-interface.d.ts +1 -0
  109. package/dist/vendor/runtime-interface.js +493 -0
  110. package/package.json +19 -11
  111. package/tsup.config.ts +12 -0
  112. package/WebTemplate.zip +0 -0
  113. package/dist/commands/intuned-cli/commands/init.command.d.ts +0 -1
  114. package/dist/commands/intuned-cli/commands/init.command.js +0 -13
  115. package/dist/commands/intuned-cli/commands/save.command.js +0 -42
  116. package/dist/commands/intuned-cli/controller/save.js +0 -357
  117. package/dist/common/runApi/errors.d.ts +0 -72
  118. package/dist/common/runApi/errors.js +0 -169
  119. package/dist/common/runApi/types.d.ts +0 -830
  120. package/dist/common/runApi/types.js +0 -73
@@ -4,7 +4,9 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.getBrowserExecutablePath = getBrowserExecutablePath;
7
+ exports.getCdpWebSocketUrl = getCdpWebSocketUrl;
7
8
  exports.getHeadlessUserAgent = getHeadlessUserAgent;
9
+ exports.getIgnoreHttpErrorsFromConfig = getIgnoreHttpErrorsFromConfig;
8
10
  exports.getLocalCdpAddress = getLocalCdpAddress;
9
11
  exports.launchBrowser = launchBrowser;
10
12
  exports.launchChromium = launchChromium;
@@ -17,9 +19,11 @@ var _child_process = require("child_process");
17
19
  var _extensionsHelpers = require("./extension/extensionsHelpers");
18
20
  var _intunedExtensionServer = require("./extension/intunedExtensionServer");
19
21
  var _util = require("util");
22
+ var _neverthrow = require("neverthrow");
23
+ var _zod = require("zod");
24
+ var _intunedJson = require("./intunedJson");
20
25
  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; }
26
+ 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
27
  const execAsync = (0, _util.promisify)(_child_process.exec);
24
28
  async function createUserDirWithPreferences() {
25
29
  const playwrightTempDir = await (0, _fsExtra.mkdtemp)("/tmp/pw-");
@@ -36,6 +40,22 @@ async function createUserDirWithPreferences() {
36
40
  await (0, _fsExtra.writeFile)((0, _path.join)(defaultDir, "Preferences"), JSON.stringify(preferences));
37
41
  return userDir;
38
42
  }
43
+ async function getIgnoreHttpErrorsFromConfig(cliOption) {
44
+ if (cliOption !== undefined) {
45
+ return cliOption;
46
+ }
47
+ const envValue = process.env.IGNORE_HTTP_ERRORS;
48
+ if (envValue !== undefined) {
49
+ return envValue.toLowerCase() === "true" || envValue === "1" || envValue.toLowerCase() === "yes";
50
+ }
51
+ try {
52
+ const intunedJsonResult = await (0, _intunedJson.loadIntunedJson)();
53
+ if (intunedJsonResult.isOk() && intunedJsonResult.value.ignoreHttpErrors !== undefined) {
54
+ return intunedJsonResult.value.ignoreHttpErrors;
55
+ }
56
+ } catch (error) {}
57
+ return false;
58
+ }
39
59
  async function launchChromium(options) {
40
60
  if ("cdpAddress" in options) {
41
61
  if (await (0, _extensionsHelpers.isIntunedExtensionEnabled)()) {
@@ -46,9 +66,22 @@ async function launchChromium(options) {
46
66
  throw new Error("No browser contexts found in the connected browser");
47
67
  }
48
68
  const context = browser.contexts()[0];
49
- let page = context.pages().at(0);
50
- if (!page) {
51
- page = await context.newPage();
69
+ let page = context.pages().at(0) ?? (await context.newPage());
70
+ const targetId = options.cdpTargetId;
71
+ if (targetId) {
72
+ for (const p of context.pages()) {
73
+ let cdp = null;
74
+ try {
75
+ cdp = await context.newCDPSession(p);
76
+ const result = await cdp.send("Target.getTargetInfo");
77
+ if (result.targetInfo.targetId === targetId) {
78
+ page = p;
79
+ break;
80
+ }
81
+ } catch (error) {} finally {
82
+ await cdp?.detach();
83
+ }
84
+ }
52
85
  }
53
86
  return {
54
87
  page,
@@ -60,7 +93,8 @@ async function launchChromium(options) {
60
93
  appModeInitialUrl,
61
94
  cdpPort,
62
95
  proxy,
63
- downloadsPath
96
+ downloadsPath,
97
+ ignoreHttpErrors
64
98
  } = options;
65
99
  let {
66
100
  executablePath
@@ -109,7 +143,8 @@ async function launchChromium(options) {
109
143
  proxy,
110
144
  downloadsPath,
111
145
  args: extraArgs,
112
- ignoreDefaultArgs: defaultArgsToIgnore
146
+ ignoreDefaultArgs: defaultArgsToIgnore,
147
+ ignoreHTTPSErrors: ignoreHttpErrors
113
148
  });
114
149
  context.once("close", async () => {
115
150
  try {
@@ -173,6 +208,31 @@ async function getBraveExecutablePath() {
173
208
  function getLocalCdpAddress(port) {
174
209
  return `http://localhost:${port}`;
175
210
  }
211
+ async function getCdpWebSocketUrl(cdpAddress) {
212
+ let response;
213
+ try {
214
+ response = await fetch(`${cdpAddress}/json/version`);
215
+ } catch (error) {
216
+ return (0, _neverthrow.err)(`Failed to fetch CDP version from ${cdpAddress}: ${error instanceof Error ? error.message : String(error)}`);
217
+ }
218
+ if (!response.ok) {
219
+ return (0, _neverthrow.err)(`Failed to get CDP WebSocket URL from ${cdpAddress}: ${response.status} ${response.statusText}`);
220
+ }
221
+ let data;
222
+ try {
223
+ data = await response.json();
224
+ } catch (error) {
225
+ return (0, _neverthrow.err)(`Invalid CDP version response from ${cdpAddress}: ${error instanceof Error ? error.message : String(error)}`);
226
+ }
227
+ const cdpJsonVersionResponseSchema = _zod.z.object({
228
+ webSocketDebuggerUrl: _zod.z.string().url()
229
+ });
230
+ const parseResult = cdpJsonVersionResponseSchema.safeParse(data);
231
+ if (!parseResult.success) {
232
+ return (0, _neverthrow.err)(`Invalid CDP version response from ${cdpAddress}: ${JSON.stringify(parseResult.error.format())}`);
233
+ }
234
+ return (0, _neverthrow.ok)(parseResult.data.webSocketDebuggerUrl);
235
+ }
176
236
  async function waitOnCdpAddress(cdpAddress) {
177
237
  const cdpAddressWithoutProtocol = cdpAddress.replace("http://", "").replace("https://", "").replace("localhost", "127.0.0.1");
178
238
  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;
@@ -20,9 +17,12 @@ export declare function withPlaywrightContext<R>(options: {
20
17
  proxy?: Proxy;
21
18
  headless: boolean;
22
19
  downloadsPath: string;
20
+ ignoreHttpErrors?: boolean;
23
21
  } & WithPlaywrightContextParameters, fn: WithPlaywrightContextWrappedFunction<R>): Promise<Ok<R, any> | Err<any, RunAutomationError>>;
24
22
  export declare function withPlaywrightContext<R>(options: {
25
23
  cdpAddress: string;
24
+ cdpTargetId?: string;
25
+ ignoreHttpErrors?: boolean;
26
26
  } & WithPlaywrightContextParameters, fn: WithPlaywrightContextWrappedFunction<R>): Promise<Ok<R, any> | Err<any, RunAutomationError>>;
27
27
  export declare function loadSessionToContext({ context, session, }: {
28
28
  context: playwright.BrowserContext;
@@ -3,27 +3,24 @@
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,
23
+ ignoreHttpErrors: cliIgnoreHttpErrors,
27
24
  importFunction,
28
25
  apiName,
29
26
  apiParameters
@@ -31,6 +28,7 @@ async function withPlaywrightContext({
31
28
  let context;
32
29
  let page;
33
30
  try {
31
+ const ignoreHttpErrors = await (0, _launchBrowser.getIgnoreHttpErrorsFromConfig)(cliIgnoreHttpErrors);
34
32
  const setupContextHookResult = importFunction ? await (0, _setupContextHook.loadSetupContextHook)({
35
33
  importFunction
36
34
  }) : (0, _neverthrow.ok)(null);
@@ -44,7 +42,9 @@ async function withPlaywrightContext({
44
42
  page,
45
43
  context
46
44
  } = await (0, _launchBrowser.launchBrowser)({
47
- cdpAddress
45
+ cdpAddress,
46
+ cdpTargetId,
47
+ ignoreHttpErrors
48
48
  }));
49
49
  } else {
50
50
  ({
@@ -53,7 +53,8 @@ async function withPlaywrightContext({
53
53
  } = await (0, _launchBrowser.launchBrowser)({
54
54
  proxy,
55
55
  headless,
56
- downloadsPath
56
+ downloadsPath,
57
+ ignoreHttpErrors
57
58
  }));
58
59
  }
59
60
  return await fn(context, page);
@@ -65,13 +66,14 @@ async function withPlaywrightContext({
65
66
  context,
66
67
  page
67
68
  } = await (0, _launchBrowser.launchBrowser)({
68
- cdpAddress
69
+ cdpAddress,
70
+ cdpTargetId,
71
+ ignoreHttpErrors
69
72
  }));
70
73
  } else {
71
74
  const port = await (0, _portfinder.getPort)({
72
75
  port: 9222
73
76
  });
74
- hookCdpUrl = (0, _launchBrowser.getLocalCdpAddress)(port);
75
77
  ({
76
78
  context,
77
79
  page
@@ -79,18 +81,26 @@ async function withPlaywrightContext({
79
81
  proxy,
80
82
  headless,
81
83
  downloadsPath,
82
- cdpPort: port
84
+ cdpPort: port,
85
+ ignoreHttpErrors
83
86
  }));
87
+ hookCdpUrl = (0, _launchBrowser.getLocalCdpAddress)(port);
84
88
  }
85
89
  let hookResult;
86
90
  try {
91
+ const wsUrlResult = await (0, _launchBrowser.getCdpWebSocketUrl)(hookCdpUrl);
92
+ if (wsUrlResult.isOk()) {
93
+ hookCdpUrl = wsUrlResult.value;
94
+ } else {
95
+ throw new Error(wsUrlResult.error);
96
+ }
87
97
  hookResult = await setupContextHook({
88
98
  apiName: apiName,
89
99
  apiParameters: apiParameters,
90
100
  cdpUrl: hookCdpUrl
91
101
  });
92
102
  } catch (error) {
93
- return (0, _neverthrow.err)(new _errors.AutomationError(error));
103
+ return (0, _neverthrow.err)(new _runtimeInterface.AutomationError(error));
94
104
  }
95
105
  if (!hookResult) {
96
106
  return await fn(context, page);
@@ -114,7 +124,7 @@ async function withPlaywrightContext({
114
124
  await cleanup?.();
115
125
  } catch (e) {
116
126
  result = {
117
- return: (0, _neverthrow.err)(new _errors.AutomationError(e))
127
+ return: (0, _neverthrow.err)(new _runtimeInterface.AutomationError(e))
118
128
  };
119
129
  }
120
130
  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"
@@ -170,6 +147,7 @@ async function runApi({
170
147
  headless: runOptions.headless,
171
148
  proxy: runOptions.proxy,
172
149
  downloadsPath,
150
+ ignoreHttpErrors: runOptions.ignoreHttpErrors,
173
151
  ...playwrightContextParameters
174
152
  }, runAutomationWithContext);
175
153
  } finally {
@@ -178,22 +156,20 @@ async function runApi({
178
156
  } else {
179
157
  return await (0, _playwrightContext.withPlaywrightContext)({
180
158
  cdpAddress: runOptions.cdpAddress,
159
+ cdpTargetId: runOptions.cdpTargetId,
160
+ ignoreHttpErrors: runOptions.ignoreHttpErrors,
181
161
  ...playwrightContextParameters
182
162
  }, runAutomationWithContext);
183
163
  }
184
164
  }
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;
165
+ return await runAutomation();
190
166
  }
191
167
  async function checkAuthSessionWithRetries(page, context, checkFn, retries = 3) {
192
168
  if (retries === 0) {
193
169
  return (0, _neverthrow.ok)(false);
194
170
  }
195
171
  let tryNumber = 0;
196
- console.log("Checking auth session with retries", `${_constants.AUTH_SESSIONS_FOLDER_NAME}/check`);
172
+ console.log("Checking AuthSession with retries", `${_constants.AUTH_SESSIONS_FOLDER_NAME}/check`);
197
173
  while (retries > tryNumber) {
198
174
  const result = await checkFn(page, context);
199
175
  if (result) return (0, _neverthrow.ok)(true);