@entifix/core 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.
Files changed (64) hide show
  1. package/dist/endpoint/endpoint.d.ts +154 -0
  2. package/dist/endpoint/endpoint.d.ts.map +1 -0
  3. package/dist/endpoint/endpoint.js +61 -0
  4. package/dist/endpoint/endpoint.js.map +1 -0
  5. package/dist/endpoint/index.d.ts +2 -0
  6. package/dist/endpoint/index.d.ts.map +1 -0
  7. package/dist/endpoint/index.js +3 -0
  8. package/dist/endpoint/index.js.map +1 -0
  9. package/dist/entity-definition/decorators/use-case/use-case.d.ts.map +1 -1
  10. package/dist/entity-definition/decorators/use-case/use-case.js +2 -1
  11. package/dist/entity-definition/decorators/use-case/use-case.js.map +1 -1
  12. package/dist/entity-definition/describe/describe-entity-use-cases.d.ts +18 -0
  13. package/dist/entity-definition/describe/describe-entity-use-cases.d.ts.map +1 -1
  14. package/dist/entity-definition/describe/describe-entity-use-cases.js +3 -1
  15. package/dist/entity-definition/describe/describe-entity-use-cases.js.map +1 -1
  16. package/dist/entity-definition/helpers/entity-definition-helpers.d.ts +5 -0
  17. package/dist/entity-definition/helpers/entity-definition-helpers.d.ts.map +1 -1
  18. package/dist/entity-definition/helpers/entity-definition-helpers.js +10 -0
  19. package/dist/entity-definition/helpers/entity-definition-helpers.js.map +1 -1
  20. package/dist/entity-definition/meta-entities/meta-use-case/meta-use-case.d.ts +29 -0
  21. package/dist/entity-definition/meta-entities/meta-use-case/meta-use-case.d.ts.map +1 -1
  22. package/dist/entity-definition/meta-entities/meta-use-case/meta-use-case.js +5 -0
  23. package/dist/entity-definition/meta-entities/meta-use-case/meta-use-case.js.map +1 -1
  24. package/dist/envelope/codec.d.ts +123 -0
  25. package/dist/envelope/codec.d.ts.map +1 -0
  26. package/dist/envelope/codec.js +203 -0
  27. package/dist/envelope/codec.js.map +1 -0
  28. package/dist/envelope/index.d.ts +2 -0
  29. package/dist/envelope/index.d.ts.map +1 -1
  30. package/dist/envelope/index.js +2 -0
  31. package/dist/envelope/index.js.map +1 -1
  32. package/dist/envelope/read-envelope.d.ts +20 -37
  33. package/dist/envelope/read-envelope.d.ts.map +1 -1
  34. package/dist/envelope/read-envelope.js +37 -126
  35. package/dist/envelope/read-envelope.js.map +1 -1
  36. package/dist/envelope/registry.d.ts +45 -0
  37. package/dist/envelope/registry.d.ts.map +1 -0
  38. package/dist/envelope/registry.js +55 -0
  39. package/dist/envelope/registry.js.map +1 -0
  40. package/dist/envelope/types.d.ts +48 -33
  41. package/dist/envelope/types.d.ts.map +1 -1
  42. package/dist/envelope/types.js +1 -1
  43. package/dist/envelope/types.js.map +1 -1
  44. package/dist/idempotency/idempotency-key.d.ts +20 -0
  45. package/dist/idempotency/idempotency-key.d.ts.map +1 -0
  46. package/dist/idempotency/idempotency-key.js +42 -0
  47. package/dist/idempotency/idempotency-key.js.map +1 -0
  48. package/dist/idempotency/index.d.ts +2 -0
  49. package/dist/idempotency/index.d.ts.map +1 -0
  50. package/dist/idempotency/index.js +3 -0
  51. package/dist/idempotency/index.js.map +1 -0
  52. package/dist/index.d.ts +3 -0
  53. package/dist/index.d.ts.map +1 -1
  54. package/dist/index.js +3 -0
  55. package/dist/index.js.map +1 -1
  56. package/dist/use-case-runner/index.d.ts +2 -0
  57. package/dist/use-case-runner/index.d.ts.map +1 -0
  58. package/dist/use-case-runner/index.js +3 -0
  59. package/dist/use-case-runner/index.js.map +1 -0
  60. package/dist/use-case-runner/use-case-runner.d.ts +29 -0
  61. package/dist/use-case-runner/use-case-runner.d.ts.map +1 -0
  62. package/dist/use-case-runner/use-case-runner.js +10 -0
  63. package/dist/use-case-runner/use-case-runner.js.map +1 -0
  64. package/package.json +1 -1
@@ -0,0 +1,123 @@
1
+ import { Effect, Schema } from 'effect';
2
+ import { ProtocolFailure } from '../failure/generic.js';
3
+ import type { AudienceEntry, DomainEvent, EntifixEnvelope, EventEnvelope } from './types.js';
4
+ /**
5
+ * A struct that keeps members it does not name: a newer peer's field is not
6
+ * ours to drop. What a registered message schema should usually be, since a
7
+ * plain `Schema.Struct` strips every unknown key on decode.
8
+ */
9
+ export declare const openStruct: <S extends Schema.Struct.Fields>(fields: S) => Schema.StructWithRest<Schema.Struct<S>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>;
10
+ /**
11
+ * The envelope frame around one `data` schema — for a reader that knows the
12
+ * one shape it accepts, such as a channel that only ever carries entity
13
+ * changes, and so needs no registry lookup.
14
+ */
15
+ export declare const Envelope: {
16
+ readonly codec: <A, I>(data: Schema.Codec<A, I>) => Schema.Struct<{
17
+ readonly meta: Schema.StructWithRest<Schema.Struct<{
18
+ type: Schema.NonEmptyString;
19
+ entity: Schema.optional<Schema.String>;
20
+ links: Schema.optional<Schema.mutable<Schema.$Array<Schema.Struct<{
21
+ readonly rel: Schema.String;
22
+ readonly href: Schema.String;
23
+ readonly method: Schema.optional<Schema.Literals<readonly ["GET", "POST", "PUT", "PATCH", "DELETE"]>>;
24
+ }>>>>;
25
+ event: Schema.optional<Schema.StructWithRest<Schema.Struct<{
26
+ name: Schema.NonEmptyString;
27
+ id: Schema.NonEmptyString;
28
+ source: Schema.NonEmptyString;
29
+ at: Schema.NonEmptyString;
30
+ correlationId: Schema.optional<Schema.String>;
31
+ aggregate: Schema.optional<Schema.Struct<{
32
+ readonly type: Schema.String;
33
+ readonly id: Schema.String;
34
+ }>>;
35
+ version: Schema.optional<Schema.Int>;
36
+ audience: Schema.optional<Schema.$Array<Schema.Struct<{
37
+ readonly kind: Schema.Literals<readonly ["organization", "party", "flow"]>;
38
+ readonly id: Schema.String;
39
+ readonly digest: Schema.optional<Schema.String>;
40
+ }>>>;
41
+ }>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>>;
42
+ }>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>;
43
+ readonly data: Schema.Codec<A, I, never, never>;
44
+ }>;
45
+ };
46
+ /**
47
+ * Decodes an arbitrary body into an envelope whose `data` has been decoded
48
+ * against the schema registered for it.
49
+ *
50
+ * Nothing is cast: a body that is not an envelope, names a type nothing
51
+ * registered, or carries `data` its schema refuses fails with a
52
+ * {@link ProtocolFailure} — poison on a bus, a dropped frame in a browser, a
53
+ * typed failure at an HTTP client. `expected` narrows the accepted `meta.type`.
54
+ */
55
+ export declare const decodeEnvelope: (body: unknown, expected?: string) => Effect.Effect<EntifixEnvelope, ProtocolFailure>;
56
+ /**
57
+ * Decodes a bus or stream message into a {@link DomainEvent}: an `event`
58
+ * envelope whose metadata is complete and whose `data` decoded against its
59
+ * registered schema.
60
+ */
61
+ export declare const decodeEvent: <TData = unknown>(body: unknown) => Effect.Effect<DomainEvent<TData>, ProtocolFailure>;
62
+ /**
63
+ * Encodes an envelope for the wire: its `data` through the registered schema,
64
+ * after checking what the registration requires of `meta` — a version on a
65
+ * versioned message, an audience on an event that needs one. A publisher learns
66
+ * here, at the write, that a message would be unreadable or undeliverable.
67
+ */
68
+ export declare const encodeEnvelope: <TData>(envelope: EntifixEnvelope<TData>) => Effect.Effect<EntifixEnvelope, ProtocolFailure>;
69
+ /** Frames a {@link DomainEvent} as an `event` envelope and encodes it. */
70
+ export declare const encodeEvent: <TData>(event: DomainEvent<TData>) => Effect.Effect<EventEnvelope, ProtocolFailure>;
71
+ /** Who may receive a message: one organization, one party, or one flow's holder. */
72
+ export interface AudienceRecipient {
73
+ readonly organizationId?: string;
74
+ readonly partyId?: string;
75
+ /** The SHA-256 digest of a flow capability the recipient presented. */
76
+ readonly flowDigests?: readonly string[];
77
+ }
78
+ /**
79
+ * Whether `recipient` is in the message's audience.
80
+ *
81
+ * ⚠️ Fails closed: a message with no audience, or an empty one, admits nobody.
82
+ * A stream that broadcast what nobody declared a recipient for would leak the
83
+ * first event someone forgot to address.
84
+ */
85
+ export declare const audienceAdmits: (audience: readonly AudienceEntry[] | undefined, recipient: AudienceRecipient) => boolean;
86
+ /** The `data` of an entity-change event, as a schema. */
87
+ export declare const EntityChangeEventSchema: Schema.Struct<{
88
+ readonly entity: Schema.String;
89
+ readonly change: Schema.Literals<readonly ["created", "updated", "deleted"]>;
90
+ readonly id: Schema.Union<readonly [Schema.String, Schema.Number]>;
91
+ }>;
92
+ /**
93
+ * The one shape the browser's entity-change channel accepts: an `event`
94
+ * envelope with complete `meta.event` and an entity change in `data`.
95
+ */
96
+ export declare const EntityChangeEnvelope: Schema.Struct<{
97
+ readonly meta: Schema.StructWithRest<Schema.Struct<{
98
+ type: Schema.Literal<"event">;
99
+ event: Schema.StructWithRest<Schema.Struct<{
100
+ name: Schema.NonEmptyString;
101
+ id: Schema.NonEmptyString;
102
+ source: Schema.NonEmptyString;
103
+ at: Schema.NonEmptyString;
104
+ correlationId: Schema.optional<Schema.String>;
105
+ aggregate: Schema.optional<Schema.Struct<{
106
+ readonly type: Schema.String;
107
+ readonly id: Schema.String;
108
+ }>>;
109
+ version: Schema.optional<Schema.Int>;
110
+ audience: Schema.optional<Schema.$Array<Schema.Struct<{
111
+ readonly kind: Schema.Literals<readonly ["organization", "party", "flow"]>;
112
+ readonly id: Schema.String;
113
+ readonly digest: Schema.optional<Schema.String>;
114
+ }>>>;
115
+ }>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>;
116
+ }>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>;
117
+ readonly data: Schema.Struct<{
118
+ readonly entity: Schema.String;
119
+ readonly change: Schema.Literals<readonly ["created", "updated", "deleted"]>;
120
+ readonly id: Schema.Union<readonly [Schema.String, Schema.Number]>;
121
+ }>;
122
+ }>;
123
+ //# sourceMappingURL=codec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../../src/envelope/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAExC,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAOxD,OAAO,KAAK,EACV,aAAa,EACb,WAAW,EACX,eAAe,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,EAC9C,WAAW,iBAAiB,KAC3B,OAaC,CAAC;AAoCL,yDAAyD;AACzD,eAAO,MAAM,uBAAuB;;;;EAIlC,CAAC;AAEH;;;GAGG;AACH,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;EAM/B,CAAC"}
@@ -0,0 +1,203 @@
1
+ import { _ as _extends } from "@swc/helpers/_/_extends";
2
+ import { _ as _object_without_properties_loose } from "@swc/helpers/_/_object_without_properties_loose";
3
+ import { Effect, Schema } from 'effect';
4
+ import { ProtocolFailure } from '../failure/generic.js';
5
+ import { lookupEvent, lookupMessage, registerMessage } from './registry.js';
6
+ /**
7
+ * A struct that keeps members it does not name: a newer peer's field is not
8
+ * ours to drop. What a registered message schema should usually be, since a
9
+ * plain `Schema.Struct` strips every unknown key on decode.
10
+ */ export const openStruct = (fields)=>Schema.StructWithRest(Schema.Struct(fields), [
11
+ Schema.Record(Schema.String, Schema.Unknown)
12
+ ]);
13
+ const AudienceEntrySchema = Schema.Struct({
14
+ kind: Schema.Literals([
15
+ 'organization',
16
+ 'party',
17
+ 'flow'
18
+ ]),
19
+ id: Schema.String,
20
+ digest: Schema.optional(Schema.String)
21
+ });
22
+ const EventMetaSchema = openStruct({
23
+ name: Schema.NonEmptyString,
24
+ id: Schema.NonEmptyString,
25
+ source: Schema.NonEmptyString,
26
+ at: Schema.NonEmptyString,
27
+ correlationId: Schema.optional(Schema.String),
28
+ aggregate: Schema.optional(Schema.Struct({
29
+ type: Schema.String,
30
+ id: Schema.String
31
+ })),
32
+ version: Schema.optional(Schema.Int),
33
+ audience: Schema.optional(Schema.Array(AudienceEntrySchema))
34
+ });
35
+ const LinkSchema = Schema.Struct({
36
+ rel: Schema.String,
37
+ href: Schema.String,
38
+ method: Schema.optional(Schema.Literals([
39
+ 'GET',
40
+ 'POST',
41
+ 'PUT',
42
+ 'PATCH',
43
+ 'DELETE'
44
+ ]))
45
+ });
46
+ const MetaSchema = openStruct({
47
+ type: Schema.NonEmptyString,
48
+ entity: Schema.optional(Schema.String),
49
+ links: Schema.optional(Schema.mutable(Schema.Array(LinkSchema))),
50
+ event: Schema.optional(EventMetaSchema)
51
+ });
52
+ const FrameSchema = Schema.Struct({
53
+ meta: MetaSchema,
54
+ data: Schema.Unknown
55
+ });
56
+ /**
57
+ * The envelope frame around one `data` schema — for a reader that knows the
58
+ * one shape it accepts, such as a channel that only ever carries entity
59
+ * changes, and so needs no registry lookup.
60
+ */ export const Envelope = {
61
+ codec: (data)=>Schema.Struct({
62
+ meta: MetaSchema,
63
+ data
64
+ })
65
+ };
66
+ const protocol = (reason)=>new ProtocolFailure({
67
+ reason
68
+ });
69
+ const describe = (error)=>error.message;
70
+ /** The registration a frame names: its `meta.type`, or its event name. */ const registrationFor = (meta)=>{
71
+ var _lookupMessage;
72
+ if (meta.type === 'event') {
73
+ var _lookupEvent;
74
+ if (meta.event === undefined) return "an 'event' envelope carried no meta.event";
75
+ return (_lookupEvent = lookupEvent(meta.event.name)) != null ? _lookupEvent : `no schema is registered for event '${meta.event.name}'`;
76
+ }
77
+ return (_lookupMessage = lookupMessage(meta.type)) != null ? _lookupMessage : `no schema is registered for message type '${meta.type}'`;
78
+ };
79
+ const checkRequirements = (registered, event, direction)=>{
80
+ if (registered.versioned && ((event == null ? void 0 : event.version) === undefined || event.aggregate === undefined)) return `'${registered.name}' describes an aggregate version and carried no aggregate or no version`;
81
+ if (direction === 'encode' && registered.kind === 'event' && registered.audience === 'required' && ((event == null ? void 0 : event.audience) === undefined || event.audience.length === 0)) return `'${registered.name}' must name its audience; an empty one reaches nobody`;
82
+ return undefined;
83
+ };
84
+ /**
85
+ * Decodes an arbitrary body into an envelope whose `data` has been decoded
86
+ * against the schema registered for it.
87
+ *
88
+ * Nothing is cast: a body that is not an envelope, names a type nothing
89
+ * registered, or carries `data` its schema refuses fails with a
90
+ * {@link ProtocolFailure} — poison on a bus, a dropped frame in a browser, a
91
+ * typed failure at an HTTP client. `expected` narrows the accepted `meta.type`.
92
+ */ export const decodeEnvelope = (body, expected)=>Effect.gen(function*() {
93
+ const frame = yield* Schema.decodeUnknownEffect(FrameSchema)(body).pipe(Effect.mapError((error)=>protocol(`the body is not an envelope: ${describe(error)}`)));
94
+ if (expected !== undefined && frame.meta.type !== expected) return yield* Effect.fail(protocol(`expected an envelope of type '${expected}' but got '${frame.meta.type}'`));
95
+ const registered = registrationFor(frame.meta);
96
+ if (typeof registered === 'string') return yield* Effect.fail(protocol(registered));
97
+ const meta = frame.meta;
98
+ const unmet = checkRequirements(registered, meta.event, 'decode');
99
+ if (unmet !== undefined) return yield* Effect.fail(protocol(unmet));
100
+ const data = yield* Schema.decodeUnknownEffect(registered.schema)(frame.data).pipe(Effect.mapError((error)=>protocol(`the data of '${registered.name}' does not decode: ${describe(error)}`)));
101
+ return {
102
+ meta,
103
+ data
104
+ };
105
+ });
106
+ /**
107
+ * Decodes a bus or stream message into a {@link DomainEvent}: an `event`
108
+ * envelope whose metadata is complete and whose `data` decoded against its
109
+ * registered schema.
110
+ */ export const decodeEvent = (body)=>Effect.map(decodeEnvelope(body, 'event'), (envelope)=>_extends({}, envelope.meta.event, {
111
+ data: envelope.data
112
+ }));
113
+ /**
114
+ * Encodes an envelope for the wire: its `data` through the registered schema,
115
+ * after checking what the registration requires of `meta` — a version on a
116
+ * versioned message, an audience on an event that needs one. A publisher learns
117
+ * here, at the write, that a message would be unreadable or undeliverable.
118
+ */ export const encodeEnvelope = (envelope)=>Effect.gen(function*() {
119
+ const registered = registrationFor(envelope.meta);
120
+ if (typeof registered === 'string') return yield* Effect.fail(protocol(registered));
121
+ const unmet = checkRequirements(registered, envelope.meta.event, 'encode');
122
+ if (unmet !== undefined) return yield* Effect.fail(protocol(unmet));
123
+ const data = yield* Schema.encodeUnknownEffect(registered.schema)(envelope.data).pipe(Effect.mapError((error)=>protocol(`the data of '${registered.name}' does not encode: ${describe(error)}`)));
124
+ return {
125
+ meta: envelope.meta,
126
+ data
127
+ };
128
+ });
129
+ /** Frames a {@link DomainEvent} as an `event` envelope and encodes it. */ export const encodeEvent = (event)=>{
130
+ const { data } = event, eventMeta = _object_without_properties_loose(event, [
131
+ "data"
132
+ ]);
133
+ return encodeEnvelope({
134
+ meta: {
135
+ type: 'event',
136
+ event: eventMeta
137
+ },
138
+ data
139
+ });
140
+ };
141
+ /**
142
+ * Whether `recipient` is in the message's audience.
143
+ *
144
+ * ⚠️ Fails closed: a message with no audience, or an empty one, admits nobody.
145
+ * A stream that broadcast what nobody declared a recipient for would leak the
146
+ * first event someone forgot to address.
147
+ */ export const audienceAdmits = (audience, recipient)=>(audience != null ? audience : []).some((entry)=>{
148
+ switch(entry.kind){
149
+ case 'organization':
150
+ return entry.id === recipient.organizationId;
151
+ case 'party':
152
+ return entry.id === recipient.partyId;
153
+ case 'flow':
154
+ var _recipient_flowDigests;
155
+ return entry.digest !== undefined && ((_recipient_flowDigests = recipient.flowDigests) != null ? _recipient_flowDigests : []).includes(entry.digest);
156
+ }
157
+ });
158
+ // What core owns, and therefore registers. Every other shape is registered by
159
+ // the package that owns it.
160
+ const SerializedEntitySchema = Schema.Record(Schema.String, Schema.Unknown);
161
+ registerMessage('entity', SerializedEntitySchema);
162
+ registerMessage('entityCollection', Schema.Array(SerializedEntitySchema));
163
+ registerMessage('entityPage', openStruct({
164
+ items: Schema.Array(SerializedEntitySchema),
165
+ total: Schema.Number,
166
+ request: Schema.optionalKey(Schema.Unknown)
167
+ }));
168
+ registerMessage('entityMetadata', openStruct({
169
+ actions: Schema.Array(Schema.Unknown),
170
+ useCases: Schema.Array(Schema.Unknown)
171
+ }));
172
+ registerMessage('entityFacets', Schema.Record(Schema.String, Schema.Array(Schema.Struct({
173
+ value: Schema.Union([
174
+ Schema.String,
175
+ Schema.Number,
176
+ Schema.Boolean
177
+ ]),
178
+ count: Schema.Number
179
+ }))));
180
+ /** The `data` of an entity-change event, as a schema. */ export const EntityChangeEventSchema = Schema.Struct({
181
+ entity: Schema.String,
182
+ change: Schema.Literals([
183
+ 'created',
184
+ 'updated',
185
+ 'deleted'
186
+ ]),
187
+ id: Schema.Union([
188
+ Schema.String,
189
+ Schema.Number
190
+ ])
191
+ });
192
+ /**
193
+ * The one shape the browser's entity-change channel accepts: an `event`
194
+ * envelope with complete `meta.event` and an entity change in `data`.
195
+ */ export const EntityChangeEnvelope = Schema.Struct({
196
+ meta: openStruct({
197
+ type: Schema.Literal('event'),
198
+ event: EventMetaSchema
199
+ }),
200
+ data: EntityChangeEventSchema
201
+ });
202
+
203
+ //# sourceMappingURL=codec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/envelope/codec.ts"],"sourcesContent":["import { Effect, Schema } from 'effect';\n\nimport { ProtocolFailure } from '../failure/generic.js';\nimport {\n lookupEvent,\n lookupMessage,\n type RegisteredMessage,\n registerMessage,\n} from './registry.js';\nimport type {\n AudienceEntry,\n DomainEvent,\n EntifixEnvelope,\n EntifixEventMeta,\n EventEnvelope,\n} from './types.js';\n\n/**\n * A struct that keeps members it does not name: a newer peer's field is not\n * ours to drop. What a registered message schema should usually be, since a\n * plain `Schema.Struct` strips every unknown key on decode.\n */\nexport const openStruct = <S extends Schema.Struct.Fields>(fields: S) =>\n Schema.StructWithRest(Schema.Struct(fields), [\n Schema.Record(Schema.String, Schema.Unknown),\n ]);\n\nconst AudienceEntrySchema = Schema.Struct({\n kind: Schema.Literals(['organization', 'party', 'flow']),\n id: Schema.String,\n digest: Schema.optional(Schema.String),\n});\n\nconst EventMetaSchema = openStruct({\n name: Schema.NonEmptyString,\n id: Schema.NonEmptyString,\n source: Schema.NonEmptyString,\n at: Schema.NonEmptyString,\n correlationId: Schema.optional(Schema.String),\n aggregate: Schema.optional(\n Schema.Struct({ type: Schema.String, id: Schema.String }),\n ),\n version: Schema.optional(Schema.Int),\n audience: Schema.optional(Schema.Array(AudienceEntrySchema)),\n});\n\nconst LinkSchema = Schema.Struct({\n rel: Schema.String,\n href: Schema.String,\n method: Schema.optional(\n Schema.Literals(['GET', 'POST', 'PUT', 'PATCH', 'DELETE']),\n ),\n});\n\nconst MetaSchema = openStruct({\n type: Schema.NonEmptyString,\n entity: Schema.optional(Schema.String),\n links: Schema.optional(Schema.mutable(Schema.Array(LinkSchema))),\n event: Schema.optional(EventMetaSchema),\n});\n\nconst FrameSchema = Schema.Struct({\n meta: MetaSchema,\n data: Schema.Unknown,\n});\n\n/**\n * The envelope frame around one `data` schema — for a reader that knows the\n * one shape it accepts, such as a channel that only ever carries entity\n * changes, and so needs no registry lookup.\n */\nexport const Envelope = {\n codec: <A, I>(data: Schema.Codec<A, I>) =>\n Schema.Struct({ meta: MetaSchema, data }),\n} as const;\n\nconst protocol = (reason: string) => new ProtocolFailure({ reason });\n\nconst describe = (error: { readonly message: string }): string => error.message;\n\n/** The registration a frame names: its `meta.type`, or its event name. */\nconst registrationFor = (meta: {\n readonly type: string;\n readonly event?: { readonly name: string } | undefined;\n}): RegisteredMessage | string => {\n if (meta.type === 'event') {\n if (meta.event === undefined)\n return \"an 'event' envelope carried no meta.event\";\n return (\n lookupEvent(meta.event.name) ??\n `no schema is registered for event '${meta.event.name}'`\n );\n }\n return (\n lookupMessage(meta.type) ??\n `no schema is registered for message type '${meta.type}'`\n );\n};\n\nconst checkRequirements = (\n registered: RegisteredMessage,\n event: EntifixEventMeta | undefined,\n direction: 'decode' | 'encode',\n): string | undefined => {\n if (\n registered.versioned &&\n (event?.version === undefined || event.aggregate === undefined)\n )\n return `'${registered.name}' describes an aggregate version and carried no aggregate or no version`;\n if (\n direction === 'encode' &&\n registered.kind === 'event' &&\n registered.audience === 'required' &&\n (event?.audience === undefined || event.audience.length === 0)\n )\n return `'${registered.name}' must name its audience; an empty one reaches nobody`;\n return undefined;\n};\n\n/**\n * Decodes an arbitrary body into an envelope whose `data` has been decoded\n * against the schema registered for it.\n *\n * Nothing is cast: a body that is not an envelope, names a type nothing\n * registered, or carries `data` its schema refuses fails with a\n * {@link ProtocolFailure} — poison on a bus, a dropped frame in a browser, a\n * typed failure at an HTTP client. `expected` narrows the accepted `meta.type`.\n */\nexport const decodeEnvelope = (\n body: unknown,\n expected?: string,\n): Effect.Effect<EntifixEnvelope, ProtocolFailure> =>\n Effect.gen(function* () {\n const frame = yield* Schema.decodeUnknownEffect(FrameSchema)(body).pipe(\n Effect.mapError(error =>\n protocol(`the body is not an envelope: ${describe(error)}`),\n ),\n );\n if (expected !== undefined && frame.meta.type !== expected)\n return yield* Effect.fail(\n protocol(\n `expected an envelope of type '${expected}' but got '${frame.meta.type}'`,\n ),\n );\n const registered = registrationFor(frame.meta);\n if (typeof registered === 'string')\n return yield* Effect.fail(protocol(registered));\n const meta: EntifixEnvelope['meta'] = frame.meta;\n const unmet = checkRequirements(registered, meta.event, 'decode');\n if (unmet !== undefined) return yield* Effect.fail(protocol(unmet));\n const data = yield* Schema.decodeUnknownEffect(registered.schema)(\n frame.data,\n ).pipe(\n Effect.mapError(error =>\n protocol(\n `the data of '${registered.name}' does not decode: ${describe(error)}`,\n ),\n ),\n );\n return { meta, data };\n });\n\n/**\n * Decodes a bus or stream message into a {@link DomainEvent}: an `event`\n * envelope whose metadata is complete and whose `data` decoded against its\n * registered schema.\n */\nexport const decodeEvent = <TData = unknown>(\n body: unknown,\n): Effect.Effect<DomainEvent<TData>, ProtocolFailure> =>\n Effect.map(decodeEnvelope(body, 'event'), envelope => ({\n ...(envelope.meta.event as EntifixEventMeta),\n data: envelope.data as TData,\n }));\n\n/**\n * Encodes an envelope for the wire: its `data` through the registered schema,\n * after checking what the registration requires of `meta` — a version on a\n * versioned message, an audience on an event that needs one. A publisher learns\n * here, at the write, that a message would be unreadable or undeliverable.\n */\nexport const encodeEnvelope = <TData>(\n envelope: EntifixEnvelope<TData>,\n): Effect.Effect<EntifixEnvelope, ProtocolFailure> =>\n Effect.gen(function* () {\n const registered = registrationFor(envelope.meta);\n if (typeof registered === 'string')\n return yield* Effect.fail(protocol(registered));\n const unmet = checkRequirements(registered, envelope.meta.event, 'encode');\n if (unmet !== undefined) return yield* Effect.fail(protocol(unmet));\n const data = yield* Schema.encodeUnknownEffect(registered.schema)(\n envelope.data,\n ).pipe(\n Effect.mapError(error =>\n protocol(\n `the data of '${registered.name}' does not encode: ${describe(error)}`,\n ),\n ),\n );\n return { meta: envelope.meta, data };\n });\n\n/** Frames a {@link DomainEvent} as an `event` envelope and encodes it. */\nexport const encodeEvent = <TData>(\n event: DomainEvent<TData>,\n): Effect.Effect<EventEnvelope, ProtocolFailure> => {\n const { data, ...eventMeta } = event;\n return encodeEnvelope({\n meta: { type: 'event', event: eventMeta },\n data,\n }) as Effect.Effect<EventEnvelope, ProtocolFailure>;\n};\n\n/** Who may receive a message: one organization, one party, or one flow's holder. */\nexport interface AudienceRecipient {\n readonly organizationId?: string;\n readonly partyId?: string;\n /** The SHA-256 digest of a flow capability the recipient presented. */\n readonly flowDigests?: readonly string[];\n}\n\n/**\n * Whether `recipient` is in the message's audience.\n *\n * ⚠️ Fails closed: a message with no audience, or an empty one, admits nobody.\n * A stream that broadcast what nobody declared a recipient for would leak the\n * first event someone forgot to address.\n */\nexport const audienceAdmits = (\n audience: readonly AudienceEntry[] | undefined,\n recipient: AudienceRecipient,\n): boolean =>\n (audience ?? []).some(entry => {\n switch (entry.kind) {\n case 'organization':\n return entry.id === recipient.organizationId;\n case 'party':\n return entry.id === recipient.partyId;\n case 'flow':\n return (\n entry.digest !== undefined &&\n (recipient.flowDigests ?? []).includes(entry.digest)\n );\n }\n });\n\n// What core owns, and therefore registers. Every other shape is registered by\n// the package that owns it.\nconst SerializedEntitySchema = Schema.Record(Schema.String, Schema.Unknown);\n\nregisterMessage('entity', SerializedEntitySchema);\nregisterMessage('entityCollection', Schema.Array(SerializedEntitySchema));\nregisterMessage(\n 'entityPage',\n openStruct({\n items: Schema.Array(SerializedEntitySchema),\n total: Schema.Number,\n request: Schema.optionalKey(Schema.Unknown),\n }),\n);\nregisterMessage(\n 'entityMetadata',\n openStruct({\n actions: Schema.Array(Schema.Unknown),\n useCases: Schema.Array(Schema.Unknown),\n }),\n);\nregisterMessage(\n 'entityFacets',\n Schema.Record(\n Schema.String,\n Schema.Array(\n Schema.Struct({\n value: Schema.Union([Schema.String, Schema.Number, Schema.Boolean]),\n count: Schema.Number,\n }),\n ),\n ),\n);\n\n/** The `data` of an entity-change event, as a schema. */\nexport const EntityChangeEventSchema = Schema.Struct({\n entity: Schema.String,\n change: Schema.Literals(['created', 'updated', 'deleted']),\n id: Schema.Union([Schema.String, Schema.Number]),\n});\n\n/**\n * The one shape the browser's entity-change channel accepts: an `event`\n * envelope with complete `meta.event` and an entity change in `data`.\n */\nexport const EntityChangeEnvelope = Schema.Struct({\n meta: openStruct({\n type: Schema.Literal('event'),\n event: EventMetaSchema,\n }),\n data: EntityChangeEventSchema,\n});\n"],"names":["Effect","Schema","ProtocolFailure","lookupEvent","lookupMessage","registerMessage","openStruct","fields","StructWithRest","Struct","Record","String","Unknown","AudienceEntrySchema","kind","Literals","id","digest","optional","EventMetaSchema","name","NonEmptyString","source","at","correlationId","aggregate","type","version","Int","audience","Array","LinkSchema","rel","href","method","MetaSchema","entity","links","mutable","event","FrameSchema","meta","data","Envelope","codec","protocol","reason","describe","error","message","registrationFor","undefined","checkRequirements","registered","direction","versioned","length","decodeEnvelope","body","expected","gen","frame","decodeUnknownEffect","pipe","mapError","fail","unmet","schema","decodeEvent","map","envelope","encodeEnvelope","encodeUnknownEffect","encodeEvent","eventMeta","audienceAdmits","recipient","some","entry","organizationId","partyId","flowDigests","includes","SerializedEntitySchema","items","total","Number","request","optionalKey","actions","useCases","value","Union","Boolean","count","EntityChangeEventSchema","change","EntityChangeEnvelope","Literal"],"mappings":";;AAAA,SAASA,MAAM,EAAEC,MAAM,QAAQ,SAAS;AAExC,SAASC,eAAe,QAAQ,wBAAwB;AACxD,SACEC,WAAW,EACXC,aAAa,EAEbC,eAAe,QACV,gBAAgB;AASvB;;;;CAIC,GACD,OAAO,MAAMC,aAAa,CAAiCC,SACzDN,OAAOO,cAAc,CAACP,OAAOQ,MAAM,CAACF,SAAS;QAC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MAAM,CAACT,OAAOU,MAAM,EAAEV,OAAOW,OAAO;AAE1EP,gBAAgB,UAAU8E;AAC1B9E,gBAAgB,oBAAoBJ,OAAO6B,KAAK,CAACqD;AACjD9E,gBACE,cACAC,WAAW;IACT8E,OAAOnF,OAAO6B,KAAK,CAACqD;IACpBE,OAAOpF,OAAOqF,MAAM;IACpBC,SAAStF,OAAOuF,WAAW,CAACvF,OAAOW,OAAO;AAC5C;AAEFP,gBACE,kBACAC,WAAW;IACTmF,SAASxF,OAAO6B,KAAK,CAAC7B,OAAOW,OAAO;IACpC8E,UAAUzF,OAAO6B,KAAK,CAAC7B,OAAOW,OAAO;AACvC;AAEFP,gBACE,gBACAJ,OAAOS,MAAM,CACXT,OAAOU,MAAM,EACbV,OAAO6B,KAAK,CACV7B,OAAOQ,MAAM,CAAC;IACZkF,OAAO1F,OAAO2F,KAAK,CAAC;QAAC3F,OAAOU,MAAM;QAAEV,OAAOqF,MAAM;QAAErF,OAAO4F,OAAO;KAAC;IAClEC,OAAO7F,OAAOqF,MAAM;AACtB;AAKN,uDAAuD,GACvD,OAAO,MAAMS,0BAA0B9F,OAAOQ,MAAM,CAAC;IACnD2B,QAAQnC,OAAOU,MAAM;IACrBqF,QAAQ/F,OAAOc,QAAQ,CAAC;QAAC;QAAW;QAAW;KAAU;IACzDC,IAAIf,OAAO2F,KAAK,CAAC;QAAC3F,OAAOU,MAAM;QAAEV,OAAOqF,MAAM;KAAC;AACjD,GAAG;AAEH;;;CAGC,GACD,OAAO,MAAMW,uBAAuBhG,OAAOQ,MAAM,CAAC;IAChDgC,MAAMnC,WAAW;QACfoB,MAAMzB,OAAOiG,OAAO,CAAC;QACrB3D,OAAOpB;IACT;IACAuB,MAAMqD;AACR,GAAG"}
@@ -1,5 +1,7 @@
1
+ export * from './codec.js';
1
2
  export * from './event-pattern.js';
2
3
  export * from './make-envelope.js';
3
4
  export * from './read-envelope.js';
5
+ export * from './registry.js';
4
6
  export * from './types.js';
5
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/envelope/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/envelope/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC"}
@@ -1,6 +1,8 @@
1
+ export * from './codec.js';
1
2
  export * from './event-pattern.js';
2
3
  export * from './make-envelope.js';
3
4
  export * from './read-envelope.js';
5
+ export * from './registry.js';
4
6
  export * from './types.js';
5
7
 
6
8
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/envelope/index.ts"],"sourcesContent":["export * from './event-pattern.js';\nexport * from './make-envelope.js';\nexport * from './read-envelope.js';\nexport * from './types.js';\n"],"names":[],"mappings":"AAAA,cAAc,qBAAqB;AACnC,cAAc,qBAAqB;AACnC,cAAc,qBAAqB;AACnC,cAAc,aAAa"}
1
+ {"version":3,"sources":["../../src/envelope/index.ts"],"sourcesContent":["export * from './codec.js';\nexport * from './event-pattern.js';\nexport * from './make-envelope.js';\nexport * from './read-envelope.js';\nexport * from './registry.js';\nexport * from './types.js';\n"],"names":[],"mappings":"AAAA,cAAc,aAAa;AAC3B,cAAc,qBAAqB;AACnC,cAAc,qBAAqB;AACnC,cAAc,qBAAqB;AACnC,cAAc,gBAAgB;AAC9B,cAAc,aAAa"}
@@ -2,54 +2,37 @@ import { Effect } from 'effect';
2
2
  import type { EntityMetadataDocument } from '../entity-definition/metadata/index.js';
3
3
  import { InvalidEnvelope } from '../failure/generic.js';
4
4
  import type { Entity, EntityConstructor } from '../types/Entity.js';
5
- import type { EntityFacetValue } from '../types/EntityFacets.js';
6
- import type { DomainEvent, EntifixEnvelope, EntifixEnvelopeType } from './types.js';
5
+ import type { EntifixEnvelope, EntifixEnvelopeType } from './types.js';
7
6
  /**
8
- * Structural check only — `meta.type` is validated separately so a mismatch
9
- * reports the expected/actual types rather than a generic "not an envelope".
10
- */
11
- export declare function isEntifixEnvelope(body: unknown): body is EntifixEnvelope;
12
- /**
13
- * Narrows an arbitrary body to an envelope of the expected `meta.type` without
14
- * requiring an entity constructor — for `command`/`transactionRecord` messages
15
- * whose `data` is not a serialized entity. `label` only sharpens error text.
7
+ * Decodes a body a **caller sent** — a request to this service — into an
8
+ * envelope of the expected type, its `data` decoded against the registered
9
+ * schema.
10
+ *
11
+ * The same decode as {@link decodeEnvelope}, failing with an
12
+ * {@link InvalidEnvelope} instead of a protocol failure: a malformed request is
13
+ * the sender's fault and answers `400`, where a peer's malformed *answer* is a
14
+ * `502`.
16
15
  */
17
- export declare function readEnvelope<TData>(body: unknown, expected: EntifixEnvelopeType, label?: string): Effect.Effect<EntifixEnvelope<TData>, InvalidEnvelope>;
18
- export declare const readEntityEnvelope: <TEntity extends Entity>(entityConstructor: EntityConstructor<TEntity>, body: unknown) => Effect.Effect<TEntity, import("../index.js").EntifixBuildError | InvalidEnvelope, never>;
19
- export declare const readEntityCollectionEnvelope: <TEntity extends Entity>(entityConstructor: EntityConstructor<TEntity>, body: unknown) => Effect.Effect<TEntity[], import("../index.js").EntifixBuildError | InvalidEnvelope, never>;
16
+ export declare const decodeRequestEnvelope: (body: unknown, expected: EntifixEnvelopeType, label?: string) => Effect.Effect<EntifixEnvelope, InvalidEnvelope>;
17
+ export declare const readEntityEnvelope: <TEntity extends Entity>(entityConstructor: EntityConstructor<TEntity>, body: unknown) => Effect.Effect<TEntity, InvalidEnvelope, never>;
18
+ export declare const readEntityCollectionEnvelope: <TEntity extends Entity>(entityConstructor: EntityConstructor<TEntity>, body: unknown) => Effect.Effect<TEntity[], InvalidEnvelope, never>;
20
19
  export declare const readEntityPageEnvelope: <TEntity extends Entity>(entityConstructor: EntityConstructor<TEntity>, body: unknown) => Effect.Effect<{
21
20
  items: TEntity[];
22
21
  total: number;
23
22
  request: import("../index.js").EntityLoadRequest<TEntity>;
24
- }, import("../index.js").EntifixBuildError | InvalidEnvelope, never>;
23
+ }, InvalidEnvelope, never>;
25
24
  /**
26
25
  * Reads a `$metadata` response back into its document.
27
26
  *
28
- * No deserialization step, and no defaulting of the members either: an envelope
29
- * whose `data` is missing a list would leave a form guessing whether "no
30
- * actions" meant "denied" or "malformed", so a broken payload fails loudly here
31
- * the way a half-populated entity does.
27
+ * No defaulting of the members: the registered schema refuses a document
28
+ * missing either list, so a form never has to guess whether "no actions" meant
29
+ * "denied" or "malformed".
32
30
  */
33
31
  export declare const readEntityMetadataEnvelope: <TEntity extends Entity>(entityConstructor: EntityConstructor<TEntity>, body: unknown) => Effect.Effect<EntityMetadataDocument, InvalidEnvelope, never>;
34
32
  /**
35
- * Reads a `$facets` response back into its counts, refusing a body whose lists
36
- * are not `{ value, count }` rows — a panel that rendered a malformed count
37
- * would show a number nobody computed.
38
- */
39
- export declare const readEntityFacetsEnvelope: <TEntity extends Entity>(entityConstructor: EntityConstructor<TEntity>, body: unknown) => Effect.Effect<Readonly<Record<string, readonly EntityFacetValue[]>>, InvalidEnvelope, never>;
40
- /**
41
- * Reads a bus message back into a {@link DomainEvent}.
42
- *
43
- * The metadata is validated rather than defaulted, and that is the point: a
44
- * message missing `id` cannot be deduplicated and a message missing `name`
45
- * cannot be routed, so both are failures a consumer must see rather than
46
- * absences it silently works around. It is the same call
47
- * {@link readEntityMetadataEnvelope} makes — a broken payload fails loudly
48
- * instead of arriving half-populated.
49
- *
50
- * Note this does not validate `data`. The transport has no idea what a
51
- * `catalog.published` payload should look like, and inventing an opinion here
52
- * is how the bus would start knowing about domains.
33
+ * Reads a `$facets` response back into its counts. The registered schema
34
+ * refuses a body whose lists are not `{ value, count }` rows — a panel that
35
+ * rendered a malformed count would show a number nobody computed.
53
36
  */
54
- export declare function readEventEnvelope<TData = unknown>(body: unknown): Effect.Effect<DomainEvent<TData>, InvalidEnvelope>;
37
+ export declare const readEntityFacetsEnvelope: <TEntity extends Entity>(entityConstructor: EntityConstructor<TEntity>, body: unknown) => Effect.Effect<Readonly<Record<string, readonly import("../index.js").EntityFacetValue[]>>, InvalidEnvelope, never>;
55
38
  //# sourceMappingURL=read-envelope.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"read-envelope.d.ts","sourceRoot":"","sources":["../../src/envelope/read-envelope.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,wCAAwC,CAAC;AAKrF,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,KAAK,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACpE,OAAO,KAAK,EAEV,gBAAgB,EACjB,MAAM,0BAA0B,CAAC;AAGlC,OAAO,KAAK,EACV,WAAW,EACX,eAAe,EACf,mBAAmB,EAGpB,MAAM,YAAY,CAAC;AAEpB;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,IAAI,eAAe,CAQxE;AA+BD;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAChC,IAAI,EAAE,OAAO,EACb,QAAQ,EAAE,mBAAmB,EAC7B,KAAK,SAAY,GAChB,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,KAAK,CAAC,EAAE,eAAe,CAAC,CAgBxD;AAED,eAAO,MAAM,kBAAkB,GAAI,OAAO,SAAS,MAAM,EACvD,mBAAmB,iBAAiB,CAAC,OAAO,CAAC,EAC7C,MAAM,OAAO,6FAsBX,CAAC;AAEL,eAAO,MAAM,4BAA4B,GAAI,OAAO,SAAS,MAAM,EAC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