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
@@ -1,8 +0,0 @@
1
- import { Lambda, type LambdaConfig, type TriggerMap } from "./lambda";
2
-
3
- export function lambda<T extends keyof TriggerMap>(
4
- id: string,
5
- config: LambdaConfig<T>
6
- ) {
7
- return new Lambda(id, config);
8
- }
@@ -1,25 +0,0 @@
1
- import {
2
- Lambda,
3
- type LambdaConfig,
4
- type TriggerMap,
5
- type TriggerUnion,
6
- } from "../lambda";
7
- import sls from "serverless-http";
8
- import type { Express } from "express";
9
-
10
- export function lambda<T extends keyof TriggerMap>(
11
- id: string,
12
- app: Express,
13
- config?: Omit<LambdaConfig<T>, "trigger" | "handler">,
14
- ) {
15
- const handler = sls(app);
16
-
17
- return new Lambda(id, {
18
- ...config,
19
- handler: handler as any,
20
- trigger: {
21
- type: "url",
22
- config: {},
23
- } as Extract<TriggerUnion, { type: T }>,
24
- });
25
- }
@@ -1,25 +0,0 @@
1
- import {
2
- Lambda,
3
- type LambdaConfig,
4
- type TriggerMap,
5
- type TriggerUnion,
6
- } from "../lambda";
7
- import type { Hono } from "hono";
8
- import { handle } from "hono/aws-lambda";
9
-
10
- export function lambda<T extends keyof TriggerMap>(
11
- id: string,
12
- app: Hono,
13
- config?: Omit<LambdaConfig<T>, "trigger" | "handler">,
14
- ) {
15
- const handler = handle(app);
16
-
17
- return new Lambda(id, {
18
- ...config,
19
- handler: handler as any,
20
- trigger: {
21
- type: "url",
22
- config: {},
23
- } as Extract<TriggerUnion, { type: T }>,
24
- });
25
- }
@@ -1,94 +0,0 @@
1
- import type { Component } from "../utils/component";
2
- import { rpc } from "../utils/rpc";
3
- import { getCallerFile } from "../utils/stack";
4
- import type {
5
- Context,
6
- APIGatewayEvent,
7
- S3Event,
8
- LambdaFunctionURLEvent,
9
- } from "aws-lambda";
10
-
11
- export type TriggerMap = {
12
- s3: {
13
- bucket: string;
14
- events: string[];
15
- };
16
- cron: {
17
- schedule: string;
18
- };
19
- url: {
20
- streaming?: boolean;
21
- };
22
- api: {
23
- id?: string;
24
- path: string;
25
- method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "*";
26
- };
27
- };
28
-
29
- export type TriggerUnion = {
30
- [K in keyof TriggerMap]: { type: K; config: TriggerMap[K] };
31
- }[keyof TriggerMap];
32
-
33
- export type LambdaHandlerOutput<T extends keyof TriggerMap> = T extends "api"
34
- ? {
35
- statusCode: number;
36
- headers: Record<string, string>;
37
- body: string;
38
- }
39
- : any;
40
-
41
- export type LambdaEvent<T extends keyof TriggerMap> = T extends "s3"
42
- ? S3Event
43
- : T extends "api"
44
- ? APIGatewayEvent
45
- : T extends "url"
46
- ? LambdaFunctionURLEvent
47
- : any;
48
-
49
- export interface LambdaConfig<T extends keyof TriggerMap> {
50
- handler: (
51
- event: LambdaEvent<T>,
52
- context: Context,
53
- ) => Promise<LambdaHandlerOutput<T>> | LambdaHandlerOutput<T>;
54
-
55
- trigger: Extract<TriggerUnion, { type: T }>;
56
- link?: Component[];
57
- }
58
-
59
- export class Lambda<T extends keyof TriggerMap> implements Component {
60
- constructor(
61
- public id: string,
62
- config: LambdaConfig<T>,
63
- ) {
64
- // remove non-serializable stuff
65
- const { handler, link, ...rest } = config;
66
- const links = link?.map((component) => component.__id()) || [];
67
-
68
- // @ts-expect-error - accessed by cli during build
69
- this.__handler = handler;
70
- // @ts-expect-error - accessed by cli during build
71
- this[Symbol.for("ocel:lambda:id")] = id;
72
-
73
- if (process.env.OCEL_PHASE === "discovery") {
74
- const source = getCallerFile();
75
-
76
- if (!source) {
77
- throw new Error(
78
- "Could not determine caller file for lambda registration",
79
- );
80
- }
81
-
82
- rpc.register({
83
- id: this.id,
84
- type: "lambda",
85
- source,
86
- config: { ...rest, links },
87
- });
88
- }
89
- }
90
-
91
- __id(): string {
92
- return this.id;
93
- }
94
- }
@@ -1,5 +0,0 @@
1
- import { Postgres, type PostgresConfig } from "./pg";
2
-
3
- export function postgres(id: string, config: PostgresConfig) {
4
- return new Postgres(id, config);
5
- }
@@ -1,33 +0,0 @@
1
- import type { Component } from "../utils/component";
2
- import { rpc } from "../utils/rpc";
3
- import { getCallerFile } from "../utils/stack";
4
-
5
- export interface PostgresConfig {
6
- version?: string;
7
- migrations?: string;
8
- }
9
-
10
- export class Postgres implements Component {
11
- constructor(public id: string, config: PostgresConfig) {
12
- // remove non-serializable stuff
13
-
14
- if (process.env.OCEL_PHASE === "discovery") {
15
- const source = getCallerFile();
16
-
17
- rpc.register({
18
- id: this.id,
19
- type: "postgres",
20
- source: source || "unknown",
21
- config: {},
22
- });
23
- }
24
- }
25
-
26
- __id(): string {
27
- return this.id;
28
- }
29
-
30
- async sql(query: string) {
31
- return {};
32
- }
33
- }
@@ -1,3 +0,0 @@
1
- export interface Component {
2
- __id(): string;
3
- }
@@ -1,2 +0,0 @@
1
- export const IS_DISCOVERY_PHASE = process.env.OCEL_PHASE === "discovery";
2
- export const IS_DEV_MODE = process.env.OCEL_DEV === "1";
@@ -1,9 +0,0 @@
1
- import { customAlphabet } from "nanoid";
2
-
3
- export function getNanoid(length = 7) {
4
- const alphabet =
5
- "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
6
- const nanoid = customAlphabet(alphabet, length);
7
-
8
- return nanoid();
9
- }
package/src/utils/rpc.ts DELETED
@@ -1,28 +0,0 @@
1
- declare global {
2
- var __ocelRegister: Promise<unknown>[];
3
- }
4
-
5
- export const rpc = {
6
- register: async (args: {
7
- id: string;
8
- type: "lambda" | "bucket" | "workflow" | "postgres";
9
- source: string;
10
- config?: Record<string, unknown>;
11
- }) => {
12
- const SERVER_URL = process.env.OCEL_SERVER;
13
- if (!SERVER_URL) {
14
- throw new Error("OCEL_SERVER environment variable is not set");
15
- }
16
-
17
- const p = fetch(`${SERVER_URL}/register`, {
18
- method: "POST",
19
- headers: {
20
- "Content-Type": "application/json",
21
- },
22
- body: JSON.stringify(args),
23
- });
24
-
25
- globalThis.__ocelRegister ??= [];
26
- globalThis.__ocelRegister.push(p);
27
- },
28
- };
@@ -1,26 +0,0 @@
1
- /**
2
- * Returns absolute path of the caller file.
3
- */
4
- export function getCallerFile(depth = 1): string | null {
5
- const original = Error.prepareStackTrace;
6
-
7
- try {
8
- Error.prepareStackTrace = (_, stack) => stack;
9
- const err = new Error();
10
-
11
- const frames = err.stack as unknown as NodeJS.CallSite[];
12
-
13
- // index 0 = getCallerFile
14
- // index 1 = the function that called getCallerFile
15
- return (
16
- frames[2 + depth]
17
- ?.getFileName()
18
- ?.replace("file://", "")
19
- ?.split("?")?.[0] ?? null
20
- );
21
- } catch {
22
- return null;
23
- } finally {
24
- Error.prepareStackTrace = original;
25
- }
26
- }
@@ -1,9 +0,0 @@
1
- import type { ZodType } from "zod";
2
- import { Workflow, type WorkflowConfig } from "./workflow";
3
-
4
- export function workflow<TInput extends ZodType = never>(
5
- id: string,
6
- config: WorkflowConfig<TInput>
7
- ) {
8
- return new Workflow<TInput>(id, config);
9
- }
@@ -1,235 +0,0 @@
1
- import type { z, ZodType } from "zod";
2
- import type { Component } from "../utils/component";
3
- import {
4
- withDurableExecution,
5
- type DurableContext,
6
- } from "@aws/durable-execution-sdk-js";
7
- import { getCallerFile } from "../utils/stack";
8
- import { rpc } from "../utils/rpc";
9
- import { SpanStatusCode, trace } from "@opentelemetry/api";
10
-
11
- export interface WorkflowConfig<TInput extends ZodType = never> {
12
- input?: TInput;
13
- queue?: {
14
- concurrencyLimit?: number;
15
- };
16
- cron?: string;
17
- run: [TInput] extends [never]
18
- ? (args: { ctx: DurableContext }) => Promise<void>
19
- : (args: { input: z.infer<TInput>; ctx: DurableContext }) => Promise<void>;
20
- link?: Component[];
21
- retry?:
22
- | {
23
- maxAttempts: number;
24
- backoffRate?: number;
25
- intervalSeconds?: number;
26
- maxTimeoutSeconds?: number;
27
- }
28
- | false;
29
- }
30
-
31
- export class Workflow<TInput extends ZodType = never> implements Component {
32
- constructor(private id: string, config: WorkflowConfig<TInput>) {
33
- const { run, input, link, ...rest } = config;
34
- const links = link?.map((component) => component.__id()) || [];
35
- const tracer = trace.getTracer("ocel/workflow");
36
-
37
- const handler = (originalEvent: any, originalFnContext: any) => {
38
- return withDurableExecution(async (event, durableCtx) => {
39
- const ctx = durableCtx as any;
40
- const isReplay = ctx.durableExecutionMode === "ReplayMode";
41
- const ops = originalEvent.InitialExecutionState?.Operations || [];
42
-
43
- const stepCompleted = (name: string) => {
44
- return (
45
- isReplay &&
46
- ops.some((op: any) => op.Name === name && op.Status === "SUCCEEDED")
47
- );
48
- };
49
-
50
- return tracer.startActiveSpan(
51
- isReplay ? `workflow-${id}-replay` : `workflow-${id}`,
52
- {
53
- attributes: { workflow_id: id },
54
- },
55
- async (rootSpan) => {
56
- // override context to add tracing
57
- const instrumentedCtx = new Proxy(durableCtx, {
58
- get(target, prop, receiver) {
59
- if (prop === "step") {
60
- return async (
61
- name: string,
62
- fn: () => Promise<any>,
63
- opts: any
64
- ) => {
65
- if (stepCompleted(name)) {
66
- return target.step(name, fn, opts);
67
- }
68
-
69
- return tracer.startActiveSpan(
70
- `step: ${name}`,
71
- async (stepSpan) => {
72
- try {
73
- console.info(
74
- `stepSpan isRecording=${stepSpan.isRecording()} spanContext=${JSON.stringify(
75
- stepSpan.spanContext()
76
- )}`
77
- );
78
-
79
- const result = await target.step(name, fn, opts);
80
- stepSpan.setStatus({ code: SpanStatusCode.OK });
81
- return result;
82
- } catch (err) {
83
- // Only record legitimate errors, not Durable Suspend/Sleep interruptions
84
- // (You can filter specifically if you know the Suspend error type)
85
- stepSpan.recordException(err as Error);
86
- stepSpan.setStatus({ code: SpanStatusCode.ERROR });
87
- throw err;
88
- } finally {
89
- stepSpan.end();
90
- }
91
- }
92
- );
93
- };
94
- }
95
-
96
- if (prop === "wait") {
97
- return async (name: string, opts: any) => {
98
- if (stepCompleted(name)) {
99
- return target.wait(name, opts);
100
- }
101
-
102
- return tracer.startActiveSpan(
103
- `wait: ${name}`,
104
- async (waitSpan) => {
105
- console.info(
106
- `waitSpan isRecording=${waitSpan.isRecording()} spanContext=${JSON.stringify(
107
- waitSpan.spanContext()
108
- )}`
109
- );
110
-
111
- waitSpan.setAttribute("duration_seconds", opts.seconds);
112
- waitSpan.setAttribute("workflow.status", "suspending");
113
-
114
- console.log(
115
- "[OTEL-FIX] Closing spans before suspend..."
116
- );
117
-
118
- waitSpan.setStatus({ code: SpanStatusCode.OK });
119
- waitSpan.end();
120
-
121
- rootSpan.setAttribute("workflow.suspend_trigger", name);
122
- rootSpan.setStatus({ code: SpanStatusCode.OK });
123
- rootSpan.end();
124
-
125
- const provider = trace.getTracerProvider();
126
- // @ts-ignore
127
- if (typeof provider.forceFlush === "function") {
128
- // @ts-ignore
129
- await provider.forceFlush();
130
- console.log("[OTEL-FIX] Flush complete.");
131
- }
132
-
133
- await target.wait(name, opts);
134
- }
135
- );
136
- };
137
- }
138
-
139
- // Default: Return the original property (e.g., ctx.runId, ctx.now)
140
- return Reflect.get(target, prop, receiver);
141
- },
142
- });
143
-
144
- console.log("eventss", {
145
- originalEvent,
146
- originalFnContext,
147
- });
148
-
149
- const executionArn = originalEvent.DurableExecutionArn;
150
- const parts = executionArn.split("/");
151
- const executionId = `${parts[parts.length - 2]}/${
152
- parts[parts.length - 1]
153
- }`;
154
-
155
- console.log("execution details", {
156
- event,
157
- originalCtx: durableCtx,
158
- executionArn,
159
- executionId,
160
- });
161
-
162
- rootSpan.setAttribute("execution_id", executionId);
163
-
164
- try {
165
- console.info(
166
- `startRootSpan isRecording=${rootSpan.isRecording()} spanContext=${JSON.stringify(
167
- rootSpan.spanContext()
168
- )}`
169
- );
170
-
171
- if (input) {
172
- const parsed = input.parse(event);
173
- await run({ input: parsed, ctx: instrumentedCtx });
174
- } else {
175
- await run({ ctx: instrumentedCtx, input: event });
176
- }
177
-
178
- rootSpan.setStatus({ code: 1 });
179
- } catch (error) {
180
- console.log("Error in workflow:", error);
181
- throw error;
182
- } finally {
183
- console.log("Ending span for workflow:", id);
184
- console.log("Span details:", rootSpan);
185
-
186
- if (rootSpan.isRecording()) {
187
- console.log("Ending root span (normal exit)");
188
- rootSpan.end();
189
- }
190
- }
191
- }
192
- );
193
- })(originalEvent, originalFnContext);
194
- };
195
-
196
- // @ts-expect-error - accessed by cli during build
197
- this.__handler = handler;
198
- // @ts-expect-error - accessed by cli during build
199
- this[Symbol.for("ocel:lambda:id")] = id;
200
-
201
- if (process.env.OCEL_PHASE === "discovery") {
202
- const source = getCallerFile();
203
-
204
- if (!source) {
205
- throw new Error(
206
- "Could not determine caller file for workflow registration"
207
- );
208
- }
209
-
210
- rpc.register({
211
- id: this.id,
212
- type: "workflow",
213
- source,
214
- config: { ...rest, links },
215
- });
216
- }
217
- }
218
-
219
- __id() {
220
- return this.id;
221
- }
222
-
223
- trigger(
224
- payload: [TInput] extends [never] ? {} : z.infer<TInput>,
225
- opts?: {
226
- idempotencyKey?: string;
227
- }
228
- ) {}
229
-
230
- batchTrigger() {}
231
-
232
- triggerAndWait() {}
233
-
234
- batchTriggerAndWait() {}
235
- }
File without changes