Pipeline contexts
@unikvs/core pipeline contexts: reading and writing runtime variables via IPipelineContext, and the per-operation context list.
Overview
Pipeline contexts are a gateway for reading and writing per-operation runtime variables (vars). Every context extends IPipelineContext, with key, signal, source, and similar info added per operation kind.
Plugin authors take keys and signals from the context passed as hook arguments, and read behavior settings from vars.
Reading and writing variables
Use the three methods get, set, and setReadonly. The set family returns this, so calls can chain.
import type { IPipelineContext } from "@unikvs/core";
declare const ctx: IPipelineContext<{ readonly count: number }>;
const count: number = ctx.get("count"); // typed read
const unknownValue: unknown = ctx.get("anything-else"); // undeclared keys are unknown
ctx.set("count", 1).setReadonly("count", 2); // returns this, so calls chain
Declaring type arguments like { count: number } types getting and setting that key. Undeclared keys are unknown, and assigning a wrong type is a type error.
// @ts-expect-error count is a number, so a string is rejected
ctx.set("count", "not a number");
Declaration merging
You can also add types via declaration merging on the global UniKvsPipelineContextTypes. Use it to define variable names shared by multiple plugins in one place.
declare global {
interface UniKvsPipelineContextTypes {
readonly "test:count": number;
}
}
declare const ctx: IPipelineContext;
const count: number = ctx.get("test:count");
ctx.set("test:count", 1); // assigning a string is a type error
Type-argument declarations and UniKvsPipelineContextTypes merging combine: a key in either resolves with types. Keys in neither are unknown.
Context hierarchy
Contexts inherit in layers. Lower layers pin down the operation further and carry more info.
Context list
Each operation kind carries fixed info.
| Type | action |
Main added properties |
|---|---|---|
IMapKeyContext |
— | key: string |
IActionContext |
10 operations | signal: AbortSignal |
ILifecycleContext |
"open" | "close" |
signal |
IOpenContext |
"open" |
signal |
ICloseContext |
"close" |
signal |
IKeyedActionContext |
7 keyed operations | key / signal |
ISetContext |
"set" |
key / signal |
IGetContext |
"get" |
key / signal / repair |
IStreamContext |
"stream" |
key / signal / repair |
IHasContext |
"has" |
key / signal |
IDeleteContext |
"delete" |
key / signal |
ITransferContext |
"copy" | "move" |
key / destinationKey / signal |
ICopyContext |
"copy" |
key / destinationKey / signal |
IMoveContext |
"move" |
key / destinationKey / signal |
IClearContext |
"clear" |
signal |
IEncodeContext |
"set" |
source / destination? (for data conversion) |
IDecodeContext |
"get" |
source / destination? (for data conversion) |
IEncodeStreamContext |
"set" |
source / destination? (for IReadableStream conversion) |
IDecodeStreamContext |
"stream" |
source / destination? (for IReadableStream conversion) |
Keys and lifecycle
IMapKeyContext is key-mapping only and carries just key (used by middleware mapKey). It carries no signal.
IActionContext carries signal: AbortSignal and 10 kinds of action ("open" | "close" | "set" | "get" | "stream" | "has" | "delete" | "clear" | "copy" | "move"). ILifecycleContext narrows to "open" | "close", and IOpenContext / ICloseContext each pin to one kind.
Keyed operations
IKeyedActionContext adds key: string and narrows action to the 7 kinds "set" | "get" | "stream" | "has" | "delete" | "copy" | "move". The following ISetContext, IGetContext, IStreamContext, IHasContext, and IDeleteContext each pin action to one kind. ITransferContext narrows action to "copy" | "move" and adds the destination key in destinationKey. ICopyContext and IMoveContext pin action to "copy" and "move" respectively. IClearContext carries no key, so it extends IActionContext directly with action "clear".
The middleware use hook receives these 8 kinds (set / get / stream / has / delete / copy / move / clear) together. Narrowing by action gives type-safe access to each operation’s key, repair, and destinationKey.
import type { IMiddleware } from "@unikvs/core";
export function describe(use: IMiddleware.UseContext): string {
switch (use.action) {
case "set":
case "get":
case "stream":
case "has":
case "delete":
case "copy":
case "move": {
return `${use.action}: ${use.key}`;
}
case "clear": {
return "clear";
}
}
}
Conversion
ITransformContext carries pre/post-conversion values. source is the input, and the result is assigned to the optional destination.
| Type | action |
source |
destination |
|---|---|---|---|
IEncodeContext |
"set" |
raw data | after encoding |
IDecodeContext |
"get" |
stored value | after decoding |
IEncodeStreamContext |
"set" |
readable stream (raw chunks) | converted readable stream |
IDecodeStreamContext |
"stream" |
readable stream (stored chunks) | converted readable stream |
Data conversion (ITransformDataContext family) source and destination are the values themselves. In stream conversion, on the other hand, (ITransformStreamContext family) source and destination are IReadableStream. Check type arguments for value vs. chunk. See Streams.
Repair
IGetContext and IStreamContext carry a read-time repair (repair) request. It holds whether a value found in a later storage should be written back to earlier ones.
import type { IGetContext } from "@unikvs/core";
declare const ctx: IGetContext;
if (ctx.repair.enabled) {
console.log(ctx.repair.targets); // readonly [string, ...string[]] (one or more)
} else {
console.log(ctx.repair.targets); // readonly [] (empty)
}
When enabled is false, targets is an empty array; when true, it is an array of one or more display names. Narrowing by enabled gives type-safe access to targets. When repair is denied, RepairNotAllowedError is used. Eligibility is controlled by @unikvs/allow-repair.
API reference
Per-symbol reference. Each member row is directly linkable.
IPipelineContext
Gateway for reading and writing runtime variables (vars). Every context extends it.
getget<const TName extends ParamName<TTypes>>(name: TName): ParamValue<TTypes, TName>
Gets a variable.
get<const TName extends ParamName<TTypes>>(name: TName): ParamValue<TTypes, TName>setset<const TName extends ParamName<TTypes>>(name: TName, value: ParamValue<TTypes, TName>): this
Sets a variable. Returns `this`.
set<const TName extends ParamName<TTypes>>(name: TName, value: ParamValue<TTypes, TName>): thissetReadonlysetReadonly<const TName extends ParamName<TTypes>>(name: TName, value: ParamValue<TTypes, TName>): this
Sets a variable as readonly. Returns `this`.
setReadonly<const TName extends ParamName<TTypes>>(name: TName, value: ParamValue<TTypes, TName>): thisIMapKeyContext
Key-mapping-only context carrying just key.
keystring
The key being mapped.
stringIActionContext
signal with 10 kinds of action, shared by all operations.
ILifecycleContext
Lifecycle context with action narrowed to "open" | "close".
action"open" | "close"
The operation kind.
"open" | "close"IOpenContext
Initialization context with action pinned to "open".
action"open"
The operation kind.
"open"ICloseContext
Teardown context with action pinned to "close".
action"close"
The operation kind.
"close"IKeyedActionContext
Context adding key and narrowing action to the 7 keyed operations.
ISetContext
Write context with action pinned to "set".
action"set"
The operation kind.
"set"IGetContext
Read context with action pinned to "get", carrying a repair (repair) request.
action"get"
The operation kind.
"get"IStreamContext
Stream-read context with action pinned to "stream", carrying a repair (repair) request.
action"stream"
The operation kind.
"stream"IHasContext
Presence-check context with action pinned to "has".
action"has"
The operation kind.
"has"IDeleteContext
Delete context with action pinned to "delete".
action"delete"
The operation kind.
"delete"ITransferContext
Shared context for copy and move. action is "copy" | "move"; key is the source key, and destinationKey is the destination key.
action"copy" | "move"
The operation kind.
"copy" | "move"keystring
The source key.
stringdestinationKeystring
The destination key.
stringICopyContext
Copy context with action pinned to "copy". key and destinationKey are inherited from ITransferContext.
action"copy"
The operation kind.
"copy"IMoveContext
Move context with action pinned to "move". key and destinationKey are inherited from ITransferContext.
action"move"
The operation kind.
"move"IClearContext
Clear-all context with action pinned to "clear", carrying no key.
action"clear"
The operation kind.
"clear"ITransformContext
Context carrying pre/post-conversion values. source is the input, and destination receives the conversion result.
action"set" | "get" | "stream"
The operation kind.
"set" | "get" | "stream"sourceTSource
The pre-conversion value.
TSourcedestination?TDestination | undefined
Where the conversion result is assigned.
TDestination | undefinedITransformDataContext
Data-conversion context converting the values themselves.
action"set" | "get"
The operation kind.
"set" | "get"IEncodeContext
Encode context with action pinned to "set".
action"set"
The operation kind.
"set"IDecodeContext
Decode context with action pinned to "get".
action"get"
The operation kind.
"get"ITransformStreamContext
Stream-conversion context. source and destination are IReadableStream.
action"set" | "stream"
The operation kind.
"set" | "stream"IEncodeStreamContext
Stream-encode context with action pinned to "set".
action"set"
The operation kind.
"set"IDecodeStreamContext
Stream-decode context with action pinned to "stream".
action"stream"
The operation kind.
"stream"