duron 0.1.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 (82) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +140 -0
  3. package/dist/action-job.d.ts +24 -0
  4. package/dist/action-job.d.ts.map +1 -0
  5. package/dist/action-job.js +108 -0
  6. package/dist/action-manager.d.ts +21 -0
  7. package/dist/action-manager.d.ts.map +1 -0
  8. package/dist/action-manager.js +78 -0
  9. package/dist/action.d.ts +129 -0
  10. package/dist/action.d.ts.map +1 -0
  11. package/dist/action.js +87 -0
  12. package/dist/adapters/adapter.d.ts +92 -0
  13. package/dist/adapters/adapter.d.ts.map +1 -0
  14. package/dist/adapters/adapter.js +424 -0
  15. package/dist/adapters/postgres/drizzle.config.d.ts +3 -0
  16. package/dist/adapters/postgres/drizzle.config.d.ts.map +1 -0
  17. package/dist/adapters/postgres/drizzle.config.js +10 -0
  18. package/dist/adapters/postgres/pglite.d.ts +13 -0
  19. package/dist/adapters/postgres/pglite.d.ts.map +1 -0
  20. package/dist/adapters/postgres/pglite.js +36 -0
  21. package/dist/adapters/postgres/postgres.d.ts +51 -0
  22. package/dist/adapters/postgres/postgres.d.ts.map +1 -0
  23. package/dist/adapters/postgres/postgres.js +867 -0
  24. package/dist/adapters/postgres/schema.d.ts +581 -0
  25. package/dist/adapters/postgres/schema.d.ts.map +1 -0
  26. package/dist/adapters/postgres/schema.default.d.ts +577 -0
  27. package/dist/adapters/postgres/schema.default.d.ts.map +1 -0
  28. package/dist/adapters/postgres/schema.default.js +3 -0
  29. package/dist/adapters/postgres/schema.js +87 -0
  30. package/dist/adapters/schemas.d.ts +516 -0
  31. package/dist/adapters/schemas.d.ts.map +1 -0
  32. package/dist/adapters/schemas.js +184 -0
  33. package/dist/client.d.ts +85 -0
  34. package/dist/client.d.ts.map +1 -0
  35. package/dist/client.js +416 -0
  36. package/dist/constants.d.ts +14 -0
  37. package/dist/constants.d.ts.map +1 -0
  38. package/dist/constants.js +22 -0
  39. package/dist/errors.d.ts +43 -0
  40. package/dist/errors.d.ts.map +1 -0
  41. package/dist/errors.js +75 -0
  42. package/dist/index.d.ts +8 -0
  43. package/dist/index.d.ts.map +1 -0
  44. package/dist/index.js +6 -0
  45. package/dist/server.d.ts +1193 -0
  46. package/dist/server.d.ts.map +1 -0
  47. package/dist/server.js +516 -0
  48. package/dist/step-manager.d.ts +46 -0
  49. package/dist/step-manager.d.ts.map +1 -0
  50. package/dist/step-manager.js +216 -0
  51. package/dist/utils/checksum.d.ts +2 -0
  52. package/dist/utils/checksum.d.ts.map +1 -0
  53. package/dist/utils/checksum.js +6 -0
  54. package/dist/utils/p-retry.d.ts +19 -0
  55. package/dist/utils/p-retry.d.ts.map +1 -0
  56. package/dist/utils/p-retry.js +130 -0
  57. package/dist/utils/wait-for-abort.d.ts +5 -0
  58. package/dist/utils/wait-for-abort.d.ts.map +1 -0
  59. package/dist/utils/wait-for-abort.js +32 -0
  60. package/migrations/postgres/0000_lethal_speed_demon.sql +64 -0
  61. package/migrations/postgres/meta/0000_snapshot.json +606 -0
  62. package/migrations/postgres/meta/_journal.json +13 -0
  63. package/package.json +88 -0
  64. package/src/action-job.ts +201 -0
  65. package/src/action-manager.ts +166 -0
  66. package/src/action.ts +247 -0
  67. package/src/adapters/adapter.ts +969 -0
  68. package/src/adapters/postgres/drizzle.config.ts +11 -0
  69. package/src/adapters/postgres/pglite.ts +86 -0
  70. package/src/adapters/postgres/postgres.ts +1346 -0
  71. package/src/adapters/postgres/schema.default.ts +5 -0
  72. package/src/adapters/postgres/schema.ts +119 -0
  73. package/src/adapters/schemas.ts +320 -0
  74. package/src/client.ts +859 -0
  75. package/src/constants.ts +37 -0
  76. package/src/errors.ts +205 -0
  77. package/src/index.ts +14 -0
  78. package/src/server.ts +718 -0
  79. package/src/step-manager.ts +471 -0
  80. package/src/utils/checksum.ts +7 -0
  81. package/src/utils/p-retry.ts +213 -0
  82. package/src/utils/wait-for-abort.ts +40 -0
@@ -0,0 +1,37 @@
1
+ // ============================================================================
2
+ // Job Status Constants
3
+ // ============================================================================
4
+
5
+ export const JOB_STATUS_CREATED = 'created' as const
6
+ export const JOB_STATUS_ACTIVE = 'active' as const
7
+ export const JOB_STATUS_COMPLETED = 'completed' as const
8
+ export const JOB_STATUS_FAILED = 'failed' as const
9
+ export const JOB_STATUS_CANCELLED = 'cancelled' as const
10
+
11
+ export const JOB_STATUSES = [
12
+ JOB_STATUS_CREATED,
13
+ JOB_STATUS_ACTIVE,
14
+ JOB_STATUS_COMPLETED,
15
+ JOB_STATUS_FAILED,
16
+ JOB_STATUS_CANCELLED,
17
+ ] as const
18
+
19
+ export type JobStatus = (typeof JOB_STATUSES)[number]
20
+
21
+ // ============================================================================
22
+ // Step Status Constants
23
+ // ============================================================================
24
+
25
+ export const STEP_STATUS_ACTIVE = 'active' as const
26
+ export const STEP_STATUS_COMPLETED = 'completed' as const
27
+ export const STEP_STATUS_FAILED = 'failed' as const
28
+ export const STEP_STATUS_CANCELLED = 'cancelled' as const
29
+
30
+ export const STEP_STATUSES = [
31
+ STEP_STATUS_ACTIVE,
32
+ STEP_STATUS_COMPLETED,
33
+ STEP_STATUS_FAILED,
34
+ STEP_STATUS_CANCELLED,
35
+ ] as const
36
+
37
+ export type StepStatus = (typeof STEP_STATUSES)[number]
package/src/errors.ts ADDED
@@ -0,0 +1,205 @@
1
+ /**
2
+ * Base class for all built-in errors in Duron.
3
+ * All errors include a cause property that can be serialized.
4
+ */
5
+ export abstract class DuronError extends Error {
6
+ /**
7
+ * The underlying cause of the error, if any.
8
+ *
9
+ * This will be serialized and stored in the database.
10
+ */
11
+ public override readonly cause?: unknown
12
+
13
+ constructor(
14
+ message: string,
15
+ options?: {
16
+ /**
17
+ * The underlying cause of the error, if any.
18
+ *
19
+ * This will be serialized and stored in the database.
20
+ */
21
+ cause?: unknown
22
+ },
23
+ ) {
24
+ super(message)
25
+ this.cause = options?.cause
26
+ // Set the name to the class name
27
+ this.name = this.constructor.name
28
+ // Ensure stack trace points to the error location
29
+ if (Error.captureStackTrace) {
30
+ Error.captureStackTrace(this, this.constructor)
31
+ }
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Error thrown when attempting to execute a step that has already been executed.
37
+ */
38
+ export class StepAlreadyExecutedError extends DuronError {
39
+ /**
40
+ * Create a new StepAlreadyExecutedError.
41
+ *
42
+ * @param stepName - The name of the step that was already executed
43
+ * @param jobId - The ID of the job containing the step
44
+ * @param actionName - The name of the action containing the step
45
+ */
46
+ constructor(stepName: string, jobId: string, actionName: string) {
47
+ super(`Step "${stepName}" has already been executed for job "${jobId}" and action "${actionName}"`)
48
+ }
49
+ }
50
+
51
+ /**
52
+ * NonRetriableError indicates that a step should not be retried.
53
+ *
54
+ * If a step handler throws this error, the step will fail immediately
55
+ * without retrying, even if retry options are configured.
56
+ */
57
+ export class NonRetriableError extends DuronError {
58
+ // Constructor inherited from DuronError
59
+ }
60
+
61
+ /**
62
+ * Error thrown when an action exceeds its timeout.
63
+ */
64
+ export class ActionTimeoutError extends DuronError {
65
+ /**
66
+ * Create a new ActionTimeoutError.
67
+ *
68
+ * @param actionName - The name of the action that timed out
69
+ * @param timeoutMs - The timeout value in milliseconds
70
+ * @param options - Optional error options including cause
71
+ */
72
+ constructor(
73
+ actionName: string,
74
+ timeoutMs: number,
75
+ options?: {
76
+ cause?: unknown
77
+ },
78
+ ) {
79
+ super(`Action "${actionName}" timed out after ${timeoutMs}ms`, options)
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Error thrown when a step exceeds its timeout.
85
+ */
86
+ export class StepTimeoutError extends DuronError {
87
+ /**
88
+ * Create a new StepTimeoutError.
89
+ *
90
+ * @param stepName - The name of the step that timed out
91
+ * @param jobId - The ID of the job containing the step
92
+ * @param timeoutMs - The timeout value in milliseconds
93
+ * @param options - Optional error options including cause
94
+ */
95
+ constructor(
96
+ stepName: string,
97
+ jobId: string,
98
+ timeoutMs: number,
99
+ options?: {
100
+ cause?: unknown
101
+ },
102
+ ) {
103
+ super(`Step "${stepName}" in job "${jobId}" timed out after ${timeoutMs}ms`, options)
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Error thrown when an action is cancelled.
109
+ */
110
+ export class ActionCancelError extends DuronError {
111
+ /**
112
+ * Create a new ActionCancelError.
113
+ *
114
+ * @param actionName - The name of the action that was cancelled
115
+ * @param jobId - The ID of the job containing the action
116
+ * @param options - Optional error options including cause
117
+ */
118
+ constructor(
119
+ actionName: string,
120
+ jobId: string,
121
+ options?: {
122
+ cause?: unknown
123
+ },
124
+ ) {
125
+ super(`Action "${actionName}" in job "${jobId}" was cancelled`, options)
126
+ }
127
+ }
128
+
129
+ /**
130
+ * Checks if an error is a DuronError instance.
131
+ */
132
+ export function isDuronError(error: unknown): error is DuronError {
133
+ return error instanceof DuronError
134
+ }
135
+
136
+ /**
137
+ * Checks if an error is a NonRetriableError instance.
138
+ */
139
+ export function isNonRetriableError(error: unknown): error is NonRetriableError {
140
+ return error instanceof NonRetriableError || error instanceof ActionCancelError || error instanceof ActionTimeoutError
141
+ }
142
+
143
+ /**
144
+ * Checks if an error is a timeout error (ActionTimeoutError or StepTimeoutError).
145
+ */
146
+ export function isTimeoutError(error: unknown): error is ActionTimeoutError | StepTimeoutError {
147
+ return error instanceof ActionTimeoutError || error instanceof StepTimeoutError
148
+ }
149
+
150
+ /**
151
+ * Checks if an error is a cancel error (ActionCancelError or StepCancelError).
152
+ */
153
+ export function isCancelError(error: unknown): error is ActionCancelError {
154
+ return error instanceof ActionCancelError
155
+ }
156
+
157
+ export type SerializableError = {
158
+ name: string
159
+ message: string
160
+ cause?: unknown
161
+ stack?: string
162
+ }
163
+
164
+ /**
165
+ * Serializes an error for storage in the database.
166
+ * Handles DuronError instances specially to preserve their type information.
167
+ */
168
+ export function serializeError(error: unknown): {
169
+ name: string
170
+ message: string
171
+ cause?: unknown
172
+ stack?: string
173
+ } {
174
+ if (error instanceof StepTimeoutError || error instanceof ActionTimeoutError) {
175
+ return {
176
+ name: error.name,
177
+ message: error.message,
178
+ cause: error.cause,
179
+ stack: undefined,
180
+ }
181
+ }
182
+
183
+ if (error instanceof DuronError) {
184
+ return {
185
+ name: error.name,
186
+ message: error.message,
187
+ cause: error.cause,
188
+ stack: error.stack,
189
+ }
190
+ }
191
+
192
+ if (error instanceof Error) {
193
+ return {
194
+ name: error.name,
195
+ message: error.message,
196
+ cause: (error as any).cause,
197
+ stack: error.stack,
198
+ }
199
+ }
200
+
201
+ return {
202
+ name: 'UnknownError',
203
+ message: String(error),
204
+ }
205
+ }
package/src/index.ts ADDED
@@ -0,0 +1,14 @@
1
+ import type { Action } from './action.js'
2
+ import { Client, type ClientOptions } from './client.js'
3
+
4
+ export { defineAction } from './action.js'
5
+ export * from './constants.js'
6
+ export { NonRetriableError } from './errors.js'
7
+ export * from './server.js'
8
+
9
+ export const duron = <
10
+ TActions extends Record<string, Action<any, any, TVariables>>,
11
+ TVariables = Record<string, unknown>,
12
+ >(
13
+ options: ClientOptions<TActions, TVariables>,
14
+ ) => new Client<TActions, TVariables>(options)