@brickkit/redis-world 0.11.0 → 0.13.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.
@@ -0,0 +1,13 @@
1
+ import { _ as RunRetention, a as QueueStats, c as WorldQueueOptions, d as RedisBackedWorldConfig, g as KeepRuns, h as resolveConfig, i as QueueBackend, l as createWorldQueue, m as configFromEnv, n as createRedisBackedWorld, o as QueuedMessage, p as ResolvedConfig, r as Deliver, s as WorldQueue, t as RedisBackedWorld, u as retryDelayMs } from "./world-BDW_evdb.js";
2
+ import { setupRedis } from "./cli.js";
3
+ /**
4
+ * Creates a namespaced debug logger.
5
+ * Logs to stderr to avoid corrupting CLI JSON output on stdout.
6
+ *
7
+ * Control via WORKFLOW_DEBUG environment variable:
8
+ * WORKFLOW_DEBUG=1 // Enable all
9
+ * WORKFLOW_DEBUG=redis-world // Named namespaces, comma-separated
10
+ */
11
+ export declare function createDebugLogger(namespace: string): (...args: unknown[]) => void;
12
+ export { type Deliver, type KeepRuns, type QueueBackend, type QueueStats, type QueuedMessage, type RedisBackedWorld, type RedisBackedWorldConfig, type ResolvedConfig, type RunRetention, type WorldQueue, type WorldQueueOptions, configFromEnv, createRedisBackedWorld, createWorldQueue, resolveConfig, retryDelayMs, setupRedis };
13
+ //# sourceMappingURL=internal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internal.d.ts","names":[],"sources":["../src/internal/debug.ts"],"mappings":";;;;;;;;;;wBAQgB,kBAAkB,wBAAiB"}
@@ -0,0 +1,4 @@
1
+ import { i as resolveConfig, r as configFromEnv } from "./keys-CqKp_Amk.js";
2
+ import { t as setupRedis } from "./cli-BEHfAdNK.js";
3
+ import { a as createDebugLogger, n as createWorldQueue, r as retryDelayMs, t as createRedisBackedWorld } from "./world-783VJfKL.js";
4
+ export { configFromEnv, createDebugLogger, createRedisBackedWorld, createWorldQueue, resolveConfig, retryDelayMs, setupRedis };
@@ -0,0 +1,104 @@
1
+ // src/config.ts
2
+ /**
3
+ * The config the `WORKFLOW_REDIS_*` variables give. `createWorld` lays the
4
+ * config it is passed over this, so an option left out still comes from env.
5
+ */
6
+ function configFromEnv() {
7
+ return {
8
+ redis: process.env.WORKFLOW_REDIS_URL || process.env.REDIS_URL || "redis://localhost:6379",
9
+ queueConcurrency: Number.parseInt(process.env.WORKFLOW_REDIS_WORKER_CONCURRENCY ?? "", 10) || void 0,
10
+ keyPrefix: process.env.WORKFLOW_REDIS_KEY_PREFIX || void 0
11
+ };
12
+ }
13
+ const DEFAULTS = {
14
+ queueConcurrency: 10,
15
+ keyPrefix: "workflow:",
16
+ backoffType: "exponential",
17
+ backoffDelayMs: 1e3,
18
+ maxBackoffMs: 3e4,
19
+ httpTimeoutMs: 3e5,
20
+ maxAttempts: 73,
21
+ enableAutoPipelining: true
22
+ };
23
+ /** Fills the shared defaults and checks the shared limits, for every queue. */
24
+ function resolveConfig(config) {
25
+ const resolved = { ...config };
26
+ for (const [key, value] of Object.entries(DEFAULTS)) if (resolved[key] === void 0 || resolved[key] === "") resolved[key] = value;
27
+ for (const [name, minimum] of [
28
+ ["queueConcurrency", 1],
29
+ ["maxAttempts", 49],
30
+ ["maxBackoffMs", 1],
31
+ ["backoffDelayMs", 0],
32
+ ["httpTimeoutMs", 1],
33
+ ["idempotencyTtlMs", 1]
34
+ ]) checkInteger(name, resolved[name], minimum);
35
+ return resolved;
36
+ }
37
+ /** Throws unless `value` is an integer from `minimum` to Node's timer limit; `undefined` passes. */
38
+ function checkInteger(name, value, minimum) {
39
+ if (value !== void 0 && (typeof value !== "number" || !Number.isSafeInteger(value) || value < minimum || value > 2147483647)) throw new RangeError(`${name} must be an integer between ${minimum} and 2147483647`);
40
+ }
41
+ // src/keys.ts
42
+ /**
43
+ * The world's Redis key layout, the one place storage, streams, the queue and
44
+ * the reaper build key names from. A key added here that belongs to a run must
45
+ * also go in `ownedByRun`, or the reaper leaks it.
46
+ */
47
+ function keys(prefix) {
48
+ const queue = `${prefix}queue:flows`;
49
+ const k = {
50
+ runKey: (id) => `${prefix}run:${id}`,
51
+ runAttributesKey: (id) => `${prefix}run:attrs:${id}`,
52
+ runMetaKey: (id) => `${prefix}run:meta:${id}`,
53
+ runsIndexKey: () => `${prefix}runs:index`,
54
+ runsByNameKey: (name) => `${prefix}runs:by_name:${name}`,
55
+ runsByStatusKey: (status) => `${prefix}runs:by_status:${status}`,
56
+ eventsKey: (runId) => `${prefix}events:data:${runId}`,
57
+ eventsByCorrelationKey: (runId, correlationId) => `${prefix}events:by_correlation:${runId}:${correlationId}`,
58
+ resumeClaimKey: (runId, resumeId) => `${prefix}events:resume:${runId}:${resumeId}`,
59
+ stepKey: (runId, stepId) => `${prefix}step:${runId}:${stepId}`,
60
+ stepsIndexKey: (runId) => `${prefix}steps:by_run:${runId}`,
61
+ stepStatusKey: (runId, stepId) => `${prefix}step:status:${runId}:${stepId}`,
62
+ hookKey: (hookId) => `${prefix}hook:${hookId}`,
63
+ hooksByTokenKey: (token) => `${prefix}hooks:by_token:${token}`,
64
+ hooksIndexKey: (runId) => `${prefix}hooks:by_run:${runId}`,
65
+ hooksTokenMapKey: (runId) => `${prefix}hooks:tokens:${runId}`,
66
+ streamKey: (runId, name) => `${prefix}stream:${runId}:${name}`,
67
+ streamsByRunKey: (runId) => `${prefix}streams:by_run:${runId}`,
68
+ streamChannel: `${prefix}stream_notify`,
69
+ reaperLockKey: `${prefix}reaper:lock`,
70
+ /** The list queue's keys. */
71
+ queue: {
72
+ ready: queue,
73
+ delayed: `${queue}:delayed`,
74
+ workers: `${queue}:workers`,
75
+ processing: (owner) => `${queue}:processing:${owner}`,
76
+ heartbeat: (owner) => `${queue}:heartbeat:${owner}`,
77
+ idempotent: (key) => `${queue}:idempotent:${key}`,
78
+ /** Every queue key, for SCAN/KEYS. */
79
+ pattern: `${prefix}queue:*`
80
+ },
81
+ /**
82
+ * Every key a run owns except its run, meta and index entries, which the
83
+ * reaper deletes last. Resume claims are not indexed: scan `resumeClaimKey(runId, "*")`.
84
+ */
85
+ ownedByRun(runId, ids) {
86
+ return [
87
+ k.eventsKey(runId),
88
+ k.runAttributesKey(runId),
89
+ k.stepsIndexKey(runId),
90
+ k.hooksIndexKey(runId),
91
+ k.hooksTokenMapKey(runId),
92
+ k.streamsByRunKey(runId),
93
+ ...[...ids.correlationIds].map((id) => k.eventsByCorrelationKey(runId, id)),
94
+ ...ids.stepIds.flatMap((id) => [k.stepKey(runId, id), k.stepStatusKey(runId, id)]),
95
+ ...ids.hookIds.map(k.hookKey),
96
+ ...ids.streamNames.map((name) => k.streamKey(runId, name))
97
+ ];
98
+ }
99
+ };
100
+ return k;
101
+ }
102
+ export { resolveConfig as i, checkInteger as n, configFromEnv as r, keys as t };
103
+
104
+ //# sourceMappingURL=keys-CqKp_Amk.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keys-CqKp_Amk.js","names":[],"sources":["../src/config.ts","../src/keys.ts"],"sourcesContent":["import type { RedisOptions } from \"ioredis\";\nimport type { RunRetention } from \"./reaper.js\";\n\n/** The options every Redis-backed world shares, whatever its queue. */\nexport interface RedisBackedWorldConfig {\n /**\n * Redis connection URL or connection options\n * Examples:\n * - 'redis://localhost:6379'\n * - 'rediss://username:password@host:6380/0'\n * - { host: 'localhost', port: 6379 }\n */\n redis: string | RedisOptions;\n\n /**\n * Number of concurrent workers processing jobs\n * Default: 10\n */\n queueConcurrency?: number;\n\n /**\n * Optional key prefix for all Redis keys\n * Useful for multi-tenancy or namespace isolation\n * Default: 'workflow:'\n */\n keyPrefix?: string;\n\n /**\n * Backoff strategy for retries: 'fixed' replays at the same interval,\n * 'exponential' doubles the delay on each attempt.\n * Default: 'exponential'\n */\n backoffType?: \"fixed\" | \"exponential\";\n\n /**\n * Base delay for backoff (ms). For exponential backoff this is multiplied\n * by 2^(attempt - 1), up to `maxBackoffMs`.\n * Default: 1000\n */\n backoffDelayMs?: number;\n\n /**\n * The longest a retry is delayed (ms). With the default number of attempts an\n * uncapped exponential backoff would wait hours between late attempts.\n * Default: 30000 (30s)\n */\n maxBackoffMs?: number;\n\n /**\n * Base URL the queue worker uses to dispatch jobs back to the user's HTTP\n * server, which must serve `world.createQueueHandler(...)` at\n * `/.well-known/workflow/v1/flow` (steps travel on the same route). A route\n * registered with `world.registerRoute`, or an in-process handler registered\n * with `world.registerHandler`, takes precedence.\n * Default: process.env.WORKFLOW_BASE_URL || `http://localhost:${process.env.PORT ?? 3000}`\n */\n baseUrl?: string;\n\n /**\n * Per-job HTTP request timeout (ms). Long-running workflow steps may need a\n * higher value.\n * Default: 300_000 (5 minutes)\n */\n httpTimeoutMs?: number;\n\n /** Maximum events a run may accumulate, reported as `EventResult.maxEvents`.\n * Default: `WORKFLOW_MAX_EVENTS` || 25_000 */\n maxEventsPerRun?: number;\n\n /**\n * Batch commands issued in the same event-loop tick into one write. A win\n * once several runs are in flight (~1.75x at 256 concurrent) and a marginal\n * loss for a single serial caller.\n * Default: true\n */\n enableAutoPipelining?: boolean;\n\n /**\n * How long finished runs are kept, per terminal status: `age` in seconds,\n * `count` of newest runs to keep. A reaper on each worker deletes the run, its event log, steps, hooks\n * and streams once it passes. A status without a policy is kept forever.\n * Default: undefined (nothing expires)\n *\n * @example\n * retention: {\n * completed: { age: 24 * 60 * 60, count: 1000 },\n * failed: { age: 7 * 24 * 60 * 60 },\n * }\n */\n retention?: RunRetention;\n\n /**\n * Delivery budget per message (see each world for what happens past it).\n * At least 49: the runtime records a run as failed (`MAX_DELIVERIES_EXCEEDED`)\n * on delivery 49, and a message retired earlier strands its run in `running`.\n * Default: 73\n */\n maxAttempts?: number;\n\n /**\n * Optional TTL (ms) for queue deduplication when `idempotencyKey`\n * is supplied to `queue()`. By default no TTL is applied: the dedup key\n * lives until the job is acknowledged, so duplicate enqueues are dropped\n * for the entire lifetime of the pending/delayed job (the semantics the\n * workflow runtime relies on when re-enqueuing pending steps on replay).\n * Only set this if you need the dedup guard to expire on wall-clock time;\n * it must be much larger than the longest expected queue delay/backlog,\n * otherwise duplicate step executions become possible.\n * Default: undefined (dedup key released on job completion)\n */\n idempotencyTtlMs?: number;\n}\n\nexport interface RedisWorldConfig extends RedisBackedWorldConfig {\n /** Worker liveness TTL in milliseconds. Default 90000; minimum 300.\n * Set above the longest event-loop pause. Renewed every TTL / 3. */\n heartbeatTtlMs?: number;\n\n /**\n * Delivery budget before retries continue at maxBackoffMs. Messages are never\n * silently retired. At least 49. Default: 73\n */\n maxAttempts?: number;\n}\n\n/**\n * The config the `WORKFLOW_REDIS_*` variables give. `createWorld` lays the\n * config it is passed over this, so an option left out still comes from env.\n */\nexport function configFromEnv() {\n return {\n redis:\n process.env.WORKFLOW_REDIS_URL ||\n process.env.REDIS_URL ||\n \"redis://localhost:6379\",\n queueConcurrency:\n Number.parseInt(\n process.env.WORKFLOW_REDIS_WORKER_CONCURRENCY ?? \"\",\n 10,\n ) || undefined,\n keyPrefix: process.env.WORKFLOW_REDIS_KEY_PREFIX || undefined,\n };\n}\n\nconst DEFAULTS = {\n queueConcurrency: 10,\n keyPrefix: \"workflow:\",\n backoffType: \"exponential\",\n backoffDelayMs: 1000,\n maxBackoffMs: 30_000,\n httpTimeoutMs: 300_000,\n maxAttempts: 73,\n enableAutoPipelining: true,\n} as const;\n\n/** A world config with every shared default filled in. */\nexport type ResolvedConfig<C extends RedisBackedWorldConfig> = C & {\n -readonly [K in keyof typeof DEFAULTS]-?: NonNullable<\n RedisBackedWorldConfig[K]\n >;\n};\n\n/** Fills the shared defaults and checks the shared limits, for every queue. */\nexport function resolveConfig<C extends RedisBackedWorldConfig>(\n config: C,\n): ResolvedConfig<C> {\n const resolved: Record<string, unknown> = { ...(config as object) };\n for (const [key, value] of Object.entries(DEFAULTS))\n if (resolved[key] === undefined || resolved[key] === \"\")\n resolved[key] = value;\n for (const [name, minimum] of [\n [\"queueConcurrency\", 1],\n [\"maxAttempts\", 49],\n [\"maxBackoffMs\", 1],\n [\"backoffDelayMs\", 0],\n [\"httpTimeoutMs\", 1],\n [\"idempotencyTtlMs\", 1],\n ] as const)\n checkInteger(name, resolved[name], minimum);\n return resolved as unknown as ResolvedConfig<C>;\n}\n\n/** Throws unless `value` is an integer from `minimum` to Node's timer limit; `undefined` passes. */\nexport function checkInteger(name: string, value: unknown, minimum: number) {\n if (\n value !== undefined &&\n (typeof value !== \"number\" ||\n !Number.isSafeInteger(value) ||\n value < minimum ||\n value > 2_147_483_647)\n )\n throw new RangeError(\n `${name} must be an integer between ${minimum} and 2147483647`,\n );\n}\n","/**\n * The world's Redis key layout, the one place storage, streams, the queue and\n * the reaper build key names from. A key added here that belongs to a run must\n * also go in `ownedByRun`, or the reaper leaks it.\n */\nexport function keys(prefix: string) {\n const queue = `${prefix}queue:flows`;\n const k = {\n runKey: (id: string) => `${prefix}run:${id}`,\n runAttributesKey: (id: string) => `${prefix}run:attrs:${id}`,\n // `<status>|<specVersion>`, read by scripts that must check status atomically.\n runMetaKey: (id: string) => `${prefix}run:meta:${id}`,\n runsIndexKey: () => `${prefix}runs:index`,\n runsByNameKey: (name: string) => `${prefix}runs:by_name:${name}`,\n runsByStatusKey: (status: string) => `${prefix}runs:by_status:${status}`,\n\n eventsKey: (runId: string) => `${prefix}events:data:${runId}`,\n // A correlation id is only unique within its run.\n eventsByCorrelationKey: (runId: string, correlationId: string) =>\n `${prefix}events:by_correlation:${runId}:${correlationId}`,\n resumeClaimKey: (runId: string, resumeId: string) =>\n `${prefix}events:resume:${runId}:${resumeId}`,\n\n stepKey: (runId: string, stepId: string) =>\n `${prefix}step:${runId}:${stepId}`,\n stepsIndexKey: (runId: string) => `${prefix}steps:by_run:${runId}`,\n // `<status>|<attempt>`, kept beside the step so a script can check it atomically.\n stepStatusKey: (runId: string, stepId: string) =>\n `${prefix}step:status:${runId}:${stepId}`,\n\n hookKey: (hookId: string) => `${prefix}hook:${hookId}`,\n hooksByTokenKey: (token: string) => `${prefix}hooks:by_token:${token}`,\n hooksIndexKey: (runId: string) => `${prefix}hooks:by_run:${runId}`,\n hooksTokenMapKey: (runId: string) => `${prefix}hooks:tokens:${runId}`,\n\n // Run IDs (`wrun_<ulid>`) never contain ':', so the first ':' after the run\n // ID always starts the stream name, even if the name contains colons itself.\n streamKey: (runId: string, name: string) =>\n `${prefix}stream:${runId}:${name}`,\n streamsByRunKey: (runId: string) => `${prefix}streams:by_run:${runId}`,\n streamChannel: `${prefix}stream_notify`,\n\n reaperLockKey: `${prefix}reaper:lock`,\n /** The list queue's keys. */\n queue: {\n ready: queue,\n delayed: `${queue}:delayed`,\n workers: `${queue}:workers`,\n processing: (owner: string) => `${queue}:processing:${owner}`,\n heartbeat: (owner: string) => `${queue}:heartbeat:${owner}`,\n idempotent: (key: string) => `${queue}:idempotent:${key}`,\n /** Every queue key, for SCAN/KEYS. */\n pattern: `${prefix}queue:*`,\n },\n\n /**\n * Every key a run owns except its run, meta and index entries, which the\n * reaper deletes last. Resume claims are not indexed: scan `resumeClaimKey(runId, \"*\")`.\n */\n ownedByRun(\n runId: string,\n ids: {\n correlationIds: Iterable<string>;\n stepIds: string[];\n hookIds: string[];\n streamNames: string[];\n },\n ): string[] {\n return [\n k.eventsKey(runId),\n k.runAttributesKey(runId),\n k.stepsIndexKey(runId),\n k.hooksIndexKey(runId),\n k.hooksTokenMapKey(runId),\n k.streamsByRunKey(runId),\n ...[...ids.correlationIds].map((id) =>\n k.eventsByCorrelationKey(runId, id),\n ),\n ...ids.stepIds.flatMap((id) => [\n k.stepKey(runId, id),\n k.stepStatusKey(runId, id),\n ]),\n ...ids.hookIds.map(k.hookKey),\n ...ids.streamNames.map((name) => k.streamKey(runId, name)),\n ];\n },\n };\n return k;\n}\n"],"mappings":";;;;;AAiIA,SAAgB,gBAAgB;CAC9B,OAAO;EACL,OACE,QAAQ,IAAI,sBACZ,QAAQ,IAAI,aACZ;EACF,kBACE,OAAO,SACL,QAAQ,IAAI,qCAAqC,IACjD,EACF,KAAK,KAAA;EACP,WAAW,QAAQ,IAAI,6BAA6B,KAAA;CACtD;AACF;AAEA,MAAM,WAAW;CACf,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