@hatchet-dev/typescript-sdk 0.0.1 → 0.0.3

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 (109) hide show
  1. package/README.md +2 -3
  2. package/dist/{hatchet/clients → clients}/admin/admin-client.d.ts +2 -2
  3. package/dist/{hatchet/clients → clients}/admin/admin-client.js +2 -2
  4. package/dist/{hatchet/clients → clients}/dispatcher/action-listener.d.ts +3 -3
  5. package/dist/{hatchet/clients → clients}/dispatcher/action-listener.js +2 -2
  6. package/dist/{hatchet/clients → clients}/dispatcher/dispatcher-client.d.ts +3 -3
  7. package/dist/{hatchet/clients → clients}/dispatcher/dispatcher-client.js +2 -2
  8. package/dist/{hatchet/clients → clients}/event/event-client.d.ts +3 -3
  9. package/dist/{hatchet/clients → clients}/event/event-client.js +2 -2
  10. package/dist/{hatchet/clients → clients}/hatchet-client/hatchet-client.d.ts +6 -6
  11. package/dist/{hatchet/clients → clients}/hatchet-client/hatchet-client.js +6 -6
  12. package/dist/{hatchet/clients → clients}/worker/worker.d.ts +6 -6
  13. package/dist/{hatchet/clients → clients}/worker/worker.js +5 -5
  14. package/dist/index.d.ts +2 -0
  15. package/dist/{hatchet/index.js → index.js} +1 -1
  16. package/dist/protoc/events/index.js +17 -0
  17. package/dist/sdk.d.ts +2 -0
  18. package/dist/{hatchet/sdk.js → sdk.js} +1 -1
  19. package/dist/{hatchet/step.js → step.js} +1 -1
  20. package/dist/{hatchet/util → util}/config-loader/config-loader.d.ts +1 -1
  21. package/dist/{hatchet/util → util}/config-loader/config-loader.js +1 -1
  22. package/package.json +8 -5
  23. package/.eslintrc.json +0 -36
  24. package/.prettierrc.json +0 -6
  25. package/dist/hatchet/clients/admin/admin-client.test.d.ts +0 -1
  26. package/dist/hatchet/clients/admin/admin-client.test.js +0 -101
  27. package/dist/hatchet/clients/dispatcher/action-listener.test.d.ts +0 -4
  28. package/dist/hatchet/clients/dispatcher/action-listener.test.js +0 -277
  29. package/dist/hatchet/clients/dispatcher/dispatcher-client.test.d.ts +0 -1
  30. package/dist/hatchet/clients/dispatcher/dispatcher-client.test.js +0 -99
  31. package/dist/hatchet/clients/event/event-client.test.d.ts +0 -1
  32. package/dist/hatchet/clients/event/event-client.test.js +0 -60
  33. package/dist/hatchet/clients/hatchet-client/hatchet-client.test.d.ts +0 -2
  34. package/dist/hatchet/clients/hatchet-client/hatchet-client.test.js +0 -135
  35. package/dist/hatchet/clients/worker/worker.test.d.ts +0 -1
  36. package/dist/hatchet/clients/worker/worker.test.js +0 -256
  37. package/dist/hatchet/index.d.ts +0 -2
  38. package/dist/hatchet/sdk.d.ts +0 -2
  39. package/dist/hatchet/util/config-loader/config-loader.test.d.ts +0 -1
  40. package/dist/hatchet/util/config-loader/config-loader.test.js +0 -72
  41. package/dist/hatchet/util/hatchet-promise/hatchet-promise.test.d.ts +0 -1
  42. package/dist/hatchet/util/hatchet-promise/hatchet-promise.test.js +0 -40
  43. package/examples/dag-worker.ts +0 -55
  44. package/examples/example-event.ts +0 -7
  45. package/examples/simple-worker.ts +0 -39
  46. package/generate-protoc.sh +0 -10
  47. package/hatchet/clients/admin/admin-client.test.ts +0 -116
  48. package/hatchet/clients/admin/admin-client.ts +0 -43
  49. package/hatchet/clients/dispatcher/action-listener.test.ts +0 -270
  50. package/hatchet/clients/dispatcher/action-listener.ts +0 -91
  51. package/hatchet/clients/dispatcher/dispatcher-client.test.ts +0 -111
  52. package/hatchet/clients/dispatcher/dispatcher-client.ts +0 -47
  53. package/hatchet/clients/event/event-client.test.ts +0 -72
  54. package/hatchet/clients/event/event-client.ts +0 -32
  55. package/hatchet/clients/hatchet-client/client-config.ts +0 -22
  56. package/hatchet/clients/hatchet-client/fixtures/.hatchet-invalid.yaml +0 -6
  57. package/hatchet/clients/hatchet-client/fixtures/.hatchet.yaml +0 -8
  58. package/hatchet/clients/hatchet-client/hatchet-client.test.ts +0 -162
  59. package/hatchet/clients/hatchet-client/hatchet-client.ts +0 -136
  60. package/hatchet/clients/hatchet-client/index.ts +0 -2
  61. package/hatchet/clients/worker/index.ts +0 -1
  62. package/hatchet/clients/worker/worker.test.ts +0 -347
  63. package/hatchet/clients/worker/worker.ts +0 -229
  64. package/hatchet/index.ts +0 -3
  65. package/hatchet/sdk.ts +0 -3
  66. package/hatchet/step.ts +0 -48
  67. package/hatchet/util/config-loader/config-loader.test.ts +0 -79
  68. package/hatchet/util/config-loader/config-loader.ts +0 -91
  69. package/hatchet/util/config-loader/fixtures/.hatchet-invalid.yaml +0 -6
  70. package/hatchet/util/config-loader/fixtures/.hatchet.yaml +0 -8
  71. package/hatchet/util/config-loader/index.ts +0 -1
  72. package/hatchet/util/errors/hatchet-error.ts +0 -8
  73. package/hatchet/util/hatchet-promise/hatchet-promise.test.ts +0 -32
  74. package/hatchet/util/hatchet-promise/hatchet-promise.ts +0 -13
  75. package/hatchet/util/logger/index.ts +0 -1
  76. package/hatchet/util/logger/logger.ts +0 -44
  77. package/hatchet/util/sleep.ts +0 -6
  78. package/hatchet/workflow.ts +0 -30
  79. package/jest.config.ts +0 -205
  80. package/protoc/dispatcher/dispatcher.ts +0 -1101
  81. package/protoc/dispatcher/index.ts +0 -1
  82. package/protoc/events/events.ts +0 -519
  83. package/protoc/google/protobuf/timestamp.ts +0 -210
  84. package/protoc/google/protobuf/wrappers.ts +0 -657
  85. package/protoc/workflows/index.ts +0 -1
  86. package/protoc/workflows/workflows.ts +0 -2158
  87. package/tsconfig.json +0 -120
  88. /package/dist/{hatchet/clients → clients}/hatchet-client/client-config.d.ts +0 -0
  89. /package/dist/{hatchet/clients → clients}/hatchet-client/client-config.js +0 -0
  90. /package/dist/{hatchet/clients → clients}/hatchet-client/index.d.ts +0 -0
  91. /package/dist/{hatchet/clients → clients}/hatchet-client/index.js +0 -0
  92. /package/dist/{hatchet/clients → clients}/worker/index.d.ts +0 -0
  93. /package/dist/{hatchet/clients → clients}/worker/index.js +0 -0
  94. /package/{protoc/events/index.ts → dist/protoc/events/index.d.ts} +0 -0
  95. /package/dist/{hatchet/step.d.ts → step.d.ts} +0 -0
  96. /package/dist/{hatchet/util → util}/config-loader/index.d.ts +0 -0
  97. /package/dist/{hatchet/util → util}/config-loader/index.js +0 -0
  98. /package/dist/{hatchet/util → util}/errors/hatchet-error.d.ts +0 -0
  99. /package/dist/{hatchet/util → util}/errors/hatchet-error.js +0 -0
  100. /package/dist/{hatchet/util → util}/hatchet-promise/hatchet-promise.d.ts +0 -0
  101. /package/dist/{hatchet/util → util}/hatchet-promise/hatchet-promise.js +0 -0
  102. /package/dist/{hatchet/util → util}/logger/index.d.ts +0 -0
  103. /package/dist/{hatchet/util → util}/logger/index.js +0 -0
  104. /package/dist/{hatchet/util → util}/logger/logger.d.ts +0 -0
  105. /package/dist/{hatchet/util → util}/logger/logger.js +0 -0
  106. /package/dist/{hatchet/util → util}/sleep.d.ts +0 -0
  107. /package/dist/{hatchet/util → util}/sleep.js +0 -0
  108. /package/dist/{hatchet/workflow.d.ts → workflow.d.ts} +0 -0
  109. /package/dist/{hatchet/workflow.js → workflow.js} +0 -0
@@ -1,270 +0,0 @@
1
- import { ActionType, AssignedAction } from '@protoc/dispatcher';
2
- import sleep from '@util/sleep';
3
- import { ServerError, Status } from 'nice-grpc-common';
4
- import { DispatcherClient } from './dispatcher-client';
5
- import { ActionListener } from './action-listener';
6
- import { mockChannel, mockFactory } from '../hatchet-client/hatchet-client.test';
7
-
8
- let dispatcher: DispatcherClient;
9
-
10
- type AssignActionMock = AssignedAction | Error;
11
-
12
- // Mock data for AssignedAction
13
- const mockAssignedActions: AssignActionMock[] = [
14
- {
15
- tenantId: 'tenant1',
16
- jobId: 'job1',
17
- jobName: 'Job One',
18
- jobRunId: 'run1',
19
- stepId: 'step1',
20
- stepRunId: 'runStep1',
21
- actionId: 'action1',
22
- actionType: ActionType.START_STEP_RUN,
23
- actionPayload: 'payload1',
24
- },
25
- // ... Add more mock AssignedAction objects as needed
26
- ];
27
-
28
- // Mock implementation of the listener
29
- export const mockListener = (fixture: AssignActionMock[]) =>
30
- (async function* gen() {
31
- for (const action of fixture) {
32
- // Simulate asynchronous behavior
33
- await sleep(100);
34
-
35
- if (action instanceof Error) {
36
- throw action;
37
- }
38
-
39
- yield action;
40
- }
41
- })();
42
-
43
- describe('ActionListener', () => {
44
- beforeEach(() => {
45
- dispatcher = new DispatcherClient(
46
- {
47
- token: 'TOKEN',
48
-
49
- host_port: 'HOST_PORT',
50
- log_level: 'OFF',
51
- tls_config: {
52
- cert_file: 'TLS_CERT_FILE',
53
- key_file: 'TLS_KEY_FILE',
54
- ca_file: 'TLS_ROOT_CA_FILE',
55
- server_name: 'TLS_SERVER_NAME',
56
- },
57
- },
58
- mockChannel,
59
- mockFactory
60
- );
61
- });
62
-
63
- it('should create a client', async () => {
64
- const listener = new ActionListener(dispatcher, mockListener(mockAssignedActions), 'WORKER_ID');
65
- expect(listener).toBeDefined();
66
- expect(listener.workerId).toEqual('WORKER_ID');
67
- });
68
-
69
- describe('actions', () => {
70
- it('it should "yield" actions', async () => {
71
- const listener = new ActionListener(
72
- dispatcher,
73
- mockListener([...mockAssignedActions, new ServerError(Status.CANCELLED, 'CANCELLED')]),
74
- 'WORKER_ID'
75
- );
76
-
77
- const actions = listener.actions();
78
- const res = [];
79
- for await (const action of actions) {
80
- res.push(action);
81
- }
82
-
83
- expect(res[0]).toEqual({
84
- tenantId: 'tenant1',
85
- jobId: 'job1',
86
- jobName: 'Job One',
87
- jobRunId: 'run1',
88
- stepId: 'step1',
89
- stepRunId: 'runStep1',
90
- actionId: 'action1',
91
- actionType: ActionType.START_STEP_RUN,
92
- actionPayload: 'payload1',
93
- });
94
- });
95
-
96
- it('it should break on grpc CANCELLED', async () => {
97
- const listener = new ActionListener(
98
- dispatcher,
99
- mockListener([...mockAssignedActions, new ServerError(Status.CANCELLED, 'CANCELLED')]),
100
- 'WORKER_ID'
101
- );
102
-
103
- const actions = listener.actions();
104
- const retrySpy = jest.spyOn(listener, 'retrySubscribe').mockResolvedValue(undefined);
105
-
106
- const res = [];
107
- for await (const action of actions) {
108
- res.push(action);
109
- }
110
-
111
- expect(res.length).toEqual(1);
112
- expect(retrySpy).not.toHaveBeenCalled();
113
- });
114
-
115
- it('it should break on unknown error', async () => {
116
- const listener = new ActionListener(
117
- dispatcher,
118
- mockListener([...mockAssignedActions, new Error('Simulated error')]),
119
- 'WORKER_ID'
120
- );
121
-
122
- const actions = listener.actions();
123
- const retrySpy = jest.spyOn(listener, 'retrySubscribe').mockResolvedValue(undefined);
124
-
125
- const res = [];
126
- for await (const action of actions) {
127
- res.push(action);
128
- }
129
-
130
- expect(res.length).toEqual(1);
131
- expect(retrySpy).not.toHaveBeenCalled();
132
- });
133
-
134
- it('it should attempt to re-establish connection on grpc UNAVAILABLE', async () => {
135
- const listener = new ActionListener(
136
- dispatcher,
137
- mockListener([...mockAssignedActions, new ServerError(Status.UNAVAILABLE, 'UNAVAILABLE')]),
138
- 'WORKER_ID'
139
- );
140
-
141
- const retrySpy = jest.spyOn(listener, 'retrySubscribe').mockResolvedValue(undefined);
142
-
143
- const actions = listener.actions();
144
-
145
- const res = [];
146
- for await (const action of actions) {
147
- res.push(action);
148
- }
149
-
150
- expect(res.length).toEqual(1);
151
- expect(retrySpy).toHaveBeenCalled();
152
- });
153
- });
154
-
155
- describe('retry_subscribe', () => {
156
- it('should exit after successful connection', async () => {
157
- const listener = new ActionListener(
158
- dispatcher,
159
- mockListener(mockAssignedActions),
160
- 'WORKER_ID'
161
- );
162
-
163
- // Mock the listener to throw an error on the first call
164
- const listenSpy = jest
165
- .spyOn(listener.client, 'listen')
166
- .mockReturnValue(mockListener(mockAssignedActions));
167
-
168
- await listener.retrySubscribe();
169
-
170
- expect(listenSpy).toHaveBeenCalledTimes(1);
171
- });
172
-
173
- it('should retry until success', async () => {
174
- const listener = new ActionListener(
175
- dispatcher,
176
- mockListener(mockAssignedActions),
177
- 'WORKER_ID'
178
- );
179
-
180
- // Mock the listener to throw an error on the first call
181
- const listenSpy = jest
182
- .spyOn(listener.client, 'listen')
183
- .mockImplementationOnce(() => {
184
- throw new Error('Simulated error');
185
- })
186
- .mockImplementationOnce(() => mockListener(mockAssignedActions));
187
-
188
- await expect(async () => {
189
- await listener.retrySubscribe();
190
- }).not.toThrow();
191
- });
192
-
193
- it('should not throw an error if successful', async () => {
194
- const listener = new ActionListener(
195
- dispatcher,
196
- mockListener(mockAssignedActions),
197
- 'WORKER_ID'
198
- );
199
-
200
- // Mock the listener to throw an error on the first call
201
- const listenSpy = jest
202
- .spyOn(listener.client, 'listen')
203
- .mockImplementationOnce(() => {
204
- throw new Error('Simulated error');
205
- })
206
- .mockImplementationOnce(() => mockListener(mockAssignedActions));
207
-
208
- await listener.retrySubscribe();
209
- expect(listenSpy).toHaveBeenCalledTimes(2);
210
- });
211
-
212
- it('should retry at most COUNT times and throw an error', async () => {
213
- const listener = new ActionListener(
214
- dispatcher,
215
- mockListener(mockAssignedActions),
216
- 'WORKER_ID'
217
- );
218
-
219
- // Mock the listener to throw an error on the first call
220
- const listenSpy = jest
221
- .spyOn(listener.client, 'listen')
222
- .mockImplementationOnce(() => {
223
- throw new Error('Simulated error');
224
- })
225
- .mockImplementationOnce(() => {
226
- throw new Error('Simulated error');
227
- })
228
- .mockImplementationOnce(() => {
229
- throw new Error('Simulated error');
230
- })
231
- .mockImplementationOnce(() => {
232
- throw new Error('Simulated error');
233
- })
234
- .mockImplementationOnce(() => {
235
- throw new Error('Simulated error');
236
- })
237
- .mockImplementationOnce(() => {
238
- throw new Error('Simulated error');
239
- })
240
- .mockImplementationOnce(() => mockListener(mockAssignedActions));
241
-
242
- try {
243
- await listener.retrySubscribe();
244
- expect(listenSpy).toHaveBeenCalledTimes(5);
245
- } catch (e: any) {
246
- expect(e.message).toEqual(`Could not subscribe to the worker after 5 retries`);
247
- }
248
- });
249
- });
250
-
251
- describe('unregister', () => {
252
- it('should unsubscribe itself', async () => {
253
- const listener = new ActionListener(
254
- dispatcher,
255
- mockListener(mockAssignedActions),
256
- 'WORKER_ID'
257
- );
258
-
259
- const unsubscribeSpy = jest.spyOn(listener.client, 'unsubscribe').mockResolvedValue({
260
- tenantId: 'TENANT_ID',
261
- workerId: 'WORKER_ID',
262
- });
263
-
264
- const res = await listener.unregister();
265
-
266
- expect(unsubscribeSpy).toHaveBeenCalled();
267
- expect(res.workerId).toEqual('WORKER_ID');
268
- });
269
- });
270
- });
@@ -1,91 +0,0 @@
1
- import { DispatcherClient as PbDispatcherClient, AssignedAction } from '@protoc/dispatcher';
2
-
3
- import { Status } from 'nice-grpc';
4
- import { ClientConfig } from '@clients/hatchet-client/client-config';
5
- import sleep from '@util/sleep';
6
- import HatchetError from '@util/errors/hatchet-error';
7
- import { DispatcherClient } from './dispatcher-client';
8
-
9
- const DEFAULT_ACTION_LISTENER_RETRY_INTERVAL = 5; // seconds
10
- const DEFAULT_ACTION_LISTENER_RETRY_COUNT = 5;
11
-
12
- export interface Action {
13
- tenantId: string;
14
- jobId: string;
15
- jobName: string;
16
- jobRunId: string;
17
- stepId: string;
18
- stepRunId: string;
19
- actionId: string;
20
- actionType: number;
21
- actionPayload: string;
22
- }
23
-
24
- export class ActionListener {
25
- config: ClientConfig;
26
- client: PbDispatcherClient;
27
- listener: AsyncIterable<AssignedAction>;
28
- workerId: string;
29
-
30
- constructor(client: DispatcherClient, listener: AsyncIterable<AssignedAction>, workerId: string) {
31
- this.config = client.config;
32
- this.client = client.client;
33
- this.listener = listener;
34
- this.workerId = workerId;
35
- }
36
-
37
- actions = () =>
38
- (async function* gen(client: ActionListener) {
39
- while (true) {
40
- try {
41
- for await (const assignedAction of client.listener) {
42
- const action: Action = {
43
- ...assignedAction,
44
- };
45
-
46
- yield action;
47
- }
48
- } catch (e: any) {
49
- if (e.code === Status.CANCELLED) {
50
- break;
51
- }
52
- if (e.code === Status.UNAVAILABLE) {
53
- client.retrySubscribe();
54
- }
55
- break;
56
- }
57
- }
58
- })(this);
59
-
60
- async retrySubscribe() {
61
- let retries = 0;
62
-
63
- while (retries < DEFAULT_ACTION_LISTENER_RETRY_COUNT) {
64
- try {
65
- await sleep(DEFAULT_ACTION_LISTENER_RETRY_INTERVAL);
66
-
67
- this.listener = this.client.listen({
68
- workerId: this.workerId,
69
- });
70
-
71
- return;
72
- } catch (e: any) {
73
- retries += 1;
74
- }
75
- }
76
-
77
- throw new HatchetError(
78
- `Could not subscribe to the worker after ${DEFAULT_ACTION_LISTENER_RETRY_COUNT} retries`
79
- );
80
- }
81
-
82
- async unregister() {
83
- try {
84
- return this.client.unsubscribe({
85
- workerId: this.workerId,
86
- });
87
- } catch (e: any) {
88
- throw new HatchetError(`Failed to unsubscribe: ${e.message}`);
89
- }
90
- }
91
- }
@@ -1,111 +0,0 @@
1
- import { ActionEventType } from '@protoc/dispatcher';
2
- import { DispatcherClient } from './dispatcher-client';
3
- import { mockChannel, mockFactory } from '../hatchet-client/hatchet-client.test';
4
-
5
- let client: DispatcherClient;
6
-
7
- describe('DispatcherClient', () => {
8
- it('should create a client', () => {
9
- const x = new DispatcherClient(
10
- {
11
- token: 'TOKEN',
12
-
13
- host_port: 'HOST_PORT',
14
- log_level: 'OFF',
15
- tls_config: {
16
- cert_file: 'TLS_CERT_FILE',
17
- key_file: 'TLS_KEY_FILE',
18
- ca_file: 'TLS_ROOT_CA_FILE',
19
- server_name: 'TLS_SERVER_NAME',
20
- },
21
- },
22
- mockChannel,
23
- mockFactory
24
- );
25
-
26
- expect(x).toBeDefined();
27
- });
28
-
29
- beforeEach(() => {
30
- client = new DispatcherClient(
31
- {
32
- token: 'TOKEN',
33
-
34
- host_port: 'HOST_PORT',
35
- log_level: 'OFF',
36
- tls_config: {
37
- cert_file: 'TLS_CERT_FILE',
38
- key_file: 'TLS_KEY_FILE',
39
- ca_file: 'TLS_ROOT_CA_FILE',
40
- server_name: 'TLS_SERVER_NAME',
41
- },
42
- },
43
- mockChannel,
44
- mockFactory
45
- );
46
- });
47
-
48
- describe('get_action_listener', () => {
49
- it('should register the worker', async () => {
50
- const clientSpy = jest.spyOn(client.client, 'register').mockResolvedValue({
51
- workerId: 'WORKER_ID',
52
- tenantId: 'TENANT_ID',
53
- workerName: 'WORKER_NAME',
54
- });
55
-
56
- const listenerSpy = jest.spyOn(client.client, 'listen');
57
-
58
- const listener = await client.get_action_listener({
59
- workerName: 'WORKER_NAME',
60
- services: ['SERVICE'],
61
- actions: ['ACTION'],
62
- });
63
-
64
- expect(clientSpy).toHaveBeenCalledWith({
65
- workerName: 'WORKER_NAME',
66
- services: ['SERVICE'],
67
- actions: ['ACTION'],
68
- });
69
-
70
- expect(listenerSpy).toHaveBeenCalledWith({
71
- workerId: 'WORKER_ID',
72
- });
73
-
74
- expect(listener).toBeDefined();
75
- expect(listener.workerId).toEqual('WORKER_ID');
76
- });
77
- });
78
-
79
- describe('send_action_event', () => {
80
- it('should send action events', () => {
81
- const clientSpy = jest.spyOn(client.client, 'sendActionEvent').mockResolvedValue({
82
- tenantId: 'TENANT_ID',
83
- workerId: 'WORKER_ID',
84
- });
85
-
86
- client.send_action_event({
87
- workerId: 'WORKER_ID',
88
- actionId: 'ACTION_ID',
89
- eventType: ActionEventType.STEP_EVENT_TYPE_COMPLETED,
90
- eventPayload: '{"foo":"bar"}',
91
- eventTimestamp: new Date(),
92
- jobId: 'a',
93
- jobRunId: 'b',
94
- stepId: 'c',
95
- stepRunId: 'd',
96
- });
97
-
98
- expect(clientSpy).toHaveBeenCalledWith({
99
- workerId: 'WORKER_ID',
100
- actionId: 'ACTION_ID',
101
- eventType: ActionEventType.STEP_EVENT_TYPE_COMPLETED,
102
- eventPayload: '{"foo":"bar"}',
103
- jobId: 'a',
104
- jobRunId: 'b',
105
- stepId: 'c',
106
- stepRunId: 'd',
107
- eventTimestamp: expect.any(Object),
108
- });
109
- });
110
- });
111
- });
@@ -1,47 +0,0 @@
1
- import { Channel, ClientFactory } from 'nice-grpc';
2
- import {
3
- DispatcherClient as PbDispatcherClient,
4
- DispatcherDefinition,
5
- ActionEvent,
6
- } from '@protoc/dispatcher';
7
- import { ClientConfig } from '@clients/hatchet-client/client-config';
8
- import HatchetError from '@util/errors/hatchet-error';
9
- import { ActionListener } from './action-listener';
10
-
11
- interface GetActionListenerOptions {
12
- workerName: string;
13
- services: string[];
14
- actions: string[];
15
- }
16
-
17
- export class DispatcherClient {
18
- config: ClientConfig;
19
- client: PbDispatcherClient;
20
-
21
- constructor(config: ClientConfig, channel: Channel, factory: ClientFactory) {
22
- this.config = config;
23
- this.client = factory.create(DispatcherDefinition, channel);
24
- }
25
-
26
- async get_action_listener(options: GetActionListenerOptions) {
27
- // Register the worker
28
- const registration = await this.client.register({
29
- ...options,
30
- });
31
-
32
- // Subscribe to the worker
33
- const listener = this.client.listen({
34
- workerId: registration.workerId,
35
- });
36
-
37
- return new ActionListener(this, listener, registration.workerId);
38
- }
39
-
40
- async send_action_event(in_: ActionEvent) {
41
- try {
42
- return this.client.sendActionEvent(in_);
43
- } catch (e: any) {
44
- throw new HatchetError(e.message);
45
- }
46
- }
47
- }
@@ -1,72 +0,0 @@
1
- import HatchetError from '@util/errors/hatchet-error';
2
- import { EventClient } from './event-client';
3
- import { mockChannel, mockFactory } from '../hatchet-client/hatchet-client.test';
4
-
5
- let client: EventClient;
6
-
7
- describe('EventClient', () => {
8
- it('should create a client', () => {
9
- const x = new EventClient(
10
- {
11
- token: 'TOKEN',
12
- host_port: 'HOST_PORT',
13
- tls_config: {
14
- cert_file: 'TLS_CERT_FILE',
15
- key_file: 'TLS_KEY_FILE',
16
- ca_file: 'TLS_ROOT_CA_FILE',
17
- server_name: 'TLS_SERVER_NAME',
18
- },
19
- },
20
- mockChannel,
21
- mockFactory
22
- );
23
-
24
- expect(x).toBeDefined();
25
- });
26
-
27
- beforeEach(() => {
28
- client = new EventClient(
29
- {
30
- token: 'TOKEN',
31
- host_port: 'HOST_PORT',
32
- tls_config: {
33
- cert_file: 'TLS_CERT_FILE',
34
- key_file: 'TLS_KEY_FILE',
35
- ca_file: 'TLS_ROOT_CA_FILE',
36
- server_name: 'TLS_SERVER_NAME',
37
- },
38
- },
39
- mockChannel,
40
- mockFactory
41
- );
42
- });
43
-
44
- it('should push events', () => {
45
- const clientSpy = jest.spyOn(client.client, 'push').mockResolvedValue({
46
- tenantId: 'x',
47
- eventId: 'y',
48
- key: 'z',
49
- eventTimestamp: new Date(),
50
- payload: 'string',
51
- });
52
-
53
- client.push('type', { foo: 'bar' });
54
-
55
- expect(clientSpy).toHaveBeenCalledWith({
56
- key: 'type',
57
- payload: '{"foo":"bar"}',
58
- eventTimestamp: expect.any(Date),
59
- });
60
- });
61
-
62
- it('should throw an error when push fails', () => {
63
- const clientSpy = jest.spyOn(client.client, 'push');
64
- clientSpy.mockImplementation(() => {
65
- throw new Error('foo');
66
- });
67
-
68
- expect(() => {
69
- client.push('type', { foo: 'bar' });
70
- }).toThrow(new HatchetError('foo'));
71
- });
72
- });
@@ -1,32 +0,0 @@
1
- import { Channel, ClientFactory } from 'nice-grpc';
2
- import {
3
- EventsServiceClient,
4
- EventsServiceDefinition,
5
- PushEventRequest,
6
- } from '@protoc/events/events';
7
- import HatchetError from '@util/errors/hatchet-error';
8
- import { ClientConfig } from '@clients/hatchet-client/client-config';
9
-
10
- export class EventClient {
11
- config: ClientConfig;
12
- client: EventsServiceClient;
13
-
14
- constructor(config: ClientConfig, channel: Channel, factory: ClientFactory) {
15
- this.config = config;
16
- this.client = factory.create(EventsServiceDefinition, channel);
17
- }
18
-
19
- push<T>(type: string, input: T) {
20
- const req: PushEventRequest = {
21
- key: type,
22
- payload: JSON.stringify(input),
23
- eventTimestamp: new Date(),
24
- };
25
-
26
- try {
27
- return this.client.push(req);
28
- } catch (e: any) {
29
- throw new HatchetError(e.message);
30
- }
31
- }
32
- }
@@ -1,22 +0,0 @@
1
- import { ChannelCredentials } from 'nice-grpc';
2
- import { z } from 'zod';
3
-
4
- const ClientTLSConfigSchema = z.object({
5
- tls_strategy: z.enum(['tls', 'mtls']).optional(),
6
- cert_file: z.string().optional(),
7
- ca_file: z.string().optional(),
8
- key_file: z.string().optional(),
9
- server_name: z.string().optional(),
10
- });
11
-
12
- export const ClientConfigSchema = z.object({
13
- token: z.string(),
14
- tls_config: ClientTLSConfigSchema,
15
- host_port: z.string(),
16
- log_level: z.enum(['OFF', 'DEBUG', 'INFO', 'WARN', 'ERROR']).optional(),
17
- });
18
-
19
- export type ClientConfig = z.infer<typeof ClientConfigSchema> & {
20
- credentials?: ChannelCredentials;
21
- };
22
- export type ClientTLSConfig = z.infer<typeof ClientTLSConfigSchema>;
@@ -1,6 +0,0 @@
1
- tenant_id: 'TENANT_ID_YAML'
2
- host_port: 'HOST_PORT_YAML'
3
- tls_config:
4
- cert_file: 'TLS_CERT_FILE_YAML'
5
- key_file: 'TLS_KEY_FILE_YAML'
6
- ca_file: 'TLS_ROOT_CA_FILE_YAML'
@@ -1,8 +0,0 @@
1
- token: 'TOKEN_YAML'
2
- tenant_id: 'TENANT_ID_YAML'
3
- host_port: 'HOST_PORT_YAML'
4
- tls_config:
5
- cert_file: 'TLS_CERT_FILE_YAML'
6
- key_file: 'TLS_KEY_FILE_YAML'
7
- ca_file: 'TLS_ROOT_CA_FILE_YAML'
8
- server_name: 'TLS_SERVER_NAME_YAML'