/** * Capabilities table for workflow runs based on their `@workflow/core` version. * * When resuming a hook or webhook, the payload must be encoded in a format * that the *target* workflow run's deployment can decode. This module provides * a way to look up what serialization formats a given `@workflow/core` version * supports, so that newer deployments can avoid encoding payloads in formats * that older deployments don't understand (e.g., the `encr` encryption format). * * ## Adding a new format * * When a new serialization format is introduced: * 1. Add the format constant to `SerializationFormat` in `serialization.ts` * 2. Add an entry to `FORMAT_VERSION_TABLE` below with the minimum * `@workflow/core` version that supports it * 3. The `getRunCapabilities()` function will automatically include it * * ## Adding a new non-format capability * * Some capabilities aren't serialization format prefixes: e.g. * byte-stream wire framing is an envelope around chunks rather than * a content format. For those, add a boolean field to `RunCapabilities` * and an entry in `CAPABILITY_VERSION_TABLE` below. * * ## History * * - `encr` (AES-256-GCM encryption): added in `4.2.0-beta.64` * Commit: 7618ac36 "Wire AES-GCM encryption into serialization layer (#1251)" * https://github.com/vercel/workflow/commit/7618ac36 * - `framedByteStreams` (wire-level chunk framing for byte streams): added in `5.0.0-beta.15` * - `gzip` (gzip payload compression): added in `5.0.0-beta.18` * - `zstd` (zstd payload compression, preferred codec): added in `5.0.0-beta.18` * alongside gzip. Unlike gzip, decoding zstd also depends on the runtime: * `node:zlib` only has it from Node.js 22.15 / 23.8. A run therefore only * counts as zstd-capable when its execution context records a `nodeVersion` * that has it (see {@link nodeVersionSupportsZstd}). Runs created before * that field existed get gzip, which every supported runtime decodes. * - `encp` (X25519 sealed-box encryption for cross-run writes): added in * `5.0.0-beta.37`. Note that producers do **not** gate `encp` on this table: * they gate on the presence of `encryptionPublicKey` on the target run, * which a run only carries if the deployment that created it could also * open `encp`. The entry exists so the capability set stays a complete, * auditable description of a run's decoding ability. * - Hook-resume consumer protocol ("consumer re-ensures `hook_received` from * `hookInput`"): deliberately NOT tracked here. Rather than predict a * release cutoff, the run's creating deployment stamps an explicit * `hookResumeInputVersion` execution-context marker (mirrored onto the * hook's resumeContext by the server); older producers gate their lazy path * on that marker. */ import semver from 'semver'; import type { CompressionMode } from './serialization/compression.js'; import { SerializationFormat, type SerializationFormatType, } from './serialization.js'; /** * Capabilities of a workflow run based on its `@workflow/core` version. */ export interface RunCapabilities { /** * The set of serialization format prefixes that the target run can decode. * Use `supportedFormats.has(SerializationFormat.ENCRYPTED)` to check * if encryption is supported, etc. */ supportedFormats: ReadonlySet; /** * Whether the target run can decode wire-framed byte streams. When true, * byte streams (`type: 'bytes'` ReadableStreams passed across boundaries) * are wrapped in a length-prefixed frame envelope on the wire so the * reader can identify chunk boundaries, which enables auto-reconnect * on transient stream errors. When false, byte streams are written as * raw bytes (the legacy format) for compatibility with older runs. */ framedByteStreams: boolean; /** Whether real-time abort packets may carry a length envelope. */ framedAbortStreams: boolean; } /** * Maps serialization format identifiers to the minimum `@workflow/core` * version that introduced support for them. Formats not listed here are * assumed to be supported by all specVersion 2 runs (e.g., `devl`). */ const FORMAT_VERSION_TABLE: ReadonlyArray<{ format: SerializationFormatType; minVersion: string; }> = [ { format: SerializationFormat.ENCRYPTED, minVersion: '4.2.0-beta.64' }, // TODO(release): verify this matches the actual version that ships payload // compression. If a "Version Packages (beta)" PR merges before this change, // bump to the next beta. A too-low cutoff makes new producers write // compressed payloads to consumers that cannot decompress them; too-high // merely delays the optimization (safe). gzip and zstd ship together, so // they share a min version. zstd additionally requires the run's runtime to // decode it; see `getRunCapabilities`. { format: SerializationFormat.GZIP, minVersion: '5.0.0-beta.18' }, { format: SerializationFormat.ZSTD, minVersion: '5.0.0-beta.18' }, // TODO(release): verify this matches the actual version that ships sealed-box // encryption. If a "Version Packages (beta)" PR merges before this change, // bump to the next beta. Unlike the entries above, a wrong cutoff here is // not a correctness hazard: producers gate `encp` on the target run carrying // an `encryptionPublicKey`, not on this table (see History above). { format: SerializationFormat.SEALED, minVersion: '5.0.0-beta.37' }, // Future entries: // { format: SerializationFormat.CBOR, minVersion: '5.x.y' }, // { format: SerializationFormat.ENCRYPTED_V2, minVersion: '5.x.y' }, ]; /** * Maps non-format capability flags (booleans on `RunCapabilities`) to the * minimum `@workflow/core` version that introduced support for them. */ const CAPABILITY_VERSION_TABLE: ReadonlyArray<{ capability: keyof Omit; minVersion: string; }> = [ // TODO(release): verify this matches the actual version that ships byte-stream // framing. If a "Version Packages (beta)" PR merges before this change, bump // to the next beta. A too-low cutoff makes new producers write framed bytes to // consumers that cannot unframe them (silent corruption); too-high merely // delays the optimization (safe). { capability: 'framedByteStreams', minVersion: '5.0.0-beta.15' }, // This change has a minor changeset. Keep this cutoff aligned with the // release that actually ships the compatible abort reader: a lower cutoff // would make new producers frame packets for old, unframed-only readers. { capability: 'framedAbortStreams', minVersion: '5.3.0' }, // NOTE: the hook-resume consumer protocol ("does the consumer re-ensure // `hook_received` from the queue message's `hookInput`?") is intentionally // NOT gated here. A version-compare against a predicted release cutoff is a // guess; instead the run's creating deployment stamps an explicit // `hookResumeInputVersion` marker into its execution context, which the // server mirrors onto the hook's resumeContext. Older producers gate their // lazy path on that marker. ]; /** * The set of formats supported by all specVersion 2 runs, regardless of * `@workflow/core` version. These are the baseline formats that were present * from the start of the specVersion 2 protocol. */ const BASELINE_FORMATS: ReadonlySet = new Set([ SerializationFormat.DEVALUE_V1, ]); /** * Node.js versions whose `node:zlib` can decode zstd (`zstdDecompressSync` * landed in 23.8.0 and was backported to 22.15.0). */ const ZSTD_NODE_RANGE = '^22.15.0 || >=23.8.0'; /** * Whether a run whose deployment runs this Node.js version can decode zstd * payloads. `undefined` (runs created before the version was recorded, or a * non-Node runtime) is treated as unable to. */ export function nodeVersionSupportsZstd( nodeVersion: string | undefined ): boolean { if (typeof nodeVersion !== 'string' || !semver.valid(nodeVersion)) { return false; } return semver.satisfies(nodeVersion, ZSTD_NODE_RANGE); } /** * The Node.js version of the current process, as recorded on the runs it * creates (`executionContext.nodeVersion`) and advertised in health-check * responses. `undefined` on runtimes that are not Node.js: Bun and Deno * expose `process.versions.node` for compatibility, but their `node:zlib` * support does not follow that version, so they must not claim it. */ export function getCurrentNodeVersion(): string | undefined { try { const versions = ( globalThis as { process?: { versions?: Record }; } ).process?.versions; if (!versions || versions.bun || versions.deno) return undefined; return typeof versions.node === 'string' ? versions.node : undefined; } catch { return undefined; } } /** * Look up what serialization capabilities a workflow run supports based on * its `@workflow/core` version string (from `executionContext.workflowCoreVersion`) * and the Node.js version of its deployment (from `executionContext.nodeVersion`). * * When the core version is `undefined`, not a string, or not a valid semver * string (e.g. older runs that predate the field, or corrupted metadata), * we assume the most conservative capabilities (baseline formats only, * non-format capabilities all `false`). When the Node.js version is unknown, * zstd is excluded. */ export function getRunCapabilities( workflowCoreVersion: string | undefined, nodeVersion?: string ): RunCapabilities { if (!workflowCoreVersion || !semver.valid(workflowCoreVersion)) { return { supportedFormats: BASELINE_FORMATS, framedByteStreams: false, framedAbortStreams: false, }; } const formats = new Set(BASELINE_FORMATS); for (const { format, minVersion } of FORMAT_VERSION_TABLE) { if (semver.gte(workflowCoreVersion, minVersion)) { formats.add(format); } } if (!nodeVersionSupportsZstd(nodeVersion)) { formats.delete(SerializationFormat.ZSTD); } const result: RunCapabilities = { supportedFormats: formats, framedByteStreams: false, framedAbortStreams: false, }; for (const { capability, minVersion } of CAPABILITY_VERSION_TABLE) { if (semver.gte(workflowCoreVersion, minVersion)) { result[capability] = true; } } return result; } /** * The compression a write may apply for a target run with these * capabilities: any codec when it decodes zstd, gzip only when it decodes * just gzip, none otherwise. Callers still gate on the run's specVersion. */ export function getCompressionMode( capabilities: RunCapabilities ): CompressionMode { if (!capabilities.supportedFormats.has(SerializationFormat.GZIP)) { return false; } return capabilities.supportedFormats.has(SerializationFormat.ZSTD) ? true : 'gzip'; }