@intuned/runtime-dev 1.0.2-abort.2 → 1.0.4-refactor.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # UNRELEASED
2
+
3
+ -
4
+
5
+ # 1.0.3
6
+
7
+ - Fix node-fetch builds (used in deprecated `downloadFile` SDK functions).
@@ -1,2 +1 @@
1
- #!/usr/bin/env node
2
1
  export {};
@@ -1,4 +1,3 @@
1
- #!/usr/bin/env node
2
1
  "use strict";
3
2
 
4
3
  var _commander = require("commander");
@@ -61,7 +60,7 @@ async function build(outfile, mode, template) {
61
60
  if (isThirdPartyWarning(warning)) return;
62
61
  warn(warning);
63
62
  },
64
- external: ["@intuned/playwright", "@intuned/playwright-core", "jsdom", "canvas", "pdfjs-dist", "pdf-to-png-converter", "crypto", "applicationinsights", "fingerprint-generator", "fingerprint-injector"]
63
+ external: ["@intuned/playwright", "@intuned/playwright-core", "jsdom", "canvas", "pdfjs-dist", "pdf-to-png-converter", "crypto", "applicationinsights", "fingerprint-generator", "fingerprint-injector", "node-fetch"]
65
64
  });
66
65
  console.log(`📦 Building ${outfile}`);
67
66
  const outfileFolder = path.dirname(outfile);
@@ -15,7 +15,6 @@ var _unixSocket = require("../common/utils/unixSocket");
15
15
  var _promises = require("timers/promises");
16
16
  var _jwtTokenManager = require("../../common/jwtTokenManager");
17
17
  var _formatZodError = require("../../common/formatZodError");
18
- var _stream = require("stream");
19
18
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
20
19
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
21
20
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -61,116 +60,133 @@ function runAutomationCLI(importFunction) {
61
60
  let generator = null;
62
61
  const abortController = new AbortController();
63
62
  client.on("error", err => {
64
- var _generator2;
65
- void ((_generator2 = generator) === null || _generator2 === void 0 ? void 0 : _generator2.throw(err).catch(() => undefined));
63
+ var _generator;
64
+ void ((_generator = generator) === null || _generator === void 0 ? void 0 : _generator.throw(err).catch(() => undefined));
66
65
  });
67
66
  process.on("SIGINT", () => {
68
- var _generator3;
69
- void ((_generator3 = generator) === null || _generator3 === void 0 ? void 0 : _generator3.throw(new Error("Interrupted")).catch(() => undefined));
67
+ var _generator2;
68
+ void ((_generator2 = generator) === null || _generator2 === void 0 ? void 0 : _generator2.throw(new Error("Interrupted")).catch(() => undefined));
70
69
  client.end();
71
70
  process.exit(1);
72
71
  });
73
72
  process.on("SIGTERM", () => {
74
- var _generator4;
75
- void ((_generator4 = generator) === null || _generator4 === void 0 ? void 0 : _generator4.throw(new Error("Interrupted")).catch(() => undefined));
73
+ var _generator3;
74
+ void ((_generator3 = generator) === null || _generator3 === void 0 ? void 0 : _generator3.throw(new Error("Interrupted")).catch(() => undefined));
76
75
  client.end();
77
76
  process.exit(1);
78
77
  });
79
78
  const jsonUnixSocket = new _unixSocket.JSONUnixSocket(client);
80
- async function runGeneratorAndSendResult(next) {
81
- const _generator = generator;
82
- if (_generator == null) return {
83
- done: true
84
- };
85
- const result = await (0, _asyncLocalStorage.runWithContext)(context, () => _generator.next(next));
86
- if (result.done) {
87
- const resultToSend = result.value.isOk() ? result.value.value : result.value.error.json;
88
- const success = result.value.isOk();
89
- jsonUnixSocket.sendJSON({
90
- type: "done",
91
- result: resultToSend,
92
- success
79
+ const messagesGenerator = jsonUnixSocket.receiveJSON();
80
+ async function receiveMessages() {
81
+ const data = await messagesGenerator.next();
82
+ if (data.done) {
83
+ return;
84
+ }
85
+ const inputParseResult = inputSchema.safeParse(data.value);
86
+ if (!inputParseResult.success) {
87
+ console.error("Validation error:", (0, _formatZodError.formatZodError)(inputParseResult.error));
88
+ return {
89
+ type: "error",
90
+ error: new _runApi.InternalInvalidInputError("Invalid input", (0, _formatZodError.formatZodError)(inputParseResult.error))
91
+ };
92
+ }
93
+ const input = inputParseResult.data;
94
+ return input;
95
+ }
96
+ let resultPromise = null;
97
+ async function handleMessage(input) {
98
+ if (input.type === "abort") {
99
+ abortController.abort();
100
+ return;
101
+ }
102
+ if (input.type === "tokenUpdate") {
103
+ _jwtTokenManager.backendFunctionsTokenManager.token = input.parameters.functionsToken;
104
+ return;
105
+ }
106
+ if (input.type === "start") {
107
+ generator = (0, _runApi.runApiGenerator)({
108
+ ...input.parameters,
109
+ abortSignal: abortController.signal,
110
+ importFunction
111
+ });
112
+ context = {
113
+ extendedPayloads: [],
114
+ runEnvironment: input.parameters.runOptions.environment === "standalone" ? _enums.RunEnvironment.DEPLOYED : _enums.RunEnvironment.IDE,
115
+ timeoutInfo: {
116
+ extendTimeoutCallback: async () => {
117
+ if (Date.now() - timeoutTimestamp < throttleTime) return;
118
+ timeoutTimestamp = Date.now();
119
+ jsonUnixSocket.sendJSON({
120
+ type: "extend"
121
+ });
122
+ }
123
+ },
124
+ ...(input.parameters.context ?? {}),
125
+ proxy: getProxyUrlFromRunOptions(input.parameters.runOptions)
126
+ };
127
+ _jwtTokenManager.backendFunctionsTokenManager.token = input.parameters.functionsToken;
128
+ resultPromise = (0, _asyncLocalStorage.runWithContext)(context, async () => {
129
+ return await generator.next();
130
+ });
131
+ }
132
+ if (input.type === "next") {
133
+ if (!generator) {
134
+ throw new Error("generator not started");
135
+ }
136
+ resultPromise = (0, _asyncLocalStorage.runWithContext)(context, async () => {
137
+ return await generator.next(input.parameters.value);
93
138
  });
94
- } else {
139
+ }
140
+ }
141
+ async function handleResult(generatorResult) {
142
+ if (!generatorResult.done) {
95
143
  jsonUnixSocket.sendJSON({
96
144
  type: "yield",
97
- result: result.value
145
+ result: generatorResult.value
98
146
  });
147
+ return;
99
148
  }
100
- return {
101
- done: result.done ?? false
102
- };
149
+ const result = generatorResult.value;
150
+ const success = result.isOk();
151
+ const resultToSend = success ? result.value : result.error.json;
152
+ jsonUnixSocket.sendJSON({
153
+ type: "done",
154
+ result: resultToSend,
155
+ success
156
+ });
103
157
  }
104
- const stream = new _stream.PassThrough({
105
- objectMode: true
106
- });
107
- async function messagesLoop() {
108
- for await (const data of jsonUnixSocket.receiveJSON()) {
109
- const inputParseResult = inputSchema.safeParse(data);
110
- if (!inputParseResult.success) {
111
- console.error("Validation error:", (0, _formatZodError.formatZodError)(inputParseResult.error));
158
+ let receiveMessagesPromise = receiveMessages();
159
+ while (true) {
160
+ const messageOrResult = await Promise.race([receiveMessagesPromise.then(message => ({
161
+ type: "message",
162
+ message
163
+ })), ...(resultPromise ? [resultPromise.then(result => ({
164
+ type: "result",
165
+ result
166
+ }))] : [])]);
167
+ if (messageOrResult.type === "message") {
168
+ const {
169
+ message
170
+ } = messageOrResult;
171
+ if (message === undefined) {
172
+ break;
173
+ }
174
+ if (message.type === "error") {
112
175
  jsonUnixSocket.sendJSON({
113
176
  type: "done",
114
- result: new _runApi.InternalInvalidInputError("Invalid input", (0, _formatZodError.formatZodError)(inputParseResult.error)).json,
177
+ result: message.error.json,
115
178
  success: false
116
179
  });
117
180
  break;
118
181
  }
119
- const input = inputParseResult.data;
120
- if (input.type === "abort") {
121
- abortController.abort();
122
- break;
123
- }
124
- if (input.type === "tokenUpdate") {
125
- _jwtTokenManager.backendFunctionsTokenManager.token = input.parameters.functionsToken;
126
- continue;
127
- }
128
- if (input.type === "start") {
129
- _jwtTokenManager.backendFunctionsTokenManager.token = input.parameters.functionsToken;
130
- generator = (0, _runApi.runApiGenerator)({
131
- ...input.parameters,
132
- abortSignal: abortController.signal,
133
- importFunction
134
- });
135
- context = {
136
- extendedPayloads: [],
137
- runEnvironment: input.parameters.runOptions.environment === "standalone" ? _enums.RunEnvironment.DEPLOYED : _enums.RunEnvironment.IDE,
138
- timeoutInfo: {
139
- extendTimeoutCallback: async () => {
140
- if (Date.now() - timeoutTimestamp < throttleTime) return;
141
- timeoutTimestamp = Date.now();
142
- jsonUnixSocket.sendJSON({
143
- type: "extend"
144
- });
145
- }
146
- },
147
- ...(input.parameters.context ?? {}),
148
- proxy: getProxyUrlFromRunOptions(input.parameters.runOptions)
149
- };
150
- stream.write({});
151
- continue;
152
- }
153
- if (input.type === "next") {
154
- if (!generator) {
155
- throw new Error("generator not started");
156
- }
157
- stream.write({
158
- value: input.parameters.value
159
- });
160
- continue;
161
- }
182
+ await handleMessage(message);
183
+ receiveMessagesPromise = receiveMessages();
184
+ continue;
162
185
  }
163
- stream.end();
164
- client.end();
165
- }
166
- void messagesLoop();
167
- for await (const {
168
- value
169
- } of stream) {
170
186
  const {
171
- done
172
- } = await runGeneratorAndSendResult(value);
173
- if (done) break;
187
+ result
188
+ } = messageOrResult;
189
+ await handleResult(result);
174
190
  }
175
191
  if (!client.closed) {
176
192
  client.end();
@@ -0,0 +1 @@
1
+ export declare function runAutomationCLI(importFunction?: (path: string) => Promise<any>): void;
@@ -0,0 +1,210 @@
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 net = _interopRequireWildcard(require("net"));
11
+ var _zod = _interopRequireDefault(require("zod"));
12
+ var _runApi = require("../../common/runApi");
13
+ var _enums = require("../../runtime/enums");
14
+ var _unixSocket = require("../common/utils/unixSocket");
15
+ var _promises = require("timers/promises");
16
+ var _jwtTokenManager = require("../../common/jwtTokenManager");
17
+ var _formatZodError = require("../../common/formatZodError");
18
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
19
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
20
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
+ const startRunApiSchema = _zod.default.object({
22
+ type: _zod.default.literal("start"),
23
+ parameters: _runApi.runApiParametersSchema.extend({
24
+ retrieveSession: _zod.default.boolean(),
25
+ functionsToken: _zod.default.string().optional(),
26
+ context: _zod.default.object({
27
+ jobId: _zod.default.string().optional(),
28
+ jobRunId: _zod.default.string().optional(),
29
+ runId: _zod.default.string().optional(),
30
+ queueId: _zod.default.string().optional()
31
+ }).optional()
32
+ })
33
+ });
34
+ const nextRunApiSchema = _zod.default.object({
35
+ type: _zod.default.literal("next"),
36
+ parameters: _zod.default.object({
37
+ value: _zod.default.string()
38
+ })
39
+ });
40
+ const abortRunApiSchema = _zod.default.object({
41
+ type: _zod.default.literal("abort"),
42
+ parameters: _zod.default.object({}).optional()
43
+ });
44
+ const tokenUpdateSchema = _zod.default.object({
45
+ type: _zod.default.literal("tokenUpdate"),
46
+ parameters: _zod.default.object({
47
+ functionsToken: _zod.default.string()
48
+ })
49
+ });
50
+ const inputSchema = _zod.default.union([startRunApiSchema, nextRunApiSchema, abortRunApiSchema, tokenUpdateSchema]);
51
+ _dotenv.default.config({
52
+ path: `.env`
53
+ });
54
+ function runAutomationCLI(importFunction) {
55
+ _commander.program.description("run user automation and communicate using unix socket").argument("<socket-path>", "path to unix socket").action(async socketPath => {
56
+ let context;
57
+ const throttleTime = 60 * 1000;
58
+ let timeoutTimestamp = Date.now();
59
+ const client = net.createConnection(socketPath);
60
+ let generator = null;
61
+ const abortController = new AbortController();
62
+ client.on("error", err => {
63
+ var _generator;
64
+ void ((_generator = generator) === null || _generator === void 0 ? void 0 : _generator.throw(err).catch(() => undefined));
65
+ });
66
+ process.on("SIGINT", () => {
67
+ var _generator2;
68
+ void ((_generator2 = generator) === null || _generator2 === void 0 ? void 0 : _generator2.throw(new Error("Interrupted")).catch(() => undefined));
69
+ client.end();
70
+ process.exit(1);
71
+ });
72
+ process.on("SIGTERM", () => {
73
+ var _generator3;
74
+ void ((_generator3 = generator) === null || _generator3 === void 0 ? void 0 : _generator3.throw(new Error("Interrupted")).catch(() => undefined));
75
+ client.end();
76
+ process.exit(1);
77
+ });
78
+ const jsonUnixSocket = new _unixSocket.JSONUnixSocket(client);
79
+ const messagesGenerator = jsonUnixSocket.receiveJSON();
80
+ async function receiveMessages() {
81
+ const data = await messagesGenerator.next();
82
+ if (data.done) {
83
+ return;
84
+ }
85
+ const inputParseResult = inputSchema.safeParse(data.value);
86
+ if (!inputParseResult.success) {
87
+ console.error("Validation error:", (0, _formatZodError.formatZodError)(inputParseResult.error));
88
+ return {
89
+ type: "error",
90
+ error: new _runApi.InternalInvalidInputError("Invalid input", (0, _formatZodError.formatZodError)(inputParseResult.error))
91
+ };
92
+ }
93
+ const input = inputParseResult.data;
94
+ return input;
95
+ }
96
+ let resultPromise = null;
97
+ async function handleMessage(input) {
98
+ if (input.type === "abort") {
99
+ abortController.abort();
100
+ return "aborted";
101
+ }
102
+ if (input.type === "tokenUpdate") {
103
+ _jwtTokenManager.backendFunctionsTokenManager.token = input.parameters.functionsToken;
104
+ return;
105
+ }
106
+ if (input.type === "start") {
107
+ generator = (0, _runApi.runApiGenerator)({
108
+ ...input.parameters,
109
+ abortSignal: abortController.signal,
110
+ importFunction
111
+ });
112
+ context = {
113
+ extendedPayloads: [],
114
+ runEnvironment: input.parameters.runOptions.environment === "standalone" ? _enums.RunEnvironment.DEPLOYED : _enums.RunEnvironment.IDE,
115
+ timeoutInfo: {
116
+ extendTimeoutCallback: async () => {
117
+ if (Date.now() - timeoutTimestamp < throttleTime) return;
118
+ timeoutTimestamp = Date.now();
119
+ jsonUnixSocket.sendJSON({
120
+ type: "extend"
121
+ });
122
+ }
123
+ },
124
+ ...(input.parameters.context ?? {}),
125
+ proxy: getProxyUrlFromRunOptions(input.parameters.runOptions)
126
+ };
127
+ _jwtTokenManager.backendFunctionsTokenManager.token = input.parameters.functionsToken;
128
+ resultPromise = (0, _asyncLocalStorage.runWithContext)(context, async () => {
129
+ return await generator.next();
130
+ });
131
+ }
132
+ if (input.type === "next") {
133
+ if (!generator) {
134
+ throw new Error("generator not started");
135
+ }
136
+ resultPromise = (0, _asyncLocalStorage.runWithContext)(context, async () => {
137
+ return await generator.next(input.parameters.value);
138
+ });
139
+ }
140
+ }
141
+ async function handleResult(generatorResult) {
142
+ if (!generatorResult.done) {
143
+ jsonUnixSocket.sendJSON({
144
+ type: "yield",
145
+ result: generatorResult.value
146
+ });
147
+ return;
148
+ }
149
+ const result = generatorResult.value;
150
+ const success = result.isOk();
151
+ const resultToSend = success ? result.value : result.error.json;
152
+ jsonUnixSocket.sendJSON({
153
+ type: "done",
154
+ result: resultToSend,
155
+ success
156
+ });
157
+ }
158
+ let receiveMessagesPromise = receiveMessages();
159
+ while (true) {
160
+ const messageOrResult = await Promise.race([receiveMessagesPromise.then(message => ({
161
+ type: "message",
162
+ message
163
+ })), ...(resultPromise ? [resultPromise.then(result => ({
164
+ type: "result",
165
+ result
166
+ }))] : [])]);
167
+ if (messageOrResult.type === "message") {
168
+ const {
169
+ message
170
+ } = messageOrResult;
171
+ if (message === undefined) {
172
+ break;
173
+ }
174
+ if (message.type === "error") {
175
+ jsonUnixSocket.sendJSON({
176
+ type: "done",
177
+ result: message.error.json,
178
+ success: false
179
+ });
180
+ break;
181
+ }
182
+ await handleMessage(message);
183
+ receiveMessagesPromise = receiveMessages();
184
+ continue;
185
+ }
186
+ const {
187
+ result
188
+ } = messageOrResult;
189
+ await handleResult(result);
190
+ }
191
+ if (!client.closed) {
192
+ client.end();
193
+ await Promise.race([new Promise(resolve => client.once("close", resolve)), new Promise(resolve => client.once("error", resolve)), (0, _promises.setTimeout)(3000)]);
194
+ }
195
+ process.exit(0);
196
+ });
197
+ _commander.program.parse(process.argv);
198
+ }
199
+ function getProxyUrlFromRunOptions(runOptions) {
200
+ if ((runOptions === null || runOptions === void 0 ? void 0 : runOptions.environment) !== "standalone") return undefined;
201
+ const proxy = runOptions.proxy;
202
+ if (!proxy) return undefined;
203
+ const url = new URL(proxy.server);
204
+ url.username = proxy.username;
205
+ url.password = proxy.password;
206
+ return url.toString();
207
+ }
208
+ if (require.main === module) {
209
+ runAutomationCLI();
210
+ }
@@ -68,7 +68,6 @@ async function* runApiGenerator({
68
68
  const abortPromise = new Promise(resolve => {
69
69
  if (!abortSignal) return;
70
70
  abortSignal.addEventListener("abort", () => {
71
- console.log("Aborted");
72
71
  resolve(abortSymbol);
73
72
  });
74
73
  });
@@ -82,11 +81,9 @@ async function* runApiGenerator({
82
81
  }
83
82
  try {
84
83
  var _context;
85
- console.log("Saving trace");
86
84
  await ((_context = context) === null || _context === void 0 ? void 0 : _context.tracing.stop({
87
85
  path: tracing.filePath
88
86
  }));
89
- console.log("Trace saved");
90
87
  } catch (error) {
91
88
  console.log(errorMessage, error === null || error === void 0 ? void 0 : error.message);
92
89
  await (0, _fsExtra.remove)(tracing.filePath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intuned/runtime-dev",
3
- "version": "1.0.2-abort.2",
3
+ "version": "1.0.4-refactor.0",
4
4
  "description": "Intuned runtime",
5
5
  "exports": {
6
6
  ".": "./dist/index.js",