createStoredContinuationCodec
API Reference for createStoredContinuationCodec.
createStoredContinuationCodec()
Create a single-use, storage-backed string continuation codec.
import { createStoredContinuationCodec } from 'run';
const continuationCodec = createStoredContinuationCodec({
storage: {
set: (key, value) => store.set(key, value),
acquire: (key, claimId, context) =>
store.claim(key, {
claimId,
claimMs: 30_000,
signal: context?.abortSignal,
}),
consume: (key, claimId) => store.deleteIfClaimedBy(key, claimId),
release: (key, claimId) => store.releaseIfClaimedBy(key, claimId),
},
});The storage methods describe a temporary claim on a continuation:
acquire()gives one caller exclusive access without deleting the value.consume()permanently deletes the value after validation succeeds.release()makes the value available again after cancellation or validation failure.
The codec calls these methods automatically. Application code does not need to choose when to consume or release a continuation.
acquire() must create a claim that expires automatically after a bounded
interval. Automatic expiry makes the continuation available again if the
process crashes before it can call consume() or release(). Both finalization
methods must only modify the record when the supplied claim identifier matches
the record's current claim.
Import
import { createStoredContinuationCodec } from 'run';API Signature
Parameters
[
{
"name": "storage",
"type": "ContinuationStorage",
"description": "The storage adapter used to save, temporarily claim, and atomically consume continuation state.",
"properties": [
{
"type": "ContinuationStorage",
"parameters": [
{
"name": "set",
"type": "(key: string, value: StoredContinuation, context?: ContinuationOperationContext) => void | Promise<void>",
"description": "Stores a continuation under the generated key."
},
{
"name": "acquire",
"type": "(key: string, claimId: string, context?: ContinuationOperationContext) => StoredContinuation | undefined | Promise<StoredContinuation | undefined>",
"description": "Atomically creates a temporary claim on a continuation without removing it."
},
{
"name": "consume",
"type": "(key: string, claimId: string) => void | Promise<void>",
"description": "Atomically removes a continuation when its claim identifier matches."
},
{
"name": "release",
"type": "(key: string, claimId: string) => void | Promise<void>",
"description": "Atomically releases a continuation when its claim identifier matches."
}
]
},
{
"type": "StoredContinuation",
"parameters": [
{
"name": "state",
"type": "RunContinuationState",
"description": "The serializable replay state."
},
{
"name": "expiresAtMs",
"type": "number",
"description": "The expiration time as a Unix timestamp in milliseconds."
}
]
},
{
"type": "ContinuationOperationContext",
"parameters": [
{
"name": "abortSignal",
"type": "AbortSignal",
"description": "The signal for the continuation operation."
},
{
"name": "deadlineMs",
"type": "number",
"description": "The operation deadline as a Unix timestamp in milliseconds."
}
]
}
]
},
{
"name": "maxAgeMs",
"type": "number",
"isOptional": true,
"description": "The lifetime assigned to stored continuations in milliseconds. Must be a positive integer. Default: 3600000."
}
]Returns
[
{
"name": "codec",
"type": "ContinuationCodec<string>",
"description": "A storage-backed string continuation codec.",
"properties": [
{
"type": "ContinuationCodec<string>",
"parameters": [
{
"name": "encode",
"type": "(state: RunContinuationState, context?: ContinuationOperationContext) => string | Promise<string>",
"description": "Stores replay state and returns a random base64url key."
},
{
"name": "decode",
"type": "(key: string, context?: ContinuationOperationContext) => RunContinuationState | Promise<RunContinuationState>",
"description": "Atomically consumes the stored continuation and returns its replay state."
}
]
},
{
"type": "RunContinuationState",
"parameters": [
{
"name": "version",
"type": "2",
"description": "The continuation state version."
},
{
"name": "runtime",
"type": "'run-replay-v2'",
"description": "The replay runtime identifier."
},
{
"name": "serde",
"type": "'run-js-v1'",
"description": "The serialization format identifier."
},
{
"name": "source",
"type": "string",
"description": "The source associated with the continuation."
},
{
"name": "logicalRunId",
"type": "string",
"description": "The logical run identifier."
},
{
"name": "scopeHash",
"type": "string",
"description": "The authenticated continuation scope hash."
},
{
"name": "determinism",
"type": "{ dateNowMs: number; randomSeed: string }",
"description": "The deterministic clock and random seed."
},
{
"name": "ledger",
"type": "RunLedgerEntry[]",
"description": "The recorded host function outcomes."
}
]
}
]
}
]