@intuned/runtime-dev 1.2.1-hooks.1 → 1.2.1-hooks.11

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 (38) hide show
  1. package/dist/commands/api/run.js +1 -2
  2. package/dist/commands/auth-sessions/load.js +3 -3
  3. package/dist/commands/auth-sessions/run-check.js +1 -2
  4. package/dist/commands/auth-sessions/run-create.js +12 -35
  5. package/dist/commands/interface/run.d.ts +1 -1
  6. package/dist/commands/interface/run.js +14 -40
  7. package/dist/commands/intuned-cli/controller/api.js +1 -2
  8. package/dist/commands/intuned-cli/controller/authSession.d.ts +6 -6
  9. package/dist/commands/intuned-cli/controller/authSession.js +3 -4
  10. package/dist/commands/intuned-cli/helpers/auth.d.ts +1 -1
  11. package/dist/commands/intuned-cli/helpers/errors.d.ts +2 -2
  12. package/dist/commands/intuned-cli/helpers/errors.js +8 -4
  13. package/dist/common/asyncLocalStorage/index.d.ts +1 -1
  14. package/dist/common/initializeContextHook.d.ts +16 -0
  15. package/dist/common/initializeContextHook.js +15 -0
  16. package/dist/common/playwrightContext.d.ts +49 -0
  17. package/dist/common/playwrightContext.js +237 -0
  18. package/dist/common/runApi/importUsingImportFunction.d.ts +9 -0
  19. package/dist/common/runApi/importUsingImportFunction.js +46 -0
  20. package/dist/common/runApi/index.d.ts +2 -7
  21. package/dist/common/runApi/index.js +87 -174
  22. package/dist/common/runApi/types.d.ts +25 -29
  23. package/dist/common/runApi/types.js +1 -2
  24. package/dist/index.d.ts +1 -2
  25. package/dist/index.js +7 -20
  26. package/dist/runtime/export.d.ts +49 -43
  27. package/dist/runtime/index.d.ts +1 -3
  28. package/dist/runtime/index.js +6 -19
  29. package/dist/runtime/store.d.ts +2 -0
  30. package/dist/runtime/store.js +23 -0
  31. package/package.json +2 -2
  32. package/.npmrc.wtf +0 -1
  33. package/dist/common/getPlaywrightConstructs.d.ts +0 -31
  34. package/dist/common/getPlaywrightConstructs.js +0 -234
  35. package/dist/runtime/getCdpAddress.d.ts +0 -1
  36. package/dist/runtime/getCdpAddress.js +0 -11
  37. package/dist/runtime/requestMoreInfo.d.ts +0 -18
  38. package/dist/runtime/requestMoreInfo.js +0 -25
@@ -0,0 +1,237 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.browserScriptsFile = void 0;
7
+ exports.getRemotePlaywrightContext = getRemotePlaywrightContext;
8
+ exports.launchChromium = launchChromium;
9
+ exports.loadSessionToContext = loadSessionToContext;
10
+ exports.withPlaywrightContext = withPlaywrightContext;
11
+ var playwright = _interopRequireWildcard(require("playwright"));
12
+ var _fsExtra = _interopRequireWildcard(require("fs-extra"));
13
+ var fs = _fsExtra;
14
+ var _contextStorageStateHelpers = require("./contextStorageStateHelpers");
15
+ var _path = _interopRequireWildcard(require("path"));
16
+ var _fileUtils = require("../commands/common/utils/fileUtils");
17
+ var _waitOn = _interopRequireDefault(require("wait-on"));
18
+ var _errors = require("./runApi/errors");
19
+ var _neverthrow = require("neverthrow");
20
+ var _initializeContextHook = require("./initializeContextHook");
21
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
22
+ 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); }
23
+ 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; }
24
+ async function createUserDirWithPreferences() {
25
+ const playwrightTempDir = await (0, _fsExtra.mkdtemp)("/tmp/pw-");
26
+ const userDir = (0, _path.join)(playwrightTempDir, "userdir");
27
+ const defaultDir = (0, _path.join)(userDir, "Default");
28
+ await (0, _fsExtra.mkdir)(defaultDir, {
29
+ recursive: true
30
+ });
31
+ const preferences = {
32
+ plugins: {
33
+ always_open_pdf_externally: true
34
+ }
35
+ };
36
+ await (0, _fsExtra.writeFile)((0, _path.join)(defaultDir, "Preferences"), JSON.stringify(preferences));
37
+ return userDir;
38
+ }
39
+ async function launchChromium({
40
+ proxy,
41
+ headless = true,
42
+ downloadsPath,
43
+ cdpAddress,
44
+ cdpPort
45
+ }) {
46
+ if (cdpAddress) {
47
+ const browser = await playwright.chromium.connectOverCDP(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);
53
+ if (!page) {
54
+ page = await context.newPage();
55
+ }
56
+ return {
57
+ page,
58
+ context
59
+ };
60
+ }
61
+ const extraArgs = [];
62
+ const userDataDir = await createUserDirWithPreferences();
63
+ if (cdpPort) {
64
+ extraArgs.push(`--remote-debugging-port=${cdpPort}`);
65
+ }
66
+ const context = await playwright.chromium.launchPersistentContext(userDataDir, {
67
+ headless,
68
+ viewport: null,
69
+ proxy,
70
+ downloadsPath,
71
+ args: extraArgs
72
+ });
73
+ if (cdpPort) {
74
+ const createdCdpAddress = getCdpAddress(cdpPort);
75
+ await waitOnCdpAddress(createdCdpAddress);
76
+ }
77
+ context.once("close", async () => {
78
+ try {
79
+ await (0, _fsExtra.rm)(userDataDir, {
80
+ recursive: true,
81
+ force: true,
82
+ retryDelay: 1000,
83
+ maxRetries: 5
84
+ });
85
+ } catch (error) {
86
+ console.error("Failed to remove user data dir", error);
87
+ }
88
+ });
89
+ const page = context.pages().at(0) ?? (await context.newPage());
90
+ return {
91
+ page,
92
+ context
93
+ };
94
+ }
95
+ const browserScriptsFile = exports.browserScriptsFile = _path.default.join(__dirname, "./assets/browser_scripts.js");
96
+ async function withPlaywrightContext({
97
+ cdpAddress,
98
+ proxy,
99
+ headless = true,
100
+ downloadsPath,
101
+ importFunction,
102
+ apiName,
103
+ apiParameters
104
+ }, fn) {
105
+ let context;
106
+ let page;
107
+ try {
108
+ const initializeContextHookResult = await (0, _initializeContextHook.loadInitializeContextHook)({
109
+ importFunction
110
+ });
111
+ if (initializeContextHookResult.isErr()) {
112
+ return initializeContextHookResult;
113
+ }
114
+ const initializeContextHook = initializeContextHookResult.value;
115
+ if (initializeContextHook === null) {
116
+ if (cdpAddress !== undefined) {
117
+ ({
118
+ page,
119
+ context
120
+ } = await launchChromium({
121
+ cdpAddress
122
+ }));
123
+ } else {
124
+ ({
125
+ page,
126
+ context
127
+ } = await launchChromium({
128
+ proxy,
129
+ headless,
130
+ downloadsPath
131
+ }));
132
+ }
133
+ return await fn(context, page);
134
+ }
135
+ let hookCdpUrl = null;
136
+ if (cdpAddress) {
137
+ hookCdpUrl = cdpAddress;
138
+ ({
139
+ context,
140
+ page
141
+ } = await launchChromium({
142
+ cdpAddress
143
+ }));
144
+ } else {
145
+ const port = 9222;
146
+ hookCdpUrl = getCdpAddress(port);
147
+ ({
148
+ context,
149
+ page
150
+ } = await launchChromium({
151
+ proxy,
152
+ headless,
153
+ downloadsPath,
154
+ cdpPort: port
155
+ }));
156
+ }
157
+ let hookResult;
158
+ try {
159
+ hookResult = await initializeContextHook({
160
+ apiName,
161
+ apiParameters,
162
+ cdpUrl: hookCdpUrl
163
+ });
164
+ } catch (error) {
165
+ return (0, _neverthrow.err)(new _errors.AutomationError(error));
166
+ }
167
+ if (!hookResult) {
168
+ return await fn(context, page);
169
+ }
170
+ const {
171
+ page: newPage,
172
+ context: newContext
173
+ } = hookResult;
174
+ try {
175
+ return await fn(newContext, newPage ?? page);
176
+ } finally {
177
+ await newContext.close();
178
+ }
179
+ } finally {
180
+ await context?.close();
181
+ }
182
+ }
183
+ async function loadSessionToContext({
184
+ context,
185
+ session
186
+ }) {
187
+ let sessionToLoad;
188
+ if (session.type === "state") {
189
+ const state = session.state;
190
+ if (state === undefined || state === null) {
191
+ return;
192
+ }
193
+ sessionToLoad = state;
194
+ } else {
195
+ const fullPath = (0, _fileUtils.getFullPathInProject)(session.path);
196
+ sessionToLoad = await fs.readJson(fullPath);
197
+ }
198
+ await (0, _contextStorageStateHelpers.setStorageState)(context, sessionToLoad);
199
+ }
200
+ function getCdpAddress(port) {
201
+ return `http://localhost:${port}`;
202
+ }
203
+ async function waitOnCdpAddress(cdpAddress) {
204
+ const cdpAddressWithoutProtocol = cdpAddress.replace("http://", "").replace("https://", "").replace("localhost", "127.0.0.1");
205
+ await (0, _waitOn.default)({
206
+ resources: [`http-get://${cdpAddressWithoutProtocol}/json/version`],
207
+ delay: 100,
208
+ interval: 100,
209
+ timeout: 5000,
210
+ tcpTimeout: 1000,
211
+ window: 1000
212
+ });
213
+ }
214
+ async function getRemotePlaywrightContext(cdpAddress) {
215
+ const playwright = await Promise.resolve().then(() => _interopRequireWildcard(require("playwright")));
216
+ let browser = null;
217
+ if (!cdpAddress) {
218
+ throw new Error("cdpAddress is required");
219
+ }
220
+ try {
221
+ await waitOnCdpAddress(cdpAddress);
222
+ } catch (error) {
223
+ console.error("Failed to connect to the browser");
224
+ process.exit(128 + 9);
225
+ }
226
+ try {
227
+ browser = await playwright.chromium.connectOverCDP(cdpAddress);
228
+ } catch (e) {
229
+ console.log(e);
230
+ throw new Error("failed to connect to the browser");
231
+ }
232
+ const context = browser.contexts()[0];
233
+ return {
234
+ browser,
235
+ context
236
+ };
237
+ }
@@ -0,0 +1,9 @@
1
+ import { Result } from "neverthrow";
2
+ import { RunAutomationError } from "./errors";
3
+ import { ExtendedRunApiParameters } from "./types";
4
+ export type ImportFunction = ExtendedRunApiParameters["importFunction"];
5
+ export declare function importUsingImportFunction<_ReturnType = any>({ path, allowGenerators, importFunction, }: {
6
+ path: string;
7
+ importFunction: ImportFunction;
8
+ allowGenerators?: boolean;
9
+ }): 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 _errors = require("./errors");
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 _errors.ApiNotFoundError(path));
19
+ }
20
+ return (0, _neverthrow.err)(new _errors.AutomationError(importedResult.error.error));
21
+ }
22
+ const imported = importedResult.value;
23
+ if (!imported || !imported.default || !imported.default.constructor) {
24
+ return (0, _neverthrow.err)(new _errors.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 _errors.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 _errors.InvalidApiError(`${path} default export must be an async function`));
43
+ } catch (error) {
44
+ return (0, _neverthrow.err)(new _errors.AutomationError(error));
45
+ }
46
+ }
@@ -4,13 +4,8 @@ import { Page, BrowserContext } from "playwright";
4
4
  import { ExtendedRunApiParameters, RunApiResult, RunApiResultWithSessionOk } from "./types";
5
5
  export * from "./types";
6
6
  export * from "./errors";
7
- export declare function runApiGenerator<ResultType = any, _YieldType = any, _NextType = any>(params: ExtendedRunApiParameters & {
8
- retrieveSession: true;
9
- }): AsyncGenerator<_YieldType, RunApiResult<ResultType, RunApiResultWithSessionOk>, _NextType>;
10
- export declare function runApiGenerator<ResultType = any, _YieldType = any, _NextType = any>(params: ExtendedRunApiParameters): AsyncGenerator<_YieldType, RunApiResult<ResultType>, _NextType>;
11
- export declare function runApi<ResultType = any>(params: ExtendedRunApiParameters & {
7
+ export declare function runApi<ResultType = any>(input: ExtendedRunApiParameters & {
12
8
  retrieveSession: true;
13
9
  }): Promise<RunApiResult<ResultType, RunApiResultWithSessionOk>>;
14
- export declare function runApi<ResultType = any>(params: ExtendedRunApiParameters): Promise<RunApiResult<ResultType>>;
10
+ export declare function runApi<ResultType = any>(input: ExtendedRunApiParameters): Promise<RunApiResult<ResultType>>;
15
11
  export declare function checkAuthSessionWithRetries(page: Page, context: BrowserContext, checkFn: (..._: any) => Promise<boolean>, retries?: number): Promise<Result<boolean, RunAutomationError>>;
16
- export type ImportFunction = ExtendedRunApiParameters["importFunction"];
@@ -4,13 +4,11 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  var _exportNames = {
7
- runApiGenerator: true,
8
7
  runApi: true,
9
8
  checkAuthSessionWithRetries: true
10
9
  };
11
10
  exports.checkAuthSessionWithRetries = checkAuthSessionWithRetries;
12
11
  exports.runApi = runApi;
13
- exports.runApiGenerator = runApiGenerator;
14
12
  var _downloadDirectory = require("../../runtime/downloadDirectory");
15
13
  var _asyncLocalStorage = require("../asyncLocalStorage");
16
14
  var _fsExtra = _interopRequireWildcard(require("fs-extra"));
@@ -30,7 +28,7 @@ Object.keys(_errors).forEach(function (key) {
30
28
  });
31
29
  });
32
30
  var _constants = require("../constants");
33
- var _getPlaywrightConstructs = require("../getPlaywrightConstructs");
31
+ var _playwrightContext = require("../playwrightContext");
34
32
  var _types = require("./types");
35
33
  Object.keys(_types).forEach(function (key) {
36
34
  if (key === "default" || key === "__esModule") return;
@@ -45,12 +43,13 @@ Object.keys(_types).forEach(function (key) {
45
43
  });
46
44
  var _formatZodError = require("../formatZodError");
47
45
  var _cleanEnvironmentVariables = require("../cleanEnvironmentVariables");
46
+ var _importUsingImportFunction = require("./importUsingImportFunction");
48
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); }
49
48
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
50
- async function* runApiGenerator({
51
- retrieveSession = false,
49
+ async function runApi({
52
50
  abortSignal,
53
51
  importFunction,
52
+ retrieveSession = false,
54
53
  ...input
55
54
  }) {
56
55
  let traceStarted = false;
@@ -59,7 +58,10 @@ async function* runApiGenerator({
59
58
  return (0, _neverthrow.err)(new _errors.InternalInvalidInputError("Input validation failed", (0, _formatZodError.formatZodError)(inputParseResult.error)));
60
59
  }
61
60
  const {
62
- automationFunction,
61
+ automationFunction: {
62
+ name,
63
+ params
64
+ },
63
65
  runOptions,
64
66
  tracing,
65
67
  auth
@@ -71,162 +73,103 @@ async function* runApiGenerator({
71
73
  resolve(abortSymbol);
72
74
  });
73
75
  });
74
- let context;
75
- let downloadsPath;
76
- async function saveTraceIfNeeded({
77
- errorMessage
78
- }) {
79
- if (!tracing.enabled || !traceStarted) {
80
- return;
81
- }
82
- try {
83
- await context?.tracing.stop({
84
- path: tracing.filePath
85
- });
86
- } catch (error) {
87
- console.log(errorMessage, error?.message);
88
- await (0, _fsExtra.remove)(tracing.filePath);
89
- }
90
- }
91
- async function* runAutomation() {
92
- let page;
93
- const validatedModuleResult = await importUsingImportFunction(automationFunction.name, importFunction);
76
+ async function runAutomation() {
77
+ const validatedModuleResult = await (0, _importUsingImportFunction.importUsingImportFunction)({
78
+ path: name,
79
+ importFunction
80
+ });
94
81
  if (validatedModuleResult.isErr()) {
95
82
  return (0, _neverthrow.err)(validatedModuleResult.error);
96
83
  }
97
- const importedModule = validatedModuleResult.value;
98
- let checkFn;
99
- if (auth?.runCheck) {
100
- if (!auth.session) {
101
- return (0, _neverthrow.err)(new _errors.AuthRequiredError());
102
- }
103
- const checkImportResult = await importUsingImportFunction(`${_constants.AUTH_SESSIONS_FOLDER_NAME}/check`, importFunction);
104
- if (checkImportResult.isErr()) {
105
- return (0, _neverthrow.err)(checkImportResult.error);
106
- }
107
- if (checkImportResult.value.type !== "async") {
108
- return (0, _neverthrow.err)(new _errors.InvalidCheckError("Check function is not an async function"));
109
- }
110
- checkFn = checkImportResult.value.func;
111
- }
84
+ const automationFunction = validatedModuleResult.value;
112
85
  if (auth && auth.session.type === "state") {
113
86
  const state = auth.session.state;
114
87
  if (state === undefined || state === null) {
115
88
  return (0, _neverthrow.err)(new _errors.AuthRequiredError());
116
89
  }
117
90
  }
118
- if (runOptions.environment === "standalone") {
119
- downloadsPath = (0, _downloadDirectory.getDownloadDirectoryPath)();
120
- const {
121
- headless,
122
- proxy
123
- } = runOptions;
124
- ({
125
- page,
126
- context
127
- } = await (0, _getPlaywrightConstructs.getProductionPlaywrightConstructs)({
128
- headless,
129
- proxy,
130
- downloadsPath,
131
- storageState: auth?.session,
132
- importFunction
133
- }));
134
- } else {
135
- const {
136
- mode,
137
- cdpAddress
138
- } = runOptions;
139
- ({
140
- page,
141
- context
142
- } = await (0, _getPlaywrightConstructs.getPlaywrightConstructsForMode)(mode, cdpAddress, auth?.session, importFunction));
143
- }
144
- if (tracing.enabled) {
145
- await context.tracing.start({
146
- screenshots: true,
147
- snapshots: true,
148
- sources: true
149
- });
150
- traceStarted = true;
151
- }
152
- (0, _cleanEnvironmentVariables.cleanEnvironmentVariables)();
153
- if (checkFn !== undefined) {
154
- try {
155
- console.log("Running auth check");
156
- const authCheckResult = await checkAuthSessionWithRetries(page, context, checkFn, 2);
157
- if (authCheckResult.isErr()) {
158
- const error = authCheckResult.error;
159
- if (["APINotFoundError", "InvalidAPIError"].includes(error.code)) {
160
- return (0, _neverthrow.err)(new _errors.InvalidCheckError(`Auth session check function failed`, error));
161
- }
162
- return authCheckResult;
91
+ const playwrightContextParameters = {
92
+ apiName: name,
93
+ apiParameters: params,
94
+ importFunction
95
+ };
96
+ const runAutomationWithContext = async (context, page) => {
97
+ async function saveTraceIfNeeded({
98
+ errorMessage
99
+ }) {
100
+ if (!tracing.enabled || !traceStarted) {
101
+ return;
163
102
  }
164
- if (!authCheckResult.value) {
165
- return (0, _neverthrow.err)(new _errors.AuthCheckFailedError());
103
+ try {
104
+ await context?.tracing.stop({
105
+ path: tracing.filePath
106
+ });
107
+ } catch (error) {
108
+ console.log(errorMessage, error?.message);
109
+ await (0, _fsExtra.remove)(tracing.filePath);
166
110
  }
167
- } catch (error) {
168
- return (0, _neverthrow.err)(new _errors.AuthCheckFailedError());
169
111
  }
170
- }
171
- const automationFunctionParameters = [...(automationFunction.params !== undefined ? [automationFunction.params] : []), page, context];
172
- let result;
173
- if (importedModule.type === "async-generator") {
174
- const generator = importedModule.generator(...automationFunctionParameters);
175
- let next = undefined;
176
- while (true) {
177
- const generatorResult = await generator.next(...(next ? [next] : []));
178
- if (!generatorResult.done) {
179
- next = yield generatorResult.value;
180
- continue;
181
- }
182
- result = generatorResult.value;
183
- break;
112
+ if (auth) {
113
+ await (0, _playwrightContext.loadSessionToContext)({
114
+ context,
115
+ session: auth.session
116
+ });
184
117
  }
185
- } else {
186
- result = await importedModule.func(...automationFunctionParameters);
187
- }
188
- return (0, _neverthrow.ok)({
189
- result,
190
- extendedPayloads: (0, _asyncLocalStorage.getExecutionContext)()?.extendedPayloads,
191
- session: retrieveSession ? await (0, _contextStorageStateHelpers.getStorageState)(context) : undefined
192
- });
193
- }
194
- try {
195
- const generator = runAutomation();
196
- let next = undefined;
197
- while (true) {
198
- const result = await Promise.race([generator.next(await next), abortPromise]);
199
- if (result === abortSymbol) {
200
- return (0, _neverthrow.err)(new _errors.AbortedError());
118
+ const scriptContent = await fs.readFile(_playwrightContext.browserScriptsFile, "utf-8");
119
+ await context.addInitScript({
120
+ content: scriptContent
121
+ });
122
+ for (const page of context.pages()) {
123
+ await page.evaluate(scriptContent);
201
124
  }
202
- if (!result.done) {
203
- next = yield result.value;
204
- continue;
125
+ if (tracing.enabled) {
126
+ await context.tracing.start({
127
+ screenshots: true,
128
+ snapshots: true,
129
+ sources: true
130
+ });
131
+ traceStarted = true;
205
132
  }
206
- return result.value;
207
- }
208
- } catch (error) {
209
- return (0, _neverthrow.err)(new _errors.AutomationError(error));
210
- } finally {
211
- await saveTraceIfNeeded({
212
- errorMessage: "failed to save trace"
213
- });
214
- await context?.close();
215
- if (downloadsPath !== undefined) {
216
- await fs.remove(downloadsPath);
133
+ (0, _cleanEnvironmentVariables.cleanEnvironmentVariables)();
134
+ const automationFunctionParameters = [...(params !== undefined ? [params] : []), page, context];
135
+ try {
136
+ return (0, _neverthrow.ok)({
137
+ result: await automationFunction(...automationFunctionParameters),
138
+ extendedPayloads: (0, _asyncLocalStorage.getExecutionContext)()?.extendedPayloads,
139
+ session: retrieveSession ? await (0, _contextStorageStateHelpers.getStorageState)(context) : undefined
140
+ });
141
+ } catch (error) {
142
+ return (0, _neverthrow.err)(new _errors.AutomationError(error));
143
+ } finally {
144
+ await saveTraceIfNeeded({
145
+ errorMessage: "failed to save trace"
146
+ });
147
+ }
148
+ };
149
+ if (runOptions.environment === "standalone") {
150
+ const downloadsPath = (0, _downloadDirectory.getDownloadDirectoryPath)();
151
+ try {
152
+ return await (0, _playwrightContext.withPlaywrightContext)({
153
+ headless: runOptions.headless,
154
+ proxy: runOptions.proxy,
155
+ downloadsPath,
156
+ ...playwrightContextParameters
157
+ }, runAutomationWithContext);
158
+ } finally {
159
+ await fs.remove(downloadsPath);
160
+ }
161
+ } else {
162
+ return await (0, _playwrightContext.withPlaywrightContext)({
163
+ cdpAddress: runOptions.cdpAddress,
164
+ ...playwrightContextParameters
165
+ }, runAutomationWithContext);
217
166
  }
218
167
  }
219
- }
220
- async function runApi(params) {
221
- const generator = runApiGenerator(params);
222
- const {
223
- value,
224
- done
225
- } = await generator.next();
226
- if (done) {
227
- return value;
168
+ const result = await Promise.race([await runAutomation(), abortPromise]);
169
+ if (result === abortSymbol) {
170
+ return (0, _neverthrow.err)(new _errors.AbortedError());
228
171
  }
229
- return (0, _neverthrow.err)(new _errors.InvalidApiError("Expected API to be async function, got async generator"));
172
+ return result;
230
173
  }
231
174
  async function checkAuthSessionWithRetries(page, context, checkFn, retries = 3) {
232
175
  if (retries === 0) {
@@ -240,34 +183,4 @@ async function checkAuthSessionWithRetries(page, context, checkFn, retries = 3)
240
183
  tryNumber++;
241
184
  }
242
185
  return (0, _neverthrow.ok)(false);
243
- }
244
- async function importUsingImportFunction(path, importFunction) {
245
- try {
246
- const importedResult = await importFunction(path);
247
- if (importedResult.isErr()) {
248
- if (importedResult.error.type === "not_found") {
249
- return (0, _neverthrow.err)(new _errors.ApiNotFoundError(path));
250
- }
251
- return (0, _neverthrow.err)(new _errors.AutomationError(importedResult.error.error));
252
- }
253
- const imported = importedResult.value;
254
- if (!imported || !imported.default || !imported.default.constructor) {
255
- return (0, _neverthrow.err)(new _errors.InvalidApiError("API file path does not have a default export"));
256
- }
257
- if (imported.default.constructor.name === "AsyncGeneratorFunction") {
258
- return (0, _neverthrow.ok)({
259
- type: "async-generator",
260
- generator: imported.default
261
- });
262
- }
263
- if (imported.default.constructor.name === "AsyncFunction") {
264
- return (0, _neverthrow.ok)({
265
- type: "async",
266
- func: imported.default
267
- });
268
- }
269
- return (0, _neverthrow.err)(new _errors.InvalidApiError("API file path does not have a default async function/generator export"));
270
- } catch (error) {
271
- return (0, _neverthrow.err)(new _errors.AutomationError(error));
272
- }
273
186
  }