@asaidimu/utils-pipeline 1.0.1 → 1.0.3
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/README.md +117 -276
- package/index.d.mts +163 -38
- package/index.d.ts +163 -38
- package/index.js +1 -1
- package/index.mjs +1 -1
- package/package.json +1 -1
package/index.d.mts
CHANGED
|
@@ -524,6 +524,13 @@ interface DataStore<T extends object> {
|
|
|
524
524
|
* @returns The current state T.
|
|
525
525
|
*/
|
|
526
526
|
get(clone?: boolean): T;
|
|
527
|
+
/**
|
|
528
|
+
* Gets a subset of the store.
|
|
529
|
+
* @param paths The paths which to include in the returned object.
|
|
530
|
+
* @param separator Optional separator for paths. Defaults to `.` .
|
|
531
|
+
* @returns An object of the shape K mapping paths to their current values.
|
|
532
|
+
*/
|
|
533
|
+
subset<K extends Record<string, any> = Record<string, any>>(paths: Array<string>, separator?: string): K;
|
|
527
534
|
/**
|
|
528
535
|
* Registers a named action function that can modify the state.
|
|
529
536
|
* @param action The action configuration object.
|
|
@@ -993,6 +1000,24 @@ interface ArtifactTemplate<TState extends object, TArtifact, TRegistry extends R
|
|
|
993
1000
|
paramKey?: (params: Record<string, unknown>) => string;
|
|
994
1001
|
virtual?: true;
|
|
995
1002
|
}
|
|
1003
|
+
interface ExportedArtifact {
|
|
1004
|
+
key: string;
|
|
1005
|
+
instance: any;
|
|
1006
|
+
state: {
|
|
1007
|
+
groups: Array<{
|
|
1008
|
+
paths: string[];
|
|
1009
|
+
options?: SubscribeOptions;
|
|
1010
|
+
}>;
|
|
1011
|
+
hash: string;
|
|
1012
|
+
};
|
|
1013
|
+
dependencies: string[];
|
|
1014
|
+
}
|
|
1015
|
+
interface ExportedContainerState {
|
|
1016
|
+
version: string;
|
|
1017
|
+
timestamp: number;
|
|
1018
|
+
artifacts: ExportedArtifact[];
|
|
1019
|
+
checksum: string;
|
|
1020
|
+
}
|
|
996
1021
|
|
|
997
1022
|
/**
|
|
998
1023
|
* A dependency injection container for managing the lifecycle, dependencies,
|
|
@@ -1019,7 +1044,7 @@ declare class ArtifactContainer<TRegistry extends Record<string, any> = Record<s
|
|
|
1019
1044
|
* Creates a new ArtifactContainer instance.
|
|
1020
1045
|
* @param store An object providing functions to interact with a global data store
|
|
1021
1046
|
*/
|
|
1022
|
-
constructor(store: Pick<DataStore<TState>, "watch" | "get" | "set">);
|
|
1047
|
+
constructor(store: Pick<DataStore<TState>, "watch" | "get" | "set" | "subset">);
|
|
1023
1048
|
/**
|
|
1024
1049
|
* Provides debug information about all artifacts currently registered in this container.
|
|
1025
1050
|
*
|
|
@@ -1136,6 +1161,13 @@ declare class ArtifactContainer<TRegistry extends Record<string, any> = Record<s
|
|
|
1136
1161
|
* Callers should await this method to guarantee full resource release.
|
|
1137
1162
|
*/
|
|
1138
1163
|
dispose(): Promise<void>;
|
|
1164
|
+
export(): Promise<ExportedContainerState>;
|
|
1165
|
+
private restore;
|
|
1166
|
+
static from<TRegistry extends Record<string, any> = Record<string, any>, TState extends object = any>(options: {
|
|
1167
|
+
store: Pick<DataStore<TState>, "watch" | "get" | "set" | "subset">;
|
|
1168
|
+
bundle?: ExportedContainerState;
|
|
1169
|
+
templates: ArtifactTemplate<TState, any, TRegistry>[];
|
|
1170
|
+
}): Promise<ArtifactContainer<TRegistry, TState>>;
|
|
1139
1171
|
}
|
|
1140
1172
|
|
|
1141
1173
|
/**
|
|
@@ -1150,17 +1182,27 @@ interface State {
|
|
|
1150
1182
|
[key: string]: any;
|
|
1151
1183
|
}
|
|
1152
1184
|
/**
|
|
1153
|
-
* Instructs the
|
|
1154
|
-
*
|
|
1185
|
+
* Instructs the factory to pause the pipeline.
|
|
1186
|
+
*
|
|
1187
|
+
* - `pause` — the `stageId` at which the pipeline should resume.
|
|
1188
|
+
* - `timeout` — how long (ms) the registry should hold the live RunContext
|
|
1189
|
+
* before running the deferred container export and clearing the
|
|
1190
|
+
* strong reference. Omit to use the registry's configured default.
|
|
1191
|
+
* A value of 0 means export immediately on pause (no deferral).
|
|
1192
|
+
* - `persist` — kept for backward compatibility; when true the registry will
|
|
1193
|
+
* always export the container bundle at expiry regardless of
|
|
1194
|
+
* whether resume was called. Ignored if timeout is 0.
|
|
1155
1195
|
*/
|
|
1156
1196
|
interface PauseInstruction {
|
|
1157
1197
|
readonly pause: string;
|
|
1198
|
+
readonly timeout?: number;
|
|
1199
|
+
readonly persist?: boolean;
|
|
1158
1200
|
}
|
|
1159
1201
|
/**
|
|
1160
1202
|
* All possible outcomes of a router:
|
|
1161
|
-
* - `string`
|
|
1162
|
-
* - `null`
|
|
1163
|
-
* - `undefined`
|
|
1203
|
+
* - `string` → jump to a named stage
|
|
1204
|
+
* - `null` → terminate the pipeline
|
|
1205
|
+
* - `undefined` → advance to the next stage in order (or natural end)
|
|
1164
1206
|
* - `PauseInstruction` → suspend and write a checkpoint
|
|
1165
1207
|
*/
|
|
1166
1208
|
type RoutingInstruction = string | null | undefined | PauseInstruction;
|
|
@@ -1183,6 +1225,10 @@ type PipelineStageRouter<S extends object> = (state: S, results: Record<string,
|
|
|
1183
1225
|
*
|
|
1184
1226
|
* The checkpoint is always written atomically with the state patches of the
|
|
1185
1227
|
* stage that triggered the pause.
|
|
1228
|
+
*
|
|
1229
|
+
* `containerBundle` is intentionally absent at write time — the factory never
|
|
1230
|
+
* exports. The registry writes it into the checkpoint (via writeCheckpoint)
|
|
1231
|
+
* when the pause timeout fires and the live RunContext is about to be released.
|
|
1186
1232
|
*/
|
|
1187
1233
|
interface PipelineCheckpoint {
|
|
1188
1234
|
readonly runId: string;
|
|
@@ -1191,8 +1237,10 @@ interface PipelineCheckpoint {
|
|
|
1191
1237
|
readonly pausedAtStageId: string;
|
|
1192
1238
|
readonly pausedAtStageLabel: string;
|
|
1193
1239
|
readonly pausedOn: string;
|
|
1240
|
+
/** Written by the registry at timeout expiry, never by the factory. */
|
|
1241
|
+
readonly containerBundle?: ExportedContainerState;
|
|
1194
1242
|
}
|
|
1195
|
-
/**
|
|
1243
|
+
/** Factory-internal namespace for all checkpoint data. */
|
|
1196
1244
|
declare const PIPELINE_DATA_KEY: "__pipeline_data__";
|
|
1197
1245
|
/**
|
|
1198
1246
|
* Run-scoped context injected into every step action as a second argument.
|
|
@@ -1353,10 +1401,14 @@ type EventPath = readonly PathNode[];
|
|
|
1353
1401
|
/**
|
|
1354
1402
|
* All events emitted during a run.
|
|
1355
1403
|
*
|
|
1356
|
-
* stage:paused (#
|
|
1404
|
+
* stage:paused (#8) is a dedicated event for stages that pause, distinct from
|
|
1357
1405
|
* stage:success. Consumers should listen to both if they want to react to all
|
|
1358
1406
|
* terminal stage outcomes. stage:success is only emitted when the stage
|
|
1359
1407
|
* advances (or terminates) — never when it pauses.
|
|
1408
|
+
*
|
|
1409
|
+
* pipeline:paused carries `pauseTimeout` forwarded from the PauseInstruction
|
|
1410
|
+
* so the PipelineRegistry can arm its cleanup timer with the correct duration
|
|
1411
|
+
* without re-reading or re-parsing the instruction.
|
|
1360
1412
|
*/
|
|
1361
1413
|
interface RunEventMap<S extends object> {
|
|
1362
1414
|
"pipeline:start": {
|
|
@@ -1379,6 +1431,12 @@ interface RunEventMap<S extends object> {
|
|
|
1379
1431
|
runId: string;
|
|
1380
1432
|
checkpoint: PipelineCheckpoint;
|
|
1381
1433
|
finalState: S;
|
|
1434
|
+
/**
|
|
1435
|
+
* The timeout value from the PauseInstruction, forwarded verbatim.
|
|
1436
|
+
* Undefined means the registry should use its configured default.
|
|
1437
|
+
* 0 means export immediately (no deferral window).
|
|
1438
|
+
*/
|
|
1439
|
+
pauseTimeout: number | undefined;
|
|
1382
1440
|
};
|
|
1383
1441
|
"pipeline:failure": {
|
|
1384
1442
|
path: EventPath;
|
|
@@ -1465,8 +1523,8 @@ interface ScopedEventBus<S extends object> {
|
|
|
1465
1523
|
on<K extends keyof RunEventMap<S>>(event: K, handler: RunEventHandler<S, K>): () => void;
|
|
1466
1524
|
}
|
|
1467
1525
|
/**
|
|
1468
|
-
* A unit of execution returned by {@link
|
|
1469
|
-
* {@link
|
|
1526
|
+
* A unit of execution returned by {@link PipelineFactory.prepare} or
|
|
1527
|
+
* {@link PipelineFactory.resume}.
|
|
1470
1528
|
*
|
|
1471
1529
|
* Subscribe to lifecycle events via {@link on}, abort via {@link abort}, and
|
|
1472
1530
|
* start execution with {@link run}.
|
|
@@ -1491,27 +1549,29 @@ interface RunContext<S extends object> {
|
|
|
1491
1549
|
*/
|
|
1492
1550
|
run(): Promise<Result<PipelineRunResult<S>>>;
|
|
1493
1551
|
}
|
|
1494
|
-
/** Minimal logger interface used by the
|
|
1552
|
+
/** Minimal logger interface used by the factory. */
|
|
1495
1553
|
interface EngineLogger {
|
|
1496
1554
|
info: (msg: string, ctx?: any) => void;
|
|
1497
1555
|
error: (msg: string, ctx?: any) => void;
|
|
1498
1556
|
}
|
|
1499
1557
|
/**
|
|
1500
|
-
* Configuration for {@link
|
|
1558
|
+
* Configuration for {@link PipelineFactory}.
|
|
1501
1559
|
*
|
|
1502
1560
|
* @typeParam S - The shape of the state managed by the pipeline.
|
|
1503
1561
|
*/
|
|
1504
|
-
interface
|
|
1562
|
+
interface PipelineFactoryOptions<S extends object = State> {
|
|
1505
1563
|
/** Logger (defaults to a no‑op). */
|
|
1506
1564
|
logger?: EngineLogger;
|
|
1507
1565
|
/**
|
|
1508
1566
|
* **Required.** A factory that, given a `runId`, returns a `DataStore<S>`.
|
|
1509
|
-
* The
|
|
1510
|
-
* isolated store instance.
|
|
1567
|
+
* The factory does **not** hold a global store; each run receives its own
|
|
1568
|
+
* isolated store instance from this function.
|
|
1511
1569
|
*
|
|
1512
|
-
* For a new run (`prepare`), the
|
|
1570
|
+
* For a new run (`prepare`), the function should return an empty store.
|
|
1513
1571
|
* For a resumed run (`resume`), it must return the **same** store that was
|
|
1514
1572
|
* used during the original run (so that state and checkpoints are preserved).
|
|
1573
|
+
*
|
|
1574
|
+
* The returned store is owned by the run, not by PipelineFactory.
|
|
1515
1575
|
*/
|
|
1516
1576
|
storeFactory: (runId: string) => Promise<DataStore<S>>;
|
|
1517
1577
|
/**
|
|
@@ -1520,37 +1580,65 @@ interface PipelineEngineOptions<S extends object = State> {
|
|
|
1520
1580
|
* If omitted, the store remains empty.
|
|
1521
1581
|
*/
|
|
1522
1582
|
initialStateFactory?: () => S;
|
|
1583
|
+
/**
|
|
1584
|
+
* Optional registry to register every RunContext with after construction.
|
|
1585
|
+
* When provided, prepare() and resume() call registry.register() before
|
|
1586
|
+
* returning, passing both the RunContext and the run's DataStore so the
|
|
1587
|
+
* registry can drive deferred export without needing a reference to the factory.
|
|
1588
|
+
*/
|
|
1589
|
+
registry?: PipelineRegistryBinding<S>;
|
|
1590
|
+
}
|
|
1591
|
+
/**
|
|
1592
|
+
* Narrow interface the factory uses to register runs with the registry.
|
|
1593
|
+
* Typed as an interface rather than the full PipelineRegistry class so that
|
|
1594
|
+
* the factory remains decoupled from the registry's full surface.
|
|
1595
|
+
*/
|
|
1596
|
+
interface PipelineRegistryBinding<S extends object> {
|
|
1597
|
+
register(context: RunContext<S>, store: DataStore<S>): void;
|
|
1598
|
+
/**
|
|
1599
|
+
* Called by resume() before constructing a new RunContext.
|
|
1600
|
+
* Returns the existing live RunContext if the registry still holds a strong
|
|
1601
|
+
* reference for this runId (i.e. the pause timeout has not yet fired).
|
|
1602
|
+
* Returns null if no live context exists and reconstruction is needed.
|
|
1603
|
+
*/
|
|
1604
|
+
getLiveContext(runId: string): RunContext<S> | null;
|
|
1523
1605
|
}
|
|
1524
1606
|
/**
|
|
1525
|
-
*
|
|
1607
|
+
* A pure factory for creating and resuming pipeline runs.
|
|
1608
|
+
*
|
|
1609
|
+
* PipelineFactory is stateless between runs. It holds only the pipeline
|
|
1610
|
+
* definition (topology), a storeFactory function, and an optional logger and
|
|
1611
|
+
* registry binding. It does not own any DataStore, timer, or live run state.
|
|
1526
1612
|
*
|
|
1527
1613
|
* @typeParam S - The shape of the state object.
|
|
1528
1614
|
*
|
|
1529
1615
|
* @example
|
|
1530
1616
|
* ```ts
|
|
1531
|
-
* const
|
|
1617
|
+
* const factory = new PipelineFactory(myDefinition, {
|
|
1532
1618
|
* storeFactory: (runId) => createMyStore(runId),
|
|
1533
1619
|
* initialStateFactory: () => ({ counter: 0 }),
|
|
1620
|
+
* registry: PipelineRegistry.default,
|
|
1534
1621
|
* });
|
|
1535
1622
|
*
|
|
1536
|
-
* const ctx = await
|
|
1623
|
+
* const ctx = await factory.prepare({ stage: "validate" });
|
|
1537
1624
|
* ctx.on("pipeline:success", (e) => console.log("done", e.finalState));
|
|
1538
1625
|
* const result = await ctx.run();
|
|
1539
1626
|
* ```
|
|
1540
1627
|
*/
|
|
1541
|
-
declare class
|
|
1628
|
+
declare class PipelineFactory<S extends object> {
|
|
1542
1629
|
private readonly definition;
|
|
1543
1630
|
private readonly logger;
|
|
1544
1631
|
private readonly storeFactory;
|
|
1545
1632
|
private readonly initialStateFactory;
|
|
1633
|
+
private readonly registry;
|
|
1546
1634
|
private readonly index;
|
|
1547
1635
|
/**
|
|
1548
|
-
* Constructs
|
|
1636
|
+
* Constructs a factory tied to a fixed pipeline definition.
|
|
1549
1637
|
*
|
|
1550
1638
|
* @param definition - The pipeline topology (stages, steps, routers).
|
|
1551
|
-
* @param options -
|
|
1639
|
+
* @param options - Factory configuration. `storeFactory` is mandatory.
|
|
1552
1640
|
*/
|
|
1553
|
-
constructor(definition: RoutingPipelineDefinition<S>, options:
|
|
1641
|
+
constructor(definition: RoutingPipelineDefinition<S>, options: PipelineFactoryOptions<S>);
|
|
1554
1642
|
/**
|
|
1555
1643
|
* Prepare an isolated {@link RunContext} for a **new** run.
|
|
1556
1644
|
*
|
|
@@ -1558,6 +1646,9 @@ declare class PipelineEngine<S extends object> {
|
|
|
1558
1646
|
* initial state from `initialStateFactory`. No execution occurs until
|
|
1559
1647
|
* `runContext.run()` is called.
|
|
1560
1648
|
*
|
|
1649
|
+
* If a registry is configured, the context and store are registered before
|
|
1650
|
+
* this method returns.
|
|
1651
|
+
*
|
|
1561
1652
|
* @param entry - Optional address describing where to start execution.
|
|
1562
1653
|
* Omit to start from the lowest‑order stage.
|
|
1563
1654
|
* @param runId - Optional run identifier (UUID v7). Generated if omitted.
|
|
@@ -1567,9 +1658,18 @@ declare class PipelineEngine<S extends object> {
|
|
|
1567
1658
|
/**
|
|
1568
1659
|
* Reconstruct a {@link RunContext} for a previously paused run.
|
|
1569
1660
|
*
|
|
1570
|
-
*
|
|
1571
|
-
*
|
|
1572
|
-
*
|
|
1661
|
+
* **Fast path (registry hit):** If a registry is configured and still holds
|
|
1662
|
+
* a live strong reference for `runId` (i.e. the pause timeout has not fired),
|
|
1663
|
+
* the existing RunContext is returned directly. No store read, no
|
|
1664
|
+
* ArtifactContainer.from() — zero serialization cost.
|
|
1665
|
+
*
|
|
1666
|
+
* **Slow path (store reconstruction):** If no live context exists (timeout
|
|
1667
|
+
* expired, process restarted, or no registry), the store is read, the
|
|
1668
|
+
* checkpoint is validated, and a fresh RunContext is built from the
|
|
1669
|
+
* containerBundle (if the registry wrote one at expiry) or from scratch.
|
|
1670
|
+
*
|
|
1671
|
+
* The context is registered with the registry before this method returns
|
|
1672
|
+
* (slow path only — the fast path context is already registered).
|
|
1573
1673
|
*
|
|
1574
1674
|
* @param runId - The identifier of the paused run.
|
|
1575
1675
|
* @returns A result containing the `RunContext`, or a failure if no
|
|
@@ -1581,7 +1681,7 @@ declare class PipelineEngine<S extends object> {
|
|
|
1581
1681
|
*
|
|
1582
1682
|
* This method is package-internal. External callers use prepare() or resume().
|
|
1583
1683
|
* Sub-pipelines access it through the {@link SubPipelineFactory} interface,
|
|
1584
|
-
* which is the only reference RunContextImpl holds to the
|
|
1684
|
+
* which is the only reference RunContextImpl holds to the factory.
|
|
1585
1685
|
*
|
|
1586
1686
|
* A single {@link ArtifactContainer} is created per run at root level and
|
|
1587
1687
|
* shared across all sub-pipelines. Steps are registered under namespaced keys
|
|
@@ -1592,16 +1692,41 @@ declare class PipelineEngine<S extends object> {
|
|
|
1592
1692
|
* threaded into {@link registerSteps} so that every step factory closure
|
|
1593
1693
|
* captures the correct signal at registration time.
|
|
1594
1694
|
*
|
|
1595
|
-
* @param runId
|
|
1596
|
-
* @param pipeline
|
|
1597
|
-
* @param entry
|
|
1598
|
-
* @param store
|
|
1599
|
-
* @param parentBus
|
|
1600
|
-
* @param parentPath
|
|
1601
|
-
* @param container
|
|
1602
|
-
* @param keyPrefix
|
|
1603
|
-
|
|
1604
|
-
|
|
1695
|
+
* @param runId - The run identifier (same for parent and sub‑pipelines).
|
|
1696
|
+
* @param pipeline - The pipeline (sub‑)definition.
|
|
1697
|
+
* @param entry - Entry address; `undefined` means start from the beginning.
|
|
1698
|
+
* @param store - The shared store instance for this run.
|
|
1699
|
+
* @param parentBus - Parent scoped bus for event bubbling (root runs pass undefined).
|
|
1700
|
+
* @param parentPath - Event path from the root.
|
|
1701
|
+
* @param container - Shared container (created once at root; passed down to sub-pipelines).
|
|
1702
|
+
* @param keyPrefix - Namespacing prefix for artifact keys within the shared container.
|
|
1703
|
+
* @param artifactBundle - Optional exported container state for store-based resume.
|
|
1704
|
+
*/
|
|
1705
|
+
buildRunContext(runId: string, pipeline: RoutingPipelineDefinition<S>, entry: EntryAddress | undefined, store: DataStore<S>, parentBus: ScopedEventBus<S> | undefined, parentPath: EventPath, container?: ArtifactContainer<Record<string, DeepPartial<S>>, S>, keyPrefix?: string, artifactBundle?: ExportedContainerState): Promise<RunContext<S>>;
|
|
1605
1706
|
}
|
|
1707
|
+
/**
|
|
1708
|
+
* Writes a checkpoint into the store using the typed EngineStoreEnvelope.
|
|
1709
|
+
* The write is synchronous from the store's perspective (the transaction
|
|
1710
|
+
* boundary lives in the caller). No `any` cast is required because
|
|
1711
|
+
* EngineStoreEnvelope fully types the factory-internal key namespace.
|
|
1712
|
+
*
|
|
1713
|
+
* Exported so the PipelineRegistry can update an existing checkpoint with a
|
|
1714
|
+
* containerBundle at timeout expiry without duplicating the write logic.
|
|
1715
|
+
*/
|
|
1716
|
+
declare function writeCheckpoint(store: DataStore<any>, checkpoint: PipelineCheckpoint): Promise<void>;
|
|
1717
|
+
/**
|
|
1718
|
+
* Reads a checkpoint from the store using the typed EngineStoreEnvelope.
|
|
1719
|
+
* Returns null if no checkpoint exists for the given pipelineId / runId pair.
|
|
1720
|
+
* Synchronous — the store's get() is a synchronous snapshot read.
|
|
1721
|
+
*/
|
|
1722
|
+
declare function readCheckpoint(store: DataStore<any>, pipelineId: string | undefined, runId: string): PipelineCheckpoint | null;
|
|
1723
|
+
/**
|
|
1724
|
+
* Removes a checkpoint from the store.
|
|
1725
|
+
* Called by the registry when a paused run expires and no resume arrived,
|
|
1726
|
+
* after the containerBundle (if any) has been written into the checkpoint.
|
|
1727
|
+
* Exported so the registry can clear stale checkpoints without reaching into
|
|
1728
|
+
* the factory's internals.
|
|
1729
|
+
*/
|
|
1730
|
+
declare function clearCheckpoint(store: DataStore<any>, pipelineId: string, runId: string): Promise<void>;
|
|
1606
1731
|
|
|
1607
|
-
export { type EngineLogger, type EntryAddress, type EventPath, PIPELINE_DATA_KEY, type PathNode, type PauseInstruction, Pipeline, type PipelineCheckpoint, type PipelineContext, type PipelineDefinition,
|
|
1732
|
+
export { type EngineLogger, type EntryAddress, type EventPath, PIPELINE_DATA_KEY, type PathNode, type PauseInstruction, Pipeline, type PipelineCheckpoint, type PipelineContext, type PipelineDefinition, type PipelineEventMap, PipelineFactory, type PipelineFactoryOptions, type PipelineRegistryBinding, type PipelineRunResult, type PipelineStageRouter, type PipelineStep, type PipelineStepDefinition, Result, type RoutingInstruction, type RoutingPipelineDefinition, type RunContext, type RunEventHandler, type RunEventMap, SequentialExecutionContext, type Stage, type StageCarry, type State, type Step, type StepStageRouter, type SubPipelineAddress, clearCheckpoint, isPauseInstruction, readCheckpoint, writeCheckpoint };
|