@entifix/transactions 0.10.2 → 0.12.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.
- package/dist/contracts/command.d.ts +1 -0
- package/dist/contracts/command.d.ts.map +1 -1
- package/dist/contracts/command.js +7 -3
- package/dist/contracts/command.js.map +1 -1
- package/dist/contracts/event.d.ts +5 -4
- package/dist/contracts/event.d.ts.map +1 -1
- package/dist/contracts/event.js +14 -1
- package/dist/contracts/event.js.map +1 -1
- package/dist/contracts/flow-capability.d.ts +22 -0
- package/dist/contracts/flow-capability.d.ts.map +1 -0
- package/dist/contracts/flow-capability.js +26 -0
- package/dist/contracts/flow-capability.js.map +1 -0
- package/dist/contracts/flow-messages.d.ts +52 -0
- package/dist/contracts/flow-messages.d.ts.map +1 -0
- package/dist/contracts/flow-messages.js +65 -0
- package/dist/contracts/flow-messages.js.map +1 -0
- package/dist/contracts/inbox.d.ts +19 -2
- package/dist/contracts/inbox.d.ts.map +1 -1
- package/dist/contracts/inbox.js +16 -1
- package/dist/contracts/inbox.js.map +1 -1
- package/dist/contracts/messages.d.ts +37 -0
- package/dist/contracts/messages.d.ts.map +1 -0
- package/dist/contracts/messages.js +68 -0
- package/dist/contracts/messages.js.map +1 -0
- package/dist/contracts/outbox.d.ts +50 -12
- package/dist/contracts/outbox.d.ts.map +1 -1
- package/dist/contracts/outbox.js +18 -1
- package/dist/contracts/outbox.js.map +1 -1
- package/dist/contracts/record.d.ts +7 -8
- package/dist/contracts/record.d.ts.map +1 -1
- package/dist/contracts/record.js +8 -8
- package/dist/contracts/record.js.map +1 -1
- package/dist/contracts/unit-of-work.d.ts +3 -2
- package/dist/contracts/unit-of-work.d.ts.map +1 -1
- package/dist/contracts/unit-of-work.js.map +1 -1
- package/dist/engine/in-memory-bulkhead.d.ts +20 -0
- package/dist/engine/in-memory-bulkhead.d.ts.map +1 -0
- package/dist/engine/in-memory-bulkhead.js +38 -0
- package/dist/engine/in-memory-bulkhead.js.map +1 -0
- package/dist/engine/in-memory-circuit-breaker.d.ts +28 -0
- package/dist/engine/in-memory-circuit-breaker.d.ts.map +1 -0
- package/dist/engine/in-memory-circuit-breaker.js +151 -0
- package/dist/engine/in-memory-circuit-breaker.js.map +1 -0
- package/dist/engine/index.d.ts +4 -0
- package/dist/engine/index.d.ts.map +1 -1
- package/dist/engine/index.js +4 -0
- package/dist/engine/index.js.map +1 -1
- package/dist/engine/outbox-metrics.d.ts +47 -0
- package/dist/engine/outbox-metrics.d.ts.map +1 -0
- package/dist/engine/outbox-metrics.js +68 -0
- package/dist/engine/outbox-metrics.js.map +1 -0
- package/dist/engine/outbox-relay.d.ts +102 -0
- package/dist/engine/outbox-relay.d.ts.map +1 -0
- package/dist/engine/outbox-relay.js +111 -0
- package/dist/engine/outbox-relay.js.map +1 -0
- package/dist/engine/run-transaction.d.ts +4 -4
- package/dist/engine/run-transaction.d.ts.map +1 -1
- package/dist/engine/run-transaction.js +4 -4
- package/dist/engine/run-transaction.js.map +1 -1
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/ports/bulkhead.d.ts +28 -0
- package/dist/ports/bulkhead.d.ts.map +1 -0
- package/dist/ports/bulkhead.js +18 -0
- package/dist/ports/bulkhead.js.map +1 -0
- package/dist/ports/circuit-breaker.d.ts +62 -0
- package/dist/ports/circuit-breaker.d.ts.map +1 -0
- package/dist/ports/circuit-breaker.js +42 -0
- package/dist/ports/circuit-breaker.js.map +1 -0
- package/dist/ports/event-bus.d.ts +10 -0
- package/dist/ports/event-bus.d.ts.map +1 -1
- package/dist/ports/event-bus.js.map +1 -1
- package/dist/ports/flow-engine.d.ts +60 -0
- package/dist/ports/flow-engine.d.ts.map +1 -0
- package/dist/ports/flow-engine.js +19 -0
- package/dist/ports/flow-engine.js.map +1 -0
- package/dist/ports/participant-client.d.ts +32 -0
- package/dist/ports/participant-client.d.ts.map +1 -0
- package/dist/ports/participant-client.js +5 -0
- package/dist/ports/participant-client.js.map +1 -0
- package/dist/reactions/on.d.ts.map +1 -1
- package/dist/reactions/on.js +2 -1
- package/dist/reactions/on.js.map +1 -1
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"command.d.ts","sourceRoot":"","sources":["../../src/contracts/command.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"command.d.ts","sourceRoot":"","sources":["../../src/contracts/command.ts"],"names":[],"mappings":"AACA,OAAO,eAAe,CAAC;AAEvB,OAAO,EAEL,iBAAiB,EACjB,KAAK,eAAe,EACpB,KAAK,eAAe,EAEpB,KAAK,gBAAgB,EACtB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC;;;;;;GAMG;AACH,MAAM,WAAW,kBAAkB,CAAC,QAAQ,GAAG,gBAAgB;IAC7D;;;;;;;;OAQG;IACH,aAAa,EAAE,MAAM,CAAC;IACtB,6CAA6C;IAC7C,IAAI,EAAE,QAAQ,CAAC;IACf,4EAA4E;IAC5E,MAAM,EAAE,MAAM,CAAC;IACf,uCAAuC;IACvC,OAAO,EAAE,QAAQ,CAAC;IAClB;;;;;;;;;;;;;;OAcG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,MAAM,eAAe,CAAC,QAAQ,GAAG,gBAAgB,IAAI,eAAe,CACxE,kBAAkB,CAAC,QAAQ,CAAC,CAC7B,CAAC;AAEF,+DAA+D;AAC/D,wBAAgB,mBAAmB,CAAC,QAAQ,GAAG,gBAAgB,EAC7D,OAAO,EAAE,kBAAkB,CAAC,QAAQ,CAAC,GACpC,eAAe,CAAC,QAAQ,CAAC,CAE3B;AAaD,kDAAkD;AAClD,eAAO,MAAM,eAAe,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,MACX,CAAC;AAEhD;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,GAAG,gBAAgB,EAC7D,IAAI,EAAE,OAAO,GACZ,MAAM,CAAC,MAAM,CACd,kBAAkB,CAAC,QAAQ,CAAC,EAC5B,iBAAiB,GAAG,eAAe,CACpC,CAuBA"}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { _ as _object_without_properties_loose } from "@swc/helpers/_/_object_without_properties_loose";
|
|
2
|
-
|
|
2
|
+
// Evaluated for its registrations: this reader decodes against them.
|
|
3
|
+
import './messages.js';
|
|
4
|
+
import { decodeRequestEnvelope, EntifixBuildError, makeEnvelope } from '@entifix/core';
|
|
3
5
|
import { Effect } from 'effect';
|
|
4
6
|
/** Frames a command as a transport-free `command` envelope. */ export function makeCommandEnvelope(command) {
|
|
5
7
|
return makeEnvelope('command', command.entity, command);
|
|
@@ -22,9 +24,11 @@ import { Effect } from 'effect';
|
|
|
22
24
|
* appearing to succeed.
|
|
23
25
|
*/ export function readCommandEnvelope(body) {
|
|
24
26
|
return Effect.gen(function*() {
|
|
25
|
-
const envelope = yield*
|
|
27
|
+
const envelope = yield* decodeRequestEnvelope(body, 'command', 'command');
|
|
26
28
|
const command = envelope.data;
|
|
27
|
-
|
|
29
|
+
// The registered schema already proved the frame; what it cannot say is
|
|
30
|
+
// that the id is a UUID, which is what makes it safe as a primary key.
|
|
31
|
+
if (!isTransactionId(command.transactionId)) {
|
|
28
32
|
return yield* Effect.fail(new EntifixBuildError('command envelope carried no valid command', undefined, {
|
|
29
33
|
body
|
|
30
34
|
}));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/contracts/command.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["../../src/contracts/command.ts"],"sourcesContent":["// Evaluated for its registrations: this reader decodes against them.\nimport './messages.js';\n\nimport {\n decodeRequestEnvelope,\n EntifixBuildError,\n type EntifixEnvelope,\n type InvalidEnvelope,\n makeEnvelope,\n type SerializedEntity,\n} from '@entifix/core';\nimport { Effect } from 'effect';\n\n/**\n * A write intent in CQRS terms. A client no longer mutates directly — it issues\n * a command, the service runs the transaction facade over it, and answers with a\n * transaction id. `payload` is a serialized entity (the same wire shape the\n * entity envelopes carry), so a command round-trips through the existing\n * (de)serializer.\n */\nexport interface TransactionCommand<TPayload = SerializedEntity> {\n /**\n * Correlates every event and record for this run, and **the client mints it**.\n *\n * It is also the stored entity's id and the idempotency key: re-sending a\n * command with the same id returns the first one's answer instead of writing\n * twice. That makes it untrusted input which becomes a primary key, so\n * {@link readCommandEnvelope} constrains it to a UUID rather than accepting\n * any string.\n */\n transactionId: string;\n /** What to do — only `create` in phase 1. */\n type: 'create';\n /** The target entity's `key` (e.g. `product`), the routing/lock subject. */\n entity: string;\n /** The serialized entity to act on. */\n payload: TPayload;\n /**\n * Whose organization this command acts in — **stamped by the route from the\n * verified token, never read from the wire**.\n *\n * {@link readCommandEnvelope} drops whatever a caller sent under this name, so\n * a client cannot name another tenant by writing one into the body. It is the\n * same rule the id already follows (`entity.id = params.id`), and the same\n * rule `requireOrganization` states: the organization comes from the token and\n * nowhere else.\n *\n * Optional because the parser produces a command without it and the route\n * fills it in a moment later. A command that reaches the engine still carrying\n * `undefined` emits events that no tenant-scoped stream connection receives —\n * which is the intended direction to fail.\n */\n organizationId?: string;\n}\n\nexport type CommandEnvelope<TPayload = SerializedEntity> = EntifixEnvelope<\n TransactionCommand<TPayload>\n>;\n\n/** Frames a command as a transport-free `command` envelope. */\nexport function makeCommandEnvelope<TPayload = SerializedEntity>(\n command: TransactionCommand<TPayload>,\n): CommandEnvelope<TPayload> {\n return makeEnvelope('command', command.entity, command);\n}\n\n/**\n * A canonical RFC 9562 UUID — any version, standard variant.\n *\n * The transaction id arrives from the client and becomes a primary key, so the\n * key space it may address has to be fixed rather than \"whatever string was\n * sent\". Accepting every version keeps v4 (random) and v7 (time-ordered) both\n * usable without the wire format becoming a second decision.\n */\nconst UUID =\n /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;\n\n/** Whether a value is a usable transaction id. */\nexport const isTransactionId = (value: unknown): value is string =>\n typeof value === 'string' && UUID.test(value);\n\n/**\n * Parses a `command` envelope, failing on a wrong shape/type.\n *\n * A command carrying no valid transaction id is rejected outright — there is no\n * server-side fallback that mints one. That is deliberate: the id is the\n * idempotency key, and a caller who omits it silently loses retry safety while\n * appearing to succeed.\n */\nexport function readCommandEnvelope<TPayload = SerializedEntity>(\n body: unknown,\n): Effect.Effect<\n TransactionCommand<TPayload>,\n EntifixBuildError | InvalidEnvelope\n> {\n return Effect.gen(function* () {\n const envelope = yield* decodeRequestEnvelope(body, 'command', 'command');\n const command = envelope.data as TransactionCommand<TPayload>;\n // The registered schema already proved the frame; what it cannot say is\n // that the id is a UUID, which is what makes it safe as a primary key.\n if (!isTransactionId(command.transactionId)) {\n return yield* Effect.fail(\n new EntifixBuildError(\n 'command envelope carried no valid command',\n undefined,\n {\n body,\n },\n ),\n );\n }\n // `organizationId` is deliberately **not** carried over from the parsed\n // body. Spreading the caller's object here would let a request name another\n // tenant, which is precisely the scoping the stream depends on.\n const { organizationId: _ignored, ...trusted } = command;\n return trusted as TransactionCommand<TPayload>;\n });\n}\n"],"names":["decodeRequestEnvelope","EntifixBuildError","makeEnvelope","Effect","makeCommandEnvelope","command","entity","UUID","isTransactionId","value","test","readCommandEnvelope","body","gen","envelope","data","transactionId","fail","undefined","organizationId","_ignored","trusted"],"mappings":";AAAA,qEAAqE;AACrE,OAAO,gBAAgB;AAEvB,SACEA,qBAAqB,EACrBC,iBAAiB,EAGjBC,YAAY,QAEP,gBAAgB;AACvB,SAASC,MAAM,QAAQ,SAAS;AAgDhC,6DAA6D,GAC7D,OAAO,SAASC,oBACdC,OAAqC;IAErC,OAAOH,aAAa,WAAWG,QAAQC,MAAM,EAAED;AACjD;AAEA;;;;;;;CAOC,GACD,MAAME,OACJ;AAEF,gDAAgD,GAChD,OAAO,MAAMC,kBAAkB,CAACC,QAC9B,OAAOA,UAAU,YAAYF,KAAKG,IAAI,CAACD,OAAO;AAEhD;;;;;;;CAOC,GACD,OAAO,SAASE,oBACdC,IAAa;IAKb,OAAOT,OAAOU,GAAG,CAAC;QAChB,MAAMC,WAAW,OAAOd,sBAAsBY,MAAM,WAAW;QAC/D,MAAMP,UAAUS,SAASC,IAAI;QAC7B,wEAAwE;QACxE,uEAAuE;QACvE,IAAI,CAACP,gBAAgBH,QAAQW,aAAa,GAAG;YAC3C,OAAO,OAAOb,OAAOc,IAAI,CACvB,IAAIhB,kBACF,6CACAiB,WACA;gBACEN;YACF;QAGN;QACA,wEAAwE;QACxE,4EAA4E;QAC5E,gEAAgE;QAChE,MAAM,EAAEO,gBAAgBC,QAAQ,EAAc,GAAGf,SAAZgB,2CAAYhB;;;QACjD,OAAOgB;IACT;AACF"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type EntifixEnvelope, type EntityId } from '@entifix/core';
|
|
2
2
|
import type { TransactionOutcome } from '../ports/transaction-handler.js';
|
|
3
3
|
import type { TransactionCommand } from './command.js';
|
|
4
|
+
import type { OutboxEvent } from './outbox.js';
|
|
4
5
|
/**
|
|
5
6
|
* The lifecycle states a transaction record settles into.
|
|
6
7
|
*
|
|
@@ -77,8 +78,8 @@ export declare const transactionEventName: (step: TransactionStep) => string;
|
|
|
77
78
|
* that can drift.
|
|
78
79
|
*/
|
|
79
80
|
export declare const transactionEventId: (transactionId: string, step: TransactionStep) => string;
|
|
80
|
-
export declare const acceptedEvent: (command: TransactionCommand, source: string) =>
|
|
81
|
-
export declare const completedEvent: (command: TransactionCommand, outcome: TransactionOutcome, source: string) =>
|
|
82
|
-
export declare const failedEvent: (command: TransactionCommand, error: unknown, source: string) =>
|
|
81
|
+
export declare const acceptedEvent: (command: TransactionCommand, source: string) => OutboxEvent<TransactionEvent>;
|
|
82
|
+
export declare const completedEvent: (command: TransactionCommand, outcome: TransactionOutcome, source: string) => OutboxEvent<TransactionEvent>;
|
|
83
|
+
export declare const failedEvent: (command: TransactionCommand, error: unknown, source: string) => OutboxEvent<TransactionEvent>;
|
|
83
84
|
export type TransactionEventEnvelope = EntifixEnvelope<TransactionEvent>;
|
|
84
85
|
//# sourceMappingURL=event.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event.d.ts","sourceRoot":"","sources":["../../src/contracts/event.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"event.d.ts","sourceRoot":"","sources":["../../src/contracts/event.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,eAAe,EACpB,KAAK,QAAQ,EACd,MAAM,eAAe,CAAC;AAEvB,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AACvD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,kBAAkB,sDAKrB,CAAC;AAEX,6DAA6D;AAC7D,MAAM,MAAM,gBAAgB,GAAG,CAAC,OAAO,kBAAkB,CAAC,CAAC,MAAM,CAAC,CAAC;AAEnE,6CAA6C;AAC7C,MAAM,MAAM,eAAe,GAAG,UAAU,GAAG,WAAW,GAAG,QAAQ,CAAC;AAElE;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,gBAAgB;IAC/B,aAAa,EAAE,MAAM,CAAC;IACtB,iCAAiC;IACjC,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,gBAAgB,CAAC;IACxB,IAAI,EAAE,eAAe,CAAC;IACtB,uDAAuD;IACvD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,0CAA0C;IAC1C,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,+CAA+C;IAC/C,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,8BAA8B;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX;;;;;;;;;;;;OAYG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAID,8EAA8E;AAC9E,eAAO,MAAM,oBAAoB,GAAI,MAAM,eAAe,KAAG,MACtC,CAAC;AAExB;;;;;;;;;;GAUG;AACH,eAAO,MAAM,kBAAkB,GAC7B,eAAe,MAAM,EACrB,MAAM,eAAe,KACpB,MAAoC,CAAC;AA2BxC,eAAO,MAAM,aAAa,GACxB,SAAS,kBAAkB,EAC3B,QAAQ,MAAM,KACb,WAAW,CAAC,gBAAgB,CAQ3B,CAAC;AAEL,eAAO,MAAM,cAAc,GACzB,SAAS,kBAAkB,EAC3B,SAAS,kBAAkB,EAC3B,QAAQ,MAAM,KACb,WAAW,CAAC,gBAAgB,CAU3B,CAAC;AAEL,eAAO,MAAM,WAAW,GACtB,SAAS,kBAAkB,EAC3B,OAAO,OAAO,EACd,QAAQ,MAAM,KACb,WAAW,CAAC,gBAAgB,CAS3B,CAAC;AAEL,MAAM,MAAM,wBAAwB,GAAG,eAAe,CAAC,gBAAgB,CAAC,CAAC"}
|
package/dist/contracts/event.js
CHANGED
|
@@ -28,12 +28,25 @@ const now = ()=>new Date().toISOString();
|
|
|
28
28
|
* keeps the idempotency claim and the dedup key the same fact rather than two
|
|
29
29
|
* that can drift.
|
|
30
30
|
*/ export const transactionEventId = (transactionId, step)=>`${transactionId}:${step}`;
|
|
31
|
-
/**
|
|
31
|
+
/** Each step's version of its transaction: `accepted` first, then its end. */ const STEP_VERSION = {
|
|
32
|
+
accepted: 0,
|
|
33
|
+
completed: 1,
|
|
34
|
+
failed: 1
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Wraps a payload as a routable message from `source`. The transaction is the
|
|
38
|
+
* aggregate, so the outbox keeps `accepted` ahead of the step that ends it.
|
|
39
|
+
*/ const message = (source, data)=>({
|
|
32
40
|
name: transactionEventName(data.step),
|
|
33
41
|
id: transactionEventId(data.transactionId, data.step),
|
|
34
42
|
source,
|
|
35
43
|
at: data.at,
|
|
36
44
|
correlationId: data.transactionId,
|
|
45
|
+
aggregate: {
|
|
46
|
+
type: 'transaction',
|
|
47
|
+
id: data.transactionId
|
|
48
|
+
},
|
|
49
|
+
version: STEP_VERSION[data.step],
|
|
37
50
|
data
|
|
38
51
|
});
|
|
39
52
|
export const acceptedEvent = (command, source)=>message(source, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/contracts/event.ts"],"sourcesContent":["import {\n type DomainEvent,\n type EntifixEnvelope,\n type EntityId,\n} from '@entifix/core';\n\nimport type { TransactionOutcome } from '../ports/transaction-handler.js';\nimport type { TransactionCommand } from './command.js';\n\n/**\n * The lifecycle states a transaction record settles into.\n *\n * A const array rather than a bare union because something has to **enumerate**\n * them: a by-state gauge must report every state including the ones at zero, and\n * a series that simply stops being reported reads on most dashboards as \"no\n * data\" rather than as \"none\" — so `STALE` falling to zero would look exactly\n * like the metric having broken.\n *\n * Note there are four states but only three {@link TransactionStep}s: `STALE` is\n * applied by the recovery sweep and has no event of its own.\n */\nexport const TRANSACTION_STATES = [\n 'PENDING',\n 'COMPLETED',\n 'FAILED',\n 'STALE',\n] as const;\n\n/** The lifecycle state a transaction record settles into. */\nexport type TransactionState = (typeof TRANSACTION_STATES)[number];\n\n/** The facade step a given event reports. */\nexport type TransactionStep = 'accepted' | 'completed' | 'failed';\n\n/**\n * What a service reports as its transaction progresses — the **payload** of a\n * {@link DomainEvent}, not the message itself. The saga tracker is a passive\n * consumer of these: for a single-step write it dispatches nothing and only\n * records what the events tell it, which is what keeps such a write\n * choreographed. A flow spanning slices is orchestrated instead, from a\n * declarative definition, and its coordinator both reads these and\n * issues the next step — but that engine is not built, and single-step writes\n * stay exactly as described here when it is.\n *\n * `transactionId` and `at` are also reachable as the message's `correlationId`\n * and `at`. That duplication is deliberate and standard (CloudEvents' `subject`\n * does the same): metadata is what the *transport* routes and deduplicates on,\n * and the payload has to stand on its own for a consumer that has already\n * unwrapped it.\n */\nexport interface TransactionEvent {\n transactionId: string;\n /** The target entity's `key`. */\n entity: string;\n state: TransactionState;\n step: TransactionStep;\n /** The assigned code once executed (`product-001`). */\n code?: string;\n /** The stored entity id once executed. */\n entityId?: EntityId;\n /** Failure detail when `step === 'failed'`. */\n error?: string;\n /** ISO-8601 emission time. */\n at: string;\n /**\n * Whose organization this happened in — the per-connection scoping key for\n * the reactive stream.\n *\n * In `data` rather than in `meta`, under the envelope's rule: `meta` describes the\n * message, `data` describes the occurrence, and which vendor's catalog a\n * record was written in is a fact about what happened.\n *\n * **Optional, and absence is not a wildcard.** An event carrying no\n * organization is delivered to no tenant-scoped connection. Defaulting the\n * other way would make every event emitted before this member existed a\n * cross-tenant delivery, and it fails silently in the direction that matters.\n */\n organizationId?: string;\n}\n\nconst now = (): string => new Date().toISOString();\n\n/** The register's name for a transaction message: `transaction.completed`. */\nexport const transactionEventName = (step: TransactionStep): string =>\n `transaction.${step}`;\n\n/**\n * The message id for one step of one transaction — and therefore the\n * deduplication key.\n *\n * It is the transaction id **and** the step, never the transaction id alone.\n * One transaction emits up to three messages, so a consumer keying on the\n * transaction id would treat `completed` as a redelivery of `accepted` and drop\n * the outcome. The outbox's unique index is on exactly this value, which is what\n * keeps the idempotency claim and the dedup key the same fact rather than two\n * that can drift.\n */\nexport const transactionEventId = (\n transactionId: string,\n step: TransactionStep,\n): string => `${transactionId}:${step}`;\n\n/** Wraps a payload as a routable message from `source`. */\nconst message = (\n source: string,\n data: TransactionEvent,\n):
|
|
1
|
+
{"version":3,"sources":["../../src/contracts/event.ts"],"sourcesContent":["import {\n type DomainEvent,\n type EntifixEnvelope,\n type EntityId,\n} from '@entifix/core';\n\nimport type { TransactionOutcome } from '../ports/transaction-handler.js';\nimport type { TransactionCommand } from './command.js';\nimport type { OutboxEvent } from './outbox.js';\n\n/**\n * The lifecycle states a transaction record settles into.\n *\n * A const array rather than a bare union because something has to **enumerate**\n * them: a by-state gauge must report every state including the ones at zero, and\n * a series that simply stops being reported reads on most dashboards as \"no\n * data\" rather than as \"none\" — so `STALE` falling to zero would look exactly\n * like the metric having broken.\n *\n * Note there are four states but only three {@link TransactionStep}s: `STALE` is\n * applied by the recovery sweep and has no event of its own.\n */\nexport const TRANSACTION_STATES = [\n 'PENDING',\n 'COMPLETED',\n 'FAILED',\n 'STALE',\n] as const;\n\n/** The lifecycle state a transaction record settles into. */\nexport type TransactionState = (typeof TRANSACTION_STATES)[number];\n\n/** The facade step a given event reports. */\nexport type TransactionStep = 'accepted' | 'completed' | 'failed';\n\n/**\n * What a service reports as its transaction progresses — the **payload** of a\n * {@link DomainEvent}, not the message itself. The saga tracker is a passive\n * consumer of these: for a single-step write it dispatches nothing and only\n * records what the events tell it, which is what keeps such a write\n * choreographed. A flow spanning slices is orchestrated instead, from a\n * declarative definition, and its coordinator both reads these and\n * issues the next step — but that engine is not built, and single-step writes\n * stay exactly as described here when it is.\n *\n * `transactionId` and `at` are also reachable as the message's `correlationId`\n * and `at`. That duplication is deliberate and standard (CloudEvents' `subject`\n * does the same): metadata is what the *transport* routes and deduplicates on,\n * and the payload has to stand on its own for a consumer that has already\n * unwrapped it.\n */\nexport interface TransactionEvent {\n transactionId: string;\n /** The target entity's `key`. */\n entity: string;\n state: TransactionState;\n step: TransactionStep;\n /** The assigned code once executed (`product-001`). */\n code?: string;\n /** The stored entity id once executed. */\n entityId?: EntityId;\n /** Failure detail when `step === 'failed'`. */\n error?: string;\n /** ISO-8601 emission time. */\n at: string;\n /**\n * Whose organization this happened in — the per-connection scoping key for\n * the reactive stream.\n *\n * In `data` rather than in `meta`, under the envelope's rule: `meta` describes the\n * message, `data` describes the occurrence, and which vendor's catalog a\n * record was written in is a fact about what happened.\n *\n * **Optional, and absence is not a wildcard.** An event carrying no\n * organization is delivered to no tenant-scoped connection. Defaulting the\n * other way would make every event emitted before this member existed a\n * cross-tenant delivery, and it fails silently in the direction that matters.\n */\n organizationId?: string;\n}\n\nconst now = (): string => new Date().toISOString();\n\n/** The register's name for a transaction message: `transaction.completed`. */\nexport const transactionEventName = (step: TransactionStep): string =>\n `transaction.${step}`;\n\n/**\n * The message id for one step of one transaction — and therefore the\n * deduplication key.\n *\n * It is the transaction id **and** the step, never the transaction id alone.\n * One transaction emits up to three messages, so a consumer keying on the\n * transaction id would treat `completed` as a redelivery of `accepted` and drop\n * the outcome. The outbox's unique index is on exactly this value, which is what\n * keeps the idempotency claim and the dedup key the same fact rather than two\n * that can drift.\n */\nexport const transactionEventId = (\n transactionId: string,\n step: TransactionStep,\n): string => `${transactionId}:${step}`;\n\n/** Each step's version of its transaction: `accepted` first, then its end. */\nconst STEP_VERSION: Record<TransactionStep, number> = {\n accepted: 0,\n completed: 1,\n failed: 1,\n};\n\n/**\n * Wraps a payload as a routable message from `source`. The transaction is the\n * aggregate, so the outbox keeps `accepted` ahead of the step that ends it.\n */\nconst message = (\n source: string,\n data: TransactionEvent,\n): OutboxEvent<TransactionEvent> => ({\n name: transactionEventName(data.step),\n id: transactionEventId(data.transactionId, data.step),\n source,\n at: data.at,\n correlationId: data.transactionId,\n aggregate: { type: 'transaction', id: data.transactionId },\n version: STEP_VERSION[data.step],\n data,\n});\n\nexport const acceptedEvent = (\n command: TransactionCommand,\n source: string,\n): OutboxEvent<TransactionEvent> =>\n message(source, {\n transactionId: command.transactionId,\n entity: command.entity,\n state: 'PENDING',\n step: 'accepted',\n at: now(),\n organizationId: command.organizationId,\n });\n\nexport const completedEvent = (\n command: TransactionCommand,\n outcome: TransactionOutcome,\n source: string,\n): OutboxEvent<TransactionEvent> =>\n message(source, {\n transactionId: command.transactionId,\n entity: command.entity,\n state: 'COMPLETED',\n step: 'completed',\n code: outcome.code,\n entityId: outcome.entityId,\n at: now(),\n organizationId: command.organizationId,\n });\n\nexport const failedEvent = (\n command: TransactionCommand,\n error: unknown,\n source: string,\n): OutboxEvent<TransactionEvent> =>\n message(source, {\n transactionId: command.transactionId,\n entity: command.entity,\n state: 'FAILED',\n step: 'failed',\n error: error instanceof Error ? error.message : String(error),\n at: now(),\n organizationId: command.organizationId,\n });\n\nexport type TransactionEventEnvelope = EntifixEnvelope<TransactionEvent>;\n"],"names":["TRANSACTION_STATES","now","Date","toISOString","transactionEventName","step","transactionEventId","transactionId","STEP_VERSION","accepted","completed","failed","message","source","data","name","id","at","correlationId","aggregate","type","version","acceptedEvent","command","entity","state","organizationId","completedEvent","outcome","code","entityId","failedEvent","error","Error","String"],"mappings":"AAUA;;;;;;;;;;;CAWC,GACD,OAAO,MAAMA,qBAAqB;IAChC;IACA;IACA;IACA;CACD,CAAU;AAsDX,MAAMC,MAAM,IAAc,IAAIC,OAAOC,WAAW;AAEhD,4EAA4E,GAC5E,OAAO,MAAMC,uBAAuB,CAACC,OACnC,CAAC,YAAY,EAAEA,MAAM,CAAC;AAExB;;;;;;;;;;CAUC,GACD,OAAO,MAAMC,qBAAqB,CAChCC,eACAF,OACW,GAAGE,cAAc,CAAC,EAAEF,MAAM,CAAC;AAExC,4EAA4E,GAC5E,MAAMG,eAAgD;IACpDC,UAAU;IACVC,WAAW;IACXC,QAAQ;AACV;AAEA;;;CAGC,GACD,MAAMC,UAAU,CACdC,QACAC,OACmC,CAAA;QACnCC,MAAMX,qBAAqBU,KAAKT,IAAI;QACpCW,IAAIV,mBAAmBQ,KAAKP,aAAa,EAAEO,KAAKT,IAAI;QACpDQ;QACAI,IAAIH,KAAKG,EAAE;QACXC,eAAeJ,KAAKP,aAAa;QACjCY,WAAW;YAAEC,MAAM;YAAeJ,IAAIF,KAAKP,aAAa;QAAC;QACzDc,SAASb,YAAY,CAACM,KAAKT,IAAI,CAAC;QAChCS;IACF,CAAA;AAEA,OAAO,MAAMQ,gBAAgB,CAC3BC,SACAV,SAEAD,QAAQC,QAAQ;QACdN,eAAegB,QAAQhB,aAAa;QACpCiB,QAAQD,QAAQC,MAAM;QACtBC,OAAO;QACPpB,MAAM;QACNY,IAAIhB;QACJyB,gBAAgBH,QAAQG,cAAc;IACxC,GAAG;AAEL,OAAO,MAAMC,iBAAiB,CAC5BJ,SACAK,SACAf,SAEAD,QAAQC,QAAQ;QACdN,eAAegB,QAAQhB,aAAa;QACpCiB,QAAQD,QAAQC,MAAM;QACtBC,OAAO;QACPpB,MAAM;QACNwB,MAAMD,QAAQC,IAAI;QAClBC,UAAUF,QAAQE,QAAQ;QAC1Bb,IAAIhB;QACJyB,gBAAgBH,QAAQG,cAAc;IACxC,GAAG;AAEL,OAAO,MAAMK,cAAc,CACzBR,SACAS,OACAnB,SAEAD,QAAQC,QAAQ;QACdN,eAAegB,QAAQhB,aAAa;QACpCiB,QAAQD,QAAQC,MAAM;QACtBC,OAAO;QACPpB,MAAM;QACN2B,OAAOA,iBAAiBC,QAAQD,MAAMpB,OAAO,GAAGsB,OAAOF;QACvDf,IAAIhB;QACJyB,gBAAgBH,QAAQG,cAAc;IACxC,GAAG"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Effect } from 'effect';
|
|
2
|
+
/**
|
|
3
|
+
* A credential that lets whoever holds it watch one flow, with no session: the
|
|
4
|
+
* anonymous buyer behind a storefront checkout. The nonce goes to the holder;
|
|
5
|
+
* only its SHA-256 digest is stored and put in the flow's audience, so the
|
|
6
|
+
* record is the authority and the credential is inert anywhere else.
|
|
7
|
+
*/
|
|
8
|
+
export interface FlowCapability {
|
|
9
|
+
readonly nonce: string;
|
|
10
|
+
readonly digest: string;
|
|
11
|
+
}
|
|
12
|
+
/** The SHA-256 digest of a nonce, as hex. Web Crypto, so it runs in a browser too. */
|
|
13
|
+
export declare const digestFlowCapability: (nonce: string) => Effect.Effect<string>;
|
|
14
|
+
/** Mints a fresh capability: 32 random bytes, and their digest. */
|
|
15
|
+
export declare const makeFlowCapability: () => Effect.Effect<FlowCapability>;
|
|
16
|
+
/**
|
|
17
|
+
* Whether `nonce` is the capability whose digest is `digest`. An empty nonce is
|
|
18
|
+
* refused outright; otherwise the comparison is constant-time, so the answer's
|
|
19
|
+
* timing says nothing about how close a guess was.
|
|
20
|
+
*/
|
|
21
|
+
export declare const verifyFlowCapability: (nonce: string, digest: string) => Effect.Effect<boolean>;
|
|
22
|
+
//# sourceMappingURL=flow-capability.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flow-capability.d.ts","sourceRoot":"","sources":["../../src/contracts/flow-capability.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC;;;;;GAKG;AACH,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAKD,sFAAsF;AACtF,eAAO,MAAM,oBAAoB,GAAI,OAAO,MAAM,KAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAGd,CAAC;AAE5D,mEAAmE;AACnE,eAAO,MAAM,kBAAkB,QAAO,MAAM,CAAC,MAAM,CAAC,cAAc,CAO9D,CAAC;AAcL;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,GAC/B,OAAO,MAAM,EACb,QAAQ,MAAM,KACb,MAAM,CAAC,MAAM,CAAC,OAAO,CAKjB,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Effect } from 'effect';
|
|
2
|
+
const hex = (bytes)=>Array.from(bytes, (byte)=>byte.toString(16).padStart(2, '0')).join('');
|
|
3
|
+
/** The SHA-256 digest of a nonce, as hex. Web Crypto, so it runs in a browser too. */ export const digestFlowCapability = (nonce)=>Effect.promise(()=>globalThis.crypto.subtle.digest('SHA-256', new TextEncoder().encode(nonce))).pipe(Effect.map((buffer)=>hex(new Uint8Array(buffer))));
|
|
4
|
+
/** Mints a fresh capability: 32 random bytes, and their digest. */ export const makeFlowCapability = ()=>Effect.suspend(()=>{
|
|
5
|
+
const nonce = hex(globalThis.crypto.getRandomValues(new Uint8Array(32)));
|
|
6
|
+
return Effect.map(digestFlowCapability(nonce), (digest)=>({
|
|
7
|
+
nonce,
|
|
8
|
+
digest
|
|
9
|
+
}));
|
|
10
|
+
});
|
|
11
|
+
/**
|
|
12
|
+
* Compares two digests in time independent of where they first differ. A
|
|
13
|
+
* length mismatch answers at once: a digest's length is no secret.
|
|
14
|
+
*/ const constantTimeEqual = (a, b)=>{
|
|
15
|
+
if (a.length !== b.length) return false;
|
|
16
|
+
let difference = 0;
|
|
17
|
+
for(let index = 0; index < a.length; index += 1)difference |= a.charCodeAt(index) ^ b.charCodeAt(index);
|
|
18
|
+
return difference === 0;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Whether `nonce` is the capability whose digest is `digest`. An empty nonce is
|
|
22
|
+
* refused outright; otherwise the comparison is constant-time, so the answer's
|
|
23
|
+
* timing says nothing about how close a guess was.
|
|
24
|
+
*/ export const verifyFlowCapability = (nonce, digest)=>nonce === '' ? Effect.succeed(false) : Effect.map(digestFlowCapability(nonce), (computed)=>constantTimeEqual(computed, digest));
|
|
25
|
+
|
|
26
|
+
//# sourceMappingURL=flow-capability.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/contracts/flow-capability.ts"],"sourcesContent":["import { Effect } from 'effect';\n\n/**\n * A credential that lets whoever holds it watch one flow, with no session: the\n * anonymous buyer behind a storefront checkout. The nonce goes to the holder;\n * only its SHA-256 digest is stored and put in the flow's audience, so the\n * record is the authority and the credential is inert anywhere else.\n */\nexport interface FlowCapability {\n readonly nonce: string;\n readonly digest: string;\n}\n\nconst hex = (bytes: Uint8Array): string =>\n Array.from(bytes, byte => byte.toString(16).padStart(2, '0')).join('');\n\n/** The SHA-256 digest of a nonce, as hex. Web Crypto, so it runs in a browser too. */\nexport const digestFlowCapability = (nonce: string): Effect.Effect<string> =>\n Effect.promise(() =>\n globalThis.crypto.subtle.digest('SHA-256', new TextEncoder().encode(nonce)),\n ).pipe(Effect.map(buffer => hex(new Uint8Array(buffer))));\n\n/** Mints a fresh capability: 32 random bytes, and their digest. */\nexport const makeFlowCapability = (): Effect.Effect<FlowCapability> =>\n Effect.suspend(() => {\n const nonce = hex(globalThis.crypto.getRandomValues(new Uint8Array(32)));\n return Effect.map(digestFlowCapability(nonce), digest => ({\n nonce,\n digest,\n }));\n });\n\n/**\n * Compares two digests in time independent of where they first differ. A\n * length mismatch answers at once: a digest's length is no secret.\n */\nconst constantTimeEqual = (a: string, b: string): boolean => {\n if (a.length !== b.length) return false;\n let difference = 0;\n for (let index = 0; index < a.length; index += 1)\n difference |= a.charCodeAt(index) ^ b.charCodeAt(index);\n return difference === 0;\n};\n\n/**\n * Whether `nonce` is the capability whose digest is `digest`. An empty nonce is\n * refused outright; otherwise the comparison is constant-time, so the answer's\n * timing says nothing about how close a guess was.\n */\nexport const verifyFlowCapability = (\n nonce: string,\n digest: string,\n): Effect.Effect<boolean> =>\n nonce === ''\n ? Effect.succeed(false)\n : Effect.map(digestFlowCapability(nonce), computed =>\n constantTimeEqual(computed, digest),\n );\n"],"names":["Effect","hex","bytes","Array","from","byte","toString","padStart","join","digestFlowCapability","nonce","promise","globalThis","crypto","subtle","digest","TextEncoder","encode","pipe","map","buffer","Uint8Array","makeFlowCapability","suspend","getRandomValues","constantTimeEqual","a","b","length","difference","index","charCodeAt","verifyFlowCapability","succeed","computed"],"mappings":"AAAA,SAASA,MAAM,QAAQ,SAAS;AAahC,MAAMC,MAAM,CAACC,QACXC,MAAMC,IAAI,CAACF,OAAOG,CAAAA,OAAQA,KAAKC,QAAQ,CAAC,IAAIC,QAAQ,CAAC,GAAG,MAAMC,IAAI,CAAC;AAErE,oFAAoF,GACpF,OAAO,MAAMC,uBAAuB,CAACC,QACnCV,OAAOW,OAAO,CAAC,IACbC,WAAWC,MAAM,CAACC,MAAM,CAACC,MAAM,CAAC,WAAW,IAAIC,cAAcC,MAAM,CAACP,SACpEQ,IAAI,CAAClB,OAAOmB,GAAG,CAACC,CAAAA,SAAUnB,IAAI,IAAIoB,WAAWD,WAAW;AAE5D,iEAAiE,GACjE,OAAO,MAAME,qBAAqB,IAChCtB,OAAOuB,OAAO,CAAC;QACb,MAAMb,QAAQT,IAAIW,WAAWC,MAAM,CAACW,eAAe,CAAC,IAAIH,WAAW;QACnE,OAAOrB,OAAOmB,GAAG,CAACV,qBAAqBC,QAAQK,CAAAA,SAAW,CAAA;gBACxDL;gBACAK;YACF,CAAA;IACF,GAAG;AAEL;;;CAGC,GACD,MAAMU,oBAAoB,CAACC,GAAWC;IACpC,IAAID,EAAEE,MAAM,KAAKD,EAAEC,MAAM,EAAE,OAAO;IAClC,IAAIC,aAAa;IACjB,IAAK,IAAIC,QAAQ,GAAGA,QAAQJ,EAAEE,MAAM,EAAEE,SAAS,EAC7CD,cAAcH,EAAEK,UAAU,CAACD,SAASH,EAAEI,UAAU,CAACD;IACnD,OAAOD,eAAe;AACxB;AAEA;;;;CAIC,GACD,OAAO,MAAMG,uBAAuB,CAClCtB,OACAK,SAEAL,UAAU,KACNV,OAAOiC,OAAO,CAAC,SACfjC,OAAOmB,GAAG,CAACV,qBAAqBC,QAAQwB,CAAAA,WACtCT,kBAAkBS,UAAUnB,SAC5B"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { type AudienceEntry } from '@entifix/core';
|
|
2
|
+
import { Schema } from 'effect';
|
|
3
|
+
import type { OutboxEvent } from './outbox.js';
|
|
4
|
+
/** The four things a flow announces about itself. */
|
|
5
|
+
export declare const FLOW_EVENTS: readonly ["flow.started", "flow.completed", "flow.failed", "flow.suspended"];
|
|
6
|
+
export type FlowEventName = (typeof FLOW_EVENTS)[number];
|
|
7
|
+
/**
|
|
8
|
+
* What a `flow.*` event carries. A `flow.failed` without a failure, or with a
|
|
9
|
+
* failure missing its `code` or `category`, does not decode: a client settling
|
|
10
|
+
* a write must be able to translate why it failed.
|
|
11
|
+
*/
|
|
12
|
+
export declare const FlowEventData: Schema.Struct<{
|
|
13
|
+
readonly flowId: Schema.String;
|
|
14
|
+
readonly definition: Schema.String;
|
|
15
|
+
readonly slipVersion: Schema.Number;
|
|
16
|
+
readonly step: Schema.optionalKey<Schema.String>;
|
|
17
|
+
readonly outcome: Schema.optionalKey<Schema.Unknown>;
|
|
18
|
+
readonly failure: Schema.optionalKey<Schema.StructWithRest<Schema.Struct<{
|
|
19
|
+
code: Schema.NonEmptyString;
|
|
20
|
+
category: Schema.NonEmptyString;
|
|
21
|
+
}>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>>;
|
|
22
|
+
}>;
|
|
23
|
+
export type FlowEventData = typeof FlowEventData.Type;
|
|
24
|
+
/**
|
|
25
|
+
* The `202` body a flow start answers: the flow's id, and where to watch it —
|
|
26
|
+
* its status read and its stream.
|
|
27
|
+
*/
|
|
28
|
+
export declare const FlowAccepted: Schema.Struct<{
|
|
29
|
+
readonly flowId: Schema.String;
|
|
30
|
+
readonly links: Schema.$Array<Schema.Struct<{
|
|
31
|
+
readonly rel: Schema.Literals<readonly ["status", "stream"]>;
|
|
32
|
+
readonly href: Schema.String;
|
|
33
|
+
}>>;
|
|
34
|
+
}>;
|
|
35
|
+
export type FlowAccepted = typeof FlowAccepted.Type;
|
|
36
|
+
export interface FlowEventInput {
|
|
37
|
+
readonly name: FlowEventName;
|
|
38
|
+
readonly source: string;
|
|
39
|
+
readonly at: string;
|
|
40
|
+
/** The step this event is about, and its ordinal — the event's version. */
|
|
41
|
+
readonly step: string;
|
|
42
|
+
readonly ordinal: number;
|
|
43
|
+
readonly audience: readonly AudienceEntry[];
|
|
44
|
+
readonly data: FlowEventData;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Builds a `flow.*` event: id `<flowId>:<step>` so a redelivered announce is
|
|
48
|
+
* the same message, `correlationId` the flow, the step ordinal as version, and
|
|
49
|
+
* an audience that must not be empty.
|
|
50
|
+
*/
|
|
51
|
+
export declare const flowEvent: (input: FlowEventInput) => OutboxEvent<FlowEventData>;
|
|
52
|
+
//# sourceMappingURL=flow-messages.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flow-messages.d.ts","sourceRoot":"","sources":["../../src/contracts/flow-messages.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,EAInB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C,qDAAqD;AACrD,eAAO,MAAM,WAAW,8EAKd,CAAC;AACX,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,CAAC,CAAC;AAQzD;;;;GAIG;AACH,eAAO,MAAM,aAAa;;;;;;;;;;EAOxB,CAAC;AACH,MAAM,MAAM,aAAa,GAAG,OAAO,aAAa,CAAC,IAAI,CAAC;AAYtD;;;GAGG;AACH,eAAO,MAAM,YAAY;;;;;;EAQvB,CAAC;AACH,MAAM,MAAM,YAAY,GAAG,OAAO,YAAY,CAAC,IAAI,CAAC;AAIpD,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;IAC7B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,2EAA2E;IAC3E,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,EAAE,SAAS,aAAa,EAAE,CAAC;IAC5C,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;CAC9B;AAED;;;;GAIG;AACH,eAAO,MAAM,SAAS,GACpB,OAAO,cAAc,KACpB,WAAW,CAAC,aAAa,CAU1B,CAAC"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
2
|
+
import { openStruct, registerEvent, registerMessage } from '@entifix/core';
|
|
3
|
+
import { Schema } from 'effect';
|
|
4
|
+
/** The four things a flow announces about itself. */ export const FLOW_EVENTS = [
|
|
5
|
+
'flow.started',
|
|
6
|
+
'flow.completed',
|
|
7
|
+
'flow.failed',
|
|
8
|
+
'flow.suspended'
|
|
9
|
+
];
|
|
10
|
+
/** A failure inside an asynchronous message: the problem's code and category. */ const FlowFailure = openStruct({
|
|
11
|
+
code: Schema.NonEmptyString,
|
|
12
|
+
category: Schema.NonEmptyString
|
|
13
|
+
});
|
|
14
|
+
/**
|
|
15
|
+
* What a `flow.*` event carries. A `flow.failed` without a failure, or with a
|
|
16
|
+
* failure missing its `code` or `category`, does not decode: a client settling
|
|
17
|
+
* a write must be able to translate why it failed.
|
|
18
|
+
*/ export const FlowEventData = Schema.Struct({
|
|
19
|
+
flowId: Schema.String,
|
|
20
|
+
definition: Schema.String,
|
|
21
|
+
slipVersion: Schema.Number,
|
|
22
|
+
step: Schema.optionalKey(Schema.String),
|
|
23
|
+
outcome: Schema.optionalKey(Schema.Unknown),
|
|
24
|
+
failure: Schema.optionalKey(FlowFailure)
|
|
25
|
+
});
|
|
26
|
+
const FlowFailedData = Schema.Struct(_extends({}, FlowEventData.fields, {
|
|
27
|
+
failure: FlowFailure
|
|
28
|
+
}));
|
|
29
|
+
for (const name of FLOW_EVENTS)registerEvent(name, name === 'flow.failed' ? FlowFailedData : FlowEventData, {
|
|
30
|
+
versioned: true
|
|
31
|
+
});
|
|
32
|
+
/**
|
|
33
|
+
* The `202` body a flow start answers: the flow's id, and where to watch it —
|
|
34
|
+
* its status read and its stream.
|
|
35
|
+
*/ export const FlowAccepted = Schema.Struct({
|
|
36
|
+
flowId: Schema.String,
|
|
37
|
+
links: Schema.Array(Schema.Struct({
|
|
38
|
+
rel: Schema.Literals([
|
|
39
|
+
'status',
|
|
40
|
+
'stream'
|
|
41
|
+
]),
|
|
42
|
+
href: Schema.String
|
|
43
|
+
}))
|
|
44
|
+
});
|
|
45
|
+
registerMessage('flowAccepted', FlowAccepted);
|
|
46
|
+
/**
|
|
47
|
+
* Builds a `flow.*` event: id `<flowId>:<step>` so a redelivered announce is
|
|
48
|
+
* the same message, `correlationId` the flow, the step ordinal as version, and
|
|
49
|
+
* an audience that must not be empty.
|
|
50
|
+
*/ export const flowEvent = (input)=>({
|
|
51
|
+
name: input.name,
|
|
52
|
+
id: `${input.data.flowId}:${input.step}`,
|
|
53
|
+
source: input.source,
|
|
54
|
+
at: input.at,
|
|
55
|
+
correlationId: input.data.flowId,
|
|
56
|
+
aggregate: {
|
|
57
|
+
type: 'flow',
|
|
58
|
+
id: input.data.flowId
|
|
59
|
+
},
|
|
60
|
+
version: input.ordinal,
|
|
61
|
+
audience: input.audience,
|
|
62
|
+
data: input.data
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
//# sourceMappingURL=flow-messages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/contracts/flow-messages.ts"],"sourcesContent":["import {\n type AudienceEntry,\n openStruct,\n registerEvent,\n registerMessage,\n} from '@entifix/core';\nimport { Schema } from 'effect';\n\nimport type { OutboxEvent } from './outbox.js';\n\n/** The four things a flow announces about itself. */\nexport const FLOW_EVENTS = [\n 'flow.started',\n 'flow.completed',\n 'flow.failed',\n 'flow.suspended',\n] as const;\nexport type FlowEventName = (typeof FLOW_EVENTS)[number];\n\n/** A failure inside an asynchronous message: the problem's code and category. */\nconst FlowFailure = openStruct({\n code: Schema.NonEmptyString,\n category: Schema.NonEmptyString,\n});\n\n/**\n * What a `flow.*` event carries. A `flow.failed` without a failure, or with a\n * failure missing its `code` or `category`, does not decode: a client settling\n * a write must be able to translate why it failed.\n */\nexport const FlowEventData = Schema.Struct({\n flowId: Schema.String,\n definition: Schema.String,\n slipVersion: Schema.Number,\n step: Schema.optionalKey(Schema.String),\n outcome: Schema.optionalKey(Schema.Unknown),\n failure: Schema.optionalKey(FlowFailure),\n});\nexport type FlowEventData = typeof FlowEventData.Type;\n\nconst FlowFailedData = Schema.Struct({\n ...FlowEventData.fields,\n failure: FlowFailure,\n});\n\nfor (const name of FLOW_EVENTS)\n registerEvent(name, name === 'flow.failed' ? FlowFailedData : FlowEventData, {\n versioned: true,\n });\n\n/**\n * The `202` body a flow start answers: the flow's id, and where to watch it —\n * its status read and its stream.\n */\nexport const FlowAccepted = Schema.Struct({\n flowId: Schema.String,\n links: Schema.Array(\n Schema.Struct({\n rel: Schema.Literals(['status', 'stream']),\n href: Schema.String,\n }),\n ),\n});\nexport type FlowAccepted = typeof FlowAccepted.Type;\n\nregisterMessage('flowAccepted', FlowAccepted);\n\nexport interface FlowEventInput {\n readonly name: FlowEventName;\n readonly source: string;\n readonly at: string;\n /** The step this event is about, and its ordinal — the event's version. */\n readonly step: string;\n readonly ordinal: number;\n readonly audience: readonly AudienceEntry[];\n readonly data: FlowEventData;\n}\n\n/**\n * Builds a `flow.*` event: id `<flowId>:<step>` so a redelivered announce is\n * the same message, `correlationId` the flow, the step ordinal as version, and\n * an audience that must not be empty.\n */\nexport const flowEvent = (\n input: FlowEventInput,\n): OutboxEvent<FlowEventData> => ({\n name: input.name,\n id: `${input.data.flowId}:${input.step}`,\n source: input.source,\n at: input.at,\n correlationId: input.data.flowId,\n aggregate: { type: 'flow', id: input.data.flowId },\n version: input.ordinal,\n audience: input.audience,\n data: input.data,\n});\n"],"names":["openStruct","registerEvent","registerMessage","Schema","FLOW_EVENTS","FlowFailure","code","NonEmptyString","category","FlowEventData","Struct","flowId","String","definition","slipVersion","Number","step","optionalKey","outcome","Unknown","failure","FlowFailedData","fields","name","versioned","FlowAccepted","links","Array","rel","Literals","href","flowEvent","input","id","data","source","at","correlationId","aggregate","type","version","ordinal","audience"],"mappings":";AAAA,SAEEA,UAAU,EACVC,aAAa,EACbC,eAAe,QACV,gBAAgB;AACvB,SAASC,MAAM,QAAQ,SAAS;AAIhC,mDAAmD,GACnD,OAAO,MAAMC,cAAc;IACzB;IACA;IACA;IACA;CACD,CAAU;AAGX,+EAA+E,GAC/E,MAAMC,cAAcL,WAAW;IAC7BM,MAAMH,OAAOI,cAAc;IAC3BC,UAAUL,OAAOI,cAAc;AACjC;AAEA;;;;CAIC,GACD,OAAO,MAAME,gBAAgBN,OAAOO,MAAM,CAAC;IACzCC,QAAQR,OAAOS,MAAM;IACrBC,YAAYV,OAAOS,MAAM;IACzBE,aAAaX,OAAOY,MAAM;IAC1BC,MAAMb,OAAOc,WAAW,CAACd,OAAOS,MAAM;IACtCM,SAASf,OAAOc,WAAW,CAACd,OAAOgB,OAAO;IAC1CC,SAASjB,OAAOc,WAAW,CAACZ;AAC9B,GAAG;AAGH,MAAMgB,iBAAiBlB,OAAOO,MAAM,CAAC,aAChCD,cAAca,MAAM;IACvBF,SAASf;;AAGX,KAAK,MAAMkB,QAAQnB,YACjBH,cAAcsB,MAAMA,SAAS,gBAAgBF,iBAAiBZ,eAAe;IAC3Ee,WAAW;AACb;AAEF;;;CAGC,GACD,OAAO,MAAMC,eAAetB,OAAOO,MAAM,CAAC;IACxCC,QAAQR,OAAOS,MAAM;IACrBc,OAAOvB,OAAOwB,KAAK,CACjBxB,OAAOO,MAAM,CAAC;QACZkB,KAAKzB,OAAO0B,QAAQ,CAAC;YAAC;YAAU;SAAS;QACzCC,MAAM3B,OAAOS,MAAM;IACrB;AAEJ,GAAG;AAGHV,gBAAgB,gBAAgBuB;AAahC;;;;CAIC,GACD,OAAO,MAAMM,YAAY,CACvBC,QACgC,CAAA;QAChCT,MAAMS,MAAMT,IAAI;QAChBU,IAAI,GAAGD,MAAME,IAAI,CAACvB,MAAM,CAAC,CAAC,EAAEqB,MAAMhB,IAAI,EAAE;QACxCmB,QAAQH,MAAMG,MAAM;QACpBC,IAAIJ,MAAMI,EAAE;QACZC,eAAeL,MAAME,IAAI,CAACvB,MAAM;QAChC2B,WAAW;YAAEC,MAAM;YAAQN,IAAID,MAAME,IAAI,CAACvB,MAAM;QAAC;QACjD6B,SAASR,MAAMS,OAAO;QACtBC,UAAUV,MAAMU,QAAQ;QACxBR,MAAMF,MAAME,IAAI;IAClB,CAAA,EAAG"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { StoreFailure } from '@entifix/core';
|
|
2
|
-
import { Context,
|
|
2
|
+
import { Context, Effect } from 'effect';
|
|
3
3
|
/**
|
|
4
4
|
* A consumer's claim on one delivered message.
|
|
5
5
|
*
|
|
@@ -53,7 +53,7 @@ export type InboxClaimResult = 'claimed' | 'duplicate';
|
|
|
53
53
|
* twice.
|
|
54
54
|
*
|
|
55
55
|
* There is deliberately **no `claimWithin(session)` here**, for the reason
|
|
56
|
-
* {@link
|
|
56
|
+
* {@link Outbox} states about `enqueueWithin`: a driver session may
|
|
57
57
|
* not enter a framework-free contract. A claim that must commit with its side
|
|
58
58
|
* effect is a {@link UnitOfWork}'s `event` claim (ADR 0009); this port is what
|
|
59
59
|
* a consumer that can claim standalone uses instead.
|
|
@@ -75,5 +75,22 @@ export interface TransactionInbox {
|
|
|
75
75
|
declare const TransactionInboxTag_base: Context.ServiceClass<TransactionInboxTag, "TransactionInboxTag", TransactionInbox>;
|
|
76
76
|
export declare class TransactionInboxTag extends TransactionInboxTag_base {
|
|
77
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Whether a consumer should discard an event because it already applied this
|
|
80
|
+
* version of its aggregate, or a later one
|
|
81
|
+
* ([ADR 0018](../../../../../docs/adr/0018-one-outbox-ordered-per-aggregate.md)).
|
|
82
|
+
*
|
|
83
|
+
* The bus redelivers and reorders; with a version per aggregate a late
|
|
84
|
+
* `v = n - 1` never overwrites `v = n`, and a delete is ordered like any other
|
|
85
|
+
* write. `applied` is what the consumer stored the last time it applied this
|
|
86
|
+
* aggregate — absent when it never has.
|
|
87
|
+
*/
|
|
88
|
+
export declare const isStale: (applied: number | undefined, version: number) => boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Applies `apply` unless the event is stale, reading what the consumer last
|
|
91
|
+
* applied for its aggregate with `applied`. Answers `'stale'` for a discarded
|
|
92
|
+
* event, so the caller can count it rather than wonder.
|
|
93
|
+
*/
|
|
94
|
+
export declare const discardIfStale: <A, E, R, E2, R2>(applied: Effect.Effect<number | undefined, E, R>, version: number, apply: Effect.Effect<A, E2, R2>) => Effect.Effect<A | "stale", E | E2, R | R2>;
|
|
78
95
|
export {};
|
|
79
96
|
//# sourceMappingURL=inbox.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inbox.d.ts","sourceRoot":"","sources":["../../src/contracts/inbox.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"inbox.d.ts","sourceRoot":"","sources":["../../src/contracts/inbox.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEzC;;;;;;;GAOG;AACH,MAAM,WAAW,UAAU;IACzB;;;;;;;;;;OAUG;IACH,QAAQ,EAAE,MAAM,CAAC;IACjB;;;;;;;OAOG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB,qEAAqE;IACrE,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;;;;;;;GAQG;AACH,MAAM,MAAM,gBAAgB,GAAG,SAAS,GAAG,WAAW,CAAC;AAEvD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;;;OAKG;IACH,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,gBAAgB,EAAE,YAAY,CAAC,CAAC;CACvE;;AAED,qBAAa,mBAAoB,SAAQ,wBAGf;CAAG;AAE7B;;;;;;;;;GASG;AACH,eAAO,MAAM,OAAO,GAClB,SAAS,MAAM,GAAG,SAAS,EAC3B,SAAS,MAAM,KACd,OAAsD,CAAC;AAE1D;;;;GAIG;AACH,eAAO,MAAM,cAAc,GAAI,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAC5C,SAAS,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,SAAS,EAAE,CAAC,EAAE,CAAC,CAAC,EAChD,SAAS,MAAM,EACf,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,CAAC,KAC9B,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,EAAE,CAGzC,CAAC"}
|
package/dist/contracts/inbox.js
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
|
-
import { Context } from 'effect';
|
|
1
|
+
import { Context, Effect } from 'effect';
|
|
2
2
|
export class TransactionInboxTag extends Context.Service()('TransactionInboxTag') {
|
|
3
3
|
}
|
|
4
|
+
/**
|
|
5
|
+
* Whether a consumer should discard an event because it already applied this
|
|
6
|
+
* version of its aggregate, or a later one
|
|
7
|
+
* ([ADR 0018](../../../../../docs/adr/0018-one-outbox-ordered-per-aggregate.md)).
|
|
8
|
+
*
|
|
9
|
+
* The bus redelivers and reorders; with a version per aggregate a late
|
|
10
|
+
* `v = n - 1` never overwrites `v = n`, and a delete is ordered like any other
|
|
11
|
+
* write. `applied` is what the consumer stored the last time it applied this
|
|
12
|
+
* aggregate — absent when it never has.
|
|
13
|
+
*/ export const isStale = (applied, version)=>applied !== undefined && version <= applied;
|
|
14
|
+
/**
|
|
15
|
+
* Applies `apply` unless the event is stale, reading what the consumer last
|
|
16
|
+
* applied for its aggregate with `applied`. Answers `'stale'` for a discarded
|
|
17
|
+
* event, so the caller can count it rather than wonder.
|
|
18
|
+
*/ export const discardIfStale = (applied, version, apply)=>Effect.flatMap(applied, (last)=>isStale(last, version) ? Effect.succeed('stale') : apply);
|
|
4
19
|
|
|
5
20
|
//# sourceMappingURL=inbox.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/contracts/inbox.ts"],"sourcesContent":["import type { StoreFailure } from '@entifix/core';\nimport { Context,
|
|
1
|
+
{"version":3,"sources":["../../src/contracts/inbox.ts"],"sourcesContent":["import type { StoreFailure } from '@entifix/core';\nimport { Context, Effect } from 'effect';\n\n/**\n * A consumer's claim on one delivered message.\n *\n * The mirror of an {@link OutboxEntry}: the outbox makes a publisher's event\n * survive a broker outage, the inbox makes a consumer's side effect survive a\n * redelivery. Each end owns one unique index, and the value in it is the same\n * `event.id`.\n */\nexport interface InboxClaim {\n /**\n * Which consumer claimed it — the subscription's durable work-queue name\n * (`<slice>.<pattern>`).\n *\n * **Half of the key, and the half that is easy to leave out.** Two consumers\n * legitimately process the same event: the saga tracker's fold and the SSE\n * hub both bind `transaction.*` today. Keyed on `eventId` alone, whichever\n * claimed first would starve the other of every message — a consumer that\n * silently stops working while the broker, the queue and the handler all\n * report success.\n */\n consumer: string;\n /**\n * The message's own id, which for a transaction step is\n * `<transactionId>:<step>`.\n *\n * Never `correlationId`: one transaction emits up to three messages, so\n * keying on the correlation id would make `completed` look like a\n * redelivery of `accepted`.\n */\n eventId: string;\n /** ISO-8601, for retention — nothing reads it to make a decision. */\n claimedAt: string;\n}\n\n/**\n * Whether {@link TransactionInbox.claim} actually wrote.\n *\n * `duplicate` is not a failure — it is how a **redelivery** is recognised, the\n * same way `OutboxEnqueueResult`'s `duplicate` recognises a replayed command.\n * Delivery is at-least-once by construction (a crash between `publish` and\n * `markSent` re-sends), so a consumer seeing this should skip its side effect\n * and ack, not error.\n */\nexport type InboxClaimResult = 'claimed' | 'duplicate';\n\n/**\n * The consumer-side half of exactly-once processing over an at-least-once bus.\n *\n * A claim is only worth anything if it is written **in the same storage\n * transaction as the side effect it guards**. Claim-then-act leaves a crash\n * window in which the claim is taken and the effect never ran, so the\n * redelivery is skipped and the work is lost — strictly worse than doing it\n * twice.\n *\n * There is deliberately **no `claimWithin(session)` here**, for the reason\n * {@link Outbox} states about `enqueueWithin`: a driver session may\n * not enter a framework-free contract. A claim that must commit with its side\n * effect is a {@link UnitOfWork}'s `event` claim (ADR 0009); this port is what\n * a consumer that can claim standalone uses instead.\n *\n * It follows that **the inbox lives with the side effect, not in one central\n * place**: the claim and the write must be in the same database to be one fact.\n * The outbox is per-tenant because the entity is; the saga tracker's inbox is in\n * the `saga` database because that is where its fold writes.\n */\nexport interface TransactionInbox {\n /**\n * Claim an event for this consumer. Idempotent per `(consumer, eventId)`.\n *\n * Answering `duplicate` means some earlier delivery already ran the side\n * effect to completion, because the claim and the effect commit together.\n */\n claim(eventId: string): Effect.Effect<InboxClaimResult, StoreFailure>;\n}\n\nexport class TransactionInboxTag extends Context.Service<\n TransactionInboxTag,\n TransactionInbox\n>()('TransactionInboxTag') {}\n\n/**\n * Whether a consumer should discard an event because it already applied this\n * version of its aggregate, or a later one\n * ([ADR 0018](../../../../../docs/adr/0018-one-outbox-ordered-per-aggregate.md)).\n *\n * The bus redelivers and reorders; with a version per aggregate a late\n * `v = n - 1` never overwrites `v = n`, and a delete is ordered like any other\n * write. `applied` is what the consumer stored the last time it applied this\n * aggregate — absent when it never has.\n */\nexport const isStale = (\n applied: number | undefined,\n version: number,\n): boolean => applied !== undefined && version <= applied;\n\n/**\n * Applies `apply` unless the event is stale, reading what the consumer last\n * applied for its aggregate with `applied`. Answers `'stale'` for a discarded\n * event, so the caller can count it rather than wonder.\n */\nexport const discardIfStale = <A, E, R, E2, R2>(\n applied: Effect.Effect<number | undefined, E, R>,\n version: number,\n apply: Effect.Effect<A, E2, R2>,\n): Effect.Effect<A | 'stale', E | E2, R | R2> =>\n Effect.flatMap(applied, (last): Effect.Effect<A | 'stale', E2, R2> =>\n isStale(last, version) ? Effect.succeed('stale' as const) : apply,\n );\n"],"names":["Context","Effect","TransactionInboxTag","Service","isStale","applied","version","undefined","discardIfStale","apply","flatMap","last","succeed"],"mappings":"AACA,SAASA,OAAO,EAAEC,MAAM,QAAQ,SAAS;AA6EzC,OAAO,MAAMC,4BAA4BF,QAAQG,OAAO,GAGpD;AAAwB;AAE5B;;;;;;;;;CASC,GACD,OAAO,MAAMC,UAAU,CACrBC,SACAC,UACYD,YAAYE,aAAaD,WAAWD,QAAQ;AAE1D;;;;CAIC,GACD,OAAO,MAAMG,iBAAiB,CAC5BH,SACAC,SACAG,QAEAR,OAAOS,OAAO,CAACL,SAAS,CAACM,OACvBP,QAAQO,MAAML,WAAWL,OAAOW,OAAO,CAAC,WAAoBH,OAC5D"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Schema } from 'effect';
|
|
2
|
+
export declare const TransactionCommandSchema: Schema.StructWithRest<Schema.Struct<{
|
|
3
|
+
transactionId: Schema.String;
|
|
4
|
+
type: Schema.String;
|
|
5
|
+
entity: Schema.String;
|
|
6
|
+
payload: Schema.Unknown;
|
|
7
|
+
}>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>;
|
|
8
|
+
export declare const TransactionAcceptedSchema: Schema.StructWithRest<Schema.Struct<{
|
|
9
|
+
transactionId: Schema.String;
|
|
10
|
+
state: Schema.Literals<readonly ["PENDING", "COMPLETED", "FAILED", "STALE"]>;
|
|
11
|
+
}>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>;
|
|
12
|
+
export declare const TransactionRecordSchema: Schema.StructWithRest<Schema.Struct<{
|
|
13
|
+
transactionId: Schema.String;
|
|
14
|
+
entity: Schema.String;
|
|
15
|
+
state: Schema.Literals<readonly ["PENDING", "COMPLETED", "FAILED", "STALE"]>;
|
|
16
|
+
createdAt: Schema.String;
|
|
17
|
+
updatedAt: Schema.String;
|
|
18
|
+
}>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>;
|
|
19
|
+
export declare const SagaResultSchema: Schema.StructWithRest<Schema.Struct<{
|
|
20
|
+
state: Schema.String;
|
|
21
|
+
outcomes: Schema.$Array<Schema.Unknown>;
|
|
22
|
+
}>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>;
|
|
23
|
+
export declare const SagaInstanceSchema: Schema.StructWithRest<Schema.Struct<{
|
|
24
|
+
sagaId: Schema.String;
|
|
25
|
+
definition: Schema.String;
|
|
26
|
+
state: Schema.String;
|
|
27
|
+
stepIndex: Schema.Number;
|
|
28
|
+
outcomes: Schema.$Array<Schema.Unknown>;
|
|
29
|
+
}>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>;
|
|
30
|
+
export declare const TransactionEventSchema: Schema.StructWithRest<Schema.Struct<{
|
|
31
|
+
transactionId: Schema.String;
|
|
32
|
+
entity: Schema.String;
|
|
33
|
+
state: Schema.Literals<readonly ["PENDING", "COMPLETED", "FAILED", "STALE"]>;
|
|
34
|
+
step: Schema.Literals<readonly ["accepted", "completed", "failed"]>;
|
|
35
|
+
at: Schema.String;
|
|
36
|
+
}>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>;
|
|
37
|
+
//# sourceMappingURL=messages.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../../src/contracts/messages.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAehC,eAAO,MAAM,wBAAwB;;;;;6DAKnC,CAAC;AAEH,eAAO,MAAM,yBAAyB;;;6DAGpC,CAAC;AAEH,eAAO,MAAM,uBAAuB;;;;;;6DAMlC,CAAC;AAEH,eAAO,MAAM,gBAAgB;;;6DAG3B,CAAC;AAEH,eAAO,MAAM,kBAAkB;;;;;;6DAM7B,CAAC;AAEH,eAAO,MAAM,sBAAsB;;;;;;6DAMjC,CAAC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { openStruct, registerEvent, registerMessage } from '@entifix/core';
|
|
2
|
+
import { Schema } from 'effect';
|
|
3
|
+
import { TRANSACTION_STATES, transactionEventName } from './event.js';
|
|
4
|
+
/**
|
|
5
|
+
* The transaction-shaped messages, registered by the package that owns them —
|
|
6
|
+
* core's registry is open, so these left core's closed union in 0.11.0 and are
|
|
7
|
+
* deleted with the saga runner and the tracker (#127).
|
|
8
|
+
*
|
|
9
|
+
* Each schema names only what a reader of it branches on and keeps the rest,
|
|
10
|
+
* which is enough to refuse a body that is some other message wearing this
|
|
11
|
+
* one's name.
|
|
12
|
+
*/ const State = Schema.Literals(TRANSACTION_STATES);
|
|
13
|
+
export const TransactionCommandSchema = openStruct({
|
|
14
|
+
transactionId: Schema.String,
|
|
15
|
+
type: Schema.String,
|
|
16
|
+
entity: Schema.String,
|
|
17
|
+
payload: Schema.Unknown
|
|
18
|
+
});
|
|
19
|
+
export const TransactionAcceptedSchema = openStruct({
|
|
20
|
+
transactionId: Schema.String,
|
|
21
|
+
state: State
|
|
22
|
+
});
|
|
23
|
+
export const TransactionRecordSchema = openStruct({
|
|
24
|
+
transactionId: Schema.String,
|
|
25
|
+
entity: Schema.String,
|
|
26
|
+
state: State,
|
|
27
|
+
createdAt: Schema.String,
|
|
28
|
+
updatedAt: Schema.String
|
|
29
|
+
});
|
|
30
|
+
export const SagaResultSchema = openStruct({
|
|
31
|
+
state: Schema.String,
|
|
32
|
+
outcomes: Schema.Array(Schema.Unknown)
|
|
33
|
+
});
|
|
34
|
+
export const SagaInstanceSchema = openStruct({
|
|
35
|
+
sagaId: Schema.String,
|
|
36
|
+
definition: Schema.String,
|
|
37
|
+
state: Schema.String,
|
|
38
|
+
stepIndex: Schema.Number,
|
|
39
|
+
outcomes: Schema.Array(Schema.Unknown)
|
|
40
|
+
});
|
|
41
|
+
export const TransactionEventSchema = openStruct({
|
|
42
|
+
transactionId: Schema.String,
|
|
43
|
+
entity: Schema.String,
|
|
44
|
+
state: State,
|
|
45
|
+
step: Schema.Literals([
|
|
46
|
+
'accepted',
|
|
47
|
+
'completed',
|
|
48
|
+
'failed'
|
|
49
|
+
]),
|
|
50
|
+
at: Schema.String
|
|
51
|
+
});
|
|
52
|
+
registerMessage('command', TransactionCommandSchema);
|
|
53
|
+
registerMessage('transactionAccepted', TransactionAcceptedSchema);
|
|
54
|
+
registerMessage('transactionRecord', TransactionRecordSchema);
|
|
55
|
+
registerMessage('sagaResult', SagaResultSchema);
|
|
56
|
+
registerMessage('sagaInstance', SagaInstanceSchema);
|
|
57
|
+
// ⚠️ Registered audience-free: a transaction event is scoped by the
|
|
58
|
+
// `organizationId` in its `data`, which is what the stream hub filters on
|
|
59
|
+
// today. The flow events that replace these carry a real audience.
|
|
60
|
+
for (const step of [
|
|
61
|
+
'accepted',
|
|
62
|
+
'completed',
|
|
63
|
+
'failed'
|
|
64
|
+
])registerEvent(transactionEventName(step), TransactionEventSchema, {
|
|
65
|
+
audience: 'none'
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
//# sourceMappingURL=messages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/contracts/messages.ts"],"sourcesContent":["import { openStruct, registerEvent, registerMessage } from '@entifix/core';\nimport { Schema } from 'effect';\n\nimport { TRANSACTION_STATES, transactionEventName } from './event.js';\n\n/**\n * The transaction-shaped messages, registered by the package that owns them —\n * core's registry is open, so these left core's closed union in 0.11.0 and are\n * deleted with the saga runner and the tracker (#127).\n *\n * Each schema names only what a reader of it branches on and keeps the rest,\n * which is enough to refuse a body that is some other message wearing this\n * one's name.\n */\nconst State = Schema.Literals(TRANSACTION_STATES);\n\nexport const TransactionCommandSchema = openStruct({\n transactionId: Schema.String,\n type: Schema.String,\n entity: Schema.String,\n payload: Schema.Unknown,\n});\n\nexport const TransactionAcceptedSchema = openStruct({\n transactionId: Schema.String,\n state: State,\n});\n\nexport const TransactionRecordSchema = openStruct({\n transactionId: Schema.String,\n entity: Schema.String,\n state: State,\n createdAt: Schema.String,\n updatedAt: Schema.String,\n});\n\nexport const SagaResultSchema = openStruct({\n state: Schema.String,\n outcomes: Schema.Array(Schema.Unknown),\n});\n\nexport const SagaInstanceSchema = openStruct({\n sagaId: Schema.String,\n definition: Schema.String,\n state: Schema.String,\n stepIndex: Schema.Number,\n outcomes: Schema.Array(Schema.Unknown),\n});\n\nexport const TransactionEventSchema = openStruct({\n transactionId: Schema.String,\n entity: Schema.String,\n state: State,\n step: Schema.Literals(['accepted', 'completed', 'failed']),\n at: Schema.String,\n});\n\nregisterMessage('command', TransactionCommandSchema);\nregisterMessage('transactionAccepted', TransactionAcceptedSchema);\nregisterMessage('transactionRecord', TransactionRecordSchema);\nregisterMessage('sagaResult', SagaResultSchema);\nregisterMessage('sagaInstance', SagaInstanceSchema);\n\n// ⚠️ Registered audience-free: a transaction event is scoped by the\n// `organizationId` in its `data`, which is what the stream hub filters on\n// today. The flow events that replace these carry a real audience.\nfor (const step of ['accepted', 'completed', 'failed'] as const)\n registerEvent(transactionEventName(step), TransactionEventSchema, {\n audience: 'none',\n });\n"],"names":["openStruct","registerEvent","registerMessage","Schema","TRANSACTION_STATES","transactionEventName","State","Literals","TransactionCommandSchema","transactionId","String","type","entity","payload","Unknown","TransactionAcceptedSchema","state","TransactionRecordSchema","createdAt","updatedAt","SagaResultSchema","outcomes","Array","SagaInstanceSchema","sagaId","definition","stepIndex","Number","TransactionEventSchema","step","at","audience"],"mappings":"AAAA,SAASA,UAAU,EAAEC,aAAa,EAAEC,eAAe,QAAQ,gBAAgB;AAC3E,SAASC,MAAM,QAAQ,SAAS;AAEhC,SAASC,kBAAkB,EAAEC,oBAAoB,QAAQ,aAAa;AAEtE;;;;;;;;CAQC,GACD,MAAMC,QAAQH,OAAOI,QAAQ,CAACH;AAE9B,OAAO,MAAMI,2BAA2BR,WAAW;IACjDS,eAAeN,OAAOO,MAAM;IAC5BC,MAAMR,OAAOO,MAAM;IACnBE,QAAQT,OAAOO,MAAM;IACrBG,SAASV,OAAOW,OAAO;AACzB,GAAG;AAEH,OAAO,MAAMC,4BAA4Bf,WAAW;IAClDS,eAAeN,OAAOO,MAAM;IAC5BM,OAAOV;AACT,GAAG;AAEH,OAAO,MAAMW,0BAA0BjB,WAAW;IAChDS,eAAeN,OAAOO,MAAM;IAC5BE,QAAQT,OAAOO,MAAM;IACrBM,OAAOV;IACPY,WAAWf,OAAOO,MAAM;IACxBS,WAAWhB,OAAOO,MAAM;AAC1B,GAAG;AAEH,OAAO,MAAMU,mBAAmBpB,WAAW;IACzCgB,OAAOb,OAAOO,MAAM;IACpBW,UAAUlB,OAAOmB,KAAK,CAACnB,OAAOW,OAAO;AACvC,GAAG;AAEH,OAAO,MAAMS,qBAAqBvB,WAAW;IAC3CwB,QAAQrB,OAAOO,MAAM;IACrBe,YAAYtB,OAAOO,MAAM;IACzBM,OAAOb,OAAOO,MAAM;IACpBgB,WAAWvB,OAAOwB,MAAM;IACxBN,UAAUlB,OAAOmB,KAAK,CAACnB,OAAOW,OAAO;AACvC,GAAG;AAEH,OAAO,MAAMc,yBAAyB5B,WAAW;IAC/CS,eAAeN,OAAOO,MAAM;IAC5BE,QAAQT,OAAOO,MAAM;IACrBM,OAAOV;IACPuB,MAAM1B,OAAOI,QAAQ,CAAC;QAAC;QAAY;QAAa;KAAS;IACzDuB,IAAI3B,OAAOO,MAAM;AACnB,GAAG;AAEHR,gBAAgB,WAAWM;AAC3BN,gBAAgB,uBAAuBa;AACvCb,gBAAgB,qBAAqBe;AACrCf,gBAAgB,cAAckB;AAC9BlB,gBAAgB,gBAAgBqB;AAEhC,oEAAoE;AACpE,0EAA0E;AAC1E,mEAAmE;AACnE,KAAK,MAAMM,QAAQ;IAAC;IAAY;IAAa;CAAS,CACpD5B,cAAcI,qBAAqBwB,OAAOD,wBAAwB;IAChEG,UAAU;AACZ"}
|