@intuned/runtime-dev 1.2.1-hooks.4 → 1.2.1-hooks.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.
Files changed (37) hide show
  1. package/InterfaceTemplate/utils.ts +1 -1
  2. package/dist/commands/api/run.js +1 -2
  3. package/dist/commands/auth-sessions/load.js +3 -3
  4. package/dist/commands/auth-sessions/run-check.js +1 -2
  5. package/dist/commands/auth-sessions/run-create.js +12 -35
  6. package/dist/commands/interface/run.d.ts +1 -1
  7. package/dist/commands/interface/run.js +14 -40
  8. package/dist/commands/intuned-cli/controller/api.js +1 -2
  9. package/dist/commands/intuned-cli/controller/authSession.d.ts +6 -6
  10. package/dist/commands/intuned-cli/controller/authSession.js +3 -4
  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 +8 -4
  14. package/dist/common/asyncLocalStorage/index.d.ts +1 -1
  15. package/dist/common/initializeContextHook.d.ts +16 -0
  16. package/dist/common/initializeContextHook.js +15 -0
  17. package/dist/common/playwrightContext.d.ts +49 -0
  18. package/dist/common/playwrightContext.js +240 -0
  19. package/dist/common/runApi/importUsingImportFunction.d.ts +9 -0
  20. package/dist/common/runApi/importUsingImportFunction.js +46 -0
  21. package/dist/common/runApi/index.d.ts +2 -7
  22. package/dist/common/runApi/index.js +86 -181
  23. package/dist/common/runApi/types.d.ts +20 -25
  24. package/dist/common/runApi/types.js +1 -2
  25. package/dist/index.d.ts +1 -2
  26. package/dist/index.js +7 -20
  27. package/dist/runtime/export.d.ts +49 -43
  28. package/dist/runtime/index.d.ts +1 -2
  29. package/dist/runtime/index.js +5 -11
  30. package/dist/runtime/store.d.ts +2 -0
  31. package/dist/runtime/store.js +23 -0
  32. package/package.json +1 -1
  33. package/.npmrc.wtf +0 -1
  34. package/dist/common/getPlaywrightConstructs.d.ts +0 -35
  35. package/dist/common/getPlaywrightConstructs.js +0 -266
  36. package/dist/runtime/requestMoreInfo.d.ts +0 -18
  37. package/dist/runtime/requestMoreInfo.js +0 -25
@@ -1,266 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getPlaywrightConstructsForMode = getPlaywrightConstructsForMode;
7
- exports.getProductionPlaywrightConstructs = getProductionPlaywrightConstructs;
8
- exports.getRemotePlaywrightContext = getRemotePlaywrightContext;
9
- exports.loadSessionToContext = loadSessionToContext;
10
- var playwright = _interopRequireWildcard(require("playwright"));
11
- var _fsExtra = _interopRequireWildcard(require("fs-extra"));
12
- var fs = _fsExtra;
13
- var _contextStorageStateHelpers = require("./contextStorageStateHelpers");
14
- var _path = _interopRequireWildcard(require("path"));
15
- var _fileUtils = require("../commands/common/utils/fileUtils");
16
- var _waitOn = _interopRequireDefault(require("wait-on"));
17
- var _runtime = require("../runtime");
18
- var _asyncLocalStorage = require("./asyncLocalStorage");
19
- var _promises = require("timers/promises");
20
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
21
- 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); }
22
- 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; }
23
- async function createUserDirWithPreferences() {
24
- const playwrightTempDir = await (0, _fsExtra.mkdtemp)("/tmp/pw-");
25
- const userDir = (0, _path.join)(playwrightTempDir, "userdir");
26
- const defaultDir = (0, _path.join)(userDir, "Default");
27
- await (0, _fsExtra.mkdir)(defaultDir, {
28
- recursive: true
29
- });
30
- const preferences = {
31
- plugins: {
32
- always_open_pdf_externally: true
33
- }
34
- };
35
- await (0, _fsExtra.writeFile)((0, _path.join)(defaultDir, "Preferences"), JSON.stringify(preferences));
36
- return userDir;
37
- }
38
- async function loadInitializeContextHook(importFunction) {
39
- console.log("Loading initializeContext hook");
40
- const result = await importFunction("hooks/initializeContext");
41
- console.log({
42
- result
43
- });
44
- if (result.isErr()) {
45
- if (result.error.type === "not_found") {
46
- return null;
47
- }
48
- console.error("Error loading initializeBrowser hook");
49
- throw result.error.error;
50
- }
51
- return result.value.default;
52
- }
53
- async function getProductionPlaywrightConstructs({
54
- proxy,
55
- headless = true,
56
- storageState,
57
- downloadsPath,
58
- hookOptions
59
- }) {
60
- const extraArgs = [];
61
- const port = 9222;
62
- let hook = null;
63
- if (hookOptions) {
64
- hook = await loadInitializeContextHook(hookOptions.importFunction);
65
- }
66
- const userDataDir = await createUserDirWithPreferences();
67
- if (hook) {
68
- extraArgs.push(`--remote-debugging-port=${port}`);
69
- }
70
- const originalContext = await playwright.chromium.launchPersistentContext(userDataDir, {
71
- headless,
72
- viewport: null,
73
- proxy,
74
- downloadsPath,
75
- args: extraArgs
76
- });
77
- const cdpAddress = `http://localhost:${port}`;
78
- await (0, _promises.setTimeout)(3000);
79
- originalContext.once("close", async () => {
80
- try {
81
- await (0, _fsExtra.rm)(userDataDir, {
82
- recursive: true,
83
- force: true,
84
- retryDelay: 1000,
85
- maxRetries: 5
86
- });
87
- } catch (error) {
88
- console.error("Failed to remove user data dir", error);
89
- }
90
- });
91
- let page = originalContext.pages().at(0) ?? null;
92
- let context = originalContext;
93
- if (hookOptions && hook) {
94
- const hookResult = await hook({
95
- apiName: hookOptions.apiName,
96
- apiParameters: hookOptions.parameters,
97
- cdpAddress
98
- });
99
- if (hookResult !== null) {
100
- ({
101
- page,
102
- context
103
- } = hookResult);
104
- const {
105
- hookVars
106
- } = hookResult;
107
- const executionContext = (0, _asyncLocalStorage.getExecutionContext)();
108
- if (hookVars && executionContext) {
109
- executionContext.hookVars = hookVars;
110
- }
111
- context.once("close", async () => {
112
- await originalContext.close();
113
- });
114
- }
115
- }
116
- if (storageState) {
117
- await loadSessionToContext({
118
- context,
119
- session: storageState
120
- });
121
- }
122
- const assetsFile = _path.default.join(__dirname, "./assets/browser_scripts.js");
123
- await context.addInitScript({
124
- path: assetsFile
125
- });
126
- if (page) {
127
- const scriptString = await (0, _fsExtra.readFile)(assetsFile, "utf8");
128
- await page.evaluate(scriptString);
129
- } else {
130
- page = await context.newPage();
131
- }
132
- return {
133
- page,
134
- context
135
- };
136
- }
137
- async function getPlaywrightConstructsForMode(mode, cdpAddress, authSession, hookOptions) {
138
- let context = null;
139
- let page = null;
140
- if (hookOptions && cdpAddress) {
141
- const hook = await loadInitializeContextHook(hookOptions.importFunction);
142
- if (hook) {
143
- const hookResult = await hook({
144
- cdpAddress,
145
- apiName: hookOptions.apiName,
146
- apiParameters: hookOptions.parameters
147
- });
148
- if (hookResult !== null) {
149
- ({
150
- page,
151
- context
152
- } = hookResult);
153
- const {
154
- hookVars
155
- } = hookResult;
156
- const executionContext = (0, _asyncLocalStorage.getExecutionContext)();
157
- if (hookVars && executionContext) {
158
- executionContext.hookVars = hookVars;
159
- }
160
- }
161
- }
162
- }
163
- if (mode == "playwright-standalone") {
164
- if (!cdpAddress) {
165
- throw new Error("cdpAddress is required");
166
- }
167
- if (!context) {
168
- ({
169
- context
170
- } = await getRemotePlaywrightContext(cdpAddress));
171
- }
172
- if (!context) {
173
- throw new Error("no context found");
174
- }
175
- const assetsFile = _path.default.join(__dirname, "./assets/browser_scripts.js");
176
- await context.addInitScript({
177
- path: assetsFile
178
- });
179
- const pages = context.pages();
180
- if (!page) {
181
- if (pages.length > 0) {
182
- page = pages[0];
183
- const scriptString = await fs.readFile(assetsFile, "utf8");
184
- await page.evaluate(scriptString);
185
- } else {
186
- page = await context.newPage();
187
- }
188
- }
189
- if (authSession) {
190
- await loadSessionToContext({
191
- context,
192
- session: authSession
193
- });
194
- }
195
- return {
196
- page,
197
- context
198
- };
199
- }
200
- const downloadsPath = (0, _runtime.getDownloadDirectoryPath)();
201
- if (mode === "playwright" || mode === "playwright-headless") {
202
- const productionConstructs = await getProductionPlaywrightConstructs({
203
- headless: mode === "playwright-headless",
204
- downloadsPath
205
- });
206
- if (authSession) {
207
- await loadSessionToContext({
208
- context: productionConstructs.context,
209
- session: authSession
210
- });
211
- }
212
- return {
213
- ...productionConstructs
214
- };
215
- }
216
- throw "invalid mode";
217
- }
218
- async function loadSessionToContext({
219
- context,
220
- session
221
- }) {
222
- let sessionToLoad;
223
- if (session.type === "state") {
224
- const state = session.state;
225
- if (state === undefined || state === null) {
226
- return;
227
- }
228
- sessionToLoad = state;
229
- } else {
230
- const fullPath = (0, _fileUtils.getFullPathInProject)(session.path);
231
- sessionToLoad = await fs.readJson(fullPath);
232
- }
233
- await (0, _contextStorageStateHelpers.setStorageState)(context, sessionToLoad);
234
- }
235
- async function getRemotePlaywrightContext(cdpAddress) {
236
- const playwright = await Promise.resolve().then(() => _interopRequireWildcard(require("playwright")));
237
- let browser = null;
238
- if (!cdpAddress) {
239
- throw new Error("cdpAddress is required");
240
- }
241
- const cdpAddressWithoutProtocol = cdpAddress.replace("http://", "").replace("https://", "").replace("localhost", "127.0.0.1");
242
- try {
243
- await (0, _waitOn.default)({
244
- resources: [`http-get://${cdpAddressWithoutProtocol}/json/version`],
245
- delay: 100,
246
- interval: 100,
247
- timeout: 5000,
248
- tcpTimeout: 1000,
249
- window: 1000
250
- });
251
- } catch (error) {
252
- console.error("Failed to connect to the browser");
253
- process.exit(128 + 9);
254
- }
255
- try {
256
- browser = await playwright.chromium.connectOverCDP(cdpAddress);
257
- } catch (e) {
258
- console.log(e);
259
- throw new Error("failed to connect to the browser");
260
- }
261
- const context = browser.contexts()[0];
262
- return {
263
- browser,
264
- context
265
- };
266
- }
@@ -1,18 +0,0 @@
1
- interface RequestMoreInfoReturnTypeBase {
2
- [REQUEST_MORE_INFO_KEY]: true;
3
- action: "request_more_info";
4
- }
5
- interface RequestMultipleChoiceReturnType extends RequestMoreInfoReturnTypeBase {
6
- messageToUser: string;
7
- choices: string[];
8
- requestType: "multiple_choice";
9
- }
10
- interface RequestOtpReturnType extends RequestMoreInfoReturnTypeBase {
11
- messageToUser: string;
12
- requestType: "otp";
13
- }
14
- declare const REQUEST_MORE_INFO_KEY: unique symbol;
15
- export declare function requestOTP(messageToUser: string): RequestOtpReturnType;
16
- export declare function requestMultipleChoice(messageToUser: string, choices: string[]): RequestMultipleChoiceReturnType;
17
- export type RequestMoreInfoDetails = RequestMultipleChoiceReturnType | RequestOtpReturnType;
18
- export {};
@@ -1,25 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.requestMultipleChoice = requestMultipleChoice;
7
- exports.requestOTP = requestOTP;
8
- const REQUEST_MORE_INFO_KEY = Symbol("REQUEST_MORE_INFO");
9
- function requestOTP(messageToUser) {
10
- return {
11
- [REQUEST_MORE_INFO_KEY]: true,
12
- action: "request_more_info",
13
- messageToUser,
14
- requestType: "otp"
15
- };
16
- }
17
- function requestMultipleChoice(messageToUser, choices) {
18
- return {
19
- [REQUEST_MORE_INFO_KEY]: true,
20
- action: "request_more_info",
21
- messageToUser,
22
- requestType: "multiple_choice",
23
- choices
24
- };
25
- }