@hatchet-dev/typescript-sdk 1.13.0 → 1.14.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/clients/dispatcher/dispatcher-client.d.ts +1 -1
- package/clients/dispatcher/heartbeat/heartbeat-controller.js +13 -1
- package/clients/dispatcher/heartbeat/heartbeat-worker.js +4 -3
- package/clients/event/event-client.d.ts +1 -2
- package/clients/event/event-client.js +2 -2
- package/clients/hatchet-client/client-config.d.ts +15 -5
- package/clients/hatchet-client/client-config.js +3 -0
- package/clients/hatchet-client/hatchet-logger.js +1 -0
- package/clients/hatchet-client/index.d.ts +1 -1
- package/clients/hatchet-client/index.js +1 -1
- package/clients/listeners/durable-listener/durable-listener-client.d.ts +6 -0
- package/clients/listeners/durable-listener/durable-listener-client.js +8 -0
- package/clients/listeners/durable-listener/pooled-durable-listener-client.d.ts +9 -2
- package/clients/listeners/durable-listener/pooled-durable-listener-client.js +67 -15
- package/clients/listeners/run-listener/child-listener-client.js +1 -0
- package/clients/listeners/run-listener/pooled-child-listener-client.d.ts +10 -2
- package/clients/listeners/run-listener/pooled-child-listener-client.js +63 -7
- package/clients/rest/api.js +10 -2
- package/clients/rest/generated/Api.d.ts +2 -2
- package/clients/rest/generated/data-contracts.d.ts +6 -0
- package/index.d.ts +2 -3
- package/index.js +2 -3
- package/{examples → legacy/examples}/affinity-workers.js +2 -2
- package/{examples → legacy/examples}/bulk-fanout-trigger.js +1 -1
- package/{examples → legacy/examples}/bulk-fanout-worker.js +1 -1
- package/{examples → legacy/examples}/bulk-trigger.js +1 -1
- package/{examples → legacy/examples}/byo-logger.js +1 -1
- package/{examples → legacy/examples}/concurrency/cancel-in-progress/concurrency-event.js +1 -1
- package/{examples → legacy/examples}/concurrency/cancel-in-progress/concurrency-worker.js +1 -1
- package/{examples → legacy/examples}/concurrency/group-round-robin/concurrency-event.js +1 -1
- package/{examples → legacy/examples}/concurrency/group-round-robin/concurrency-worker-expression.js +2 -2
- package/{examples → legacy/examples}/concurrency/group-round-robin/concurrency-worker-key-fn.js +2 -2
- package/{examples → legacy/examples}/crons/cron-worker.d.ts +1 -1
- package/{examples → legacy/examples}/crons/cron-worker.js +1 -1
- package/{examples → legacy/examples}/crons/programatic-crons.js +1 -1
- package/{examples → legacy/examples}/dag-worker.js +1 -1
- package/{examples → legacy/examples}/example-event-with-results.js +2 -2
- package/{examples → legacy/examples}/example-event.js +1 -1
- package/{examples → legacy/examples}/fanout-worker.js +1 -1
- package/{examples → legacy/examples}/logger.js +1 -1
- package/{examples → legacy/examples}/manual-trigger.js +1 -1
- package/{examples → legacy/examples}/multi-workflow.js +1 -1
- package/{examples → legacy/examples}/namespaced-worker.js +1 -1
- package/{examples → legacy/examples}/on-failure.js +1 -1
- package/{examples → legacy/examples}/rate-limit/events.js +1 -1
- package/{examples → legacy/examples}/rate-limit/worker.js +2 -2
- package/{examples → legacy/examples}/retries-with-backoff.js +1 -1
- package/{examples → legacy/examples}/retries-worker.js +1 -1
- package/{examples → legacy/examples}/scheduled-runs/programatic-schedules.js +1 -1
- package/{examples → legacy/examples}/simple-worker.d.ts +1 -1
- package/{examples → legacy/examples}/simple-worker.js +1 -1
- package/{examples → legacy/examples}/sticky-trigger.js +1 -1
- package/{examples → legacy/examples}/sticky-worker-with-check.js +2 -2
- package/{examples → legacy/examples}/sticky-worker.js +2 -2
- package/{examples → legacy/examples}/stream-by-additional-meta.js +2 -2
- package/legacy/legacy-client.d.ts +30 -0
- package/legacy/legacy-client.js +67 -0
- package/legacy/legacy-transformer.d.ts +38 -0
- package/legacy/legacy-transformer.js +181 -0
- package/legacy/step.d.ts +429 -0
- package/legacy/step.js +662 -0
- package/legacy/workflow.d.ts +488 -0
- package/legacy/workflow.js +72 -0
- package/package.json +7 -7
- package/step.d.ts +1 -430
- package/step.js +6 -647
- package/util/abort-error.d.ts +38 -0
- package/util/abort-error.js +58 -0
- package/util/hatchet-promise/hatchet-promise.d.ts +8 -1
- package/util/hatchet-promise/hatchet-promise.js +3 -2
- package/util/logger/index.d.ts +1 -0
- package/util/logger/index.js +1 -0
- package/util/logger/task-run-log.d.ts +3 -0
- package/util/logger/task-run-log.js +20 -0
- package/util/sleep.d.ts +5 -2
- package/util/sleep.js +27 -4
- package/util/workflow-run-ref.d.ts +7 -1
- package/util/workflow-run-ref.js +5 -3
- package/v1/client/admin.d.ts +8 -1
- package/v1/client/admin.js +22 -6
- package/v1/client/client.d.ts +36 -18
- package/v1/client/client.interface.d.ts +9 -2
- package/v1/client/client.js +47 -27
- package/v1/client/features/crons.d.ts +3 -3
- package/v1/client/features/crons.js +2 -2
- package/v1/client/features/schedules.js +2 -2
- package/v1/client/features/workflows.d.ts +5 -5
- package/v1/client/features/workflows.js +7 -8
- package/v1/client/worker/context.d.ts +49 -10
- package/v1/client/worker/context.js +92 -20
- package/v1/client/worker/deprecated/legacy-v1-worker.d.ts +3 -3
- package/v1/client/worker/deprecated/legacy-v1-worker.js +2 -2
- package/v1/client/worker/deprecated/legacy-worker.js +9 -7
- package/v1/client/worker/slot-utils.d.ts +3 -3
- package/v1/client/worker/worker-internal.d.ts +7 -16
- package/v1/client/worker/worker-internal.js +225 -209
- package/v1/client/worker/worker.d.ts +18 -18
- package/v1/client/worker/worker.js +45 -30
- package/v1/declaration.d.ts +28 -7
- package/v1/declaration.js +56 -6
- package/v1/examples/__e2e__/harness.d.ts +19 -0
- package/v1/examples/__e2e__/harness.js +70 -0
- package/v1/examples/affinity/affinity-workers.js +4 -4
- package/v1/examples/bulk_fanout/workflow.d.ts +9 -0
- package/v1/examples/bulk_fanout/workflow.js +34 -0
- package/v1/examples/bulk_operations/workflow.d.ts +3 -0
- package/v1/examples/bulk_operations/workflow.js +44 -0
- package/v1/examples/cancellation/cancellation-workflow.d.ts +2 -0
- package/v1/examples/cancellation/cancellation-workflow.js +69 -0
- package/v1/examples/cancellation/run.js +53 -0
- package/v1/examples/cancellation/worker.d.ts +1 -0
- package/v1/examples/cancellation/worker.js +29 -0
- package/v1/examples/concurrency-types.d.ts +5 -0
- package/v1/examples/concurrency-types.js +2 -0
- package/v1/examples/concurrency_cancel_in_progress/workflow.d.ts +9 -0
- package/v1/examples/concurrency_cancel_in_progress/workflow.js +45 -0
- package/v1/examples/concurrency_cancel_newest/workflow.d.ts +9 -0
- package/v1/examples/concurrency_cancel_newest/workflow.js +45 -0
- package/v1/examples/concurrency_limit_rr/load.d.ts +1 -0
- package/v1/examples/concurrency_limit_rr/load.js +54 -0
- package/v1/examples/concurrency_limit_rr/run.d.ts +1 -0
- package/v1/examples/concurrency_limit_rr/run.js +39 -0
- package/v1/examples/concurrency_limit_rr/worker.d.ts +1 -0
- package/v1/examples/concurrency_limit_rr/worker.js +24 -0
- package/v1/examples/concurrency_limit_rr/workflow.d.ts +12 -0
- package/v1/examples/concurrency_limit_rr/workflow.js +62 -0
- package/v1/examples/concurrency_multiple_keys/workflow.d.ts +12 -0
- package/v1/examples/concurrency_multiple_keys/workflow.js +42 -0
- package/v1/examples/concurrency_workflow_level/workflow.d.ts +13 -0
- package/v1/examples/concurrency_workflow_level/workflow.js +49 -0
- package/v1/examples/conditions/complex-workflow.d.ts +1 -0
- package/v1/examples/conditions/complex-workflow.js +107 -0
- package/v1/examples/conditions/event.d.ts +1 -0
- package/v1/examples/conditions/event.js +28 -0
- package/v1/examples/conditions/run.d.ts +1 -0
- package/v1/examples/conditions/run.js +25 -0
- package/v1/examples/conditions/worker.d.ts +1 -0
- package/v1/examples/conditions/worker.js +24 -0
- package/v1/examples/conditions/workflow.d.ts +11 -0
- package/v1/examples/conditions/workflow.js +41 -0
- package/v1/examples/durable/workflow.d.ts +7 -0
- package/v1/examples/durable/workflow.js +116 -0
- package/v1/examples/durable_event/event.d.ts +1 -0
- package/v1/examples/durable_event/event.js +28 -0
- package/v1/examples/durable_event/run.d.ts +1 -0
- package/v1/examples/durable_event/run.js +30 -0
- package/v1/examples/durable_event/worker.d.ts +1 -0
- package/v1/examples/durable_event/worker.js +24 -0
- package/v1/examples/durable_event/workflow.d.ts +6 -0
- package/v1/examples/durable_event/workflow.js +46 -0
- package/v1/examples/durable_sleep/event.d.ts +1 -0
- package/v1/examples/durable_sleep/event.js +28 -0
- package/v1/examples/durable_sleep/run.d.ts +1 -0
- package/v1/examples/durable_sleep/run.js +30 -0
- package/v1/examples/durable_sleep/worker.d.ts +1 -0
- package/v1/examples/durable_sleep/worker.js +24 -0
- package/v1/examples/durable_sleep/workflow.d.ts +1 -0
- package/v1/examples/durable_sleep/workflow.js +31 -0
- package/v1/examples/e2e-worker.d.ts +1 -0
- package/v1/examples/e2e-worker.js +82 -0
- package/v1/examples/events/event.d.ts +1 -0
- package/v1/examples/events/event.js +53 -0
- package/v1/examples/events/filter.d.ts +1 -0
- package/v1/examples/events/filter.js +32 -0
- package/v1/examples/events/worker.d.ts +1 -0
- package/v1/examples/events/worker.js +24 -0
- package/v1/examples/events/workflow.d.ts +19 -0
- package/v1/examples/events/workflow.js +60 -0
- package/v1/examples/high-memory/workflow-with-child.js +1 -1
- package/v1/examples/logger/byo-logger.d.ts +1 -0
- package/v1/examples/logger/byo-logger.js +73 -0
- package/v1/examples/logger/logger.d.ts +1 -0
- package/v1/examples/logger/logger.js +46 -0
- package/v1/examples/logger/workflow.d.ts +2 -0
- package/v1/examples/logger/workflow.js +38 -0
- package/v1/examples/multiple_wf_concurrency/workflow.js +3 -3
- package/v1/examples/on_failure/workflow.d.ts +1 -0
- package/v1/examples/on_failure/workflow.js +12 -7
- package/v1/examples/priority/workflow.js +1 -1
- package/v1/examples/retries/workflow.js +2 -2
- package/v1/examples/return_exceptions/workflow.d.ts +6 -0
- package/v1/examples/return_exceptions/workflow.js +22 -0
- package/v1/examples/run_details/workflow.d.ts +4 -0
- package/v1/examples/run_details/workflow.js +65 -0
- package/v1/examples/simple/e2e-workflows.d.ts +14 -0
- package/v1/examples/simple/e2e-workflows.js +27 -0
- package/v1/examples/simple/enqueue.js +2 -2
- package/v1/examples/sticky/workflow.js +2 -2
- package/v1/examples/timeout/run.d.ts +1 -0
- package/v1/examples/timeout/run.js +30 -0
- package/v1/examples/timeout/worker.d.ts +1 -0
- package/v1/examples/timeout/worker.js +25 -0
- package/v1/examples/timeout/workflow.d.ts +10 -0
- package/v1/examples/timeout/workflow.js +56 -0
- package/v1/examples/webhooks/workflow.d.ts +5 -0
- package/v1/examples/webhooks/workflow.js +23 -0
- package/v1/index.d.ts +1 -0
- package/v1/index.js +1 -0
- package/v1/parent-run-context-vars.d.ts +6 -0
- package/v1/parent-run-context-vars.js +3 -0
- package/v1/task.d.ts +22 -7
- package/v1/task.js +4 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/workflow.d.ts +3 -491
- package/workflow.js +12 -62
- package/clients/hatchet-client/hatchet-client.d.ts +0 -35
- package/clients/hatchet-client/hatchet-client.js +0 -108
- package/clients/worker/handler.d.ts +0 -50
- package/clients/worker/handler.js +0 -214
- package/clients/worker/index.d.ts +0 -1
- package/clients/worker/index.js +0 -17
- package/clients/worker/worker.d.ts +0 -59
- package/clients/worker/worker.js +0 -568
- package/examples/webhooks.js +0 -45
- /package/{examples → legacy/examples}/affinity-workers.d.ts +0 -0
- /package/{examples → legacy/examples}/bulk-fanout-trigger.d.ts +0 -0
- /package/{examples → legacy/examples}/bulk-fanout-worker.d.ts +0 -0
- /package/{examples → legacy/examples}/bulk-trigger.d.ts +0 -0
- /package/{examples → legacy/examples}/byo-logger.d.ts +0 -0
- /package/{examples → legacy/examples}/concurrency/cancel-in-progress/concurrency-event.d.ts +0 -0
- /package/{examples → legacy/examples}/concurrency/cancel-in-progress/concurrency-worker.d.ts +0 -0
- /package/{examples → legacy/examples}/concurrency/group-round-robin/concurrency-event.d.ts +0 -0
- /package/{examples → legacy/examples}/concurrency/group-round-robin/concurrency-worker-expression.d.ts +0 -0
- /package/{examples → legacy/examples}/concurrency/group-round-robin/concurrency-worker-key-fn.d.ts +0 -0
- /package/{examples → legacy/examples}/crons/programatic-crons.d.ts +0 -0
- /package/{examples → legacy/examples}/dag-worker.d.ts +0 -0
- /package/{examples → legacy/examples}/example-event-with-results.d.ts +0 -0
- /package/{examples → legacy/examples}/example-event.d.ts +0 -0
- /package/{examples → legacy/examples}/fanout-worker.d.ts +0 -0
- /package/{examples → legacy/examples}/logger.d.ts +0 -0
- /package/{examples → legacy/examples}/manual-trigger.d.ts +0 -0
- /package/{examples → legacy/examples}/multi-workflow.d.ts +0 -0
- /package/{examples → legacy/examples}/namespaced-worker.d.ts +0 -0
- /package/{examples → legacy/examples}/on-failure.d.ts +0 -0
- /package/{examples → legacy/examples}/rate-limit/events.d.ts +0 -0
- /package/{examples → legacy/examples}/rate-limit/worker.d.ts +0 -0
- /package/{examples → legacy/examples}/retries-with-backoff.d.ts +0 -0
- /package/{examples → legacy/examples}/retries-worker.d.ts +0 -0
- /package/{examples → legacy/examples}/scheduled-runs/programatic-schedules.d.ts +0 -0
- /package/{examples → legacy/examples}/sticky-trigger.d.ts +0 -0
- /package/{examples → legacy/examples}/sticky-worker-with-check.d.ts +0 -0
- /package/{examples → legacy/examples}/sticky-worker.d.ts +0 -0
- /package/{examples → legacy/examples}/stream-by-additional-meta.d.ts +0 -0
- /package/{examples/webhooks.d.ts → v1/examples/cancellation/run.d.ts} +0 -0
package/legacy/step.js
ADDED
|
@@ -0,0 +1,662 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
45
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
46
|
+
};
|
|
47
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
+
exports.V0DurableContext = exports.V0Context = exports.V0ContextWorker = exports.CreateStepSchema = exports.DesiredWorkerLabelSchema = exports.CreateRateLimitSchema = void 0;
|
|
49
|
+
exports.mapRateLimit = mapRateLimit;
|
|
50
|
+
/* eslint-disable no-underscore-dangle */
|
|
51
|
+
/* eslint-disable max-classes-per-file */
|
|
52
|
+
const hatchet_error_1 = __importDefault(require("../util/errors/hatchet-error"));
|
|
53
|
+
const z = __importStar(require("zod"));
|
|
54
|
+
const parse_1 = require("../util/parse");
|
|
55
|
+
const workflows_1 = require("../protoc/workflows");
|
|
56
|
+
const v1_1 = require("../v1");
|
|
57
|
+
const declaration_1 = require("../v1/declaration");
|
|
58
|
+
const conditions_1 = require("../v1/conditions");
|
|
59
|
+
const condition_1 = require("../protoc/v1/shared/condition");
|
|
60
|
+
const transformer_1 = require("../v1/conditions/transformer");
|
|
61
|
+
const apply_namespace_1 = require("../util/apply-namespace");
|
|
62
|
+
exports.CreateRateLimitSchema = z.object({
|
|
63
|
+
key: z.string().optional(),
|
|
64
|
+
staticKey: z.string().optional(),
|
|
65
|
+
dynamicKey: z.string().optional(),
|
|
66
|
+
units: z.union([z.number().min(0), z.string()]),
|
|
67
|
+
limit: z.union([z.number().min(1), z.string()]).optional(),
|
|
68
|
+
duration: z.nativeEnum(workflows_1.RateLimitDuration).optional(),
|
|
69
|
+
});
|
|
70
|
+
exports.DesiredWorkerLabelSchema = z
|
|
71
|
+
.union([
|
|
72
|
+
z.string(),
|
|
73
|
+
z.number().int(),
|
|
74
|
+
z.object({
|
|
75
|
+
value: z.union([z.string(), z.number()]),
|
|
76
|
+
required: z.boolean().optional(),
|
|
77
|
+
weight: z.number().int().optional(),
|
|
78
|
+
// (optional) comparator for the label
|
|
79
|
+
// if not provided, the default is EQUAL
|
|
80
|
+
// desired COMPARATOR actual (i.e. desired > actual for GREATER_THAN)
|
|
81
|
+
comparator: z.nativeEnum(workflows_1.WorkerLabelComparator).optional(),
|
|
82
|
+
}),
|
|
83
|
+
])
|
|
84
|
+
.optional();
|
|
85
|
+
exports.CreateStepSchema = z.object({
|
|
86
|
+
name: z.string(),
|
|
87
|
+
parents: z.array(z.string()).optional(),
|
|
88
|
+
timeout: z.string().optional(),
|
|
89
|
+
retries: z.number().optional(),
|
|
90
|
+
rate_limits: z.array(exports.CreateRateLimitSchema).optional(),
|
|
91
|
+
worker_labels: z.record(z.lazy(() => exports.DesiredWorkerLabelSchema)).optional(),
|
|
92
|
+
backoff: z
|
|
93
|
+
.object({
|
|
94
|
+
factor: z.number().optional(),
|
|
95
|
+
maxSeconds: z.number().optional(),
|
|
96
|
+
})
|
|
97
|
+
.optional(),
|
|
98
|
+
});
|
|
99
|
+
class V0ContextWorker {
|
|
100
|
+
constructor(worker) {
|
|
101
|
+
this.worker = worker;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Gets the ID of the worker.
|
|
105
|
+
* @returns The ID of the worker.
|
|
106
|
+
*/
|
|
107
|
+
id() {
|
|
108
|
+
return this.worker.workerId;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Checks if the worker has a registered workflow.
|
|
112
|
+
* @param workflowName - The name of the workflow to check.
|
|
113
|
+
* @returns True if the workflow is registered, otherwise false.
|
|
114
|
+
*/
|
|
115
|
+
hasWorkflow(workflowName) {
|
|
116
|
+
return !!this.worker.workflow_registry.find((workflow) => 'id' in workflow ? workflow.id === workflowName : workflow.name === workflowName);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Gets the current state of the worker labels.
|
|
120
|
+
* @returns The labels of the worker.
|
|
121
|
+
*/
|
|
122
|
+
labels() {
|
|
123
|
+
return this.worker.labels;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Upserts the a set of labels on the worker.
|
|
127
|
+
* @param labels - The labels to upsert.
|
|
128
|
+
* @returns A promise that resolves when the labels have been upserted.
|
|
129
|
+
*/
|
|
130
|
+
upsertLabels(labels) {
|
|
131
|
+
return this.worker.upsertLabels(labels);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
exports.V0ContextWorker = V0ContextWorker;
|
|
135
|
+
class V0Context {
|
|
136
|
+
constructor(action, client, worker) {
|
|
137
|
+
// @deprecated use ctx.abortController instead
|
|
138
|
+
this.controller = new AbortController();
|
|
139
|
+
this.overridesData = {};
|
|
140
|
+
this.spawnIndex = 0;
|
|
141
|
+
try {
|
|
142
|
+
const data = (0, parse_1.parseJSON)(action.actionPayload);
|
|
143
|
+
this.data = data;
|
|
144
|
+
this.action = action;
|
|
145
|
+
this.v0 = client;
|
|
146
|
+
this.worker = new V0ContextWorker(worker);
|
|
147
|
+
this.logger = client.config.logger(`Context Logger`, client.config.log_level);
|
|
148
|
+
// if this is a getGroupKeyRunId, the data is the workflow input
|
|
149
|
+
if (action.getGroupKeyRunId !== '') {
|
|
150
|
+
this.input = data;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
this.input = data.input;
|
|
154
|
+
}
|
|
155
|
+
this.overridesData = data.overrides || {};
|
|
156
|
+
}
|
|
157
|
+
catch (e) {
|
|
158
|
+
throw new hatchet_error_1.default(`Could not parse payload: ${e.message}`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
get abortController() {
|
|
162
|
+
return this.controller;
|
|
163
|
+
}
|
|
164
|
+
get cancelled() {
|
|
165
|
+
return this.controller.signal.aborted;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Retrieves the output of a parent task.
|
|
169
|
+
* @param parentTask - The a CreateTaskOpts or string of the parent task name.
|
|
170
|
+
* @returns The output of the specified parent task.
|
|
171
|
+
* @throws An error if the task output is not found.
|
|
172
|
+
*/
|
|
173
|
+
parentOutput(parentTask) {
|
|
174
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
175
|
+
// NOTE: parentOutput is async since we plan on potentially making this a cacheable server call
|
|
176
|
+
if (typeof parentTask === 'string') {
|
|
177
|
+
return this.stepOutput(parentTask);
|
|
178
|
+
}
|
|
179
|
+
return this.stepOutput(parentTask.name);
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Get the output of a task.
|
|
184
|
+
* @param task - The name of the task to get the output for.
|
|
185
|
+
* @returns The output of the task.
|
|
186
|
+
* @throws An error if the task output is not found.
|
|
187
|
+
* @deprecated use ctx.parentOutput instead
|
|
188
|
+
*/
|
|
189
|
+
stepOutput(step) {
|
|
190
|
+
if (!this.data.parents) {
|
|
191
|
+
throw new hatchet_error_1.default('Parent task outputs not found');
|
|
192
|
+
}
|
|
193
|
+
if (!this.data.parents[step]) {
|
|
194
|
+
throw new hatchet_error_1.default(`Output for parent task '${step}' not found`);
|
|
195
|
+
}
|
|
196
|
+
return this.data.parents[step];
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Returns errors from any task runs in the workflow.
|
|
200
|
+
* @returns A record mapping task names to error messages.
|
|
201
|
+
* @throws A warning if no errors are found (this method should be used in on-failure tasks).
|
|
202
|
+
* @deprecated use ctx.errors() instead
|
|
203
|
+
*/
|
|
204
|
+
stepRunErrors() {
|
|
205
|
+
return this.errors();
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Returns errors from any task runs in the workflow.
|
|
209
|
+
* @returns A record mapping task names to error messages.
|
|
210
|
+
* @throws A warning if no errors are found (this method should be used in on-failure tasks).
|
|
211
|
+
*/
|
|
212
|
+
errors() {
|
|
213
|
+
const errors = this.data.step_run_errors || {};
|
|
214
|
+
if (Object.keys(errors).length === 0) {
|
|
215
|
+
this.logger.error('No run errors found. `ctx.errors` is intended to be run in an on-failure task, and will only work on engine versions more recent than v0.53.10');
|
|
216
|
+
}
|
|
217
|
+
return errors;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Gets the dag conditional triggers for the current workflow run.
|
|
221
|
+
* @returns The triggers for the current workflow.
|
|
222
|
+
*/
|
|
223
|
+
triggers() {
|
|
224
|
+
return this.data.triggers;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Determines if the workflow was triggered by an event.
|
|
228
|
+
* @returns True if the workflow was triggered by an event, otherwise false.
|
|
229
|
+
*/
|
|
230
|
+
triggeredByEvent() {
|
|
231
|
+
var _a;
|
|
232
|
+
return ((_a = this.data) === null || _a === void 0 ? void 0 : _a.triggered_by) === 'event';
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Gets the input data for the current workflow.
|
|
236
|
+
* @returns The input data for the workflow.
|
|
237
|
+
* @deprecated use task input parameter instead
|
|
238
|
+
*/
|
|
239
|
+
workflowInput() {
|
|
240
|
+
return this.input;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Gets the name of the current workflow.
|
|
244
|
+
* @returns The name of the workflow.
|
|
245
|
+
*/
|
|
246
|
+
workflowName() {
|
|
247
|
+
return this.action.jobName;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Gets the user data associated with the workflow.
|
|
251
|
+
* @returns The user data.
|
|
252
|
+
*/
|
|
253
|
+
userData() {
|
|
254
|
+
var _a;
|
|
255
|
+
return (_a = this.data) === null || _a === void 0 ? void 0 : _a.user_data;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Gets the name of the current task.
|
|
259
|
+
* @returns The name of the task.
|
|
260
|
+
* @deprecated use ctx.taskName instead
|
|
261
|
+
*/
|
|
262
|
+
stepName() {
|
|
263
|
+
return this.taskName();
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Gets the name of the current running task.
|
|
267
|
+
* @returns The name of the task.
|
|
268
|
+
*/
|
|
269
|
+
taskName() {
|
|
270
|
+
return this.action.taskName;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Gets the ID of the current workflow run.
|
|
274
|
+
* @returns The workflow run ID.
|
|
275
|
+
*/
|
|
276
|
+
workflowRunId() {
|
|
277
|
+
return this.action.workflowRunId;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Gets the ID of the current task run.
|
|
281
|
+
* @returns The task run ID.
|
|
282
|
+
*/
|
|
283
|
+
taskRunExternalId() {
|
|
284
|
+
return this.action.taskRunExternalId;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Gets the ID of the current task run.
|
|
288
|
+
* @returns The task run ID.
|
|
289
|
+
* @deprecated use taskRunExternalId instead
|
|
290
|
+
*/
|
|
291
|
+
taskRunId() {
|
|
292
|
+
return this.action.taskRunExternalId;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Gets the number of times the current task has been retried.
|
|
296
|
+
* @returns The retry count.
|
|
297
|
+
*/
|
|
298
|
+
retryCount() {
|
|
299
|
+
return this.action.retryCount;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Logs a message from the current task.
|
|
303
|
+
* @param message - The message to log.
|
|
304
|
+
* @param level - The log level (optional).
|
|
305
|
+
*/
|
|
306
|
+
log(message, level) {
|
|
307
|
+
const { taskRunExternalId } = this.action;
|
|
308
|
+
if (!taskRunExternalId) {
|
|
309
|
+
// log a warning
|
|
310
|
+
this.logger.warn('cannot log from context without stepRunId');
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
this.v0.event.putLog(taskRunExternalId, message, level, this.retryCount());
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Refreshes the timeout for the current task.
|
|
317
|
+
* @param incrementBy - The interval by which to increment the timeout.
|
|
318
|
+
* The interval should be specified in the format of '10s' for 10 seconds, '1m' for 1 minute, or '1d' for 1 day.
|
|
319
|
+
*/
|
|
320
|
+
refreshTimeout(incrementBy) {
|
|
321
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
322
|
+
const { taskRunExternalId } = this.action;
|
|
323
|
+
if (!taskRunExternalId) {
|
|
324
|
+
// log a warning
|
|
325
|
+
this.logger.warn('cannot refresh timeout from context without stepRunId');
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
yield this.v0.dispatcher.refreshTimeout(incrementBy, taskRunExternalId);
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Releases a worker slot for a task run such that the worker can pick up another task.
|
|
333
|
+
* Note: this is an advanced feature that may lead to unexpected behavior if used incorrectly.
|
|
334
|
+
* @returns A promise that resolves when the slot has been released.
|
|
335
|
+
*/
|
|
336
|
+
releaseSlot() {
|
|
337
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
338
|
+
yield this.v0.dispatcher.client.releaseSlot({
|
|
339
|
+
taskRunExternalId: this.action.taskRunExternalId,
|
|
340
|
+
});
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Streams data from the current task run.
|
|
345
|
+
* @param data - The data to stream (string or binary).
|
|
346
|
+
* @returns A promise that resolves when the data has been streamed.
|
|
347
|
+
*/
|
|
348
|
+
putStream(data) {
|
|
349
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
350
|
+
const { taskRunExternalId } = this.action;
|
|
351
|
+
if (!taskRunExternalId) {
|
|
352
|
+
// log a warning
|
|
353
|
+
this.logger.warn('cannot log from context without stepRunId');
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
yield this.v0.event.putStream(taskRunExternalId, data, undefined);
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Runs multiple children workflows in parallel without waiting for their results.
|
|
361
|
+
* @param children - An array of objects containing the workflow name, input data, and options for each workflow.
|
|
362
|
+
* @returns A list of workflow run references to the enqueued runs.
|
|
363
|
+
*/
|
|
364
|
+
bulkRunNoWaitChildren(children) {
|
|
365
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
366
|
+
return this.spawnWorkflows(children);
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Runs multiple children workflows in parallel and waits for all results.
|
|
371
|
+
* @param children - An array of objects containing the workflow name, input data, and options for each workflow.
|
|
372
|
+
* @returns A list of results from the children workflows.
|
|
373
|
+
*/
|
|
374
|
+
bulkRunChildren(children) {
|
|
375
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
376
|
+
const runs = yield this.bulkRunNoWaitChildren(children);
|
|
377
|
+
return Promise.all(runs.map((run) => run.output));
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Spawns multiple workflows.
|
|
382
|
+
*
|
|
383
|
+
* @param workflows - An array of objects containing the workflow name, input data, and options for each workflow.
|
|
384
|
+
* @returns A list of references to the spawned workflow runs.
|
|
385
|
+
* @deprecated Use bulkRunNoWaitChildren or bulkRunChildren instead.
|
|
386
|
+
*/
|
|
387
|
+
spawnWorkflows(workflows) {
|
|
388
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
389
|
+
const { workflowRunId, taskRunExternalId } = this.action;
|
|
390
|
+
const workflowRuns = workflows.map(({ workflow, input, options }) => {
|
|
391
|
+
let workflowName;
|
|
392
|
+
if (typeof workflow === 'string') {
|
|
393
|
+
workflowName = workflow;
|
|
394
|
+
}
|
|
395
|
+
else {
|
|
396
|
+
workflowName = workflow.id;
|
|
397
|
+
}
|
|
398
|
+
const name = (0, apply_namespace_1.applyNamespace)(workflowName, this.v0.config.namespace);
|
|
399
|
+
const opts = options || {};
|
|
400
|
+
const { sticky } = opts;
|
|
401
|
+
if (sticky && !this.worker.hasWorkflow(name)) {
|
|
402
|
+
throw new hatchet_error_1.default(`Cannot run with sticky: workflow ${name} is not registered on the worker`);
|
|
403
|
+
}
|
|
404
|
+
const resp = {
|
|
405
|
+
workflowName: name,
|
|
406
|
+
input,
|
|
407
|
+
options: Object.assign(Object.assign({}, opts), { parentId: workflowRunId, parentStepRunId: taskRunExternalId, childIndex: this.spawnIndex, desiredWorkerId: sticky ? this.worker.id() : undefined }),
|
|
408
|
+
};
|
|
409
|
+
this.spawnIndex += 1;
|
|
410
|
+
return resp;
|
|
411
|
+
});
|
|
412
|
+
try {
|
|
413
|
+
const batchSize = 100;
|
|
414
|
+
let resp = [];
|
|
415
|
+
for (let i = 0; i < workflowRuns.length; i += batchSize) {
|
|
416
|
+
const batch = workflowRuns.slice(i, i + batchSize);
|
|
417
|
+
const batchResp = yield this.v0.admin.runWorkflows(batch);
|
|
418
|
+
resp = resp.concat(batchResp);
|
|
419
|
+
}
|
|
420
|
+
const res = [];
|
|
421
|
+
resp.forEach((ref, index) => {
|
|
422
|
+
const wf = workflows[index].workflow;
|
|
423
|
+
if (wf instanceof declaration_1.TaskWorkflowDeclaration) {
|
|
424
|
+
// eslint-disable-next-line no-param-reassign
|
|
425
|
+
ref._standaloneTaskName = wf._standalone_task_name;
|
|
426
|
+
}
|
|
427
|
+
res.push(ref);
|
|
428
|
+
});
|
|
429
|
+
return resp;
|
|
430
|
+
}
|
|
431
|
+
catch (e) {
|
|
432
|
+
throw new hatchet_error_1.default(e.message);
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Runs a new workflow and waits for its result.
|
|
438
|
+
*
|
|
439
|
+
* @param workflow - The workflow to run (name, Workflow instance, or WorkflowV1 instance).
|
|
440
|
+
* @param input - The input data for the workflow.
|
|
441
|
+
* @param options - An options object containing key, sticky, priority, and additionalMetadata.
|
|
442
|
+
* @returns The result of the workflow.
|
|
443
|
+
*/
|
|
444
|
+
runChild(workflow, input, options) {
|
|
445
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
446
|
+
const run = yield this.spawnWorkflow(workflow, input, options);
|
|
447
|
+
return run.output;
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Enqueues a new workflow without waiting for its result.
|
|
452
|
+
*
|
|
453
|
+
* @param workflow - The workflow to enqueue (name, Workflow instance, or WorkflowV1 instance).
|
|
454
|
+
* @param input - The input data for the workflow.
|
|
455
|
+
* @param options - An options object containing key, sticky, priority, and additionalMetadata.
|
|
456
|
+
* @returns A reference to the spawned workflow run.
|
|
457
|
+
*/
|
|
458
|
+
runNoWaitChild(workflow, input, options) {
|
|
459
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
460
|
+
return this.spawnWorkflow(workflow, input, options);
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Spawns a new workflow.
|
|
465
|
+
*
|
|
466
|
+
* @param workflow - The workflow to spawn (name, Workflow instance, or WorkflowV1 instance).
|
|
467
|
+
* @param input - The input data for the workflow.
|
|
468
|
+
* @param options - Additional options for spawning the workflow.
|
|
469
|
+
* @returns A reference to the spawned workflow run.
|
|
470
|
+
* @deprecated Use runChild or runNoWaitChild instead.
|
|
471
|
+
*/
|
|
472
|
+
spawnWorkflow(workflow, input, options) {
|
|
473
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
474
|
+
const { workflowRunId, taskRunExternalId } = this.action;
|
|
475
|
+
let workflowName = '';
|
|
476
|
+
if (typeof workflow === 'string') {
|
|
477
|
+
workflowName = workflow;
|
|
478
|
+
}
|
|
479
|
+
else {
|
|
480
|
+
workflowName = workflow.id;
|
|
481
|
+
}
|
|
482
|
+
const name = (0, apply_namespace_1.applyNamespace)(workflowName, this.v0.config.namespace);
|
|
483
|
+
const opts = options || {};
|
|
484
|
+
const { sticky } = opts;
|
|
485
|
+
if (sticky && !this.worker.hasWorkflow(name)) {
|
|
486
|
+
throw new hatchet_error_1.default(`cannot run with sticky: workflow ${name} is not registered on the worker`);
|
|
487
|
+
}
|
|
488
|
+
try {
|
|
489
|
+
const resp = yield this.v0.admin.runWorkflow(name, input, Object.assign({ parentId: workflowRunId, parentStepRunId: taskRunExternalId, childIndex: this.spawnIndex, desiredWorkerId: sticky ? this.worker.id() : undefined }, opts));
|
|
490
|
+
this.spawnIndex += 1;
|
|
491
|
+
if (workflow instanceof declaration_1.TaskWorkflowDeclaration) {
|
|
492
|
+
resp._standaloneTaskName = workflow._standalone_task_name;
|
|
493
|
+
}
|
|
494
|
+
return resp;
|
|
495
|
+
}
|
|
496
|
+
catch (e) {
|
|
497
|
+
throw new hatchet_error_1.default(e.message);
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Retrieves additional metadata associated with the current workflow run.
|
|
503
|
+
* @returns A record of metadata key-value pairs.
|
|
504
|
+
*/
|
|
505
|
+
additionalMetadata() {
|
|
506
|
+
if (!this.action.additionalMetadata) {
|
|
507
|
+
return {};
|
|
508
|
+
}
|
|
509
|
+
// parse the additional metadata
|
|
510
|
+
const res = (0, parse_1.parseJSON)(this.action.additionalMetadata);
|
|
511
|
+
return res;
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Gets the index of this workflow if it was spawned as part of a bulk operation.
|
|
515
|
+
* @returns The child index number, or undefined if not set.
|
|
516
|
+
*/
|
|
517
|
+
childIndex() {
|
|
518
|
+
return this.action.childWorkflowIndex;
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Gets the key associated with this workflow if it was spawned as a child workflow.
|
|
522
|
+
* @returns The child key, or undefined if not set.
|
|
523
|
+
*/
|
|
524
|
+
childKey() {
|
|
525
|
+
return this.action.childWorkflowKey;
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* Gets the ID of the parent workflow run if this workflow was spawned as a child.
|
|
529
|
+
* @returns The parent workflow run ID, or undefined if not a child workflow.
|
|
530
|
+
*/
|
|
531
|
+
parentWorkflowRunId() {
|
|
532
|
+
return this.action.parentWorkflowRunId;
|
|
533
|
+
}
|
|
534
|
+
priority() {
|
|
535
|
+
switch (this.action.priority) {
|
|
536
|
+
case 1:
|
|
537
|
+
return v1_1.Priority.LOW;
|
|
538
|
+
case 2:
|
|
539
|
+
return v1_1.Priority.MEDIUM;
|
|
540
|
+
case 3:
|
|
541
|
+
return v1_1.Priority.HIGH;
|
|
542
|
+
default:
|
|
543
|
+
return undefined;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
exports.V0Context = V0Context;
|
|
548
|
+
class V0DurableContext extends V0Context {
|
|
549
|
+
constructor() {
|
|
550
|
+
super(...arguments);
|
|
551
|
+
this.waitKey = 0;
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Pauses execution for the specified duration.
|
|
555
|
+
* Duration is "global" meaning it will wait in real time regardless of transient failures like worker restarts.
|
|
556
|
+
* @param duration - The duration to sleep for.
|
|
557
|
+
* @returns A promise that resolves when the sleep duration has elapsed.
|
|
558
|
+
*/
|
|
559
|
+
sleepFor(duration, readableDataKey) {
|
|
560
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
561
|
+
return this.waitFor({ sleepFor: duration, readableDataKey });
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* Pauses execution until the specified conditions are met.
|
|
566
|
+
* Conditions are "global" meaning they will wait in real time regardless of transient failures like worker restarts.
|
|
567
|
+
* @param conditions - The conditions to wait for.
|
|
568
|
+
* @returns A promise that resolves with the event that satisfied the conditions.
|
|
569
|
+
*/
|
|
570
|
+
waitFor(conditions) {
|
|
571
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
572
|
+
const pbConditions = (0, transformer_1.conditionsToPb)((0, conditions_1.Render)(condition_1.Action.CREATE, conditions));
|
|
573
|
+
// eslint-disable-next-line no-plusplus
|
|
574
|
+
const key = `waitFor-${this.waitKey++}`;
|
|
575
|
+
yield this.v0.durableListener.registerDurableEvent({
|
|
576
|
+
taskId: this.action.taskRunExternalId,
|
|
577
|
+
signalKey: key,
|
|
578
|
+
sleepConditions: pbConditions.sleepConditions,
|
|
579
|
+
userEventConditions: pbConditions.userEventConditions,
|
|
580
|
+
});
|
|
581
|
+
const listener = this.v0.durableListener.subscribe({
|
|
582
|
+
taskId: this.action.taskRunExternalId,
|
|
583
|
+
signalKey: key,
|
|
584
|
+
});
|
|
585
|
+
const event = yield listener.get();
|
|
586
|
+
// Convert event.data from Uint8Array to string if needed
|
|
587
|
+
const eventData = event.data instanceof Uint8Array ? new TextDecoder().decode(event.data) : event.data;
|
|
588
|
+
const res = JSON.parse(eventData);
|
|
589
|
+
return res.CREATE;
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
exports.V0DurableContext = V0DurableContext;
|
|
594
|
+
function mapRateLimit(limits) {
|
|
595
|
+
if (!limits)
|
|
596
|
+
return [];
|
|
597
|
+
return limits.map((l) => {
|
|
598
|
+
let key = l.staticKey;
|
|
599
|
+
const keyExpression = l.dynamicKey;
|
|
600
|
+
if (l.key !== undefined) {
|
|
601
|
+
// eslint-disable-next-line no-console
|
|
602
|
+
console.warn('key is deprecated and will be removed in a future release, please use staticKey instead');
|
|
603
|
+
key = l.key;
|
|
604
|
+
}
|
|
605
|
+
if (keyExpression !== undefined) {
|
|
606
|
+
if (key !== undefined) {
|
|
607
|
+
throw new Error('Cannot have both static key and dynamic key set');
|
|
608
|
+
}
|
|
609
|
+
key = keyExpression;
|
|
610
|
+
if (!validateCelExpression(keyExpression)) {
|
|
611
|
+
throw new Error(`Invalid CEL expression: ${keyExpression}`);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
if (key === undefined) {
|
|
615
|
+
throw new Error(`Invalid key`);
|
|
616
|
+
}
|
|
617
|
+
let units;
|
|
618
|
+
let unitsExpression;
|
|
619
|
+
if (typeof l.units === 'number') {
|
|
620
|
+
units = l.units;
|
|
621
|
+
}
|
|
622
|
+
else {
|
|
623
|
+
if (!validateCelExpression(l.units)) {
|
|
624
|
+
throw new Error(`Invalid CEL expression: ${l.units}`);
|
|
625
|
+
}
|
|
626
|
+
unitsExpression = l.units;
|
|
627
|
+
}
|
|
628
|
+
let limitExpression;
|
|
629
|
+
if (l.limit !== undefined) {
|
|
630
|
+
if (typeof l.limit === 'number') {
|
|
631
|
+
limitExpression = `${l.limit}`;
|
|
632
|
+
}
|
|
633
|
+
else {
|
|
634
|
+
if (!validateCelExpression(l.limit)) {
|
|
635
|
+
throw new Error(`Invalid CEL expression: ${l.limit}`);
|
|
636
|
+
}
|
|
637
|
+
limitExpression = l.limit;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
if (keyExpression !== undefined && limitExpression === undefined) {
|
|
641
|
+
throw new Error('CEL based keys requires limit to be set');
|
|
642
|
+
}
|
|
643
|
+
if (limitExpression === undefined) {
|
|
644
|
+
limitExpression = `-1`;
|
|
645
|
+
}
|
|
646
|
+
return {
|
|
647
|
+
key,
|
|
648
|
+
keyExpr: keyExpression,
|
|
649
|
+
units,
|
|
650
|
+
unitsExpr: unitsExpression,
|
|
651
|
+
limitValuesExpr: limitExpression,
|
|
652
|
+
duration: l.duration,
|
|
653
|
+
};
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
// Helper function to validate CEL expressions
|
|
657
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
658
|
+
function validateCelExpression(expr) {
|
|
659
|
+
// This is a placeholder. In a real implementation, you'd need to use a CEL parser or validator.
|
|
660
|
+
// For now, we'll just return true to mimic the behavior.
|
|
661
|
+
return true;
|
|
662
|
+
}
|