@intuned/runtime-dev 1.2.1-hooks.9 → 1.2.1-results.0

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 (37) hide show
  1. package/CHANGELOG.md +2 -1
  2. package/dist/commands/api/run.js +6 -2
  3. package/dist/commands/auth-sessions/load.js +3 -3
  4. package/dist/commands/auth-sessions/run-check.js +2 -1
  5. package/dist/commands/auth-sessions/run-create.js +35 -12
  6. package/dist/commands/interface/run.d.ts +1 -1
  7. package/dist/commands/interface/run.js +40 -14
  8. package/dist/commands/intuned-cli/controller/api.js +2 -1
  9. package/dist/commands/intuned-cli/controller/authSession.d.ts +6 -6
  10. package/dist/commands/intuned-cli/controller/authSession.js +4 -3
  11. package/dist/commands/intuned-cli/helpers/auth.d.ts +1 -1
  12. package/dist/commands/intuned-cli/helpers/errors.d.ts +2 -2
  13. package/dist/commands/intuned-cli/helpers/errors.js +4 -8
  14. package/dist/common/asyncLocalStorage/index.d.ts +0 -1
  15. package/dist/common/getPlaywrightConstructs.d.ts +30 -0
  16. package/dist/common/{playwrightContext.js → getPlaywrightConstructs.js} +80 -132
  17. package/dist/common/runApi/errors.d.ts +9 -2
  18. package/dist/common/runApi/errors.js +17 -3
  19. package/dist/common/runApi/index.d.ts +7 -2
  20. package/dist/common/runApi/index.js +185 -87
  21. package/dist/common/runApi/types.d.ts +25 -21
  22. package/dist/common/runApi/types.js +2 -1
  23. package/dist/index.d.ts +2 -1
  24. package/dist/index.js +20 -7
  25. package/dist/runtime/export.d.ts +43 -49
  26. package/dist/runtime/index.d.ts +2 -1
  27. package/dist/runtime/index.js +11 -5
  28. package/dist/runtime/requestMoreInfo.d.ts +18 -0
  29. package/dist/runtime/requestMoreInfo.js +25 -0
  30. package/package.json +1 -3
  31. package/dist/common/initializeContextHook.d.ts +0 -16
  32. package/dist/common/initializeContextHook.js +0 -15
  33. package/dist/common/playwrightContext.d.ts +0 -49
  34. package/dist/common/runApi/importUsingImportFunction.d.ts +0 -9
  35. package/dist/common/runApi/importUsingImportFunction.js +0 -46
  36. package/dist/runtime/store.d.ts +0 -2
  37. package/dist/runtime/store.js +0 -23
@@ -3,11 +3,10 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.browserScriptsFile = void 0;
6
+ exports.getPlaywrightConstructsForMode = getPlaywrightConstructsForMode;
7
+ exports.getProductionPlaywrightConstructs = getProductionPlaywrightConstructs;
7
8
  exports.getRemotePlaywrightContext = getRemotePlaywrightContext;
8
- exports.launchChromium = launchChromium;
9
9
  exports.loadSessionToContext = loadSessionToContext;
10
- exports.withPlaywrightContext = withPlaywrightContext;
11
10
  var playwright = _interopRequireWildcard(require("playwright"));
12
11
  var _fsExtra = _interopRequireWildcard(require("fs-extra"));
13
12
  var fs = _fsExtra;
@@ -15,10 +14,7 @@ var _contextStorageStateHelpers = require("./contextStorageStateHelpers");
15
14
  var _path = _interopRequireWildcard(require("path"));
16
15
  var _fileUtils = require("../commands/common/utils/fileUtils");
17
16
  var _waitOn = _interopRequireDefault(require("wait-on"));
18
- var _errors = require("./runApi/errors");
19
- var _neverthrow = require("neverthrow");
20
- var _initializeContextHook = require("./initializeContextHook");
21
- var _getPortPlease = require("get-port-please");
17
+ var _runtime = require("../runtime");
22
18
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
23
19
  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); }
24
20
  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; }
@@ -37,44 +33,23 @@ async function createUserDirWithPreferences() {
37
33
  await (0, _fsExtra.writeFile)((0, _path.join)(defaultDir, "Preferences"), JSON.stringify(preferences));
38
34
  return userDir;
39
35
  }
40
- async function launchChromium({
36
+ async function getProductionPlaywrightConstructs({
41
37
  proxy,
42
38
  headless = true,
43
- downloadsPath,
44
- cdpAddress,
45
- cdpPort
39
+ storageState,
40
+ downloadsPath
46
41
  }) {
47
- if (cdpAddress) {
48
- const browser = await playwright.chromium.connectOverCDP(cdpAddress);
49
- if (browser.contexts().length === 0) {
50
- throw new Error("No browser contexts found in the connected browser");
51
- }
52
- const context = browser.contexts()[0];
53
- let page = context.pages().at(0);
54
- if (!page) {
55
- page = await context.newPage();
56
- }
57
- return {
58
- page,
59
- context
60
- };
42
+ const extraArgs = ["--no-first-run", "--disable-sync", "--disable-translate", "--disable-features=TranslateUI", "--disable-features=NetworkService", "--lang=en"];
43
+ if (headless) {
44
+ extraArgs.push("--headless=new");
61
45
  }
62
- const extraArgs = [];
63
46
  const userDataDir = await createUserDirWithPreferences();
64
- if (cdpPort) {
65
- extraArgs.push(`--remote-debugging-port=${cdpPort}`);
66
- }
67
47
  const context = await playwright.chromium.launchPersistentContext(userDataDir, {
68
48
  headless,
69
49
  viewport: null,
70
50
  proxy,
71
- downloadsPath,
72
- args: extraArgs
51
+ downloadsPath
73
52
  });
74
- if (cdpPort) {
75
- const createdCdpAddress = getCdpAddress(cdpPort);
76
- await waitOnCdpAddress(createdCdpAddress);
77
- }
78
53
  context.once("close", async () => {
79
54
  try {
80
55
  await (0, _fsExtra.rm)(userDataDir, {
@@ -87,101 +62,80 @@ async function launchChromium({
87
62
  console.error("Failed to remove user data dir", error);
88
63
  }
89
64
  });
90
- const page = context.pages().at(0) ?? (await context.newPage());
65
+ if (storageState) {
66
+ await loadSessionToContext({
67
+ context,
68
+ session: storageState
69
+ });
70
+ }
71
+ const assetsFile = _path.default.join(__dirname, "./assets/browser_scripts.js");
72
+ await context.addInitScript({
73
+ path: assetsFile
74
+ });
75
+ let page = context.pages().at(0);
76
+ if (page) {
77
+ const scriptString = await (0, _fsExtra.readFile)(assetsFile, "utf8");
78
+ await page.evaluate(scriptString);
79
+ } else {
80
+ page = await context.newPage();
81
+ }
91
82
  return {
92
83
  page,
93
84
  context
94
85
  };
95
86
  }
96
- const browserScriptsFile = exports.browserScriptsFile = _path.default.join(__dirname, "./assets/browser_scripts.js");
97
- async function withPlaywrightContext({
98
- cdpAddress,
99
- proxy,
100
- headless = true,
101
- downloadsPath,
102
- importFunction,
103
- apiName,
104
- apiParameters
105
- }, fn) {
106
- let context;
107
- let page;
108
- try {
109
- const initializeContextHookResult = await (0, _initializeContextHook.loadInitializeContextHook)({
110
- importFunction
111
- });
112
- if (initializeContextHookResult.isErr()) {
113
- return initializeContextHookResult;
87
+ async function getPlaywrightConstructsForMode(mode, cdpAddress, authSession) {
88
+ if (mode == "playwright-standalone") {
89
+ if (!cdpAddress) {
90
+ throw new Error("cdpAddress is required");
114
91
  }
115
- const initializeContextHook = initializeContextHookResult.value;
116
- if (initializeContextHook === null) {
117
- if (cdpAddress !== undefined) {
118
- ({
119
- page,
120
- context
121
- } = await launchChromium({
122
- cdpAddress
123
- }));
124
- } else {
125
- ({
126
- page,
127
- context
128
- } = await launchChromium({
129
- proxy,
130
- headless,
131
- downloadsPath
132
- }));
133
- }
134
- return await fn(context, page);
92
+ const {
93
+ context
94
+ } = await getRemotePlaywrightContext(cdpAddress);
95
+ if (!context) {
96
+ throw new Error("no context found");
135
97
  }
136
- let hookCdpUrl = null;
137
- if (cdpAddress) {
138
- hookCdpUrl = cdpAddress;
139
- ({
140
- context,
141
- page
142
- } = await launchChromium({
143
- cdpAddress
144
- }));
98
+ const assetsFile = _path.default.join(__dirname, "./assets/browser_scripts.js");
99
+ await context.addInitScript({
100
+ path: assetsFile
101
+ });
102
+ const pages = await context.pages();
103
+ let page = null;
104
+ if (pages.length > 0) {
105
+ page = pages[0];
106
+ const scriptString = await fs.readFile(assetsFile, "utf8");
107
+ await page.evaluate(scriptString);
145
108
  } else {
146
- const port = await (0, _getPortPlease.getPort)({
147
- port: 9222
148
- });
149
- hookCdpUrl = getCdpAddress(port);
150
- ({
151
- context,
152
- page
153
- } = await launchChromium({
154
- proxy,
155
- headless,
156
- downloadsPath,
157
- cdpPort: port
158
- }));
109
+ page = await context.newPage();
159
110
  }
160
- let hookResult;
161
- try {
162
- hookResult = await initializeContextHook({
163
- apiName,
164
- apiParameters,
165
- cdpUrl: hookCdpUrl
111
+ if (authSession) {
112
+ await loadSessionToContext({
113
+ context,
114
+ session: authSession
166
115
  });
167
- } catch (error) {
168
- return (0, _neverthrow.err)(new _errors.AutomationError(error));
169
- }
170
- if (!hookResult) {
171
- return await fn(context, page);
172
116
  }
173
- const {
174
- page: newPage,
175
- context: newContext
176
- } = hookResult;
177
- try {
178
- return await fn(newContext, newPage ?? page);
179
- } finally {
180
- await newContext.close();
117
+ return {
118
+ page,
119
+ context
120
+ };
121
+ }
122
+ const downloadsPath = (0, _runtime.getDownloadDirectoryPath)();
123
+ if (mode === "playwright" || mode === "playwright-headless") {
124
+ const productionConstructs = await getProductionPlaywrightConstructs({
125
+ headless: mode === "playwright-headless",
126
+ downloadsPath
127
+ });
128
+ if (authSession) {
129
+ await loadSessionToContext({
130
+ context: productionConstructs.context,
131
+ session: authSession
132
+ });
181
133
  }
182
- } finally {
183
- await context?.close();
134
+ return {
135
+ ...productionConstructs
136
+ };
184
137
  }
138
+ throw "invalid mode";
185
139
  }
186
140
  async function loadSessionToContext({
187
141
  context,
@@ -200,28 +154,22 @@ async function loadSessionToContext({
200
154
  }
201
155
  await (0, _contextStorageStateHelpers.setStorageState)(context, sessionToLoad);
202
156
  }
203
- function getCdpAddress(port) {
204
- return `http://localhost:${port}`;
205
- }
206
- async function waitOnCdpAddress(cdpAddress) {
207
- const cdpAddressWithoutProtocol = cdpAddress.replace("http://", "").replace("https://", "").replace("localhost", "127.0.0.1");
208
- await (0, _waitOn.default)({
209
- resources: [`http-get://${cdpAddressWithoutProtocol}/json/version`],
210
- delay: 100,
211
- interval: 100,
212
- timeout: 5000,
213
- tcpTimeout: 1000,
214
- window: 1000
215
- });
216
- }
217
157
  async function getRemotePlaywrightContext(cdpAddress) {
218
158
  const playwright = await Promise.resolve().then(() => _interopRequireWildcard(require("playwright")));
219
159
  let browser = null;
220
160
  if (!cdpAddress) {
221
161
  throw new Error("cdpAddress is required");
222
162
  }
163
+ const cdpAddressWithoutProtocol = cdpAddress.replace("http://", "").replace("https://", "").replace("localhost", "127.0.0.1");
223
164
  try {
224
- await waitOnCdpAddress(cdpAddress);
165
+ await (0, _waitOn.default)({
166
+ resources: [`http-get://${cdpAddressWithoutProtocol}/json/version`],
167
+ delay: 100,
168
+ interval: 100,
169
+ timeout: 5000,
170
+ tcpTimeout: 1000,
171
+ window: 1000
172
+ });
225
173
  } catch (error) {
226
174
  console.error("Failed to connect to the browser");
227
175
  process.exit(128 + 9);
@@ -9,7 +9,8 @@ export declare const authCheckFailedErrorCode = "AuthCheckFailedError";
9
9
  export declare const maxLevelsExceededErrorCode = "MaxLevelsExceededError";
10
10
  export declare const automationError = "AutomationError";
11
11
  export declare const internalInvalidInputErrorCode = "InternalInvalidInputError";
12
- export declare const runAutomationErrorCodes: readonly ["APINotFoundError", "InvalidAPIError", "InvalidCheckError", "AbortedError", "AuthRequiredError", "AuthCheckNotFoundError", "AuthCheckFailedError", "MaxLevelsExceededError", "AutomationError", "InternalInvalidInputError"];
12
+ export declare const resultTooBigErrorCode = "ResultTooBigError";
13
+ export declare const runAutomationErrorCodes: readonly ["APINotFoundError", "InvalidAPIError", "InvalidCheckError", "AbortedError", "AuthRequiredError", "AuthCheckNotFoundError", "AuthCheckFailedError", "MaxLevelsExceededError", "AutomationError", "InternalInvalidInputError", "ResultTooBigError"];
13
14
  export type RunAutomationErrorCode = typeof runAutomationErrorCodes[number];
14
15
  export declare abstract class RunAutomationError<T = any> {
15
16
  code: RunAutomationErrorCode;
@@ -19,7 +20,7 @@ export declare abstract class RunAutomationError<T = any> {
19
20
  cause?: RunAutomationError;
20
21
  details?: T;
21
22
  get json(): {
22
- code: "APINotFoundError" | "InvalidAPIError" | "InvalidCheckError" | "AbortedError" | "AuthRequiredError" | "AuthCheckNotFoundError" | "AuthCheckFailedError" | "MaxLevelsExceededError" | "AutomationError" | "InternalInvalidInputError";
23
+ code: "APINotFoundError" | "InvalidAPIError" | "InvalidCheckError" | "AbortedError" | "AuthRequiredError" | "AuthCheckNotFoundError" | "AuthCheckFailedError" | "MaxLevelsExceededError" | "AutomationError" | "InternalInvalidInputError" | "ResultTooBigError";
23
24
  details: T | undefined;
24
25
  cause: any;
25
26
  };
@@ -63,3 +64,9 @@ export declare class AutomationError extends RunAutomationError<{
63
64
  export declare class InternalInvalidInputError extends RunAutomationError {
64
65
  constructor(message: string, details?: any);
65
66
  }
67
+ export declare class ResultTooBigError extends RunAutomationError<{
68
+ sizeInBytes: number;
69
+ maxSizeInBytes: number;
70
+ }> {
71
+ constructor(sizeInBytes: number, maxSizeInBytes: number);
72
+ }
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.runAutomationErrorCodes = exports.maxLevelsExceededErrorCode = exports.invalidCheckErrorCode = exports.invalidApiErrorCode = exports.internalInvalidInputErrorCode = exports.automationError = exports.authRequiredErrorCode = exports.authCheckNotFoundErrorCode = exports.authCheckFailedErrorCode = exports.apiNotFoundErrorCode = exports.abortedErrorCode = exports.RunAutomationError = exports.MaxLevelsExceededError = exports.InvalidCheckError = exports.InvalidApiError = exports.InternalInvalidInputError = exports.AutomationError = exports.AuthRequiredError = exports.AuthCheckNotFoundError = exports.AuthCheckFailedError = exports.ApiNotFoundError = exports.AbortedError = void 0;
6
+ exports.runAutomationErrorCodes = exports.resultTooBigErrorCode = exports.maxLevelsExceededErrorCode = exports.invalidCheckErrorCode = exports.invalidApiErrorCode = exports.internalInvalidInputErrorCode = exports.automationError = exports.authRequiredErrorCode = exports.authCheckNotFoundErrorCode = exports.authCheckFailedErrorCode = exports.apiNotFoundErrorCode = exports.abortedErrorCode = exports.RunAutomationError = exports.ResultTooBigError = exports.MaxLevelsExceededError = exports.InvalidCheckError = exports.InvalidApiError = exports.InternalInvalidInputError = exports.AutomationError = exports.AuthRequiredError = exports.AuthCheckNotFoundError = exports.AuthCheckFailedError = exports.ApiNotFoundError = exports.AbortedError = void 0;
7
7
  var _runtime = require("../../runtime");
8
8
  var playwright = _interopRequireWildcard(require("playwright-core"));
9
9
  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); }
@@ -18,7 +18,8 @@ const authCheckFailedErrorCode = exports.authCheckFailedErrorCode = "AuthCheckFa
18
18
  const maxLevelsExceededErrorCode = exports.maxLevelsExceededErrorCode = "MaxLevelsExceededError";
19
19
  const automationError = exports.automationError = "AutomationError";
20
20
  const internalInvalidInputErrorCode = exports.internalInvalidInputErrorCode = "InternalInvalidInputError";
21
- const runAutomationErrorCodes = exports.runAutomationErrorCodes = [apiNotFoundErrorCode, invalidApiErrorCode, invalidCheckErrorCode, abortedErrorCode, authRequiredErrorCode, authCheckNotFoundErrorCode, authCheckFailedErrorCode, maxLevelsExceededErrorCode, automationError, internalInvalidInputErrorCode];
21
+ const resultTooBigErrorCode = exports.resultTooBigErrorCode = "ResultTooBigError";
22
+ const runAutomationErrorCodes = exports.runAutomationErrorCodes = [apiNotFoundErrorCode, invalidApiErrorCode, invalidCheckErrorCode, abortedErrorCode, authRequiredErrorCode, authCheckNotFoundErrorCode, authCheckFailedErrorCode, maxLevelsExceededErrorCode, automationError, internalInvalidInputErrorCode, resultTooBigErrorCode];
22
23
  class RunAutomationError {
23
24
  wrapped = false;
24
25
  get json() {
@@ -152,4 +153,17 @@ class InternalInvalidInputError extends RunAutomationError {
152
153
  this.details = details;
153
154
  }
154
155
  }
155
- exports.InternalInvalidInputError = InternalInvalidInputError;
156
+ exports.InternalInvalidInputError = InternalInvalidInputError;
157
+ class ResultTooBigError extends RunAutomationError {
158
+ constructor(sizeInBytes, maxSizeInBytes) {
159
+ super();
160
+ this.code = resultTooBigErrorCode;
161
+ this.statusCode = 413;
162
+ this.message = `Automation result is too big. Size: ${Math.round(sizeInBytes / 1024 / 1024 * 100) / 100}MB, Max allowed: ${Math.round(maxSizeInBytes / 1024 / 1024 * 100) / 100}MB`;
163
+ this.details = {
164
+ sizeInBytes,
165
+ maxSizeInBytes
166
+ };
167
+ }
168
+ }
169
+ exports.ResultTooBigError = ResultTooBigError;
@@ -4,8 +4,13 @@ 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 runApi<ResultType = any>(input: ExtendedRunApiParameters & {
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 & {
8
12
  retrieveSession: true;
9
13
  }): Promise<RunApiResult<ResultType, RunApiResultWithSessionOk>>;
10
- export declare function runApi<ResultType = any>(input: ExtendedRunApiParameters): Promise<RunApiResult<ResultType>>;
14
+ export declare function runApi<ResultType = any>(params: ExtendedRunApiParameters): Promise<RunApiResult<ResultType>>;
11
15
  export declare function checkAuthSessionWithRetries(page: Page, context: BrowserContext, checkFn: (..._: any) => Promise<boolean>, retries?: number): Promise<Result<boolean, RunAutomationError>>;
16
+ export type ImportFunction = ExtendedRunApiParameters["importFunction"];