@intuned/runtime-dev 1.2.0-dev-peer → 1.2.1-hooks.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.
- package/.babelrc +2 -2
- package/.npmrc.wtf +1 -0
- package/CHANGELOG.md +5 -1
- package/WebTemplate.zip +0 -0
- package/bin/intuned +2 -0
- package/dist/commands/api/run.js +10 -3
- package/dist/commands/auth-sessions/run-check.js +18 -4
- package/dist/commands/auth-sessions/run-create.js +2 -1
- package/dist/commands/common/browserUtils.d.ts +1 -1
- package/dist/commands/common/browserUtils.js +1 -1
- package/dist/commands/common/getFirstLineNumber.js +2 -4
- package/dist/commands/interface/run.js +7 -6
- package/dist/commands/intuned-cli/commands/attempt.command.d.ts +1 -0
- package/dist/commands/intuned-cli/commands/attempt.command.js +8 -0
- package/dist/commands/intuned-cli/commands/attempt_api.command.d.ts +1 -0
- package/dist/commands/intuned-cli/commands/attempt_api.command.js +40 -0
- package/dist/commands/intuned-cli/commands/attempt_authsession.command.d.ts +1 -0
- package/dist/commands/intuned-cli/commands/attempt_authsession.command.js +8 -0
- package/dist/commands/intuned-cli/commands/attempt_authsession_check.command.d.ts +1 -0
- package/dist/commands/intuned-cli/commands/attempt_authsession_check.command.js +24 -0
- package/dist/commands/intuned-cli/commands/attempt_authsession_create.command.d.ts +1 -0
- package/dist/commands/intuned-cli/commands/attempt_authsession_create.command.js +36 -0
- package/dist/commands/intuned-cli/commands/build.command.d.ts +1 -0
- package/dist/commands/intuned-cli/commands/build.command.js +12 -0
- package/dist/commands/intuned-cli/commands/command.d.ts +2 -0
- package/dist/commands/intuned-cli/commands/command.js +9 -0
- package/dist/commands/intuned-cli/commands/deploy.command.d.ts +1 -0
- package/dist/commands/intuned-cli/commands/deploy.command.js +46 -0
- package/dist/commands/intuned-cli/commands/index.d.ts +15 -0
- package/dist/commands/intuned-cli/commands/index.js +170 -0
- package/dist/commands/intuned-cli/commands/init.command.d.ts +1 -0
- package/dist/commands/intuned-cli/commands/init.command.js +13 -0
- package/dist/commands/intuned-cli/commands/run.command.d.ts +1 -0
- package/dist/commands/intuned-cli/commands/run.command.js +8 -0
- package/dist/commands/intuned-cli/commands/run_api.command.d.ts +1 -0
- package/dist/commands/intuned-cli/commands/run_api.command.js +54 -0
- package/dist/commands/intuned-cli/commands/run_authsession.command.d.ts +20 -0
- package/dist/commands/intuned-cli/commands/run_authsession.command.js +13 -0
- package/dist/commands/intuned-cli/commands/run_authsession_create.command.d.ts +1 -0
- package/dist/commands/intuned-cli/commands/run_authsession_create.command.js +39 -0
- package/dist/commands/intuned-cli/commands/run_authsession_update.command.d.ts +1 -0
- package/dist/commands/intuned-cli/commands/run_authsession_update.command.js +39 -0
- package/dist/commands/intuned-cli/commands/run_authsession_validate.command.d.ts +1 -0
- package/dist/commands/intuned-cli/commands/run_authsession_validate.command.js +37 -0
- package/dist/commands/intuned-cli/commands/types.d.ts +21 -0
- package/dist/commands/intuned-cli/commands/types.js +21 -0
- package/dist/commands/intuned-cli/constants/index.d.ts +17 -0
- package/dist/commands/intuned-cli/constants/index.js +25 -0
- package/dist/commands/intuned-cli/controller/__test__/api.test.js +280 -0
- package/dist/commands/intuned-cli/controller/__test__/authSession.test.js +676 -0
- package/dist/commands/intuned-cli/controller/api.d.ts +44 -0
- package/dist/commands/intuned-cli/controller/api.js +181 -0
- package/dist/commands/intuned-cli/controller/authSession.d.ts +198 -0
- package/dist/commands/intuned-cli/controller/authSession.js +292 -0
- package/dist/commands/intuned-cli/controller/build.d.ts +1 -0
- package/dist/commands/intuned-cli/controller/build.js +36 -0
- package/dist/commands/intuned-cli/controller/deploy.d.ts +15 -0
- package/dist/commands/{deploy/utils.js → intuned-cli/controller/deploy.js} +115 -109
- package/dist/commands/intuned-cli/controller/index.d.ts +1 -0
- package/dist/commands/intuned-cli/controller/index.js +46 -0
- package/dist/commands/intuned-cli/helpers/api.d.ts +3 -0
- package/dist/commands/intuned-cli/helpers/api.js +19 -0
- package/dist/commands/intuned-cli/helpers/auth.d.ts +41 -0
- package/dist/commands/intuned-cli/helpers/auth.js +129 -0
- package/dist/commands/intuned-cli/helpers/backend.d.ts +8 -0
- package/dist/commands/intuned-cli/helpers/backend.js +26 -0
- package/dist/commands/intuned-cli/helpers/context.d.ts +3 -0
- package/dist/commands/intuned-cli/helpers/context.js +33 -0
- package/dist/commands/intuned-cli/helpers/errors.d.ts +14 -0
- package/dist/commands/intuned-cli/helpers/errors.js +55 -0
- package/dist/commands/intuned-cli/helpers/index.d.ts +10 -0
- package/dist/commands/intuned-cli/helpers/index.js +115 -0
- package/dist/commands/intuned-cli/helpers/intunedJson.d.ts +1 -0
- package/dist/{common/cli/utils.js → commands/intuned-cli/helpers/intunedJson.js} +0 -16
- package/dist/commands/intuned-cli/helpers/proxy.d.ts +5 -0
- package/dist/commands/intuned-cli/helpers/proxy.js +23 -0
- package/dist/commands/intuned-cli/helpers/terminal.d.ts +5 -0
- package/dist/commands/intuned-cli/helpers/terminal.js +13 -0
- package/dist/commands/intuned-cli/helpers/timeout.d.ts +1 -0
- package/dist/commands/intuned-cli/helpers/timeout.js +23 -0
- package/dist/commands/intuned-cli/helpers/validation.d.ts +2 -0
- package/dist/commands/intuned-cli/helpers/validation.js +12 -0
- package/dist/commands/intuned-cli/index.d.ts +1 -0
- package/dist/commands/intuned-cli/index.js +16 -0
- package/dist/commands/intuned-cli/main.d.ts +1 -0
- package/dist/commands/intuned-cli/main.js +15 -0
- package/dist/{common/cli → commands/intuned-cli}/types.d.ts +1 -34
- package/dist/{common/cli → commands/intuned-cli}/types.js +1 -2
- package/dist/common/assets/browser_scripts.js +2509 -2143
- package/dist/common/asyncLocalStorage/index.d.ts +2 -2
- package/dist/common/backendFunctions/getAuthSessionParameters.d.ts +1 -0
- package/dist/common/backendFunctions/getAuthSessionParameters.js +38 -0
- package/dist/common/constants.d.ts +1 -0
- package/dist/common/constants.js +2 -1
- package/dist/common/getPlaywrightConstructs.d.ts +6 -5
- package/dist/common/getPlaywrightConstructs.js +73 -37
- package/dist/common/jwtTokenManager.js +3 -5
- package/dist/common/runApi/errors.d.ts +1 -1
- package/dist/common/runApi/errors.js +4 -5
- package/dist/common/runApi/index.d.ts +4 -1
- package/dist/common/runApi/index.js +22 -21
- package/dist/common/runApi/types.d.ts +144 -13
- package/dist/common/runApi/types.js +28 -27
- package/dist/index.d.ts +1 -1
- package/dist/index.js +6 -0
- package/dist/runtime/executionHelpers.test.js +3 -4
- package/dist/runtime/export.d.ts +18 -0
- package/dist/runtime/extendPayload.js +1 -1
- package/dist/runtime/extendTimeout.js +0 -7
- package/dist/runtime/getAuthSessionParameters.d.ts +1 -0
- package/dist/runtime/getAuthSessionParameters.js +20 -0
- package/dist/runtime/getCdpAddress.d.ts +1 -0
- package/dist/runtime/getCdpAddress.js +11 -0
- package/dist/runtime/index.d.ts +2 -0
- package/dist/runtime/index.js +15 -1
- package/package.json +11 -10
- package/template.tsconfig.json +4 -7
- package/tsconfig.json +2 -1
- package/bin/check-auth-session +0 -3
- package/bin/cli-build +0 -3
- package/bin/create-auth-session +0 -3
- package/bin/deploy +0 -3
- package/bin/init +0 -3
- package/bin/run-api +0 -3
- package/dist/commands/cli-auth-sessions/check.d.ts +0 -2
- package/dist/commands/cli-auth-sessions/check.js +0 -40
- package/dist/commands/cli-auth-sessions/create.d.ts +0 -2
- package/dist/commands/cli-auth-sessions/create.js +0 -53
- package/dist/commands/cli-auth-sessions/utils.d.ts +0 -28
- package/dist/commands/cli-auth-sessions/utils.js +0 -285
- package/dist/commands/cli-build/cli-build.d.ts +0 -2
- package/dist/commands/cli-build/cli-build.js +0 -20
- package/dist/commands/deploy/deploy.d.ts +0 -2
- package/dist/commands/deploy/deploy.js +0 -47
- package/dist/commands/deploy/utils.d.ts +0 -16
- package/dist/commands/init/init.d.ts +0 -2
- package/dist/commands/init/init.js +0 -22
- package/dist/commands/init/utils.d.ts +0 -11
- package/dist/commands/init/utils.js +0 -181
- package/dist/commands/run-api-cli/run-api.d.ts +0 -2
- package/dist/commands/run-api-cli/run-api.js +0 -57
- package/dist/commands/run-api-cli/utils.d.ts +0 -9
- package/dist/commands/run-api-cli/utils.js +0 -144
- package/dist/common/cli/cliReadme.d.ts +0 -1
- package/dist/common/cli/cliReadme.js +0 -92
- package/dist/common/cli/constants.d.ts +0 -33
- package/dist/common/cli/constants.js +0 -39
- package/dist/common/cli/utils.d.ts +0 -6
|
@@ -5,12 +5,12 @@ export declare const asyncLocalStorage: AsyncLocalStorage<InternalRunInfo>;
|
|
|
5
5
|
export declare function runWithContext<R, TArgs extends any[]>(contextData: InternalRunInfo, callback: (...args: TArgs) => R, ...args: TArgs): R;
|
|
6
6
|
interface TimeoutInfo {
|
|
7
7
|
extendTimeoutCallback?: () => Promise<void>;
|
|
8
|
-
timeoutDuration?: number;
|
|
9
|
-
timeoutTimestamp?: number;
|
|
10
8
|
}
|
|
11
9
|
export interface InternalRunInfo extends RunInfo {
|
|
12
10
|
extendedPayloads: Payload[];
|
|
11
|
+
cdpAddress?: string;
|
|
13
12
|
timeoutInfo?: TimeoutInfo;
|
|
13
|
+
getAuthSessionParameters?: () => Promise<any>;
|
|
14
14
|
}
|
|
15
15
|
export declare function getExecutionContext(): InternalRunInfo | undefined;
|
|
16
16
|
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getAuthSessionParameters(): Promise<any>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getAuthSessionParameters = getAuthSessionParameters;
|
|
7
|
+
var _isNil = _interopRequireDefault(require("lodash/isNil"));
|
|
8
|
+
var _asyncLocalStorage = require("../asyncLocalStorage");
|
|
9
|
+
var _zod = require("zod");
|
|
10
|
+
var _jwtTokenManager = require("../jwtTokenManager");
|
|
11
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
12
|
+
const authSessionParametersResponseSchema = _zod.z.object({
|
|
13
|
+
parameters: _zod.z.any().refine(v => v !== undefined)
|
|
14
|
+
});
|
|
15
|
+
async function getAuthSessionParameters() {
|
|
16
|
+
const context = (0, _asyncLocalStorage.getExecutionContext)();
|
|
17
|
+
if (!context) {
|
|
18
|
+
throw new Error("getAuthSessionParameters failed due to an internal error (context was not found).");
|
|
19
|
+
}
|
|
20
|
+
const authSessionId = context.authSessionId;
|
|
21
|
+
if ((0, _isNil.default)(authSessionId)) {
|
|
22
|
+
throw new Error("Auth sessions are not enabled");
|
|
23
|
+
}
|
|
24
|
+
const response = await (0, _jwtTokenManager.callBackendFunctionWithToken)(`auth-session/${authSessionId}/parameters`, {
|
|
25
|
+
method: "GET"
|
|
26
|
+
});
|
|
27
|
+
const body = await response.text();
|
|
28
|
+
if (!response.ok) {
|
|
29
|
+
throw new Error(`getAuthSessionParameters failed with status ${response.status}: ${body}`);
|
|
30
|
+
}
|
|
31
|
+
let json;
|
|
32
|
+
try {
|
|
33
|
+
json = JSON.parse(body);
|
|
34
|
+
} catch (e) {
|
|
35
|
+
throw new Error(`Expected JSON response, but got ${body}`);
|
|
36
|
+
}
|
|
37
|
+
return authSessionParametersResponseSchema.parse(json).parameters;
|
|
38
|
+
}
|
package/dist/common/constants.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.AUTH_SESSIONS_INSTANCES_FOLDER_NAME = exports.AUTH_SESSIONS_FOLDER_NAME = void 0;
|
|
6
|
+
exports.AUTH_SESSIONS_INSTANCES_FOLDER_NAME = exports.AUTH_SESSIONS_FOLDER_NAME = exports.API_FOLDER_NAME = void 0;
|
|
7
|
+
const API_FOLDER_NAME = exports.API_FOLDER_NAME = "api";
|
|
7
8
|
const AUTH_SESSIONS_FOLDER_NAME = exports.AUTH_SESSIONS_FOLDER_NAME = "auth-sessions";
|
|
8
9
|
const AUTH_SESSIONS_INSTANCES_FOLDER_NAME = exports.AUTH_SESSIONS_INSTANCES_FOLDER_NAME = "auth-sessions-instances";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import * as playwright from "playwright
|
|
2
|
-
import type { RunApiSession } from "./runApi";
|
|
3
|
-
interface Proxy {
|
|
1
|
+
import * as playwright from "playwright";
|
|
2
|
+
import type { ImportFunction, RunApiSession } from "./runApi";
|
|
3
|
+
export interface Proxy {
|
|
4
4
|
server: string;
|
|
5
5
|
username: string;
|
|
6
6
|
password: string;
|
|
@@ -10,12 +10,13 @@ interface GetPlaywrightConstructsOptions {
|
|
|
10
10
|
headless?: boolean;
|
|
11
11
|
storageState?: RunApiSession;
|
|
12
12
|
downloadsPath: string;
|
|
13
|
+
importFunction?: ImportFunction;
|
|
13
14
|
}
|
|
14
|
-
export declare function getProductionPlaywrightConstructs({ proxy, headless, storageState, downloadsPath, }: GetPlaywrightConstructsOptions): Promise<{
|
|
15
|
+
export declare function getProductionPlaywrightConstructs({ proxy, headless, storageState, downloadsPath, importFunction, }: GetPlaywrightConstructsOptions): Promise<{
|
|
15
16
|
page: playwright.Page;
|
|
16
17
|
context: playwright.BrowserContext;
|
|
17
18
|
}>;
|
|
18
|
-
export declare function getPlaywrightConstructsForMode(mode: "vanilla" | "playwright" | "playwright-standalone" | "playwright-headless", cdpAddress: string | undefined, authSession?: RunApiSession): Promise<{
|
|
19
|
+
export declare function getPlaywrightConstructsForMode(mode: "vanilla" | "playwright" | "playwright-standalone" | "playwright-headless", cdpAddress: string | undefined, authSession?: RunApiSession, importFunction?: ImportFunction): Promise<{
|
|
19
20
|
page: playwright.Page;
|
|
20
21
|
context: playwright.BrowserContext;
|
|
21
22
|
}>;
|
|
@@ -7,7 +7,7 @@ exports.getPlaywrightConstructsForMode = getPlaywrightConstructsForMode;
|
|
|
7
7
|
exports.getProductionPlaywrightConstructs = getProductionPlaywrightConstructs;
|
|
8
8
|
exports.getRemotePlaywrightContext = getRemotePlaywrightContext;
|
|
9
9
|
exports.loadSessionToContext = loadSessionToContext;
|
|
10
|
-
var playwright = _interopRequireWildcard(require("playwright
|
|
10
|
+
var playwright = _interopRequireWildcard(require("playwright"));
|
|
11
11
|
var _fsExtra = _interopRequireWildcard(require("fs-extra"));
|
|
12
12
|
var fs = _fsExtra;
|
|
13
13
|
var _contextStorageStateHelpers = require("./contextStorageStateHelpers");
|
|
@@ -33,42 +33,60 @@ async function createUserDirWithPreferences() {
|
|
|
33
33
|
await (0, _fsExtra.writeFile)((0, _path.join)(defaultDir, "Preferences"), JSON.stringify(preferences));
|
|
34
34
|
return userDir;
|
|
35
35
|
}
|
|
36
|
+
async function loadGetDeployedPageAndContextHook(importFunction) {
|
|
37
|
+
const result = await importFunction("hooks/createDeployedContextAndPage");
|
|
38
|
+
if (result.isErr()) {
|
|
39
|
+
throw result.error;
|
|
40
|
+
}
|
|
41
|
+
return result.value.default;
|
|
42
|
+
}
|
|
36
43
|
async function getProductionPlaywrightConstructs({
|
|
37
44
|
proxy,
|
|
38
45
|
headless = true,
|
|
39
46
|
storageState,
|
|
40
|
-
downloadsPath
|
|
47
|
+
downloadsPath,
|
|
48
|
+
importFunction
|
|
41
49
|
}) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
50
|
+
let context;
|
|
51
|
+
let page;
|
|
52
|
+
let hook = null;
|
|
53
|
+
if (importFunction) {
|
|
54
|
+
hook = await loadGetDeployedPageAndContextHook(importFunction);
|
|
45
55
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
userAgent
|
|
59
|
-
});
|
|
60
|
-
context.once("close", async () => {
|
|
61
|
-
try {
|
|
62
|
-
await (0, _fsExtra.rm)(userDataDir, {
|
|
63
|
-
recursive: true,
|
|
64
|
-
force: true,
|
|
65
|
-
retryDelay: 1000,
|
|
66
|
-
maxRetries: 5
|
|
67
|
-
});
|
|
68
|
-
} catch (error) {
|
|
69
|
-
console.error("Failed to remove user data dir", error);
|
|
56
|
+
if (hook) {
|
|
57
|
+
({
|
|
58
|
+
page,
|
|
59
|
+
context
|
|
60
|
+
} = await hook({
|
|
61
|
+
proxy,
|
|
62
|
+
headless
|
|
63
|
+
}));
|
|
64
|
+
} else {
|
|
65
|
+
const extraArgs = ["--no-first-run", "--disable-sync", "--disable-translate", "--disable-features=TranslateUI", "--disable-features=NetworkService", "--lang=en"];
|
|
66
|
+
if (headless) {
|
|
67
|
+
extraArgs.push("--headless=new");
|
|
70
68
|
}
|
|
71
|
-
|
|
69
|
+
const userDataDir = await createUserDirWithPreferences();
|
|
70
|
+
context = await playwright.chromium.launchPersistentContext(userDataDir, {
|
|
71
|
+
headless,
|
|
72
|
+
viewport: null,
|
|
73
|
+
proxy,
|
|
74
|
+
downloadsPath
|
|
75
|
+
});
|
|
76
|
+
context.once("close", async () => {
|
|
77
|
+
try {
|
|
78
|
+
await (0, _fsExtra.rm)(userDataDir, {
|
|
79
|
+
recursive: true,
|
|
80
|
+
force: true,
|
|
81
|
+
retryDelay: 1000,
|
|
82
|
+
maxRetries: 5
|
|
83
|
+
});
|
|
84
|
+
} catch (error) {
|
|
85
|
+
console.error("Failed to remove user data dir", error);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
page = context.pages().at(0) ?? null;
|
|
89
|
+
}
|
|
72
90
|
if (storageState) {
|
|
73
91
|
await loadSessionToContext({
|
|
74
92
|
context,
|
|
@@ -79,7 +97,6 @@ async function getProductionPlaywrightConstructs({
|
|
|
79
97
|
await context.addInitScript({
|
|
80
98
|
path: assetsFile
|
|
81
99
|
});
|
|
82
|
-
let page = context.pages().at(0);
|
|
83
100
|
if (page) {
|
|
84
101
|
const scriptString = await (0, _fsExtra.readFile)(assetsFile, "utf8");
|
|
85
102
|
await page.evaluate(scriptString);
|
|
@@ -91,15 +108,34 @@ async function getProductionPlaywrightConstructs({
|
|
|
91
108
|
context
|
|
92
109
|
};
|
|
93
110
|
}
|
|
94
|
-
|
|
95
|
-
|
|
111
|
+
async function loadGetIDEPageAndContextHook(importFunction) {
|
|
112
|
+
const result = await importFunction("hooks/createDeployedContextAndPage");
|
|
113
|
+
if (result.isErr()) {
|
|
114
|
+
throw result.error;
|
|
115
|
+
}
|
|
116
|
+
return result.value.default;
|
|
117
|
+
}
|
|
118
|
+
async function getPlaywrightConstructsForMode(mode, cdpAddress, authSession, importFunction) {
|
|
119
|
+
let hook = null;
|
|
120
|
+
if (importFunction) {
|
|
121
|
+
hook = await loadGetIDEPageAndContextHook(importFunction);
|
|
122
|
+
}
|
|
96
123
|
if (mode == "playwright-standalone") {
|
|
97
124
|
if (!cdpAddress) {
|
|
98
125
|
throw new Error("cdpAddress is required");
|
|
99
126
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
127
|
+
let context;
|
|
128
|
+
if (hook) {
|
|
129
|
+
({
|
|
130
|
+
context
|
|
131
|
+
} = await hook({
|
|
132
|
+
cdpAddress
|
|
133
|
+
}));
|
|
134
|
+
} else {
|
|
135
|
+
({
|
|
136
|
+
context
|
|
137
|
+
} = await getRemotePlaywrightContext(cdpAddress));
|
|
138
|
+
}
|
|
103
139
|
if (!context) {
|
|
104
140
|
throw new Error("no context found");
|
|
105
141
|
}
|
|
@@ -163,7 +199,7 @@ async function loadSessionToContext({
|
|
|
163
199
|
await (0, _contextStorageStateHelpers.setStorageState)(context, sessionToLoad);
|
|
164
200
|
}
|
|
165
201
|
async function getRemotePlaywrightContext(cdpAddress) {
|
|
166
|
-
const playwright = await Promise.resolve().then(() => _interopRequireWildcard(require("playwright
|
|
202
|
+
const playwright = await Promise.resolve().then(() => _interopRequireWildcard(require("playwright")));
|
|
167
203
|
let browser = null;
|
|
168
204
|
if (!cdpAddress) {
|
|
169
205
|
throw new Error("cdpAddress is required");
|
|
@@ -36,8 +36,7 @@ class JwtTokenManager {
|
|
|
36
36
|
return Math.max(timeToRefresh, timeWindow);
|
|
37
37
|
}
|
|
38
38
|
async scheduleTokenRefresh() {
|
|
39
|
-
|
|
40
|
-
if (((_process$env$RUN_ENVI = process.env.RUN_ENVIRONMENT) === null || _process$env$RUN_ENVI === void 0 ? void 0 : _process$env$RUN_ENVI.toLowerCase()) !== "authoring") return;
|
|
39
|
+
if (process.env.RUN_ENVIRONMENT?.toLowerCase() !== "authoring") return;
|
|
41
40
|
const timeToRefresh = this.timeToRefresh;
|
|
42
41
|
if (timeToRefresh === undefined) return;
|
|
43
42
|
if (this.tokenRefreshTimeout) clearTimeout(this.tokenRefreshTimeout);
|
|
@@ -51,8 +50,7 @@ class JwtTokenManager {
|
|
|
51
50
|
}, timeToRefresh);
|
|
52
51
|
}
|
|
53
52
|
async refreshToken() {
|
|
54
|
-
|
|
55
|
-
if (((_process$env$RUN_ENVI2 = process.env.RUN_ENVIRONMENT) === null || _process$env$RUN_ENVI2 === void 0 ? void 0 : _process$env$RUN_ENVI2.toLowerCase()) !== "authoring") return;
|
|
53
|
+
if (process.env.RUN_ENVIRONMENT?.toLowerCase() !== "authoring") return;
|
|
56
54
|
const res = await this.fetchWithToken(this.refreshTokenPath, {
|
|
57
55
|
method: "GET"
|
|
58
56
|
});
|
|
@@ -65,7 +63,7 @@ class JwtTokenManager {
|
|
|
65
63
|
if (newToken) this._token = newToken;
|
|
66
64
|
}
|
|
67
65
|
fetchWithToken(...[input, init]) {
|
|
68
|
-
const headers = new Headers(init
|
|
66
|
+
const headers = new Headers(init?.headers);
|
|
69
67
|
headers.set("Authorization", `Bearer ${this.token}`);
|
|
70
68
|
return (0, _crossFetch.default)(input, {
|
|
71
69
|
...init,
|
|
@@ -10,7 +10,7 @@ export declare const maxLevelsExceededErrorCode = "MaxLevelsExceededError";
|
|
|
10
10
|
export declare const automationError = "AutomationError";
|
|
11
11
|
export declare const internalInvalidInputErrorCode = "InternalInvalidInputError";
|
|
12
12
|
export declare const runAutomationErrorCodes: readonly ["APINotFoundError", "InvalidAPIError", "InvalidCheckError", "AbortedError", "AuthRequiredError", "AuthCheckNotFoundError", "AuthCheckFailedError", "MaxLevelsExceededError", "AutomationError", "InternalInvalidInputError"];
|
|
13
|
-
export type RunAutomationErrorCode =
|
|
13
|
+
export type RunAutomationErrorCode = typeof runAutomationErrorCodes[number];
|
|
14
14
|
export declare abstract class RunAutomationError<T = any> {
|
|
15
15
|
code: RunAutomationErrorCode;
|
|
16
16
|
statusCode: number;
|
|
@@ -22,11 +22,10 @@ const runAutomationErrorCodes = exports.runAutomationErrorCodes = [apiNotFoundEr
|
|
|
22
22
|
class RunAutomationError {
|
|
23
23
|
wrapped = false;
|
|
24
24
|
get json() {
|
|
25
|
-
var _this$cause;
|
|
26
25
|
return {
|
|
27
26
|
code: this.code,
|
|
28
27
|
details: this.details,
|
|
29
|
-
cause:
|
|
28
|
+
cause: this.cause?.json
|
|
30
29
|
};
|
|
31
30
|
}
|
|
32
31
|
}
|
|
@@ -136,10 +135,10 @@ class AutomationError extends RunAutomationError {
|
|
|
136
135
|
};
|
|
137
136
|
}
|
|
138
137
|
this.statusCode = 500;
|
|
139
|
-
this.message = `[${
|
|
138
|
+
this.message = `[${error?.name ?? error}] ${error?.message}`;
|
|
140
139
|
this.details = {
|
|
141
|
-
name: error
|
|
142
|
-
message: error
|
|
140
|
+
name: error?.name,
|
|
141
|
+
message: error?.message
|
|
143
142
|
};
|
|
144
143
|
}
|
|
145
144
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Result } from "neverthrow";
|
|
2
2
|
import { RunAutomationError } from "./errors";
|
|
3
|
-
import { Page, BrowserContext } from "playwright
|
|
3
|
+
import { Page, BrowserContext } from "playwright";
|
|
4
4
|
import { ExtendedRunApiParameters, RunApiResult, RunApiResultWithSessionOk } from "./types";
|
|
5
5
|
export * from "./types";
|
|
6
6
|
export * from "./errors";
|
|
@@ -8,6 +8,9 @@ export declare function runApiGenerator<ResultType = any, _YieldType = any, _Nex
|
|
|
8
8
|
retrieveSession: true;
|
|
9
9
|
}): AsyncGenerator<_YieldType, RunApiResult<ResultType, RunApiResultWithSessionOk>, _NextType>;
|
|
10
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 & {
|
|
12
|
+
retrieveSession: true;
|
|
13
|
+
}): Promise<RunApiResult<ResultType, RunApiResultWithSessionOk>>;
|
|
11
14
|
export declare function runApi<ResultType = any>(params: ExtendedRunApiParameters): Promise<RunApiResult<ResultType>>;
|
|
12
15
|
export declare function checkAuthSessionWithRetries(page: Page, context: BrowserContext, checkFn: (..._: any) => Promise<boolean>, retries?: number): Promise<Result<boolean, RunAutomationError>>;
|
|
13
16
|
export type ImportFunction = ExtendedRunApiParameters["importFunction"];
|
|
@@ -80,17 +80,15 @@ async function* runApiGenerator({
|
|
|
80
80
|
return;
|
|
81
81
|
}
|
|
82
82
|
try {
|
|
83
|
-
|
|
84
|
-
await ((_context = context) === null || _context === void 0 ? void 0 : _context.tracing.stop({
|
|
83
|
+
await context?.tracing.stop({
|
|
85
84
|
path: tracing.filePath
|
|
86
|
-
})
|
|
85
|
+
});
|
|
87
86
|
} catch (error) {
|
|
88
|
-
console.log(errorMessage, error
|
|
87
|
+
console.log(errorMessage, error?.message);
|
|
89
88
|
await (0, _fsExtra.remove)(tracing.filePath);
|
|
90
89
|
}
|
|
91
90
|
}
|
|
92
91
|
async function* runAutomation() {
|
|
93
|
-
var _getExecutionContext;
|
|
94
92
|
let page;
|
|
95
93
|
const validatedModuleResult = await importUsingImportFunction(automationFunction.name, importFunction);
|
|
96
94
|
if (validatedModuleResult.isErr()) {
|
|
@@ -98,7 +96,7 @@ async function* runApiGenerator({
|
|
|
98
96
|
}
|
|
99
97
|
const importedModule = validatedModuleResult.value;
|
|
100
98
|
let checkFn;
|
|
101
|
-
if (auth
|
|
99
|
+
if (auth?.runCheck) {
|
|
102
100
|
if (!auth.session) {
|
|
103
101
|
return (0, _neverthrow.err)(new _errors.AuthRequiredError());
|
|
104
102
|
}
|
|
@@ -130,7 +128,8 @@ async function* runApiGenerator({
|
|
|
130
128
|
headless,
|
|
131
129
|
proxy,
|
|
132
130
|
downloadsPath,
|
|
133
|
-
storageState: auth
|
|
131
|
+
storageState: auth?.session,
|
|
132
|
+
importFunction
|
|
134
133
|
}));
|
|
135
134
|
} else {
|
|
136
135
|
const {
|
|
@@ -140,7 +139,7 @@ async function* runApiGenerator({
|
|
|
140
139
|
({
|
|
141
140
|
page,
|
|
142
141
|
context
|
|
143
|
-
} = await (0, _getPlaywrightConstructs.getPlaywrightConstructsForMode)(mode, cdpAddress, auth
|
|
142
|
+
} = await (0, _getPlaywrightConstructs.getPlaywrightConstructsForMode)(mode, cdpAddress, auth?.session, importFunction));
|
|
144
143
|
}
|
|
145
144
|
if (tracing.enabled) {
|
|
146
145
|
await context.tracing.start({
|
|
@@ -152,20 +151,23 @@ async function* runApiGenerator({
|
|
|
152
151
|
}
|
|
153
152
|
(0, _cleanEnvironmentVariables.cleanEnvironmentVariables)();
|
|
154
153
|
if (checkFn !== undefined) {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
154
|
+
try {
|
|
155
|
+
console.log("Running auth check");
|
|
156
|
+
const authCheckResult = await checkAuthSessionWithRetries(page, context, checkFn, 2);
|
|
157
|
+
if (authCheckResult.isErr()) {
|
|
158
|
+
const error = authCheckResult.error;
|
|
159
|
+
if (["APINotFoundError", "InvalidAPIError"].includes(error.code)) {
|
|
160
|
+
return (0, _neverthrow.err)(new _errors.InvalidCheckError(`Auth session check function failed`, error));
|
|
161
|
+
}
|
|
162
|
+
return authCheckResult;
|
|
161
163
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
164
|
+
if (!authCheckResult.value) {
|
|
165
|
+
return (0, _neverthrow.err)(new _errors.AuthCheckFailedError());
|
|
166
|
+
}
|
|
167
|
+
} catch (error) {
|
|
165
168
|
return (0, _neverthrow.err)(new _errors.AuthCheckFailedError());
|
|
166
169
|
}
|
|
167
170
|
}
|
|
168
|
-
console.log("Running automation");
|
|
169
171
|
const automationFunctionParameters = [...(automationFunction.params !== undefined ? [automationFunction.params] : []), page, context];
|
|
170
172
|
let result;
|
|
171
173
|
if (importedModule.type === "async-generator") {
|
|
@@ -185,7 +187,7 @@ async function* runApiGenerator({
|
|
|
185
187
|
}
|
|
186
188
|
return (0, _neverthrow.ok)({
|
|
187
189
|
result,
|
|
188
|
-
extendedPayloads: (
|
|
190
|
+
extendedPayloads: (0, _asyncLocalStorage.getExecutionContext)()?.extendedPayloads,
|
|
189
191
|
session: retrieveSession ? await (0, _contextStorageStateHelpers.getStorageState)(context) : undefined
|
|
190
192
|
});
|
|
191
193
|
}
|
|
@@ -206,11 +208,10 @@ async function* runApiGenerator({
|
|
|
206
208
|
} catch (error) {
|
|
207
209
|
return (0, _neverthrow.err)(new _errors.AutomationError(error));
|
|
208
210
|
} finally {
|
|
209
|
-
var _context2;
|
|
210
211
|
await saveTraceIfNeeded({
|
|
211
212
|
errorMessage: "failed to save trace"
|
|
212
213
|
});
|
|
213
|
-
await
|
|
214
|
+
await context?.close();
|
|
214
215
|
if (downloadsPath !== undefined) {
|
|
215
216
|
await fs.remove(downloadsPath);
|
|
216
217
|
}
|