@entifix/business 0.16.0 → 0.18.0

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.
@@ -5,7 +5,17 @@ import { Effect, Schema } from 'effect';
5
5
  * flow that started under one grammar finishes under that grammar even after
6
6
  * the next one ships.
7
7
  */
8
- export declare const SLIP_VERSION: 1;
8
+ export declare const SLIP_VERSION: 2;
9
+ /**
10
+ * Every grammar version a stored slip may be written in. A flow started under
11
+ * an earlier one still decodes and finishes under it.
12
+ *
13
+ * - `1` — the walk as first shipped.
14
+ * - `2` — the same walk, and a wait past the commit point announces
15
+ * `flow.waiting`: the caller's write has landed and what is left is someone
16
+ * else's, so a browser holding the write pending can let it go.
17
+ */
18
+ export declare const SLIP_VERSIONS: readonly [1, 2];
9
19
  /**
10
20
  * Tags that place a step in a segment of the flow: whose lines, which payment.
11
21
  * `entitlements` are the facts verified for the party the segment acts for —
@@ -116,7 +126,7 @@ export type SlipResolution = typeof SlipResolution.Type;
116
126
  * flow that has already started.
117
127
  */
118
128
  export declare const Slip: Schema.Struct<{
119
- readonly slipVersion: Schema.Literal<1>;
129
+ readonly slipVersion: Schema.Literals<readonly [1, 2]>;
120
130
  readonly flowId: Schema.String;
121
131
  readonly recipe: Schema.String;
122
132
  /** The recipe demands a commit point even with no retriable step (a checkout). */
@@ -1 +1 @@
1
- {"version":3,"file":"slip.d.ts","sourceRoot":"","sources":["../../src/flow/slip.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAExC;;;;GAIG;AACH,eAAO,MAAM,YAAY,EAAG,CAAU,CAAC;AAOvC;;;;;GAKG;AACH,eAAO,MAAM,WAAW;;;;EAItB,CAAC;AACH,MAAM,MAAM,WAAW,GAAG,OAAO,WAAW,CAAC,IAAI,CAAC;AAElD;;;;;GAKG;AACH,eAAO,MAAM,S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;;EAIzB,CAAC;AACH,MAAM,MAAM,cAAc,GAAG,OAAO,cAAc,CAAC,IAAI,CAAC;AAExD;;;;GAIG;AACH,eAAO,MAAM,IAAI;;;;IAIf,kFAAkF;;;;;YAhDlF,uDAAuD;;YAEvD,gFAAgF;;;;;;;;YAIhF,uEAAuE;;;;;;;;;;;YAKvE,sEAAsE;;;gBAnBtE,sDAAsD;;;;;;;;;;;;IA6DtD;;;OAGG;;;;;;EAEH,CAAC;AACH,MAAM,MAAM,IAAI,GAAG,OAAO,IAAI,CAAC,IAAI,CAAC;AAEpC;;;;GAIG;AACH,eAAO,MAAM,UAAU,GACrB,OAAO,OAAO,KACb,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,eAAe,CAQnC,CAAC;AAEJ,4DAA4D;AAC5D,eAAO,MAAM,UAAU,GAAI,MAAM,IAAI,KAAG,OACT,CAAC;AAEhC,kDAAkD;AAClD,eAAO,MAAM,SAAS,GAAI,MAAM,IAAI,KAAG,SAAS,QAAQ,EACb,CAAC"}
package/dist/flow/slip.js CHANGED
@@ -4,7 +4,19 @@ import { Effect, Schema } from 'effect';
4
4
  * The grammar version a slip is written in. An interpreter branches on it, so a
5
5
  * flow that started under one grammar finishes under that grammar even after
6
6
  * the next one ships.
7
- */ export const SLIP_VERSION = 1;
7
+ */ export const SLIP_VERSION = 2;
8
+ /**
9
+ * Every grammar version a stored slip may be written in. A flow started under
10
+ * an earlier one still decodes and finishes under it.
11
+ *
12
+ * - `1` — the walk as first shipped.
13
+ * - `2` — the same walk, and a wait past the commit point announces
14
+ * `flow.waiting`: the caller's write has landed and what is left is someone
15
+ * else's, so a browser holding the write pending can let it go.
16
+ */ export const SLIP_VERSIONS = [
17
+ 1,
18
+ SLIP_VERSION
19
+ ];
8
20
  const Call = Schema.Struct({
9
21
  method: Schema.Literals([
10
22
  'GET',
@@ -73,7 +85,7 @@ export const SlipStage = Schema.Struct({
73
85
  * stored as the flow's payload. Changing a recipe or a fragment never changes a
74
86
  * flow that has already started.
75
87
  */ export const Slip = Schema.Struct({
76
- slipVersion: Schema.Literal(SLIP_VERSION),
88
+ slipVersion: Schema.Literals(SLIP_VERSIONS),
77
89
  flowId: Schema.String,
78
90
  recipe: Schema.String,
79
91
  /** The recipe demands a commit point even with no retriable step (a checkout). */ requiresCommitPoint: Schema.optionalKey(Schema.Boolean),
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/flow/slip.ts"],"sourcesContent":["import { ProtocolFailure } from '@entifix/core';\nimport { Effect, Schema } from 'effect';\n\n/**\n * The grammar version a slip is written in. An interpreter branches on it, so a\n * flow that started under one grammar finishes under that grammar even after\n * the next one ships.\n */\nexport const SLIP_VERSION = 1 as const;\n\nconst Call = Schema.Struct({\n method: Schema.Literals(['GET', 'POST', 'PUT', 'PATCH', 'DELETE']),\n path: Schema.String,\n});\n\n/**\n * Tags that place a step in a segment of the flow: whose lines, which payment.\n * `entitlements` are the facts verified for the party the segment acts for —\n * a vendor's provisioning, never the caller's — and a segment's steps are\n * checked against them beside the flow's context.\n */\nexport const SlipSegment = Schema.Struct({\n vendor: Schema.optionalKey(Schema.String),\n paymentMethod: Schema.optionalKey(Schema.String),\n entitlements: Schema.optionalKey(Schema.Array(Schema.String)),\n});\nexport type SlipSegment = typeof SlipSegment.Type;\n\n/**\n * A step that runs only when an earlier wait was answered with one of\n * `outcomes` — the one branch a slip can take, decided by what the wait was\n * told rather than by anything known at composition. A step guarded on a wait\n * that was itself skipped is skipped too.\n */\nexport const SlipGuard = Schema.Struct({\n /** The step name of the wait, earlier in the slip. */\n wait: Schema.String,\n outcomes: Schema.Array(Schema.String),\n});\nexport type SlipGuard = typeof SlipGuard.Type;\n\n/** One pinned step: a fragment reference and everything resolved for it. */\nexport const SlipStep = Schema.Struct({\n /** `id@version`, pinned when the slip was composed. */\n ref: Schema.String,\n /** Unique within the slip; what an Activity is named after (`segment:step`). */\n step: Schema.String,\n cardinality: Schema.Literals(['one', 'perVendor', 'perLine']),\n segment: Schema.optionalKey(SlipSegment),\n /** The line a `perLine` step was expanded for, by the recipe's key. */\n line: Schema.optionalKey(Schema.String),\n participant: Schema.optionalKey(Schema.String),\n call: Schema.optionalKey(Call),\n compensation: Schema.optionalKey(Call),\n /** Configuration pinned at composition — a hold's expiry, a limit. */\n parameters: Schema.optionalKey(Schema.Record(Schema.String, Schema.Unknown)),\n guard: Schema.optionalKey(SlipGuard),\n});\nexport type SlipStep = typeof SlipStep.Type;\n\nexport const SlipStage = Schema.Struct({\n name: Schema.String,\n steps: Schema.Array(SlipStep),\n});\nexport type SlipStage = typeof SlipStage.Type;\n\n/** Who may receive what the flow announces — the envelope's audience. */\nexport const SlipAudienceEntry = Schema.Struct({\n kind: Schema.Literals(['organization', 'party', 'flow']),\n id: Schema.String,\n digest: Schema.optionalKey(Schema.String),\n});\nexport type SlipAudienceEntry = typeof SlipAudienceEntry.Type;\n\n/** Which variant each use case resolved to, and by which rule. */\nexport const SlipResolution = Schema.Struct({\n useCase: Schema.String,\n variant: Schema.String,\n rule: Schema.Number,\n});\nexport type SlipResolution = typeof SlipResolution.Type;\n\n/**\n * A routing slip: ordered stages of pinned steps, composed once at start and\n * stored as the flow's payload. Changing a recipe or a fragment never changes a\n * flow that has already started.\n */\nexport const Slip = Schema.Struct({\n slipVersion: Schema.Literal(SLIP_VERSION),\n flowId: Schema.String,\n recipe: Schema.String,\n /** The recipe demands a commit point even with no retriable step (a checkout). */\n requiresCommitPoint: Schema.optionalKey(Schema.Boolean),\n stages: Schema.Array(SlipStage),\n resolution: Schema.Array(SlipResolution),\n composedAt: Schema.String,\n /**\n * Who the flow's `flow.*` events reach beside the flow itself — the\n * organizations and parties it acts for, pinned when it was composed.\n */\n audience: Schema.optionalKey(Schema.Array(SlipAudienceEntry)),\n});\nexport type Slip = typeof Slip.Type;\n\n/**\n * Decodes a stored or received slip. An unknown `slipVersion`, like any shape\n * the grammar does not describe, is a protocol failure: an interpreter must\n * never guess at a grammar it was not built for.\n */\nexport const decodeSlip = (\n input: unknown,\n): Effect.Effect<Slip, ProtocolFailure> =>\n Schema.decodeUnknownEffect(Slip)(input).pipe(\n Effect.mapError(\n error =>\n new ProtocolFailure({\n reason: `the slip does not decode: ${error.message}`,\n }),\n ),\n );\n\n/** Encodes a slip as the workflow payload it travels as. */\nexport const encodeSlip = (slip: Slip): unknown =>\n Schema.encodeSync(Slip)(slip);\n\n/** Every step of a slip, in the order it runs. */\nexport const slipSteps = (slip: Slip): readonly SlipStep[] =>\n slip.stages.flatMap(stage => stage.steps);\n"],"names":["ProtocolFailure","Effect","Schema","SLIP_VERSION","Call","Struct","method","Literals","path","String","SlipSegment","vendor","optionalKey","paymentMethod","entitlements","Array","SlipGuard","wait","outcomes","SlipStep","ref","step","cardinality","segment","line","participant","call","compensation","parameters","Record","Unknown","guard","SlipStage","name","steps","SlipAudienceEntry","kind","id","digest","SlipResolution","useCase","variant","rule","Number","Slip","slipVersion","Literal","flowId","recipe","requiresCommitPoint","Boolean","stages","resolution","composedAt","audience","decodeSlip","input","decodeUnknownEffect","pipe","mapError","error","reason","message","encodeSlip","slip","encodeSync","slipSteps","flatMap","stage"],"mappings":"AAAA,SAASA,eAAe,QAAQ,gBAAgB;AAChD,SAASC,MAAM,EAAEC,MAAM,QAAQ,SAAS;AAExC;;;;CAIC,GACD,OAAO,MAAMC,eAAe,EAAW;AAEvC,MAAMC,OAAOF,OAAOG,MAAM,CAAC;IACzBC,QAAQJ,OAAOK,QAAQ,CAAC;QAAC;QAAO;QAAQ;QAAO;QAAS;KAAS;IACjEC,MAAMN,OAAOO,MAAM;AACrB;AAEA;;;;;CAKC,GACD,OAAO,MAAMC,cAAcR,OAAOG,MAAM,CAAC;IACvCM,QAAQT,OAAOU,WAAW,CAACV,OAAOO,MAAM;IACxCI,eAAeX,OAAOU,WAAW,CAACV,OAAOO,MAAM;IAC/CK,cAAcZ,OAAOU,WAAW,CAACV,OAAOa,KAAK,CAACb,OAAOO,MAAM;AAC7D,GAAG;AAGH;;;;;CAKC,GACD,OAAO,MA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;AAGH;;;;CAIC,GACD,OAAO,MAAMC,OAAO1C,OAAOG,MAAM,CAAC;IAChCwC,aAAa3C,OAAO4C,OAAO,CAAC3C;IAC5B4C,QAAQ7C,OAAOO,MAAM;IACrBuC,QAAQ9C,OAAOO,MAAM;IACrB,gFAAgF,GAChFwC,qBAAqB/C,OAAOU,WAAW,CAACV,OAAOgD,OAAO;IACtDC,QAAQjD,OAAOa,KAAK,CAACiB;IACrBoB,YAAYlD,OAAOa,KAAK,CAACwB;IACzBc,YAAYnD,OAAOO,MAAM;IACzB;;;GAGC,GACD6C,UAAUpD,OAAOU,WAAW,CAACV,OAAOa,KAAK,CAACoB;AAC5C,GAAG;AAGH;;;;CAIC,GACD,OAAO,MAAMoB,aAAa,CACxBC,QAEAtD,OAAOuD,mBAAmB,CAACb,MAAMY,OAAOE,IAAI,CAC1CzD,OAAO0D,QAAQ,CACbC,CAAAA,QACE,IAAI5D,gBAAgB;YAClB6D,QAAQ,CAAC,0BAA0B,EAAED,MAAME,OAAO,EAAE;QACtD,KAEJ;AAEJ,0DAA0D,GAC1D,OAAO,MAAMC,aAAa,CAACC,OACzB9D,OAAO+D,UAAU,CAACrB,MAAMoB,MAAM;AAEhC,gDAAgD,GAChD,OAAO,MAAME,YAAY,CAACF,OACxBA,KAAKb,MAAM,CAACgB,OAAO,CAACC,CAAAA,QAASA,MAAMlC,KAAK,EAAE"}
1
+ {"version":3,"sources":["../../src/flow/slip.ts"],"sourcesContent":["import { ProtocolFailure } from '@entifix/core';\nimport { Effect, Schema } from 'effect';\n\n/**\n * The grammar version a slip is written in. An interpreter branches on it, so a\n * flow that started under one grammar finishes under that grammar even after\n * the next one ships.\n */\nexport const SLIP_VERSION = 2 as const;\n\n/**\n * Every grammar version a stored slip may be written in. A flow started under\n * an earlier one still decodes and finishes under it.\n *\n * - `1` — the walk as first shipped.\n * - `2` — the same walk, and a wait past the commit point announces\n * `flow.waiting`: the caller's write has landed and what is left is someone\n * else's, so a browser holding the write pending can let it go.\n */\nexport const SLIP_VERSIONS = [1, SLIP_VERSION] as const;\n\nconst Call = Schema.Struct({\n method: Schema.Literals(['GET', 'POST', 'PUT', 'PATCH', 'DELETE']),\n path: Schema.String,\n});\n\n/**\n * Tags that place a step in a segment of the flow: whose lines, which payment.\n * `entitlements` are the facts verified for the party the segment acts for —\n * a vendor's provisioning, never the caller's — and a segment's steps are\n * checked against them beside the flow's context.\n */\nexport const SlipSegment = Schema.Struct({\n vendor: Schema.optionalKey(Schema.String),\n paymentMethod: Schema.optionalKey(Schema.String),\n entitlements: Schema.optionalKey(Schema.Array(Schema.String)),\n});\nexport type SlipSegment = typeof SlipSegment.Type;\n\n/**\n * A step that runs only when an earlier wait was answered with one of\n * `outcomes` — the one branch a slip can take, decided by what the wait was\n * told rather than by anything known at composition. A step guarded on a wait\n * that was itself skipped is skipped too.\n */\nexport const SlipGuard = Schema.Struct({\n /** The step name of the wait, earlier in the slip. */\n wait: Schema.String,\n outcomes: Schema.Array(Schema.String),\n});\nexport type SlipGuard = typeof SlipGuard.Type;\n\n/** One pinned step: a fragment reference and everything resolved for it. */\nexport const SlipStep = Schema.Struct({\n /** `id@version`, pinned when the slip was composed. */\n ref: Schema.String,\n /** Unique within the slip; what an Activity is named after (`segment:step`). */\n step: Schema.String,\n cardinality: Schema.Literals(['one', 'perVendor', 'perLine']),\n segment: Schema.optionalKey(SlipSegment),\n /** The line a `perLine` step was expanded for, by the recipe's key. */\n line: Schema.optionalKey(Schema.String),\n participant: Schema.optionalKey(Schema.String),\n call: Schema.optionalKey(Call),\n compensation: Schema.optionalKey(Call),\n /** Configuration pinned at composition — a hold's expiry, a limit. */\n parameters: Schema.optionalKey(Schema.Record(Schema.String, Schema.Unknown)),\n guard: Schema.optionalKey(SlipGuard),\n});\nexport type SlipStep = typeof SlipStep.Type;\n\nexport const SlipStage = Schema.Struct({\n name: Schema.String,\n steps: Schema.Array(SlipStep),\n});\nexport type SlipStage = typeof SlipStage.Type;\n\n/** Who may receive what the flow announces — the envelope's audience. */\nexport const SlipAudienceEntry = Schema.Struct({\n kind: Schema.Literals(['organization', 'party', 'flow']),\n id: Schema.String,\n digest: Schema.optionalKey(Schema.String),\n});\nexport type SlipAudienceEntry = typeof SlipAudienceEntry.Type;\n\n/** Which variant each use case resolved to, and by which rule. */\nexport const SlipResolution = Schema.Struct({\n useCase: Schema.String,\n variant: Schema.String,\n rule: Schema.Number,\n});\nexport type SlipResolution = typeof SlipResolution.Type;\n\n/**\n * A routing slip: ordered stages of pinned steps, composed once at start and\n * stored as the flow's payload. Changing a recipe or a fragment never changes a\n * flow that has already started.\n */\nexport const Slip = Schema.Struct({\n slipVersion: Schema.Literals(SLIP_VERSIONS),\n flowId: Schema.String,\n recipe: Schema.String,\n /** The recipe demands a commit point even with no retriable step (a checkout). */\n requiresCommitPoint: Schema.optionalKey(Schema.Boolean),\n stages: Schema.Array(SlipStage),\n resolution: Schema.Array(SlipResolution),\n composedAt: Schema.String,\n /**\n * Who the flow's `flow.*` events reach beside the flow itself — the\n * organizations and parties it acts for, pinned when it was composed.\n */\n audience: Schema.optionalKey(Schema.Array(SlipAudienceEntry)),\n});\nexport type Slip = typeof Slip.Type;\n\n/**\n * Decodes a stored or received slip. An unknown `slipVersion`, like any shape\n * the grammar does not describe, is a protocol failure: an interpreter must\n * never guess at a grammar it was not built for.\n */\nexport const decodeSlip = (\n input: unknown,\n): Effect.Effect<Slip, ProtocolFailure> =>\n Schema.decodeUnknownEffect(Slip)(input).pipe(\n Effect.mapError(\n error =>\n new ProtocolFailure({\n reason: `the slip does not decode: ${error.message}`,\n }),\n ),\n );\n\n/** Encodes a slip as the workflow payload it travels as. */\nexport const encodeSlip = (slip: Slip): unknown =>\n Schema.encodeSync(Slip)(slip);\n\n/** Every step of a slip, in the order it runs. */\nexport const slipSteps = (slip: Slip): readonly SlipStep[] =>\n slip.stages.flatMap(stage => stage.steps);\n"],"names":["ProtocolFailure","Effect","Schema","SLIP_VERSION","SLIP_VERSIONS","Call","Struct","method","Literals","path","String","SlipSegment","vendor","optionalKey","paymentMethod","entitlements","Array","SlipGuard","wait","outcomes","SlipStep","ref","step","cardinality","segment","line","participant","call","compensation","parameters","Record","Unknown","guard","SlipStage","name","steps","SlipAudienceEntry","kind","id","digest","SlipResolution","useCase","variant","rule","Number","Slip","slipVersion","flowId","recipe","requiresCommitPoint","Boolean","stages","resolution","composedAt","audience","decodeSlip","input","decodeUnknownEffect","pipe","mapError","error","reason","message","encodeSlip","slip","encodeSync","slipSteps","flatMap","stage"],"mappings":"AAAA,SAASA,eAAe,QAAQ,gBAAgB;AAChD,SAASC,MAAM,EAAEC,MAAM,QAAQ,SAAS;AAExC;;;;CAIC,GACD,OAAO,MAAMC,eAAe,EAAW;AAEvC;;;;;;;;CAQC,GACD,OAAO,MAAMC,gBAAgB;IAAC;IAAGD;CAAa,CAAU;AAExD,MAAME,OAAOH,OAAOI,MAAM,CAAC;IACzBC,QAAQL,OAAOM,QAAQ,CAAC;QAAC;QAAO;QAAQ;QAAO;QAAS;KAAS;IACjEC,MAAMP,OAAOQ,MAAM;AACrB;AAEA;;;;;CAKC,GACD,OAAO,MAAMC,cAAcT,OAAOI,MAAM,CAAC;IACvCM,QAAQV,OAAOW,WAAW,CAACX,OAAOQ,MAAM;IACxCI,eAAeZ,OAAOW,WAAW,CAACX,OAAOQ,MAAM;IAC/CK,cAAcb,OAAOW,WAAW,CAACX,OAAOc,KAAK,CAACd,OAAOQ,MAAM;AAC7D,GAAG;AAGH;;;;;CAKC,GACD,OAAO,MAAMO,YAAYf,OAAOI,MAAM,CAAC;IACrC,oDAAoD,GACpDY,MAAMhB,OAAOQ,MAAM;IACnBS,UAAUjB,OAAOc,KAAK,CAACd,OAAOQ,MAAM;AACtC,GAAG;AAGH,0EAA0E,GAC1E,OAAO,MAAMU,WAAWlB,OAAOI,MAAM,CAAC;IACpC,qDAAqD,GACrDe,KAAKnB,OAAOQ,MAAM;IAClB,8EAA8E,GAC9EY,MAAMpB,OAAOQ,MAAM;IACnBa,aAA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YlD,OAAOc,KAAK,CAACwB;IACzBa,YAAYnD,OAAOQ,MAAM;IACzB;;;GAGC,GACD4C,UAAUpD,OAAOW,WAAW,CAACX,OAAOc,KAAK,CAACoB;AAC5C,GAAG;AAGH;;;;CAIC,GACD,OAAO,MAAMmB,aAAa,CACxBC,QAEAtD,OAAOuD,mBAAmB,CAACZ,MAAMW,OAAOE,IAAI,CAC1CzD,OAAO0D,QAAQ,CACbC,CAAAA,QACE,IAAI5D,gBAAgB;YAClB6D,QAAQ,CAAC,0BAA0B,EAAED,MAAME,OAAO,EAAE;QACtD,KAEJ;AAEJ,0DAA0D,GAC1D,OAAO,MAAMC,aAAa,CAACC,OACzB9D,OAAO+D,UAAU,CAACpB,MAAMmB,MAAM;AAEhC,gDAAgD,GAChD,OAAO,MAAME,YAAY,CAACF,OACxBA,KAAKb,MAAM,CAACgB,OAAO,CAACC,CAAAA,QAASA,MAAMjC,KAAK,EAAE"}
@@ -47,6 +47,19 @@ export interface ShutdownRegistry {
47
47
  * it consults any probe: nothing is failing, the process is leaving.
48
48
  */
49
49
  readonly terminating: Effect.Effect<boolean>;
50
+ /**
51
+ * Completes the moment this process begins terminating — on the signal,
52
+ * **before** the server closes — and never fails.
53
+ *
54
+ * For a connection that only ends when told to: a server-sent stream. The
55
+ * server's close waits for every open response, and a stream is one that
56
+ * never finishes by itself, so a stream drained in a hook or a finalizer —
57
+ * both run after that close — was held open until the server gave up on it,
58
+ * twenty seconds on, and then cut without the `retry:` that spreads its
59
+ * reconnect (#492). Awaiting this instead ends it first, and the close is
60
+ * immediate.
61
+ */
62
+ readonly whenTerminating: Effect.Effect<void>;
50
63
  /**
51
64
  * Run every hook, phase by phase, in registration order. Never fails.
52
65
  *
@@ -1 +1 @@
1
- {"version":3,"file":"shutdown-registry.d.ts","sourceRoot":"","sources":["../../src/shutdown/shutdown-registry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhD;;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,MAAM,MAAM,eAAe,GACvB;IAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACrD;IACE,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC;IAC9B,oEAAoE;IACpE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB,GACD;IAAE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAA;CAAE,CAAC;AAEjC,yCAAyC;AACzC,MAAM,MAAM,mBAAmB,GAAG,CAChC,OAAO,EAAE,eAAe,KACrB,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;;AAEzB,qBAAa,mBAAoB,SAAQ,wBAGf;CAAG;AAyB7B;;;;;;;;;GASG;AACH,eAAO,MAAM,oBAAoB,GAC/B,SAAQ,mBAAuC,KAC9C;IACD,QAAQ,EAAE,gBAAgB,CAAC;IAC3B,KAAK,EAAE,MAAM,IAAI,CAAC;CA4CnB,CAAC"}
@@ -49,13 +49,22 @@ export class ShutdownRegistryTag extends Context.Service()('ShutdownRegistryTag'
49
49
  */ export const makeShutdownRegistry = (report = ()=>Effect.void)=>{
50
50
  const hooks = [];
51
51
  let terminating = false;
52
+ const waiting = [];
53
+ const signalled = new Promise((resolve)=>{
54
+ waiting.push(resolve);
55
+ });
56
+ const terminate = ()=>{
57
+ terminating = true;
58
+ for (const wake of waiting)wake();
59
+ };
52
60
  const registry = {
53
61
  register: (hook)=>Effect.sync(()=>{
54
62
  hooks.push(hook);
55
63
  }),
56
64
  terminating: Effect.sync(()=>terminating),
65
+ whenTerminating: Effect.promise(()=>signalled),
57
66
  drain: Effect.gen(function*() {
58
- terminating = true;
67
+ terminate();
59
68
  yield* report({
60
69
  _tag: 'draining',
61
70
  hooks: hooks.length
@@ -75,9 +84,7 @@ export class ShutdownRegistryTag extends Context.Service()('ShutdownRegistryTag'
75
84
  };
76
85
  return {
77
86
  registry,
78
- begin: ()=>{
79
- terminating = true;
80
- }
87
+ begin: terminate
81
88
  };
82
89
  };
83
90
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/shutdown/shutdown-registry.ts"],"sourcesContent":["import { Cause, Context, Effect } from 'effect';\n\n/**\n * The order a shutdown runs in.\n *\n * **`stop-intake`** stops the process taking new work — cancelling the bus's\n * consumers, interrupting a polling daemon. **`flush`** carries out what is\n * already committed but not yet delivered: the outbox relay's final pass. The\n * two are separate phases rather than one list because they are ordered against\n * each other and not merely against the connections closing — flushing while\n * consumers are still folding events would race a drain against fresh work, and\n * the relay would go on finding entries the handlers keep writing.\n */\nexport const SHUTDOWN_PHASES = ['stop-intake', 'flush'] as const;\n\nexport type ShutdownPhase = (typeof SHUTDOWN_PHASES)[number];\n\n/**\n * One thing to do before this process's connections close.\n *\n * `run` is fully applied, exactly like `HealthProbe.check`: whoever\n * registers the hook has already closed over the connector, the fiber or the\n * store, so the drain can run every hook without knowing what any of them\n * touches.\n */\nexport interface ShutdownHook {\n /** Logged when the hook runs, and when it fails. Never rendered to a caller. */\n readonly name: string;\n readonly phase: ShutdownPhase;\n readonly run: Effect.Effect<void>;\n}\n\n/**\n * How long one hook may take before the drain moves on without it.\n *\n * Not belt-and-braces. Kubernetes sends SIGKILL at the end of\n * `terminationGracePeriodSeconds` whatever the process is doing, so an\n * unbounded hook does not buy time — it spends the budget of the hooks behind\n * it and then loses them entirely. A hook that cannot finish in time has\n * already failed; the difference is whether the *next* one gets to run.\n */\nconst HOOK_TIMEOUT = '10 seconds';\n\n/**\n * What a service does on its way out, registered by whoever opened the thing\n * that needs draining.\n *\n * A sibling of `HealthRegistry` and `WiringRegistry`, and for the same reason:\n * a `-service` that gains a bus gains its consumer drain by merging that\n * adapter's layer, and nothing in the service's own `main.ts` has to remember\n * to list it. A hand-written sequence per composition root drifts the first\n * time someone adds a dependency; this cannot, because the hook ships with the\n * client that needs it.\n */\nexport interface ShutdownRegistry {\n /** Add a hook. Called by an adapter layer as it constructs its client. */\n readonly register: (hook: ShutdownHook) => Effect.Effect<void>;\n /**\n * Whether this process has begun terminating.\n *\n * Read by `GET /api/health/ready`, which answers `503` from here **before**\n * it consults any probe: nothing is failing, the process is leaving.\n */\n readonly terminating: Effect.Effect<boolean>;\n /**\n * Run every hook, phase by phase, in registration order. Never fails.\n *\n * Called from a `Layer` finalizer, which is what orders it: the drain layer\n * is built after the composition root, so it is released before it, and the\n * connections a hook needs are still open while it runs.\n */\n readonly drain: Effect.Effect<void>;\n}\n\n/**\n * DI tag for the shutdown registry.\n *\n * In the contract layer beside `HealthRegistryTag` so an\n * `entifix-ts-*-client` package can register a hook without depending on a\n * shell.\n */\n/**\n * What a drain did, for the host to log\n * ([ADR 0013](../../../../../docs/adr/0013-failures-are-typed-values-decided-at-the-shell.md)):\n * the registry says it, the shell that made it decides how loud.\n */\nexport type ShutdownOutcome =\n | { readonly _tag: 'draining'; readonly hooks: number }\n | {\n readonly _tag: 'hookFailed';\n readonly hook: string;\n readonly phase: ShutdownPhase;\n /** `Cause.pretty`: a timeout and a failure stay distinguishable. */\n readonly cause: string;\n }\n | { readonly _tag: 'drained' };\n\n/** Where a drain reports what it did. */\nexport type ShutdownOutcomeSink = (\n outcome: ShutdownOutcome,\n) => Effect.Effect<void>;\n\nexport class ShutdownRegistryTag extends Context.Service<\n ShutdownRegistryTag,\n ShutdownRegistry\n>()('ShutdownRegistryTag') {}\n\n/**\n * Runs one hook and swallows whatever it does.\n *\n * A failure and a timeout are reported the same way on purpose: both mean this\n * hook did not do its job, both leave the ones behind it still worth running,\n * and neither is something a terminating process can act on. `Cause.pretty`\n * keeps them distinguishable for whoever reads the log the host writes.\n */\nconst runHook =\n (report: ShutdownOutcomeSink) =>\n (hook: ShutdownHook): Effect.Effect<void> =>\n hook.run.pipe(\n Effect.timeout(HOOK_TIMEOUT),\n Effect.catchCause(cause =>\n report({\n _tag: 'hookFailed',\n hook: hook.name,\n phase: hook.phase,\n cause: Cause.pretty(cause),\n }),\n ),\n );\n\n/**\n * Builds the registry and the synchronous latch that flips it.\n *\n * Two halves for two callers, the `makeAmqpConnector` shape: the\n * `registry` goes into the layer graph, while `begin` is called from a\n * `process.on('SIGTERM')` handler — outside Effect, in the same tick the\n * runtime starts interrupting — so `/api/health/ready` stops claiming to be\n * ready the instant the signal lands rather than whenever the finalizers get\n * around to running.\n */\nexport const makeShutdownRegistry = (\n report: ShutdownOutcomeSink = () => Effect.void,\n): {\n registry: ShutdownRegistry;\n begin: () => void;\n} => {\n const hooks: ShutdownHook[] = [];\n let terminating = false;\n\n const registry: ShutdownRegistry = {\n register: hook =>\n Effect.sync(() => {\n hooks.push(hook);\n }),\n\n terminating: Effect.sync(() => terminating),\n\n drain: Effect.gen(function* () {\n terminating = true;\n yield* report({ _tag: 'draining', hooks: hooks.length });\n for (const phase of SHUTDOWN_PHASES) {\n // Sequential, and in registration order: `stop-intake` before `flush`\n // is the whole contract, and within a phase a later hook may depend on\n // an earlier one having stopped.\n yield* Effect.forEach(\n hooks.filter(hook => hook.phase === phase),\n runHook(report),\n { discard: true },\n );\n }\n yield* report({ _tag: 'drained' });\n }),\n };\n\n return {\n registry,\n begin: () => {\n terminating = true;\n },\n };\n};\n"],"names":["Cause","Context","Effect","SHUTDOWN_PHASES","HOOK_TIMEOUT","ShutdownRegistryTag","Service","runHook","report","hook","run","pipe","timeout","catchCause","cause","_tag","name","phase","pretty","makeShutdownRegistry","void","hooks","terminating","registry","register","sync","push","drain","gen","length","forEach","filter","discard","begin"],"mappings":"AAAA,SAASA,KAAK,EAAEC,OAAO,EAAEC,MAAM,QAAQ,SAAS;AAEhD;;;;;;;;;;CAUC,GACD,OAAO,MAAMC,kBAAkB;IAAC;IAAe;CAAQ,CAAU;AAmBjE;;;;;;;;CAQC,GACD,MAAMC,eAAe;AA6DrB,OAAO,MAAMC,4BAA4BJ,QAAQK,OAAO,GAGpD;AAAwB;AAE5B;;;;;;;CAOC,GACD,MAAMC,UACJ,CAACC,SACD,CAACC,OACCA,KAAKC,GAAG,CAACC,IAAI,CACXT,OAAOU,OAAO,CAACR,eACfF,OAAOW,UAAU,CAACC,CAAAA,QAChBN,OAAO;gBACLO,MAAM;gBACNN,MAAMA,KAAKO,IAAI;gBACfC,OAAOR,KAAKQ,KAAK;gBACjBH,OAAOd,MAAMkB,MAAM,CAACJ;YACtB;AAIR;;;;;;;;;CASC,GACD,OAAO,MAAMK,uBAAuB,CAClCX,SAA8B,IAAMN,OAAOkB,IAAI;IAK/C,MAAMC,QAAwB,EAAE;IAChC,IAAIC,cAAc;IAElB,MAAMC,WAA6B;QACjCC,UAAUf,CAAAA,OACRP,OAAOuB,IAAI,CAAC;gBACVJ,MAAMK,IAAI,CAACjB;YACb;QAEFa,aAAapB,OAAOuB,IAAI,CAAC,IAAMH;QAE/BK,OAAOzB,OAAO0B,GAAG,CAAC;YAChBN,cAAc;YACd,OAAOd,OAAO;gBAAEO,MAAM;gBAAYM,OAAOA,MAAMQ,MAAM;YAAC;YACtD,KAAK,MAAMZ,SAASd,gBAAiB;gBACnC,sEAAsE;gBACtE,uEAAuE;gBACvE,iCAAiC;gBACjC,OAAOD,OAAO4B,OAAO,CACnBT,MAAMU,MAAM,CAACtB,CAAAA,OAAQA,KAAKQ,KAAK,KAAKA,QACpCV,QAAQC,SACR;oBAAEwB,SAAS;gBAAK;YAEpB;YACA,OAAOxB,OAAO;gBAAEO,MAAM;YAAU;QAClC;IACF;IAEA,OAAO;QACLQ;QACAU,OAAO;YACLX,cAAc;QAChB;IACF;AACF,EAAE"}
1
+ {"version":3,"sources":["../../src/shutdown/shutdown-registry.ts"],"sourcesContent":["import { Cause, Context, Effect } from 'effect';\n\n/**\n * The order a shutdown runs in.\n *\n * **`stop-intake`** stops the process taking new work — cancelling the bus's\n * consumers, interrupting a polling daemon. **`flush`** carries out what is\n * already committed but not yet delivered: the outbox relay's final pass. The\n * two are separate phases rather than one list because they are ordered against\n * each other and not merely against the connections closing — flushing while\n * consumers are still folding events would race a drain against fresh work, and\n * the relay would go on finding entries the handlers keep writing.\n */\nexport const SHUTDOWN_PHASES = ['stop-intake', 'flush'] as const;\n\nexport type ShutdownPhase = (typeof SHUTDOWN_PHASES)[number];\n\n/**\n * One thing to do before this process's connections close.\n *\n * `run` is fully applied, exactly like `HealthProbe.check`: whoever\n * registers the hook has already closed over the connector, the fiber or the\n * store, so the drain can run every hook without knowing what any of them\n * touches.\n */\nexport interface ShutdownHook {\n /** Logged when the hook runs, and when it fails. Never rendered to a caller. */\n readonly name: string;\n readonly phase: ShutdownPhase;\n readonly run: Effect.Effect<void>;\n}\n\n/**\n * How long one hook may take before the drain moves on without it.\n *\n * Not belt-and-braces. Kubernetes sends SIGKILL at the end of\n * `terminationGracePeriodSeconds` whatever the process is doing, so an\n * unbounded hook does not buy time — it spends the budget of the hooks behind\n * it and then loses them entirely. A hook that cannot finish in time has\n * already failed; the difference is whether the *next* one gets to run.\n */\nconst HOOK_TIMEOUT = '10 seconds';\n\n/**\n * What a service does on its way out, registered by whoever opened the thing\n * that needs draining.\n *\n * A sibling of `HealthRegistry` and `WiringRegistry`, and for the same reason:\n * a `-service` that gains a bus gains its consumer drain by merging that\n * adapter's layer, and nothing in the service's own `main.ts` has to remember\n * to list it. A hand-written sequence per composition root drifts the first\n * time someone adds a dependency; this cannot, because the hook ships with the\n * client that needs it.\n */\nexport interface ShutdownRegistry {\n /** Add a hook. Called by an adapter layer as it constructs its client. */\n readonly register: (hook: ShutdownHook) => Effect.Effect<void>;\n /**\n * Whether this process has begun terminating.\n *\n * Read by `GET /api/health/ready`, which answers `503` from here **before**\n * it consults any probe: nothing is failing, the process is leaving.\n */\n readonly terminating: Effect.Effect<boolean>;\n /**\n * Completes the moment this process begins terminating — on the signal,\n * **before** the server closes — and never fails.\n *\n * For a connection that only ends when told to: a server-sent stream. The\n * server's close waits for every open response, and a stream is one that\n * never finishes by itself, so a stream drained in a hook or a finalizer —\n * both run after that close — was held open until the server gave up on it,\n * twenty seconds on, and then cut without the `retry:` that spreads its\n * reconnect (#492). Awaiting this instead ends it first, and the close is\n * immediate.\n */\n readonly whenTerminating: Effect.Effect<void>;\n /**\n * Run every hook, phase by phase, in registration order. Never fails.\n *\n * Called from a `Layer` finalizer, which is what orders it: the drain layer\n * is built after the composition root, so it is released before it, and the\n * connections a hook needs are still open while it runs.\n */\n readonly drain: Effect.Effect<void>;\n}\n\n/**\n * DI tag for the shutdown registry.\n *\n * In the contract layer beside `HealthRegistryTag` so an\n * `entifix-ts-*-client` package can register a hook without depending on a\n * shell.\n */\n/**\n * What a drain did, for the host to log\n * ([ADR 0013](../../../../../docs/adr/0013-failures-are-typed-values-decided-at-the-shell.md)):\n * the registry says it, the shell that made it decides how loud.\n */\nexport type ShutdownOutcome =\n | { readonly _tag: 'draining'; readonly hooks: number }\n | {\n readonly _tag: 'hookFailed';\n readonly hook: string;\n readonly phase: ShutdownPhase;\n /** `Cause.pretty`: a timeout and a failure stay distinguishable. */\n readonly cause: string;\n }\n | { readonly _tag: 'drained' };\n\n/** Where a drain reports what it did. */\nexport type ShutdownOutcomeSink = (\n outcome: ShutdownOutcome,\n) => Effect.Effect<void>;\n\nexport class ShutdownRegistryTag extends Context.Service<\n ShutdownRegistryTag,\n ShutdownRegistry\n>()('ShutdownRegistryTag') {}\n\n/**\n * Runs one hook and swallows whatever it does.\n *\n * A failure and a timeout are reported the same way on purpose: both mean this\n * hook did not do its job, both leave the ones behind it still worth running,\n * and neither is something a terminating process can act on. `Cause.pretty`\n * keeps them distinguishable for whoever reads the log the host writes.\n */\nconst runHook =\n (report: ShutdownOutcomeSink) =>\n (hook: ShutdownHook): Effect.Effect<void> =>\n hook.run.pipe(\n Effect.timeout(HOOK_TIMEOUT),\n Effect.catchCause(cause =>\n report({\n _tag: 'hookFailed',\n hook: hook.name,\n phase: hook.phase,\n cause: Cause.pretty(cause),\n }),\n ),\n );\n\n/**\n * Builds the registry and the synchronous latch that flips it.\n *\n * Two halves for two callers, the `makeAmqpConnector` shape: the\n * `registry` goes into the layer graph, while `begin` is called from a\n * `process.on('SIGTERM')` handler — outside Effect, in the same tick the\n * runtime starts interrupting — so `/api/health/ready` stops claiming to be\n * ready the instant the signal lands rather than whenever the finalizers get\n * around to running.\n */\nexport const makeShutdownRegistry = (\n report: ShutdownOutcomeSink = () => Effect.void,\n): {\n registry: ShutdownRegistry;\n begin: () => void;\n} => {\n const hooks: ShutdownHook[] = [];\n let terminating = false;\n const waiting: (() => void)[] = [];\n const signalled = new Promise<void>(resolve => {\n waiting.push(resolve);\n });\n const terminate = () => {\n terminating = true;\n for (const wake of waiting) wake();\n };\n\n const registry: ShutdownRegistry = {\n register: hook =>\n Effect.sync(() => {\n hooks.push(hook);\n }),\n\n terminating: Effect.sync(() => terminating),\n\n whenTerminating: Effect.promise(() => signalled),\n\n drain: Effect.gen(function* () {\n terminate();\n yield* report({ _tag: 'draining', hooks: hooks.length });\n for (const phase of SHUTDOWN_PHASES) {\n // Sequential, and in registration order: `stop-intake` before `flush`\n // is the whole contract, and within a phase a later hook may depend on\n // an earlier one having stopped.\n yield* Effect.forEach(\n hooks.filter(hook => hook.phase === phase),\n runHook(report),\n { discard: true },\n );\n }\n yield* report({ _tag: 'drained' });\n }),\n };\n\n return {\n registry,\n begin: terminate,\n };\n};\n"],"names":["Cause","Context","Effect","SHUTDOWN_PHASES","HOOK_TIMEOUT","ShutdownRegistryTag","Service","runHook","report","hook","run","pipe","timeout","catchCause","cause","_tag","name","phase","pretty","makeShutdownRegistry","void","hooks","terminating","waiting","signalled","Promise","resolve","push","terminate","wake","registry","register","sync","whenTerminating","promise","drain","gen","length","forEach","filter","discard","begin"],"mappings":"AAAA,SAASA,KAAK,EAAEC,OAAO,EAAEC,MAAM,QAAQ,SAAS;AAEhD;;;;;;;;;;CAUC,GACD,OAAO,MAAMC,kBAAkB;IAAC;IAAe;CAAQ,CAAU;AAmBjE;;;;;;;;CAQC,GACD,MAAMC,eAAe;AA0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AAOb;IACT;AACF,EAAE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@entifix/business",
3
- "version": "0.16.0",
3
+ "version": "0.18.0",
4
4
  "description": "The ports every adapter implements — repository, session, token, health — and the generic get, load, save and delete use-cases.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -66,7 +66,7 @@
66
66
  },
67
67
  "dependencies": {
68
68
  "@swc/helpers": "0.5.23",
69
- "@entifix/core": "0.16.0"
69
+ "@entifix/core": "0.18.0"
70
70
  },
71
71
  "peerDependencies": {
72
72
  "effect": "^4.0.7"