@intuned/runtime-dev 0.1.0-test.37 → 0.1.0-test.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/InterfaceTemplate/index.ts +1 -5
  2. package/Intuned.json +1 -1
  3. package/WebTemplate/api.ts +92 -90
  4. package/WebTemplate/controllers/async.ts +48 -52
  5. package/WebTemplate/controllers/authSessions/check.ts +4 -3
  6. package/WebTemplate/controllers/authSessions/create.ts +7 -5
  7. package/WebTemplate/controllers/authSessions/resumeOperation.ts +1 -1
  8. package/WebTemplate/controllers/runApi/helpers.ts +7 -12
  9. package/WebTemplate/index.playwright.ts +42 -32
  10. package/WebTemplate/jobs.ts +2 -13
  11. package/WebTemplate/utils.ts +1 -48
  12. package/api/test2.ts +5 -7
  13. package/auth-sessions/check.ts +1 -3
  14. package/auth-sessions/create.ts +10 -10
  15. package/bin/intuned-ts-check +1 -1
  16. package/dist/commands/api/run.js +4 -6
  17. package/dist/commands/auth-sessions/run-check.js +2 -8
  18. package/dist/commands/auth-sessions/run-create.js +5 -10
  19. package/dist/commands/build.js +13 -16
  20. package/dist/commands/common/getDefaultExportFromFile.d.ts +1 -0
  21. package/dist/commands/common/{tsNodeImport.js → getDefaultExportFromFile.js} +5 -6
  22. package/dist/commands/common/utils/settings.js +5 -5
  23. package/dist/commands/common/utils/webTemplate.d.ts +1 -0
  24. package/dist/commands/common/utils/{template.js → webTemplate.js} +7 -7
  25. package/dist/commands/interface/run.d.ts +1 -1
  26. package/dist/commands/interface/run.js +106 -131
  27. package/dist/commands/ts-check.js +7 -9
  28. package/dist/common/getPlaywrightConstructs.js +1 -5
  29. package/dist/common/runApi/errors.d.ts +3 -8
  30. package/dist/common/runApi/errors.js +3 -26
  31. package/dist/common/runApi/index.d.ts +1 -1
  32. package/dist/common/runApi/index.js +55 -33
  33. package/dist/common/runApi/types.d.ts +61 -277
  34. package/dist/common/runApi/types.js +2 -26
  35. package/dist/runtime/executionHelpers.test.js +6 -6
  36. package/package.json +3 -5
  37. package/testing +0 -0
  38. package/tsconfig.json +2 -1
  39. package/InterfaceTemplate/utils.ts +0 -257
  40. package/dist/commands/common/tsNodeImport.d.ts +0 -1
  41. package/dist/commands/common/utils/template.d.ts +0 -2
  42. package/dist/common/formatZodError.d.ts +0 -2
  43. package/dist/common/formatZodError.js +0 -18
@@ -1,257 +0,0 @@
1
- import * as playwright from "@intuned/playwright-core";
2
- import { Handler, Response } from "@tinyhttp/app";
3
- import * as path from "path";
4
- import { getExecutionContext } from "@intuned/runtime";
5
- import { setTimeout } from "timers/promises";
6
-
7
- export class FunctionNotFoundError extends Error {
8
- functionName: string;
9
- path: string;
10
- constructor(functionName: string, path: string) {
11
- const message = `function ${functionName} not found in ${path}`;
12
- super(message);
13
- this.functionName = functionName;
14
- this.path = path;
15
- this.name = "FunctionNotFound";
16
- Object.setPrototypeOf(this, FunctionNotFoundError.prototype);
17
- }
18
- }
19
-
20
- export function getIsRetryableError(error: any) {
21
- if (error?.message) {
22
- return error.message.includes("ERR_NETWORK_CHANGED");
23
- }
24
- return false;
25
- }
26
-
27
- export function getErrorResponse(error: any): {
28
- status: number;
29
- body: any;
30
- } {
31
- if (error instanceof FunctionNotFoundError) {
32
- return {
33
- status: 404,
34
- body: {
35
- message: error.message,
36
- error: error.name,
37
- },
38
- };
39
- }
40
- if (error instanceof playwright.errors.TimeoutError) {
41
- return {
42
- status: 500,
43
- body: { message: error.message, error: error.name },
44
- };
45
- }
46
- /**
47
- * here we use error.constructor.name instead of error instanceof RunError
48
- * this is because the error is thrown by importing the runner from the api code on intuned app
49
- * the definition of class RunError which is imported from here is different than the class RunError
50
- * imported from the user.
51
- */
52
- if (error.constructor.name === "RunError") {
53
- return {
54
- status: 200,
55
- body: {
56
- status: error.options.status_code ?? 500,
57
- message: error.message,
58
- error: error.options.error_code ?? error.name,
59
- intunedOptions: error.options,
60
- },
61
- };
62
- }
63
- return {
64
- status: 500,
65
- body: { error: error?.name ?? error, message: error?.message },
66
- };
67
- }
68
-
69
- export function handlePlaywrightExecutionError(error: any, res: Response) {
70
- const { status, body } = getErrorResponse(error);
71
- return res.status(status).json(body);
72
- }
73
-
74
- type EventTraceEvent = {
75
- type: "event";
76
- time: number;
77
- class: string;
78
- method: string;
79
- params: any;
80
- pageId?: string;
81
- };
82
-
83
- type AppHandlerParams = Parameters<Handler>;
84
-
85
- export function errorRetryMiddleware(handler: Handler) {
86
- return async (...args: AppHandlerParams) => {
87
- let attempts = 1;
88
- const [req, res, next] = args;
89
- // eslint-disable-next-line no-constant-condition
90
- while (true) {
91
- try {
92
- await handler(req, res, next);
93
- break;
94
- } catch (error) {
95
- console.log(error?.name, error?.message);
96
- if (!getIsRetryableError(error) || attempts >= 3) {
97
- return handlePlaywrightExecutionError(error, res);
98
- }
99
- attempts++;
100
- }
101
- }
102
- };
103
- }
104
-
105
- export function getTraceFilePath(runId: string, attemptNumber?: string) {
106
- const fileName = `${runId}${attemptNumber ? `_${attemptNumber}` : ""}`;
107
- return path.join(process.env.TRACES_DIRECTORY ?? "", `${fileName}.zip`);
108
- }
109
-
110
- export interface ProxyConfig {
111
- username: string;
112
- server: string;
113
- password: string;
114
- }
115
-
116
- export function proxyToUrl(proxy: ProxyConfig) {
117
- const url = new URL(proxy.server);
118
- url.username = proxy.username;
119
- url.password = proxy.password;
120
- return url.toString();
121
- }
122
-
123
- export function isJobRunMachine() {
124
- return process.env.JOB_ID && process.env.JOB_RUN_ID;
125
- }
126
-
127
- export function isHeadless() {
128
- return process.env.INTUNED_PLAYWRIGHT_HEADLESS !== "0";
129
- }
130
-
131
- export abstract class AsyncRunEndpointController {
132
- private static activeRequests: Set<string> = new Set();
133
-
134
- static isRunning(taskToken: string) {
135
- return this.activeRequests.has(taskToken);
136
- }
137
-
138
- static addRequest(taskToken: string) {
139
- this.activeRequests.add(taskToken);
140
- }
141
-
142
- static removeRequest(taskToken: string) {
143
- this.activeRequests.delete(taskToken);
144
- }
145
-
146
- static get activeRequestsCount() {
147
- return this.activeRequests.size;
148
- }
149
- }
150
-
151
- export async function waitWithExtendableTimeout<T>({
152
- initialTimeout,
153
- promise,
154
- abortController,
155
- }: {
156
- initialTimeout: number;
157
- promise: Promise<T>;
158
- abortController?: AbortController;
159
- }): Promise<
160
- | {
161
- timedOut: true;
162
- }
163
- | {
164
- timedOut: false;
165
- result: T;
166
- }
167
- > {
168
- const context = getExecutionContext()!;
169
- if (context.timeoutInfo) {
170
- context.timeoutInfo.timeoutTimestamp = Date.now() + initialTimeout;
171
- }
172
- const timerSymbol = Symbol("timer");
173
- if (!context) {
174
- const result = await Promise.race([
175
- promise,
176
- setTimeout(initialTimeout, timerSymbol),
177
- ]);
178
- if (result === timerSymbol) {
179
- abortController?.abort("Timed out");
180
- return { timedOut: true };
181
- }
182
- return { timedOut: false, result };
183
- }
184
-
185
- let taskTimeout = initialTimeout;
186
- while (true as boolean) {
187
- const result = await Promise.race([
188
- promise,
189
- setTimeout(taskTimeout, timerSymbol),
190
- ]);
191
-
192
- if (result !== timerSymbol) {
193
- break;
194
- }
195
-
196
- const remainingTime =
197
- (context.timeoutInfo?.timeoutTimestamp ?? 0) - Date.now();
198
- if (remainingTime < 0) {
199
- abortController?.abort("Timed out");
200
- return { timedOut: true };
201
- }
202
-
203
- taskTimeout = remainingTime;
204
- }
205
-
206
- return {
207
- timedOut: false,
208
- result: await promise,
209
- };
210
- }
211
-
212
- export async function importModule(path: string) {
213
- const [folderName, ...functionNameParts] = path.split("/");
214
- const functionNameDepth = functionNameParts.length;
215
-
216
- // string literals should be inline
217
- // currently we support only 5 levels of depth
218
- // rollup dynamic import does not support multiple levels of dynamic imports so we need to specify the possible paths explicitly
219
- try {
220
- let imported: any = undefined;
221
- switch (functionNameDepth) {
222
- case 1:
223
- imported = await import(`./${folderName}/${functionNameParts[0]}.ts`);
224
- break;
225
- case 2:
226
- imported = await import(
227
- `./${folderName}/${functionNameParts[0]}/${functionNameParts[1]}.ts`
228
- );
229
- break;
230
- case 3:
231
- imported = await import(
232
- `./${folderName}/${functionNameParts[0]}/${functionNameParts[1]}/${functionNameParts[2]}.ts`
233
- );
234
- break;
235
- case 4:
236
- imported = await import(
237
- `./${folderName}/${functionNameParts[0]}/${functionNameParts[1]}/${functionNameParts[2]}/${functionNameParts[3]}.ts`
238
- );
239
- break;
240
- case 5:
241
- imported = await import(
242
- `./${folderName}/${functionNameParts[0]}/${functionNameParts[1]}/${functionNameParts[2]}/${functionNameParts[3]}/${functionNameParts[4]}.ts`
243
- );
244
- break;
245
- default:
246
- throw new Error(
247
- "intuned supports maximum 5 levels of depth in the api folder"
248
- );
249
- }
250
- return imported;
251
- } catch (error: any) {
252
- if (error.message.includes("Unknown variable dynamic import")) {
253
- throw new FunctionNotFoundError("", path);
254
- }
255
- throw error;
256
- }
257
- }
@@ -1 +0,0 @@
1
- export declare function tsNodeImport(apiName: string): Promise<any>;
@@ -1,2 +0,0 @@
1
- export type TemplateName = "WebTemplate" | "InterfaceTemplate";
2
- export declare const moveTemplateFiles: (templateName: TemplateName) => Promise<void>;
@@ -1,2 +0,0 @@
1
- import { ZodError } from "zod";
2
- export declare function formatZodError(zodError: ZodError): string[];
@@ -1,18 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.formatZodError = formatZodError;
7
- function formatZodError(zodError) {
8
- const formattedErrors = zodError.errors.map(error => {
9
- const path = error.path.map(segment => {
10
- return typeof segment === "number" ? `[${segment}]` : segment;
11
- }).join(".");
12
- if (path) {
13
- return `${path} is invalid - ${error.message}`;
14
- }
15
- return error.message;
16
- });
17
- return formattedErrors;
18
- }