@intuned/runtime-dev 0.1.0-test.10 → 0.1.0-test.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +1 -2
- package/dist/commands/api/run.js +170 -105
- package/dist/commands/api/run.ts +105 -0
- package/dist/commands/auth-sessions/load.js +26 -28
- package/dist/commands/auth-sessions/load.ts +30 -0
- package/dist/commands/auth-sessions/run-check.js +53 -52
- package/dist/commands/auth-sessions/run-check.ts +51 -0
- package/dist/commands/auth-sessions/run-create.js +96 -91
- package/dist/commands/auth-sessions/run-create.ts +91 -0
- package/dist/commands/browser/save-state.js +16 -14
- package/dist/commands/browser/save-state.ts +14 -0
- package/dist/commands/browser/start-browser.js +11 -11
- package/dist/commands/browser/start-browser.ts +11 -0
- package/dist/commands/build.js +108 -75
- package/dist/commands/build.ts +78 -0
- package/dist/commands/common/browserUtils.js +51 -53
- package/dist/commands/common/browserUtils.ts +45 -0
- package/dist/commands/common/getDefaultExportFromFile.js +12 -17
- package/dist/commands/common/getDefaultExportFromFile.ts +11 -0
- package/dist/commands/common/getFirstLineNumber.js +93 -93
- package/dist/commands/common/{getFirstLineNumber.test.js → getFirstLineNumber.test.ts} +46 -51
- package/dist/commands/common/getFirstLineNumber.ts +96 -0
- package/dist/commands/common/sendMessageToClient.js +4 -9
- package/dist/commands/common/sendMessageToClient.ts +3 -0
- package/dist/commands/common/utils/fileUtils.js +22 -32
- package/dist/commands/common/utils/fileUtils.ts +23 -0
- package/dist/commands/common/utils/settings.js +19 -27
- package/dist/commands/common/utils/settings.ts +22 -0
- package/dist/commands/common/utils/unixSocket.js +43 -43
- package/dist/commands/common/utils/unixSocket.ts +38 -0
- package/dist/commands/common/utils/webTemplate.js +27 -29
- package/dist/commands/common/utils/webTemplate.ts +22 -0
- package/dist/commands/interface/run.js +151 -150
- package/dist/commands/interface/run.ts +156 -0
- package/dist/commands/ts-check.js +49 -49
- package/dist/commands/ts-check.ts +50 -0
- package/dist/common/Logger/Logger/index.d.ts +1 -1
- package/dist/common/Logger/Logger/index.js +42 -55
- package/dist/common/Logger/Logger/index.ts +53 -0
- package/dist/common/Logger/Logger/types.js +1 -5
- package/dist/common/Logger/Logger/types.ts +1 -0
- package/dist/common/Logger/index.d.ts +1 -1
- package/dist/common/Logger/index.js +42 -55
- package/dist/common/Logger/index.ts +53 -0
- package/dist/common/Logger/types.js +1 -5
- package/dist/common/Logger/types.ts +1 -0
- package/dist/common/asyncLocalStorage/index.js +8 -16
- package/dist/common/asyncLocalStorage/index.ts +9 -0
- package/dist/common/cleanEnvironmentVariables.js +12 -16
- package/dist/common/cleanEnvironmentVariables.ts +10 -0
- package/dist/common/constants.js +1 -7
- package/dist/common/constants.ts +1 -0
- package/dist/common/contextStorageStateHelpers.js +38 -47
- package/dist/common/contextStorageStateHelpers.ts +43 -0
- package/dist/common/getPlaywrightConstructs.d.ts +1 -1
- package/dist/common/getPlaywrightConstructs.js +196 -177
- package/dist/common/getPlaywrightConstructs.ts +181 -0
- package/dist/common/jwtTokenManager.js +79 -76
- package/dist/common/jwtTokenManager.ts +71 -0
- package/dist/common/runApi/errors.d.ts +1 -1
- package/dist/common/runApi/errors.js +150 -159
- package/dist/common/runApi/errors.ts +154 -0
- package/dist/common/runApi/index.d.ts +4 -4
- package/dist/common/runApi/index.js +220 -269
- package/dist/common/runApi/index.ts +253 -0
- package/dist/common/runApi/types.d.ts +1 -1
- package/dist/common/runApi/types.js +56 -49
- package/dist/common/runApi/types.ts +43 -0
- package/dist/common/settingsSchema.js +10 -15
- package/dist/common/settingsSchema.ts +9 -0
- package/dist/common/telemetry.js +30 -28
- package/dist/common/telemetry.ts +23 -0
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -69
- package/dist/index.ts +4 -0
- package/dist/runtime/RunError.d.ts +1 -1
- package/dist/runtime/RunError.js +11 -18
- package/dist/runtime/RunError.ts +12 -0
- package/dist/runtime/downloadDirectory.js +13 -19
- package/dist/runtime/downloadDirectory.ts +13 -0
- package/dist/runtime/enums.d.ts +1 -11
- package/dist/runtime/enums.js +12 -18
- package/dist/runtime/enums.ts +12 -0
- package/dist/runtime/executionHelpers.test.ts +51 -0
- package/dist/runtime/export.d.ts +1 -202
- package/dist/runtime/extendPayload.d.ts +1 -1
- package/dist/runtime/extendPayload.js +15 -21
- package/dist/runtime/extendPayload.ts +15 -0
- package/dist/runtime/extendTimeout.js +21 -28
- package/dist/runtime/extendTimeout.ts +24 -0
- package/dist/runtime/index.d.ts +7 -7
- package/dist/runtime/index.js +6 -53
- package/dist/runtime/index.ts +6 -0
- package/dist/runtime/requestMoreInfo.js +16 -23
- package/dist/runtime/requestMoreInfo.ts +18 -0
- package/dist/runtime/runInfo.d.ts +1 -1
- package/dist/runtime/runInfo.js +14 -21
- package/dist/runtime/runInfo.ts +15 -0
- package/package.json +3 -2
- package/tsconfig.json +1 -1
- package/dist/runtime/enums.d.js +0 -5
- package/dist/runtime/executionHelpers.test.js +0 -53
- package/dist/runtime/export.d.js +0 -5
|
@@ -1,288 +1,239 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return _errors[key];
|
|
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
|
+
}));
|
|
30
29
|
}
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
var _getPlaywrightConstructs = require("../getPlaywrightConstructs");
|
|
35
|
-
var _types = require("./types");
|
|
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];
|
|
30
|
+
else {
|
|
31
|
+
const { mode, cdpAddress } = runOptions;
|
|
32
|
+
({ page, context } = await getPlaywrightConstructsForMode(mode, cdpAddress, auth?.session));
|
|
44
33
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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;
|
|
34
|
+
const executionContext = getExecutionContext();
|
|
35
|
+
if (!executionContext) {
|
|
36
|
+
throw ""; // todo
|
|
99
37
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
+
}
|
|
107
49
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
sources: true
|
|
114
|
-
});
|
|
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)({
|
|
132
|
-
code: "AuthRequiredError"
|
|
50
|
+
if (tracing.enabled) {
|
|
51
|
+
await context.tracing.start({
|
|
52
|
+
screenshots: true,
|
|
53
|
+
snapshots: true,
|
|
54
|
+
sources: true,
|
|
133
55
|
});
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
return authCheckResult;
|
|
143
|
-
}
|
|
144
|
-
if (!authCheckResult.value) {
|
|
145
|
-
return (0, _neverthrow.err)({
|
|
146
|
-
code: "AuthCheckFailedError"
|
|
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);
|
|
147
64
|
});
|
|
148
|
-
|
|
65
|
+
});
|
|
66
|
+
if (auth && auth.session.type === "state") {
|
|
67
|
+
await setContextStorageState(context, auth.session.state);
|
|
149
68
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
+
}
|
|
116
|
+
return ok({
|
|
117
|
+
result,
|
|
118
|
+
extendedPayloads: getExecutionContext()?.extendedPayloads,
|
|
119
|
+
session: retrieveSession
|
|
120
|
+
? await getContextStorageState(context)
|
|
121
|
+
: undefined,
|
|
122
|
+
});
|
|
155
123
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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;
|
|
165
140
|
}
|
|
166
|
-
result = generatorResult.value;
|
|
167
|
-
break;
|
|
168
|
-
}
|
|
169
|
-
} else {
|
|
170
|
-
result = await importResult.value.func(...args);
|
|
171
141
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
let next = undefined;
|
|
181
|
-
while (true) {
|
|
182
|
-
const result = await Promise.race([generator.next(await next), abortPromise]);
|
|
183
|
-
if (result === abortSymbol) {
|
|
184
|
-
return (0, _neverthrow.err)(new _errors.AbortedError());
|
|
185
|
-
}
|
|
186
|
-
if (!result.done) {
|
|
187
|
-
next = yield result.value;
|
|
188
|
-
continue;
|
|
189
|
-
}
|
|
190
|
-
return result.value;
|
|
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);
|
|
191
150
|
}
|
|
192
|
-
} catch (error) {
|
|
193
|
-
console.log("run errored", error);
|
|
194
|
-
return (0, _neverthrow.err)(new _errors.AutomationError(error));
|
|
195
|
-
} finally {
|
|
196
|
-
await saveTraceIfNeeded({
|
|
197
|
-
errorMessage: "failed to save trace"
|
|
198
|
-
});
|
|
199
|
-
await context.close();
|
|
200
|
-
await fs.remove(downloadsPath);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
async function runApi(params) {
|
|
204
|
-
const generator = runApiGenerator(params);
|
|
205
|
-
const {
|
|
206
|
-
value,
|
|
207
|
-
done
|
|
208
|
-
} = await generator.next();
|
|
209
|
-
if (done) {
|
|
210
|
-
return value;
|
|
211
|
-
}
|
|
212
|
-
return (0, _neverthrow.err)(new _errors.InvalidApiError("Expected API to be async function, got async generator"));
|
|
213
151
|
}
|
|
214
|
-
async function
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
if (importResult.isErr()) {
|
|
222
|
-
return (0, _neverthrow.err)(importResult.error);
|
|
223
|
-
}
|
|
224
|
-
if (importResult.value.type !== "async") {
|
|
225
|
-
return (0, _neverthrow.err)(new _errors.InvalidCheckError("Check function is not an async function"));
|
|
226
|
-
}
|
|
227
|
-
const check = importResult.value.func;
|
|
228
|
-
while (retries > tryNumber) {
|
|
229
|
-
const result = await check(page, context);
|
|
230
|
-
if (result) return (0, _neverthrow.ok)(true);
|
|
231
|
-
tryNumber++;
|
|
232
|
-
}
|
|
233
|
-
return (0, _neverthrow.ok)(false);
|
|
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"));
|
|
234
159
|
}
|
|
235
|
-
async function
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
try {
|
|
239
|
-
let imported = undefined;
|
|
240
|
-
switch (functionNameDepth) {
|
|
241
|
-
case 1:
|
|
242
|
-
imported = await Promise.resolve(require(`../../../${functionNameParts[0]}.ts`));
|
|
243
|
-
break;
|
|
244
|
-
case 2:
|
|
245
|
-
imported = await Promise.resolve(require(`../../../${functionNameParts[0]}/${functionNameParts[1]}.ts`));
|
|
246
|
-
break;
|
|
247
|
-
case 3:
|
|
248
|
-
imported = await Promise.resolve(require(`../../../${functionNameParts[0]}/${functionNameParts[1]}/${functionNameParts[2]}.ts`));
|
|
249
|
-
break;
|
|
250
|
-
case 4:
|
|
251
|
-
imported = await Promise.resolve(require(`../../../${functionNameParts[0]}/${functionNameParts[1]}/${functionNameParts[2]}/${functionNameParts[3]}.ts`));
|
|
252
|
-
break;
|
|
253
|
-
case 5:
|
|
254
|
-
imported = await Promise.resolve(require(`../../../${functionNameParts[0]}/${functionNameParts[1]}/${functionNameParts[2]}/${functionNameParts[3]}/${functionNameParts[4]}.ts`));
|
|
255
|
-
break;
|
|
256
|
-
case 6:
|
|
257
|
-
imported = await Promise.resolve(require(`../../../${functionNameParts[0]}/${functionNameParts[1]}/${functionNameParts[2]}/${functionNameParts[3]}/${functionNameParts[4]}/${functionNameParts[5]}.ts`));
|
|
258
|
-
break;
|
|
259
|
-
default:
|
|
260
|
-
return (0, _neverthrow.err)(new _errors.MaxLevelsExceededError(5));
|
|
160
|
+
export async function checkAuthSessionWithRetries(page, context, retries = 3) {
|
|
161
|
+
if (retries === 0) {
|
|
162
|
+
return ok(false);
|
|
261
163
|
}
|
|
262
|
-
|
|
263
|
-
|
|
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);
|
|
264
169
|
}
|
|
265
|
-
if (
|
|
266
|
-
|
|
267
|
-
type: "async-generator",
|
|
268
|
-
generator: imported.default
|
|
269
|
-
});
|
|
170
|
+
if (importResult.value.type !== "async") {
|
|
171
|
+
return err(new InvalidCheckError("Check function is not an async function"));
|
|
270
172
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
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++;
|
|
276
179
|
}
|
|
277
|
-
return (
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
180
|
+
return ok(false);
|
|
181
|
+
}
|
|
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"));
|
|
281
228
|
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
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;
|
|
285
238
|
}
|
|
286
|
-
|
|
287
|
-
}
|
|
288
|
-
}
|
|
239
|
+
}
|