@hatchet-dev/typescript-sdk 1.13.1 → 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.
Files changed (245) hide show
  1. package/clients/dispatcher/dispatcher-client.d.ts +1 -1
  2. package/clients/dispatcher/heartbeat/heartbeat-controller.js +13 -1
  3. package/clients/dispatcher/heartbeat/heartbeat-worker.js +4 -3
  4. package/clients/event/event-client.d.ts +1 -2
  5. package/clients/event/event-client.js +2 -2
  6. package/clients/hatchet-client/client-config.d.ts +15 -5
  7. package/clients/hatchet-client/client-config.js +3 -0
  8. package/clients/hatchet-client/hatchet-logger.js +1 -0
  9. package/clients/hatchet-client/index.d.ts +1 -1
  10. package/clients/hatchet-client/index.js +1 -1
  11. package/clients/listeners/durable-listener/durable-listener-client.d.ts +6 -0
  12. package/clients/listeners/durable-listener/durable-listener-client.js +8 -0
  13. package/clients/listeners/durable-listener/pooled-durable-listener-client.d.ts +9 -2
  14. package/clients/listeners/durable-listener/pooled-durable-listener-client.js +67 -15
  15. package/clients/listeners/run-listener/child-listener-client.js +1 -0
  16. package/clients/listeners/run-listener/pooled-child-listener-client.d.ts +10 -2
  17. package/clients/listeners/run-listener/pooled-child-listener-client.js +63 -7
  18. package/clients/rest/api.js +10 -2
  19. package/clients/rest/generated/Api.d.ts +2 -2
  20. package/clients/rest/generated/data-contracts.d.ts +6 -0
  21. package/index.d.ts +2 -3
  22. package/index.js +2 -3
  23. package/{examples → legacy/examples}/affinity-workers.js +2 -2
  24. package/{examples → legacy/examples}/bulk-fanout-trigger.js +1 -1
  25. package/{examples → legacy/examples}/bulk-fanout-worker.js +1 -1
  26. package/{examples → legacy/examples}/bulk-trigger.js +1 -1
  27. package/{examples → legacy/examples}/byo-logger.js +1 -1
  28. package/{examples → legacy/examples}/concurrency/cancel-in-progress/concurrency-event.js +1 -1
  29. package/{examples → legacy/examples}/concurrency/cancel-in-progress/concurrency-worker.js +1 -1
  30. package/{examples → legacy/examples}/concurrency/group-round-robin/concurrency-event.js +1 -1
  31. package/{examples → legacy/examples}/concurrency/group-round-robin/concurrency-worker-expression.js +2 -2
  32. package/{examples → legacy/examples}/concurrency/group-round-robin/concurrency-worker-key-fn.js +2 -2
  33. package/{examples → legacy/examples}/crons/cron-worker.d.ts +1 -1
  34. package/{examples → legacy/examples}/crons/cron-worker.js +1 -1
  35. package/{examples → legacy/examples}/crons/programatic-crons.js +1 -1
  36. package/{examples → legacy/examples}/dag-worker.js +1 -1
  37. package/{examples → legacy/examples}/example-event-with-results.js +2 -2
  38. package/{examples → legacy/examples}/example-event.js +1 -1
  39. package/{examples → legacy/examples}/fanout-worker.js +1 -1
  40. package/{examples → legacy/examples}/logger.js +1 -1
  41. package/{examples → legacy/examples}/manual-trigger.js +1 -1
  42. package/{examples → legacy/examples}/multi-workflow.js +1 -1
  43. package/{examples → legacy/examples}/namespaced-worker.js +1 -1
  44. package/{examples → legacy/examples}/on-failure.js +1 -1
  45. package/{examples → legacy/examples}/rate-limit/events.js +1 -1
  46. package/{examples → legacy/examples}/rate-limit/worker.js +2 -2
  47. package/{examples → legacy/examples}/retries-with-backoff.js +1 -1
  48. package/{examples → legacy/examples}/retries-worker.js +1 -1
  49. package/{examples → legacy/examples}/scheduled-runs/programatic-schedules.js +1 -1
  50. package/{examples → legacy/examples}/simple-worker.d.ts +1 -1
  51. package/{examples → legacy/examples}/simple-worker.js +1 -1
  52. package/{examples → legacy/examples}/sticky-trigger.js +1 -1
  53. package/{examples → legacy/examples}/sticky-worker-with-check.js +2 -2
  54. package/{examples → legacy/examples}/sticky-worker.js +2 -2
  55. package/{examples → legacy/examples}/stream-by-additional-meta.js +2 -2
  56. package/legacy/legacy-client.d.ts +30 -0
  57. package/legacy/legacy-client.js +67 -0
  58. package/legacy/legacy-transformer.d.ts +38 -0
  59. package/legacy/legacy-transformer.js +181 -0
  60. package/legacy/step.d.ts +429 -0
  61. package/legacy/step.js +662 -0
  62. package/legacy/workflow.d.ts +488 -0
  63. package/legacy/workflow.js +72 -0
  64. package/package.json +2 -2
  65. package/step.d.ts +1 -430
  66. package/step.js +6 -647
  67. package/util/abort-error.d.ts +38 -0
  68. package/util/abort-error.js +58 -0
  69. package/util/hatchet-promise/hatchet-promise.d.ts +8 -1
  70. package/util/hatchet-promise/hatchet-promise.js +3 -2
  71. package/util/logger/index.d.ts +1 -0
  72. package/util/logger/index.js +1 -0
  73. package/util/logger/task-run-log.d.ts +3 -0
  74. package/util/logger/task-run-log.js +20 -0
  75. package/util/sleep.d.ts +5 -2
  76. package/util/sleep.js +27 -4
  77. package/util/workflow-run-ref.d.ts +7 -1
  78. package/util/workflow-run-ref.js +5 -3
  79. package/v1/client/admin.d.ts +8 -1
  80. package/v1/client/admin.js +22 -6
  81. package/v1/client/client.d.ts +36 -18
  82. package/v1/client/client.interface.d.ts +9 -2
  83. package/v1/client/client.js +47 -27
  84. package/v1/client/features/crons.d.ts +3 -3
  85. package/v1/client/features/crons.js +2 -2
  86. package/v1/client/features/schedules.js +2 -2
  87. package/v1/client/features/workflows.d.ts +5 -5
  88. package/v1/client/features/workflows.js +7 -8
  89. package/v1/client/worker/context.d.ts +49 -10
  90. package/v1/client/worker/context.js +92 -20
  91. package/v1/client/worker/deprecated/legacy-v1-worker.d.ts +3 -3
  92. package/v1/client/worker/deprecated/legacy-v1-worker.js +2 -2
  93. package/v1/client/worker/deprecated/legacy-worker.js +9 -7
  94. package/v1/client/worker/slot-utils.d.ts +3 -3
  95. package/v1/client/worker/worker-internal.d.ts +7 -16
  96. package/v1/client/worker/worker-internal.js +225 -209
  97. package/v1/client/worker/worker.d.ts +18 -18
  98. package/v1/client/worker/worker.js +45 -30
  99. package/v1/declaration.d.ts +28 -7
  100. package/v1/declaration.js +56 -6
  101. package/v1/examples/__e2e__/harness.d.ts +19 -0
  102. package/v1/examples/__e2e__/harness.js +70 -0
  103. package/v1/examples/affinity/affinity-workers.js +4 -4
  104. package/v1/examples/bulk_fanout/workflow.d.ts +9 -0
  105. package/v1/examples/bulk_fanout/workflow.js +34 -0
  106. package/v1/examples/bulk_operations/workflow.d.ts +3 -0
  107. package/v1/examples/bulk_operations/workflow.js +44 -0
  108. package/v1/examples/cancellation/cancellation-workflow.d.ts +2 -0
  109. package/v1/examples/cancellation/cancellation-workflow.js +69 -0
  110. package/v1/examples/cancellation/run.js +53 -0
  111. package/v1/examples/cancellation/worker.d.ts +1 -0
  112. package/v1/examples/cancellation/worker.js +29 -0
  113. package/v1/examples/concurrency-types.d.ts +5 -0
  114. package/v1/examples/concurrency-types.js +2 -0
  115. package/v1/examples/concurrency_cancel_in_progress/workflow.d.ts +9 -0
  116. package/v1/examples/concurrency_cancel_in_progress/workflow.js +45 -0
  117. package/v1/examples/concurrency_cancel_newest/workflow.d.ts +9 -0
  118. package/v1/examples/concurrency_cancel_newest/workflow.js +45 -0
  119. package/v1/examples/concurrency_limit_rr/load.d.ts +1 -0
  120. package/v1/examples/concurrency_limit_rr/load.js +54 -0
  121. package/v1/examples/concurrency_limit_rr/run.d.ts +1 -0
  122. package/v1/examples/concurrency_limit_rr/run.js +39 -0
  123. package/v1/examples/concurrency_limit_rr/worker.d.ts +1 -0
  124. package/v1/examples/concurrency_limit_rr/worker.js +24 -0
  125. package/v1/examples/concurrency_limit_rr/workflow.d.ts +12 -0
  126. package/v1/examples/concurrency_limit_rr/workflow.js +62 -0
  127. package/v1/examples/concurrency_multiple_keys/workflow.d.ts +12 -0
  128. package/v1/examples/concurrency_multiple_keys/workflow.js +42 -0
  129. package/v1/examples/concurrency_workflow_level/workflow.d.ts +13 -0
  130. package/v1/examples/concurrency_workflow_level/workflow.js +49 -0
  131. package/v1/examples/conditions/complex-workflow.d.ts +1 -0
  132. package/v1/examples/conditions/complex-workflow.js +107 -0
  133. package/v1/examples/conditions/event.d.ts +1 -0
  134. package/v1/examples/conditions/event.js +28 -0
  135. package/v1/examples/conditions/run.d.ts +1 -0
  136. package/v1/examples/conditions/run.js +25 -0
  137. package/v1/examples/conditions/worker.d.ts +1 -0
  138. package/v1/examples/conditions/worker.js +24 -0
  139. package/v1/examples/conditions/workflow.d.ts +11 -0
  140. package/v1/examples/conditions/workflow.js +41 -0
  141. package/v1/examples/durable/workflow.d.ts +7 -0
  142. package/v1/examples/durable/workflow.js +116 -0
  143. package/v1/examples/durable_event/event.d.ts +1 -0
  144. package/v1/examples/durable_event/event.js +28 -0
  145. package/v1/examples/durable_event/run.d.ts +1 -0
  146. package/v1/examples/durable_event/run.js +30 -0
  147. package/v1/examples/durable_event/worker.d.ts +1 -0
  148. package/v1/examples/durable_event/worker.js +24 -0
  149. package/v1/examples/durable_event/workflow.d.ts +6 -0
  150. package/v1/examples/durable_event/workflow.js +46 -0
  151. package/v1/examples/durable_sleep/event.d.ts +1 -0
  152. package/v1/examples/durable_sleep/event.js +28 -0
  153. package/v1/examples/durable_sleep/run.d.ts +1 -0
  154. package/v1/examples/durable_sleep/run.js +30 -0
  155. package/v1/examples/durable_sleep/worker.d.ts +1 -0
  156. package/v1/examples/durable_sleep/worker.js +24 -0
  157. package/v1/examples/durable_sleep/workflow.d.ts +1 -0
  158. package/v1/examples/durable_sleep/workflow.js +31 -0
  159. package/v1/examples/e2e-worker.d.ts +1 -0
  160. package/v1/examples/e2e-worker.js +82 -0
  161. package/v1/examples/events/event.d.ts +1 -0
  162. package/v1/examples/events/event.js +53 -0
  163. package/v1/examples/events/filter.d.ts +1 -0
  164. package/v1/examples/events/filter.js +32 -0
  165. package/v1/examples/events/worker.d.ts +1 -0
  166. package/v1/examples/events/worker.js +24 -0
  167. package/v1/examples/events/workflow.d.ts +19 -0
  168. package/v1/examples/events/workflow.js +60 -0
  169. package/v1/examples/high-memory/workflow-with-child.js +1 -1
  170. package/v1/examples/logger/byo-logger.d.ts +1 -0
  171. package/v1/examples/logger/byo-logger.js +73 -0
  172. package/v1/examples/logger/logger.d.ts +1 -0
  173. package/v1/examples/logger/logger.js +46 -0
  174. package/v1/examples/logger/workflow.d.ts +2 -0
  175. package/v1/examples/logger/workflow.js +38 -0
  176. package/v1/examples/multiple_wf_concurrency/workflow.js +3 -3
  177. package/v1/examples/on_failure/workflow.d.ts +1 -0
  178. package/v1/examples/on_failure/workflow.js +12 -7
  179. package/v1/examples/priority/workflow.js +1 -1
  180. package/v1/examples/retries/workflow.js +2 -2
  181. package/v1/examples/return_exceptions/workflow.d.ts +6 -0
  182. package/v1/examples/return_exceptions/workflow.js +22 -0
  183. package/v1/examples/run_details/workflow.d.ts +4 -0
  184. package/v1/examples/run_details/workflow.js +65 -0
  185. package/v1/examples/simple/e2e-workflows.d.ts +14 -0
  186. package/v1/examples/simple/e2e-workflows.js +27 -0
  187. package/v1/examples/simple/enqueue.js +2 -2
  188. package/v1/examples/sticky/workflow.js +2 -2
  189. package/v1/examples/timeout/run.d.ts +1 -0
  190. package/v1/examples/timeout/run.js +30 -0
  191. package/v1/examples/timeout/worker.d.ts +1 -0
  192. package/v1/examples/timeout/worker.js +25 -0
  193. package/v1/examples/timeout/workflow.d.ts +10 -0
  194. package/v1/examples/timeout/workflow.js +56 -0
  195. package/v1/examples/webhooks/workflow.d.ts +5 -0
  196. package/v1/examples/webhooks/workflow.js +23 -0
  197. package/v1/index.d.ts +1 -0
  198. package/v1/index.js +1 -0
  199. package/v1/parent-run-context-vars.d.ts +6 -0
  200. package/v1/parent-run-context-vars.js +3 -0
  201. package/v1/task.d.ts +22 -7
  202. package/v1/task.js +4 -1
  203. package/version.d.ts +1 -1
  204. package/version.js +1 -1
  205. package/workflow.d.ts +3 -491
  206. package/workflow.js +12 -62
  207. package/clients/hatchet-client/hatchet-client.d.ts +0 -35
  208. package/clients/hatchet-client/hatchet-client.js +0 -108
  209. package/clients/worker/handler.d.ts +0 -50
  210. package/clients/worker/handler.js +0 -214
  211. package/clients/worker/index.d.ts +0 -1
  212. package/clients/worker/index.js +0 -17
  213. package/clients/worker/worker.d.ts +0 -59
  214. package/clients/worker/worker.js +0 -568
  215. package/examples/webhooks.js +0 -45
  216. /package/{examples → legacy/examples}/affinity-workers.d.ts +0 -0
  217. /package/{examples → legacy/examples}/bulk-fanout-trigger.d.ts +0 -0
  218. /package/{examples → legacy/examples}/bulk-fanout-worker.d.ts +0 -0
  219. /package/{examples → legacy/examples}/bulk-trigger.d.ts +0 -0
  220. /package/{examples → legacy/examples}/byo-logger.d.ts +0 -0
  221. /package/{examples → legacy/examples}/concurrency/cancel-in-progress/concurrency-event.d.ts +0 -0
  222. /package/{examples → legacy/examples}/concurrency/cancel-in-progress/concurrency-worker.d.ts +0 -0
  223. /package/{examples → legacy/examples}/concurrency/group-round-robin/concurrency-event.d.ts +0 -0
  224. /package/{examples → legacy/examples}/concurrency/group-round-robin/concurrency-worker-expression.d.ts +0 -0
  225. /package/{examples → legacy/examples}/concurrency/group-round-robin/concurrency-worker-key-fn.d.ts +0 -0
  226. /package/{examples → legacy/examples}/crons/programatic-crons.d.ts +0 -0
  227. /package/{examples → legacy/examples}/dag-worker.d.ts +0 -0
  228. /package/{examples → legacy/examples}/example-event-with-results.d.ts +0 -0
  229. /package/{examples → legacy/examples}/example-event.d.ts +0 -0
  230. /package/{examples → legacy/examples}/fanout-worker.d.ts +0 -0
  231. /package/{examples → legacy/examples}/logger.d.ts +0 -0
  232. /package/{examples → legacy/examples}/manual-trigger.d.ts +0 -0
  233. /package/{examples → legacy/examples}/multi-workflow.d.ts +0 -0
  234. /package/{examples → legacy/examples}/namespaced-worker.d.ts +0 -0
  235. /package/{examples → legacy/examples}/on-failure.d.ts +0 -0
  236. /package/{examples → legacy/examples}/rate-limit/events.d.ts +0 -0
  237. /package/{examples → legacy/examples}/rate-limit/worker.d.ts +0 -0
  238. /package/{examples → legacy/examples}/retries-with-backoff.d.ts +0 -0
  239. /package/{examples → legacy/examples}/retries-worker.d.ts +0 -0
  240. /package/{examples → legacy/examples}/scheduled-runs/programatic-schedules.d.ts +0 -0
  241. /package/{examples → legacy/examples}/sticky-trigger.d.ts +0 -0
  242. /package/{examples → legacy/examples}/sticky-worker-with-check.d.ts +0 -0
  243. /package/{examples → legacy/examples}/sticky-worker.d.ts +0 -0
  244. /package/{examples → legacy/examples}/stream-by-additional-meta.d.ts +0 -0
  245. /package/{examples/webhooks.d.ts → v1/examples/cancellation/run.d.ts} +0 -0
@@ -1,108 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.LegacyHatchetClient = void 0;
16
- const zod_1 = require("zod");
17
- const config_loader_1 = require("../../util/config-loader");
18
- const event_client_1 = require("../event/event-client");
19
- const dispatcher_client_1 = require("../dispatcher/dispatcher-client");
20
- const admin_client_1 = require("../admin/admin-client");
21
- const nice_grpc_1 = require("nice-grpc");
22
- const worker_1 = require("../worker");
23
- const hatchet_logger_1 = require("./hatchet-logger");
24
- const grpc_helpers_1 = require("../../util/grpc-helpers");
25
- const client_config_1 = require("./client-config");
26
- const child_listener_client_1 = require("../listeners/run-listener/child-listener-client");
27
- const rest_1 = __importDefault(require("../rest"));
28
- const durable_listener_client_1 = require("../listeners/durable-listener/durable-listener-client");
29
- class LegacyHatchetClient {
30
- constructor(config, options, axiosOpts, runs, listener) {
31
- // Initializes a new Client instance.
32
- // Loads config in the following order: config param > yaml file > env vars
33
- var _a;
34
- this.runs = runs;
35
- const loaded = config_loader_1.ConfigLoader.loadClientConfig(config, {
36
- path: options === null || options === void 0 ? void 0 : options.config_path,
37
- });
38
- try {
39
- const valid = client_config_1.ClientConfigSchema.parse(loaded);
40
- let logConstructor = config === null || config === void 0 ? void 0 : config.logger;
41
- if (logConstructor == null) {
42
- logConstructor = hatchet_logger_1.DEFAULT_LOGGER;
43
- }
44
- this.config = Object.assign(Object.assign({}, valid), { logger: logConstructor });
45
- }
46
- catch (e) {
47
- if (e instanceof zod_1.z.ZodError) {
48
- throw new Error(`Invalid client config: ${e.message}`);
49
- }
50
- throw e;
51
- }
52
- this.credentials =
53
- (_a = options === null || options === void 0 ? void 0 : options.credentials) !== null && _a !== void 0 ? _a : config_loader_1.ConfigLoader.createCredentials(this.config.tls_config);
54
- const clientFactory = (0, nice_grpc_1.createClientFactory)().use((0, grpc_helpers_1.addTokenMiddleware)(this.config.token));
55
- this.tenantId = this.config.tenant_id;
56
- this.api = (0, rest_1.default)(this.config.api_url, this.config.token, axiosOpts);
57
- this.event = new event_client_1.EventClient(this.config, (0, grpc_helpers_1.channelFactory)(this.config, this.credentials), clientFactory, this);
58
- this.dispatcher = new dispatcher_client_1.DispatcherClient(this.config, (0, grpc_helpers_1.channelFactory)(this.config, this.credentials), clientFactory);
59
- this.listener =
60
- listener ||
61
- new child_listener_client_1.RunListenerClient(this.config, (0, grpc_helpers_1.channelFactory)(this.config, this.credentials), clientFactory, this.api);
62
- this.admin = new admin_client_1.AdminClient(this.config, (0, grpc_helpers_1.channelFactory)(this.config, this.credentials), clientFactory, this.api, this.tenantId, this.listener, this.runs);
63
- this.durableListener = new durable_listener_client_1.DurableListenerClient(this.config, (0, grpc_helpers_1.channelFactory)(this.config, this.credentials), clientFactory, this.api);
64
- this.logger = this.config.logger('HatchetClient', this.config.log_level);
65
- this.logger.debug(`Initialized HatchetClient`);
66
- }
67
- static init(config, options, axiosConfig) {
68
- return new LegacyHatchetClient(config, options, axiosConfig);
69
- }
70
- // @deprecated
71
- run(workflow) {
72
- return __awaiter(this, void 0, void 0, function* () {
73
- this.logger.warn('HatchetClient.run is deprecated and will be removed in a future release. Use HatchetClient.worker and Worker.start instead.');
74
- const worker = yield this.worker(workflow);
75
- worker.start();
76
- return worker;
77
- });
78
- }
79
- worker(workflow, opts) {
80
- return __awaiter(this, void 0, void 0, function* () {
81
- const name = typeof workflow === 'string' ? workflow : workflow.id;
82
- let options = {
83
- name,
84
- };
85
- if (typeof opts === 'number') {
86
- this.logger.warn('@deprecated maxRuns param is deprecated and will be removed in a future release in favor of WorkerOpts');
87
- options = Object.assign(Object.assign({}, options), { maxRuns: opts });
88
- }
89
- else {
90
- options = Object.assign(Object.assign({}, options), opts);
91
- }
92
- const worker = new worker_1.V0Worker(this, options);
93
- if (typeof workflow !== 'string') {
94
- yield worker.registerWorkflow(workflow);
95
- return worker;
96
- }
97
- return worker;
98
- });
99
- }
100
- webhooks(workflows) {
101
- // TODO v1 workflows
102
- const worker = new worker_1.V0Worker(this, {
103
- name: 'webhook-worker',
104
- });
105
- return worker.getHandler(workflows);
106
- }
107
- }
108
- exports.LegacyHatchetClient = LegacyHatchetClient;
@@ -1,50 +0,0 @@
1
- import { IncomingMessage, ServerResponse } from 'http';
2
- import { Workflow } from '../../workflow';
3
- import { V0Worker } from './worker';
4
- export interface HandlerOpts {
5
- secret: string;
6
- }
7
- export declare class WebhookHandler {
8
- private worker;
9
- private workflows;
10
- constructor(worker: V0Worker, workflows: Workflow[]);
11
- /**
12
- * Handles a request with a provided body, secret, and signature.
13
- *
14
- * @param {string | undefined} body - The body of the request.
15
- * @param {string | undefined} secret - The secret used for signature verification.
16
- * @param {string | string[] | undefined | null} signature - The signature of the request.
17
- *
18
- * @throws {HatchetError} - If no signature is provided or the signature is not a string.
19
- * @throws {HatchetError} - If no secret is provided.
20
- * @throws {HatchetError} - If no body is provided.
21
- */
22
- handle(body: string | undefined, signature: string | string[] | undefined | null, secret: string | undefined): Promise<void>;
23
- private checkSignature;
24
- private getHealthcheckResponse;
25
- /**
26
- * Express Handler
27
- *
28
- * This method is an asynchronous function that returns an Express middleware handler.
29
- * The handler function is responsible for handling incoming requests and invoking the
30
- * corresponding logic based on the provided secret.
31
- */
32
- expressHandler({ secret }: HandlerOpts): (req: any, res: any) => void;
33
- /**
34
- * A method that returns an HTTP request handler.
35
- */
36
- httpHandler({ secret }: HandlerOpts): (req: IncomingMessage, res: ServerResponse) => void;
37
- /**
38
- * A method that returns a Next.js pages router request handler.
39
- */
40
- nextJSPagesHandler({ secret }: HandlerOpts): (req: any, res: any) => Promise<any>;
41
- /**
42
- * A method that returns a Next.js request handler.
43
- */
44
- nextJSHandler({ secret }: HandlerOpts): {
45
- GET: () => Promise<Response>;
46
- POST: (req: Request) => Promise<Response>;
47
- PUT: (req: Request) => Promise<Response>;
48
- };
49
- private getBody;
50
- }
@@ -1,214 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.WebhookHandler = void 0;
16
- const hatchet_error_1 = __importDefault(require("../../util/errors/hatchet-error"));
17
- const crypto_1 = require("crypto");
18
- const okMessage = 'The Hatchet webhooks endpoint is up and running!';
19
- class WebhookHandler {
20
- // eslint-disable-next-line no-useless-constructor
21
- constructor(worker, workflows
22
- // eslint-disable-next-line no-empty-function
23
- ) {
24
- this.worker = worker;
25
- this.workflows = workflows;
26
- }
27
- /**
28
- * Handles a request with a provided body, secret, and signature.
29
- *
30
- * @param {string | undefined} body - The body of the request.
31
- * @param {string | undefined} secret - The secret used for signature verification.
32
- * @param {string | string[] | undefined | null} signature - The signature of the request.
33
- *
34
- * @throws {HatchetError} - If no signature is provided or the signature is not a string.
35
- * @throws {HatchetError} - If no secret is provided.
36
- * @throws {HatchetError} - If no body is provided.
37
- */
38
- handle(body, signature, secret) {
39
- return __awaiter(this, void 0, void 0, function* () {
40
- this.checkSignature(body, signature, secret);
41
- const action = JSON.parse(body);
42
- yield this.worker.handleAction(action);
43
- });
44
- }
45
- checkSignature(body, signature, secret) {
46
- if (!signature || typeof signature !== 'string') {
47
- throw new hatchet_error_1.default('No signature provided');
48
- }
49
- if (!secret) {
50
- throw new hatchet_error_1.default('No secret provided');
51
- }
52
- if (!body) {
53
- throw new hatchet_error_1.default('No body provided');
54
- }
55
- // verify hmac signature
56
- const actualSignature = (0, crypto_1.createHmac)('sha256', secret).update(body).digest('hex');
57
- if (actualSignature !== signature) {
58
- throw new hatchet_error_1.default(`Invalid signature, expected ${actualSignature}, got ${signature}`);
59
- }
60
- }
61
- getHealthcheckResponse(body, signature, secret) {
62
- return __awaiter(this, void 0, void 0, function* () {
63
- this.checkSignature(body, signature, secret);
64
- for (const workflow of this.workflows) {
65
- yield this.worker.registerWorkflow(workflow);
66
- }
67
- return {
68
- actions: Object.keys(this.worker.action_registry),
69
- };
70
- });
71
- }
72
- /**
73
- * Express Handler
74
- *
75
- * This method is an asynchronous function that returns an Express middleware handler.
76
- * The handler function is responsible for handling incoming requests and invoking the
77
- * corresponding logic based on the provided secret.
78
- */
79
- expressHandler({ secret }) {
80
- return (req, res) => {
81
- if (req.method === 'GET') {
82
- res.status(200).send(okMessage);
83
- return;
84
- }
85
- if (req.method === 'PUT') {
86
- let { body } = req;
87
- if (typeof body !== 'string') {
88
- body = JSON.stringify(body);
89
- }
90
- this.getHealthcheckResponse(body, req.headers['x-hatchet-signature'], secret)
91
- .then((resp) => {
92
- res.status(200).json(resp);
93
- })
94
- .catch((err) => {
95
- res.status(500);
96
- this.worker.logger.error(`Error handling request: ${err.message}`);
97
- });
98
- return;
99
- }
100
- if (req.method !== 'POST') {
101
- res.status(405).json({ error: 'Method not allowed' });
102
- return;
103
- }
104
- let action = req.body;
105
- if (typeof action !== 'string') {
106
- action = JSON.stringify(action);
107
- }
108
- this.handle(action, req.headers['x-hatchet-signature'], secret)
109
- .then(() => {
110
- res.status(200);
111
- })
112
- .catch((err) => {
113
- res.status(500);
114
- this.worker.logger.error(`Error handling request: ${err.message}`);
115
- });
116
- };
117
- }
118
- /**
119
- * A method that returns an HTTP request handler.
120
- */
121
- httpHandler({ secret }) {
122
- return (req, res) => {
123
- const handle = () => __awaiter(this, void 0, void 0, function* () {
124
- if (req.method === 'GET') {
125
- res.writeHead(200, { 'Content-Type': 'application/json' });
126
- res.write(okMessage);
127
- res.end();
128
- return;
129
- }
130
- const body = yield this.getBody(req);
131
- if (req.method === 'PUT') {
132
- const resp = yield this.getHealthcheckResponse(body, req.headers['x-hatchet-signature'], secret);
133
- res.writeHead(200, { 'Content-Type': 'application/json' });
134
- res.write(JSON.stringify(resp));
135
- res.end();
136
- return;
137
- }
138
- if (req.method !== 'POST') {
139
- res.writeHead(405, { 'Content-Type': 'application/json' });
140
- res.write(JSON.stringify({ error: 'Method not allowed' }));
141
- res.end();
142
- return;
143
- }
144
- yield this.handle(body, req.headers['x-hatchet-signature'], secret);
145
- res.writeHead(200, 'OK');
146
- res.end();
147
- });
148
- handle().catch((e) => {
149
- this.worker.logger.error(`Error handling request: ${e.message}`);
150
- res.writeHead(500, 'Internal server error');
151
- res.end();
152
- });
153
- };
154
- }
155
- /**
156
- * A method that returns a Next.js pages router request handler.
157
- */
158
- nextJSPagesHandler({ secret }) {
159
- return (req, res) => __awaiter(this, void 0, void 0, function* () {
160
- if (req.method === 'GET') {
161
- return res.status(200).send(okMessage);
162
- }
163
- const sig = req.headers['x-hatchet-signature'];
164
- const body = JSON.stringify(req.body);
165
- if (req.method === 'PUT') {
166
- const resp = yield this.getHealthcheckResponse(body, sig, secret);
167
- return res.status(200).send(JSON.stringify(resp));
168
- }
169
- if (req.method !== 'POST') {
170
- return res.status(405).send('Method not allowed');
171
- }
172
- yield this.handle(body, sig, secret);
173
- return res.status(200).send('ok');
174
- });
175
- }
176
- /**
177
- * A method that returns a Next.js request handler.
178
- */
179
- nextJSHandler({ secret }) {
180
- const ok = () => __awaiter(this, void 0, void 0, function* () {
181
- return new Response(okMessage, { status: 200 });
182
- });
183
- const f = (req) => __awaiter(this, void 0, void 0, function* () {
184
- const sig = req.headers.get('x-hatchet-signature');
185
- const body = yield req.text();
186
- if (req.method === 'PUT') {
187
- const resp = yield this.getHealthcheckResponse(body, sig, secret);
188
- return new Response(JSON.stringify(resp), { status: 200 });
189
- }
190
- if (req.method !== 'POST') {
191
- return new Response('Method not allowed', { status: 405 });
192
- }
193
- yield this.handle(body, sig, secret);
194
- return new Response('ok', { status: 200 });
195
- });
196
- return {
197
- GET: ok,
198
- POST: f,
199
- PUT: f,
200
- };
201
- }
202
- getBody(req) {
203
- return new Promise((resolve) => {
204
- let body = '';
205
- req.on('data', (chunk) => {
206
- body += chunk;
207
- });
208
- req.on('end', () => {
209
- resolve(body);
210
- });
211
- });
212
- }
213
- }
214
- exports.WebhookHandler = WebhookHandler;
@@ -1 +0,0 @@
1
- export * from './worker';
@@ -1,17 +0,0 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./worker"), exports);
@@ -1,59 +0,0 @@
1
- import { LegacyHatchetClient } from '../hatchet-client';
2
- import { Action, ActionKey, ActionListener } from '../dispatcher/action-listener';
3
- import { StepActionEvent, StepActionEventType, GroupKeyActionEvent, GroupKeyActionEventType } from '../../protoc/dispatcher';
4
- import HatchetPromise from '../../util/hatchet-promise/hatchet-promise';
5
- import { Workflow } from '../../workflow';
6
- import { Logger } from '../../util/logger';
7
- import { WebhookHandler } from './handler';
8
- import { WebhookWorkerCreateRequest } from '../rest/generated/data-contracts';
9
- import { WorkflowDefinition } from '../../v1';
10
- import { V0Context, StepRunFunction } from '../../step';
11
- import { WorkerLabels } from '../dispatcher/dispatcher-client';
12
- export type ActionRegistry = Record<Action['actionId'], Function>;
13
- export interface WorkerOpts {
14
- name: string;
15
- handleKill?: boolean;
16
- maxRuns?: number;
17
- labels?: WorkerLabels;
18
- }
19
- export declare class V0Worker {
20
- client: LegacyHatchetClient;
21
- name: string;
22
- workerId: string | undefined;
23
- killing: boolean;
24
- handle_kill: boolean;
25
- action_registry: ActionRegistry;
26
- workflow_registry: Array<WorkflowDefinition | Workflow>;
27
- listener: ActionListener | undefined;
28
- futures: Record<ActionKey, HatchetPromise<any>>;
29
- contexts: Record<ActionKey, V0Context<any, any>>;
30
- maxRuns?: number;
31
- logger: Logger;
32
- registeredWorkflowPromises: Array<Promise<any>>;
33
- labels: WorkerLabels;
34
- constructor(client: LegacyHatchetClient, options: {
35
- name: string;
36
- handleKill?: boolean;
37
- maxRuns?: number;
38
- labels?: WorkerLabels;
39
- });
40
- private registerActions;
41
- getHandler(workflows: Workflow[]): WebhookHandler;
42
- registerWebhook(webhook: WebhookWorkerCreateRequest): Promise<import("axios").AxiosResponse<import("../rest/generated/data-contracts").WebhookWorkerCreated, any, {}>>;
43
- /**
44
- * @deprecated use registerWorkflow instead
45
- */
46
- register_workflow(initWorkflow: Workflow): Promise<void>;
47
- registerWorkflow(initWorkflow: Workflow): Promise<void>;
48
- registerAction<T, K>(actionId: string, action: StepRunFunction<T, K>): void;
49
- handleStartStepRun(action: Action): Promise<void>;
50
- handleStartGroupKeyRun(action: Action): Promise<void>;
51
- getStepActionEvent(action: Action, eventType: StepActionEventType, shouldNotRetry: boolean, payload?: any, retryCount?: number): StepActionEvent;
52
- getGroupKeyActionEvent(action: Action, eventType: GroupKeyActionEventType, payload?: any): GroupKeyActionEvent;
53
- handleCancelStepRun(action: Action): Promise<void>;
54
- stop(): Promise<void>;
55
- exitGracefully(handleKill: boolean): Promise<void>;
56
- start(): Promise<void>;
57
- handleAction(action: Action): Promise<void>;
58
- upsertLabels(labels: WorkerLabels): Promise<WorkerLabels>;
59
- }