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

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.
@@ -8,8 +8,8 @@ interface TimeoutInfo {
8
8
  }
9
9
  export interface InternalRunInfo extends RunInfo {
10
10
  extendedPayloads: Payload[];
11
- cdpAddress?: string;
12
11
  timeoutInfo?: TimeoutInfo;
12
+ hookVars?: Record<string, any>;
13
13
  getAuthSessionParameters?: () => Promise<any>;
14
14
  }
15
15
  export declare function getExecutionContext(): InternalRunInfo | undefined;
@@ -10,13 +10,17 @@ interface GetPlaywrightConstructsOptions {
10
10
  headless?: boolean;
11
11
  storageState?: RunApiSession;
12
12
  downloadsPath: string;
13
- importFunction?: ImportFunction;
13
+ hookOptions?: {
14
+ importFunction: ImportFunction;
15
+ apiName: string;
16
+ parameters: any;
17
+ };
14
18
  }
15
- export declare function getProductionPlaywrightConstructs({ proxy, headless, storageState, downloadsPath, importFunction, }: GetPlaywrightConstructsOptions): Promise<{
19
+ export declare function getProductionPlaywrightConstructs({ proxy, headless, storageState, downloadsPath, hookOptions, }: GetPlaywrightConstructsOptions): Promise<{
16
20
  page: playwright.Page;
17
21
  context: playwright.BrowserContext;
18
22
  }>;
19
- export declare function getPlaywrightConstructsForMode(mode: "vanilla" | "playwright" | "playwright-standalone" | "playwright-headless", cdpAddress: string | undefined, authSession?: RunApiSession, importFunction?: ImportFunction): Promise<{
23
+ export declare function getPlaywrightConstructsForMode(mode: "vanilla" | "playwright" | "playwright-standalone" | "playwright-headless", cdpAddress: string | undefined, authSession?: RunApiSession, hookOptions?: GetPlaywrightConstructsOptions["hookOptions"]): Promise<{
20
24
  page: playwright.Page;
21
25
  context: playwright.BrowserContext;
22
26
  }>;
@@ -15,6 +15,9 @@ var _path = _interopRequireWildcard(require("path"));
15
15
  var _fileUtils = require("../commands/common/utils/fileUtils");
16
16
  var _waitOn = _interopRequireDefault(require("wait-on"));
17
17
  var _runtime = require("../runtime");
18
+ var _asyncLocalStorage = require("./asyncLocalStorage");
19
+ var _getPort = _interopRequireDefault(require("get-port"));
20
+ var _promises = require("timers/promises");
18
21
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
19
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); }
20
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; }
@@ -34,7 +37,11 @@ async function createUserDirWithPreferences() {
34
37
  return userDir;
35
38
  }
36
39
  async function loadInitializeContextHook(importFunction) {
40
+ console.log("Loading initializeContext hook");
37
41
  const result = await importFunction("hooks/initializeContext");
42
+ console.log({
43
+ result
44
+ });
38
45
  if (result.isErr()) {
39
46
  if (result.error.type === "not_found") {
40
47
  return null;
@@ -49,48 +56,63 @@ async function getProductionPlaywrightConstructs({
49
56
  headless = true,
50
57
  storageState,
51
58
  downloadsPath,
52
- importFunction
59
+ hookOptions
53
60
  }) {
54
- let context;
55
- let page;
61
+ const extraArgs = [];
62
+ const port = await (0, _getPort.default)();
56
63
  let hook = null;
57
- if (importFunction) {
58
- hook = await loadInitializeContextHook(importFunction);
64
+ if (hookOptions) {
65
+ hook = await loadInitializeContextHook(hookOptions.importFunction);
59
66
  }
67
+ const userDataDir = await createUserDirWithPreferences();
60
68
  if (hook) {
61
- ({
62
- page,
63
- context
64
- } = await hook({
65
- type: "launch",
66
- proxy,
67
- headless
68
- }));
69
- } else {
70
- const extraArgs = ["--no-first-run", "--disable-sync", "--disable-translate", "--disable-features=TranslateUI", "--disable-features=NetworkService", "--lang=en"];
71
- if (headless) {
72
- extraArgs.push("--headless=new");
69
+ extraArgs.push(`--remote-debugging-port=${port}`);
70
+ }
71
+ const originalContext = await playwright.chromium.launchPersistentContext(userDataDir, {
72
+ headless,
73
+ viewport: null,
74
+ proxy,
75
+ downloadsPath,
76
+ args: extraArgs
77
+ });
78
+ const cdpAddress = `http://localhost:${port}`;
79
+ await (0, _promises.setTimeout)(3000);
80
+ originalContext.once("close", async () => {
81
+ try {
82
+ await (0, _fsExtra.rm)(userDataDir, {
83
+ recursive: true,
84
+ force: true,
85
+ retryDelay: 1000,
86
+ maxRetries: 5
87
+ });
88
+ } catch (error) {
89
+ console.error("Failed to remove user data dir", error);
73
90
  }
74
- const userDataDir = await createUserDirWithPreferences();
75
- context = await playwright.chromium.launchPersistentContext(userDataDir, {
76
- headless,
77
- viewport: null,
78
- proxy,
79
- downloadsPath
91
+ });
92
+ let page = originalContext.pages().at(0) ?? null;
93
+ let context = originalContext;
94
+ if (hookOptions && hook) {
95
+ const hookResult = await hook({
96
+ apiName: hookOptions.apiName,
97
+ apiParameters: hookOptions.parameters,
98
+ cdpAddress
80
99
  });
81
- context.once("close", async () => {
82
- try {
83
- await (0, _fsExtra.rm)(userDataDir, {
84
- recursive: true,
85
- force: true,
86
- retryDelay: 1000,
87
- maxRetries: 5
88
- });
89
- } catch (error) {
90
- console.error("Failed to remove user data dir", error);
100
+ if (hookResult !== null) {
101
+ ({
102
+ page,
103
+ context: context
104
+ } = hookResult);
105
+ const {
106
+ hookVars
107
+ } = hookResult;
108
+ const executionContext = (0, _asyncLocalStorage.getExecutionContext)();
109
+ if (hookVars && executionContext) {
110
+ executionContext.hookVars = hookVars;
91
111
  }
92
- });
93
- page = context.pages().at(0) ?? null;
112
+ context.once("close", async () => {
113
+ await originalContext.close();
114
+ });
115
+ }
94
116
  }
95
117
  if (storageState) {
96
118
  await loadSessionToContext({
@@ -113,26 +135,37 @@ async function getProductionPlaywrightConstructs({
113
135
  context
114
136
  };
115
137
  }
116
- async function getPlaywrightConstructsForMode(mode, cdpAddress, authSession, importFunction) {
117
- let hook = null;
118
- if (importFunction) {
119
- hook = await loadInitializeContextHook(importFunction);
138
+ async function getPlaywrightConstructsForMode(mode, cdpAddress, authSession, hookOptions) {
139
+ let context = null;
140
+ let page = null;
141
+ if (hookOptions && cdpAddress) {
142
+ const hook = await loadInitializeContextHook(hookOptions.importFunction);
143
+ if (hook) {
144
+ const hookResult = await hook({
145
+ cdpAddress,
146
+ apiName: hookOptions.apiName,
147
+ apiParameters: hookOptions.parameters
148
+ });
149
+ if (hookResult !== null) {
150
+ ({
151
+ page,
152
+ context
153
+ } = hookResult);
154
+ const {
155
+ hookVars
156
+ } = hookResult;
157
+ const executionContext = (0, _asyncLocalStorage.getExecutionContext)();
158
+ if (hookVars && executionContext) {
159
+ executionContext.hookVars = hookVars;
160
+ }
161
+ }
162
+ }
120
163
  }
121
164
  if (mode == "playwright-standalone") {
122
165
  if (!cdpAddress) {
123
166
  throw new Error("cdpAddress is required");
124
167
  }
125
- let context;
126
- let page = null;
127
- if (hook) {
128
- ({
129
- context,
130
- page
131
- } = await hook({
132
- type: "cdp",
133
- cdpAddress
134
- }));
135
- } else {
168
+ if (!context) {
136
169
  ({
137
170
  context
138
171
  } = await getRemotePlaywrightContext(cdpAddress));
@@ -129,7 +129,11 @@ async function* runApiGenerator({
129
129
  proxy,
130
130
  downloadsPath,
131
131
  storageState: auth?.session,
132
- importFunction
132
+ hookOptions: {
133
+ apiName: automationFunction.name,
134
+ parameters: automationFunction.params,
135
+ importFunction
136
+ }
133
137
  }));
134
138
  } else {
135
139
  const {
@@ -139,7 +143,11 @@ async function* runApiGenerator({
139
143
  ({
140
144
  page,
141
145
  context
142
- } = await (0, _getPlaywrightConstructs.getPlaywrightConstructsForMode)(mode, cdpAddress, auth?.session, importFunction));
146
+ } = await (0, _getPlaywrightConstructs.getPlaywrightConstructsForMode)(mode, cdpAddress, auth?.session, {
147
+ apiName: automationFunction.name,
148
+ parameters: automationFunction.params,
149
+ importFunction
150
+ }));
143
151
  }
144
152
  if (tracing.enabled) {
145
153
  await context.tracing.start({
@@ -687,13 +687,13 @@ export declare const runApiParametersSchema: z.ZodObject<{
687
687
  cdpAddress: z.ZodString;
688
688
  mode: z.ZodUnion<[z.ZodLiteral<"vanilla">, z.ZodLiteral<"playwright">, z.ZodLiteral<"playwright-standalone">, z.ZodLiteral<"playwright-headless">]>;
689
689
  }, "strip", z.ZodTypeAny, {
690
- cdpAddress: string;
691
690
  mode: "vanilla" | "playwright" | "playwright-standalone" | "playwright-headless";
692
691
  environment: "cdp";
693
- }, {
694
692
  cdpAddress: string;
693
+ }, {
695
694
  mode: "vanilla" | "playwright" | "playwright-standalone" | "playwright-headless";
696
695
  environment: "cdp";
696
+ cdpAddress: string;
697
697
  }>]>>;
698
698
  retrieveSession: z.ZodDefault<z.ZodBoolean>;
699
699
  }, "strip", z.ZodTypeAny, {
@@ -716,9 +716,9 @@ export declare const runApiParametersSchema: z.ZodObject<{
716
716
  password: string;
717
717
  } | undefined;
718
718
  } | {
719
- cdpAddress: string;
720
719
  mode: "vanilla" | "playwright" | "playwright-standalone" | "playwright-headless";
721
720
  environment: "cdp";
721
+ cdpAddress: string;
722
722
  };
723
723
  retrieveSession: boolean;
724
724
  auth?: {
@@ -811,9 +811,9 @@ export declare const runApiParametersSchema: z.ZodObject<{
811
811
  password: string;
812
812
  } | undefined;
813
813
  } | {
814
- cdpAddress: string;
815
814
  mode: "vanilla" | "playwright" | "playwright-standalone" | "playwright-headless";
816
815
  environment: "cdp";
816
+ cdpAddress: string;
817
817
  } | undefined;
818
818
  retrieveSession?: boolean | undefined;
819
819
  }>;
@@ -6,4 +6,3 @@ export { RunError } from "./RunError";
6
6
  export { requestMultipleChoice, requestOTP } from "./requestMoreInfo";
7
7
  export type { RequestMoreInfoDetails } from "./requestMoreInfo";
8
8
  export { getDownloadDirectoryPath } from "./downloadDirectory";
9
- export { getCdpAddress } from "./getCdpAddress";
@@ -27,12 +27,6 @@ Object.defineProperty(exports, "getAuthSessionParameters", {
27
27
  return _getAuthSessionParameters.getAuthSessionParameters;
28
28
  }
29
29
  });
30
- Object.defineProperty(exports, "getCdpAddress", {
31
- enumerable: true,
32
- get: function () {
33
- return _getCdpAddress.getCdpAddress;
34
- }
35
- });
36
30
  Object.defineProperty(exports, "getDownloadDirectoryPath", {
37
31
  enumerable: true,
38
32
  get: function () {
@@ -63,5 +57,4 @@ var _getAuthSessionParameters = require("./getAuthSessionParameters");
63
57
  var _runInfo = require("./runInfo");
64
58
  var _RunError = require("./RunError");
65
59
  var _requestMoreInfo = require("./requestMoreInfo");
66
- var _downloadDirectory = require("./downloadDirectory");
67
- var _getCdpAddress = require("./getCdpAddress");
60
+ var _downloadDirectory = require("./downloadDirectory");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intuned/runtime-dev",
3
- "version": "1.2.1-hooks.1",
3
+ "version": "1.2.1-hooks.3",
4
4
  "description": "Intuned runtime",
5
5
  "exports": {
6
6
  ".": "./dist/index.js",
@@ -1 +0,0 @@
1
- export declare function getCdpAddress(): string | undefined;
@@ -1,11 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getCdpAddress = getCdpAddress;
7
- var _asyncLocalStorage = require("../common/asyncLocalStorage");
8
- function getCdpAddress() {
9
- const context = (0, _asyncLocalStorage.getExecutionContext)();
10
- return context?.cdpAddress;
11
- }