@intuned/runtime-dev 1.3.18-interface.13 → 1.3.18-interface.16
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 +27 -0
- package/.claude/settings.local.json +7 -0
- package/.eslintignore +10 -0
- package/.eslintrc.js +39 -0
- package/InterfaceTemplate/__utils.ts +3 -1
- package/WebTemplate.zip +0 -0
- package/dist/commands/api/run.d.ts +1 -3
- package/dist/commands/api/run.js +64 -2039
- package/dist/commands/auth-sessions/load.d.ts +1 -0
- package/dist/commands/auth-sessions/load.js +28 -1552
- package/dist/commands/auth-sessions/run-check.d.ts +1 -0
- package/dist/commands/auth-sessions/run-check.js +71 -1961
- package/dist/commands/auth-sessions/run-create.d.ts +1 -0
- package/dist/commands/auth-sessions/run-create.js +65 -1955
- package/dist/commands/browser/save-state.d.ts +1 -0
- package/dist/commands/browser/save-state.js +11 -102
- package/dist/commands/browser/start-browser.d.ts +1 -0
- package/dist/commands/browser/start-browser.js +9 -62
- package/dist/commands/build.d.ts +1 -2
- package/dist/commands/build.js +52 -172
- package/dist/commands/common/browserUtils.d.ts +14 -0
- package/dist/commands/common/browserUtils.js +57 -0
- package/dist/commands/common/getFirstLineNumber.d.ts +9 -0
- package/dist/commands/common/getFirstLineNumber.js +101 -0
- package/dist/commands/common/getFirstLineNumber.test.js +227 -0
- package/dist/commands/common/projectExclusions.d.ts +2 -0
- package/dist/commands/common/projectExclusions.js +8 -0
- package/dist/commands/common/sendMessageToClient.d.ts +1 -0
- package/dist/commands/common/sendMessageToClient.js +10 -0
- package/dist/commands/common/tsNodeImport.d.ts +2 -5
- package/dist/commands/common/tsNodeImport.js +15 -55
- package/dist/commands/common/utils/fileUtils.d.ts +6 -0
- package/dist/commands/common/utils/fileUtils.js +32 -0
- package/dist/commands/common/utils/interfaceClient.d.ts +31 -0
- package/dist/commands/common/utils/interfaceClient.js +98 -0
- package/dist/commands/common/utils/settings.d.ts +2 -0
- package/dist/commands/common/utils/settings.js +31 -0
- package/dist/commands/common/utils/template.d.ts +2 -0
- package/dist/commands/common/utils/template.js +30 -0
- package/dist/commands/get-headless-user-agent.d.ts +1 -2
- package/dist/commands/get-headless-user-agent.js +12 -348
- package/dist/commands/interface/pingPong.d.ts +2 -0
- package/dist/commands/interface/pingPong.js +11 -0
- package/dist/commands/interface/run.d.ts +1 -3
- package/dist/commands/interface/run.js +139 -2034
- package/dist/commands/interface/runApi.d.ts +1 -0
- package/dist/commands/interface/runApi.js +102 -0
- 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 +30 -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 +19 -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 +24 -0
- package/dist/commands/intuned-cli/commands/authsession.command.d.ts +1 -0
- package/dist/commands/intuned-cli/commands/authsession.command.js +8 -0
- package/dist/commands/intuned-cli/commands/authsession_record.command.d.ts +1 -0
- package/dist/commands/intuned-cli/commands/authsession_record.command.js +32 -0
- package/dist/commands/intuned-cli/commands/authsession_scaffold.command.d.ts +1 -0
- package/dist/commands/intuned-cli/commands/authsession_scaffold.command.js +16 -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 +42 -0
- package/dist/commands/intuned-cli/commands/index.d.ts +18 -0
- package/dist/commands/intuned-cli/commands/index.js +203 -0
- package/dist/commands/intuned-cli/commands/provision.command.d.ts +13 -0
- package/dist/commands/intuned-cli/commands/provision.command.js +50 -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 +46 -0
- package/dist/commands/intuned-cli/commands/run_authsession.command.d.ts +35 -0
- package/dist/commands/intuned-cli/commands/run_authsession.command.js +17 -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 +35 -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 +35 -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 +33 -0
- package/dist/commands/intuned-cli/commands/types.d.ts +39 -0
- package/dist/commands/intuned-cli/commands/types.js +33 -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 +398 -0
- package/dist/commands/intuned-cli/controller/__test__/authSession.test.js +1060 -0
- package/dist/commands/intuned-cli/controller/api.d.ts +42 -0
- package/dist/commands/intuned-cli/controller/api.js +194 -0
- package/dist/commands/intuned-cli/controller/authSession.d.ts +209 -0
- package/dist/commands/intuned-cli/controller/authSession.js +425 -0
- package/dist/commands/intuned-cli/controller/build.d.ts +1 -0
- package/dist/commands/intuned-cli/controller/build.js +35 -0
- package/dist/commands/intuned-cli/controller/deploy.d.ts +5 -0
- package/dist/commands/intuned-cli/controller/deploy.js +317 -0
- package/dist/commands/intuned-cli/controller/index.d.ts +1 -0
- package/dist/commands/intuned-cli/controller/index.js +45 -0
- package/dist/commands/intuned-cli/controller/provision.d.ts +21 -0
- package/dist/commands/intuned-cli/controller/provision.js +299 -0
- package/dist/commands/intuned-cli/controller/scaffold.d.ts +1 -0
- package/dist/commands/intuned-cli/controller/scaffold.js +77 -0
- package/dist/commands/intuned-cli/helpers/__test__/browser.test.js +152 -0
- package/dist/commands/intuned-cli/helpers/__test__/tracing.test.js +41 -0
- package/dist/commands/intuned-cli/helpers/api.d.ts +3 -0
- package/dist/commands/intuned-cli/helpers/api.js +16 -0
- package/dist/commands/intuned-cli/helpers/auth.d.ts +46 -0
- package/dist/commands/intuned-cli/helpers/auth.js +147 -0
- package/dist/commands/intuned-cli/helpers/backend.d.ts +8 -0
- package/dist/commands/intuned-cli/helpers/backend.js +35 -0
- package/dist/commands/intuned-cli/helpers/browser.d.ts +14 -0
- package/dist/commands/intuned-cli/helpers/browser.js +93 -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 +16 -0
- package/dist/commands/intuned-cli/helpers/errors.js +44 -0
- package/dist/commands/intuned-cli/helpers/index.d.ts +12 -0
- package/dist/commands/intuned-cli/helpers/index.js +137 -0
- package/dist/commands/intuned-cli/helpers/intunedJson.d.ts +39 -0
- package/dist/commands/intuned-cli/helpers/intunedJson.js +20 -0
- package/dist/commands/intuned-cli/helpers/prompts.d.ts +3 -0
- package/dist/commands/intuned-cli/helpers/prompts.js +71 -0
- 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/traces.d.ts +2 -0
- package/dist/commands/intuned-cli/helpers/traces.js +32 -0
- package/dist/commands/intuned-cli/helpers/validation.d.ts +2 -0
- package/dist/commands/intuned-cli/helpers/validation.js +14 -0
- package/dist/commands/intuned-cli/helpers/wrapper.d.ts +2 -0
- package/dist/commands/intuned-cli/helpers/wrapper.js +76 -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 -2
- package/dist/commands/intuned-cli/main.js +24 -5392
- package/dist/commands/intuned-cli/types.d.ts +70 -0
- package/dist/commands/intuned-cli/types.js +22 -0
- package/dist/commands/ts-check.d.ts +1 -0
- package/dist/commands/ts-check.js +28 -131
- package/dist/common/Logger/Logger/index.d.ts +12 -0
- package/dist/common/Logger/Logger/index.js +60 -0
- package/dist/common/Logger/Logger/types.d.ts +8 -0
- package/dist/common/Logger/Logger/types.js +5 -0
- package/dist/common/Logger/index.d.ts +12 -0
- package/dist/common/Logger/index.js +60 -0
- package/dist/common/Logger/types.d.ts +8 -0
- package/dist/common/Logger/types.js +5 -0
- package/dist/common/asyncLocalStorage/index.d.ts +8 -9
- package/dist/common/asyncLocalStorage/index.js +9 -33
- package/dist/common/backendFunctions/getAuthSessionParameters.d.ts +1 -0
- package/dist/common/backendFunctions/getAuthSessionParameters.js +38 -0
- package/dist/common/binStartupScript.d.ts +1 -2
- package/dist/common/binStartupScript.js +10 -126
- package/dist/common/browserTabs.d.ts +72 -0
- package/dist/common/browserTabs.js +74 -0
- package/dist/common/cleanEnvironmentVariables.d.ts +1 -3
- package/dist/common/cleanEnvironmentVariables.js +7 -29
- package/dist/common/constants.d.ts +11 -13
- package/dist/common/constants.js +15 -57
- package/dist/common/contextStorageStateHelpers.d.ts +6 -8
- package/dist/common/contextStorageStateHelpers.js +22 -46
- package/dist/common/extension/extensionsHelpers.d.ts +11 -0
- package/dist/common/extension/extensionsHelpers.js +147 -0
- package/dist/common/extension/intunedExtensionServer.d.ts +24 -0
- package/dist/common/extension/intunedExtensionServer.js +178 -0
- package/dist/common/extension/types.d.ts +219 -0
- package/dist/common/extension/types.js +51 -0
- package/dist/common/formatZodError.d.ts +2 -0
- package/dist/common/formatZodError.js +18 -0
- package/dist/common/intunedJson.d.ts +229 -0
- package/dist/common/intunedJson.js +132 -0
- package/dist/common/jwtTokenManager.d.ts +4 -6
- package/dist/common/jwtTokenManager.js +40 -107
- package/dist/common/launchBrowser.d.ts +34 -0
- package/dist/common/launchBrowser.js +247 -0
- package/dist/common/playwrightContext.d.ts +29 -0
- package/dist/common/playwrightContext.js +148 -0
- package/dist/common/runApi/importUsingImportFunction.d.ts +7 -0
- package/dist/common/runApi/importUsingImportFunction.js +46 -0
- package/dist/common/runApi/index.d.ts +6 -9
- package/dist/common/runApi/index.js +70 -1765
- package/dist/common/settingsSchema.d.ts +6 -9
- package/dist/common/settingsSchema.js +18 -53
- package/dist/common/setupContextHook.d.ts +16 -0
- package/dist/common/setupContextHook.js +22 -0
- package/dist/common/telemetry.d.ts +3 -6
- package/dist/common/telemetry.js +8 -40
- package/dist/index.d.ts +4 -6
- package/dist/index.js +92 -783
- package/dist/runtime/RunError.d.ts +5 -0
- package/dist/runtime/RunError.js +19 -0
- package/dist/runtime/attemptStore.d.ts +2 -0
- package/dist/runtime/attemptStore.js +23 -0
- package/dist/runtime/captcha.d.ts +15 -0
- package/dist/runtime/captcha.js +191 -0
- package/dist/runtime/captcha.test.js +821 -0
- package/dist/runtime/downloadDirectory.d.ts +1 -0
- package/dist/runtime/downloadDirectory.js +19 -0
- package/dist/runtime/enums.d.js +5 -0
- package/dist/runtime/enums.d.ts +11 -0
- package/dist/runtime/enums.js +18 -0
- package/dist/runtime/executionHelpers.test.js +52 -0
- package/dist/runtime/export.d.js +5 -0
- package/dist/runtime/export.d.ts +284 -0
- package/dist/runtime/extendPayload.d.ts +2 -0
- package/dist/runtime/extendPayload.js +21 -0
- package/dist/runtime/extendTimeout.d.ts +1 -0
- package/dist/runtime/extendTimeout.js +23 -0
- package/dist/runtime/getAiGatewayConfig.d.ts +10 -0
- package/dist/runtime/getAiGatewayConfig.js +16 -0
- package/dist/runtime/getAuthSessionParameters.d.ts +1 -0
- package/dist/runtime/getAuthSessionParameters.js +20 -0
- package/dist/runtime/index.d.ts +10 -168
- package/dist/runtime/index.js +88 -778
- package/dist/runtime/persistentStore.d.ts +2 -0
- package/dist/runtime/persistentStore.js +37 -0
- package/dist/runtime/persistentStore.test.js +101 -0
- package/dist/runtime/runInfo.d.ts +2 -0
- package/dist/runtime/runInfo.js +21 -0
- package/dist/vendor/runtime-interface.d.ts +1 -0
- package/dist/vendor/runtime-interface.js +474 -0
- package/package.json +18 -72
- package/template.tsconfig.json +11 -0
- package/tsconfig.eslint.json +5 -0
- package/tsconfig.json +25 -0
- package/tsup.config.ts +11 -0
- package/typedoc.json +49 -0
- package/dist/export.d-BAUMB-lG.d.ts +0 -140
|
@@ -1,2061 +1,170 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
var
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
)
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
exports2.maxLevelsExceededErrorCode = "MaxLevelsExceededError";
|
|
46
|
-
exports2.automationError = "AutomationError";
|
|
47
|
-
exports2.internalInvalidInputErrorCode = "InternalInvalidInputError";
|
|
48
|
-
exports2.resultTooBigErrorCode = "ResultTooBigError";
|
|
49
|
-
exports2.runAutomationErrorCodes = [
|
|
50
|
-
exports2.apiNotFoundErrorCode,
|
|
51
|
-
exports2.invalidApiErrorCode,
|
|
52
|
-
exports2.invalidCheckErrorCode,
|
|
53
|
-
exports2.abortedErrorCode,
|
|
54
|
-
exports2.authRequiredErrorCode,
|
|
55
|
-
exports2.authCheckNotFoundErrorCode,
|
|
56
|
-
exports2.authCheckFailedErrorCode,
|
|
57
|
-
exports2.maxLevelsExceededErrorCode,
|
|
58
|
-
exports2.automationError,
|
|
59
|
-
exports2.internalInvalidInputErrorCode,
|
|
60
|
-
exports2.resultTooBigErrorCode
|
|
61
|
-
];
|
|
62
|
-
var RunAutomationError4 = class {
|
|
63
|
-
constructor(code, message) {
|
|
64
|
-
this.code = code;
|
|
65
|
-
this.message = message;
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.runAutomationCLI = runAutomationCLI;
|
|
7
|
+
var _commander = require("commander");
|
|
8
|
+
var _dotenv = _interopRequireDefault(require("dotenv"));
|
|
9
|
+
var _asyncLocalStorage = require("../../common/asyncLocalStorage");
|
|
10
|
+
var _runApi = require("../../common/runApi");
|
|
11
|
+
var _enums = require("../../runtime/enums");
|
|
12
|
+
var _promises = require("timers/promises");
|
|
13
|
+
var _jwtTokenManager = require("../../common/jwtTokenManager");
|
|
14
|
+
var _formatZodError = require("../../common/formatZodError");
|
|
15
|
+
var _neverthrow = require("neverthrow");
|
|
16
|
+
var _getAuthSessionParameters = require("../../common/backendFunctions/getAuthSessionParameters");
|
|
17
|
+
var _runtimeInterface = require("../../vendor/runtime-interface");
|
|
18
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
19
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
20
|
+
_dotenv.default.config({
|
|
21
|
+
path: `.env`
|
|
22
|
+
});
|
|
23
|
+
function runAutomationCLI(importFunction) {
|
|
24
|
+
_commander.program.description("run user automation and communicate using unix socket").argument("<input>", "In unix socket mode: path to the unix socket. In jsonl mode: path to the jsonl file. In tcp socket mode: host:port").option("--mode [mode]", "Communication mode: unix, jsonl, or tcp").action(async (input, {
|
|
25
|
+
mode = "unix"
|
|
26
|
+
}) => {
|
|
27
|
+
let context;
|
|
28
|
+
const throttleTime = 60 * 1000;
|
|
29
|
+
let timeoutTimestamp = Date.now();
|
|
30
|
+
const client = createClient(mode, input);
|
|
31
|
+
const abortController = new AbortController();
|
|
32
|
+
const interruptSignalHandler = async () => {
|
|
33
|
+
abortController.abort();
|
|
34
|
+
await (0, _promises.setTimeout)(60_000);
|
|
35
|
+
await client.close();
|
|
36
|
+
process.exit(1);
|
|
37
|
+
};
|
|
38
|
+
process.on("SIGINT", interruptSignalHandler);
|
|
39
|
+
process.on("SIGTERM", interruptSignalHandler);
|
|
40
|
+
const messagesGenerator = client.receiveJSON();
|
|
41
|
+
async function receiveMessages() {
|
|
42
|
+
const data = await messagesGenerator.next();
|
|
43
|
+
if (data.done) {
|
|
44
|
+
return;
|
|
66
45
|
}
|
|
67
|
-
|
|
46
|
+
const inputParseResult = _runtimeInterface.runApiInputSchema.safeParse(data.value);
|
|
47
|
+
if (!inputParseResult.success) {
|
|
48
|
+
console.error("Validation error:", (0, _formatZodError.formatZodError)(inputParseResult.error));
|
|
68
49
|
return {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
cause: this.cause?.json
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
exports2.RunAutomationError = RunAutomationError4;
|
|
76
|
-
var ApiNotFoundError3 = class extends RunAutomationError4 {
|
|
77
|
-
constructor(apiName) {
|
|
78
|
-
super(exports2.apiNotFoundErrorCode, `API ${apiName} not found`);
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
exports2.ApiNotFoundError = ApiNotFoundError3;
|
|
82
|
-
var InvalidApiError2 = class extends RunAutomationError4 {
|
|
83
|
-
constructor(message) {
|
|
84
|
-
super(exports2.invalidApiErrorCode, `API is invalid: ${message}`);
|
|
85
|
-
this.details = {
|
|
86
|
-
message
|
|
50
|
+
type: "error",
|
|
51
|
+
error: new _runtimeInterface.InternalInvalidInputError("Invalid input", (0, _formatZodError.formatZodError)(inputParseResult.error))
|
|
87
52
|
};
|
|
88
53
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
var InvalidCheckError = class extends RunAutomationError4 {
|
|
92
|
-
constructor(message, cause) {
|
|
93
|
-
super(exports2.invalidCheckErrorCode, message);
|
|
94
|
-
this.cause = cause;
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
exports2.InvalidCheckError = InvalidCheckError;
|
|
98
|
-
var AbortedError2 = class extends RunAutomationError4 {
|
|
99
|
-
constructor() {
|
|
100
|
-
super(exports2.abortedErrorCode, "Operation was aborted");
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
exports2.AbortedError = AbortedError2;
|
|
104
|
-
var AuthRequiredError2 = class extends RunAutomationError4 {
|
|
105
|
-
constructor() {
|
|
106
|
-
super(exports2.authRequiredErrorCode, "AuthSessions are required");
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
exports2.AuthRequiredError = AuthRequiredError2;
|
|
110
|
-
var AuthCheckNotFoundError = class extends RunAutomationError4 {
|
|
111
|
-
constructor() {
|
|
112
|
-
super(exports2.authCheckNotFoundErrorCode, "AuthSession check not found");
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
exports2.AuthCheckNotFoundError = AuthCheckNotFoundError;
|
|
116
|
-
var AuthCheckFailedError = class extends RunAutomationError4 {
|
|
117
|
-
constructor() {
|
|
118
|
-
super(exports2.authCheckFailedErrorCode, "AuthSession check failed");
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
exports2.AuthCheckFailedError = AuthCheckFailedError;
|
|
122
|
-
var MaxLevelsExceededError = class extends RunAutomationError4 {
|
|
123
|
-
constructor(levels) {
|
|
124
|
-
super(exports2.maxLevelsExceededErrorCode, `Max levels exceeded. Only ${levels} levels are supported`);
|
|
125
|
-
this.details = { levels };
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
exports2.MaxLevelsExceededError = MaxLevelsExceededError;
|
|
129
|
-
var AutomationError4 = class extends RunAutomationError4 {
|
|
130
|
-
constructor(error) {
|
|
131
|
-
super(exports2.automationError, `[${error?.name ?? error}] ${error?.message}`);
|
|
132
|
-
this.details = {
|
|
133
|
-
...error
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
exports2.AutomationError = AutomationError4;
|
|
138
|
-
var InternalInvalidInputError3 = class extends RunAutomationError4 {
|
|
139
|
-
constructor(message, details) {
|
|
140
|
-
super(exports2.internalInvalidInputErrorCode, message);
|
|
141
|
-
this.details = details;
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
exports2.InternalInvalidInputError = InternalInvalidInputError3;
|
|
145
|
-
var ResultTooBigError2 = class extends RunAutomationError4 {
|
|
146
|
-
constructor(sizeInBytes, maxSizeInBytes) {
|
|
147
|
-
super(exports2.resultTooBigErrorCode, `Automation result is too big. Size: ${Math.round(sizeInBytes / 1024 / 1024 * 100) / 100}MB, Max allowed: ${Math.round(maxSizeInBytes / 1024 / 1024 * 100) / 100}MB`);
|
|
148
|
-
this.details = { sizeInBytes, maxSizeInBytes };
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
exports2.ResultTooBigError = ResultTooBigError2;
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
// ../packages/runtime-interface/dist/types.js
|
|
156
|
-
var require_types = __commonJS({
|
|
157
|
-
"../packages/runtime-interface/dist/types.js"(exports2) {
|
|
158
|
-
"use strict";
|
|
159
|
-
var __importDefault = exports2 && exports2.__importDefault || function(mod) {
|
|
160
|
-
return mod && mod.__esModule ? mod : { "default": mod };
|
|
161
|
-
};
|
|
162
|
-
Object.defineProperty(exports2, "__esModule", { value: true });
|
|
163
|
-
exports2.outputRunApiSchema = exports2.pongMessageSchema = exports2.doneMessageSchema = exports2.extendMessageSchema = exports2.runApiInputSchema = exports2.pingSchema = exports2.tokenUpdateSchema = exports2.abortRunApiSchema = exports2.startRunApiSchema = exports2.startRunApiParametersSchema = exports2.runApiParametersSchema = exports2.runApiRunOptionsSchema = exports2.runApiCdpRunOptionsSchema = exports2.runApiStandaloneRunOptionsSchema = exports2.runApiAuthSchema = exports2.runApiTracingSchema = exports2.runApiAutomationFunctionSchema = exports2.runApiSessionSchema = exports2.runApiStorageStateSchema = void 0;
|
|
164
|
-
exports2.runApiResultOkSchema = runApiResultOkSchema;
|
|
165
|
-
exports2.runApiResultWithSessionOkSchema = runApiResultWithSessionOkSchema;
|
|
166
|
-
var zod_1 = __importDefault(require("zod"));
|
|
167
|
-
exports2.runApiStorageStateSchema = zod_1.default.object({
|
|
168
|
-
cookies: zod_1.default.array(zod_1.default.object({
|
|
169
|
-
name: zod_1.default.string(),
|
|
170
|
-
value: zod_1.default.string(),
|
|
171
|
-
domain: zod_1.default.string(),
|
|
172
|
-
path: zod_1.default.string(),
|
|
173
|
-
expires: zod_1.default.number(),
|
|
174
|
-
httpOnly: zod_1.default.boolean(),
|
|
175
|
-
secure: zod_1.default.boolean(),
|
|
176
|
-
sameSite: zod_1.default.enum(["Strict", "Lax", "None"])
|
|
177
|
-
})),
|
|
178
|
-
origins: zod_1.default.array(zod_1.default.object({
|
|
179
|
-
origin: zod_1.default.string(),
|
|
180
|
-
localStorage: zod_1.default.array(zod_1.default.object({
|
|
181
|
-
name: zod_1.default.string(),
|
|
182
|
-
value: zod_1.default.string()
|
|
183
|
-
}))
|
|
184
|
-
})),
|
|
185
|
-
sessionStorage: zod_1.default.array(zod_1.default.object({
|
|
186
|
-
origin: zod_1.default.string(),
|
|
187
|
-
sessionStorage: zod_1.default.array(zod_1.default.object({
|
|
188
|
-
name: zod_1.default.string(),
|
|
189
|
-
value: zod_1.default.string()
|
|
190
|
-
}))
|
|
191
|
-
})).optional()
|
|
192
|
-
});
|
|
193
|
-
exports2.runApiSessionSchema = zod_1.default.discriminatedUnion("type", [
|
|
194
|
-
zod_1.default.object({
|
|
195
|
-
type: zod_1.default.literal("file"),
|
|
196
|
-
path: zod_1.default.string()
|
|
197
|
-
}),
|
|
198
|
-
zod_1.default.object({
|
|
199
|
-
type: zod_1.default.literal("state"),
|
|
200
|
-
state: exports2.runApiStorageStateSchema.nullable().optional()
|
|
201
|
-
})
|
|
202
|
-
]);
|
|
203
|
-
exports2.runApiAutomationFunctionSchema = zod_1.default.object({
|
|
204
|
-
name: zod_1.default.string(),
|
|
205
|
-
params: zod_1.default.any().optional()
|
|
206
|
-
});
|
|
207
|
-
exports2.runApiTracingSchema = zod_1.default.discriminatedUnion("enabled", [
|
|
208
|
-
zod_1.default.object({ enabled: zod_1.default.literal(false) }),
|
|
209
|
-
zod_1.default.object({ enabled: zod_1.default.literal(true), filePath: zod_1.default.string() })
|
|
210
|
-
]).optional().default({ enabled: false });
|
|
211
|
-
exports2.runApiAuthSchema = zod_1.default.object({
|
|
212
|
-
session: exports2.runApiSessionSchema,
|
|
213
|
-
parameters: zod_1.default.record(zod_1.default.any()).optional()
|
|
214
|
-
}).optional();
|
|
215
|
-
exports2.runApiStandaloneRunOptionsSchema = zod_1.default.object({
|
|
216
|
-
environment: zod_1.default.literal("standalone"),
|
|
217
|
-
headless: zod_1.default.boolean().default(true),
|
|
218
|
-
proxy: zod_1.default.object({
|
|
219
|
-
server: zod_1.default.string(),
|
|
220
|
-
username: zod_1.default.string(),
|
|
221
|
-
password: zod_1.default.string()
|
|
222
|
-
}).optional()
|
|
223
|
-
});
|
|
224
|
-
exports2.runApiCdpRunOptionsSchema = zod_1.default.object({
|
|
225
|
-
environment: zod_1.default.literal("cdp"),
|
|
226
|
-
cdpAddress: zod_1.default.string(),
|
|
227
|
-
cdpTargetId: zod_1.default.string().optional()
|
|
228
|
-
});
|
|
229
|
-
exports2.runApiRunOptionsSchema = zod_1.default.discriminatedUnion("environment", [
|
|
230
|
-
exports2.runApiStandaloneRunOptionsSchema,
|
|
231
|
-
exports2.runApiCdpRunOptionsSchema
|
|
232
|
-
]).default({ environment: "standalone", headless: true });
|
|
233
|
-
exports2.runApiParametersSchema = zod_1.default.object({
|
|
234
|
-
automationFunction: exports2.runApiAutomationFunctionSchema,
|
|
235
|
-
tracing: exports2.runApiTracingSchema,
|
|
236
|
-
auth: exports2.runApiAuthSchema,
|
|
237
|
-
runOptions: exports2.runApiRunOptionsSchema,
|
|
238
|
-
retrieveSession: zod_1.default.boolean().default(false)
|
|
239
|
-
});
|
|
240
|
-
function runApiResultOkSchema(resultSchema) {
|
|
241
|
-
return zod_1.default.object({
|
|
242
|
-
result: resultSchema,
|
|
243
|
-
extendedPayloads: zod_1.default.array(zod_1.default.object({
|
|
244
|
-
api: zod_1.default.string(),
|
|
245
|
-
parameters: zod_1.default.record(zod_1.default.any())
|
|
246
|
-
})).optional()
|
|
247
|
-
});
|
|
54
|
+
const input = inputParseResult.data;
|
|
55
|
+
return input;
|
|
248
56
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
exports2.startRunApiParametersSchema = exports2.runApiParametersSchema.extend({
|
|
255
|
-
retrieveSession: zod_1.default.boolean(),
|
|
256
|
-
functionsToken: zod_1.default.string().optional(),
|
|
257
|
-
context: zod_1.default.object({
|
|
258
|
-
jobId: zod_1.default.string().optional(),
|
|
259
|
-
jobRunId: zod_1.default.string().optional(),
|
|
260
|
-
runId: zod_1.default.string().optional(),
|
|
261
|
-
queueId: zod_1.default.string().optional(),
|
|
262
|
-
authSessionId: zod_1.default.string().optional()
|
|
263
|
-
}).optional()
|
|
264
|
-
});
|
|
265
|
-
exports2.startRunApiSchema = zod_1.default.object({
|
|
266
|
-
type: zod_1.default.literal("start"),
|
|
267
|
-
parameters: exports2.startRunApiParametersSchema
|
|
268
|
-
});
|
|
269
|
-
exports2.abortRunApiSchema = zod_1.default.object({
|
|
270
|
-
type: zod_1.default.literal("abort"),
|
|
271
|
-
parameters: zod_1.default.object({}).optional()
|
|
272
|
-
});
|
|
273
|
-
exports2.tokenUpdateSchema = zod_1.default.object({
|
|
274
|
-
type: zod_1.default.literal("tokenUpdate"),
|
|
275
|
-
parameters: zod_1.default.object({
|
|
276
|
-
functionsToken: zod_1.default.string()
|
|
277
|
-
})
|
|
278
|
-
});
|
|
279
|
-
exports2.pingSchema = zod_1.default.object({
|
|
280
|
-
type: zod_1.default.literal("ping"),
|
|
281
|
-
parameters: zod_1.default.object({}).optional()
|
|
282
|
-
});
|
|
283
|
-
exports2.runApiInputSchema = zod_1.default.union([
|
|
284
|
-
exports2.startRunApiSchema,
|
|
285
|
-
exports2.abortRunApiSchema,
|
|
286
|
-
exports2.tokenUpdateSchema,
|
|
287
|
-
exports2.pingSchema
|
|
288
|
-
]);
|
|
289
|
-
exports2.extendMessageSchema = zod_1.default.object({ type: zod_1.default.literal("extend") });
|
|
290
|
-
exports2.doneMessageSchema = zod_1.default.object({
|
|
291
|
-
type: zod_1.default.literal("done"),
|
|
292
|
-
result: zod_1.default.any(),
|
|
293
|
-
success: zod_1.default.boolean()
|
|
294
|
-
});
|
|
295
|
-
exports2.pongMessageSchema = zod_1.default.object({
|
|
296
|
-
type: zod_1.default.literal("pong")
|
|
297
|
-
});
|
|
298
|
-
exports2.outputRunApiSchema = zod_1.default.union([
|
|
299
|
-
exports2.extendMessageSchema,
|
|
300
|
-
exports2.doneMessageSchema,
|
|
301
|
-
exports2.pongMessageSchema
|
|
302
|
-
]);
|
|
303
|
-
}
|
|
304
|
-
});
|
|
305
|
-
|
|
306
|
-
// ../packages/runtime-interface/dist/interfaceClient.js
|
|
307
|
-
var require_interfaceClient = __commonJS({
|
|
308
|
-
"../packages/runtime-interface/dist/interfaceClient.js"(exports2) {
|
|
309
|
-
"use strict";
|
|
310
|
-
var __createBinding = exports2 && exports2.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
311
|
-
if (k2 === void 0) k2 = k;
|
|
312
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
313
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
314
|
-
desc = { enumerable: true, get: function() {
|
|
315
|
-
return m[k];
|
|
316
|
-
} };
|
|
317
|
-
}
|
|
318
|
-
Object.defineProperty(o, k2, desc);
|
|
319
|
-
}) : (function(o, m, k, k2) {
|
|
320
|
-
if (k2 === void 0) k2 = k;
|
|
321
|
-
o[k2] = m[k];
|
|
322
|
-
}));
|
|
323
|
-
var __setModuleDefault = exports2 && exports2.__setModuleDefault || (Object.create ? (function(o, v) {
|
|
324
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
325
|
-
}) : function(o, v) {
|
|
326
|
-
o["default"] = v;
|
|
327
|
-
});
|
|
328
|
-
var __importStar = exports2 && exports2.__importStar || function(mod) {
|
|
329
|
-
if (mod && mod.__esModule) return mod;
|
|
330
|
-
var result = {};
|
|
331
|
-
if (mod != null) {
|
|
332
|
-
for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
333
|
-
}
|
|
334
|
-
__setModuleDefault(result, mod);
|
|
335
|
-
return result;
|
|
336
|
-
};
|
|
337
|
-
Object.defineProperty(exports2, "__esModule", { value: true });
|
|
338
|
-
exports2.JSONLFileClient = exports2.TCPSocketClient = exports2.UnixSocketClient = exports2.SocketClient = void 0;
|
|
339
|
-
var net = __importStar(require("net"));
|
|
340
|
-
var fs5 = __importStar(require("fs-extra"));
|
|
341
|
-
var readline_1 = require("readline");
|
|
342
|
-
var promises_1 = require("timers/promises");
|
|
343
|
-
var SocketClient = class _SocketClient {
|
|
344
|
-
constructor(socket) {
|
|
345
|
-
this.socket = socket;
|
|
57
|
+
let resultPromise = null;
|
|
58
|
+
async function handleMessage(message) {
|
|
59
|
+
if (message.type === "abort") {
|
|
60
|
+
abortController.abort();
|
|
61
|
+
return;
|
|
346
62
|
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
const buffer = Buffer.alloc(_SocketClient.LENGTH_HEADER_LENGTH + length);
|
|
351
|
-
buffer.writeUInt32BE(length, 0);
|
|
352
|
-
buffer.write(dataToSend, _SocketClient.LENGTH_HEADER_LENGTH);
|
|
353
|
-
this.socket.write(buffer);
|
|
63
|
+
if (message.type === "tokenUpdate") {
|
|
64
|
+
_jwtTokenManager.backendFunctionsTokenManager.token = message.parameters.functionsToken;
|
|
65
|
+
return;
|
|
354
66
|
}
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
67
|
+
if (message.type === "start") {
|
|
68
|
+
context = {
|
|
69
|
+
extendedPayloads: [],
|
|
70
|
+
runEnvironment: message.parameters.runOptions.environment === "standalone" ? _enums.RunEnvironment.DEPLOYED : _enums.RunEnvironment.IDE,
|
|
71
|
+
timeoutInfo: {
|
|
72
|
+
extendTimeoutCallback: async () => {
|
|
73
|
+
if (Date.now() - timeoutTimestamp < throttleTime) return;
|
|
74
|
+
timeoutTimestamp = Date.now();
|
|
75
|
+
client.sendJSON({
|
|
76
|
+
type: "extend"
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
getAuthSessionParameters: _getAuthSessionParameters.getAuthSessionParameters,
|
|
81
|
+
...(message.parameters.context ?? {}),
|
|
82
|
+
proxy: getProxyUrlFromRunOptions(message.parameters.runOptions)
|
|
83
|
+
};
|
|
84
|
+
_jwtTokenManager.backendFunctionsTokenManager.token = message.parameters.functionsToken;
|
|
85
|
+
resultPromise = (0, _asyncLocalStorage.runWithContext)(context, async () => {
|
|
86
|
+
return await (0, _runApi.runApi)({
|
|
87
|
+
...message.parameters,
|
|
88
|
+
abortSignal: abortController.signal,
|
|
89
|
+
importFunction: importFunction ?? _defaultImport
|
|
360
90
|
});
|
|
361
|
-
this.socket.once("error", reject);
|
|
362
|
-
});
|
|
363
|
-
while (true) {
|
|
364
|
-
const chunk = await Promise.race([
|
|
365
|
-
new Promise((resolve2) => this.socket.once("data", (data2) => {
|
|
366
|
-
if (typeof data2 === "string") {
|
|
367
|
-
return resolve2(Buffer.from(data2));
|
|
368
|
-
}
|
|
369
|
-
resolve2(data2);
|
|
370
|
-
})),
|
|
371
|
-
endPromise
|
|
372
|
-
]);
|
|
373
|
-
if (!(chunk instanceof Buffer)) {
|
|
374
|
-
break;
|
|
375
|
-
}
|
|
376
|
-
buffer = Buffer.concat([buffer, chunk]);
|
|
377
|
-
const length = buffer.readUInt32BE(0);
|
|
378
|
-
if (buffer.length < length + _SocketClient.LENGTH_HEADER_LENGTH) {
|
|
379
|
-
continue;
|
|
380
|
-
}
|
|
381
|
-
const data = buffer.subarray(_SocketClient.LENGTH_HEADER_LENGTH, length + _SocketClient.LENGTH_HEADER_LENGTH);
|
|
382
|
-
buffer = buffer.subarray(length + _SocketClient.LENGTH_HEADER_LENGTH);
|
|
383
|
-
yield JSON.parse(data.toString());
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
async close() {
|
|
387
|
-
this.socket.end();
|
|
388
|
-
await Promise.race([
|
|
389
|
-
new Promise((resolve2) => this.socket.once("close", resolve2)),
|
|
390
|
-
new Promise((resolve2) => this.socket.once("error", resolve2)),
|
|
391
|
-
(0, promises_1.setTimeout)(3e3)
|
|
392
|
-
]);
|
|
393
|
-
}
|
|
394
|
-
get closed() {
|
|
395
|
-
return this.socket.closed;
|
|
396
|
-
}
|
|
397
|
-
};
|
|
398
|
-
exports2.SocketClient = SocketClient;
|
|
399
|
-
SocketClient.LENGTH_HEADER_LENGTH = 4;
|
|
400
|
-
var UnixSocketClient2 = class extends SocketClient {
|
|
401
|
-
constructor(path3) {
|
|
402
|
-
super(net.createConnection(path3));
|
|
403
|
-
}
|
|
404
|
-
};
|
|
405
|
-
exports2.UnixSocketClient = UnixSocketClient2;
|
|
406
|
-
var TCPSocketClient2 = class extends SocketClient {
|
|
407
|
-
constructor(host, port) {
|
|
408
|
-
super(net.createConnection(port, host));
|
|
409
|
-
}
|
|
410
|
-
};
|
|
411
|
-
exports2.TCPSocketClient = TCPSocketClient2;
|
|
412
|
-
var JSONLFileClient2 = class {
|
|
413
|
-
constructor(filePath) {
|
|
414
|
-
this.fileStream = fs5.createReadStream(filePath, { encoding: "utf-8" });
|
|
415
|
-
}
|
|
416
|
-
sendJSON(data) {
|
|
417
|
-
console.log("Sending message", data);
|
|
418
|
-
}
|
|
419
|
-
async *receiveJSON() {
|
|
420
|
-
const rl = (0, readline_1.createInterface)({
|
|
421
|
-
input: this.fileStream,
|
|
422
|
-
crlfDelay: Infinity
|
|
423
91
|
});
|
|
424
|
-
for await (const line of rl) {
|
|
425
|
-
if (line.trim() === "") {
|
|
426
|
-
continue;
|
|
427
|
-
}
|
|
428
|
-
yield JSON.parse(line);
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
async close() {
|
|
432
|
-
this.fileStream.close();
|
|
433
|
-
await Promise.race([
|
|
434
|
-
new Promise((resolve2) => this.fileStream.once("close", resolve2)),
|
|
435
|
-
new Promise((resolve2) => this.fileStream.once("error", resolve2)),
|
|
436
|
-
(0, promises_1.setTimeout)(3e3)
|
|
437
|
-
]);
|
|
438
|
-
}
|
|
439
|
-
get closed() {
|
|
440
|
-
return this.fileStream.closed;
|
|
441
|
-
}
|
|
442
|
-
};
|
|
443
|
-
exports2.JSONLFileClient = JSONLFileClient2;
|
|
444
|
-
}
|
|
445
|
-
});
|
|
446
|
-
|
|
447
|
-
// ../packages/runtime-interface/dist/index.js
|
|
448
|
-
var require_dist = __commonJS({
|
|
449
|
-
"../packages/runtime-interface/dist/index.js"(exports2) {
|
|
450
|
-
"use strict";
|
|
451
|
-
var __createBinding = exports2 && exports2.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
452
|
-
if (k2 === void 0) k2 = k;
|
|
453
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
454
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
455
|
-
desc = { enumerable: true, get: function() {
|
|
456
|
-
return m[k];
|
|
457
|
-
} };
|
|
458
|
-
}
|
|
459
|
-
Object.defineProperty(o, k2, desc);
|
|
460
|
-
}) : (function(o, m, k, k2) {
|
|
461
|
-
if (k2 === void 0) k2 = k;
|
|
462
|
-
o[k2] = m[k];
|
|
463
|
-
}));
|
|
464
|
-
var __exportStar = exports2 && exports2.__exportStar || function(m, exports3) {
|
|
465
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports3, p)) __createBinding(exports3, m, p);
|
|
466
|
-
};
|
|
467
|
-
Object.defineProperty(exports2, "__esModule", { value: true });
|
|
468
|
-
__exportStar(require_errors(), exports2);
|
|
469
|
-
__exportStar(require_types(), exports2);
|
|
470
|
-
__exportStar(require_interfaceClient(), exports2);
|
|
471
|
-
}
|
|
472
|
-
});
|
|
473
|
-
|
|
474
|
-
// src/commands/interface/run.ts
|
|
475
|
-
var run_exports = {};
|
|
476
|
-
__export(run_exports, {
|
|
477
|
-
runAutomationCLI: () => runAutomationCLI
|
|
478
|
-
});
|
|
479
|
-
module.exports = __toCommonJS(run_exports);
|
|
480
|
-
var import_commander = require("commander");
|
|
481
|
-
var import_dotenv2 = __toESM(require("dotenv"));
|
|
482
|
-
|
|
483
|
-
// src/common/asyncLocalStorage/index.ts
|
|
484
|
-
var import_node_async_hooks = require("async_hooks");
|
|
485
|
-
var asyncLocalStorage = new import_node_async_hooks.AsyncLocalStorage();
|
|
486
|
-
function runWithContext(contextData, callback, ...args) {
|
|
487
|
-
return asyncLocalStorage.run(contextData, callback, ...args);
|
|
488
|
-
}
|
|
489
|
-
function getExecutionContext() {
|
|
490
|
-
const contextData = asyncLocalStorage.getStore();
|
|
491
|
-
return contextData;
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
// src/runtime/attemptStore.ts
|
|
495
|
-
var attemptStore = Object.freeze({
|
|
496
|
-
get: (key) => {
|
|
497
|
-
const context = getExecutionContext();
|
|
498
|
-
return context?.store ? context.store[key] : void 0;
|
|
499
|
-
},
|
|
500
|
-
set: (key, value) => {
|
|
501
|
-
const context = getExecutionContext();
|
|
502
|
-
if (!context) {
|
|
503
|
-
throw new Error("store.set failed due to an internal error.");
|
|
504
|
-
}
|
|
505
|
-
if (!context.store) {
|
|
506
|
-
context.store = {};
|
|
507
|
-
}
|
|
508
|
-
context.store[key] = value;
|
|
509
|
-
}
|
|
510
|
-
});
|
|
511
|
-
|
|
512
|
-
// src/runtime/persistentStore.ts
|
|
513
|
-
var import_zod = require("zod");
|
|
514
|
-
|
|
515
|
-
// src/common/jwtTokenManager.ts
|
|
516
|
-
var import_cross_fetch = __toESM(require("cross-fetch"));
|
|
517
|
-
var jwt = __toESM(require("jsonwebtoken"));
|
|
518
|
-
var import_neverthrow = require("neverthrow");
|
|
519
|
-
|
|
520
|
-
// src/common/constants.ts
|
|
521
|
-
var WORKSPACE_ID_ENV_VAR_KEY = "INTUNED_WORKSPACE_ID";
|
|
522
|
-
var PROJECT_ID_ENV_VAR_KEY = "INTUNED_PROJECT_ID";
|
|
523
|
-
var API_KEY_ENV_VAR_KEY = "INTUNED_API_KEY";
|
|
524
|
-
var AUTH_TOKEN_ENV_VAR_KEY = "INTUNED_AUTH_TOKEN";
|
|
525
|
-
var API_KEY_HEADER_NAME = "x-api-key";
|
|
526
|
-
var CLI_ENV_VAR_KEY = "INTUNED_CLI";
|
|
527
|
-
|
|
528
|
-
// src/common/jwtTokenManager.ts
|
|
529
|
-
var JwtTokenManager = class {
|
|
530
|
-
constructor(refreshTokenPath) {
|
|
531
|
-
this.refreshTokenPath = refreshTokenPath;
|
|
532
|
-
this._token = void 0;
|
|
533
|
-
}
|
|
534
|
-
get token() {
|
|
535
|
-
return this._token;
|
|
536
|
-
}
|
|
537
|
-
// When the token is set, the schedule for renewal is issued automatically
|
|
538
|
-
// This is currently being set it two places:
|
|
539
|
-
// 1. Whenever the runner starts, initializes it from the environment variable (set whenever the api is run from the authoring IDE)
|
|
540
|
-
// 2. Whenever a published api is called to run (/api/run/*), it is set to the token received in the run request.
|
|
541
|
-
set token(newToken) {
|
|
542
|
-
if (this._token != newToken) {
|
|
543
|
-
this._token = newToken;
|
|
544
|
-
void this.scheduleTokenRefresh();
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
get timeToRefresh() {
|
|
548
|
-
if (!this._token) return;
|
|
549
|
-
const payload = jwt.decode(this._token);
|
|
550
|
-
if (!payload || typeof payload == "string") return;
|
|
551
|
-
const expiry = payload.expiry;
|
|
552
|
-
if (!expiry || typeof expiry !== "number") return;
|
|
553
|
-
const timeWindow = 60 * 1e3;
|
|
554
|
-
const timeToRefresh = expiry - Date.now() - timeWindow;
|
|
555
|
-
return Math.max(timeToRefresh, timeWindow);
|
|
556
|
-
}
|
|
557
|
-
async scheduleTokenRefresh() {
|
|
558
|
-
if (process.env.RUN_ENVIRONMENT?.toLowerCase() !== "authoring") return;
|
|
559
|
-
const timeToRefresh = this.timeToRefresh;
|
|
560
|
-
if (timeToRefresh === void 0) return;
|
|
561
|
-
if (this.tokenRefreshTimeout) clearTimeout(this.tokenRefreshTimeout);
|
|
562
|
-
this.tokenRefreshTimeout = setTimeout(async () => {
|
|
563
|
-
const result = await this.refreshToken();
|
|
564
|
-
if (result && result.isErr()) {
|
|
565
|
-
console.error(`[Internal Error] ${result.error}`);
|
|
566
92
|
return;
|
|
567
93
|
}
|
|
568
|
-
await this.scheduleTokenRefresh();
|
|
569
|
-
}, timeToRefresh);
|
|
570
|
-
}
|
|
571
|
-
async refreshToken() {
|
|
572
|
-
if (process.env.RUN_ENVIRONMENT?.toLowerCase() !== "authoring") return;
|
|
573
|
-
const res = await this.fetchWithToken(
|
|
574
|
-
`${this.backendFunctionsBaseUrl}/${this.refreshTokenPath}`,
|
|
575
|
-
{
|
|
576
|
-
method: "GET"
|
|
577
|
-
}
|
|
578
|
-
);
|
|
579
|
-
if (res.status === 401) {
|
|
580
|
-
return (0, import_neverthrow.err)("Unauthorized");
|
|
581
|
-
}
|
|
582
|
-
const jsonResult = await import_neverthrow.ResultAsync.fromPromise(
|
|
583
|
-
res.json(),
|
|
584
|
-
() => "not json"
|
|
585
|
-
);
|
|
586
|
-
if (jsonResult.isErr()) return;
|
|
587
|
-
const newToken = jsonResult.value.token;
|
|
588
|
-
if (newToken) this._token = newToken;
|
|
589
|
-
}
|
|
590
|
-
async fetchWithToken(...[input, init]) {
|
|
591
|
-
const headers = new Headers(init?.headers);
|
|
592
|
-
const apiKey = process.env[API_KEY_ENV_VAR_KEY];
|
|
593
|
-
if (apiKey) {
|
|
594
|
-
headers.set(API_KEY_HEADER_NAME, apiKey);
|
|
595
|
-
}
|
|
596
|
-
const token = process.env[AUTH_TOKEN_ENV_VAR_KEY];
|
|
597
|
-
if (token) {
|
|
598
|
-
headers.set("Authorization", `Bearer ${token}`);
|
|
599
|
-
}
|
|
600
|
-
if (this.token !== void 0) {
|
|
601
|
-
headers.set("Authorization", `Bearer ${this.token}`);
|
|
602
|
-
}
|
|
603
|
-
const result = await (0, import_cross_fetch.default)(input, {
|
|
604
|
-
...init,
|
|
605
|
-
headers
|
|
606
|
-
});
|
|
607
|
-
if (result.status === 401 && process.env[CLI_ENV_VAR_KEY] === "true") {
|
|
608
|
-
console.warn(
|
|
609
|
-
"Unauthorized backend function call - make sure to provision your project to Intuned to set up the correct API credentials.\nRun 'intuned provision' or see https://docs.intunedhq.com/docs/05-references/cli#provision-project for more information."
|
|
610
|
-
);
|
|
611
|
-
}
|
|
612
|
-
return result;
|
|
613
|
-
}
|
|
614
|
-
get backendFunctionsBaseUrl() {
|
|
615
|
-
try {
|
|
616
|
-
if (!process.env.FUNCTIONS_DOMAIN) {
|
|
617
|
-
throw new Error(
|
|
618
|
-
`Cannot call backend function - FUNCTIONS_DOMAIN not set`
|
|
619
|
-
);
|
|
620
|
-
}
|
|
621
|
-
const domain = process.env.FUNCTIONS_DOMAIN;
|
|
622
|
-
if (!process.env[WORKSPACE_ID_ENV_VAR_KEY]) {
|
|
623
|
-
throw new Error(
|
|
624
|
-
`Cannot call backend function - ${WORKSPACE_ID_ENV_VAR_KEY} not set`
|
|
625
|
-
);
|
|
626
|
-
}
|
|
627
|
-
const workspaceId = process.env[WORKSPACE_ID_ENV_VAR_KEY];
|
|
628
|
-
if (!process.env[PROJECT_ID_ENV_VAR_KEY] && !process.env.INTUNED_INTEGRATION_ID) {
|
|
629
|
-
throw new Error(
|
|
630
|
-
`Cannot call backend function - ${PROJECT_ID_ENV_VAR_KEY} or INTUNED_INTEGRATION_ID not set`
|
|
631
|
-
);
|
|
632
|
-
}
|
|
633
|
-
const projectId = process.env.INTUNED_INTEGRATION_ID ?? process.env[PROJECT_ID_ENV_VAR_KEY];
|
|
634
|
-
return `${domain}/api/${workspaceId}/functions/${projectId}`;
|
|
635
|
-
} catch (e) {
|
|
636
|
-
if (process.env[CLI_ENV_VAR_KEY] === "true") {
|
|
637
|
-
throw new Error(
|
|
638
|
-
`API credentials not set - make sure to provision your project to Intuned to set up the correct API credentials.
|
|
639
|
-
Run 'intuned provision' or see https://docs.intunedhq.com/docs/05-references/cli#provision-project for more information.
|
|
640
|
-
Original error: ${e.message}`
|
|
641
|
-
);
|
|
642
|
-
}
|
|
643
94
|
}
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
const parsedKey = keySchema.parse(key);
|
|
666
|
-
const response = await callBackendFunctionWithToken(
|
|
667
|
-
`kv-store/${parsedKey}`,
|
|
668
|
-
{
|
|
669
|
-
method: "GET"
|
|
670
|
-
}
|
|
671
|
-
);
|
|
672
|
-
const json = await response.json();
|
|
673
|
-
if (!response.ok) {
|
|
674
|
-
throw new Error(json.message);
|
|
675
|
-
}
|
|
676
|
-
return json.value;
|
|
677
|
-
},
|
|
678
|
-
set: async (key, value) => {
|
|
679
|
-
const keyResult = keySchema.parse(key);
|
|
680
|
-
const response = await callBackendFunctionWithToken(
|
|
681
|
-
`kv-store/${keyResult}`,
|
|
682
|
-
{
|
|
683
|
-
method: "PUT",
|
|
684
|
-
body: JSON.stringify(value),
|
|
685
|
-
headers: {
|
|
686
|
-
"Content-Type": "application/json"
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
);
|
|
690
|
-
const json = await response.json();
|
|
691
|
-
if (!response.ok) {
|
|
692
|
-
throw new Error(json.message);
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
});
|
|
696
|
-
|
|
697
|
-
// src/runtime/RunError.ts
|
|
698
|
-
var RunError = class _RunError extends Error {
|
|
699
|
-
constructor(message, options) {
|
|
700
|
-
super(message);
|
|
701
|
-
this.message = message;
|
|
702
|
-
this.name = "USER_GENERATED_ERROR";
|
|
703
|
-
this.options = options ?? {
|
|
704
|
-
retryable: false
|
|
705
|
-
};
|
|
706
|
-
Object.setPrototypeOf(this, _RunError.prototype);
|
|
707
|
-
}
|
|
708
|
-
};
|
|
709
|
-
new RunError("", {});
|
|
710
|
-
|
|
711
|
-
// src/common/extension/intunedExtensionServer.ts
|
|
712
|
-
var import_fastify = __toESM(require("fastify"));
|
|
713
|
-
|
|
714
|
-
// src/common/extension/types.ts
|
|
715
|
-
var import_zod2 = require("zod");
|
|
716
|
-
var captchaTypeSchema = import_zod2.z.enum([
|
|
717
|
-
"aws",
|
|
718
|
-
"cloudflare",
|
|
719
|
-
"customcaptcha",
|
|
720
|
-
"funcaptcha",
|
|
721
|
-
"geetest",
|
|
722
|
-
"hcaptcha",
|
|
723
|
-
"lemincaptcha",
|
|
724
|
-
"recaptcha",
|
|
725
|
-
"textcaptcha"
|
|
726
|
-
]);
|
|
727
|
-
var captchaStatusSchema = import_zod2.z.enum([
|
|
728
|
-
"attached",
|
|
729
|
-
"solving",
|
|
730
|
-
"solved",
|
|
731
|
-
"error",
|
|
732
|
-
"detached"
|
|
733
|
-
]);
|
|
734
|
-
var captchaErrorCodeSchema = import_zod2.z.enum([
|
|
735
|
-
"HIT_LIMIT",
|
|
736
|
-
"MAX_RETRIES",
|
|
737
|
-
"UNEXPECTED_SERVER_RESPONSE",
|
|
738
|
-
"UNEXPECTED_ERROR"
|
|
739
|
-
]);
|
|
740
|
-
var captchaErrorSchema = import_zod2.z.object({
|
|
741
|
-
code: captchaErrorCodeSchema,
|
|
742
|
-
error: import_zod2.z.unknown().optional()
|
|
743
|
-
});
|
|
744
|
-
var captchaBaseSchema = import_zod2.z.object({
|
|
745
|
-
id: import_zod2.z.string().min(1),
|
|
746
|
-
tabId: import_zod2.z.number().int().positive(),
|
|
747
|
-
type: captchaTypeSchema,
|
|
748
|
-
retryCount: import_zod2.z.number().int().nonnegative().optional()
|
|
749
|
-
});
|
|
750
|
-
var captchaNonErrorSchema = captchaBaseSchema.extend({
|
|
751
|
-
status: captchaStatusSchema.exclude(["error"])
|
|
752
|
-
});
|
|
753
|
-
var captchaErrorStatusSchema = captchaBaseSchema.extend({
|
|
754
|
-
status: import_zod2.z.literal("error"),
|
|
755
|
-
error: captchaErrorSchema
|
|
756
|
-
});
|
|
757
|
-
var captchaSchema = import_zod2.z.discriminatedUnion("status", [
|
|
758
|
-
captchaBaseSchema.extend({
|
|
759
|
-
status: import_zod2.z.literal("attached")
|
|
760
|
-
}),
|
|
761
|
-
captchaBaseSchema.extend({
|
|
762
|
-
status: import_zod2.z.literal("solving")
|
|
763
|
-
}),
|
|
764
|
-
captchaBaseSchema.extend({
|
|
765
|
-
status: import_zod2.z.literal("solved")
|
|
766
|
-
}),
|
|
767
|
-
captchaBaseSchema.extend({
|
|
768
|
-
status: import_zod2.z.literal("detached")
|
|
769
|
-
}),
|
|
770
|
-
captchaErrorStatusSchema
|
|
771
|
-
]);
|
|
772
|
-
|
|
773
|
-
// src/commands/common/utils/fileUtils.ts
|
|
774
|
-
var path = __toESM(require("path"));
|
|
775
|
-
var fs = __toESM(require("fs-extra"));
|
|
776
|
-
var import_dotenv = __toESM(require("dotenv"));
|
|
777
|
-
import_dotenv.default.config();
|
|
778
|
-
function getFullPathInProject(...paths) {
|
|
779
|
-
return path.resolve(process.cwd(), process.env.ROOT || "./", ...paths);
|
|
780
|
-
}
|
|
781
|
-
|
|
782
|
-
// src/common/settingsSchema.ts
|
|
783
|
-
var z3 = __toESM(require("zod"));
|
|
784
|
-
var baseCaptchaSchema = z3.object({
|
|
785
|
-
enabled: z3.boolean()
|
|
786
|
-
});
|
|
787
|
-
var customCaptchaSchema = baseCaptchaSchema.extend({
|
|
788
|
-
imageLocators: z3.array(z3.string()).min(1, "At least one image locator is required"),
|
|
789
|
-
submitLocators: z3.array(z3.string()).min(1, "At least one submit locator is required"),
|
|
790
|
-
inputLocators: z3.array(z3.string()).min(1, "At least one input locator is required")
|
|
791
|
-
});
|
|
792
|
-
var textCaptchaSchema = baseCaptchaSchema.extend({
|
|
793
|
-
labelLocators: z3.array(z3.string()).min(1, "At least one image locator is required"),
|
|
794
|
-
submitLocators: z3.array(z3.string()).min(1, "At least one submit locator is required"),
|
|
795
|
-
inputLocators: z3.array(z3.string()).min(1, "At least one input locator is required")
|
|
796
|
-
});
|
|
797
|
-
var captchaSolverSolveSettingsSchema = z3.object({
|
|
798
|
-
autoSolve: z3.boolean().default(true),
|
|
799
|
-
solveDelay: z3.number().min(0).default(2e3),
|
|
800
|
-
maxRetries: z3.number().min(0).default(3),
|
|
801
|
-
timeout: z3.number().min(0).default(3e4)
|
|
802
|
-
});
|
|
803
|
-
var captchaSolverSettingsSchema = z3.object({
|
|
804
|
-
enabled: z3.boolean().default(false),
|
|
805
|
-
port: z3.number().int().min(1).max(65535).optional(),
|
|
806
|
-
cloudflare: baseCaptchaSchema.optional(),
|
|
807
|
-
googleRecaptchaV2: baseCaptchaSchema.optional(),
|
|
808
|
-
googleRecaptchaV3: baseCaptchaSchema.optional(),
|
|
809
|
-
awscaptcha: baseCaptchaSchema.optional(),
|
|
810
|
-
hcaptcha: baseCaptchaSchema.optional(),
|
|
811
|
-
funcaptcha: baseCaptchaSchema.optional(),
|
|
812
|
-
geetest: baseCaptchaSchema.optional(),
|
|
813
|
-
lemin: baseCaptchaSchema.optional(),
|
|
814
|
-
customCaptcha: customCaptchaSchema.optional(),
|
|
815
|
-
text: textCaptchaSchema.optional(),
|
|
816
|
-
settings: captchaSolverSolveSettingsSchema.default(
|
|
817
|
-
captchaSolverSolveSettingsSchema.parse({})
|
|
818
|
-
)
|
|
819
|
-
}).default({});
|
|
820
|
-
var authSessionsSchema = z3.object({
|
|
821
|
-
enabled: z3.boolean()
|
|
822
|
-
}).optional().default({
|
|
823
|
-
enabled: false
|
|
824
|
-
});
|
|
825
|
-
var stealthModeSchema = z3.object({
|
|
826
|
-
enabled: z3.boolean()
|
|
827
|
-
}).optional().default({
|
|
828
|
-
enabled: false
|
|
829
|
-
});
|
|
830
|
-
var settingsSchema = z3.object({
|
|
831
|
-
authSessions: authSessionsSchema,
|
|
832
|
-
stealthMode: stealthModeSchema,
|
|
833
|
-
captchaSolver: captchaSolverSettingsSchema.optional()
|
|
834
|
-
});
|
|
835
|
-
|
|
836
|
-
// src/commands/common/utils/settings.ts
|
|
837
|
-
var import_promises = require("fs/promises");
|
|
838
|
-
async function getSettings() {
|
|
839
|
-
const settingsFilePath = getFullPathInProject("Intuned.json");
|
|
840
|
-
const settings = await (0, import_promises.readFile)(settingsFilePath, { encoding: "utf-8" });
|
|
841
|
-
if (settings) {
|
|
842
|
-
const parsed = settingsSchema.safeParse(JSON.parse(settings));
|
|
843
|
-
if (parsed.success) {
|
|
844
|
-
return parsed.data;
|
|
845
|
-
} else {
|
|
846
|
-
throw new Error(parsed.error.message);
|
|
847
|
-
}
|
|
848
|
-
}
|
|
849
|
-
return {
|
|
850
|
-
authSessions: { enabled: false },
|
|
851
|
-
stealthMode: { enabled: false }
|
|
852
|
-
};
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
// src/common/extension/extensionsHelpers.ts
|
|
856
|
-
var import_path = __toESM(require("path"));
|
|
857
|
-
var import_promises2 = require("fs/promises");
|
|
858
|
-
var import_portfinder = require("portfinder");
|
|
859
|
-
var INTUNED_WORKER = "intunedWorker.js";
|
|
860
|
-
var INTUNED_EXTENSION_SETTINGS_FILE_NAME = "intunedSettings.json";
|
|
861
|
-
var captchaSolverSettings = null;
|
|
862
|
-
async function getIntunedCaptchaExtensionPort() {
|
|
863
|
-
if (process.env.INTUNED_CAPTCHA_EXTENSION_PORT) {
|
|
864
|
-
return parseInt(process.env.INTUNED_CAPTCHA_EXTENSION_PORT, 10);
|
|
865
|
-
}
|
|
866
|
-
return await (0, import_portfinder.getPort)({});
|
|
867
|
-
}
|
|
868
|
-
async function resolveCaptchaSolverSettings(input) {
|
|
869
|
-
const parsed = captchaSolverSettingsSchema.parse(input || {});
|
|
870
|
-
if (!parsed.port) {
|
|
871
|
-
parsed.port = await getIntunedCaptchaExtensionPort();
|
|
872
|
-
}
|
|
873
|
-
return parsed;
|
|
874
|
-
}
|
|
875
|
-
function isIntunedExtensionLoaded() {
|
|
876
|
-
return !!getIntunedExtensionPath();
|
|
877
|
-
}
|
|
878
|
-
function buildExtensionsList() {
|
|
879
|
-
const extensionsList = [];
|
|
880
|
-
if (isIntunedExtensionLoaded()) {
|
|
881
|
-
const intunedExtensionPath = getIntunedExtensionPath();
|
|
882
|
-
extensionsList.push(intunedExtensionPath);
|
|
883
|
-
}
|
|
884
|
-
return extensionsList;
|
|
885
|
-
}
|
|
886
|
-
function getIntunedExtensionPath() {
|
|
887
|
-
return process.env.INTUNED_EXTENSION_PATH;
|
|
888
|
-
}
|
|
889
|
-
async function isIntunedExtensionEnabled() {
|
|
890
|
-
const path3 = getIntunedExtensionPath();
|
|
891
|
-
if (!path3) {
|
|
892
|
-
return false;
|
|
893
|
-
}
|
|
894
|
-
const captchaSolverSettings2 = await getIntunedCaptchaSolverSettings();
|
|
895
|
-
return captchaSolverSettings2.enabled;
|
|
896
|
-
}
|
|
897
|
-
async function getIntunedExtensionWorker(context) {
|
|
898
|
-
if (!await isIntunedExtensionEnabled()) {
|
|
899
|
-
return null;
|
|
900
|
-
}
|
|
901
|
-
let attemptCount = 0;
|
|
902
|
-
while (attemptCount < 4) {
|
|
903
|
-
const intunedServiceWorker = context.serviceWorkers().find((serviceWorker) => serviceWorker.url().includes(INTUNED_WORKER));
|
|
904
|
-
if (intunedServiceWorker) {
|
|
905
|
-
return intunedServiceWorker;
|
|
906
|
-
}
|
|
907
|
-
try {
|
|
908
|
-
await context.waitForEvent("serviceworker", { timeout: 3e3 });
|
|
909
|
-
} catch (err7) {
|
|
910
|
-
console.log(`Error accessing service workers (attempt ${attemptCount})`);
|
|
911
|
-
}
|
|
912
|
-
attemptCount++;
|
|
913
|
-
}
|
|
914
|
-
console.error("Failed to get intuned worker after 5 attmepts");
|
|
915
|
-
return null;
|
|
916
|
-
}
|
|
917
|
-
async function getIntunedExtensionSettings(captchaSolverSettings2) {
|
|
918
|
-
const [domain, workspaceId, projectId] = [
|
|
919
|
-
process.env.FUNCTIONS_DOMAIN,
|
|
920
|
-
process.env[WORKSPACE_ID_ENV_VAR_KEY],
|
|
921
|
-
process.env.INTUNED_INTEGRATION_ID ?? process.env[PROJECT_ID_ENV_VAR_KEY]
|
|
922
|
-
];
|
|
923
|
-
if (!domain || !workspaceId || !projectId) {
|
|
924
|
-
const missingEnvVars = [
|
|
925
|
-
domain && "FUNCTIONS_DOMAIN",
|
|
926
|
-
workspaceId && WORKSPACE_ID_ENV_VAR_KEY,
|
|
927
|
-
projectId && `INTUNED_INTEGRATION_ID OR ${PROJECT_ID_ENV_VAR_KEY}`
|
|
928
|
-
];
|
|
929
|
-
throw new Error(
|
|
930
|
-
`Missing required environment variables: ${missingEnvVars}`
|
|
931
|
-
);
|
|
932
|
-
}
|
|
933
|
-
const authentication = (() => {
|
|
934
|
-
if (process.env.INTUNED_API_KEY) {
|
|
935
|
-
return { type: "apiKey", apiKey: process.env.INTUNED_API_KEY };
|
|
936
|
-
}
|
|
937
|
-
if (process.env.INTUNED_BASIC_AUTH_USERNAME && process.env.INTUNED_BASIC_AUTH_PASSWORD) {
|
|
938
|
-
const credentials = `${process.env.INTUNED_BASIC_AUTH_USERNAME}:${process.env.INTUNED_BASIC_AUTH_PASSWORD}`;
|
|
939
|
-
const token = Buffer.from(credentials, "utf-8").toString("base64");
|
|
940
|
-
return { type: "basic", token };
|
|
941
|
-
}
|
|
942
|
-
return {
|
|
943
|
-
type: "bearer",
|
|
944
|
-
token: backendFunctionsTokenManager.token
|
|
945
|
-
};
|
|
946
|
-
})();
|
|
947
|
-
const baseUrl = process.env.INTUNED_API_BASE_URL ?? domain;
|
|
948
|
-
return {
|
|
949
|
-
...captchaSolverSettings2,
|
|
950
|
-
workspaceId,
|
|
951
|
-
projectId,
|
|
952
|
-
baseUrl,
|
|
953
|
-
authentication
|
|
954
|
-
};
|
|
955
|
-
}
|
|
956
|
-
async function getIntunedCaptchaSolverSettings() {
|
|
957
|
-
if (captchaSolverSettings) {
|
|
958
|
-
return captchaSolverSettings;
|
|
959
|
-
}
|
|
960
|
-
const settings = await getSettings();
|
|
961
|
-
captchaSolverSettings = await resolveCaptchaSolverSettings(
|
|
962
|
-
settings.captchaSolver
|
|
963
|
-
);
|
|
964
|
-
return captchaSolverSettings;
|
|
965
|
-
}
|
|
966
|
-
async function setupIntunedExtension() {
|
|
967
|
-
if (!await isIntunedExtensionEnabled()) {
|
|
968
|
-
return;
|
|
969
|
-
}
|
|
970
|
-
const intunedExtensionPath = getIntunedExtensionPath();
|
|
971
|
-
const intunedExtensionSettingsPath = import_path.default.join(
|
|
972
|
-
intunedExtensionPath,
|
|
973
|
-
INTUNED_EXTENSION_SETTINGS_FILE_NAME
|
|
974
|
-
);
|
|
975
|
-
const captchaSolverSettings2 = await getIntunedCaptchaSolverSettings();
|
|
976
|
-
await setupIntunedExtensionServer(captchaSolverSettings2);
|
|
977
|
-
const captchaSolverSettingsWithRunContext = await getIntunedExtensionSettings(
|
|
978
|
-
captchaSolverSettings2
|
|
979
|
-
);
|
|
980
|
-
await (0, import_promises2.writeFile)(
|
|
981
|
-
intunedExtensionSettingsPath,
|
|
982
|
-
JSON.stringify(captchaSolverSettingsWithRunContext)
|
|
983
|
-
);
|
|
984
|
-
}
|
|
985
|
-
|
|
986
|
-
// src/common/extension/intunedExtensionServer.ts
|
|
987
|
-
var TabCaptchaState = class {
|
|
988
|
-
constructor(tabId) {
|
|
989
|
-
this.tabId = tabId;
|
|
990
|
-
this.captchasById = /* @__PURE__ */ new Map();
|
|
991
|
-
this.subscribers = new Array();
|
|
992
|
-
}
|
|
993
|
-
subscribe(handler) {
|
|
994
|
-
this.subscribers.push(handler);
|
|
995
|
-
}
|
|
996
|
-
unsubscribe(handler, status) {
|
|
997
|
-
const index = this.subscribers.findIndex(
|
|
998
|
-
(subscriber) => subscriber.handler === handler && (subscriber.status === status || !status)
|
|
999
|
-
);
|
|
1000
|
-
if (index !== -1) {
|
|
1001
|
-
this.subscribers.splice(index, 1);
|
|
1002
|
-
}
|
|
1003
|
-
}
|
|
1004
|
-
getCaptchas() {
|
|
1005
|
-
return [...this.captchasById.values()];
|
|
1006
|
-
}
|
|
1007
|
-
async upsertCaptcha(captcha) {
|
|
1008
|
-
this.captchasById.set(captcha.id, captcha);
|
|
1009
|
-
for (const subscriber of this.subscribers) {
|
|
1010
|
-
if (!subscriber.status || subscriber.status === captcha.status) {
|
|
1011
|
-
await subscriber.handler(captcha);
|
|
1012
|
-
}
|
|
1013
|
-
}
|
|
1014
|
-
}
|
|
1015
|
-
};
|
|
1016
|
-
var ExtensionServer = class {
|
|
1017
|
-
constructor() {
|
|
1018
|
-
this.app = null;
|
|
1019
|
-
this.tabs = /* @__PURE__ */ new Map();
|
|
1020
|
-
}
|
|
1021
|
-
getOrCreateTab(tabId) {
|
|
1022
|
-
const existing = this.tabs.get(tabId);
|
|
1023
|
-
if (existing) return existing;
|
|
1024
|
-
const created = new TabCaptchaState(tabId);
|
|
1025
|
-
this.tabs.set(tabId, created);
|
|
1026
|
-
return created;
|
|
1027
|
-
}
|
|
1028
|
-
async handleUpsertCaptcha(captcha) {
|
|
1029
|
-
const tab = this.getOrCreateTab(captcha.tabId);
|
|
1030
|
-
await tab.upsertCaptcha(captcha);
|
|
1031
|
-
}
|
|
1032
|
-
async start({
|
|
1033
|
-
port,
|
|
1034
|
-
host = "0.0.0.0"
|
|
1035
|
-
}) {
|
|
1036
|
-
if (this.app) {
|
|
1037
|
-
return;
|
|
1038
|
-
}
|
|
1039
|
-
this.app = (0, import_fastify.default)({
|
|
1040
|
-
logger: false,
|
|
1041
|
-
bodyLimit: 1e6
|
|
1042
|
-
});
|
|
1043
|
-
this.app.post("/state", async (request, reply) => {
|
|
1044
|
-
try {
|
|
1045
|
-
const result = captchaSchema.safeParse(request.body);
|
|
1046
|
-
if (!result.success) {
|
|
1047
|
-
return reply.code(400).send({
|
|
1048
|
-
error: "Invalid captcha payload",
|
|
1049
|
-
details: result.error.flatten().fieldErrors
|
|
95
|
+
let receiveMessagesPromise = receiveMessages();
|
|
96
|
+
while (true) {
|
|
97
|
+
const messageOrResult = await Promise.race([receiveMessagesPromise.then(message => ({
|
|
98
|
+
type: "message",
|
|
99
|
+
message
|
|
100
|
+
})), ...(resultPromise ? [resultPromise.then(result => ({
|
|
101
|
+
type: "result",
|
|
102
|
+
result
|
|
103
|
+
}))] : [])]);
|
|
104
|
+
if (messageOrResult.type === "message") {
|
|
105
|
+
const {
|
|
106
|
+
message
|
|
107
|
+
} = messageOrResult;
|
|
108
|
+
if (message === undefined) {
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
if (message.type === "error") {
|
|
112
|
+
client.sendJSON({
|
|
113
|
+
type: "done",
|
|
114
|
+
result: message.error.json,
|
|
115
|
+
success: false
|
|
1050
116
|
});
|
|
117
|
+
break;
|
|
1051
118
|
}
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
if (!this.app) return;
|
|
1072
|
-
const toClose = this.app;
|
|
1073
|
-
this.app = null;
|
|
1074
|
-
await toClose.close();
|
|
1075
|
-
}
|
|
1076
|
-
async getCaptchas(page, status) {
|
|
1077
|
-
const tabId = await getTabId(page);
|
|
1078
|
-
const tab = this.tabs.get(tabId);
|
|
1079
|
-
if (!tab) return [];
|
|
1080
|
-
const captchas = tab.getCaptchas();
|
|
1081
|
-
if (!status) return captchas;
|
|
1082
|
-
return captchas.filter((c) => c.status === status && c.tabId === tabId);
|
|
1083
|
-
}
|
|
1084
|
-
async subscribe(page, handler, status) {
|
|
1085
|
-
const tabId = await getTabId(page);
|
|
1086
|
-
const tab = this.getOrCreateTab(tabId);
|
|
1087
|
-
tab.subscribe({ handler, status });
|
|
1088
|
-
}
|
|
1089
|
-
async unsubscribe(page, handler, status) {
|
|
1090
|
-
const tabId = await getTabId(page);
|
|
1091
|
-
const tab = this.tabs.get(tabId);
|
|
1092
|
-
if (!tab) return;
|
|
1093
|
-
tab.unsubscribe(handler, status);
|
|
1094
|
-
}
|
|
1095
|
-
removeTab(tabId) {
|
|
1096
|
-
this.tabs.delete(tabId);
|
|
1097
|
-
}
|
|
1098
|
-
async getTabId(page) {
|
|
1099
|
-
return await getTabId(page);
|
|
1100
|
-
}
|
|
1101
|
-
};
|
|
1102
|
-
var extensionServerSingleton = null;
|
|
1103
|
-
async function setupIntunedExtensionServer(captchaSolverSettings2) {
|
|
1104
|
-
if (!captchaSolverSettings2) {
|
|
1105
|
-
captchaSolverSettings2 = await resolveCaptchaSolverSettings();
|
|
1106
|
-
}
|
|
1107
|
-
if (!extensionServerSingleton) {
|
|
1108
|
-
extensionServerSingleton = new ExtensionServer();
|
|
1109
|
-
}
|
|
1110
|
-
await extensionServerSingleton.start({
|
|
1111
|
-
port: captchaSolverSettings2.port ?? 9e3
|
|
1112
|
-
});
|
|
1113
|
-
}
|
|
1114
|
-
async function cleanIntunedExtensionServer() {
|
|
1115
|
-
if (extensionServerSingleton) {
|
|
1116
|
-
await extensionServerSingleton.stop();
|
|
1117
|
-
extensionServerSingleton = null;
|
|
1118
|
-
}
|
|
1119
|
-
}
|
|
1120
|
-
var TAB_ID_CACHE_KEY = "__INTUNED_CACHED_TAB_ID__";
|
|
1121
|
-
async function getTabId(page) {
|
|
1122
|
-
const cached = page[TAB_ID_CACHE_KEY];
|
|
1123
|
-
if (cached !== void 0 && typeof cached === "number") {
|
|
1124
|
-
return cached;
|
|
1125
|
-
}
|
|
1126
|
-
let tabId;
|
|
1127
|
-
try {
|
|
1128
|
-
tabId = await page.evaluate("window.__INTUNED_TAB_ID__", { timeout: 100 });
|
|
1129
|
-
} catch {
|
|
1130
|
-
await page.waitForFunction("window.__INTUNED_TAB_ID__ !== undefined", {
|
|
1131
|
-
timeout: 15e3
|
|
1132
|
-
});
|
|
1133
|
-
tabId = await page.evaluate("window.__INTUNED_TAB_ID__");
|
|
1134
|
-
}
|
|
1135
|
-
const numericTabId = Number(tabId);
|
|
1136
|
-
page[TAB_ID_CACHE_KEY] = numericTabId;
|
|
1137
|
-
return numericTabId;
|
|
1138
|
-
}
|
|
1139
|
-
|
|
1140
|
-
// src/runtime/downloadDirectory.ts
|
|
1141
|
-
var import_fs_extra = require("fs-extra");
|
|
1142
|
-
function getDownloadDirectoryPath() {
|
|
1143
|
-
const context = getExecutionContext();
|
|
1144
|
-
if (!context) {
|
|
1145
|
-
throw new Error("ExecutionContext not found");
|
|
1146
|
-
}
|
|
1147
|
-
const path3 = `/tmp/downloads/${context.runId}`;
|
|
1148
|
-
(0, import_fs_extra.ensureDirSync)(path3, {
|
|
1149
|
-
mode: 1533
|
|
1150
|
-
});
|
|
1151
|
-
return path3;
|
|
1152
|
-
}
|
|
1153
|
-
|
|
1154
|
-
// src/common/runApi/index.ts
|
|
1155
|
-
var fs4 = __toESM(require("fs-extra"));
|
|
1156
|
-
var import_fs_extra3 = require("fs-extra");
|
|
1157
|
-
var import_neverthrow6 = require("neverthrow");
|
|
1158
|
-
|
|
1159
|
-
// src/common/contextStorageStateHelpers.ts
|
|
1160
|
-
async function setStorageState(context, state) {
|
|
1161
|
-
if ("cookies" in state && state.cookies) {
|
|
1162
|
-
await context.addCookies(state.cookies);
|
|
1163
|
-
}
|
|
1164
|
-
const page = await context.newPage();
|
|
1165
|
-
if ("origins" in state && state.origins) {
|
|
1166
|
-
for (const originData of state.origins || []) {
|
|
1167
|
-
const origin = originData.origin;
|
|
1168
|
-
await page.route(
|
|
1169
|
-
`${origin}/*`,
|
|
1170
|
-
(route) => route.fulfill({
|
|
1171
|
-
body: "<html><head><title>Set Storage</title></head><body><h1>Set Storage</h1></body></html>",
|
|
1172
|
-
contentType: "text/html",
|
|
1173
|
-
status: 200
|
|
1174
|
-
})
|
|
1175
|
-
);
|
|
1176
|
-
try {
|
|
1177
|
-
await page.goto(origin);
|
|
1178
|
-
for (const item of originData.localStorage) {
|
|
1179
|
-
await page.evaluate(
|
|
1180
|
-
([key, value]) => {
|
|
1181
|
-
window.localStorage.setItem(key, value);
|
|
1182
|
-
},
|
|
1183
|
-
[item.name, item.value]
|
|
1184
|
-
);
|
|
1185
|
-
}
|
|
1186
|
-
} finally {
|
|
1187
|
-
await page.unroute(`${origin}/*`);
|
|
1188
|
-
}
|
|
1189
|
-
}
|
|
1190
|
-
}
|
|
1191
|
-
if ("sessionStorage" in state && state.sessionStorage) {
|
|
1192
|
-
await context.addInitScript((storage) => {
|
|
1193
|
-
for (const { origin, sessionStorage } of storage) {
|
|
1194
|
-
if (window.location.origin === origin) {
|
|
1195
|
-
for (const item of sessionStorage) {
|
|
1196
|
-
const value = window.sessionStorage.getItem(item.name);
|
|
1197
|
-
if (!value) {
|
|
1198
|
-
window.sessionStorage.setItem(item.name, item.value);
|
|
1199
|
-
}
|
|
1200
|
-
}
|
|
1201
|
-
}
|
|
1202
|
-
}
|
|
1203
|
-
}, state.sessionStorage);
|
|
1204
|
-
}
|
|
1205
|
-
await page.close();
|
|
1206
|
-
}
|
|
1207
|
-
async function getStorageState(context) {
|
|
1208
|
-
const result = {
|
|
1209
|
-
cookies: [],
|
|
1210
|
-
origins: []
|
|
1211
|
-
};
|
|
1212
|
-
const storageState = await context.storageState();
|
|
1213
|
-
result.cookies = storageState.cookies;
|
|
1214
|
-
result.origins = storageState.origins;
|
|
1215
|
-
const sessionDataList = [];
|
|
1216
|
-
const pages = await context.pages();
|
|
1217
|
-
for (const page of pages) {
|
|
1218
|
-
if (page.isClosed()) continue;
|
|
1219
|
-
try {
|
|
1220
|
-
const sessionData = await page.evaluate(() => {
|
|
1221
|
-
const items = { ...window.sessionStorage };
|
|
1222
|
-
return {
|
|
1223
|
-
origin: window.location.origin,
|
|
1224
|
-
sessionStorage: Object.entries(items).map(([name, value]) => ({
|
|
1225
|
-
name,
|
|
1226
|
-
value
|
|
1227
|
-
}))
|
|
1228
|
-
};
|
|
1229
|
-
});
|
|
1230
|
-
sessionDataList.push(sessionData);
|
|
1231
|
-
} catch (error) {
|
|
1232
|
-
console.error("Error getting sessionStorage:", error);
|
|
1233
|
-
}
|
|
1234
|
-
}
|
|
1235
|
-
result["sessionStorage"] = sessionDataList;
|
|
1236
|
-
return result;
|
|
1237
|
-
}
|
|
1238
|
-
|
|
1239
|
-
// src/common/playwrightContext.ts
|
|
1240
|
-
var fs3 = __toESM(require("fs-extra"));
|
|
1241
|
-
var import_neverthrow5 = require("neverthrow");
|
|
1242
|
-
|
|
1243
|
-
// src/common/setupContextHook.ts
|
|
1244
|
-
var import_neverthrow3 = require("neverthrow");
|
|
1245
|
-
|
|
1246
|
-
// src/common/runApi/importUsingImportFunction.ts
|
|
1247
|
-
var import_neverthrow2 = require("neverthrow");
|
|
1248
|
-
var import_runtime_interface = __toESM(require_dist());
|
|
1249
|
-
async function importUsingImportFunction({
|
|
1250
|
-
path: path3,
|
|
1251
|
-
allowGenerators = true,
|
|
1252
|
-
importFunction
|
|
1253
|
-
}) {
|
|
1254
|
-
try {
|
|
1255
|
-
const importedResult = await importFunction(path3);
|
|
1256
|
-
if (importedResult.isErr()) {
|
|
1257
|
-
if (importedResult.error.type === "not_found") {
|
|
1258
|
-
return (0, import_neverthrow2.err)(new import_runtime_interface.ApiNotFoundError(path3));
|
|
1259
|
-
}
|
|
1260
|
-
return (0, import_neverthrow2.err)(new import_runtime_interface.AutomationError(importedResult.error.error));
|
|
1261
|
-
}
|
|
1262
|
-
const imported = importedResult.value;
|
|
1263
|
-
if (!imported || !imported.default || !imported.default.constructor) {
|
|
1264
|
-
return (0, import_neverthrow2.err)(new import_runtime_interface.InvalidApiError(`${path3} does not have a default export`));
|
|
1265
|
-
}
|
|
1266
|
-
if (imported.default.constructor.name === "AsyncGeneratorFunction") {
|
|
1267
|
-
if (!allowGenerators) {
|
|
1268
|
-
return (0, import_neverthrow2.err)(
|
|
1269
|
-
new import_runtime_interface.InvalidApiError(
|
|
1270
|
-
`${path3} default export must be an async function`
|
|
1271
|
-
)
|
|
1272
|
-
);
|
|
1273
|
-
}
|
|
1274
|
-
return (0, import_neverthrow2.ok)(async (...args) => {
|
|
1275
|
-
const generator = imported.default(
|
|
1276
|
-
...args
|
|
1277
|
-
);
|
|
1278
|
-
const result = await generator.next();
|
|
1279
|
-
if (!result.done) {
|
|
1280
|
-
throw new Error("Yield is not supported");
|
|
1281
|
-
}
|
|
1282
|
-
return result.value;
|
|
1283
|
-
});
|
|
1284
|
-
}
|
|
1285
|
-
if (imported.default.constructor.name === "AsyncFunction") {
|
|
1286
|
-
return (0, import_neverthrow2.ok)(imported.default);
|
|
1287
|
-
}
|
|
1288
|
-
return (0, import_neverthrow2.err)(
|
|
1289
|
-
new import_runtime_interface.InvalidApiError(`${path3} default export must be an async function`)
|
|
1290
|
-
);
|
|
1291
|
-
} catch (error) {
|
|
1292
|
-
return (0, import_neverthrow2.err)(new import_runtime_interface.AutomationError(error));
|
|
1293
|
-
}
|
|
1294
|
-
}
|
|
1295
|
-
|
|
1296
|
-
// src/common/setupContextHook.ts
|
|
1297
|
-
var import_runtime_interface2 = __toESM(require_dist());
|
|
1298
|
-
var setupContextHookPath = "hooks/setupContext";
|
|
1299
|
-
async function loadSetupContextHook({
|
|
1300
|
-
importFunction
|
|
1301
|
-
}) {
|
|
1302
|
-
const importedFunctionResult = await importUsingImportFunction({
|
|
1303
|
-
path: setupContextHookPath,
|
|
1304
|
-
importFunction
|
|
1305
|
-
});
|
|
1306
|
-
if (importedFunctionResult.isErr() && importedFunctionResult.error instanceof import_runtime_interface2.ApiNotFoundError) {
|
|
1307
|
-
return (0, import_neverthrow3.ok)(null);
|
|
1308
|
-
}
|
|
1309
|
-
return importedFunctionResult;
|
|
1310
|
-
}
|
|
1311
|
-
|
|
1312
|
-
// src/common/playwrightContext.ts
|
|
1313
|
-
var import_portfinder2 = require("portfinder");
|
|
1314
|
-
|
|
1315
|
-
// src/common/launchBrowser.ts
|
|
1316
|
-
var playwright = __toESM(require("playwright"));
|
|
1317
|
-
var import_fs_extra2 = require("fs-extra");
|
|
1318
|
-
var import_path2 = require("path");
|
|
1319
|
-
var fs2 = __toESM(require("fs-extra"));
|
|
1320
|
-
var import_wait_on = __toESM(require("wait-on"));
|
|
1321
|
-
var import_child_process = require("child_process");
|
|
1322
|
-
var import_util = require("util");
|
|
1323
|
-
var import_neverthrow4 = require("neverthrow");
|
|
1324
|
-
var import_zod3 = require("zod");
|
|
1325
|
-
var execAsync = (0, import_util.promisify)(import_child_process.exec);
|
|
1326
|
-
async function createUserDirWithPreferences() {
|
|
1327
|
-
const playwrightTempDir = await (0, import_fs_extra2.mkdtemp)("/tmp/pw-");
|
|
1328
|
-
const userDir = (0, import_path2.join)(playwrightTempDir, "userdir");
|
|
1329
|
-
const defaultDir = (0, import_path2.join)(userDir, "Default");
|
|
1330
|
-
await (0, import_fs_extra2.mkdir)(defaultDir, {
|
|
1331
|
-
recursive: true
|
|
1332
|
-
});
|
|
1333
|
-
const preferences = {
|
|
1334
|
-
plugins: {
|
|
1335
|
-
always_open_pdf_externally: true
|
|
1336
|
-
}
|
|
1337
|
-
};
|
|
1338
|
-
await (0, import_fs_extra2.writeFile)((0, import_path2.join)(defaultDir, "Preferences"), JSON.stringify(preferences));
|
|
1339
|
-
return userDir;
|
|
1340
|
-
}
|
|
1341
|
-
async function launchChromium(options) {
|
|
1342
|
-
if ("cdpAddress" in options) {
|
|
1343
|
-
if (await isIntunedExtensionEnabled()) {
|
|
1344
|
-
await setupIntunedExtensionServer();
|
|
1345
|
-
}
|
|
1346
|
-
const browser = await playwright.chromium.connectOverCDP(
|
|
1347
|
-
options.cdpAddress
|
|
1348
|
-
);
|
|
1349
|
-
if (browser.contexts().length === 0) {
|
|
1350
|
-
throw new Error("No browser contexts found in the connected browser");
|
|
1351
|
-
}
|
|
1352
|
-
const context2 = browser.contexts()[0];
|
|
1353
|
-
let page2 = context2.pages().at(0) ?? await context2.newPage();
|
|
1354
|
-
const targetId = options.cdpTargetId;
|
|
1355
|
-
if (targetId) {
|
|
1356
|
-
for (const p of context2.pages()) {
|
|
1357
|
-
let cdp = null;
|
|
1358
|
-
try {
|
|
1359
|
-
cdp = await context2.newCDPSession(p);
|
|
1360
|
-
const result = await cdp.send("Target.getTargetInfo");
|
|
1361
|
-
if (result.targetInfo.targetId === targetId) {
|
|
1362
|
-
page2 = p;
|
|
1363
|
-
break;
|
|
1364
|
-
}
|
|
1365
|
-
} catch (error) {
|
|
1366
|
-
} finally {
|
|
1367
|
-
await cdp?.detach();
|
|
1368
|
-
}
|
|
1369
|
-
}
|
|
1370
|
-
}
|
|
1371
|
-
return { page: page2, context: context2 };
|
|
1372
|
-
}
|
|
1373
|
-
const { headless, appModeInitialUrl, cdpPort, proxy, downloadsPath } = options;
|
|
1374
|
-
let { executablePath } = options;
|
|
1375
|
-
const defaultArgsToIgnore = [
|
|
1376
|
-
"--disable-extensions",
|
|
1377
|
-
"--disable-component-extensions-with-background-pages",
|
|
1378
|
-
"--disable-background-networking",
|
|
1379
|
-
"--disable-backgrounding-occluded-windows",
|
|
1380
|
-
"--disable-background-timer-throttling"
|
|
1381
|
-
];
|
|
1382
|
-
const extraArgs = [];
|
|
1383
|
-
const userDataDir = await createUserDirWithPreferences();
|
|
1384
|
-
if (isIntunedExtensionLoaded()) {
|
|
1385
|
-
const extensionsList = buildExtensionsList();
|
|
1386
|
-
const extensions = extensionsList.join(",");
|
|
1387
|
-
extraArgs.push(`--disable-extensions-except=${extensions}`);
|
|
1388
|
-
extraArgs.push(`--load-extension=${extensions}`);
|
|
1389
|
-
}
|
|
1390
|
-
if (await isIntunedExtensionEnabled()) {
|
|
1391
|
-
await setupIntunedExtension();
|
|
1392
|
-
if (proxy) {
|
|
1393
|
-
extraArgs.push('--proxy-bypass-list="<-loopback>"');
|
|
1394
|
-
}
|
|
1395
|
-
}
|
|
1396
|
-
if (cdpPort) {
|
|
1397
|
-
extraArgs.push(`--remote-debugging-port=${cdpPort}`);
|
|
1398
|
-
}
|
|
1399
|
-
if (headless) {
|
|
1400
|
-
defaultArgsToIgnore.push("--headless=old");
|
|
1401
|
-
extraArgs.push("--headless=new");
|
|
1402
|
-
}
|
|
1403
|
-
if (appModeInitialUrl) {
|
|
1404
|
-
extraArgs.push(`--app=${appModeInitialUrl}`);
|
|
1405
|
-
}
|
|
1406
|
-
if (executablePath) {
|
|
1407
|
-
executablePath = await fs2.realpath(executablePath);
|
|
1408
|
-
if (!await fs2.exists(executablePath)) {
|
|
1409
|
-
console.log(
|
|
1410
|
-
`Warning: Executable path ${executablePath} does not exist. Falling back to default.`
|
|
1411
|
-
);
|
|
1412
|
-
executablePath = void 0;
|
|
1413
|
-
}
|
|
1414
|
-
}
|
|
1415
|
-
const viewport = null;
|
|
1416
|
-
const userAgent = process.env.__PLAYWRIGHT_USER_AGENT_OVERRIDE ?? await getHeadlessUserAgent({
|
|
1417
|
-
executablePath
|
|
1418
|
-
});
|
|
1419
|
-
const context = await playwright.chromium.launchPersistentContext(
|
|
1420
|
-
userDataDir,
|
|
1421
|
-
{
|
|
1422
|
-
userAgent,
|
|
1423
|
-
executablePath,
|
|
1424
|
-
headless,
|
|
1425
|
-
viewport,
|
|
1426
|
-
proxy,
|
|
1427
|
-
downloadsPath,
|
|
1428
|
-
args: extraArgs,
|
|
1429
|
-
ignoreDefaultArgs: defaultArgsToIgnore
|
|
1430
|
-
}
|
|
1431
|
-
);
|
|
1432
|
-
context.once("close", async () => {
|
|
1433
|
-
try {
|
|
1434
|
-
await (0, import_fs_extra2.rm)(userDataDir, {
|
|
1435
|
-
recursive: true,
|
|
1436
|
-
force: true,
|
|
1437
|
-
retryDelay: 1e3,
|
|
1438
|
-
maxRetries: 5
|
|
1439
|
-
});
|
|
1440
|
-
if (await isIntunedExtensionEnabled()) {
|
|
1441
|
-
await cleanIntunedExtensionServer();
|
|
1442
|
-
}
|
|
1443
|
-
} catch (error) {
|
|
1444
|
-
console.error("Failed to remove user data dir", error);
|
|
1445
|
-
}
|
|
1446
|
-
});
|
|
1447
|
-
if (cdpPort) {
|
|
1448
|
-
const createdCdpAddress = getLocalCdpAddress(cdpPort);
|
|
1449
|
-
await waitOnCdpAddress(createdCdpAddress);
|
|
1450
|
-
}
|
|
1451
|
-
const page = context.pages().at(0) ?? await context.newPage();
|
|
1452
|
-
if (await isIntunedExtensionEnabled()) {
|
|
1453
|
-
await getIntunedExtensionWorker(context);
|
|
1454
|
-
}
|
|
1455
|
-
return {
|
|
1456
|
-
page,
|
|
1457
|
-
context
|
|
1458
|
-
};
|
|
1459
|
-
}
|
|
1460
|
-
async function getBrowserExecutablePath() {
|
|
1461
|
-
const browserType = getBrowserType();
|
|
1462
|
-
if (browserType === "brave") {
|
|
1463
|
-
return await getBraveExecutablePath();
|
|
1464
|
-
}
|
|
1465
|
-
}
|
|
1466
|
-
async function launchBrowser(options) {
|
|
1467
|
-
if ("cdpAddress" in options) {
|
|
1468
|
-
return launchChromium(options);
|
|
1469
|
-
}
|
|
1470
|
-
return launchChromium({
|
|
1471
|
-
...options,
|
|
1472
|
-
executablePath: await getBrowserExecutablePath()
|
|
1473
|
-
});
|
|
1474
|
-
}
|
|
1475
|
-
function getBrowserType() {
|
|
1476
|
-
if (process.env.BROWSER_TYPE === "brave") {
|
|
1477
|
-
return "brave";
|
|
1478
|
-
}
|
|
1479
|
-
return "chromium";
|
|
1480
|
-
}
|
|
1481
|
-
async function getBraveExecutablePath() {
|
|
1482
|
-
const { stdout } = await execAsync("which brave-browser-stable");
|
|
1483
|
-
const bravePath = stdout.trim();
|
|
1484
|
-
if (bravePath.length === 0) {
|
|
1485
|
-
throw new Error("Brave browser not found");
|
|
1486
|
-
}
|
|
1487
|
-
return bravePath;
|
|
1488
|
-
}
|
|
1489
|
-
function getLocalCdpAddress(port) {
|
|
1490
|
-
return `http://localhost:${port}`;
|
|
1491
|
-
}
|
|
1492
|
-
async function getCdpWebSocketUrl(cdpAddress) {
|
|
1493
|
-
let response;
|
|
1494
|
-
try {
|
|
1495
|
-
response = await fetch(`${cdpAddress}/json/version`);
|
|
1496
|
-
} catch (error) {
|
|
1497
|
-
return (0, import_neverthrow4.err)(
|
|
1498
|
-
`Failed to fetch CDP version from ${cdpAddress}: ${error instanceof Error ? error.message : String(error)}`
|
|
1499
|
-
);
|
|
1500
|
-
}
|
|
1501
|
-
if (!response.ok) {
|
|
1502
|
-
return (0, import_neverthrow4.err)(
|
|
1503
|
-
`Failed to get CDP WebSocket URL from ${cdpAddress}: ${response.status} ${response.statusText}`
|
|
1504
|
-
);
|
|
1505
|
-
}
|
|
1506
|
-
let data;
|
|
1507
|
-
try {
|
|
1508
|
-
data = await response.json();
|
|
1509
|
-
} catch (error) {
|
|
1510
|
-
return (0, import_neverthrow4.err)(
|
|
1511
|
-
`Invalid CDP version response from ${cdpAddress}: ${error instanceof Error ? error.message : String(error)}`
|
|
1512
|
-
);
|
|
1513
|
-
}
|
|
1514
|
-
const cdpJsonVersionResponseSchema = import_zod3.z.object({
|
|
1515
|
-
webSocketDebuggerUrl: import_zod3.z.string().url()
|
|
1516
|
-
});
|
|
1517
|
-
const parseResult = cdpJsonVersionResponseSchema.safeParse(data);
|
|
1518
|
-
if (!parseResult.success) {
|
|
1519
|
-
return (0, import_neverthrow4.err)(
|
|
1520
|
-
`Invalid CDP version response from ${cdpAddress}: ${JSON.stringify(
|
|
1521
|
-
parseResult.error.format()
|
|
1522
|
-
)}`
|
|
1523
|
-
);
|
|
1524
|
-
}
|
|
1525
|
-
return (0, import_neverthrow4.ok)(parseResult.data.webSocketDebuggerUrl);
|
|
1526
|
-
}
|
|
1527
|
-
async function waitOnCdpAddress(cdpAddress) {
|
|
1528
|
-
const cdpAddressWithoutProtocol = cdpAddress.replace("http://", "").replace("https://", "").replace("localhost", "127.0.0.1");
|
|
1529
|
-
await (0, import_wait_on.default)({
|
|
1530
|
-
resources: [`http-get://${cdpAddressWithoutProtocol}/json/version`],
|
|
1531
|
-
delay: 100,
|
|
1532
|
-
interval: 100,
|
|
1533
|
-
timeout: 5e3,
|
|
1534
|
-
tcpTimeout: 1e3,
|
|
1535
|
-
window: 1e3
|
|
1536
|
-
});
|
|
1537
|
-
}
|
|
1538
|
-
async function getHeadlessUserAgent({
|
|
1539
|
-
executablePath,
|
|
1540
|
-
args,
|
|
1541
|
-
ignoreDefaultArgs
|
|
1542
|
-
}) {
|
|
1543
|
-
const browser = await playwright.chromium.launch({
|
|
1544
|
-
headless: true,
|
|
1545
|
-
executablePath,
|
|
1546
|
-
args,
|
|
1547
|
-
ignoreDefaultArgs
|
|
1548
|
-
});
|
|
1549
|
-
const context = await browser.newContext();
|
|
1550
|
-
const page = await context.newPage();
|
|
1551
|
-
let userAgent = await page.evaluate(() => navigator.userAgent);
|
|
1552
|
-
await browser.close();
|
|
1553
|
-
if (!userAgent || typeof userAgent !== "string") {
|
|
1554
|
-
return void 0;
|
|
1555
|
-
}
|
|
1556
|
-
userAgent = userAgent.replace("HeadlessChrome", "Chrome");
|
|
1557
|
-
return userAgent;
|
|
1558
|
-
}
|
|
1559
|
-
|
|
1560
|
-
// src/common/playwrightContext.ts
|
|
1561
|
-
var import_runtime_interface3 = __toESM(require_dist());
|
|
1562
|
-
async function withPlaywrightContext({
|
|
1563
|
-
cdpAddress,
|
|
1564
|
-
cdpTargetId,
|
|
1565
|
-
proxy,
|
|
1566
|
-
headless = true,
|
|
1567
|
-
downloadsPath,
|
|
1568
|
-
importFunction,
|
|
1569
|
-
apiName,
|
|
1570
|
-
apiParameters
|
|
1571
|
-
}, fn) {
|
|
1572
|
-
let context;
|
|
1573
|
-
let page;
|
|
1574
|
-
try {
|
|
1575
|
-
const setupContextHookResult = importFunction ? await loadSetupContextHook({
|
|
1576
|
-
importFunction
|
|
1577
|
-
}) : (0, import_neverthrow5.ok)(null);
|
|
1578
|
-
if (setupContextHookResult.isErr()) {
|
|
1579
|
-
return setupContextHookResult;
|
|
1580
|
-
}
|
|
1581
|
-
const setupContextHook = setupContextHookResult.value;
|
|
1582
|
-
if (setupContextHook === null) {
|
|
1583
|
-
if (cdpAddress !== void 0) {
|
|
1584
|
-
({ page, context } = await launchBrowser({ cdpAddress, cdpTargetId }));
|
|
1585
|
-
} else {
|
|
1586
|
-
({ page, context } = await launchBrowser({
|
|
1587
|
-
proxy,
|
|
1588
|
-
headless,
|
|
1589
|
-
downloadsPath
|
|
1590
|
-
}));
|
|
1591
|
-
}
|
|
1592
|
-
return await fn(context, page);
|
|
1593
|
-
}
|
|
1594
|
-
let hookCdpUrl = null;
|
|
1595
|
-
if (cdpAddress) {
|
|
1596
|
-
hookCdpUrl = cdpAddress;
|
|
1597
|
-
({ context, page } = await launchBrowser({ cdpAddress, cdpTargetId }));
|
|
1598
|
-
} else {
|
|
1599
|
-
const port = await (0, import_portfinder2.getPort)({
|
|
1600
|
-
port: 9222
|
|
1601
|
-
});
|
|
1602
|
-
({ context, page } = await launchBrowser({
|
|
1603
|
-
proxy,
|
|
1604
|
-
headless,
|
|
1605
|
-
downloadsPath,
|
|
1606
|
-
cdpPort: port
|
|
1607
|
-
}));
|
|
1608
|
-
hookCdpUrl = getLocalCdpAddress(port);
|
|
1609
|
-
}
|
|
1610
|
-
let hookResult;
|
|
1611
|
-
try {
|
|
1612
|
-
const wsUrlResult = await getCdpWebSocketUrl(hookCdpUrl);
|
|
1613
|
-
if (wsUrlResult.isOk()) {
|
|
1614
|
-
hookCdpUrl = wsUrlResult.value;
|
|
1615
|
-
} else {
|
|
1616
|
-
throw new Error(wsUrlResult.error);
|
|
1617
|
-
}
|
|
1618
|
-
hookResult = await setupContextHook({
|
|
1619
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
1620
|
-
apiName,
|
|
1621
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
1622
|
-
apiParameters,
|
|
1623
|
-
cdpUrl: hookCdpUrl
|
|
119
|
+
if (message.type === "ping") {
|
|
120
|
+
client.sendJSON({
|
|
121
|
+
type: "pong"
|
|
122
|
+
});
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
await handleMessage(message);
|
|
126
|
+
receiveMessagesPromise = receiveMessages();
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const {
|
|
130
|
+
result
|
|
131
|
+
} = messageOrResult;
|
|
132
|
+
const success = result.isOk();
|
|
133
|
+
const resultToSend = success ? result.value : result.error.json;
|
|
134
|
+
client.sendJSON({
|
|
135
|
+
type: "done",
|
|
136
|
+
result: resultToSend,
|
|
137
|
+
success
|
|
1624
138
|
});
|
|
1625
|
-
|
|
1626
|
-
return (0, import_neverthrow5.err)(new import_runtime_interface3.AutomationError(error));
|
|
1627
|
-
}
|
|
1628
|
-
if (!hookResult) {
|
|
1629
|
-
return await fn(context, page);
|
|
1630
|
-
}
|
|
1631
|
-
const { page: newPage, context: newContext, cleanup } = hookResult;
|
|
1632
|
-
let result;
|
|
1633
|
-
try {
|
|
1634
|
-
result = { return: await fn(newContext, newPage ?? page) };
|
|
1635
|
-
} catch (e) {
|
|
1636
|
-
result = { throw: e };
|
|
1637
|
-
}
|
|
1638
|
-
try {
|
|
1639
|
-
await cleanup?.();
|
|
1640
|
-
} catch (e) {
|
|
1641
|
-
result = { return: (0, import_neverthrow5.err)(new import_runtime_interface3.AutomationError(e)) };
|
|
1642
|
-
}
|
|
1643
|
-
if ("throw" in result) {
|
|
1644
|
-
throw result.throw;
|
|
139
|
+
break;
|
|
1645
140
|
}
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
await context?.close();
|
|
1649
|
-
}
|
|
1650
|
-
}
|
|
1651
|
-
async function loadSessionToContext({
|
|
1652
|
-
context,
|
|
1653
|
-
session
|
|
1654
|
-
}) {
|
|
1655
|
-
let sessionToLoad;
|
|
1656
|
-
if (session.type === "state") {
|
|
1657
|
-
const state = session.state;
|
|
1658
|
-
if (state === void 0 || state === null) {
|
|
1659
|
-
return;
|
|
141
|
+
if (!client.closed) {
|
|
142
|
+
await client.close();
|
|
1660
143
|
}
|
|
1661
|
-
|
|
1662
|
-
} else {
|
|
1663
|
-
const fullPath = getFullPathInProject(session.path);
|
|
1664
|
-
sessionToLoad = await fs3.readJson(fullPath);
|
|
1665
|
-
}
|
|
1666
|
-
await setStorageState(context, sessionToLoad);
|
|
1667
|
-
}
|
|
1668
|
-
|
|
1669
|
-
// src/common/formatZodError.ts
|
|
1670
|
-
function formatZodError(zodError) {
|
|
1671
|
-
const formattedErrors = zodError.errors.map((error) => {
|
|
1672
|
-
const path3 = error.path.map((segment) => {
|
|
1673
|
-
return typeof segment === "number" ? `[${segment}]` : segment;
|
|
1674
|
-
}).join(".");
|
|
1675
|
-
if (path3) {
|
|
1676
|
-
return `${path3} is invalid - ${error.message}`;
|
|
1677
|
-
}
|
|
1678
|
-
return error.message;
|
|
144
|
+
process.exit(0);
|
|
1679
145
|
});
|
|
1680
|
-
|
|
1681
|
-
}
|
|
1682
|
-
|
|
1683
|
-
// src/common/cleanEnvironmentVariables.ts
|
|
1684
|
-
function cleanEnvironmentVariables() {
|
|
1685
|
-
Object.keys(process.env).filter((i) => {
|
|
1686
|
-
if (i.toLocaleLowerCase().startsWith("npm")) {
|
|
1687
|
-
return true;
|
|
1688
|
-
}
|
|
1689
|
-
if (i.toLocaleLowerCase().startsWith("fly") && i !== "FLY_ALLOC_ID") {
|
|
1690
|
-
return true;
|
|
1691
|
-
}
|
|
1692
|
-
}).forEach((i) => delete process.env[i]);
|
|
1693
|
-
}
|
|
1694
|
-
|
|
1695
|
-
// src/common/runApi/index.ts
|
|
1696
|
-
var import_runtime_interface4 = __toESM(require_dist());
|
|
1697
|
-
function getObjectSizeInBytes(obj) {
|
|
1698
|
-
try {
|
|
1699
|
-
return new TextEncoder().encode(JSON.stringify(obj)).length;
|
|
1700
|
-
} catch (error) {
|
|
1701
|
-
return new TextEncoder().encode(String(obj)).length;
|
|
1702
|
-
}
|
|
1703
|
-
}
|
|
1704
|
-
async function runApi({
|
|
1705
|
-
abortSignal,
|
|
1706
|
-
importFunction,
|
|
1707
|
-
retrieveSession = false,
|
|
1708
|
-
...input
|
|
1709
|
-
}) {
|
|
1710
|
-
let traceStarted = false;
|
|
1711
|
-
const inputParseResult = import_runtime_interface4.runApiParametersSchema.safeParse(input);
|
|
1712
|
-
if (!inputParseResult.success) {
|
|
1713
|
-
return (0, import_neverthrow6.err)(
|
|
1714
|
-
new import_runtime_interface4.InternalInvalidInputError(
|
|
1715
|
-
"Input validation failed",
|
|
1716
|
-
formatZodError(inputParseResult.error)
|
|
1717
|
-
)
|
|
1718
|
-
);
|
|
1719
|
-
}
|
|
1720
|
-
const {
|
|
1721
|
-
automationFunction: { name, params },
|
|
1722
|
-
runOptions,
|
|
1723
|
-
tracing,
|
|
1724
|
-
auth
|
|
1725
|
-
} = inputParseResult.data;
|
|
1726
|
-
const abortSymbol = /* @__PURE__ */ Symbol("abort");
|
|
1727
|
-
const abortPromise = new Promise((resolve2) => {
|
|
1728
|
-
if (!abortSignal) return;
|
|
1729
|
-
abortSignal.addEventListener("abort", () => {
|
|
1730
|
-
resolve2(abortSymbol);
|
|
1731
|
-
});
|
|
1732
|
-
});
|
|
1733
|
-
async function runAutomation() {
|
|
1734
|
-
const validatedModuleResult = await importUsingImportFunction({
|
|
1735
|
-
path: name,
|
|
1736
|
-
importFunction
|
|
1737
|
-
});
|
|
1738
|
-
if (validatedModuleResult.isErr()) {
|
|
1739
|
-
return (0, import_neverthrow6.err)(validatedModuleResult.error);
|
|
1740
|
-
}
|
|
1741
|
-
const automationFunction = validatedModuleResult.value;
|
|
1742
|
-
if (auth && auth.session.type === "state") {
|
|
1743
|
-
const state = auth.session.state;
|
|
1744
|
-
if (state === void 0 || state === null) {
|
|
1745
|
-
return (0, import_neverthrow6.err)(new import_runtime_interface4.AuthRequiredError());
|
|
1746
|
-
}
|
|
1747
|
-
}
|
|
1748
|
-
const playwrightContextParameters = {
|
|
1749
|
-
apiName: name,
|
|
1750
|
-
apiParameters: params,
|
|
1751
|
-
importFunction
|
|
1752
|
-
};
|
|
1753
|
-
const intunedContext = getExecutionContext();
|
|
1754
|
-
const runAutomationWithContext = async (context, page) => {
|
|
1755
|
-
async function saveTraceIfNeeded({
|
|
1756
|
-
errorMessage
|
|
1757
|
-
}) {
|
|
1758
|
-
if (!tracing.enabled || !traceStarted) {
|
|
1759
|
-
return;
|
|
1760
|
-
}
|
|
1761
|
-
try {
|
|
1762
|
-
await context?.tracing.stop({ path: tracing.filePath });
|
|
1763
|
-
} catch (error) {
|
|
1764
|
-
console.log(errorMessage, error?.message);
|
|
1765
|
-
await (0, import_fs_extra3.remove)(tracing.filePath);
|
|
1766
|
-
}
|
|
1767
|
-
}
|
|
1768
|
-
if (auth) {
|
|
1769
|
-
await loadSessionToContext({
|
|
1770
|
-
context,
|
|
1771
|
-
session: auth.session
|
|
1772
|
-
});
|
|
1773
|
-
if (intunedContext && auth.parameters) {
|
|
1774
|
-
intunedContext.getAuthSessionParameters = async () => auth.parameters;
|
|
1775
|
-
}
|
|
1776
|
-
}
|
|
1777
|
-
if (tracing.enabled) {
|
|
1778
|
-
await context.tracing.start({
|
|
1779
|
-
screenshots: true,
|
|
1780
|
-
snapshots: true,
|
|
1781
|
-
sources: true
|
|
1782
|
-
});
|
|
1783
|
-
traceStarted = true;
|
|
1784
|
-
}
|
|
1785
|
-
cleanEnvironmentVariables();
|
|
1786
|
-
const automationFunctionParameters = [
|
|
1787
|
-
...params !== void 0 ? [params] : [],
|
|
1788
|
-
page,
|
|
1789
|
-
context
|
|
1790
|
-
];
|
|
1791
|
-
try {
|
|
1792
|
-
const [automationFunctionResult] = await Promise.all([
|
|
1793
|
-
automationFunction(...automationFunctionParameters)
|
|
1794
|
-
]);
|
|
1795
|
-
const MAX_RESULT_SIZE_BYTES = 2 * 1024 * 1024;
|
|
1796
|
-
const resultSizeInBytes = getObjectSizeInBytes(
|
|
1797
|
-
automationFunctionResult
|
|
1798
|
-
);
|
|
1799
|
-
if (resultSizeInBytes > MAX_RESULT_SIZE_BYTES) {
|
|
1800
|
-
return (0, import_neverthrow6.err)(
|
|
1801
|
-
new import_runtime_interface4.ResultTooBigError(resultSizeInBytes, MAX_RESULT_SIZE_BYTES)
|
|
1802
|
-
);
|
|
1803
|
-
}
|
|
1804
|
-
if (retrieveSession) {
|
|
1805
|
-
return (0, import_neverthrow6.ok)({
|
|
1806
|
-
result: automationFunctionResult,
|
|
1807
|
-
extendedPayloads: intunedContext?.extendedPayloads,
|
|
1808
|
-
session: await getStorageState(context)
|
|
1809
|
-
});
|
|
1810
|
-
}
|
|
1811
|
-
return (0, import_neverthrow6.ok)({
|
|
1812
|
-
result: automationFunctionResult,
|
|
1813
|
-
extendedPayloads: intunedContext?.extendedPayloads
|
|
1814
|
-
});
|
|
1815
|
-
} catch (error) {
|
|
1816
|
-
return (0, import_neverthrow6.err)(new import_runtime_interface4.AutomationError(error));
|
|
1817
|
-
} finally {
|
|
1818
|
-
await saveTraceIfNeeded({ errorMessage: "failed to save trace" });
|
|
1819
|
-
}
|
|
1820
|
-
};
|
|
1821
|
-
if (intunedContext?.store) {
|
|
1822
|
-
intunedContext.store = {};
|
|
1823
|
-
}
|
|
1824
|
-
if (runOptions.environment === "standalone") {
|
|
1825
|
-
const downloadsPath = getDownloadDirectoryPath();
|
|
1826
|
-
try {
|
|
1827
|
-
return await withPlaywrightContext(
|
|
1828
|
-
{
|
|
1829
|
-
headless: runOptions.headless,
|
|
1830
|
-
proxy: runOptions.proxy,
|
|
1831
|
-
downloadsPath,
|
|
1832
|
-
...playwrightContextParameters
|
|
1833
|
-
},
|
|
1834
|
-
runAutomationWithContext
|
|
1835
|
-
);
|
|
1836
|
-
} finally {
|
|
1837
|
-
await fs4.remove(downloadsPath);
|
|
1838
|
-
}
|
|
1839
|
-
} else {
|
|
1840
|
-
return await withPlaywrightContext(
|
|
1841
|
-
{
|
|
1842
|
-
cdpAddress: runOptions.cdpAddress,
|
|
1843
|
-
cdpTargetId: runOptions.cdpTargetId,
|
|
1844
|
-
...playwrightContextParameters
|
|
1845
|
-
},
|
|
1846
|
-
runAutomationWithContext
|
|
1847
|
-
);
|
|
1848
|
-
}
|
|
1849
|
-
}
|
|
1850
|
-
const result = await Promise.race([await runAutomation(), abortPromise]);
|
|
1851
|
-
if (result === abortSymbol) {
|
|
1852
|
-
return (0, import_neverthrow6.err)(new import_runtime_interface4.AbortedError());
|
|
1853
|
-
}
|
|
1854
|
-
return result;
|
|
1855
|
-
}
|
|
1856
|
-
|
|
1857
|
-
// src/commands/interface/run.ts
|
|
1858
|
-
var import_promises3 = require("timers/promises");
|
|
1859
|
-
var import_neverthrow7 = require("neverthrow");
|
|
1860
|
-
|
|
1861
|
-
// src/common/backendFunctions/getAuthSessionParameters.ts
|
|
1862
|
-
var import_isNil = __toESM(require("lodash/isNil"));
|
|
1863
|
-
var import_zod4 = require("zod");
|
|
1864
|
-
var authSessionParametersResponseSchema = import_zod4.z.object({
|
|
1865
|
-
parameters: import_zod4.z.any().refine((v) => v !== void 0)
|
|
1866
|
-
});
|
|
1867
|
-
async function getAuthSessionParameters2() {
|
|
1868
|
-
const context = getExecutionContext();
|
|
1869
|
-
if (!context) {
|
|
1870
|
-
throw new Error(
|
|
1871
|
-
"getAuthSessionParameters failed due to an internal error (context was not found)."
|
|
1872
|
-
);
|
|
1873
|
-
}
|
|
1874
|
-
const authSessionId = context.authSessionId;
|
|
1875
|
-
if ((0, import_isNil.default)(authSessionId)) {
|
|
1876
|
-
throw new Error("AuthSessions are not enabled");
|
|
1877
|
-
}
|
|
1878
|
-
const response = await callBackendFunctionWithToken(
|
|
1879
|
-
`auth-session/${authSessionId}/parameters`,
|
|
1880
|
-
{
|
|
1881
|
-
method: "GET"
|
|
1882
|
-
}
|
|
1883
|
-
);
|
|
1884
|
-
const body = await response.text();
|
|
1885
|
-
if (!response.ok) {
|
|
1886
|
-
throw new Error(
|
|
1887
|
-
`getAuthSessionParameters failed with status ${response.status}: ${body}`
|
|
1888
|
-
);
|
|
1889
|
-
}
|
|
1890
|
-
let json;
|
|
1891
|
-
try {
|
|
1892
|
-
json = JSON.parse(body);
|
|
1893
|
-
} catch (e) {
|
|
1894
|
-
throw new Error(`Expected JSON response, but got ${body}`);
|
|
1895
|
-
}
|
|
1896
|
-
return authSessionParametersResponseSchema.parse(json).parameters;
|
|
1897
|
-
}
|
|
1898
|
-
|
|
1899
|
-
// src/commands/interface/run.ts
|
|
1900
|
-
var import_runtime_interface5 = __toESM(require_dist());
|
|
1901
|
-
import_dotenv2.default.config({
|
|
1902
|
-
path: `.env`
|
|
1903
|
-
});
|
|
1904
|
-
function runAutomationCLI(importFunction) {
|
|
1905
|
-
import_commander.program.description("run user automation and communicate using unix socket").argument(
|
|
1906
|
-
"<input>",
|
|
1907
|
-
"In unix socket mode: path to the unix socket. In jsonl mode: path to the jsonl file. In tcp socket mode: host:port"
|
|
1908
|
-
).option("--mode [mode]", "Communication mode: unix, jsonl, or tcp").action(
|
|
1909
|
-
async (input, {
|
|
1910
|
-
mode = "unix"
|
|
1911
|
-
}) => {
|
|
1912
|
-
let context;
|
|
1913
|
-
const throttleTime = 60 * 1e3;
|
|
1914
|
-
let timeoutTimestamp = Date.now();
|
|
1915
|
-
const client = createClient(mode, input);
|
|
1916
|
-
const abortController = new AbortController();
|
|
1917
|
-
const interruptSignalHandler = async () => {
|
|
1918
|
-
abortController.abort();
|
|
1919
|
-
await (0, import_promises3.setTimeout)(6e4);
|
|
1920
|
-
await client.close();
|
|
1921
|
-
process.exit(1);
|
|
1922
|
-
};
|
|
1923
|
-
process.on("SIGINT", interruptSignalHandler);
|
|
1924
|
-
process.on("SIGTERM", interruptSignalHandler);
|
|
1925
|
-
const messagesGenerator = client.receiveJSON();
|
|
1926
|
-
async function receiveMessages() {
|
|
1927
|
-
const data = await messagesGenerator.next();
|
|
1928
|
-
if (data.done) {
|
|
1929
|
-
return;
|
|
1930
|
-
}
|
|
1931
|
-
const inputParseResult = import_runtime_interface5.runApiInputSchema.safeParse(data.value);
|
|
1932
|
-
if (!inputParseResult.success) {
|
|
1933
|
-
console.error(
|
|
1934
|
-
"Validation error:",
|
|
1935
|
-
formatZodError(inputParseResult.error)
|
|
1936
|
-
);
|
|
1937
|
-
return {
|
|
1938
|
-
type: "error",
|
|
1939
|
-
error: new import_runtime_interface5.InternalInvalidInputError(
|
|
1940
|
-
"Invalid input",
|
|
1941
|
-
formatZodError(inputParseResult.error)
|
|
1942
|
-
)
|
|
1943
|
-
};
|
|
1944
|
-
}
|
|
1945
|
-
const input2 = inputParseResult.data;
|
|
1946
|
-
return input2;
|
|
1947
|
-
}
|
|
1948
|
-
let resultPromise = null;
|
|
1949
|
-
async function handleMessage(message) {
|
|
1950
|
-
if (message.type === "abort") {
|
|
1951
|
-
abortController.abort();
|
|
1952
|
-
return;
|
|
1953
|
-
}
|
|
1954
|
-
if (message.type === "tokenUpdate") {
|
|
1955
|
-
backendFunctionsTokenManager.token = message.parameters.functionsToken;
|
|
1956
|
-
return;
|
|
1957
|
-
}
|
|
1958
|
-
if (message.type === "start") {
|
|
1959
|
-
context = {
|
|
1960
|
-
extendedPayloads: [],
|
|
1961
|
-
runEnvironment: message.parameters.runOptions.environment === "standalone" ? "DEPLOYED" /* DEPLOYED */ : "IDE" /* IDE */,
|
|
1962
|
-
timeoutInfo: {
|
|
1963
|
-
extendTimeoutCallback: async () => {
|
|
1964
|
-
if (Date.now() - timeoutTimestamp < throttleTime) return;
|
|
1965
|
-
timeoutTimestamp = Date.now();
|
|
1966
|
-
client.sendJSON({ type: "extend" });
|
|
1967
|
-
}
|
|
1968
|
-
},
|
|
1969
|
-
getAuthSessionParameters: getAuthSessionParameters2,
|
|
1970
|
-
...message.parameters.context ?? {},
|
|
1971
|
-
proxy: getProxyUrlFromRunOptions(message.parameters.runOptions)
|
|
1972
|
-
};
|
|
1973
|
-
backendFunctionsTokenManager.token = message.parameters.functionsToken;
|
|
1974
|
-
resultPromise = runWithContext(context, async () => {
|
|
1975
|
-
return await runApi({
|
|
1976
|
-
...message.parameters,
|
|
1977
|
-
abortSignal: abortController.signal,
|
|
1978
|
-
importFunction: importFunction ?? _defaultImport
|
|
1979
|
-
});
|
|
1980
|
-
});
|
|
1981
|
-
return;
|
|
1982
|
-
}
|
|
1983
|
-
}
|
|
1984
|
-
let receiveMessagesPromise = receiveMessages();
|
|
1985
|
-
while (true) {
|
|
1986
|
-
const messageOrResult = await Promise.race([
|
|
1987
|
-
receiveMessagesPromise.then((message) => ({
|
|
1988
|
-
type: "message",
|
|
1989
|
-
message
|
|
1990
|
-
})),
|
|
1991
|
-
...resultPromise ? [
|
|
1992
|
-
resultPromise.then((result2) => ({
|
|
1993
|
-
type: "result",
|
|
1994
|
-
result: result2
|
|
1995
|
-
}))
|
|
1996
|
-
] : []
|
|
1997
|
-
]);
|
|
1998
|
-
if (messageOrResult.type === "message") {
|
|
1999
|
-
const { message } = messageOrResult;
|
|
2000
|
-
if (message === void 0) {
|
|
2001
|
-
break;
|
|
2002
|
-
}
|
|
2003
|
-
if (message.type === "error") {
|
|
2004
|
-
client.sendJSON({
|
|
2005
|
-
type: "done",
|
|
2006
|
-
result: message.error.json,
|
|
2007
|
-
success: false
|
|
2008
|
-
});
|
|
2009
|
-
break;
|
|
2010
|
-
}
|
|
2011
|
-
if (message.type === "ping") {
|
|
2012
|
-
client.sendJSON({
|
|
2013
|
-
type: "pong"
|
|
2014
|
-
});
|
|
2015
|
-
break;
|
|
2016
|
-
}
|
|
2017
|
-
await handleMessage(message);
|
|
2018
|
-
receiveMessagesPromise = receiveMessages();
|
|
2019
|
-
continue;
|
|
2020
|
-
}
|
|
2021
|
-
const { result } = messageOrResult;
|
|
2022
|
-
const success = result.isOk();
|
|
2023
|
-
const resultToSend = success ? result.value : result.error.json;
|
|
2024
|
-
client.sendJSON({
|
|
2025
|
-
type: "done",
|
|
2026
|
-
result: resultToSend,
|
|
2027
|
-
success
|
|
2028
|
-
});
|
|
2029
|
-
break;
|
|
2030
|
-
}
|
|
2031
|
-
if (!client.closed) {
|
|
2032
|
-
await client.close();
|
|
2033
|
-
}
|
|
2034
|
-
process.exit(0);
|
|
2035
|
-
}
|
|
2036
|
-
);
|
|
2037
|
-
import_commander.program.parse(process.argv);
|
|
146
|
+
_commander.program.parse(process.argv);
|
|
2038
147
|
}
|
|
2039
148
|
function getProxyUrlFromRunOptions(runOptions) {
|
|
2040
|
-
if (runOptions?.environment !== "standalone") return
|
|
149
|
+
if (runOptions?.environment !== "standalone") return undefined;
|
|
2041
150
|
const proxy = runOptions.proxy;
|
|
2042
|
-
if (!proxy) return
|
|
151
|
+
if (!proxy) return undefined;
|
|
2043
152
|
const url = new URL(proxy.server);
|
|
2044
153
|
url.username = proxy.username;
|
|
2045
154
|
url.password = proxy.password;
|
|
2046
155
|
return url.toString();
|
|
2047
156
|
}
|
|
2048
|
-
|
|
157
|
+
const _defaultImport = async path => {
|
|
2049
158
|
try {
|
|
2050
|
-
const imported = await Promise
|
|
2051
|
-
return (0,
|
|
159
|
+
const imported = await (specifier => new Promise(r => r(specifier)).then(s => _interopRequireWildcard(require(s))))(`${process.cwd()}/${path}`);
|
|
160
|
+
return (0, _neverthrow.ok)(imported);
|
|
2052
161
|
} catch (e) {
|
|
2053
162
|
if ("code" in e && e.code === "MODULE_NOT_FOUND" || e.code === "ERR_MODULE_NOT_FOUND") {
|
|
2054
|
-
return (0,
|
|
163
|
+
return (0, _neverthrow.err)({
|
|
2055
164
|
type: "not_found"
|
|
2056
165
|
});
|
|
2057
166
|
}
|
|
2058
|
-
return (0,
|
|
167
|
+
return (0, _neverthrow.err)({
|
|
2059
168
|
type: "other",
|
|
2060
169
|
error: e
|
|
2061
170
|
});
|
|
@@ -2063,7 +172,7 @@ var _defaultImport = async (path3) => {
|
|
|
2063
172
|
};
|
|
2064
173
|
function createClient(mode, input) {
|
|
2065
174
|
if (mode === "jsonl") {
|
|
2066
|
-
return new
|
|
175
|
+
return new _runtimeInterface.JSONLFileClient(input);
|
|
2067
176
|
}
|
|
2068
177
|
if (mode === "tcp") {
|
|
2069
178
|
const [host, portString] = input.split(":");
|
|
@@ -2071,14 +180,10 @@ function createClient(mode, input) {
|
|
|
2071
180
|
if (!host || isNaN(port)) {
|
|
2072
181
|
throw new Error("Invalid tcp input, expected format host:port");
|
|
2073
182
|
}
|
|
2074
|
-
return new
|
|
183
|
+
return new _runtimeInterface.TCPSocketClient(host, port);
|
|
2075
184
|
}
|
|
2076
|
-
return new
|
|
185
|
+
return new _runtimeInterface.UnixSocketClient(input);
|
|
2077
186
|
}
|
|
2078
187
|
if (require.main === module) {
|
|
2079
188
|
runAutomationCLI();
|
|
2080
|
-
}
|
|
2081
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
2082
|
-
0 && (module.exports = {
|
|
2083
|
-
runAutomationCLI
|
|
2084
|
-
});
|
|
189
|
+
}
|