@brickkit/redis-world 0.16.0-beta.0 → 0.16.0-beta.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.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { _ as RunRetention, a as QueueStats, f as RedisWorldConfig, g as KeepRuns, t as RedisBackedWorld } from "./world-BDW_evdb.js";
1
+ import { _ as RunRetention, a as QueueStats, f as RedisWorldConfig, g as KeepRuns, t as RedisBackedWorld } from "./world-DCbuc04m.js";
2
2
  import "ioredis";
3
3
  /** `config` is laid over `configFromEnv()`: an option left out comes from the
4
4
  * `WORKFLOW_REDIS_*` variables, then the defaults. */
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { n as checkInteger, r as configFromEnv, t as keys } from "./keys-CqKp_Amk.js";
2
- import { i as debug, n as createWorldQueue, r as retryDelayMs, t as createRedisBackedWorld } from "./world-783VJfKL.js";
2
+ import { i as debug, n as createWorldQueue, r as retryDelayMs, t as createRedisBackedWorld } from "./world-Cac56z4M.js";
3
3
  import { randomUUID } from "node:crypto";
4
4
  import { setTimeout } from "node:timers/promises";
5
5
  import { MessageId, ValidQueueName } from "@workflow/world";
@@ -1,4 +1,4 @@
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";
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-DCbuc04m.js";
2
2
  import { setupRedis } from "./cli.js";
3
3
  /**
4
4
  * Creates a namespaced debug logger.
package/dist/internal.js CHANGED
@@ -1,4 +1,4 @@
1
1
  import { i as resolveConfig, r as configFromEnv } from "./keys-CqKp_Amk.js";
2
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";
3
+ import { a as createDebugLogger, n as createWorldQueue, r as retryDelayMs, t as createRedisBackedWorld } from "./world-Cac56z4M.js";
4
4
  export { configFromEnv, createDebugLogger, createRedisBackedWorld, createWorldQueue, resolveConfig, retryDelayMs, setupRedis };
@@ -1 +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,kBAAkB;CAClB,WAAW;CACX,aAAa;CACb,gBAAgB;CAChB,cAAc;CACd,eAAe;CACf,aAAa;CACb,sBAAsB;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C7B,GAAG,OAAO,cAAc,MAAM,GAAG;EAEnC,UAAU,WAAmB,GAAG,OAAO,OAAO;EAC9C,kBAAkB,UAAkB,GAAG,OAAO,iBAAiB;EAC/D,gBAAgB,UAAkB,GAAG,OAAO,eAAe;EAC3D,mBAAmB,UAAkB,GAAG,OAAO,eAAe;EAI9D,YAAY,OAAe,SACzB,GAAG,OAAO,SAAS,MAAM,GAAG;EAC9B,kBAAkB,UAAkB,GAAG,OAAO,iBAAiB;EAC/D,eAAe,GAAG,OAAO;EAEzB,eAAe,GAAG,OAAO;;EAEzB,OAAO;GACL,OAAO;GACP,SAAS,GAAG,MAAM;GAClB,SAAS,GAAG,MAAM;GAClB,aAAa,UAAkB,GAAG,MAAM,cAAc;GACtD,YAAY,UAAkB,GAAG,MAAM,aAAa;GACpD,aAAa,QAAgB,GAAG,MAAM,cAAc;;GAEpD,SAAS,GAAG,OAAO;EACrB;;;;;EAMA,WACE,OACA,KAMU;GACV,OAAO;IACL,EAAE,UAAU,KAAK;IACjB,EAAE,iBAAiB,KAAK;IACxB,EAAE,cAAc,KAAK;IACrB,EAAE,cAAc,KAAK;IACrB,EAAE,iBAAiB,KAAK;IACxB,EAAE,gBAAgB,KAAK;IACvB,GAAG,CAAC,GAAG,IAAI,cAAc,CAAC,CAAC,KAAK,OAC9B,EAAE,uBAAuB,OAAO,EAAE,CACpC;IACA,GAAG,IAAI,QAAQ,SAAS,OAAO,CAC7B,EAAE,QAAQ,OAAO,EAAE,GACnB,EAAE,cAAc,OAAO,EAAE,CAC3B,CAAC;IACD,GAAG,IAAI,QAAQ,IAAI,EAAE,OAAO;IAC5B,GAAG,IAAI,YAAY,KAAK,SAAS,EAAE,UAAU,OAAO,IAAI,CAAC;GAC3D;EACF;CACF;CACA,OAAO;AACT"}
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 /**\n * The key every delivery to the flow route carries, so the Workflow SDK\n * refuses a request that did not come from this world's queue. The same\n * value on every process of the app: the app's own and each worker's.\n * Generate one with\n * `node -e \"console.log(crypto.randomBytes(32).toString('base64url'))\"`.\n * Default: process.env.WORKFLOW_QUEUE_AUTH_KEY. Required.\n */\n queueAuthKey?: string;\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":";;;;;AA2IA,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,kBAAkB;CAClB,WAAW;CACX,aAAa;CACb,gBAAgB;CAChB,cAAc;CACd,eAAe;CACf,aAAa;CACb,sBAAsB;AACxB;;AAUA,SAAgB,cACd,QACmB;CACnB,MAAM,WAAoC,EAAE,GAAI,OAAkB;CAClE,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,QAAQ,GAChD,IAAI,SAAS,SAAS,KAAA,KAAa,SAAS,SAAS,IACnD,SAAS,OAAO;CACpB,KAAK,MAAM,CAAC,MAAM,YAAY;EAC5B,CAAC,oBAAoB,CAAC;EACtB,CAAC,eAAe,EAAE;EAClB,CAAC,gBAAgB,CAAC;EAClB,CAAC,kBAAkB,CAAC;EACpB,CAA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AAO;;EAEzB,OAAO;GACL,OAAO;GACP,SAAS,GAAG,MAAM;GAClB,SAAS,GAAG,MAAM;GAClB,aAAa,UAAkB,GAAG,MAAM,cAAc;GACtD,YAAY,UAAkB,GAAG,MAAM,aAAa;GACpD,aAAa,QAAgB,GAAG,MAAM,cAAc;;GAEpD,SAAS,GAAG,OAAO;EACrB;;;;;EAMA,WACE,OACA,KAMU;GACV,OAAO;IACL,EAAE,UAAU,KAAK;IACjB,EAAE,iBAAiB,KAAK;IACxB,EAAE,cAAc,KAAK;IACrB,EAAE,cAAc,KAAK;IACrB,EAAE,iBAAiB,KAAK;IACxB,EAAE,gBAAgB,KAAK;IACvB,GAAG,CAAC,GAAG,IAAI,cAAc,CAAC,CAAC,KAAK,OAC9B,EAAE,uBAAuB,OAAO,EAAE,CACpC;IACA,GAAG,IAAI,QAAQ,SAAS,OAAO,CAC7B,EAAE,QAAQ,OAAO,EAAE,GACnB,EAAE,cAAc,OAAO,EAAE,CAC3B,CAAC;IACD,GAAG,IAAI,QAAQ,IAAI,EAAE,OAAO;IAC5B,GAAG,IAAI,YAAY,KAAK,SAAS,EAAE,UAAU,OAAO,IAAI,CAAC;GAC3D;EACF;CACF;CACA,OAAO;AACT"}
@@ -1,7 +1,7 @@
1
1
  import { i as resolveConfig, t as keys } from "./keys-CqKp_Amk.js";
2
2
  import { createHash } from "node:crypto";
3
3
  import { EventSchema, FIRST_EVENT_SLOT, HookSchema, MessageId, StepSchema, WorkflowRunSchema, entityResolveData, eventIdToSlot, isLegacySpecVersion, mintedSpecVersion, parseQueueName, parseQueueRoute, reenqueueActiveRuns, requiresNewerWorld, slotToEventId, stripEventDataRefs, validateAttributeChanges } from "@workflow/world";
4
- import { createAppRouteUrl, createWorkflowBaseUrl, createWorkflowUrl, globalSingleton } from "@workflow/utils";
4
+ import { QUEUE_AUTH_HEADER, createAppRouteUrl, createWorkflowBaseUrl, createWorkflowUrl, globalSingleton, isQueueAuthRefusal, isWellFormedQueueAuthKey, setQueueAuthKeySource } from "@workflow/utils";
5
5
  import { encodeTime, monotonicFactory, ulid } from "ulid";
6
6
  import { EntityConflictError, HookNotFoundError, RunExpiredError, RunNotSupportedError, TooEarlyError, WorkflowRunNotFoundError, WorkflowWorldError } from "@workflow/errors";
7
7
  import { Redis } from "ioredis";
@@ -182,6 +182,13 @@ function initialMessageKey(message) {
182
182
  return typeof runId === "string" && runInput !== void 0 ? `start:${runId}` : void 0;
183
183
  }
184
184
  // src/world-queue.ts
185
+ const GENERATE_KEY = `node -e "console.log(crypto.randomBytes(32).toString('base64url'))"`;
186
+ /** @throws Unless the config or `WORKFLOW_QUEUE_AUTH_KEY` holds a well-formed key. */
187
+ function queueAuthKeyOf(config) {
188
+ const key = config.queueAuthKey ?? process.env.WORKFLOW_QUEUE_AUTH_KEY;
189
+ if (isWellFormedQueueAuthKey(key)) return key;
190
+ throw new Error(key ? `WORKFLOW_QUEUE_AUTH_KEY is not a queue auth key (43 characters, base64url). Generate one with: ${GENERATE_KEY}` : `Set WORKFLOW_QUEUE_AUTH_KEY to the same key on every process of the app, so the flow route accepts only this world's deliveries. Generate one with: ${GENERATE_KEY}`);
191
+ }
185
192
  /** A response without a valid `{ timeoutSeconds }` acknowledges the message. */
186
193
  function parseReschedule(text) {
187
194
  try {
@@ -196,6 +203,12 @@ function parseReschedule(text) {
196
203
  */
197
204
  function createWorldQueue(backend, config, options) {
198
205
  const { debug } = options;
206
+ const queueAuthKey = queueAuthKeyOf(config);
207
+ const keySourceId = `@brickkit/redis-world:${crypto.randomUUID()}`;
208
+ setQueueAuthKeySource(keySourceId, {
209
+ current: () => queueAuthKey,
210
+ reload: async () => queueAuthKey
211
+ });
199
212
  const generateMessageId = monotonicFactory();
200
213
  const { httpTimeoutMs } = config;
201
214
  const directHandlers = /* @__PURE__ */ new Map();
@@ -226,6 +239,7 @@ function createWorldQueue(backend, config, options) {
226
239
  const { prefix } = parseQueueName(job.queueName);
227
240
  const headers = new Headers(job.headers);
228
241
  headers.set("content-type", "application/json");
242
+ headers.set(QUEUE_AUTH_HEADER, queueAuthKey);
229
243
  headers.set("x-vqs-queue-name", job.queueName);
230
244
  headers.set("x-vqs-message-id", job.messageId);
231
245
  headers.set("x-vqs-message-attempt", String(job.attempt));
@@ -251,6 +265,7 @@ function createWorldQueue(backend, config, options) {
251
265
  });
252
266
  }
253
267
  const text = await response.text();
268
+ if (isQueueAuthRefusal(response)) throw new Error("The flow route refused this delivery's queue key: give every process of the app the same WORKFLOW_QUEUE_AUTH_KEY");
254
269
  if (!response.ok) throw new Error(`HTTP ${response.status}: ${text}`);
255
270
  return parseReschedule(text);
256
271
  };
@@ -283,6 +298,7 @@ function createWorldQueue(backend, config, options) {
283
298
  };
284
299
  };
285
300
  return {
301
+ queueAuthMode: "loopback",
286
302
  queue,
287
303
  createQueueHandler,
288
304
  getDeploymentId: async () => "redis",
@@ -297,6 +313,7 @@ function createWorldQueue(backend, config, options) {
297
313
  async close() {
298
314
  closed = true;
299
315
  await backend.close();
316
+ setQueueAuthKeySource(keySourceId, void 0);
300
317
  }
301
318
  };
302
319
  }
@@ -2449,4 +2466,4 @@ function createRedisBackedWorld(config, world) {
2449
2466
  }
2450
2467
  export { createDebugLogger as a, debug as i, createWorldQueue as n, retryDelayMs as r, createRedisBackedWorld as t };
2451
2468
 
2452
- //# sourceMappingURL=world-783VJfKL.js.map
2469
+ //# sourceMappingURL=world-Cac56z4M.js.map