ocel 0.0.1-alpha.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (271) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +2 -0
  3. package/README.md +98 -0
  4. package/dist/binding/binding.d.ts +22 -0
  5. package/dist/binding/binding.js +60 -0
  6. package/dist/binding/unprovisioned.d.ts +17 -0
  7. package/dist/binding/unprovisioned.js +25 -0
  8. package/dist/bucket/bucket-client.d.ts +4 -0
  9. package/dist/bucket/bucket-client.js +5 -0
  10. package/dist/bucket/bucket-context.d.ts +8 -0
  11. package/dist/bucket/bucket-context.js +10 -0
  12. package/dist/bucket/bucket.d.ts +60 -0
  13. package/dist/bucket/bucket.js +91 -0
  14. package/dist/bucket/client.d.ts +49 -0
  15. package/dist/bucket/client.js +95 -0
  16. package/dist/bucket/errors.d.ts +12 -0
  17. package/dist/bucket/errors.js +20 -0
  18. package/dist/bucket/express.d.ts +10 -0
  19. package/dist/bucket/express.js +24 -0
  20. package/dist/bucket/hono.d.ts +18 -0
  21. package/dist/bucket/hono.js +13 -0
  22. package/dist/bucket/index.d.ts +8 -0
  23. package/dist/bucket/index.js +6 -0
  24. package/dist/bucket/keys.d.ts +5 -0
  25. package/dist/bucket/keys.js +24 -0
  26. package/dist/bucket/metadata.d.ts +6 -0
  27. package/dist/bucket/metadata.js +6 -0
  28. package/dist/bucket/next.d.ts +15 -0
  29. package/dist/bucket/next.js +11 -0
  30. package/dist/bucket/objects.d.ts +130 -0
  31. package/dist/bucket/objects.js +350 -0
  32. package/dist/bucket/route.d.ts +13 -0
  33. package/dist/bucket/route.js +247 -0
  34. package/dist/bucket/server-only.js +5 -0
  35. package/dist/bucket/types.d.ts +63 -0
  36. package/dist/bucket/uploader.d.ts +3 -0
  37. package/dist/bucket/uploader.js +6 -0
  38. package/dist/build-env.d.ts +17 -0
  39. package/dist/build-env.js +41 -0
  40. package/dist/config.d.ts +30 -0
  41. package/dist/config.js +9 -0
  42. package/dist/declaration/callsite.d.ts +15 -0
  43. package/dist/declaration/callsite.js +43 -0
  44. package/dist/declaration/component.d.ts +4 -0
  45. package/dist/declaration/component.js +1 -0
  46. package/dist/declaration/defer.d.ts +5 -0
  47. package/dist/declaration/defer.js +9 -0
  48. package/dist/delivery/duration.d.ts +13 -0
  49. package/dist/delivery/duration.js +36 -0
  50. package/dist/delivery/lane.d.ts +4 -0
  51. package/dist/delivery/lane.js +15 -0
  52. package/dist/delivery/payload.d.ts +6 -0
  53. package/dist/delivery/payload.js +14 -0
  54. package/dist/delivery/retry.d.ts +16 -0
  55. package/dist/delivery/retry.js +10 -0
  56. package/dist/delivery/schema.d.ts +10 -0
  57. package/dist/delivery/schema.js +20 -0
  58. package/dist/dns.d.ts +11 -0
  59. package/dist/dns.js +4 -0
  60. package/dist/edge.d.ts +4 -0
  61. package/dist/edge.js +4 -0
  62. package/dist/env/access.d.ts +22 -0
  63. package/dist/env/access.js +49 -0
  64. package/dist/env/client.d.ts +5 -0
  65. package/dist/env/client.js +10 -0
  66. package/dist/env/declare.d.ts +2 -0
  67. package/dist/env/declare.js +83 -0
  68. package/dist/env/definition.d.ts +46 -0
  69. package/dist/env/definition.js +164 -0
  70. package/dist/env/deployment.d.ts +23 -0
  71. package/dist/env/deployment.js +13 -0
  72. package/dist/env/edge.d.ts +10 -0
  73. package/dist/env/edge.js +30 -0
  74. package/dist/env/errors.d.ts +9 -0
  75. package/dist/env/errors.js +9 -0
  76. package/dist/env/file.d.ts +1 -0
  77. package/dist/env/file.js +27 -0
  78. package/dist/env/index.d.ts +10 -0
  79. package/dist/env/index.js +63 -0
  80. package/dist/env/live.d.ts +3 -0
  81. package/dist/env/live.js +12 -0
  82. package/dist/env/schema.d.ts +27 -0
  83. package/dist/env/schema.js +43 -0
  84. package/dist/env/scope.d.ts +7 -0
  85. package/dist/env/scope.js +45 -0
  86. package/dist/env/standard.d.ts +10 -0
  87. package/dist/env/standard.js +21 -0
  88. package/dist/env/value.d.ts +5 -0
  89. package/dist/env/value.js +26 -0
  90. package/dist/env-source.d.ts +17 -0
  91. package/dist/env-source.js +16 -0
  92. package/dist/gen/proto/app/bucket/v1/bucket_pb.d.ts +911 -0
  93. package/dist/gen/proto/app/bucket/v1/bucket_pb.js +253 -0
  94. package/dist/gen/proto/app/realtime/v1/realtime_pb.d.ts +50 -0
  95. package/dist/gen/proto/app/realtime/v1/realtime_pb.js +23 -0
  96. package/dist/gen/proto/app/resources/v1/resources_pb.d.ts +554 -0
  97. package/dist/gen/proto/app/resources/v1/resources_pb.js +214 -0
  98. package/dist/gen/proto/app/resources/v1/variables_pb.d.ts +231 -0
  99. package/dist/gen/proto/app/resources/v1/variables_pb.js +101 -0
  100. package/dist/gen/proto/app/task/v1/task_pb.d.ts +498 -0
  101. package/dist/gen/proto/app/task/v1/task_pb.js +151 -0
  102. package/dist/gen/proto/app/topic/v1/topic_pb.d.ts +456 -0
  103. package/dist/gen/proto/app/topic/v1/topic_pb.js +125 -0
  104. package/dist/gen/proto/buf/validate/validate_pb.d.ts +4833 -0
  105. package/dist/gen/proto/buf/validate/validate_pb.js +356 -0
  106. package/dist/gen/proto/common/bindings/v1/bindings_pb.d.ts +378 -0
  107. package/dist/gen/proto/common/bindings/v1/bindings_pb.js +140 -0
  108. package/dist/generated/config.d.ts +428 -0
  109. package/dist/generated/config.js +2 -0
  110. package/dist/kv/entries.d.ts +193 -0
  111. package/dist/kv/entries.js +325 -0
  112. package/dist/kv/errors.d.ts +12 -0
  113. package/dist/kv/errors.js +14 -0
  114. package/dist/kv/index.d.ts +6 -0
  115. package/dist/kv/index.js +3 -0
  116. package/dist/kv/ioredis.d.ts +2 -0
  117. package/dist/kv/ioredis.js +20 -0
  118. package/dist/kv/kv.d.ts +84 -0
  119. package/dist/kv/kv.js +177 -0
  120. package/dist/kv/pattern.d.ts +27 -0
  121. package/dist/kv/pattern.js +73 -0
  122. package/dist/kv/ttl.d.ts +22 -0
  123. package/dist/kv/ttl.js +21 -0
  124. package/dist/postgres/index.d.ts +21 -2
  125. package/dist/postgres/index.js +73 -2
  126. package/dist/postgres/pg.d.ts +5 -4
  127. package/dist/postgres/pg.js +16 -12
  128. package/dist/providers/aws/dns.d.ts +11 -0
  129. package/dist/providers/aws/dns.js +4 -0
  130. package/dist/providers/aws/edge.d.ts +6 -0
  131. package/dist/providers/aws/edge.js +8 -0
  132. package/dist/providers/aws/index.d.ts +4 -0
  133. package/dist/providers/aws/index.js +4 -0
  134. package/dist/providers/gcp/edge.d.ts +14 -0
  135. package/dist/providers/gcp/edge.js +14 -0
  136. package/dist/providers/gcp/index.d.ts +9 -0
  137. package/dist/providers/gcp/index.js +9 -0
  138. package/dist/providers/vps/index.d.ts +4 -0
  139. package/dist/providers/vps/index.js +4 -0
  140. package/dist/realtime/client-error.d.ts +30 -0
  141. package/dist/realtime/client-error.js +17 -0
  142. package/dist/realtime/client-event-socket.d.ts +8 -0
  143. package/dist/realtime/client-event-socket.js +169 -0
  144. package/dist/realtime/client-transport.d.ts +29 -0
  145. package/dist/realtime/client-transport.js +13 -0
  146. package/dist/realtime/client.d.ts +150 -0
  147. package/dist/realtime/client.js +345 -0
  148. package/dist/realtime/express.d.ts +18 -0
  149. package/dist/realtime/express.js +83 -0
  150. package/dist/realtime/handler.d.ts +56 -0
  151. package/dist/realtime/handler.js +287 -0
  152. package/dist/realtime/hono.d.ts +19 -0
  153. package/dist/realtime/hono.js +24 -0
  154. package/dist/realtime/index.d.ts +2 -0
  155. package/dist/realtime/index.js +2 -0
  156. package/dist/realtime/next.d.ts +26 -0
  157. package/dist/realtime/next.js +16 -0
  158. package/dist/realtime/realtime.d.ts +181 -0
  159. package/dist/realtime/realtime.js +205 -0
  160. package/dist/realtime/token.d.ts +20 -0
  161. package/dist/realtime/token.js +45 -0
  162. package/dist/realtime/transport.d.ts +11 -0
  163. package/dist/realtime/transport.js +15 -0
  164. package/dist/realtime/wire.d.ts +30 -0
  165. package/dist/realtime/wire.js +104 -0
  166. package/dist/runtime/dev-server.d.ts +8 -0
  167. package/dist/runtime/dev-server.js +8 -0
  168. package/dist/runtime/rpc.d.ts +23 -0
  169. package/dist/runtime/rpc.js +24 -0
  170. package/dist/runtime/transport.d.ts +11 -0
  171. package/dist/runtime/transport.js +35 -0
  172. package/dist/task/errors.d.ts +7 -0
  173. package/dist/task/errors.js +7 -0
  174. package/dist/task/hooks.d.ts +57 -0
  175. package/dist/task/hooks.js +1 -0
  176. package/dist/task/index.d.ts +9 -0
  177. package/dist/task/index.js +4 -0
  178. package/dist/task/runs.d.ts +74 -0
  179. package/dist/task/runs.js +82 -0
  180. package/dist/task/task.d.ts +117 -0
  181. package/dist/task/task.js +109 -0
  182. package/dist/topic/dead-letters.d.ts +53 -0
  183. package/dist/topic/dead-letters.js +62 -0
  184. package/dist/topic/index.d.ts +8 -0
  185. package/dist/topic/index.js +4 -0
  186. package/dist/topic/topic.d.ts +85 -0
  187. package/dist/topic/topic.js +117 -0
  188. package/dist/version.d.ts +2 -0
  189. package/dist/version.js +2 -0
  190. package/dist/worker/context.d.ts +36 -0
  191. package/dist/worker/context.js +1 -0
  192. package/dist/worker/deliver.d.ts +13 -0
  193. package/dist/worker/deliver.js +258 -0
  194. package/dist/worker/index.d.ts +4 -0
  195. package/dist/worker/index.js +2 -0
  196. package/dist/worker/registry.d.ts +28 -0
  197. package/dist/worker/registry.js +22 -0
  198. package/dist/worker/semaphore.d.ts +7 -0
  199. package/dist/worker/semaphore.js +36 -0
  200. package/dist/worker/worker.d.ts +21 -0
  201. package/dist/worker/worker.js +32 -0
  202. package/package.json +158 -57
  203. package/bin/run.js +0 -39
  204. package/dist/blob/bucket.d.ts +0 -55
  205. package/dist/blob/bucket.js +0 -65
  206. package/dist/blob/index.d.ts +0 -8
  207. package/dist/blob/index.js +0 -13
  208. package/dist/blob/integrations/client.d.ts +0 -25
  209. package/dist/blob/integrations/client.js +0 -71
  210. package/dist/blob/integrations/hono.d.ts +0 -31
  211. package/dist/blob/integrations/hono.js +0 -11
  212. package/dist/blob/mime-types.d.ts +0 -1
  213. package/dist/blob/parse.d.ts +0 -16
  214. package/dist/blob/parse.js +0 -69
  215. package/dist/blob/s3-listener.d.ts +0 -7
  216. package/dist/blob/s3-listener.js +0 -35
  217. package/dist/blob/types.d.ts +0 -128
  218. package/dist/blob/types.js +0 -1
  219. package/dist/blob/upload-error.d.ts +0 -35
  220. package/dist/blob/upload-error.js +0 -54
  221. package/dist/blob/uploader.d.ts +0 -67
  222. package/dist/blob/uploader.js +0 -323
  223. package/dist/internal/db/index.d.ts +0 -68
  224. package/dist/internal/db/index.js +0 -73
  225. package/dist/lambda/index.d.ts +0 -2
  226. package/dist/lambda/index.js +0 -4
  227. package/dist/lambda/integrations/express.d.ts +0 -3
  228. package/dist/lambda/integrations/express.js +0 -13
  229. package/dist/lambda/integrations/hono.d.ts +0 -3
  230. package/dist/lambda/integrations/hono.js +0 -13
  231. package/dist/lambda/lambda.d.ts +0 -43
  232. package/dist/lambda/lambda.js +0 -30
  233. package/dist/utils/component.d.ts +0 -3
  234. package/dist/utils/constants.d.ts +0 -2
  235. package/dist/utils/constants.js +0 -2
  236. package/dist/utils/nanoid.d.ts +0 -1
  237. package/dist/utils/nanoid.js +0 -6
  238. package/dist/utils/rpc.d.ts +0 -11
  239. package/dist/utils/rpc.js +0 -17
  240. package/dist/utils/stack.d.ts +0 -4
  241. package/dist/utils/stack.js +0 -23
  242. package/dist/workflow/index.d.ts +0 -3
  243. package/dist/workflow/index.js +0 -4
  244. package/dist/workflow/workflow.d.ts +0 -34
  245. package/dist/workflow/workflow.js +0 -146
  246. package/src/blob/bucket.ts +0 -92
  247. package/src/blob/index.ts +0 -34
  248. package/src/blob/integrations/client.ts +0 -127
  249. package/src/blob/integrations/hono.ts +0 -16
  250. package/src/blob/mime-types.ts +0 -68
  251. package/src/blob/parse.ts +0 -93
  252. package/src/blob/s3-listener.ts +0 -48
  253. package/src/blob/types.ts +0 -142
  254. package/src/blob/upload-error.ts +0 -67
  255. package/src/blob/uploader.ts +0 -426
  256. package/src/internal/db/index.ts +0 -78
  257. package/src/lambda/index.ts +0 -8
  258. package/src/lambda/integrations/express.ts +0 -25
  259. package/src/lambda/integrations/hono.ts +0 -25
  260. package/src/lambda/lambda.ts +0 -94
  261. package/src/postgres/index.ts +0 -5
  262. package/src/postgres/pg.ts +0 -33
  263. package/src/utils/component.ts +0 -3
  264. package/src/utils/constants.ts +0 -2
  265. package/src/utils/nanoid.ts +0 -9
  266. package/src/utils/rpc.ts +0 -28
  267. package/src/utils/stack.ts +0 -26
  268. package/src/workflow/index.ts +0 -9
  269. package/src/workflow/workflow.ts +0 -235
  270. /package/dist/{blob/mime-types.js → bucket/server-only.d.ts} +0 -0
  271. /package/dist/{utils/component.js → bucket/types.js} +0 -0
@@ -0,0 +1,74 @@
1
+ import type { JsonObject } from "@bufbuild/protobuf";
2
+ import { type Duration } from "../delivery/duration.js";
3
+ import type { RunHandle, Task } from "./task.js";
4
+ /** Where a run is in its life. */
5
+ export type RunStatus = "DELAYED" | "QUEUED" | "EXECUTING" | "COMPLETED" | "FAILED" | "CANCELED" | "EXPIRED" | "TIMED_OUT";
6
+ /** A run of a task, as the runs store records it. */
7
+ export interface Run {
8
+ /** The run's id. */
9
+ id: string;
10
+ /** The name the task the run belongs to was declared under. */
11
+ task: string;
12
+ /** Where the run is in its life. */
13
+ status: RunStatus;
14
+ /** The payload it was triggered with. */
15
+ payload: unknown;
16
+ /** The output its `run` returned, once it completed. */
17
+ output: unknown;
18
+ /** The error that failed it, once it failed. */
19
+ error: string | undefined;
20
+ /** How many attempts were made. */
21
+ attempts: number;
22
+ /** The tags it was triggered with. */
23
+ tags: string[];
24
+ /** The metadata it was triggered with. */
25
+ metadata: JsonObject;
26
+ /** When it was triggered. */
27
+ createdAt: Date | undefined;
28
+ /** When it is or was due. */
29
+ dueAt: Date | undefined;
30
+ /** When its first attempt started. */
31
+ startedAt: Date | undefined;
32
+ /** When it ended. */
33
+ finishedAt: Date | undefined;
34
+ /** When it expires if it has not started. */
35
+ expiresAt: Date | undefined;
36
+ }
37
+ /** Which runs to list. */
38
+ export interface ListRunsOptions {
39
+ /** Only the runs of this task, given as its handle or its declared name. */
40
+ task?: Task | string;
41
+ /** Only the runs in this status, or in any of these. */
42
+ status?: RunStatus | RunStatus[];
43
+ /** Only the runs carrying every one of these tags. */
44
+ tags?: string[];
45
+ /** The `nextCursor` of the previous page. */
46
+ cursor?: string;
47
+ /** The most runs on the page. */
48
+ limit?: number;
49
+ }
50
+ /** One page of runs. */
51
+ export interface RunPage {
52
+ /** The runs on this page. */
53
+ runs: Run[];
54
+ /** Passed as `cursor` to read the next page; empty on the last one. */
55
+ nextCursor: string;
56
+ }
57
+ /** When a rescheduled run is due. */
58
+ export interface RescheduleOptions {
59
+ /** How long from now the run is due, or the date it is due at. */
60
+ delay: Duration | Date;
61
+ }
62
+ /** Reads and acts on the runs of this app's tasks. */
63
+ export declare const runs: {
64
+ /** The run with this id. */
65
+ retrieve(id: string): Promise<Run>;
66
+ /** One page of runs, newest first. */
67
+ list(options?: ListRunsOptions): Promise<RunPage>;
68
+ /** Stops the run from making another attempt; an attempt in progress sees its signal abort. */
69
+ cancel(id: string): Promise<Run>;
70
+ /** Starts a new run with the same payload and options, and answers its handle. */
71
+ replay(id: string): Promise<RunHandle>;
72
+ /** Moves a run that has not started to a new due time. */
73
+ reschedule(id: string, options: RescheduleOptions): Promise<Run>;
74
+ };
@@ -0,0 +1,82 @@
1
+ import { timestampDate } from "@bufbuild/protobuf/wkt";
2
+ import { createClient } from "@connectrpc/connect";
3
+ import { unprovisioned, unprovisionedPhase } from "../binding/unprovisioned.js";
4
+ import { encodeDueAt } from "../delivery/duration.js";
5
+ import { decodeJson } from "../delivery/payload.js";
6
+ import { RunStatus as ProtoRunStatus, TaskService, } from "../gen/proto/app/task/v1/task_pb.js";
7
+ import { createRuntimeTransport } from "../runtime/transport.js";
8
+ const decodeTimestamp = (timestamp) => timestamp ? timestampDate(timestamp) : undefined;
9
+ function decodeRun(run) {
10
+ if (!run)
11
+ throw new Error("the ocel runtime answered without a run");
12
+ return {
13
+ id: run.id,
14
+ task: run.task,
15
+ status: ProtoRunStatus[run.status],
16
+ payload: decodeJson(run.payload),
17
+ output: decodeJson(run.output),
18
+ error: run.error || undefined,
19
+ attempts: run.attempts,
20
+ tags: run.tags,
21
+ metadata: decodeJson(run.metadata) ?? {},
22
+ createdAt: decodeTimestamp(run.createdAt),
23
+ dueAt: decodeTimestamp(run.dueAt),
24
+ startedAt: decodeTimestamp(run.startedAt),
25
+ finishedAt: decodeTimestamp(run.finishedAt),
26
+ expiresAt: decodeTimestamp(run.expiresAt),
27
+ };
28
+ }
29
+ function encodeRunStatus(status) {
30
+ const value = ProtoRunStatus[status];
31
+ if (value === undefined) {
32
+ throw new Error(`"${status}" is not a run status`);
33
+ }
34
+ return value;
35
+ }
36
+ function createTaskClient(operation) {
37
+ if (unprovisionedPhase()) {
38
+ throw unprovisioned("runs", operation);
39
+ }
40
+ return createClient(TaskService, createRuntimeTransport());
41
+ }
42
+ /** Reads and acts on the runs of this app's tasks. */
43
+ export const runs = {
44
+ /** The run with this id. */
45
+ async retrieve(id) {
46
+ const { run } = await createTaskClient("retrieve").retrieveRun({ id });
47
+ return decodeRun(run);
48
+ },
49
+ /** One page of runs, newest first. */
50
+ async list(options = {}) {
51
+ const client = createTaskClient("list");
52
+ const statuses = options.status === undefined
53
+ ? []
54
+ : (Array.isArray(options.status) ? options.status : [options.status]).map(encodeRunStatus);
55
+ const page = await client.listRuns({
56
+ task: typeof options.task === "string" ? options.task : (options.task?.name ?? ""),
57
+ statuses,
58
+ tags: options.tags ?? [],
59
+ cursor: options.cursor ?? "",
60
+ limit: options.limit ?? 0,
61
+ });
62
+ return { runs: page.runs.map(decodeRun), nextCursor: page.nextCursor };
63
+ },
64
+ /** Stops the run from making another attempt; an attempt in progress sees its signal abort. */
65
+ async cancel(id) {
66
+ const { run } = await createTaskClient("cancel").cancelRun({ id });
67
+ return decodeRun(run);
68
+ },
69
+ /** Starts a new run with the same payload and options, and answers its handle. */
70
+ async replay(id) {
71
+ const { id: replayed } = await createTaskClient("replay").replayRun({ id });
72
+ return { id: replayed };
73
+ },
74
+ /** Moves a run that has not started to a new due time. */
75
+ async reschedule(id, options) {
76
+ const { run } = await createTaskClient("reschedule").rescheduleRun({
77
+ id,
78
+ dueAt: encodeDueAt(options.delay),
79
+ });
80
+ return decodeRun(run);
81
+ },
82
+ };
@@ -0,0 +1,117 @@
1
+ import type { JsonObject } from "@bufbuild/protobuf";
2
+ import type { StandardSchemaV1 } from "@standard-schema/spec";
3
+ import { type Duration } from "../delivery/duration.js";
4
+ import { type Lane } from "../delivery/lane.js";
5
+ import { type RetryOptions } from "../delivery/retry.js";
6
+ import type { RunOptions } from "../worker/context.js";
7
+ import type { Worker } from "../worker/worker.js";
8
+ import type { TaskHooks } from "./hooks.js";
9
+ /** How many runs a task takes at once, and how long it waits to fill them. */
10
+ export interface BatchOptions {
11
+ /** The most runs one attempt receives. */
12
+ size: number;
13
+ /** How long a batch waits to fill before its runs are delivered. */
14
+ timeout?: Duration;
15
+ }
16
+ /** What a task's `run` receives for a payload, and returns. */
17
+ export type RunFunction<TPayload, TOutput> = (payload: TPayload, options: RunOptions) => TOutput | Promise<TOutput>;
18
+ /** How a task is declared. */
19
+ export interface TaskOptions<TPayload, TOutput> extends TaskHooks<TPayload, TOutput> {
20
+ /** A Standard Schema every payload is validated against before `run` sees it. */
21
+ schema?: StandardSchemaV1;
22
+ /** Does the work of one run, or of one batch when `batch` is set. */
23
+ run: RunFunction<TPayload, TOutput>;
24
+ /** How a failed attempt is retried. */
25
+ retry?: RetryOptions;
26
+ /** The most runs of this task in progress at once. */
27
+ concurrency?: number;
28
+ /** The longest an attempt may take before the run ends as timed out. */
29
+ maxDuration?: Duration;
30
+ /** How long a run may wait to start before it expires. */
31
+ ttl?: Duration;
32
+ /** Runs one at a time per trigger `key`, in the order they were triggered. */
33
+ ordered?: boolean;
34
+ /** Delivers runs in batches; `run` then receives a list of payloads. */
35
+ batch?: BatchOptions;
36
+ /** The worker that serves this task; without one, the worker named `worker`. */
37
+ worker?: Worker;
38
+ /** A cron expression this task is triggered on. */
39
+ cron?: string;
40
+ }
41
+ /** How one trigger is run. */
42
+ export interface TriggerOptions {
43
+ /** How long to wait before the run is due, or the date it is due at. */
44
+ delay?: Duration | Date;
45
+ /** How long the run may wait to start before it expires. */
46
+ ttl?: Duration;
47
+ /** A trigger repeating a key still remembered returns the run it started instead of a new one. */
48
+ idempotencyKey?: string;
49
+ /** How long `idempotencyKey` is remembered. */
50
+ idempotencyKeyTTL?: Duration;
51
+ /** Triggers sharing `key` within `delay` of each other fold into the first one's run, due `delay` after the last. */
52
+ debounce?: {
53
+ key: string;
54
+ delay: Duration;
55
+ };
56
+ /** Runs of an `ordered` task sharing this key run one at a time, in order. */
57
+ key?: string;
58
+ /** The lane the run waits in. */
59
+ lane?: Lane;
60
+ /** Lowers the task's own `retry.maxAttempts` for this run. */
61
+ maxAttempts?: number;
62
+ /** Tags `runs.list` can filter by. */
63
+ tags?: string[];
64
+ /** Values stored with the run. */
65
+ metadata?: JsonObject;
66
+ }
67
+ /** One item of a `batchTrigger`. */
68
+ export interface BatchTriggerItem<TInput> {
69
+ /** The run's payload. */
70
+ payload: TInput;
71
+ /** How the run is triggered. */
72
+ options?: TriggerOptions;
73
+ }
74
+ /** A triggered run. */
75
+ export interface RunHandle {
76
+ /** The run's id, which `runs` reads and acts on. */
77
+ id: string;
78
+ }
79
+ /** A declared task, and the handle its runs are triggered through. */
80
+ export declare class Task<TInput = unknown, TOutput = unknown> {
81
+ /** The name this task was declared under. */
82
+ readonly name: string;
83
+ /** Type-only: the payload `trigger` takes and the output `run` returns. */
84
+ readonly types?: {
85
+ input: TInput;
86
+ output: TOutput;
87
+ };
88
+ private client;
89
+ constructor(name: string, options: TaskOptions<never, unknown>);
90
+ private ensureClient;
91
+ /** Starts a run of this task with `payload`. */
92
+ trigger(payload: TInput, options?: TriggerOptions): Promise<RunHandle>;
93
+ /** Starts one run per item, and answers their handles in the order of `items`. */
94
+ batchTrigger(items: BatchTriggerItem<TInput>[]): Promise<RunHandle[]>;
95
+ }
96
+ type SchemaOptions<TSchema extends StandardSchemaV1, TPayload, TOutput> = Omit<TaskOptions<TPayload, TOutput>, "schema"> & {
97
+ schema: TSchema;
98
+ };
99
+ /** Declares a task whose payloads `schema` validates, delivered in batches. */
100
+ export declare function task<TSchema extends StandardSchemaV1, TOutput = undefined>(name: string, options: SchemaOptions<TSchema, StandardSchemaV1.InferOutput<TSchema>[], TOutput> & {
101
+ batch: BatchOptions;
102
+ }): Task<StandardSchemaV1.InferInput<TSchema>, TOutput>;
103
+ /** Declares a task whose payloads `schema` validates. */
104
+ export declare function task<TSchema extends StandardSchemaV1, TOutput = undefined>(name: string, options: SchemaOptions<TSchema, StandardSchemaV1.InferOutput<TSchema>, TOutput> & {
105
+ batch?: undefined;
106
+ }): Task<StandardSchemaV1.InferInput<TSchema>, TOutput>;
107
+ /** Declares a task delivered in batches, typed by the list its `run` takes. */
108
+ export declare function task<TPayload = unknown, TOutput = undefined>(name: string, options: TaskOptions<TPayload[], TOutput> & {
109
+ schema?: undefined;
110
+ batch: BatchOptions;
111
+ }): Task<TPayload, TOutput>;
112
+ /** Declares a task, typed by the payload its `run` takes. */
113
+ export declare function task<TPayload = unknown, TOutput = undefined>(name: string, options: TaskOptions<TPayload, TOutput> & {
114
+ schema?: undefined;
115
+ batch?: undefined;
116
+ }): Task<TPayload, TOutput>;
117
+ export {};
@@ -0,0 +1,109 @@
1
+ import { createClient } from "@connectrpc/connect";
2
+ import { refuseUnbound } from "../binding/binding.js";
3
+ import { unprovisioned, unprovisionedPhase } from "../binding/unprovisioned.js";
4
+ import { declarationSite } from "../declaration/callsite.js";
5
+ import { defer } from "../declaration/defer.js";
6
+ import { encodeDueAt, encodeDuration } from "../delivery/duration.js";
7
+ import { encodeLane } from "../delivery/lane.js";
8
+ import { encodePayload } from "../delivery/payload.js";
9
+ import { encodeRetryPolicy } from "../delivery/retry.js";
10
+ import { encodeJsonSchema } from "../delivery/schema.js";
11
+ import { ResourceType } from "../gen/proto/app/resources/v1/resources_pb.js";
12
+ import { TaskService } from "../gen/proto/app/task/v1/task_pb.js";
13
+ import { rpc } from "../runtime/rpc.js";
14
+ import { createRuntimeTransport } from "../runtime/transport.js";
15
+ import { DEFAULT_WORKER, registerRun } from "../worker/registry.js";
16
+ function encodeTriggerOptions(options = {}) {
17
+ return {
18
+ dueAt: encodeDueAt(options.delay),
19
+ ttl: encodeDuration(options.ttl),
20
+ idempotencyKey: options.idempotencyKey ?? "",
21
+ idempotencyKeyTtl: encodeDuration(options.idempotencyKeyTTL),
22
+ debounce: options.debounce && {
23
+ key: options.debounce.key,
24
+ delay: encodeDuration(options.debounce.delay),
25
+ },
26
+ key: options.key ?? "",
27
+ lane: encodeLane(options.lane),
28
+ maxAttempts: options.maxAttempts ?? 0,
29
+ tags: options.tags ?? [],
30
+ metadata: options.metadata && new TextEncoder().encode(JSON.stringify(options.metadata)),
31
+ };
32
+ }
33
+ /** A declared task, and the handle its runs are triggered through. */
34
+ export class Task {
35
+ /** The name this task was declared under. */
36
+ name;
37
+ client;
38
+ constructor(name, options) {
39
+ this.name = name;
40
+ registerRun({
41
+ kind: "task",
42
+ topic: name,
43
+ name,
44
+ worker: options.worker?.name ?? DEFAULT_WORKER,
45
+ batch: options.batch !== undefined,
46
+ schema: options.schema,
47
+ run: options.run,
48
+ hooks: options,
49
+ });
50
+ if (process.env.OCEL_PHASE === "discovery") {
51
+ defer(rpc.resource.declare({
52
+ resource: { name, type: ResourceType.TASK },
53
+ config: {
54
+ case: "task",
55
+ value: {
56
+ schema: encodeJsonSchema(options.schema),
57
+ ordered: options.ordered ?? false,
58
+ retry: encodeRetryPolicy(options.retry),
59
+ concurrency: options.concurrency ?? 0,
60
+ maxDuration: encodeDuration(options.maxDuration),
61
+ ttl: encodeDuration(options.ttl),
62
+ batch: options.batch && {
63
+ size: options.batch.size,
64
+ timeout: encodeDuration(options.batch.timeout),
65
+ },
66
+ worker: options.worker?.name ?? "",
67
+ cron: options.cron ?? "",
68
+ },
69
+ },
70
+ source: declarationSite(),
71
+ }));
72
+ }
73
+ }
74
+ ensureClient(operation) {
75
+ if (unprovisionedPhase()) {
76
+ throw unprovisioned(`task("${this.name}")`, operation);
77
+ }
78
+ const refusal = refuseUnbound(this.name, "task");
79
+ if (refusal) {
80
+ throw refusal;
81
+ }
82
+ return (this.client ??= createClient(TaskService, createRuntimeTransport()));
83
+ }
84
+ /** Starts a run of this task with `payload`. */
85
+ async trigger(payload, options) {
86
+ const client = this.ensureClient("trigger");
87
+ const { id } = await client.trigger({
88
+ task: this.name,
89
+ payload: encodePayload(payload),
90
+ options: encodeTriggerOptions(options),
91
+ });
92
+ return { id };
93
+ }
94
+ /** Starts one run per item, and answers their handles in the order of `items`. */
95
+ async batchTrigger(items) {
96
+ const client = this.ensureClient("batchTrigger");
97
+ const { ids } = await client.batchTrigger({
98
+ task: this.name,
99
+ items: items.map((item) => ({
100
+ payload: encodePayload(item.payload),
101
+ options: encodeTriggerOptions(item.options),
102
+ })),
103
+ });
104
+ return ids.map((id) => ({ id }));
105
+ }
106
+ }
107
+ export function task(name, options) {
108
+ return new Task(name, options);
109
+ }
@@ -0,0 +1,53 @@
1
+ import type { Client } from "@connectrpc/connect";
2
+ import type { TopicService } from "../gen/proto/app/topic/v1/topic_pb.js";
3
+ /** A message a consumer gave up on after its last attempt failed. */
4
+ export interface DeadLetter {
5
+ /** The execution that failed: the message as this consumer received it. */
6
+ execution: string;
7
+ /** The message's id. */
8
+ messageId: string;
9
+ /** When the message was sent. */
10
+ publishedAt: Date | undefined;
11
+ /** The message's payload. */
12
+ payload: unknown;
13
+ /** How many attempts were made. */
14
+ attempts: number;
15
+ /** The last attempt's error. */
16
+ error: string;
17
+ /** When the last attempt failed. */
18
+ failedAt: Date | undefined;
19
+ }
20
+ /** One page of dead letters. */
21
+ export interface DeadLetterPage {
22
+ /** The dead letters on this page. */
23
+ deadLetters: DeadLetter[];
24
+ /** Passed as `cursor` to read the next page; empty on the last one. */
25
+ nextCursor: string;
26
+ }
27
+ /** Which page of dead letters to read. */
28
+ export interface DeadLetterListOptions {
29
+ /** The `nextCursor` of the previous page. */
30
+ cursor?: string;
31
+ /** The most dead letters on the page. */
32
+ limit?: number;
33
+ }
34
+ interface TopicConnection {
35
+ client: Client<typeof TopicService>;
36
+ topic: string;
37
+ }
38
+ /** The dead letters of one consumer of a topic. */
39
+ export declare class DeadLetters {
40
+ /** The consumer these dead letters belong to. */
41
+ readonly consumer: string;
42
+ private readonly connect;
43
+ constructor(consumer: string, connect: (operation: string) => TopicConnection);
44
+ /** One page of this consumer's dead letters. */
45
+ list(options?: DeadLetterListOptions): Promise<DeadLetterPage>;
46
+ /** Delivers the given executions again, or every dead letter; answers how many. */
47
+ redrive(executions?: string[]): Promise<number>;
48
+ /** Deletes the given executions, or every dead letter; answers how many. */
49
+ purge(executions?: string[]): Promise<number>;
50
+ /** How many dead letters this consumer has. */
51
+ count(): Promise<number>;
52
+ }
53
+ export {};
@@ -0,0 +1,62 @@
1
+ import { timestampDate } from "@bufbuild/protobuf/wkt";
2
+ import { decodeJson } from "../delivery/payload.js";
3
+ function decodeDeadLetter(letter) {
4
+ return {
5
+ execution: letter.execution,
6
+ messageId: letter.message?.id ?? "",
7
+ publishedAt: letter.message?.publishedAt
8
+ ? timestampDate(letter.message.publishedAt)
9
+ : undefined,
10
+ payload: decodeJson(letter.payload),
11
+ attempts: letter.attempts,
12
+ error: letter.error,
13
+ failedAt: letter.failedAt ? timestampDate(letter.failedAt) : undefined,
14
+ };
15
+ }
16
+ /** The dead letters of one consumer of a topic. */
17
+ export class DeadLetters {
18
+ /** The consumer these dead letters belong to. */
19
+ consumer;
20
+ connect;
21
+ constructor(consumer, connect) {
22
+ this.consumer = consumer;
23
+ this.connect = connect;
24
+ }
25
+ /** One page of this consumer's dead letters. */
26
+ async list(options = {}) {
27
+ const { client, topic } = this.connect("deadLetter.list");
28
+ const page = await client.listDeadLetters({
29
+ topic,
30
+ consumer: this.consumer,
31
+ cursor: options.cursor ?? "",
32
+ limit: options.limit ?? 0,
33
+ });
34
+ return { deadLetters: page.deadLetters.map(decodeDeadLetter), nextCursor: page.nextCursor };
35
+ }
36
+ /** Delivers the given executions again, or every dead letter; answers how many. */
37
+ async redrive(executions = []) {
38
+ const { client, topic } = this.connect("deadLetter.redrive");
39
+ const { redriven } = await client.redriveDeadLetters({
40
+ topic,
41
+ consumer: this.consumer,
42
+ executions,
43
+ });
44
+ return Number(redriven);
45
+ }
46
+ /** Deletes the given executions, or every dead letter; answers how many. */
47
+ async purge(executions = []) {
48
+ const { client, topic } = this.connect("deadLetter.purge");
49
+ const { purged } = await client.purgeDeadLetters({
50
+ topic,
51
+ consumer: this.consumer,
52
+ executions,
53
+ });
54
+ return Number(purged);
55
+ }
56
+ /** How many dead letters this consumer has. */
57
+ async count() {
58
+ const { client, topic } = this.connect("deadLetter.count");
59
+ const { count } = await client.countDeadLetters({ topic, consumer: this.consumer });
60
+ return Number(count);
61
+ }
62
+ }
@@ -0,0 +1,8 @@
1
+ export { UnprovisionedResourceError } from "../binding/unprovisioned.js";
2
+ export type { Duration } from "../delivery/duration.js";
3
+ export type { Lane } from "../delivery/lane.js";
4
+ export type { RetryOptions } from "../delivery/retry.js";
5
+ export { AbortTaskRunError } from "../task/errors.js";
6
+ export type { RunContext, RunOptions } from "../worker/context.js";
7
+ export { type DeadLetter, type DeadLetterListOptions, type DeadLetterPage, DeadLetters, } from "./dead-letters.js";
8
+ export { type BatchConsumerOptions, type Consumer, type ConsumerFunction, type ConsumerOptions, type SendOptions, Topic, type TopicOptions, topic, } from "./topic.js";
@@ -0,0 +1,4 @@
1
+ export { UnprovisionedResourceError } from "../binding/unprovisioned.js";
2
+ export { AbortTaskRunError } from "../task/errors.js";
3
+ export { DeadLetters, } from "./dead-letters.js";
4
+ export { Topic, topic, } from "./topic.js";
@@ -0,0 +1,85 @@
1
+ import type { StandardSchemaV1 } from "@standard-schema/spec";
2
+ import { type Duration } from "../delivery/duration.js";
3
+ import { type Lane } from "../delivery/lane.js";
4
+ import { type RetryOptions } from "../delivery/retry.js";
5
+ import type { RunOptions } from "../worker/context.js";
6
+ import type { Worker } from "../worker/worker.js";
7
+ import { DeadLetters } from "./dead-letters.js";
8
+ /** How a topic is declared. */
9
+ export interface TopicOptions<TSchema extends StandardSchemaV1 | undefined> {
10
+ /** A Standard Schema every consumer validates a payload against before it sees it. */
11
+ schema?: TSchema;
12
+ /** Delivers messages sharing a send `key` one at a time, in the order they were sent. */
13
+ ordered?: boolean;
14
+ /** How a consumer's failed attempt is retried, unless the consumer says otherwise. */
15
+ retry?: RetryOptions;
16
+ }
17
+ /** How one message is sent. */
18
+ export interface SendOptions {
19
+ /** How long to wait before the message is delivered, or the date it is delivered at. */
20
+ delay?: Duration | Date;
21
+ /** A send repeating a key still remembered is delivered once. */
22
+ idempotencyKey?: string;
23
+ /** Messages of an `ordered` topic sharing this key are delivered one at a time, in order. */
24
+ key?: string;
25
+ /** The lane the message waits in. */
26
+ lane?: Lane;
27
+ }
28
+ /** How a consumer is declared. */
29
+ export interface ConsumerOptions {
30
+ /** How a failed attempt is retried; without it, the topic's `retry`. */
31
+ retry?: RetryOptions;
32
+ /** The most messages this consumer serves at once. */
33
+ concurrency?: number;
34
+ /** The lanes this consumer reads; without them, every lane. */
35
+ lanes?: Lane[];
36
+ /** The longest an attempt may take before it counts as failed. */
37
+ maxDuration?: Duration;
38
+ /** The worker that serves this consumer; without one, the worker named `worker`. */
39
+ worker?: Worker;
40
+ }
41
+ /** How a batch consumer is declared. */
42
+ export interface BatchConsumerOptions extends ConsumerOptions {
43
+ /** The most messages one attempt receives. */
44
+ batchSize: number;
45
+ /** How long a batch waits to fill before it is delivered. */
46
+ batchTimeout?: Duration;
47
+ }
48
+ /** What a consumer does with one message. */
49
+ export type ConsumerFunction<TPayload> = (payload: TPayload, options: RunOptions) => unknown;
50
+ /** A declared consumer of a topic. */
51
+ export interface Consumer {
52
+ /** The name this consumer was declared under. */
53
+ readonly name: string;
54
+ /** The name of the topic it consumes. */
55
+ readonly topic: string;
56
+ }
57
+ /** A declared topic, and the handle its messages are sent through. */
58
+ export declare class Topic<TInput = unknown, TOutput = TInput> {
59
+ /** The name this topic was declared under. */
60
+ readonly name: string;
61
+ /** Type-only: the payload `send` takes and the payload a consumer receives. */
62
+ readonly types?: {
63
+ input: TInput;
64
+ output: TOutput;
65
+ };
66
+ private readonly schema;
67
+ private client;
68
+ constructor(name: string, options: TopicOptions<StandardSchemaV1 | undefined>);
69
+ private ensureClient;
70
+ /** Sends `payload` to every consumer of this topic, and answers the message's id. */
71
+ send(payload: TInput, options?: SendOptions): Promise<string>;
72
+ private declareConsumer;
73
+ /** Declares a consumer that receives every message sent to this topic, one at a time. */
74
+ consumer(name: string, run: ConsumerFunction<TOutput>, options?: ConsumerOptions): Consumer;
75
+ /** Declares a consumer that receives the messages sent to this topic in batches. */
76
+ batchConsumer(name: string, run: ConsumerFunction<TOutput[]>, options: BatchConsumerOptions): Consumer;
77
+ /** The messages a consumer of this topic gave up on. */
78
+ deadLetter(consumer: string | Consumer): DeadLetters;
79
+ }
80
+ /** Declares a topic whose payloads `schema` validates. */
81
+ export declare function topic<TSchema extends StandardSchemaV1>(name: string, options: TopicOptions<TSchema> & {
82
+ schema: TSchema;
83
+ }): Topic<StandardSchemaV1.InferInput<TSchema>, StandardSchemaV1.InferOutput<TSchema>>;
84
+ /** Declares a topic whose payloads are of type `TPayload`. */
85
+ export declare function topic<TPayload = unknown>(name: string, options?: TopicOptions<undefined>): Topic<TPayload, TPayload>;