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

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 (36) 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 +240 -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 +86 -181
  22. package/dist/common/runApi/types.d.ts +20 -25
  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 -2
  28. package/dist/runtime/index.js +5 -11
  29. package/dist/runtime/store.d.ts +2 -0
  30. package/dist/runtime/store.js +23 -0
  31. package/package.json +1 -1
  32. package/.npmrc.wtf +0 -1
  33. package/dist/common/getPlaywrightConstructs.d.ts +0 -35
  34. package/dist/common/getPlaywrightConstructs.js +0 -267
  35. package/dist/runtime/requestMoreInfo.d.ts +0 -18
  36. package/dist/runtime/requestMoreInfo.js +0 -25
@@ -1,267 +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 _getPort = _interopRequireDefault(require("get-port"));
20
- var _promises = require("timers/promises");
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 loadInitializeContextHook(importFunction) {
40
- console.log("Loading initializeContext hook");
41
- const result = await importFunction("hooks/initializeContext");
42
- console.log({
43
- result
44
- });
45
- if (result.isErr()) {
46
- if (result.error.type === "not_found") {
47
- return null;
48
- }
49
- console.error("Error loading initializeBrowser hook");
50
- throw result.error.error;
51
- }
52
- return result.value.default;
53
- }
54
- async function getProductionPlaywrightConstructs({
55
- proxy,
56
- headless = true,
57
- storageState,
58
- downloadsPath,
59
- hookOptions
60
- }) {
61
- const extraArgs = [];
62
- const port = await (0, _getPort.default)();
63
- let hook = null;
64
- if (hookOptions) {
65
- hook = await loadInitializeContextHook(hookOptions.importFunction);
66
- }
67
- const userDataDir = await createUserDirWithPreferences();
68
- if (hook) {
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);
90
- }
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
99
- });
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;
111
- }
112
- context.once("close", async () => {
113
- await originalContext.close();
114
- });
115
- }
116
- }
117
- if (storageState) {
118
- await loadSessionToContext({
119
- context,
120
- session: storageState
121
- });
122
- }
123
- const assetsFile = _path.default.join(__dirname, "./assets/browser_scripts.js");
124
- await context.addInitScript({
125
- path: assetsFile
126
- });
127
- if (page) {
128
- const scriptString = await (0, _fsExtra.readFile)(assetsFile, "utf8");
129
- await page.evaluate(scriptString);
130
- } else {
131
- page = await context.newPage();
132
- }
133
- return {
134
- page,
135
- context
136
- };
137
- }
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
- }
163
- }
164
- if (mode == "playwright-standalone") {
165
- if (!cdpAddress) {
166
- throw new Error("cdpAddress is required");
167
- }
168
- if (!context) {
169
- ({
170
- context
171
- } = await getRemotePlaywrightContext(cdpAddress));
172
- }
173
- if (!context) {
174
- throw new Error("no context found");
175
- }
176
- const assetsFile = _path.default.join(__dirname, "./assets/browser_scripts.js");
177
- await context.addInitScript({
178
- path: assetsFile
179
- });
180
- const pages = context.pages();
181
- if (!page) {
182
- if (pages.length > 0) {
183
- page = pages[0];
184
- const scriptString = await fs.readFile(assetsFile, "utf8");
185
- await page.evaluate(scriptString);
186
- } else {
187
- page = await context.newPage();
188
- }
189
- }
190
- if (authSession) {
191
- await loadSessionToContext({
192
- context,
193
- session: authSession
194
- });
195
- }
196
- return {
197
- page,
198
- context
199
- };
200
- }
201
- const downloadsPath = (0, _runtime.getDownloadDirectoryPath)();
202
- if (mode === "playwright" || mode === "playwright-headless") {
203
- const productionConstructs = await getProductionPlaywrightConstructs({
204
- headless: mode === "playwright-headless",
205
- downloadsPath
206
- });
207
- if (authSession) {
208
- await loadSessionToContext({
209
- context: productionConstructs.context,
210
- session: authSession
211
- });
212
- }
213
- return {
214
- ...productionConstructs
215
- };
216
- }
217
- throw "invalid mode";
218
- }
219
- async function loadSessionToContext({
220
- context,
221
- session
222
- }) {
223
- let sessionToLoad;
224
- if (session.type === "state") {
225
- const state = session.state;
226
- if (state === undefined || state === null) {
227
- return;
228
- }
229
- sessionToLoad = state;
230
- } else {
231
- const fullPath = (0, _fileUtils.getFullPathInProject)(session.path);
232
- sessionToLoad = await fs.readJson(fullPath);
233
- }
234
- await (0, _contextStorageStateHelpers.setStorageState)(context, sessionToLoad);
235
- }
236
- async function getRemotePlaywrightContext(cdpAddress) {
237
- const playwright = await Promise.resolve().then(() => _interopRequireWildcard(require("playwright")));
238
- let browser = null;
239
- if (!cdpAddress) {
240
- throw new Error("cdpAddress is required");
241
- }
242
- const cdpAddressWithoutProtocol = cdpAddress.replace("http://", "").replace("https://", "").replace("localhost", "127.0.0.1");
243
- try {
244
- await (0, _waitOn.default)({
245
- resources: [`http-get://${cdpAddressWithoutProtocol}/json/version`],
246
- delay: 100,
247
- interval: 100,
248
- timeout: 5000,
249
- tcpTimeout: 1000,
250
- window: 1000
251
- });
252
- } catch (error) {
253
- console.error("Failed to connect to the browser");
254
- process.exit(128 + 9);
255
- }
256
- try {
257
- browser = await playwright.chromium.connectOverCDP(cdpAddress);
258
- } catch (e) {
259
- console.log(e);
260
- throw new Error("failed to connect to the browser");
261
- }
262
- const context = browser.contexts()[0];
263
- return {
264
- browser,
265
- context
266
- };
267
- }
@@ -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
- }