@intuned/runtime-dev 1.0.4-stuck.1 → 1.0.6-cli-auth.0.0.1

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 (95) hide show
  1. package/InterfaceTemplate/utils.ts +53 -31
  2. package/bin/cli-build +3 -0
  3. package/bin/create-auth-session +3 -0
  4. package/bin/deploy +3 -0
  5. package/bin/init +3 -0
  6. package/bin/run-api +3 -0
  7. package/dist/commands/api/run.js +2 -2
  8. package/dist/commands/auth-sessions/load.js +1 -1
  9. package/dist/commands/auth-sessions/run-check.js +2 -2
  10. package/dist/commands/auth-sessions/run-create.js +2 -2
  11. package/dist/commands/browser/save-state.js +1 -1
  12. package/dist/commands/browser/start-browser.js +1 -1
  13. package/dist/commands/build.js +4 -4
  14. package/dist/commands/cli-auth-sessions/check.d.ts +0 -0
  15. package/dist/commands/cli-auth-sessions/check.js +1 -0
  16. package/dist/commands/cli-auth-sessions/create.d.ts +2 -0
  17. package/dist/commands/cli-auth-sessions/create.js +28 -0
  18. package/dist/commands/cli-auth-sessions/utils.d.ts +4 -0
  19. package/dist/commands/cli-auth-sessions/utils.js +55 -0
  20. package/dist/commands/cli-build/cli-build.d.ts +2 -0
  21. package/dist/commands/cli-build/cli-build.js +20 -0
  22. package/dist/commands/common/browserUtils.d.ts +1 -1
  23. package/dist/commands/common/browserUtils.js +3 -3
  24. package/dist/commands/common/getFirstLineNumber.test.js +2 -2
  25. package/dist/commands/common/projectExclusions.d.ts +2 -0
  26. package/dist/commands/common/projectExclusions.js +8 -0
  27. package/dist/commands/common/tsNodeImport.d.ts +2 -1
  28. package/dist/commands/common/tsNodeImport.js +20 -6
  29. package/dist/commands/common/utils/fileUtils.js +2 -2
  30. package/dist/commands/common/utils/settings.js +1 -1
  31. package/dist/commands/common/utils/template.d.ts +1 -1
  32. package/dist/commands/common/utils/template.js +1 -1
  33. package/dist/commands/deploy/deploy.d.ts +2 -0
  34. package/dist/commands/deploy/deploy.js +47 -0
  35. package/dist/commands/deploy/utils.d.ts +16 -0
  36. package/dist/commands/deploy/utils.js +350 -0
  37. package/dist/commands/init/init.d.ts +2 -0
  38. package/dist/commands/init/init.js +22 -0
  39. package/dist/commands/init/utils.d.ts +9 -0
  40. package/dist/commands/init/utils.js +179 -0
  41. package/dist/commands/interface/run.js +141 -94
  42. package/dist/commands/run-api-cli/run-api.d.ts +2 -0
  43. package/dist/commands/run-api-cli/run-api.js +48 -0
  44. package/dist/commands/run-api-cli/utils.d.ts +10 -0
  45. package/dist/commands/run-api-cli/utils.js +98 -0
  46. package/dist/commands/ts-check.js +2 -2
  47. package/dist/common/Logger/Logger/index.js +1 -1
  48. package/dist/common/Logger/index.js +1 -1
  49. package/dist/common/asyncLocalStorage/index.d.ts +1 -1
  50. package/dist/common/asyncLocalStorage/index.js +2 -2
  51. package/dist/common/cli/cliReadme.d.ts +1 -0
  52. package/dist/common/cli/cliReadme.js +69 -0
  53. package/dist/common/cli/constants.d.ts +24 -0
  54. package/dist/common/cli/constants.js +30 -0
  55. package/dist/common/cli/types.d.ts +52 -0
  56. package/dist/common/cli/types.js +7 -0
  57. package/dist/common/cli/utils.d.ts +6 -0
  58. package/dist/common/cli/utils.js +35 -0
  59. package/dist/common/contextStorageStateHelpers.d.ts +20 -13
  60. package/dist/common/contextStorageStateHelpers.js +69 -42
  61. package/dist/common/getPlaywrightConstructs.d.ts +1 -1
  62. package/dist/common/getPlaywrightConstructs.js +5 -5
  63. package/dist/common/jwtTokenManager.js +2 -2
  64. package/dist/common/runApi/errors.d.ts +0 -11
  65. package/dist/common/runApi/errors.js +2 -41
  66. package/dist/common/runApi/index.d.ts +2 -1
  67. package/dist/common/runApi/index.js +11 -4
  68. package/dist/common/runApi/types.d.ts +82 -76
  69. package/dist/common/runApi/types.js +1 -1
  70. package/dist/common/settingsSchema.js +1 -1
  71. package/dist/common/telemetry.js +1 -1
  72. package/package.json +14 -6
  73. package/WebTemplate/accessKeyHelpers.ts +0 -28
  74. package/WebTemplate/api.ts +0 -139
  75. package/WebTemplate/app.ts +0 -18
  76. package/WebTemplate/controllers/async.ts +0 -142
  77. package/WebTemplate/controllers/authSessions/check.ts +0 -61
  78. package/WebTemplate/controllers/authSessions/create.ts +0 -101
  79. package/WebTemplate/controllers/authSessions/index.ts +0 -41
  80. package/WebTemplate/controllers/authSessions/killOperation.ts +0 -35
  81. package/WebTemplate/controllers/authSessions/resumeOperation.ts +0 -56
  82. package/WebTemplate/controllers/authSessions/store.ts +0 -19
  83. package/WebTemplate/controllers/controllers.ts +0 -73
  84. package/WebTemplate/controllers/runApi/helpers.ts +0 -128
  85. package/WebTemplate/controllers/runApi/index.ts +0 -68
  86. package/WebTemplate/controllers/runApi/types.ts +0 -13
  87. package/WebTemplate/controllers/traces.ts +0 -151
  88. package/WebTemplate/features.ts +0 -8
  89. package/WebTemplate/headers.ts +0 -6
  90. package/WebTemplate/index.playwright.ts +0 -47
  91. package/WebTemplate/index.vanilla.ts +0 -26
  92. package/WebTemplate/jobs.ts +0 -356
  93. package/WebTemplate/shutdown.ts +0 -64
  94. package/WebTemplate/utils.ts +0 -257
  95. package/WebTemplate.zip +0 -0
@@ -1,139 +0,0 @@
1
- import {
2
- errorRetryMiddleware,
3
- proxyToUrl,
4
- ProxyConfig,
5
- isJobRunMachine,
6
- } from "./utils";
7
- import { accessKeyValidatorMiddleware } from "./accessKeyHelpers";
8
- import { runWithContext } from "@intuned/runtime";
9
- import {
10
- createAuthSessionController,
11
- createAuthSessionAsyncController,
12
- resumeAuthSessionOperationController,
13
- resumeAuthSessionOperationAsyncController,
14
- killAuthSessionOperationController,
15
- checkAuthSessionController,
16
- checkAuthSessionAsyncController,
17
- } from "./controllers/authSessions";
18
- import { app } from "./app";
19
- import {
20
- getActiveAsyncEndpointController,
21
- runApiAsyncController,
22
- runApiController,
23
- } from "./controllers/runApi";
24
- import {
25
- uploadTraceController,
26
- deleteTraceController,
27
- } from "./controllers/traces";
28
- import {
29
- RUN_ID_HEADER,
30
- JOB_ID_HEADER,
31
- JOB_RUN_ID_HEADER,
32
- QUEUE_ID_HEADER,
33
- } from "./headers";
34
- import { FEATURES } from "./features";
35
-
36
- if (!isJobRunMachine()) {
37
- app.use((req, _, next) => {
38
- const runId = req.headers[RUN_ID_HEADER] as string;
39
- const jobId = req.headers[JOB_ID_HEADER] as string;
40
- const jobRunId = req.headers[JOB_RUN_ID_HEADER] as string;
41
- const queueId = req.headers[QUEUE_ID_HEADER] as string;
42
- const proxy = req?.body?.proxy
43
- ? (proxyToUrl(req.body.proxy as ProxyConfig) as string)
44
- : undefined;
45
- const contextData = {
46
- runId: runId ?? "",
47
- jobId,
48
- jobRunId,
49
- queueId,
50
- proxy,
51
- ...(req?.body?.executionContext ?? {}),
52
- };
53
- runWithContext(contextData, next);
54
- });
55
-
56
- app.get(
57
- "/api/protected/health",
58
- accessKeyValidatorMiddleware,
59
- async (req, res) => {
60
- res.status(200).json({ status: "ok" });
61
- }
62
- );
63
-
64
- app.post(
65
- "/api/auth-session/create",
66
- accessKeyValidatorMiddleware,
67
- errorRetryMiddleware(createAuthSessionController)
68
- );
69
-
70
- app.post(
71
- "/api/auth-session-async/create",
72
- accessKeyValidatorMiddleware,
73
- errorRetryMiddleware(createAuthSessionAsyncController)
74
- );
75
-
76
- app.post(
77
- "/api/auth-session/check",
78
- accessKeyValidatorMiddleware,
79
- errorRetryMiddleware(checkAuthSessionController)
80
- );
81
-
82
- app.post(
83
- "/api/auth-session-async/check",
84
- accessKeyValidatorMiddleware,
85
- errorRetryMiddleware(checkAuthSessionAsyncController)
86
- );
87
-
88
- app.post(
89
- "/api/auth-session/kill",
90
- accessKeyValidatorMiddleware,
91
- errorRetryMiddleware(killAuthSessionOperationController)
92
- );
93
-
94
- app.post(
95
- "/api/auth-session/resume",
96
- accessKeyValidatorMiddleware,
97
- errorRetryMiddleware(resumeAuthSessionOperationController)
98
- );
99
-
100
- app.post(
101
- "/api/auth-session-async/resume",
102
- accessKeyValidatorMiddleware,
103
- errorRetryMiddleware(resumeAuthSessionOperationAsyncController)
104
- );
105
-
106
- app.post(
107
- "/api/run/*",
108
- accessKeyValidatorMiddleware,
109
- errorRetryMiddleware(runApiController)
110
- );
111
-
112
- app.post(
113
- "/api/run-async/start/*",
114
- accessKeyValidatorMiddleware,
115
- errorRetryMiddleware(runApiAsyncController) // todo: this probably needs changing
116
- );
117
-
118
- app.get(
119
- "/api/run-async/count",
120
- accessKeyValidatorMiddleware,
121
- getActiveAsyncEndpointController
122
- );
123
-
124
- app.post(
125
- "/api/trace/upload/:runId",
126
- accessKeyValidatorMiddleware,
127
- uploadTraceController
128
- );
129
-
130
- app.post(
131
- "/api/trace/delete/:runId",
132
- accessKeyValidatorMiddleware,
133
- deleteTraceController
134
- );
135
- }
136
-
137
- app.get("/api/features", accessKeyValidatorMiddleware, async (_, res) => {
138
- res.status(200).json({ features: FEATURES });
139
- });
@@ -1,18 +0,0 @@
1
- import { App } from "@tinyhttp/app";
2
- import { json as parser } from "milliparsec";
3
-
4
- export const port = process.env.PORT ? parseInt(process.env.PORT) : 4000;
5
-
6
- export const app = new App();
7
-
8
- app.use("/", parser());
9
-
10
- // add header with flyio instance id where the request is served from
11
- app.use((_, res, next) => {
12
- res.setHeader("fly-instance-id", process.env.FLY_ALLOC_ID ?? "");
13
- next();
14
- });
15
-
16
- app.get("/api/health", async (req, res) => {
17
- res.status(200).json({ status: "ok" });
18
- });
@@ -1,142 +0,0 @@
1
- import { AsyncRunEndpointController, getErrorResponse } from "../utils";
2
- import {
3
- backendFunctionsTokenManager,
4
- callBackendFunctionWithToken,
5
- } from "@intuned/runtime/dist/common/jwtTokenManager";
6
- import retry from "async-retry";
7
- import { ShutdownController } from "../shutdown";
8
- import { Handler } from "@tinyhttp/app";
9
- import { RequestHandler } from "./controllers";
10
-
11
- export async function runEndpointAsync<_Parameters extends any[], _Result>(
12
- handler: RequestHandler<_Parameters, _Result>,
13
- parameters: _Parameters,
14
- endpointParameters: {
15
- functionsToken: string;
16
- taskToken: string;
17
- startTime: number;
18
- // only used for logging
19
- requestId?: string;
20
- }
21
- ): Promise<{
22
- status: number;
23
- body: _Result;
24
- }> {
25
- const {
26
- functionsToken,
27
- taskToken,
28
- startTime,
29
- requestId = "endpoint",
30
- } = endpointParameters;
31
- AsyncRunEndpointController.addRequest(taskToken);
32
-
33
- backendFunctionsTokenManager.token = functionsToken;
34
-
35
- let status: number, body: _Result;
36
- try {
37
- console.log("Running", requestId, "in async mode");
38
- ({ status, body } = await handler(...parameters));
39
- console.log("Finished running", requestId, "in async mode");
40
- } catch (e: any) {
41
- console.error(`Error running ${requestId} in async mode:`, e);
42
- ({ status, body } = getErrorResponse(e));
43
- }
44
-
45
- try {
46
- // report result
47
- console.log("Reporting result for", requestId);
48
- try {
49
- // `retry` will keep retrying any errors unless they are thrown through calling `bail`
50
- await retry(
51
- async (bail: (e: Error) => void) => {
52
- const response = await callBackendFunctionWithToken(
53
- "run/reportResult",
54
- {
55
- method: "POST",
56
- headers: {
57
- "Content-Type": "application/json",
58
- "fly-instance-id": process.env.FLY_ALLOC_ID ?? "",
59
- },
60
- body: JSON.stringify({
61
- status,
62
- body,
63
- startTime,
64
- taskToken,
65
- }),
66
- }
67
- ).catch((e) => {
68
- const message = `Reporting result failed for ${requestId}, error: ${e.message}`;
69
- console.error(message);
70
- throw e;
71
- });
72
- if (!response.ok) {
73
- // Do not retry unauthenticated, forbidden, not found or too large responses
74
- if ([401, 403, 404, 413].includes(response.status)) {
75
- const message = `Reporting result failed for ${requestId} (non-retryable), status ${response.status}`;
76
- console.error(message);
77
- bail(new Error(message));
78
- }
79
- // retry other errors (such as 502)
80
- const message = `Reporting result failed for ${requestId}, status ${response.status}`;
81
- console.error(message);
82
- throw new Error(message);
83
- }
84
-
85
- return response;
86
- },
87
- {
88
- retries: 5,
89
- factor: 2,
90
- maxTimeout: 1000 * 60, // 1 minute
91
- minTimeout: 1000 * 5, // 5 seconds
92
- }
93
- );
94
- console.log("Reported result for", requestId);
95
- } catch (e: any) {
96
- // ignore
97
- }
98
- } finally {
99
- AsyncRunEndpointController.removeRequest(taskToken);
100
- }
101
-
102
- await ShutdownController.instance.checkForShutdown();
103
- return { status, body };
104
- }
105
-
106
- export type AsyncEndpointBody<T = unknown> = T & {
107
- functionsToken: string;
108
- taskToken: string;
109
- startTime: number;
110
- };
111
-
112
- export function makeAsyncEndpointController<_Parameters extends any[]>({
113
- requestId,
114
- handler,
115
- parameters,
116
- }: {
117
- requestId: string | ((req: any) => string);
118
- handler: RequestHandler<_Parameters, any>;
119
- parameters: _Parameters | ((req: any) => _Parameters);
120
- }): Handler {
121
- return async (req, res) => {
122
- const { taskToken, startTime, functionsToken } =
123
- req.body as AsyncEndpointBody<_Parameters>;
124
- if (AsyncRunEndpointController.isRunning(taskToken)) {
125
- res.status(409).json({ error: "Already running" });
126
- return;
127
- }
128
-
129
- void runEndpointAsync(
130
- handler,
131
- typeof parameters === "function" ? parameters(req) : parameters,
132
- {
133
- taskToken,
134
- startTime,
135
- functionsToken,
136
- requestId: typeof requestId === "function" ? requestId(req) : requestId,
137
- }
138
- );
139
-
140
- res.status(202).json({});
141
- };
142
- }
@@ -1,61 +0,0 @@
1
- import { readJSON } from "fs-extra";
2
- import { isHeadless } from "../../utils";
3
- import { runApi } from "@intuned/runtime/dist/common/runApi";
4
- import { importModule } from "../../utils";
5
-
6
- export async function checkAuthSession({
7
- proxy,
8
- session,
9
- }: {
10
- mode: "async" | "sync";
11
- session: any;
12
- proxy?: {
13
- server: string;
14
- username: string;
15
- password: string;
16
- };
17
- }): Promise<{
18
- status: number;
19
- body: any;
20
- }> {
21
- const isAuthSessionEnabled = (await readJSON("./Intuned.json")).authSessions
22
- ?.enabled;
23
-
24
- if (!isAuthSessionEnabled) {
25
- return {
26
- status: 400,
27
- body: {
28
- error: "Invalid Request",
29
- message: "auth sessions are not enabled",
30
- },
31
- };
32
- }
33
-
34
- const result = await runApi<boolean>({
35
- automationFunction: {
36
- name: "auth-sessions/check",
37
- },
38
- runOptions: {
39
- environment: "standalone",
40
- headless: isHeadless(),
41
- proxy,
42
- },
43
- auth: {
44
- session: {
45
- type: "state",
46
- state: session,
47
- },
48
- runCheck: false,
49
- },
50
- importFunction: importModule,
51
- });
52
- if (result.isErr()) {
53
- return result.error.apiResponse;
54
- }
55
- return {
56
- status: 200,
57
- body: {
58
- result: result.value.result,
59
- },
60
- };
61
- }
@@ -1,101 +0,0 @@
1
- import { authSessionsContextsStore } from "./store";
2
- import { getTraceFilePath, isHeadless, importModule } from "../../utils";
3
- import * as fs from "fs-extra";
4
- import { runApiGenerator } from "@intuned/runtime/dist/common/runApi";
5
- import type { RequestMoreInfoDetails } from "@intuned/runtime/dist/runtime";
6
-
7
- export async function createAuthSession({
8
- parameters,
9
- operationId,
10
- proxy,
11
- saveTrace,
12
- }: {
13
- operationId: string;
14
- mode: "async" | "sync";
15
- parameters: object | null | undefined;
16
- proxy?: {
17
- server: string;
18
- username: string;
19
- password: string;
20
- };
21
- saveTrace: boolean;
22
- }) {
23
- const isAuthSessionEnabled = (await fs.readJSON("./Intuned.json"))
24
- .authSessions?.enabled;
25
-
26
- if (!isAuthSessionEnabled) {
27
- return {
28
- status: 400,
29
- body: {
30
- error: "Invalid Request",
31
- message: "auth sessions are not enabled",
32
- },
33
- };
34
- }
35
-
36
- if (authSessionsContextsStore.has(operationId)) {
37
- return {
38
- status: 400,
39
- body: {
40
- error: "Invalid Request",
41
- message:
42
- "operation id already exists, please use a different one, or kill the existing operation, and try again",
43
- },
44
- };
45
- }
46
- const headless = isHeadless();
47
-
48
- const abortController = new AbortController();
49
-
50
- const createGenerator = runApiGenerator<
51
- void,
52
- RequestMoreInfoDetails,
53
- unknown
54
- >({
55
- automationFunction: {
56
- name: "auth-sessions/create",
57
- params: parameters,
58
- },
59
- tracing: saveTrace
60
- ? {
61
- enabled: true,
62
- filePath: getTraceFilePath(operationId),
63
- }
64
- : { enabled: false },
65
- runOptions: {
66
- environment: "standalone",
67
- headless,
68
- proxy,
69
- },
70
- abortSignal: abortController.signal,
71
- retrieveSession: true,
72
- importFunction: importModule,
73
- });
74
-
75
- const result = await createGenerator.next();
76
-
77
- if (result.done === true) {
78
- const r = result.value;
79
-
80
- if (r.isErr()) {
81
- return r.error.apiResponse;
82
- }
83
-
84
- return {
85
- status: 200,
86
- body: { ...r.value.session, status: 200 },
87
- };
88
- }
89
-
90
- authSessionsContextsStore.set(operationId, {
91
- done: false,
92
- generator: createGenerator,
93
- requestInfo: result.value,
94
- abortController,
95
- });
96
-
97
- return {
98
- status: 200,
99
- body: result.value,
100
- };
101
- }
@@ -1,41 +0,0 @@
1
- import { createAuthSession } from "./create";
2
- import { makeEndpointControllers } from "../controllers";
3
- import { checkAuthSession } from "./check";
4
- import { killAuthSessionOperation } from "./killOperation";
5
- import { resumeAuthSessionOperation } from "./resumeOperation";
6
-
7
- export const {
8
- sync: createAuthSessionController,
9
- async: createAuthSessionAsyncController,
10
- } = makeEndpointControllers({
11
- requestId: (req) => `createAuthSession ${req.body.operationId}`,
12
- handler: createAuthSession,
13
- parameters: (req): [any] => [req.body],
14
- });
15
-
16
- export const {
17
- sync: checkAuthSessionController,
18
- async: checkAuthSessionAsyncController,
19
- } = makeEndpointControllers({
20
- requestId: "checkAuthSession",
21
- handler: checkAuthSession,
22
- parameters: (req): [any] => [req.body],
23
- });
24
-
25
- export const {
26
- sync: killAuthSessionOperationController,
27
- async: killAuthSessionOperationAsyncController,
28
- } = makeEndpointControllers({
29
- requestId: (req) => `killAuthSessionOperation ${req.body.operationId}`,
30
- handler: killAuthSessionOperation,
31
- parameters: (req): [any] => [req.body],
32
- });
33
-
34
- export const {
35
- sync: resumeAuthSessionOperationController,
36
- async: resumeAuthSessionOperationAsyncController,
37
- } = makeEndpointControllers({
38
- requestId: (req) => `resumeAuthSessionOperation ${req.body.operationId}`,
39
- handler: resumeAuthSessionOperation,
40
- parameters: (req): [any] => [req.body],
41
- });
@@ -1,35 +0,0 @@
1
- import { authSessionsContextsStore } from "./store";
2
-
3
- export async function killAuthSessionOperation({
4
- operationId,
5
- }: {
6
- operationId: string;
7
- }): Promise<{
8
- status: number;
9
- body: any;
10
- }> {
11
- const operation = authSessionsContextsStore.get(operationId);
12
- if (!operation) {
13
- return {
14
- status: 200,
15
- body: {
16
- done: true,
17
- },
18
- };
19
- }
20
-
21
- try {
22
- operation.abortController.abort();
23
- authSessionsContextsStore.delete(operationId);
24
-
25
- return { status: 200, body: { done: true } };
26
- } catch (error) {
27
- return {
28
- status: 500,
29
- body: {
30
- error,
31
- message: "the server failed to kill the operation",
32
- },
33
- };
34
- }
35
- }
@@ -1,56 +0,0 @@
1
- import { authSessionsContextsStore } from "./store";
2
-
3
- export async function resumeAuthSessionOperation({
4
- operationId,
5
- input,
6
- }: {
7
- operationId: string;
8
- input: string;
9
- }) {
10
- const operation = authSessionsContextsStore.get(operationId);
11
- if (!operation) {
12
- return {
13
- status: 404,
14
- body: {
15
- error: "Invalid Request",
16
- message: "operation not found",
17
- },
18
- };
19
- }
20
-
21
- if (
22
- operation.requestInfo.requestType === "multiple_choice" &&
23
- !operation.requestInfo.choices.includes(input)
24
- ) {
25
- return {
26
- status: 400,
27
- body: {
28
- error: "Invalid Request",
29
- message: "input does not match any of the choices",
30
- },
31
- };
32
- }
33
-
34
- const result = await operation.generator.next(input);
35
-
36
- if (result.done === true) {
37
- authSessionsContextsStore.delete(operationId);
38
- if (result.value.isErr()) {
39
- return result.value.error.apiResponse;
40
- }
41
- return {
42
- status: 200,
43
- body: result.value.value.session,
44
- };
45
- }
46
-
47
- authSessionsContextsStore.set(operationId, {
48
- ...operation,
49
- requestInfo: result.value,
50
- });
51
-
52
- return {
53
- status: 200,
54
- body: result.value,
55
- };
56
- }
@@ -1,19 +0,0 @@
1
- import type { RequestMoreInfoDetails } from "@intuned/runtime/dist/runtime";
2
- import type {
3
- RunApiResult,
4
- RunApiResultWithSessionOk,
5
- } from "@intuned/runtime/dist/common/runApi";
6
-
7
- export const authSessionsContextsStore = new Map<
8
- string,
9
- {
10
- done: boolean | undefined;
11
- generator: AsyncGenerator<
12
- RequestMoreInfoDetails,
13
- RunApiResult<void, RunApiResultWithSessionOk>,
14
- string
15
- >;
16
- requestInfo: RequestMoreInfoDetails;
17
- abortController: AbortController;
18
- }
19
- >();
@@ -1,73 +0,0 @@
1
- import { AsyncRunEndpointController } from "../utils";
2
- import { Handler } from "@tinyhttp/app";
3
- import { AsyncEndpointBody, runEndpointAsync } from "./async";
4
-
5
- export type RequestHandler<P extends any[], R> = (...parameters: P) => Promise<{
6
- status: number;
7
- body: R;
8
- }>;
9
-
10
- export function makeSyncEndpointController<_Parameters extends any[], _Result>({
11
- handler,
12
- parameters,
13
- }: {
14
- handler: RequestHandler<_Parameters, any>;
15
- parameters: _Parameters | ((req: any) => _Parameters);
16
- }): Handler {
17
- return async (req, res) => {
18
- const { status, body } = await handler(
19
- ...(typeof parameters === "function" ? parameters(req) : parameters)
20
- );
21
- res.status(status).json(body);
22
- };
23
- }
24
-
25
- export function makeAsyncEndpointController<_Parameters extends any[]>({
26
- requestId,
27
- handler,
28
- parameters,
29
- }: {
30
- requestId: string | ((req: any) => string);
31
- handler: RequestHandler<_Parameters, any>;
32
- parameters: _Parameters | ((req: any) => _Parameters);
33
- }): Handler {
34
- return async (req, res) => {
35
- const { taskToken, startTime, functionsToken } =
36
- req.body as AsyncEndpointBody<_Parameters>;
37
- if (AsyncRunEndpointController.isRunning(taskToken)) {
38
- res.status(409).json({ error: "Already running" });
39
- return;
40
- }
41
-
42
- void runEndpointAsync(
43
- handler,
44
- typeof parameters === "function" ? parameters(req) : parameters,
45
- {
46
- taskToken,
47
- startTime,
48
- functionsToken,
49
- requestId: typeof requestId === "function" ? requestId(req) : requestId,
50
- }
51
- );
52
-
53
- res.status(202).json({});
54
- };
55
- }
56
-
57
- export function makeEndpointControllers<_Parameters extends any[]>({
58
- requestId,
59
- handler,
60
- parameters,
61
- }: {
62
- requestId: string | ((req: any) => string);
63
- handler: RequestHandler<_Parameters, any>;
64
- parameters: _Parameters | ((req: any) => _Parameters);
65
- }): {
66
- sync: Handler;
67
- async: Handler;
68
- } {
69
- return {
70
- sync: makeSyncEndpointController({ handler, parameters }),
71
- async: makeAsyncEndpointController({ requestId, handler, parameters }),
72
- };
73
- }