@intuned/runtime-dev 1.0.0-udas.4 → 1.0.0-udas.6
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.
|
@@ -35,16 +35,24 @@ async function getContextStorageState(context) {
|
|
|
35
35
|
};
|
|
36
36
|
}
|
|
37
37
|
async function setContextStorageState(context, storage) {
|
|
38
|
+
console.log("Setting storage state");
|
|
38
39
|
await context.intunedSetStorageState(storage);
|
|
40
|
+
console.log("Storage state set");
|
|
39
41
|
const sessionStorage = storage.sessionStorage;
|
|
42
|
+
console.log("Adding init script session");
|
|
40
43
|
await context.addInitScript(storage => {
|
|
41
44
|
for (const {
|
|
42
45
|
origin,
|
|
43
46
|
sessionStorage
|
|
44
47
|
} of storage) {
|
|
45
48
|
if (window.location.origin === origin) {
|
|
46
|
-
for (const item of sessionStorage)
|
|
49
|
+
for (const item of sessionStorage) {
|
|
50
|
+
const existingItem = window.sessionStorage.getItem(item.name);
|
|
51
|
+
if (existingItem !== null) continue;
|
|
52
|
+
window.sessionStorage.setItem(item.name, item.value);
|
|
53
|
+
}
|
|
47
54
|
}
|
|
48
55
|
}
|
|
49
56
|
}, sessionStorage);
|
|
57
|
+
console.log("Added init script session");
|
|
50
58
|
}
|
|
@@ -9,4 +9,4 @@ export declare function runApiGenerator<ResultType = any, _YieldType = any, _Nex
|
|
|
9
9
|
}): AsyncGenerator<_YieldType, RunApiResult<ResultType, RunApiResultWithSessionOk>, _NextType>;
|
|
10
10
|
export declare function runApiGenerator<ResultType = any, _YieldType = any, _NextType = any>(params: ExtendedRunApiParameters): AsyncGenerator<_YieldType, RunApiResult<ResultType>, _NextType>;
|
|
11
11
|
export declare function runApi<ResultType = any>(params: ExtendedRunApiParameters): Promise<RunApiResult<ResultType>>;
|
|
12
|
-
export declare function checkAuthSessionWithRetries(page: Page, context: BrowserContext,
|
|
12
|
+
export declare function checkAuthSessionWithRetries(page: Page, context: BrowserContext, checkFn: (..._: any) => Promise<boolean>, retries?: number): Promise<Result<boolean, RunAutomationError>>;
|
|
@@ -64,40 +64,15 @@ async function* runApiGenerator({
|
|
|
64
64
|
tracing,
|
|
65
65
|
auth
|
|
66
66
|
} = inputParseResult.data;
|
|
67
|
-
|
|
67
|
+
const abortSymbol = Symbol("abort");
|
|
68
|
+
const abortPromise = new Promise(resolve => {
|
|
69
|
+
if (!abortSignal) return;
|
|
70
|
+
abortSignal.addEventListener("abort", () => {
|
|
71
|
+
resolve(abortSymbol);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
68
74
|
let context;
|
|
69
75
|
let downloadsPath;
|
|
70
|
-
if (runOptions.environment === "standalone") {
|
|
71
|
-
downloadsPath = (0, _downloadDirectory.getDownloadDirectoryPath)();
|
|
72
|
-
const {
|
|
73
|
-
headless,
|
|
74
|
-
proxy
|
|
75
|
-
} = runOptions;
|
|
76
|
-
console.log("Running in standalone mode", {
|
|
77
|
-
headless,
|
|
78
|
-
proxy,
|
|
79
|
-
downloadsPath
|
|
80
|
-
});
|
|
81
|
-
({
|
|
82
|
-
page,
|
|
83
|
-
context
|
|
84
|
-
} = await (0, _getPlaywrightConstructs.getProductionPlaywrightConstructs)({
|
|
85
|
-
headless,
|
|
86
|
-
proxy,
|
|
87
|
-
downloadsPath,
|
|
88
|
-
storageState: auth === null || auth === void 0 ? void 0 : auth.session
|
|
89
|
-
}));
|
|
90
|
-
console.log("Got page and context");
|
|
91
|
-
} else {
|
|
92
|
-
const {
|
|
93
|
-
mode,
|
|
94
|
-
cdpAddress
|
|
95
|
-
} = runOptions;
|
|
96
|
-
({
|
|
97
|
-
page,
|
|
98
|
-
context
|
|
99
|
-
} = await (0, _getPlaywrightConstructs.getPlaywrightConstructsForMode)(mode, cdpAddress, auth === null || auth === void 0 ? void 0 : auth.session));
|
|
100
|
-
}
|
|
101
76
|
async function saveTraceIfNeeded({
|
|
102
77
|
errorMessage
|
|
103
78
|
}) {
|
|
@@ -105,46 +80,80 @@ async function* runApiGenerator({
|
|
|
105
80
|
return;
|
|
106
81
|
}
|
|
107
82
|
try {
|
|
108
|
-
|
|
83
|
+
var _context;
|
|
84
|
+
await ((_context = context) === null || _context === void 0 ? void 0 : _context.tracing.stop({
|
|
109
85
|
path: tracing.filePath
|
|
110
|
-
});
|
|
86
|
+
}));
|
|
111
87
|
} catch (error) {
|
|
112
88
|
console.log(errorMessage, error === null || error === void 0 ? void 0 : error.message);
|
|
113
89
|
await (0, _fsExtra.remove)(tracing.filePath);
|
|
114
90
|
}
|
|
115
91
|
}
|
|
116
|
-
if (tracing.enabled) {
|
|
117
|
-
console.log("Starting trace");
|
|
118
|
-
await context.tracing.start({
|
|
119
|
-
screenshots: true,
|
|
120
|
-
snapshots: true,
|
|
121
|
-
sources: true
|
|
122
|
-
});
|
|
123
|
-
traceStarted = true;
|
|
124
|
-
}
|
|
125
|
-
const abortSymbol = Symbol("abort");
|
|
126
|
-
const abortPromise = new Promise(resolve => {
|
|
127
|
-
if (!abortSignal) return;
|
|
128
|
-
abortSignal.addEventListener("abort", () => {
|
|
129
|
-
resolve(abortSymbol);
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
if (auth && auth.session.type === "state") {
|
|
133
|
-
const state = auth.session.state;
|
|
134
|
-
if (state === undefined || state === null) {
|
|
135
|
-
return (0, _neverthrow.err)(new _errors.AuthRequiredError());
|
|
136
|
-
}
|
|
137
|
-
await (0, _contextStorageStateHelpers.setContextStorageState)(context, state);
|
|
138
|
-
}
|
|
139
92
|
async function* runAutomation() {
|
|
140
93
|
var _getExecutionContext;
|
|
141
|
-
|
|
94
|
+
let page;
|
|
95
|
+
const validatedModuleResult = await importUsingImportFunction(automationFunction.name, importFunction);
|
|
96
|
+
if (validatedModuleResult.isErr()) {
|
|
97
|
+
return (0, _neverthrow.err)(validatedModuleResult.error);
|
|
98
|
+
}
|
|
99
|
+
const importedModule = validatedModuleResult.value;
|
|
100
|
+
let checkFn;
|
|
142
101
|
if (auth !== null && auth !== void 0 && auth.runCheck) {
|
|
143
102
|
if (!auth.session) {
|
|
144
103
|
return (0, _neverthrow.err)(new _errors.AuthRequiredError());
|
|
145
104
|
}
|
|
105
|
+
const checkImportResult = await importUsingImportFunction(`${_constants.AUTH_SESSIONS_FOLDER_NAME}/check`, importFunction);
|
|
106
|
+
if (checkImportResult.isErr()) {
|
|
107
|
+
return (0, _neverthrow.err)(checkImportResult.error);
|
|
108
|
+
}
|
|
109
|
+
if (checkImportResult.value.type !== "async") {
|
|
110
|
+
return (0, _neverthrow.err)(new _errors.InvalidCheckError("Check function is not an async function"));
|
|
111
|
+
}
|
|
112
|
+
checkFn = checkImportResult.value.func;
|
|
113
|
+
}
|
|
114
|
+
if (auth && auth.session.type === "state") {
|
|
115
|
+
const state = auth.session.state;
|
|
116
|
+
if (state === undefined || state === null) {
|
|
117
|
+
return (0, _neverthrow.err)(new _errors.AuthRequiredError());
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (runOptions.environment === "standalone") {
|
|
121
|
+
downloadsPath = (0, _downloadDirectory.getDownloadDirectoryPath)();
|
|
122
|
+
const {
|
|
123
|
+
headless,
|
|
124
|
+
proxy
|
|
125
|
+
} = runOptions;
|
|
126
|
+
({
|
|
127
|
+
page,
|
|
128
|
+
context
|
|
129
|
+
} = await (0, _getPlaywrightConstructs.getProductionPlaywrightConstructs)({
|
|
130
|
+
headless,
|
|
131
|
+
proxy,
|
|
132
|
+
downloadsPath,
|
|
133
|
+
storageState: auth === null || auth === void 0 ? void 0 : auth.session
|
|
134
|
+
}));
|
|
135
|
+
} else {
|
|
136
|
+
const {
|
|
137
|
+
mode,
|
|
138
|
+
cdpAddress
|
|
139
|
+
} = runOptions;
|
|
140
|
+
({
|
|
141
|
+
page,
|
|
142
|
+
context
|
|
143
|
+
} = await (0, _getPlaywrightConstructs.getPlaywrightConstructsForMode)(mode, cdpAddress, auth === null || auth === void 0 ? void 0 : auth.session));
|
|
144
|
+
}
|
|
145
|
+
if (tracing.enabled) {
|
|
146
|
+
await context.tracing.start({
|
|
147
|
+
screenshots: true,
|
|
148
|
+
snapshots: true,
|
|
149
|
+
sources: true
|
|
150
|
+
});
|
|
151
|
+
traceStarted = true;
|
|
152
|
+
}
|
|
153
|
+
(0, _cleanEnvironmentVariables.cleanEnvironmentVariables)();
|
|
154
|
+
if (checkFn !== undefined) {
|
|
146
155
|
console.log("Running auth check");
|
|
147
|
-
const authCheckResult = await checkAuthSessionWithRetries(page, context,
|
|
156
|
+
const authCheckResult = await checkAuthSessionWithRetries(page, context, checkFn, 2);
|
|
148
157
|
if (authCheckResult.isErr()) {
|
|
149
158
|
const error = authCheckResult.error;
|
|
150
159
|
if (["APINotFoundError", "InvalidAPIError"].includes(error.code)) {
|
|
@@ -157,14 +166,10 @@ async function* runApiGenerator({
|
|
|
157
166
|
}
|
|
158
167
|
}
|
|
159
168
|
console.log("Running automation");
|
|
160
|
-
const
|
|
161
|
-
const validatedModuleResult = await importUsingImportFunction(automationFunction.name, importFunction);
|
|
162
|
-
if (validatedModuleResult.isErr()) {
|
|
163
|
-
return validatedModuleResult;
|
|
164
|
-
}
|
|
169
|
+
const automationFunctionParameters = [...(automationFunction.params !== undefined ? [automationFunction.params] : []), page, context];
|
|
165
170
|
let result;
|
|
166
|
-
if (
|
|
167
|
-
const generator =
|
|
171
|
+
if (importedModule.type === "async-generator") {
|
|
172
|
+
const generator = importedModule.generator(...automationFunctionParameters);
|
|
168
173
|
let next = undefined;
|
|
169
174
|
while (true) {
|
|
170
175
|
const generatorResult = await generator.next(...(next ? [next] : []));
|
|
@@ -176,7 +181,7 @@ async function* runApiGenerator({
|
|
|
176
181
|
break;
|
|
177
182
|
}
|
|
178
183
|
} else {
|
|
179
|
-
result = await
|
|
184
|
+
result = await importedModule.func(...automationFunctionParameters);
|
|
180
185
|
}
|
|
181
186
|
return (0, _neverthrow.ok)({
|
|
182
187
|
result,
|
|
@@ -201,10 +206,11 @@ async function* runApiGenerator({
|
|
|
201
206
|
} catch (error) {
|
|
202
207
|
return (0, _neverthrow.err)(new _errors.AutomationError(error));
|
|
203
208
|
} finally {
|
|
209
|
+
var _context2;
|
|
204
210
|
await saveTraceIfNeeded({
|
|
205
211
|
errorMessage: "failed to save trace"
|
|
206
212
|
});
|
|
207
|
-
await context.close();
|
|
213
|
+
await ((_context2 = context) === null || _context2 === void 0 ? void 0 : _context2.close());
|
|
208
214
|
if (downloadsPath !== undefined) {
|
|
209
215
|
await fs.remove(downloadsPath);
|
|
210
216
|
}
|
|
@@ -221,22 +227,14 @@ async function runApi(params) {
|
|
|
221
227
|
}
|
|
222
228
|
return (0, _neverthrow.err)(new _errors.InvalidApiError("Expected API to be async function, got async generator"));
|
|
223
229
|
}
|
|
224
|
-
async function checkAuthSessionWithRetries(page, context,
|
|
230
|
+
async function checkAuthSessionWithRetries(page, context, checkFn, retries = 3) {
|
|
225
231
|
if (retries === 0) {
|
|
226
232
|
return (0, _neverthrow.ok)(false);
|
|
227
233
|
}
|
|
228
234
|
let tryNumber = 0;
|
|
229
235
|
console.log("Checking auth session with retries", `${_constants.AUTH_SESSIONS_FOLDER_NAME}/check`);
|
|
230
|
-
const importResult = await importUsingImportFunction(`${_constants.AUTH_SESSIONS_FOLDER_NAME}/check`, importFunction);
|
|
231
|
-
if (importResult.isErr()) {
|
|
232
|
-
return (0, _neverthrow.err)(importResult.error);
|
|
233
|
-
}
|
|
234
|
-
if (importResult.value.type !== "async") {
|
|
235
|
-
return (0, _neverthrow.err)(new _errors.InvalidCheckError("Check function is not an async function"));
|
|
236
|
-
}
|
|
237
|
-
const check = importResult.value.func;
|
|
238
236
|
while (retries > tryNumber) {
|
|
239
|
-
const result = await
|
|
237
|
+
const result = await checkFn(page, context);
|
|
240
238
|
if (result) return (0, _neverthrow.ok)(true);
|
|
241
239
|
tryNumber++;
|
|
242
240
|
}
|
|
@@ -262,7 +260,6 @@ async function importUsingImportFunction(path, importFunction) {
|
|
|
262
260
|
}
|
|
263
261
|
return (0, _neverthrow.err)(new _errors.InvalidApiError("API file path does not have a default async function/generator export"));
|
|
264
262
|
} catch (error) {
|
|
265
|
-
console.error(error);
|
|
266
263
|
return (0, _neverthrow.err)(new _errors.ApiNotFoundError(path));
|
|
267
264
|
}
|
|
268
265
|
}
|