@intuned/runtime-dev 1.2.1-hooks.12 → 1.2.1-hooks.13

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.
@@ -29,7 +29,8 @@ type WithPlaywrightContextParameters = {
29
29
  apiName: string;
30
30
  apiParameters: any;
31
31
  };
32
- export type WithPlaywrightContextWrappedFunction<R> = (context: playwright.BrowserContext, page: playwright.Page) => Promise<Ok<R, any> | Err<any, RunAutomationError>>;
32
+ type WithPlaywrightContextWrappedFunctionReturn<R> = Ok<R, any> | Err<any, RunAutomationError>;
33
+ export type WithPlaywrightContextWrappedFunction<R> = (context: playwright.BrowserContext, page: playwright.Page) => Promise<WithPlaywrightContextWrappedFunctionReturn<R>>;
33
34
  export declare function withPlaywrightContext<R>(options: {
34
35
  proxy?: Proxy;
35
36
  headless: boolean;
@@ -17,7 +17,8 @@ var _fileUtils = require("../commands/common/utils/fileUtils");
17
17
  var _waitOn = _interopRequireDefault(require("wait-on"));
18
18
  var _errors = require("./runApi/errors");
19
19
  var _neverthrow = require("neverthrow");
20
- var _initializeContextHook = require("./initializeContextHook");
20
+ var _setupContextHook = require("./setupContextHook");
21
+ var _portfinder = require("portfinder");
21
22
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
22
23
  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
24
  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; }
@@ -70,10 +71,6 @@ async function launchChromium({
70
71
  downloadsPath,
71
72
  args: extraArgs
72
73
  });
73
- if (cdpPort) {
74
- const createdCdpAddress = getCdpAddress(cdpPort);
75
- await waitOnCdpAddress(createdCdpAddress);
76
- }
77
74
  context.once("close", async () => {
78
75
  try {
79
76
  await (0, _fsExtra.rm)(userDataDir, {
@@ -86,6 +83,10 @@ async function launchChromium({
86
83
  console.error("Failed to remove user data dir", error);
87
84
  }
88
85
  });
86
+ if (cdpPort) {
87
+ const createdCdpAddress = getCdpAddress(cdpPort);
88
+ await waitOnCdpAddress(createdCdpAddress);
89
+ }
89
90
  const page = context.pages().at(0) ?? (await context.newPage());
90
91
  return {
91
92
  page,
@@ -105,14 +106,14 @@ async function withPlaywrightContext({
105
106
  let context;
106
107
  let page;
107
108
  try {
108
- const initializeContextHookResult = await (0, _initializeContextHook.loadInitializeContextHook)({
109
+ const setupContextHookResult = await (0, _setupContextHook.loadSetupContextHook)({
109
110
  importFunction
110
111
  });
111
- if (initializeContextHookResult.isErr()) {
112
- return initializeContextHookResult;
112
+ if (setupContextHookResult.isErr()) {
113
+ return setupContextHookResult;
113
114
  }
114
- const initializeContextHook = initializeContextHookResult.value;
115
- if (initializeContextHook === null) {
115
+ const setupContextHook = setupContextHookResult.value;
116
+ if (setupContextHook === null) {
116
117
  if (cdpAddress !== undefined) {
117
118
  ({
118
119
  page,
@@ -142,7 +143,9 @@ async function withPlaywrightContext({
142
143
  cdpAddress
143
144
  }));
144
145
  } else {
145
- const port = 9222;
146
+ const port = await (0, _portfinder.getPort)({
147
+ port: 9222
148
+ });
146
149
  hookCdpUrl = getCdpAddress(port);
147
150
  ({
148
151
  context,
@@ -156,7 +159,7 @@ async function withPlaywrightContext({
156
159
  }
157
160
  let hookResult;
158
161
  try {
159
- hookResult = await initializeContextHook({
162
+ hookResult = await setupContextHook({
160
163
  apiName,
161
164
  apiParameters,
162
165
  cdpUrl: hookCdpUrl
@@ -169,13 +172,30 @@ async function withPlaywrightContext({
169
172
  }
170
173
  const {
171
174
  page: newPage,
172
- context: newContext
175
+ context: newContext,
176
+ cleanup
173
177
  } = hookResult;
178
+ let result;
174
179
  try {
175
- return await fn(newContext, newPage ?? page);
176
- } finally {
177
- await newContext.close();
180
+ result = {
181
+ return: await fn(newContext, newPage ?? page)
182
+ };
183
+ } catch (e) {
184
+ result = {
185
+ throw: e
186
+ };
187
+ }
188
+ try {
189
+ await cleanup?.();
190
+ } catch (e) {
191
+ result = {
192
+ return: (0, _neverthrow.err)(new _errors.AutomationError(e))
193
+ };
194
+ }
195
+ if ("throw" in result) {
196
+ throw result.throw;
178
197
  }
198
+ return result.return;
179
199
  } finally {
180
200
  await context?.close();
181
201
  }
@@ -7,10 +7,11 @@ export type InitializeContextHookOptions = {
7
7
  apiParameters: any;
8
8
  cdpUrl: string;
9
9
  };
10
- export type InitializeContextHook = (options: InitializeContextHookOptions) => Promise<{
10
+ export type SetupContextHook = (options: InitializeContextHookOptions) => Promise<{
11
11
  page: playwright.Page | null;
12
12
  context: playwright.BrowserContext;
13
+ cleanup: () => Promise<void>;
13
14
  } | null>;
14
- export declare function loadInitializeContextHook({ importFunction, }: {
15
+ export declare function loadSetupContextHook({ importFunction, }: {
15
16
  importFunction: ImportFunction;
16
- }): Promise<Ok<InitializeContextHook | null, any> | Err<any, RunAutomationError>>;
17
+ }): Promise<Ok<SetupContextHook | null, any> | Err<any, RunAutomationError>>;
@@ -3,13 +3,14 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.loadInitializeContextHook = loadInitializeContextHook;
6
+ exports.loadSetupContextHook = loadSetupContextHook;
7
7
  var _importUsingImportFunction = require("./runApi/importUsingImportFunction");
8
- async function loadInitializeContextHook({
8
+ const setupContextHookPath = "hooks/setupContext";
9
+ async function loadSetupContextHook({
9
10
  importFunction
10
11
  }) {
11
12
  return await (0, _importUsingImportFunction.importUsingImportFunction)({
12
- path: "hooks/initializeContext",
13
+ path: setupContextHookPath,
13
14
  importFunction
14
15
  });
15
16
  }
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { extendPayload, extendTimeout, runInfo, RunError, getAuthSessionParameters, store, } from "./runtime";
1
+ export { extendPayload, extendTimeout, runInfo, RunError, getAuthSessionParameters, attemptStore, } from "./runtime";
2
2
  export { runWithContext, getExecutionContext, } from "./common/asyncLocalStorage";
3
3
  export { getDownloadDirectoryPath } from "./runtime/downloadDirectory";
package/dist/index.js CHANGED
@@ -9,6 +9,12 @@ Object.defineProperty(exports, "RunError", {
9
9
  return _runtime.RunError;
10
10
  }
11
11
  });
12
+ Object.defineProperty(exports, "attemptStore", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _runtime.attemptStore;
16
+ }
17
+ });
12
18
  Object.defineProperty(exports, "extendPayload", {
13
19
  enumerable: true,
14
20
  get: function () {
@@ -51,12 +57,6 @@ Object.defineProperty(exports, "runWithContext", {
51
57
  return _asyncLocalStorage.runWithContext;
52
58
  }
53
59
  });
54
- Object.defineProperty(exports, "store", {
55
- enumerable: true,
56
- get: function () {
57
- return _runtime.store;
58
- }
59
- });
60
60
  var _runtime = require("./runtime");
61
61
  var _asyncLocalStorage = require("./common/asyncLocalStorage");
62
62
  var _downloadDirectory = require("./runtime/downloadDirectory");
@@ -0,0 +1,2 @@
1
+ import { Store } from "./export";
2
+ export declare const attemptStore: Store;
@@ -3,9 +3,9 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.store = void 0;
6
+ exports.attemptStore = void 0;
7
7
  var _asyncLocalStorage = require("../common/asyncLocalStorage");
8
- const store = exports.store = Object.freeze({
8
+ const attemptStore = exports.attemptStore = Object.freeze({
9
9
  get: key => {
10
10
  const context = (0, _asyncLocalStorage.getExecutionContext)();
11
11
  return context?.store ? context.store[key] : undefined;
@@ -223,4 +223,4 @@ export interface Store {
223
223
  * const missingData = store.get('nonexistent'); // undefined
224
224
  * ```
225
225
  */
226
- export declare const store: Store;
226
+ export declare const attemptStore: Store;
@@ -1,6 +1,6 @@
1
1
  export { extendPayload } from "./extendPayload";
2
2
  export { extendTimeout } from "./extendTimeout";
3
- export { store } from "./store";
3
+ export { attemptStore } from "./attemptStore";
4
4
  export { getAuthSessionParameters } from "./getAuthSessionParameters";
5
5
  export { runInfo } from "./runInfo";
6
6
  export { RunError } from "./RunError";
@@ -9,6 +9,12 @@ Object.defineProperty(exports, "RunError", {
9
9
  return _RunError.RunError;
10
10
  }
11
11
  });
12
+ Object.defineProperty(exports, "attemptStore", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _attemptStore.attemptStore;
16
+ }
17
+ });
12
18
  Object.defineProperty(exports, "extendPayload", {
13
19
  enumerable: true,
14
20
  get: function () {
@@ -39,15 +45,9 @@ Object.defineProperty(exports, "runInfo", {
39
45
  return _runInfo.runInfo;
40
46
  }
41
47
  });
42
- Object.defineProperty(exports, "store", {
43
- enumerable: true,
44
- get: function () {
45
- return _store.store;
46
- }
47
- });
48
48
  var _extendPayload = require("./extendPayload");
49
49
  var _extendTimeout = require("./extendTimeout");
50
- var _store = require("./store");
50
+ var _attemptStore = require("./attemptStore");
51
51
  var _getAuthSessionParameters = require("./getAuthSessionParameters");
52
52
  var _runInfo = require("./runInfo");
53
53
  var _RunError = require("./RunError");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intuned/runtime-dev",
3
- "version": "1.2.1-hooks.12",
3
+ "version": "1.2.1-hooks.13",
4
4
  "description": "Intuned runtime",
5
5
  "exports": {
6
6
  ".": "./dist/index.js",
@@ -79,6 +79,7 @@
79
79
  "nanoid": "3",
80
80
  "neverthrow": "6.1.0",
81
81
  "playwright-extra": "4.3.6",
82
+ "portfinder": "^1.0.37",
82
83
  "prettier": "2.8.0",
83
84
  "promptly": "3.2.0",
84
85
  "rollup": "3.26.2",
@@ -1,2 +0,0 @@
1
- import { Store } from "./export";
2
- export declare const store: Store;