@intuned/runtime-dev 0.1.0-test.14 → 0.1.0-test.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/.babelrc +1 -2
  2. package/WebTemplate/api.ts +90 -92
  3. package/WebTemplate/controllers/authSessions/create.ts +2 -2
  4. package/WebTemplate/controllers/authSessions/store.ts +1 -1
  5. package/WebTemplate/controllers/runApi/helpers.ts +14 -12
  6. package/WebTemplate/index.playwright.ts +32 -42
  7. package/WebTemplate/jobs.ts +13 -2
  8. package/WebTemplate/utils.ts +53 -1
  9. package/api/test2.ts +6 -1
  10. package/auth-sessions/check.ts +3 -1
  11. package/auth-sessions/create.ts +10 -10
  12. package/bin/intuned-api-run +1 -1
  13. package/bin/intuned-auth-session-check +1 -1
  14. package/bin/intuned-auth-session-create +1 -1
  15. package/bin/intuned-auth-session-load +1 -1
  16. package/bin/intuned-auth-session-refresh +1 -1
  17. package/bin/intuned-browser-save-state +1 -1
  18. package/bin/intuned-browser-start +1 -1
  19. package/bin/intuned-build +1 -1
  20. package/bin/intuned-ts-check +1 -1
  21. package/dist/commands/api/run.js +109 -170
  22. package/dist/commands/auth-sessions/load.js +28 -26
  23. package/dist/commands/auth-sessions/run-check.js +54 -53
  24. package/dist/commands/auth-sessions/run-create.js +93 -96
  25. package/dist/commands/browser/save-state.js +14 -16
  26. package/dist/commands/browser/start-browser.js +11 -11
  27. package/dist/commands/build.js +79 -109
  28. package/dist/commands/common/browserUtils.js +53 -51
  29. package/dist/commands/common/getFirstLineNumber.js +93 -93
  30. package/dist/commands/common/{getFirstLineNumber.test.ts → getFirstLineNumber.test.js} +53 -48
  31. package/dist/commands/common/sendMessageToClient.js +9 -4
  32. package/dist/commands/common/tsNodeImport.d.ts +1 -0
  33. package/dist/commands/common/tsNodeImport.js +18 -0
  34. package/dist/commands/common/utils/fileUtils.js +32 -22
  35. package/dist/commands/common/utils/settings.js +27 -19
  36. package/dist/commands/common/utils/unixSocket.js +43 -43
  37. package/dist/commands/common/utils/webTemplate.js +30 -28
  38. package/dist/commands/interface/run.js +162 -139
  39. package/dist/commands/ts-check.js +50 -50
  40. package/dist/common/Logger/Logger/index.js +55 -42
  41. package/dist/common/Logger/Logger/types.js +5 -1
  42. package/dist/common/Logger/index.js +55 -42
  43. package/dist/common/Logger/types.js +5 -1
  44. package/dist/common/asyncLocalStorage/index.js +16 -8
  45. package/dist/common/cleanEnvironmentVariables.js +16 -12
  46. package/dist/common/constants.js +7 -1
  47. package/dist/common/contextStorageStateHelpers.js +47 -38
  48. package/dist/common/getPlaywrightConstructs.js +178 -197
  49. package/dist/common/jwtTokenManager.js +76 -79
  50. package/dist/common/runApi/errors.js +159 -150
  51. package/dist/common/runApi/index.js +240 -232
  52. package/dist/common/runApi/types.d.ts +19 -5
  53. package/dist/common/runApi/types.js +53 -56
  54. package/dist/common/settingsSchema.js +15 -10
  55. package/dist/common/telemetry.js +28 -30
  56. package/dist/index.js +69 -4
  57. package/dist/runtime/RunError.js +18 -11
  58. package/dist/runtime/downloadDirectory.js +19 -13
  59. package/dist/runtime/enums.d.js +5 -0
  60. package/dist/runtime/enums.d.ts +11 -1
  61. package/dist/runtime/enums.js +18 -12
  62. package/dist/runtime/executionHelpers.test.js +53 -0
  63. package/dist/runtime/export.d.js +5 -0
  64. package/dist/runtime/export.d.ts +202 -1
  65. package/dist/runtime/extendPayload.js +21 -15
  66. package/dist/runtime/extendTimeout.js +28 -21
  67. package/dist/runtime/index.js +53 -6
  68. package/dist/runtime/requestMoreInfo.js +23 -16
  69. package/dist/runtime/runInfo.js +21 -14
  70. package/package.json +4 -7
  71. package/dist/commands/api/run.ts +0 -105
  72. package/dist/commands/auth-sessions/load.ts +0 -30
  73. package/dist/commands/auth-sessions/run-check.ts +0 -51
  74. package/dist/commands/auth-sessions/run-create.ts +0 -91
  75. package/dist/commands/browser/save-state.ts +0 -14
  76. package/dist/commands/browser/start-browser.ts +0 -11
  77. package/dist/commands/build.ts +0 -79
  78. package/dist/commands/common/browserUtils.ts +0 -45
  79. package/dist/commands/common/getDefaultExportFromFile.d.ts +0 -1
  80. package/dist/commands/common/getDefaultExportFromFile.js +0 -12
  81. package/dist/commands/common/getDefaultExportFromFile.ts +0 -11
  82. package/dist/commands/common/getFirstLineNumber.ts +0 -96
  83. package/dist/commands/common/sendMessageToClient.ts +0 -3
  84. package/dist/commands/common/utils/fileUtils.ts +0 -23
  85. package/dist/commands/common/utils/settings.ts +0 -22
  86. package/dist/commands/common/utils/unixSocket.ts +0 -38
  87. package/dist/commands/common/utils/webTemplate.ts +0 -23
  88. package/dist/commands/interface/run.ts +0 -156
  89. package/dist/commands/ts-check.ts +0 -51
  90. package/dist/common/Logger/Logger/index.ts +0 -53
  91. package/dist/common/Logger/Logger/types.ts +0 -1
  92. package/dist/common/Logger/index.ts +0 -53
  93. package/dist/common/Logger/types.ts +0 -1
  94. package/dist/common/asyncLocalStorage/index.ts +0 -9
  95. package/dist/common/cleanEnvironmentVariables.ts +0 -10
  96. package/dist/common/constants.ts +0 -1
  97. package/dist/common/contextStorageStateHelpers.ts +0 -43
  98. package/dist/common/getPlaywrightConstructs.ts +0 -182
  99. package/dist/common/jwtTokenManager.ts +0 -71
  100. package/dist/common/runApi/errors.ts +0 -154
  101. package/dist/common/runApi/index.ts +0 -253
  102. package/dist/common/runApi/types.ts +0 -43
  103. package/dist/common/settingsSchema.ts +0 -9
  104. package/dist/common/telemetry.ts +0 -23
  105. package/dist/index.ts +0 -4
  106. package/dist/runtime/RunError.ts +0 -12
  107. package/dist/runtime/downloadDirectory.ts +0 -13
  108. package/dist/runtime/enums.ts +0 -12
  109. package/dist/runtime/executionHelpers.test.ts +0 -51
  110. package/dist/runtime/extendPayload.ts +0 -15
  111. package/dist/runtime/extendTimeout.ts +0 -24
  112. package/dist/runtime/index.ts +0 -6
  113. package/dist/runtime/requestMoreInfo.ts +0 -18
  114. package/dist/runtime/runInfo.ts +0 -15
@@ -1,239 +1,247 @@
1
- import { getDownloadDirectoryPath } from "../../runtime/downloadDirectory";
2
- import { getExecutionContext } from "../asyncLocalStorage";
3
- import * as fs from "fs-extra";
4
- import { backendFunctionsTokenManager } from "../jwtTokenManager";
5
- import { getContextStorageState, setContextStorageState, } from "../contextStorageStateHelpers";
6
- import { remove } from "fs-extra";
7
- import { ok, err } from "neverthrow";
8
- import { AbortedError, ApiNotFoundError, AutomationError, InvalidApiError, InvalidCheckError, MaxLevelsExceededError, } from "./errors.js";
9
- import { AUTH_SESSIONS_FOLDER_NAME } from "../constants";
10
- import { getPlaywrightConstructsForMode, getProductionPlaywrightConstructs, } from "../getPlaywrightConstructs";
11
- import { runApiParametersSchema, } from "./types.js";
12
- export * from "./types.js";
13
- export * from "./errors.js";
14
- export async function* runApiGenerator({ retrieveSession = false, abortSignal, ...input }) {
15
- let traceStarted = false;
16
- const { automationFunction, runOptions, tracing, auth, functionsToken } = runApiParametersSchema.parse(input);
17
- backendFunctionsTokenManager.token = functionsToken;
18
- const downloadsPath = getDownloadDirectoryPath();
19
- let page;
20
- let context;
21
- if (runOptions.environment === "deployed") {
22
- const { headless, proxy } = runOptions;
23
- ({ page, context } = await getProductionPlaywrightConstructs({
24
- headless,
25
- proxy,
26
- downloadsPath,
27
- storageState: auth?.session,
28
- }));
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ var _exportNames = {
7
+ runApiGenerator: true,
8
+ runApi: true,
9
+ checkAuthSessionWithRetries: true
10
+ };
11
+ exports.checkAuthSessionWithRetries = checkAuthSessionWithRetries;
12
+ exports.runApi = runApi;
13
+ exports.runApiGenerator = runApiGenerator;
14
+ var _downloadDirectory = require("../../runtime/downloadDirectory");
15
+ var _asyncLocalStorage = require("../asyncLocalStorage");
16
+ var _fsExtra = _interopRequireWildcard(require("fs-extra"));
17
+ var fs = _fsExtra;
18
+ var _jwtTokenManager = require("../jwtTokenManager");
19
+ var _contextStorageStateHelpers = require("../contextStorageStateHelpers");
20
+ var _neverthrow = require("neverthrow");
21
+ var _errors = require("./errors.js");
22
+ Object.keys(_errors).forEach(function (key) {
23
+ if (key === "default" || key === "__esModule") return;
24
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
25
+ if (key in exports && exports[key] === _errors[key]) return;
26
+ Object.defineProperty(exports, key, {
27
+ enumerable: true,
28
+ get: function () {
29
+ return _errors[key];
30
+ }
31
+ });
32
+ });
33
+ var _constants = require("../constants");
34
+ var _getPlaywrightConstructs = require("../getPlaywrightConstructs");
35
+ var _types = require("./types.js");
36
+ Object.keys(_types).forEach(function (key) {
37
+ if (key === "default" || key === "__esModule") return;
38
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
39
+ if (key in exports && exports[key] === _types[key]) return;
40
+ Object.defineProperty(exports, key, {
41
+ enumerable: true,
42
+ get: function () {
43
+ return _types[key];
44
+ }
45
+ });
46
+ });
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 && Object.prototype.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; }
49
+ async function* runApiGenerator({
50
+ retrieveSession = false,
51
+ abortSignal,
52
+ ...input
53
+ }) {
54
+ let traceStarted = false;
55
+ const {
56
+ automationFunction,
57
+ runOptions,
58
+ tracing,
59
+ auth,
60
+ functionsToken
61
+ } = _types.runApiParametersSchema.parse(input);
62
+ _jwtTokenManager.backendFunctionsTokenManager.token = functionsToken;
63
+ const downloadsPath = (0, _downloadDirectory.getDownloadDirectoryPath)();
64
+ let page;
65
+ let context;
66
+ if (runOptions.environment === "deployed") {
67
+ const {
68
+ headless,
69
+ proxy
70
+ } = runOptions;
71
+ ({
72
+ page,
73
+ context
74
+ } = await (0, _getPlaywrightConstructs.getProductionPlaywrightConstructs)({
75
+ headless,
76
+ proxy,
77
+ downloadsPath,
78
+ storageState: auth === null || auth === void 0 ? void 0 : auth.session
79
+ }));
80
+ } else {
81
+ const {
82
+ mode,
83
+ cdpAddress
84
+ } = runOptions;
85
+ ({
86
+ page,
87
+ context
88
+ } = await (0, _getPlaywrightConstructs.getPlaywrightConstructsForMode)(mode, cdpAddress, auth === null || auth === void 0 ? void 0 : auth.session));
89
+ }
90
+ const executionContext = (0, _asyncLocalStorage.getExecutionContext)();
91
+ if (!executionContext) {
92
+ throw "";
93
+ }
94
+ async function saveTraceIfNeeded({
95
+ errorMessage
96
+ }) {
97
+ if (!tracing.enabled || !traceStarted) {
98
+ return;
29
99
  }
30
- else {
31
- const { mode, cdpAddress } = runOptions;
32
- ({ page, context } = await getPlaywrightConstructsForMode(mode, cdpAddress, auth?.session));
33
- }
34
- const executionContext = getExecutionContext();
35
- if (!executionContext) {
36
- throw ""; // todo
37
- }
38
- async function saveTraceIfNeeded({ errorMessage }) {
39
- if (!tracing.enabled || !traceStarted) {
40
- return;
41
- }
42
- try {
43
- await context.tracing.stop({ path: tracing.filePath });
44
- }
45
- catch (error) {
46
- console.log(errorMessage, error?.message);
47
- await remove(tracing.filePath);
48
- }
49
- }
50
- if (tracing.enabled) {
51
- await context.tracing.start({
52
- screenshots: true,
53
- snapshots: true,
54
- sources: true,
55
- });
56
- traceStarted = true;
57
- }
58
- const abortSymbol = Symbol("abort");
59
- const abortPromise = new Promise((resolve) => {
60
- if (!abortSignal)
61
- return;
62
- abortSignal.addEventListener("abort", () => {
63
- resolve(abortSymbol);
64
- });
100
+ try {
101
+ await context.tracing.stop({
102
+ path: tracing.filePath
103
+ });
104
+ } catch (error) {
105
+ console.log(errorMessage, error === null || error === void 0 ? void 0 : error.message);
106
+ await (0, _fsExtra.remove)(tracing.filePath);
107
+ }
108
+ }
109
+ if (tracing.enabled) {
110
+ await context.tracing.start({
111
+ screenshots: true,
112
+ snapshots: true,
113
+ sources: true
65
114
  });
66
- if (auth && auth.session.type === "state") {
67
- await setContextStorageState(context, auth.session.state);
68
- }
69
- async function* runAutomation() {
70
- if (auth?.runCheck) {
71
- if (!auth.session) {
72
- return err({ code: "AuthRequiredError" });
73
- }
74
- console.log("Running auth check");
75
- const authCheckResult = await checkAuthSessionWithRetries(page, context, 2);
76
- if (authCheckResult.isErr()) {
77
- const error = authCheckResult.error;
78
- if (["APINotFoundError", "InvalidAPIError"].includes(error.code)) {
79
- return err(new InvalidCheckError(`Auth session check function failed`, error));
80
- }
81
- return authCheckResult;
82
- }
83
- if (!authCheckResult.value) {
84
- return err({
85
- code: "AuthCheckFailedError",
86
- });
87
- }
88
- }
89
- console.log("Running function", automationFunction.name);
90
- const args = [
91
- ...(automationFunction.params ? [automationFunction.params] : []),
92
- page,
93
- context,
94
- ];
95
- const importResult = await importFunction(automationFunction.name);
96
- if (importResult.isErr()) {
97
- return importResult;
98
- }
99
- let result;
100
- if (importResult.value.type === "async-generator") {
101
- const generator = importResult.value.generator(...args);
102
- let next = undefined;
103
- while (true) {
104
- const generatorResult = await generator.next(...(next ? [next] : []));
105
- if (!generatorResult.done) {
106
- next = yield generatorResult.value;
107
- continue;
108
- }
109
- result = generatorResult.value;
110
- break;
111
- }
112
- }
113
- else {
114
- result = await importResult.value.func(...args);
115
+ traceStarted = true;
116
+ }
117
+ const abortSymbol = Symbol("abort");
118
+ const abortPromise = new Promise(resolve => {
119
+ if (!abortSignal) return;
120
+ abortSignal.addEventListener("abort", () => {
121
+ resolve(abortSymbol);
122
+ });
123
+ });
124
+ if (auth && auth.session.type === "state") {
125
+ await (0, _contextStorageStateHelpers.setContextStorageState)(context, auth.session.state);
126
+ }
127
+ async function* runAutomation() {
128
+ var _getExecutionContext;
129
+ if (auth !== null && auth !== void 0 && auth.runCheck) {
130
+ if (!auth.session) {
131
+ return (0, _neverthrow.err)(new _errors.AuthRequiredError());
132
+ }
133
+ console.log("Running auth check");
134
+ const authCheckResult = await checkAuthSessionWithRetries(page, context, 2);
135
+ if (authCheckResult.isErr()) {
136
+ const error = authCheckResult.error;
137
+ if (["APINotFoundError", "InvalidAPIError"].includes(error.code)) {
138
+ return (0, _neverthrow.err)(new _errors.InvalidCheckError(`Auth session check function failed`, error));
115
139
  }
116
- return ok({
117
- result,
118
- extendedPayloads: getExecutionContext()?.extendedPayloads,
119
- session: retrieveSession
120
- ? await getContextStorageState(context)
121
- : undefined,
122
- });
123
- }
124
- try {
125
- const generator = runAutomation();
126
- let next = undefined;
127
- while (true) {
128
- const result = await Promise.race([
129
- generator.next(await next),
130
- abortPromise,
131
- ]);
132
- if (result === abortSymbol) {
133
- return err(new AbortedError());
134
- }
135
- if (!result.done) {
136
- next = yield result.value;
137
- continue;
138
- }
139
- return result.value;
140
+ return authCheckResult;
141
+ }
142
+ if (!authCheckResult.value) {
143
+ return (0, _neverthrow.err)(new _errors.AuthCheckFailedError());
144
+ }
145
+ }
146
+ const args = [...(automationFunction.params ? [automationFunction.params] : []), page, context];
147
+ const validatedModuleResult = await validateModule(automationFunction.module);
148
+ if (validatedModuleResult.isErr()) {
149
+ return validatedModuleResult;
150
+ }
151
+ let result;
152
+ if (validatedModuleResult.value.type === "async-generator") {
153
+ const generator = validatedModuleResult.value.generator(...args);
154
+ let next = undefined;
155
+ while (true) {
156
+ const generatorResult = await generator.next(...(next ? [next] : []));
157
+ if (!generatorResult.done) {
158
+ next = yield generatorResult.value;
159
+ continue;
140
160
  }
141
- }
142
- catch (error) {
143
- console.log("run errored", error);
144
- return err(new AutomationError(error));
145
- }
146
- finally {
147
- await saveTraceIfNeeded({ errorMessage: "failed to save trace" });
148
- await context.close();
149
- await fs.remove(downloadsPath);
150
- }
151
- }
152
- export async function runApi(params) {
153
- const generator = runApiGenerator(params);
154
- const { value, done } = await generator.next();
155
- if (done) {
156
- return value;
157
- }
158
- return err(new InvalidApiError("Expected API to be async function, got async generator"));
161
+ result = generatorResult.value;
162
+ break;
163
+ }
164
+ } else {
165
+ result = await validatedModuleResult.value.func(...args);
166
+ }
167
+ return (0, _neverthrow.ok)({
168
+ result,
169
+ extendedPayloads: (_getExecutionContext = (0, _asyncLocalStorage.getExecutionContext)()) === null || _getExecutionContext === void 0 ? void 0 : _getExecutionContext.extendedPayloads,
170
+ session: retrieveSession ? await (0, _contextStorageStateHelpers.getContextStorageState)(context) : undefined
171
+ });
172
+ }
173
+ try {
174
+ const generator = runAutomation();
175
+ let next = undefined;
176
+ while (true) {
177
+ const result = await Promise.race([generator.next(await next), abortPromise]);
178
+ if (result === abortSymbol) {
179
+ return (0, _neverthrow.err)(new _errors.AbortedError());
180
+ }
181
+ if (!result.done) {
182
+ next = yield result.value;
183
+ continue;
184
+ }
185
+ return result.value;
186
+ }
187
+ } catch (error) {
188
+ console.log("run errored", error);
189
+ return (0, _neverthrow.err)(new _errors.AutomationError(error));
190
+ } finally {
191
+ await saveTraceIfNeeded({
192
+ errorMessage: "failed to save trace"
193
+ });
194
+ await context.close();
195
+ await fs.remove(downloadsPath);
196
+ }
159
197
  }
160
- export async function checkAuthSessionWithRetries(page, context, retries = 3) {
161
- if (retries === 0) {
162
- return ok(false);
163
- }
164
- let tryNumber = 0;
165
- console.log("Checking auth session with retries", `${AUTH_SESSIONS_FOLDER_NAME}/check`);
166
- const importResult = await importFunction(`${AUTH_SESSIONS_FOLDER_NAME}/check`);
167
- if (importResult.isErr()) {
168
- return err(importResult.error);
169
- }
170
- if (importResult.value.type !== "async") {
171
- return err(new InvalidCheckError("Check function is not an async function"));
172
- }
173
- const check = importResult.value.func;
174
- while (retries > tryNumber) {
175
- const result = await check(page, context);
176
- if (result)
177
- return ok(true);
178
- tryNumber++;
179
- }
180
- return ok(false);
198
+ async function runApi(params) {
199
+ const generator = runApiGenerator(params);
200
+ const {
201
+ value,
202
+ done
203
+ } = await generator.next();
204
+ if (done) {
205
+ return value;
206
+ }
207
+ return (0, _neverthrow.err)(new _errors.InvalidApiError("Expected API to be async function, got async generator"));
181
208
  }
182
- async function importFunction(path) {
183
- const functionNameParts = path.split("/");
184
- const functionNameDepth = functionNameParts.length;
185
- // string literals should be inline
186
- // currently we support only 5 levels of depth
187
- // rollup dynamic import does not support multiple levels of dynamic imports so we need to specify the possible paths explicitly
188
- try {
189
- let imported = undefined;
190
- switch (functionNameDepth) {
191
- case 1:
192
- imported = await import(`../../../${functionNameParts[0]}.ts`);
193
- break;
194
- case 2:
195
- imported = await import(`../../../${functionNameParts[0]}/${functionNameParts[1]}.ts`);
196
- break;
197
- case 3:
198
- imported = await import(`../../../${functionNameParts[0]}/${functionNameParts[1]}/${functionNameParts[2]}.ts`);
199
- break;
200
- case 4:
201
- imported = await import(`../../../${functionNameParts[0]}/${functionNameParts[1]}/${functionNameParts[2]}/${functionNameParts[3]}.ts`);
202
- break;
203
- case 5:
204
- imported = await import(`../../../${functionNameParts[0]}/${functionNameParts[1]}/${functionNameParts[2]}/${functionNameParts[3]}/${functionNameParts[4]}.ts`);
205
- break;
206
- case 6:
207
- imported = await import(`../../../${functionNameParts[0]}/${functionNameParts[1]}/${functionNameParts[2]}/${functionNameParts[3]}/${functionNameParts[4]}/${functionNameParts[5]}.ts`);
208
- break;
209
- default:
210
- return err(new MaxLevelsExceededError(5));
211
- }
212
- if (!imported || !imported.default || !imported.default.constructor) {
213
- return err(new InvalidApiError("API file path does not have a default export"));
214
- }
215
- if (imported.default.constructor.name === "AsyncGeneratorFunction") {
216
- return ok({
217
- type: "async-generator",
218
- generator: imported.default,
219
- });
220
- }
221
- if (imported.default.constructor.name === "AsyncFunction") {
222
- return ok({
223
- type: "async",
224
- func: imported.default,
225
- });
226
- }
227
- return err(new InvalidApiError("API file path does not have a default async function/generator export"));
228
- }
229
- catch (error) {
230
- if (error.message.includes("Unknown variable dynamic import")) {
231
- return err(new ApiNotFoundError(path));
232
- }
233
- if (error?.code === "ERR_MODULE_NOT_FOUND") {
234
- console.log("API not found", error);
235
- return err(new ApiNotFoundError(path));
236
- }
237
- throw error;
238
- }
209
+ async function checkAuthSessionWithRetries(page, context, retries = 3) {
210
+ if (retries === 0) {
211
+ return (0, _neverthrow.ok)(false);
212
+ }
213
+ let tryNumber = 0;
214
+ console.log("Checking auth session with retries", `${_constants.AUTH_SESSIONS_FOLDER_NAME}/check`);
215
+ const importResult = await validateModule(`${_constants.AUTH_SESSIONS_FOLDER_NAME}/check`);
216
+ if (importResult.isErr()) {
217
+ return (0, _neverthrow.err)(importResult.error);
218
+ }
219
+ if (importResult.value.type !== "async") {
220
+ return (0, _neverthrow.err)(new _errors.InvalidCheckError("Check function is not an async function"));
221
+ }
222
+ const check = importResult.value.func;
223
+ while (retries > tryNumber) {
224
+ const result = await check(page, context);
225
+ if (result) return (0, _neverthrow.ok)(true);
226
+ tryNumber++;
227
+ }
228
+ return (0, _neverthrow.ok)(false);
239
229
  }
230
+ async function validateModule(module) {
231
+ if (!module || !module.default || !module.default.constructor) {
232
+ return (0, _neverthrow.err)(new _errors.InvalidApiError("API file path does not have a default export"));
233
+ }
234
+ if (module.default.constructor.name === "AsyncGeneratorFunction") {
235
+ return (0, _neverthrow.ok)({
236
+ type: "async-generator",
237
+ generator: module.default
238
+ });
239
+ }
240
+ if (module.default.constructor.name === "AsyncFunction") {
241
+ return (0, _neverthrow.ok)({
242
+ type: "async",
243
+ func: module.default
244
+ });
245
+ }
246
+ return (0, _neverthrow.err)(new _errors.InvalidApiError("API file path does not have a default async function/generator export"));
247
+ }
@@ -127,13 +127,23 @@ export declare const runApiSessionSchema: z.ZodDiscriminatedUnion<"type", [z.Zod
127
127
  export declare const runApiParametersSchema: z.ZodObject<{
128
128
  functionsToken: z.ZodOptional<z.ZodString>;
129
129
  automationFunction: z.ZodObject<{
130
- name: z.ZodString;
130
+ module: z.ZodObject<{
131
+ default: z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>;
132
+ }, "strip", z.ZodTypeAny, {
133
+ default: (...args: unknown[]) => unknown;
134
+ }, {
135
+ default: (...args: unknown[]) => unknown;
136
+ }>;
131
137
  params: z.ZodOptional<z.ZodAny>;
132
138
  }, "strip", z.ZodTypeAny, {
133
- name: string;
139
+ module: {
140
+ default: (...args: unknown[]) => unknown;
141
+ };
134
142
  params?: any;
135
143
  }, {
136
- name: string;
144
+ module: {
145
+ default: (...args: unknown[]) => unknown;
146
+ };
137
147
  params?: any;
138
148
  }>;
139
149
  tracing: z.ZodDefault<z.ZodOptional<z.ZodDiscriminatedUnion<"enabled", [z.ZodObject<{
@@ -360,7 +370,9 @@ export declare const runApiParametersSchema: z.ZodObject<{
360
370
  retrieveSession: z.ZodDefault<z.ZodBoolean>;
361
371
  }, "strip", z.ZodTypeAny, {
362
372
  automationFunction: {
363
- name: string;
373
+ module: {
374
+ default: (...args: unknown[]) => unknown;
375
+ };
364
376
  params?: any;
365
377
  };
366
378
  tracing: {
@@ -416,7 +428,9 @@ export declare const runApiParametersSchema: z.ZodObject<{
416
428
  } | undefined;
417
429
  }, {
418
430
  automationFunction: {
419
- name: string;
431
+ module: {
432
+ default: (...args: unknown[]) => unknown;
433
+ };
420
434
  params?: any;
421
435
  };
422
436
  functionsToken?: string | undefined;
@@ -1,57 +1,54 @@
1
- import z from "zod";
2
- export const runApiSessionSchema = z.discriminatedUnion("type", [
3
- z.object({
4
- type: z.literal("file"),
5
- path: z.string(),
6
- }),
7
- z.object({
8
- type: z.literal("state"),
9
- state: z.custom((v) => v),
10
- }),
11
- ]);
12
- export const runApiParametersSchema = z.object({
13
- functionsToken: z.string().optional(),
14
- automationFunction: z.object({
15
- name: z.string(),
16
- params: z.any().optional(),
17
- }),
18
- tracing: z
19
- .discriminatedUnion("enabled", [
20
- z.object({ enabled: z.literal(false) }),
21
- z.object({ enabled: z.literal(true), filePath: z.string() }),
22
- ])
23
- .optional()
24
- .default({ enabled: false }),
25
- auth: z
26
- .object({
27
- session: runApiSessionSchema,
28
- runCheck: z.boolean().default(false),
29
- })
30
- .optional(),
31
- runOptions: z
32
- .discriminatedUnion("environment", [
33
- z.object({
34
- environment: z.literal("deployed"),
35
- headless: z.boolean().default(true),
36
- proxy: z
37
- .object({
38
- server: z.string(),
39
- username: z.string(),
40
- password: z.string(),
41
- })
42
- .optional(),
43
- }),
44
- z.object({
45
- environment: z.literal("ide"),
46
- cdpAddress: z.string(),
47
- mode: z.union([
48
- z.literal("vanilla"),
49
- z.literal("playwright"),
50
- z.literal("playwright-standalone"),
51
- z.literal("playwright-headless"),
52
- ]),
53
- }),
54
- ])
55
- .default({ environment: "deployed" }),
56
- retrieveSession: z.boolean().default(false),
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
57
5
  });
6
+ exports.runApiSessionSchema = exports.runApiParametersSchema = void 0;
7
+ var _zod = _interopRequireDefault(require("zod"));
8
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
+ const runApiSessionSchema = exports.runApiSessionSchema = _zod.default.discriminatedUnion("type", [_zod.default.object({
10
+ type: _zod.default.literal("file"),
11
+ path: _zod.default.string()
12
+ }), _zod.default.object({
13
+ type: _zod.default.literal("state"),
14
+ state: _zod.default.custom(v => v)
15
+ })]);
16
+ const runApiParametersSchema = exports.runApiParametersSchema = _zod.default.object({
17
+ functionsToken: _zod.default.string().optional(),
18
+ automationFunction: _zod.default.object({
19
+ module: _zod.default.object({
20
+ default: _zod.default.function()
21
+ }, {
22
+ required_error: "API handler must have a default export function"
23
+ }),
24
+ params: _zod.default.any().optional()
25
+ }),
26
+ tracing: _zod.default.discriminatedUnion("enabled", [_zod.default.object({
27
+ enabled: _zod.default.literal(false)
28
+ }), _zod.default.object({
29
+ enabled: _zod.default.literal(true),
30
+ filePath: _zod.default.string()
31
+ })]).optional().default({
32
+ enabled: false
33
+ }),
34
+ auth: _zod.default.object({
35
+ session: runApiSessionSchema,
36
+ runCheck: _zod.default.boolean().default(false)
37
+ }).optional(),
38
+ runOptions: _zod.default.discriminatedUnion("environment", [_zod.default.object({
39
+ environment: _zod.default.literal("deployed"),
40
+ headless: _zod.default.boolean().default(true),
41
+ proxy: _zod.default.object({
42
+ server: _zod.default.string(),
43
+ username: _zod.default.string(),
44
+ password: _zod.default.string()
45
+ }).optional()
46
+ }), _zod.default.object({
47
+ environment: _zod.default.literal("ide"),
48
+ cdpAddress: _zod.default.string(),
49
+ mode: _zod.default.union([_zod.default.literal("vanilla"), _zod.default.literal("playwright"), _zod.default.literal("playwright-standalone"), _zod.default.literal("playwright-headless")])
50
+ })]).default({
51
+ environment: "deployed"
52
+ }),
53
+ retrieveSession: _zod.default.boolean().default(false)
54
+ });