@intuned/runtime-dev 1.3.18-interface.13 → 1.3.18-interface.4

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 (230) hide show
  1. package/.babelrc +21 -0
  2. package/.eslintignore +10 -0
  3. package/.eslintrc.js +39 -0
  4. package/.turbo/turbo-build.log +2 -0
  5. package/WebTemplate.zip +0 -0
  6. package/dist/commands/api/run.d.ts +1 -3
  7. package/dist/commands/api/run.js +65 -2039
  8. package/dist/commands/auth-sessions/load.d.ts +1 -0
  9. package/dist/commands/auth-sessions/load.js +28 -1552
  10. package/dist/commands/auth-sessions/run-check.d.ts +1 -0
  11. package/dist/commands/auth-sessions/run-check.js +72 -1961
  12. package/dist/commands/auth-sessions/run-create.d.ts +1 -0
  13. package/dist/commands/auth-sessions/run-create.js +66 -1955
  14. package/dist/commands/browser/save-state.d.ts +1 -0
  15. package/dist/commands/browser/save-state.js +11 -102
  16. package/dist/commands/browser/start-browser.d.ts +1 -0
  17. package/dist/commands/browser/start-browser.js +9 -62
  18. package/dist/commands/build.d.ts +1 -2
  19. package/dist/commands/build.js +53 -172
  20. package/dist/commands/common/browserUtils.d.ts +14 -0
  21. package/dist/commands/common/browserUtils.js +58 -0
  22. package/dist/commands/common/getFirstLineNumber.d.ts +9 -0
  23. package/dist/commands/common/getFirstLineNumber.js +101 -0
  24. package/dist/commands/common/getFirstLineNumber.test.js +228 -0
  25. package/dist/commands/common/projectExclusions.d.ts +2 -0
  26. package/dist/commands/common/projectExclusions.js +8 -0
  27. package/dist/commands/common/sendMessageToClient.d.ts +1 -0
  28. package/dist/commands/common/sendMessageToClient.js +10 -0
  29. package/dist/commands/common/tsNodeImport.d.ts +2 -5
  30. package/dist/commands/common/tsNodeImport.js +16 -55
  31. package/dist/commands/common/utils/fileUtils.d.ts +6 -0
  32. package/dist/commands/common/utils/fileUtils.js +33 -0
  33. package/dist/commands/common/utils/interfaceClient.d.ts +31 -0
  34. package/dist/commands/common/utils/interfaceClient.js +99 -0
  35. package/dist/commands/common/utils/settings.d.ts +2 -0
  36. package/dist/commands/common/utils/settings.js +31 -0
  37. package/dist/commands/common/utils/template.d.ts +2 -0
  38. package/dist/commands/common/utils/template.js +31 -0
  39. package/dist/commands/get-headless-user-agent.d.ts +1 -2
  40. package/dist/commands/get-headless-user-agent.js +12 -348
  41. package/dist/commands/interface/pingPong.d.ts +2 -0
  42. package/dist/commands/interface/pingPong.js +11 -0
  43. package/dist/commands/interface/run.d.ts +1 -3
  44. package/dist/commands/interface/run.js +140 -2034
  45. package/dist/commands/interface/runApi.d.ts +1 -0
  46. package/dist/commands/interface/runApi.js +102 -0
  47. package/dist/commands/intuned-cli/commands/attempt.command.d.ts +1 -0
  48. package/dist/commands/intuned-cli/commands/attempt.command.js +8 -0
  49. package/dist/commands/intuned-cli/commands/attempt_api.command.d.ts +1 -0
  50. package/dist/commands/intuned-cli/commands/attempt_api.command.js +30 -0
  51. package/dist/commands/intuned-cli/commands/attempt_authsession.command.d.ts +1 -0
  52. package/dist/commands/intuned-cli/commands/attempt_authsession.command.js +8 -0
  53. package/dist/commands/intuned-cli/commands/attempt_authsession_check.command.d.ts +1 -0
  54. package/dist/commands/intuned-cli/commands/attempt_authsession_check.command.js +19 -0
  55. package/dist/commands/intuned-cli/commands/attempt_authsession_create.command.d.ts +1 -0
  56. package/dist/commands/intuned-cli/commands/attempt_authsession_create.command.js +24 -0
  57. package/dist/commands/intuned-cli/commands/authsession.command.d.ts +1 -0
  58. package/dist/commands/intuned-cli/commands/authsession.command.js +8 -0
  59. package/dist/commands/intuned-cli/commands/authsession_record.command.d.ts +1 -0
  60. package/dist/commands/intuned-cli/commands/authsession_record.command.js +32 -0
  61. package/dist/commands/intuned-cli/commands/authsession_scaffold.command.d.ts +1 -0
  62. package/dist/commands/intuned-cli/commands/authsession_scaffold.command.js +16 -0
  63. package/dist/commands/intuned-cli/commands/build.command.d.ts +1 -0
  64. package/dist/commands/intuned-cli/commands/build.command.js +12 -0
  65. package/dist/commands/intuned-cli/commands/command.d.ts +2 -0
  66. package/dist/commands/intuned-cli/commands/command.js +9 -0
  67. package/dist/commands/intuned-cli/commands/deploy.command.d.ts +1 -0
  68. package/dist/commands/intuned-cli/commands/deploy.command.js +42 -0
  69. package/dist/commands/intuned-cli/commands/index.d.ts +18 -0
  70. package/dist/commands/intuned-cli/commands/index.js +203 -0
  71. package/dist/commands/intuned-cli/commands/provision.command.d.ts +13 -0
  72. package/dist/commands/intuned-cli/commands/provision.command.js +50 -0
  73. package/dist/commands/intuned-cli/commands/run.command.d.ts +1 -0
  74. package/dist/commands/intuned-cli/commands/run.command.js +8 -0
  75. package/dist/commands/intuned-cli/commands/run_api.command.d.ts +1 -0
  76. package/dist/commands/intuned-cli/commands/run_api.command.js +46 -0
  77. package/dist/commands/intuned-cli/commands/run_authsession.command.d.ts +34 -0
  78. package/dist/commands/intuned-cli/commands/run_authsession.command.js +17 -0
  79. package/dist/commands/intuned-cli/commands/run_authsession_create.command.d.ts +1 -0
  80. package/dist/commands/intuned-cli/commands/run_authsession_create.command.js +35 -0
  81. package/dist/commands/intuned-cli/commands/run_authsession_update.command.d.ts +1 -0
  82. package/dist/commands/intuned-cli/commands/run_authsession_update.command.js +35 -0
  83. package/dist/commands/intuned-cli/commands/run_authsession_validate.command.d.ts +1 -0
  84. package/dist/commands/intuned-cli/commands/run_authsession_validate.command.js +33 -0
  85. package/dist/commands/intuned-cli/commands/types.d.ts +39 -0
  86. package/dist/commands/intuned-cli/commands/types.js +33 -0
  87. package/dist/commands/intuned-cli/constants/index.d.ts +17 -0
  88. package/dist/commands/intuned-cli/constants/index.js +25 -0
  89. package/dist/commands/intuned-cli/controller/__test__/api.test.js +398 -0
  90. package/dist/commands/intuned-cli/controller/__test__/authSession.test.js +1060 -0
  91. package/dist/commands/intuned-cli/controller/api.d.ts +42 -0
  92. package/dist/commands/intuned-cli/controller/api.js +195 -0
  93. package/dist/commands/intuned-cli/controller/authSession.d.ts +209 -0
  94. package/dist/commands/intuned-cli/controller/authSession.js +425 -0
  95. package/dist/commands/intuned-cli/controller/build.d.ts +1 -0
  96. package/dist/commands/intuned-cli/controller/build.js +36 -0
  97. package/dist/commands/intuned-cli/controller/deploy.d.ts +5 -0
  98. package/dist/commands/intuned-cli/controller/deploy.js +318 -0
  99. package/dist/commands/intuned-cli/controller/index.d.ts +1 -0
  100. package/dist/commands/intuned-cli/controller/index.js +46 -0
  101. package/dist/commands/intuned-cli/controller/provision.d.ts +21 -0
  102. package/dist/commands/intuned-cli/controller/provision.js +300 -0
  103. package/dist/commands/intuned-cli/controller/scaffold.d.ts +1 -0
  104. package/dist/commands/intuned-cli/controller/scaffold.js +77 -0
  105. package/dist/commands/intuned-cli/helpers/__test__/browser.test.js +152 -0
  106. package/dist/commands/intuned-cli/helpers/__test__/tracing.test.js +41 -0
  107. package/dist/commands/intuned-cli/helpers/api.d.ts +3 -0
  108. package/dist/commands/intuned-cli/helpers/api.js +16 -0
  109. package/dist/commands/intuned-cli/helpers/auth.d.ts +46 -0
  110. package/dist/commands/intuned-cli/helpers/auth.js +147 -0
  111. package/dist/commands/intuned-cli/helpers/backend.d.ts +8 -0
  112. package/dist/commands/intuned-cli/helpers/backend.js +35 -0
  113. package/dist/commands/intuned-cli/helpers/browser.d.ts +14 -0
  114. package/dist/commands/intuned-cli/helpers/browser.js +93 -0
  115. package/dist/commands/intuned-cli/helpers/context.d.ts +3 -0
  116. package/dist/commands/intuned-cli/helpers/context.js +33 -0
  117. package/dist/commands/intuned-cli/helpers/errors.d.ts +16 -0
  118. package/dist/commands/intuned-cli/helpers/errors.js +44 -0
  119. package/dist/commands/intuned-cli/helpers/index.d.ts +12 -0
  120. package/dist/commands/intuned-cli/helpers/index.js +137 -0
  121. package/dist/commands/intuned-cli/helpers/intunedJson.d.ts +39 -0
  122. package/dist/commands/intuned-cli/helpers/intunedJson.js +20 -0
  123. package/dist/commands/intuned-cli/helpers/prompts.d.ts +3 -0
  124. package/dist/commands/intuned-cli/helpers/prompts.js +71 -0
  125. package/dist/commands/intuned-cli/helpers/proxy.d.ts +5 -0
  126. package/dist/commands/intuned-cli/helpers/proxy.js +23 -0
  127. package/dist/commands/intuned-cli/helpers/terminal.d.ts +5 -0
  128. package/dist/commands/intuned-cli/helpers/terminal.js +13 -0
  129. package/dist/commands/intuned-cli/helpers/timeout.d.ts +1 -0
  130. package/dist/commands/intuned-cli/helpers/timeout.js +23 -0
  131. package/dist/commands/intuned-cli/helpers/traces.d.ts +2 -0
  132. package/dist/commands/intuned-cli/helpers/traces.js +32 -0
  133. package/dist/commands/intuned-cli/helpers/validation.d.ts +2 -0
  134. package/dist/commands/intuned-cli/helpers/validation.js +14 -0
  135. package/dist/commands/intuned-cli/helpers/wrapper.d.ts +2 -0
  136. package/dist/commands/intuned-cli/helpers/wrapper.js +76 -0
  137. package/dist/commands/intuned-cli/index.d.ts +1 -0
  138. package/dist/commands/intuned-cli/index.js +16 -0
  139. package/dist/commands/intuned-cli/main.d.ts +1 -2
  140. package/dist/commands/intuned-cli/main.js +25 -5392
  141. package/dist/commands/intuned-cli/types.d.ts +70 -0
  142. package/dist/commands/intuned-cli/types.js +22 -0
  143. package/dist/commands/ts-check.d.ts +1 -0
  144. package/dist/commands/ts-check.js +29 -131
  145. package/dist/common/Logger/Logger/index.d.ts +12 -0
  146. package/dist/common/Logger/Logger/index.js +60 -0
  147. package/dist/common/Logger/Logger/types.d.ts +8 -0
  148. package/dist/common/Logger/Logger/types.js +5 -0
  149. package/dist/common/Logger/index.d.ts +12 -0
  150. package/dist/common/Logger/index.js +60 -0
  151. package/dist/common/Logger/types.d.ts +8 -0
  152. package/dist/common/Logger/types.js +5 -0
  153. package/dist/common/asyncLocalStorage/index.d.ts +8 -9
  154. package/dist/common/asyncLocalStorage/index.js +9 -33
  155. package/dist/common/backendFunctions/getAuthSessionParameters.d.ts +1 -0
  156. package/dist/common/backendFunctions/getAuthSessionParameters.js +38 -0
  157. package/dist/common/binStartupScript.d.ts +1 -2
  158. package/dist/common/binStartupScript.js +11 -126
  159. package/dist/common/browserTabs.d.ts +72 -0
  160. package/dist/common/browserTabs.js +74 -0
  161. package/dist/common/cleanEnvironmentVariables.d.ts +1 -3
  162. package/dist/common/cleanEnvironmentVariables.js +7 -29
  163. package/dist/common/constants.d.ts +11 -13
  164. package/dist/common/constants.js +15 -57
  165. package/dist/common/contextStorageStateHelpers.d.ts +6 -8
  166. package/dist/common/contextStorageStateHelpers.js +22 -46
  167. package/dist/common/extension/extensionsHelpers.d.ts +11 -0
  168. package/dist/common/extension/extensionsHelpers.js +147 -0
  169. package/dist/common/extension/intunedExtensionServer.d.ts +24 -0
  170. package/dist/common/extension/intunedExtensionServer.js +178 -0
  171. package/dist/common/extension/types.d.ts +212 -0
  172. package/dist/common/extension/types.js +51 -0
  173. package/dist/common/formatZodError.d.ts +2 -0
  174. package/dist/common/formatZodError.js +18 -0
  175. package/dist/common/intunedJson.d.ts +229 -0
  176. package/dist/common/intunedJson.js +133 -0
  177. package/dist/common/jwtTokenManager.d.ts +4 -6
  178. package/dist/common/jwtTokenManager.js +41 -107
  179. package/dist/common/launchBrowser.d.ts +34 -0
  180. package/dist/common/launchBrowser.js +248 -0
  181. package/dist/common/playwrightContext.d.ts +30 -0
  182. package/dist/common/playwrightContext.js +153 -0
  183. package/dist/common/runApi/importUsingImportFunction.d.ts +7 -0
  184. package/dist/common/runApi/importUsingImportFunction.js +46 -0
  185. package/dist/common/runApi/index.d.ts +6 -9
  186. package/dist/common/runApi/index.js +78 -1765
  187. package/dist/common/settingsSchema.d.ts +51 -58
  188. package/dist/common/settingsSchema.js +20 -54
  189. package/dist/common/setupContextHook.d.ts +16 -0
  190. package/dist/common/setupContextHook.js +22 -0
  191. package/dist/common/telemetry.d.ts +3 -6
  192. package/dist/common/telemetry.js +9 -40
  193. package/dist/index.d.ts +4 -6
  194. package/dist/index.js +92 -783
  195. package/dist/runtime/RunError.d.ts +5 -0
  196. package/dist/runtime/RunError.js +19 -0
  197. package/dist/runtime/attemptStore.d.ts +2 -0
  198. package/dist/runtime/attemptStore.js +23 -0
  199. package/dist/runtime/captcha.d.ts +15 -0
  200. package/dist/runtime/captcha.js +191 -0
  201. package/dist/runtime/captcha.test.js +821 -0
  202. package/dist/runtime/downloadDirectory.d.ts +1 -0
  203. package/dist/runtime/downloadDirectory.js +19 -0
  204. package/dist/runtime/enums.d.js +5 -0
  205. package/dist/runtime/enums.d.ts +11 -0
  206. package/dist/runtime/enums.js +18 -0
  207. package/dist/runtime/executionHelpers.test.js +52 -0
  208. package/dist/runtime/export.d.js +5 -0
  209. package/dist/runtime/export.d.ts +284 -0
  210. package/dist/runtime/extendPayload.d.ts +2 -0
  211. package/dist/runtime/extendPayload.js +21 -0
  212. package/dist/runtime/extendTimeout.d.ts +1 -0
  213. package/dist/runtime/extendTimeout.js +23 -0
  214. package/dist/runtime/getAiGatewayConfig.d.ts +10 -0
  215. package/dist/runtime/getAiGatewayConfig.js +16 -0
  216. package/dist/runtime/getAuthSessionParameters.d.ts +1 -0
  217. package/dist/runtime/getAuthSessionParameters.js +20 -0
  218. package/dist/runtime/index.d.ts +10 -168
  219. package/dist/runtime/index.js +88 -778
  220. package/dist/runtime/persistentStore.d.ts +2 -0
  221. package/dist/runtime/persistentStore.js +37 -0
  222. package/dist/runtime/persistentStore.test.js +101 -0
  223. package/dist/runtime/runInfo.d.ts +2 -0
  224. package/dist/runtime/runInfo.js +21 -0
  225. package/package.json +16 -77
  226. package/template.tsconfig.json +11 -0
  227. package/tsconfig.eslint.json +5 -0
  228. package/tsconfig.json +25 -0
  229. package/typedoc.json +49 -0
  230. package/dist/export.d-BAUMB-lG.d.ts +0 -140
@@ -0,0 +1,248 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getBrowserExecutablePath = getBrowserExecutablePath;
7
+ exports.getCdpWebSocketUrl = getCdpWebSocketUrl;
8
+ exports.getHeadlessUserAgent = getHeadlessUserAgent;
9
+ exports.getLocalCdpAddress = getLocalCdpAddress;
10
+ exports.launchBrowser = launchBrowser;
11
+ exports.launchChromium = launchChromium;
12
+ var playwright = _interopRequireWildcard(require("playwright"));
13
+ var _fsExtra = _interopRequireWildcard(require("fs-extra"));
14
+ var fs = _fsExtra;
15
+ var _path = require("path");
16
+ var _waitOn = _interopRequireDefault(require("wait-on"));
17
+ var _child_process = require("child_process");
18
+ var _extensionsHelpers = require("./extension/extensionsHelpers");
19
+ var _intunedExtensionServer = require("./extension/intunedExtensionServer");
20
+ var _util = require("util");
21
+ var _neverthrow = require("neverthrow");
22
+ var _zod = require("zod");
23
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
24
+ 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); }
25
+ 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
+ const execAsync = (0, _util.promisify)(_child_process.exec);
27
+ async function createUserDirWithPreferences() {
28
+ const playwrightTempDir = await (0, _fsExtra.mkdtemp)("/tmp/pw-");
29
+ const userDir = (0, _path.join)(playwrightTempDir, "userdir");
30
+ const defaultDir = (0, _path.join)(userDir, "Default");
31
+ await (0, _fsExtra.mkdir)(defaultDir, {
32
+ recursive: true
33
+ });
34
+ const preferences = {
35
+ plugins: {
36
+ always_open_pdf_externally: true
37
+ }
38
+ };
39
+ await (0, _fsExtra.writeFile)((0, _path.join)(defaultDir, "Preferences"), JSON.stringify(preferences));
40
+ return userDir;
41
+ }
42
+ async function launchChromium(options) {
43
+ if ("cdpAddress" in options) {
44
+ if (await (0, _extensionsHelpers.isIntunedExtensionEnabled)()) {
45
+ await (0, _intunedExtensionServer.setupIntunedExtensionServer)();
46
+ }
47
+ const browser = await playwright.chromium.connectOverCDP(options.cdpAddress);
48
+ if (browser.contexts().length === 0) {
49
+ throw new Error("No browser contexts found in the connected browser");
50
+ }
51
+ const context = browser.contexts()[0];
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
+ }
68
+ }
69
+ return {
70
+ page,
71
+ context
72
+ };
73
+ }
74
+ const {
75
+ headless,
76
+ appModeInitialUrl,
77
+ cdpPort,
78
+ proxy,
79
+ downloadsPath
80
+ } = options;
81
+ let {
82
+ executablePath
83
+ } = options;
84
+ const defaultArgsToIgnore = ["--disable-extensions", "--disable-component-extensions-with-background-pages", "--disable-background-networking", "--disable-backgrounding-occluded-windows", "--disable-background-timer-throttling"];
85
+ const extraArgs = [];
86
+ const userDataDir = await createUserDirWithPreferences();
87
+ if ((0, _extensionsHelpers.isIntunedExtensionLoaded)()) {
88
+ const extensionsList = (0, _extensionsHelpers.buildExtensionsList)();
89
+ const extensions = extensionsList.join(",");
90
+ extraArgs.push(`--disable-extensions-except=${extensions}`);
91
+ extraArgs.push(`--load-extension=${extensions}`);
92
+ }
93
+ if (await (0, _extensionsHelpers.isIntunedExtensionEnabled)()) {
94
+ await (0, _extensionsHelpers.setupIntunedExtension)();
95
+ if (proxy) {
96
+ extraArgs.push('--proxy-bypass-list="<-loopback>"');
97
+ }
98
+ }
99
+ if (cdpPort) {
100
+ extraArgs.push(`--remote-debugging-port=${cdpPort}`);
101
+ }
102
+ if (headless) {
103
+ defaultArgsToIgnore.push("--headless=old");
104
+ extraArgs.push("--headless=new");
105
+ }
106
+ if (appModeInitialUrl) {
107
+ extraArgs.push(`--app=${appModeInitialUrl}`);
108
+ }
109
+ if (executablePath) {
110
+ executablePath = await fs.realpath(executablePath);
111
+ if (!(await fs.exists(executablePath))) {
112
+ console.log(`Warning: Executable path ${executablePath} does not exist. Falling back to default.`);
113
+ executablePath = undefined;
114
+ }
115
+ }
116
+ const viewport = null;
117
+ const userAgent = process.env.__PLAYWRIGHT_USER_AGENT_OVERRIDE ?? (await getHeadlessUserAgent({
118
+ executablePath
119
+ }));
120
+ const context = await playwright.chromium.launchPersistentContext(userDataDir, {
121
+ userAgent,
122
+ executablePath,
123
+ headless,
124
+ viewport,
125
+ proxy,
126
+ downloadsPath,
127
+ args: extraArgs,
128
+ ignoreDefaultArgs: defaultArgsToIgnore
129
+ });
130
+ context.once("close", async () => {
131
+ try {
132
+ await (0, _fsExtra.rm)(userDataDir, {
133
+ recursive: true,
134
+ force: true,
135
+ retryDelay: 1000,
136
+ maxRetries: 5
137
+ });
138
+ if (await (0, _extensionsHelpers.isIntunedExtensionEnabled)()) {
139
+ await (0, _intunedExtensionServer.cleanIntunedExtensionServer)();
140
+ }
141
+ } catch (error) {
142
+ console.error("Failed to remove user data dir", error);
143
+ }
144
+ });
145
+ if (cdpPort) {
146
+ const createdCdpAddress = getLocalCdpAddress(cdpPort);
147
+ await waitOnCdpAddress(createdCdpAddress);
148
+ }
149
+ const page = context.pages().at(0) ?? (await context.newPage());
150
+ if (await (0, _extensionsHelpers.isIntunedExtensionEnabled)()) {
151
+ await (0, _extensionsHelpers.getIntunedExtensionWorker)(context);
152
+ }
153
+ return {
154
+ page,
155
+ context
156
+ };
157
+ }
158
+ async function getBrowserExecutablePath() {
159
+ const browserType = getBrowserType();
160
+ if (browserType === "brave") {
161
+ return await getBraveExecutablePath();
162
+ }
163
+ }
164
+ async function launchBrowser(options) {
165
+ if ("cdpAddress" in options) {
166
+ return launchChromium(options);
167
+ }
168
+ return launchChromium({
169
+ ...options,
170
+ executablePath: await getBrowserExecutablePath()
171
+ });
172
+ }
173
+ function getBrowserType() {
174
+ if (process.env.BROWSER_TYPE === "brave") {
175
+ return "brave";
176
+ }
177
+ return "chromium";
178
+ }
179
+ async function getBraveExecutablePath() {
180
+ const {
181
+ stdout
182
+ } = await execAsync("which brave-browser-stable");
183
+ const bravePath = stdout.trim();
184
+ if (bravePath.length === 0) {
185
+ throw new Error("Brave browser not found");
186
+ }
187
+ return bravePath;
188
+ }
189
+ function getLocalCdpAddress(port) {
190
+ return `http://localhost:${port}`;
191
+ }
192
+ async function getCdpWebSocketUrl(cdpAddress) {
193
+ let response;
194
+ try {
195
+ response = await fetch(`${cdpAddress}/json/version`);
196
+ } catch (error) {
197
+ return (0, _neverthrow.err)(`Failed to fetch CDP version from ${cdpAddress}: ${error instanceof Error ? error.message : String(error)}`);
198
+ }
199
+ if (!response.ok) {
200
+ return (0, _neverthrow.err)(`Failed to get CDP WebSocket URL from ${cdpAddress}: ${response.status} ${response.statusText}`);
201
+ }
202
+ let data;
203
+ try {
204
+ data = await response.json();
205
+ } catch (error) {
206
+ return (0, _neverthrow.err)(`Invalid CDP version response from ${cdpAddress}: ${error instanceof Error ? error.message : String(error)}`);
207
+ }
208
+ const cdpJsonVersionResponseSchema = _zod.z.object({
209
+ webSocketDebuggerUrl: _zod.z.string().url()
210
+ });
211
+ const parseResult = cdpJsonVersionResponseSchema.safeParse(data);
212
+ if (!parseResult.success) {
213
+ return (0, _neverthrow.err)(`Invalid CDP version response from ${cdpAddress}: ${JSON.stringify(parseResult.error.format())}`);
214
+ }
215
+ return (0, _neverthrow.ok)(parseResult.data.webSocketDebuggerUrl);
216
+ }
217
+ async function waitOnCdpAddress(cdpAddress) {
218
+ const cdpAddressWithoutProtocol = cdpAddress.replace("http://", "").replace("https://", "").replace("localhost", "127.0.0.1");
219
+ await (0, _waitOn.default)({
220
+ resources: [`http-get://${cdpAddressWithoutProtocol}/json/version`],
221
+ delay: 100,
222
+ interval: 100,
223
+ timeout: 5000,
224
+ tcpTimeout: 1000,
225
+ window: 1000
226
+ });
227
+ }
228
+ async function getHeadlessUserAgent({
229
+ executablePath,
230
+ args,
231
+ ignoreDefaultArgs
232
+ }) {
233
+ const browser = await playwright.chromium.launch({
234
+ headless: true,
235
+ executablePath,
236
+ args,
237
+ ignoreDefaultArgs
238
+ });
239
+ const context = await browser.newContext();
240
+ const page = await context.newPage();
241
+ let userAgent = await page.evaluate(() => navigator.userAgent);
242
+ await browser.close();
243
+ if (!userAgent || typeof userAgent !== "string") {
244
+ return undefined;
245
+ }
246
+ userAgent = userAgent.replace("HeadlessChrome", "Chrome");
247
+ return userAgent;
248
+ }
@@ -0,0 +1,30 @@
1
+ import type * as playwright from "playwright";
2
+ import { Err, Ok } from "neverthrow";
3
+ import { type Proxy } from "./launchBrowser";
4
+ import { ImportFunction, RunAutomationError, RunApiSession } from "@intuned/runtime-interface";
5
+ export declare const browserScriptsFile: string;
6
+ type WithPlaywrightContextParameters = {
7
+ importFunction: ImportFunction;
8
+ apiName: string;
9
+ apiParameters: any;
10
+ } | {
11
+ importFunction?: undefined;
12
+ apiName?: undefined;
13
+ apiParameters?: undefined;
14
+ };
15
+ type WithPlaywrightContextWrappedFunctionReturn<R> = Ok<R, any> | Err<any, RunAutomationError>;
16
+ export type WithPlaywrightContextWrappedFunction<R> = (context: playwright.BrowserContext, page: playwright.Page) => Promise<WithPlaywrightContextWrappedFunctionReturn<R>>;
17
+ export declare function withPlaywrightContext<R>(options: {
18
+ proxy?: Proxy;
19
+ headless: boolean;
20
+ downloadsPath: string;
21
+ } & WithPlaywrightContextParameters, fn: WithPlaywrightContextWrappedFunction<R>): Promise<Ok<R, any> | Err<any, RunAutomationError>>;
22
+ export declare function withPlaywrightContext<R>(options: {
23
+ cdpAddress: string;
24
+ cdpTargetId?: string;
25
+ } & WithPlaywrightContextParameters, fn: WithPlaywrightContextWrappedFunction<R>): Promise<Ok<R, any> | Err<any, RunAutomationError>>;
26
+ export declare function loadSessionToContext({ context, session, }: {
27
+ context: playwright.BrowserContext;
28
+ session: RunApiSession;
29
+ }): Promise<void>;
30
+ export {};
@@ -0,0 +1,153 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.browserScriptsFile = void 0;
7
+ exports.loadSessionToContext = loadSessionToContext;
8
+ exports.withPlaywrightContext = withPlaywrightContext;
9
+ var _contextStorageStateHelpers = require("./contextStorageStateHelpers");
10
+ var _path = _interopRequireDefault(require("path"));
11
+ var fs = _interopRequireWildcard(require("fs-extra"));
12
+ var _fileUtils = require("../commands/common/utils/fileUtils");
13
+ var _neverthrow = require("neverthrow");
14
+ var _setupContextHook = require("./setupContextHook");
15
+ var _portfinder = require("portfinder");
16
+ var _launchBrowser = require("./launchBrowser");
17
+ var _runtimeInterface = require("@intuned/runtime-interface");
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");
22
+ async function withPlaywrightContext({
23
+ cdpAddress,
24
+ cdpTargetId,
25
+ proxy,
26
+ headless = true,
27
+ downloadsPath,
28
+ importFunction,
29
+ apiName,
30
+ apiParameters
31
+ }, fn) {
32
+ let context;
33
+ let page;
34
+ try {
35
+ const setupContextHookResult = importFunction ? await (0, _setupContextHook.loadSetupContextHook)({
36
+ importFunction
37
+ }) : (0, _neverthrow.ok)(null);
38
+ if (setupContextHookResult.isErr()) {
39
+ return setupContextHookResult;
40
+ }
41
+ const setupContextHook = setupContextHookResult.value;
42
+ if (setupContextHook === null) {
43
+ if (cdpAddress !== undefined) {
44
+ ({
45
+ page,
46
+ context
47
+ } = await (0, _launchBrowser.launchBrowser)({
48
+ cdpAddress,
49
+ cdpTargetId
50
+ }));
51
+ } else {
52
+ ({
53
+ page,
54
+ context
55
+ } = await (0, _launchBrowser.launchBrowser)({
56
+ proxy,
57
+ headless,
58
+ downloadsPath
59
+ }));
60
+ }
61
+ return await fn(context, page);
62
+ }
63
+ let hookCdpUrl = null;
64
+ if (cdpAddress) {
65
+ hookCdpUrl = cdpAddress;
66
+ ({
67
+ context,
68
+ page
69
+ } = await (0, _launchBrowser.launchBrowser)({
70
+ cdpAddress,
71
+ cdpTargetId
72
+ }));
73
+ } else {
74
+ const port = await (0, _portfinder.getPort)({
75
+ port: 9222
76
+ });
77
+ ({
78
+ context,
79
+ page
80
+ } = await (0, _launchBrowser.launchBrowser)({
81
+ proxy,
82
+ headless,
83
+ downloadsPath,
84
+ cdpPort: port
85
+ }));
86
+ hookCdpUrl = (0, _launchBrowser.getLocalCdpAddress)(port);
87
+ }
88
+ let hookResult;
89
+ try {
90
+ const wsUrlResult = await (0, _launchBrowser.getCdpWebSocketUrl)(hookCdpUrl);
91
+ if (wsUrlResult.isOk()) {
92
+ hookCdpUrl = wsUrlResult.value;
93
+ } else {
94
+ throw new Error(wsUrlResult.error);
95
+ }
96
+ hookResult = await setupContextHook({
97
+ apiName: apiName,
98
+ apiParameters: apiParameters,
99
+ cdpUrl: hookCdpUrl
100
+ });
101
+ } catch (error) {
102
+ return (0, _neverthrow.err)(new _runtimeInterface.AutomationError(error));
103
+ }
104
+ if (!hookResult) {
105
+ return await fn(context, page);
106
+ }
107
+ const {
108
+ page: newPage,
109
+ context: newContext,
110
+ cleanup
111
+ } = hookResult;
112
+ let result;
113
+ try {
114
+ result = {
115
+ return: await fn(newContext, newPage ?? page)
116
+ };
117
+ } catch (e) {
118
+ result = {
119
+ throw: e
120
+ };
121
+ }
122
+ try {
123
+ await cleanup?.();
124
+ } catch (e) {
125
+ result = {
126
+ return: (0, _neverthrow.err)(new _runtimeInterface.AutomationError(e))
127
+ };
128
+ }
129
+ if ("throw" in result) {
130
+ throw result.throw;
131
+ }
132
+ return result.return;
133
+ } finally {
134
+ await context?.close();
135
+ }
136
+ }
137
+ async function loadSessionToContext({
138
+ context,
139
+ session
140
+ }) {
141
+ let sessionToLoad;
142
+ if (session.type === "state") {
143
+ const state = session.state;
144
+ if (state === undefined || state === null) {
145
+ return;
146
+ }
147
+ sessionToLoad = state;
148
+ } else {
149
+ const fullPath = (0, _fileUtils.getFullPathInProject)(session.path);
150
+ sessionToLoad = await fs.readJson(fullPath);
151
+ }
152
+ await (0, _contextStorageStateHelpers.setStorageState)(context, sessionToLoad);
153
+ }
@@ -0,0 +1,7 @@
1
+ import { Result } from "neverthrow";
2
+ import { ImportFunction, RunAutomationError } from "@intuned/runtime-interface";
3
+ export declare function importUsingImportFunction<_ReturnType = any>({ path, allowGenerators, importFunction, }: {
4
+ path: string;
5
+ importFunction: ImportFunction;
6
+ allowGenerators?: boolean;
7
+ }): Promise<Result<(..._: any) => Promise<_ReturnType>, RunAutomationError>>;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.importUsingImportFunction = importUsingImportFunction;
7
+ var _neverthrow = require("neverthrow");
8
+ var _runtimeInterface = require("@intuned/runtime-interface");
9
+ async function importUsingImportFunction({
10
+ path,
11
+ allowGenerators = true,
12
+ importFunction
13
+ }) {
14
+ try {
15
+ const importedResult = await importFunction(path);
16
+ if (importedResult.isErr()) {
17
+ if (importedResult.error.type === "not_found") {
18
+ return (0, _neverthrow.err)(new _runtimeInterface.ApiNotFoundError(path));
19
+ }
20
+ return (0, _neverthrow.err)(new _runtimeInterface.AutomationError(importedResult.error.error));
21
+ }
22
+ const imported = importedResult.value;
23
+ if (!imported || !imported.default || !imported.default.constructor) {
24
+ return (0, _neverthrow.err)(new _runtimeInterface.InvalidApiError(`${path} does not have a default export`));
25
+ }
26
+ if (imported.default.constructor.name === "AsyncGeneratorFunction") {
27
+ if (!allowGenerators) {
28
+ return (0, _neverthrow.err)(new _runtimeInterface.InvalidApiError(`${path} default export must be an async function`));
29
+ }
30
+ return (0, _neverthrow.ok)(async (...args) => {
31
+ const generator = imported.default(...args);
32
+ const result = await generator.next();
33
+ if (!result.done) {
34
+ throw new Error("Yield is not supported");
35
+ }
36
+ return result.value;
37
+ });
38
+ }
39
+ if (imported.default.constructor.name === "AsyncFunction") {
40
+ return (0, _neverthrow.ok)(imported.default);
41
+ }
42
+ return (0, _neverthrow.err)(new _runtimeInterface.InvalidApiError(`${path} default export must be an async function`));
43
+ } catch (error) {
44
+ return (0, _neverthrow.err)(new _runtimeInterface.AutomationError(error));
45
+ }
46
+ }
@@ -1,11 +1,8 @@
1
- import { Result } from 'neverthrow';
2
- import { Page, BrowserContext } from 'playwright';
3
- import { ExtendedRunApiParameters, RunApiResult, RunApiResultWithSessionOk, RunAutomationError } from '@intuned/runtime-interface';
4
-
5
- declare function runApi<ResultType = any>(input: ExtendedRunApiParameters & {
1
+ import { Result } from "neverthrow";
2
+ import type { Page, BrowserContext } from "playwright";
3
+ import { ExtendedRunApiParameters, RunApiResult, RunApiResultWithSessionOk, RunAutomationError } from "@intuned/runtime-interface";
4
+ export declare function runApi<ResultType = any>(input: ExtendedRunApiParameters & {
6
5
  retrieveSession: true;
7
6
  }): Promise<RunApiResult<ResultType, RunApiResultWithSessionOk<ResultType>>>;
8
- declare function runApi<ResultType = any>(input: ExtendedRunApiParameters): Promise<RunApiResult<ResultType>>;
9
- declare function checkAuthSessionWithRetries(page: Page, context: BrowserContext, checkFn: (..._: any) => Promise<boolean>, retries?: number): Promise<Result<boolean, RunAutomationError>>;
10
-
11
- export { checkAuthSessionWithRetries, runApi };
7
+ export declare function runApi<ResultType = any>(input: ExtendedRunApiParameters): Promise<RunApiResult<ResultType>>;
8
+ export declare function checkAuthSessionWithRetries(page: Page, context: BrowserContext, checkFn: (..._: any) => Promise<boolean>, retries?: number): Promise<Result<boolean, RunAutomationError>>;