@spine-event-engine/core 2.0.0-snapshot.24 → 2.0.0-snapshot.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -13
- package/REFERENCE.md +16 -12
- package/dist/index.d.ts +46 -18
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +263 -73
- package/dist/index.js.map +1 -1
- package/dist/query/entity-query.d.ts.map +1 -1
- package/dist/query/entity-query.js +4 -6
- package/dist/query/entity-query.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -15,6 +15,7 @@ import { clone, create, createRegistry, fromBinary, fromJsonString, getOption, h
|
|
|
15
15
|
import { base64Decode, base64Encode } from "@bufbuild/protobuf/wire";
|
|
16
16
|
import { AnySchema, Int32ValueSchema, Int64ValueSchema, StringValueSchema, } from "@bufbuild/protobuf/wkt";
|
|
17
17
|
import { validate as validateWithSpine } from "@spine-event-engine/validation";
|
|
18
|
+
import { registeredProtoModules } from "@spine-event-engine/proto/codegen";
|
|
18
19
|
import { ActorContextSchema, CommandContextSchema, CommandIdSchema, CommandSchema, CommandContext_ScheduleSchema, Command_SystemPropertiesSchema, ConstraintViolationSchema, EmailAddressSchema, EnrichmentSchema, Enrichment_ContainerSchema, EventContextSchema, EventIdSchema, EventSchema, FieldPathSchema, InternetDomainSchema, LocalDateSchema, LocalDateTimeSchema, LocalTimeSchema, MessageIdSchema, OriginSchema, RejectionEventContextSchema, TemplateStringSchema, TenantIdSchema, UserIdSchema, ValidationErrorSchema, VersionSchema, YearMonthSchema, ZoneIdSchema, ZonedDateTimeSchema, type_url_prefix, } from "@spine-event-engine/proto";
|
|
19
20
|
export { Time } from "./time/index.js";
|
|
20
21
|
export { EntityColumn, } from "./entity/entity-column.js";
|
|
@@ -418,34 +419,70 @@ export const TypeUrls = {
|
|
|
418
419
|
};
|
|
419
420
|
Object.freeze(TypeUrls);
|
|
420
421
|
/**
|
|
421
|
-
* Packs and unpacks
|
|
422
|
+
* Packs and unpacks Protobuf messages in `google.protobuf.Any` envelopes.
|
|
422
423
|
*/
|
|
423
|
-
|
|
424
|
-
|
|
424
|
+
// eslint-disable-next-line @typescript-eslint/no-extraneous-class -- Static overloads mirror the JVM utility API.
|
|
425
|
+
export class AnyPacker {
|
|
425
426
|
/**
|
|
426
|
-
*
|
|
427
|
-
* @typeParam Schema Generated schema of the enclosed message.
|
|
428
|
-
* @param schema The message schema.
|
|
429
|
-
* @param message The message to pack.
|
|
430
|
-
* @param options The packing options.
|
|
431
|
-
* @returns The packed Any message.
|
|
427
|
+
* Creates no public instances of this utility class.
|
|
432
428
|
*/
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
429
|
+
constructor() {
|
|
430
|
+
throw new Error("AnyPacker cannot be constructed.");
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Packs a typed message or returns an existing Any unchanged.
|
|
434
|
+
* @param schemaOrPacked Generated schema or existing Any message.
|
|
435
|
+
* @param message Message to pack when a schema is supplied.
|
|
436
|
+
* @returns Packed Any message.
|
|
437
|
+
*/
|
|
438
|
+
static pack(schemaOrPacked, message) {
|
|
439
|
+
if ("$typeName" in schemaOrPacked)
|
|
440
|
+
return schemaOrPacked;
|
|
441
|
+
if (message === undefined)
|
|
442
|
+
throw new TypeError("AnyPacker.pack requires a message.");
|
|
443
|
+
const schema = schemaOrPacked;
|
|
444
|
+
if (schema.typeName === AnySchema.typeName && message.$typeName === AnySchema.typeName)
|
|
445
|
+
return message;
|
|
436
446
|
return create(AnySchema, {
|
|
437
447
|
typeUrl: TypeUrls.derive(schema),
|
|
438
|
-
value: toBinary(schema, message
|
|
448
|
+
value: toBinary(schema, message),
|
|
439
449
|
});
|
|
440
|
-
}
|
|
450
|
+
}
|
|
441
451
|
/**
|
|
442
452
|
* Unpacks an `Any` when its type URL exactly matches the requested schema.
|
|
443
453
|
* @typeParam Schema Generated schema expected in the envelope.
|
|
444
454
|
* @param packed The packed message.
|
|
445
455
|
* @param schema The expected schema.
|
|
446
|
-
* @returns The
|
|
456
|
+
* @returns The decoded message.
|
|
457
|
+
*/
|
|
458
|
+
static unpack(packed, schema) {
|
|
459
|
+
if (packed.typeUrl !== TypeUrls.derive(schema)) {
|
|
460
|
+
throw new TypeError(`Expected Any type URL "${TypeUrls.derive(schema)}", got "${packed.typeUrl}".`);
|
|
461
|
+
}
|
|
462
|
+
return fromBinary(schema, packed.value);
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* Unpacks an `Any` when its exact type URL is registered.
|
|
466
|
+
* @param registry The schema registry.
|
|
467
|
+
* @param packed The packed message.
|
|
468
|
+
* @returns The decoded message.
|
|
447
469
|
*/
|
|
448
|
-
|
|
470
|
+
static unpackUsing(registry, packed) {
|
|
471
|
+
const metadata = registry.findByTypeUrl(packed.typeUrl);
|
|
472
|
+
if (metadata === undefined)
|
|
473
|
+
throw new TypeError(`Unregistered Any type URL "${packed.typeUrl}".`);
|
|
474
|
+
return fromBinary(metadata.schema, packed.value);
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Decodes a matching `Any`, returning `undefined` for mismatch or malformed bytes.
|
|
478
|
+
* @typeParam Schema Expected generated message schema.
|
|
479
|
+
* @param packed Packed message to decode, or absent message.
|
|
480
|
+
* @param schema Expected message schema.
|
|
481
|
+
* @returns Decoded message or `undefined`.
|
|
482
|
+
*/
|
|
483
|
+
static tryUnpack(packed, schema) {
|
|
484
|
+
if (packed === undefined)
|
|
485
|
+
return undefined;
|
|
449
486
|
if (packed.typeUrl !== TypeUrls.derive(schema))
|
|
450
487
|
return undefined;
|
|
451
488
|
try {
|
|
@@ -454,14 +491,16 @@ export const AnyMessages = {
|
|
|
454
491
|
catch {
|
|
455
492
|
return undefined;
|
|
456
493
|
}
|
|
457
|
-
}
|
|
494
|
+
}
|
|
458
495
|
/**
|
|
459
|
-
*
|
|
460
|
-
* @param registry
|
|
461
|
-
* @param packed
|
|
462
|
-
* @returns
|
|
496
|
+
* Decodes an exactly registered `Any` when available and well formed.
|
|
497
|
+
* @param registry Generated schema registry.
|
|
498
|
+
* @param packed Packed message to decode, or absent message.
|
|
499
|
+
* @returns Decoded message or `undefined`.
|
|
463
500
|
*/
|
|
464
|
-
|
|
501
|
+
static tryUnpackUsing(registry, packed) {
|
|
502
|
+
if (packed === undefined)
|
|
503
|
+
return undefined;
|
|
465
504
|
const metadata = registry.findByTypeUrl(packed.typeUrl);
|
|
466
505
|
if (metadata === undefined)
|
|
467
506
|
return undefined;
|
|
@@ -471,34 +510,44 @@ export const AnyMessages = {
|
|
|
471
510
|
catch {
|
|
472
511
|
return undefined;
|
|
473
512
|
}
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
Object.freeze(
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
Object.freeze(AnyPacker);
|
|
477
516
|
function isProtobufRegistry(types) {
|
|
478
517
|
return "kind" in types;
|
|
479
518
|
}
|
|
519
|
+
const RegistrySources = Object.freeze({
|
|
520
|
+
/**
|
|
521
|
+
* Resolves a fixed or late-bound Protobuf descriptor registry.
|
|
522
|
+
*
|
|
523
|
+
* @param source Registry value or provider.
|
|
524
|
+
* @returns The current registry, if available.
|
|
525
|
+
*/
|
|
526
|
+
current(source) {
|
|
527
|
+
return typeof source === "function" ? source() : source;
|
|
528
|
+
},
|
|
529
|
+
});
|
|
480
530
|
/**
|
|
481
531
|
* Creates reversible compact Proto JSON conversion for a generated message.
|
|
482
532
|
*
|
|
483
533
|
* @typeParam Schema Generated schema used for conversion.
|
|
484
534
|
* @param schema Message schema used for Proto JSON.
|
|
485
|
-
* @param
|
|
486
|
-
* @param typeUrls Canonical URLs restored on parsed Any messages.
|
|
535
|
+
* @param registrySource Registry or late-bound registry for expanding packed Any messages.
|
|
487
536
|
* @returns Schema-bound stringifier.
|
|
488
537
|
*/
|
|
489
|
-
function defaultMessageStringifier(schema,
|
|
538
|
+
function defaultMessageStringifier(schema, registrySource) {
|
|
490
539
|
return Object.freeze({
|
|
491
540
|
/**
|
|
492
|
-
* Parses compact Proto JSON and restores
|
|
541
|
+
* Parses compact Proto JSON and restores each original Any type URL.
|
|
493
542
|
*
|
|
494
543
|
* @param value Compact Proto JSON text.
|
|
495
544
|
* @returns Parsed generated message.
|
|
496
545
|
*/
|
|
497
546
|
fromString(value) {
|
|
498
|
-
const
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
547
|
+
const registry = RegistrySources.current(registrySource);
|
|
548
|
+
const options = registry === undefined ? undefined : { registry };
|
|
549
|
+
const message = fromJsonString(schema, value, options);
|
|
550
|
+
AnyJsonUrls.restore(schema, message, JSON.parse(value), registry);
|
|
502
551
|
return message;
|
|
503
552
|
},
|
|
504
553
|
/**
|
|
@@ -508,32 +557,131 @@ function defaultMessageStringifier(schema, registry, typeUrls) {
|
|
|
508
557
|
* @returns Compact Proto JSON text.
|
|
509
558
|
*/
|
|
510
559
|
toString(value) {
|
|
560
|
+
const registry = RegistrySources.current(registrySource);
|
|
511
561
|
return registry === undefined
|
|
512
562
|
? toJsonString(schema, value)
|
|
513
563
|
: toJsonString(schema, value, { registry });
|
|
514
564
|
},
|
|
515
565
|
});
|
|
516
566
|
}
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
567
|
+
const AnyJsonUrls = Object.freeze({
|
|
568
|
+
/**
|
|
569
|
+
* WKT payloads represented under an Any JSON `value` key.
|
|
570
|
+
*/
|
|
571
|
+
customJsonTypes: new Set([
|
|
572
|
+
"google.protobuf.Any",
|
|
573
|
+
"google.protobuf.Timestamp",
|
|
574
|
+
"google.protobuf.Duration",
|
|
575
|
+
"google.protobuf.FieldMask",
|
|
576
|
+
"google.protobuf.Struct",
|
|
577
|
+
"google.protobuf.Value",
|
|
578
|
+
"google.protobuf.ListValue",
|
|
579
|
+
"google.protobuf.DoubleValue",
|
|
580
|
+
"google.protobuf.FloatValue",
|
|
581
|
+
"google.protobuf.Int64Value",
|
|
582
|
+
"google.protobuf.UInt64Value",
|
|
583
|
+
"google.protobuf.Int32Value",
|
|
584
|
+
"google.protobuf.UInt32Value",
|
|
585
|
+
"google.protobuf.BoolValue",
|
|
586
|
+
"google.protobuf.StringValue",
|
|
587
|
+
"google.protobuf.BytesValue",
|
|
588
|
+
]),
|
|
589
|
+
/**
|
|
590
|
+
* Restores original Any URLs by following real Protobuf message fields.
|
|
591
|
+
*
|
|
592
|
+
* @param schema Current message descriptor.
|
|
593
|
+
* @param message Decoded current message.
|
|
594
|
+
* @param json Corresponding Proto JSON value.
|
|
595
|
+
* @param registry Descriptor registry for packed messages.
|
|
596
|
+
* @returns Whether any URL in this message changed.
|
|
597
|
+
*/
|
|
598
|
+
restore(schema, message, json, registry) {
|
|
599
|
+
if (typeof json !== "object" || json === null || Array.isArray(json))
|
|
600
|
+
return false;
|
|
601
|
+
const source = json;
|
|
602
|
+
if (schema.typeName === AnySchema.typeName)
|
|
603
|
+
return this.any(message, source, registry);
|
|
604
|
+
if (["google.protobuf.Struct", "google.protobuf.Value", "google.protobuf.ListValue"].includes(schema.typeName))
|
|
605
|
+
return false;
|
|
606
|
+
let changed = false;
|
|
607
|
+
for (const field of schema.fields) {
|
|
608
|
+
const value = source[field.jsonName] ?? source[field.name];
|
|
609
|
+
if (value === undefined)
|
|
610
|
+
continue;
|
|
611
|
+
changed = this.field(field, message, value, registry) || changed;
|
|
532
612
|
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
613
|
+
return changed;
|
|
614
|
+
},
|
|
615
|
+
/**
|
|
616
|
+
* Restores one Any and descends into its parsed packed payload.
|
|
617
|
+
* @param message Decoded Any message.
|
|
618
|
+
* @param source Its Proto JSON value.
|
|
619
|
+
* @param registry Descriptor registry for the payload.
|
|
620
|
+
* @returns Whether its URL or nested payload changed.
|
|
621
|
+
*/
|
|
622
|
+
any(message, source, registry) {
|
|
623
|
+
const typeUrl = source["@type"];
|
|
624
|
+
if (typeof typeUrl !== "string")
|
|
625
|
+
return false;
|
|
626
|
+
const changedUrl = message.typeUrl !== typeUrl;
|
|
627
|
+
message.typeUrl = typeUrl;
|
|
628
|
+
const schema = registry?.getMessage(typeUrl.slice(typeUrl.lastIndexOf("/") + 1));
|
|
629
|
+
if (schema === undefined)
|
|
630
|
+
return changedUrl;
|
|
631
|
+
const payload = fromBinary(schema, message.value);
|
|
632
|
+
const json = this.customJsonTypes.has(schema.typeName) ? source.value : source;
|
|
633
|
+
const changedPayload = this.restore(schema, payload, json, registry);
|
|
634
|
+
if (changedPayload)
|
|
635
|
+
message.value = toBinary(schema, payload);
|
|
636
|
+
return changedUrl || changedPayload;
|
|
637
|
+
},
|
|
638
|
+
/**
|
|
639
|
+
* Restores nested message, list, map, or selected oneof field values.
|
|
640
|
+
* @param field Current message field descriptor.
|
|
641
|
+
* @param parent Decoded parent message.
|
|
642
|
+
* @param json Corresponding field JSON.
|
|
643
|
+
* @param registry Descriptor registry for packed messages.
|
|
644
|
+
* @returns Whether any nested URL changed.
|
|
645
|
+
*/
|
|
646
|
+
field(field, parent, json, registry) {
|
|
647
|
+
const value = this.selected(field, parent);
|
|
648
|
+
if (value === undefined)
|
|
649
|
+
return false;
|
|
650
|
+
if (field.fieldKind === "message")
|
|
651
|
+
return this.restore(field.message, value, json, registry);
|
|
652
|
+
if (field.fieldKind === "list" && field.listKind === "message" && Array.isArray(json))
|
|
653
|
+
return json
|
|
654
|
+
.map((item, index) => {
|
|
655
|
+
const nested = value[index];
|
|
656
|
+
return nested === undefined ? false : this.restore(field.message, nested, item, registry);
|
|
657
|
+
})
|
|
658
|
+
.some(Boolean);
|
|
659
|
+
if (field.fieldKind === "map" &&
|
|
660
|
+
field.mapKind === "message" &&
|
|
661
|
+
typeof json === "object" &&
|
|
662
|
+
json !== null)
|
|
663
|
+
return Object.entries(json)
|
|
664
|
+
.map(([key, item]) => {
|
|
665
|
+
const nested = value[key];
|
|
666
|
+
return nested === undefined ? false : this.restore(field.message, nested, item, registry);
|
|
667
|
+
})
|
|
668
|
+
.some(Boolean);
|
|
669
|
+
return false;
|
|
670
|
+
},
|
|
671
|
+
/**
|
|
672
|
+
* Returns a field's decoded value, respecting oneof cases.
|
|
673
|
+
* @param field Field descriptor.
|
|
674
|
+
* @param parent Decoded parent message.
|
|
675
|
+
* @returns Selected value, if set.
|
|
676
|
+
*/
|
|
677
|
+
selected(field, parent) {
|
|
678
|
+
const source = parent;
|
|
679
|
+
if (field.oneof === undefined)
|
|
680
|
+
return source[field.localName];
|
|
681
|
+
const oneof = source[field.oneof.localName];
|
|
682
|
+
return oneof?.case === field.localName ? oneof.value : undefined;
|
|
683
|
+
},
|
|
684
|
+
});
|
|
537
685
|
const FieldStringifiers = Object.freeze({
|
|
538
686
|
/**
|
|
539
687
|
* Returns reversible conversion for one singular message, enum, or scalar field.
|
|
@@ -898,10 +1046,7 @@ export const Stringifiers = {
|
|
|
898
1046
|
: isProtobufRegistry(types)
|
|
899
1047
|
? types
|
|
900
1048
|
: createRegistry(...types.list().map((metadata) => metadata.descriptor));
|
|
901
|
-
|
|
902
|
-
? new Map()
|
|
903
|
-
: new Map(types.list().map((metadata) => [metadata.schema.typeName, metadata.typeUrl]));
|
|
904
|
-
return defaultMessageStringifier(schema, registry, typeUrls);
|
|
1049
|
+
return defaultMessageStringifier(schema, registry);
|
|
905
1050
|
},
|
|
906
1051
|
/**
|
|
907
1052
|
* Creates a reversible stringifier for one supported singular field.
|
|
@@ -924,8 +1069,8 @@ Object.freeze(Stringifiers);
|
|
|
924
1069
|
*/
|
|
925
1070
|
export class StringifierRegistry {
|
|
926
1071
|
#registered = new Map();
|
|
1072
|
+
#selected = new Set();
|
|
927
1073
|
#types;
|
|
928
|
-
#typeUrls = new Map();
|
|
929
1074
|
/**
|
|
930
1075
|
* Creates an empty registry or a snapshot of another registry.
|
|
931
1076
|
*
|
|
@@ -937,7 +1082,6 @@ export class StringifierRegistry {
|
|
|
937
1082
|
this.#registered.set(typeName, stringifier);
|
|
938
1083
|
}
|
|
939
1084
|
this.#types = source.#types;
|
|
940
|
-
this.#typeUrls = new Map(source.#typeUrls);
|
|
941
1085
|
}
|
|
942
1086
|
}
|
|
943
1087
|
/**
|
|
@@ -947,6 +1091,10 @@ export class StringifierRegistry {
|
|
|
947
1091
|
* @param stringifier The reversible stringifier.
|
|
948
1092
|
*/
|
|
949
1093
|
register(schema, stringifier) {
|
|
1094
|
+
if (this === defaultStringifierRegistry &&
|
|
1095
|
+
this.#selected.has(schema.typeName) &&
|
|
1096
|
+
this.#registered.get(schema.typeName) !== stringifier)
|
|
1097
|
+
throw new Error(`Stringifier choice is already in use for ${schema.typeName}.`);
|
|
950
1098
|
this.#registered.set(schema.typeName, stringifier);
|
|
951
1099
|
}
|
|
952
1100
|
/**
|
|
@@ -955,9 +1103,10 @@ export class StringifierRegistry {
|
|
|
955
1103
|
* @param types Resolves message types embedded in `Any` values.
|
|
956
1104
|
*/
|
|
957
1105
|
setTypeRegistry(types) {
|
|
1106
|
+
if (this === defaultStringifierRegistry)
|
|
1107
|
+
throw new Error("The shared stringifier registry uses generated module descriptors.");
|
|
958
1108
|
const metadata = types.list();
|
|
959
1109
|
this.#types = createRegistry(...metadata.map((item) => item.descriptor));
|
|
960
|
-
this.#typeUrls = new Map(metadata.map((item) => [item.schema.typeName, item.typeUrl]));
|
|
961
1110
|
}
|
|
962
1111
|
/**
|
|
963
1112
|
* Returns the custom stringifier or the default compact Proto JSON mapping.
|
|
@@ -967,8 +1116,10 @@ export class StringifierRegistry {
|
|
|
967
1116
|
*/
|
|
968
1117
|
forMessage(schema) {
|
|
969
1118
|
const registered = this.#registered.get(schema.typeName);
|
|
1119
|
+
if (this === defaultStringifierRegistry)
|
|
1120
|
+
this.#selected.add(schema.typeName);
|
|
970
1121
|
return registered === undefined
|
|
971
|
-
? defaultMessageStringifier(schema, this.#types
|
|
1122
|
+
? defaultMessageStringifier(schema, () => this.#types ?? GeneratedDescriptors.current())
|
|
972
1123
|
: registered;
|
|
973
1124
|
}
|
|
974
1125
|
/**
|
|
@@ -986,6 +1137,31 @@ export class StringifierRegistry {
|
|
|
986
1137
|
return FieldStringifiers.create(field, (schema) => this.forMessage(schema));
|
|
987
1138
|
}
|
|
988
1139
|
}
|
|
1140
|
+
let descriptorSnapshot;
|
|
1141
|
+
let descriptorRegistry;
|
|
1142
|
+
/**
|
|
1143
|
+
* Reuses descriptor composition until another generated module registers.
|
|
1144
|
+
*/
|
|
1145
|
+
const GeneratedDescriptors = Object.freeze({
|
|
1146
|
+
/**
|
|
1147
|
+
* Resolves descriptors from the current generated-module inventory.
|
|
1148
|
+
*
|
|
1149
|
+
* @returns The composed registry, if available.
|
|
1150
|
+
*/
|
|
1151
|
+
current() {
|
|
1152
|
+
const current = registeredProtoModules();
|
|
1153
|
+
if (current !== descriptorSnapshot) {
|
|
1154
|
+
const types = TypeRegistry.from(...current);
|
|
1155
|
+
descriptorRegistry = createRegistry(...types.list().map((item) => item.descriptor));
|
|
1156
|
+
descriptorSnapshot = current;
|
|
1157
|
+
}
|
|
1158
|
+
return descriptorRegistry;
|
|
1159
|
+
},
|
|
1160
|
+
});
|
|
1161
|
+
/**
|
|
1162
|
+
* Shared converter policy for application storage factories and mappings.
|
|
1163
|
+
*/
|
|
1164
|
+
export const defaultStringifierRegistry = new StringifierRegistry();
|
|
989
1165
|
/**
|
|
990
1166
|
* Packs and unpacks the identifier types supported by Spine JVM storage.
|
|
991
1167
|
*/
|
|
@@ -1032,31 +1208,29 @@ const IdentifierValues = Object.freeze({
|
|
|
1032
1208
|
});
|
|
1033
1209
|
function packIdentifier(type, value) {
|
|
1034
1210
|
if (typeof type !== "string") {
|
|
1035
|
-
return
|
|
1211
|
+
return AnyPacker.pack(type, value);
|
|
1036
1212
|
}
|
|
1037
1213
|
switch (type) {
|
|
1038
1214
|
case "string":
|
|
1039
1215
|
if (typeof value !== "string")
|
|
1040
1216
|
throw new TypeError("Identifier must be a string.");
|
|
1041
|
-
return
|
|
1042
|
-
validate: false,
|
|
1043
|
-
});
|
|
1217
|
+
return AnyPacker.pack(StringValueSchema, create(StringValueSchema, { value }));
|
|
1044
1218
|
case "int32":
|
|
1045
|
-
return
|
|
1219
|
+
return AnyPacker.pack(Int32ValueSchema, create(Int32ValueSchema, { value: IdentifierValues.int32(value) }));
|
|
1046
1220
|
case "int64":
|
|
1047
|
-
return
|
|
1221
|
+
return AnyPacker.pack(Int64ValueSchema, create(Int64ValueSchema, { value: IdentifierValues.int64(value) }));
|
|
1048
1222
|
}
|
|
1049
1223
|
}
|
|
1050
1224
|
function unpackIdentifier(type, value) {
|
|
1051
1225
|
if (typeof type !== "string")
|
|
1052
|
-
return
|
|
1226
|
+
return type.typeName === AnySchema.typeName ? value : AnyPacker.tryUnpack(value, type);
|
|
1053
1227
|
switch (type) {
|
|
1054
1228
|
case "string":
|
|
1055
|
-
return
|
|
1229
|
+
return AnyPacker.tryUnpack(value, StringValueSchema)?.value;
|
|
1056
1230
|
case "int32":
|
|
1057
|
-
return
|
|
1231
|
+
return AnyPacker.tryUnpack(value, Int32ValueSchema)?.value;
|
|
1058
1232
|
case "int64":
|
|
1059
|
-
return
|
|
1233
|
+
return AnyPacker.tryUnpack(value, Int64ValueSchema)?.value;
|
|
1060
1234
|
}
|
|
1061
1235
|
}
|
|
1062
1236
|
function freshSignalId() {
|
|
@@ -1078,6 +1252,22 @@ function freshSignalId() {
|
|
|
1078
1252
|
const hex = [...bytes].map((value) => value.toString(16).padStart(2, "0")).join("");
|
|
1079
1253
|
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
1080
1254
|
}
|
|
1255
|
+
/**
|
|
1256
|
+
* Keeps signal validation at the semantic envelope boundary.
|
|
1257
|
+
*/
|
|
1258
|
+
const SignalPacking = Object.freeze({
|
|
1259
|
+
/**
|
|
1260
|
+
* Validates and packs a domain signal.
|
|
1261
|
+
* @typeParam Schema Generated signal schema.
|
|
1262
|
+
* @param input Domain message and validation setting.
|
|
1263
|
+
* @returns Packed signal message.
|
|
1264
|
+
*/
|
|
1265
|
+
pack(input) {
|
|
1266
|
+
if (input.validate !== false)
|
|
1267
|
+
Validate.check(input.schema, input.message);
|
|
1268
|
+
return AnyPacker.pack(input.schema, input.message);
|
|
1269
|
+
},
|
|
1270
|
+
});
|
|
1081
1271
|
/**
|
|
1082
1272
|
* Creates generated Spine command and event envelopes.
|
|
1083
1273
|
*/
|
|
@@ -1092,7 +1282,7 @@ export const SignalEnvelopes = {
|
|
|
1092
1282
|
command(input) {
|
|
1093
1283
|
return create(CommandSchema, {
|
|
1094
1284
|
id: create(CommandIdSchema, { uuid: freshSignalId() }),
|
|
1095
|
-
message:
|
|
1285
|
+
message: SignalPacking.pack(input),
|
|
1096
1286
|
context: clone(CommandContextSchema, input.context),
|
|
1097
1287
|
});
|
|
1098
1288
|
},
|
|
@@ -1105,7 +1295,7 @@ export const SignalEnvelopes = {
|
|
|
1105
1295
|
event(input) {
|
|
1106
1296
|
return create(EventSchema, {
|
|
1107
1297
|
id: create(EventIdSchema, { value: freshSignalId() }),
|
|
1108
|
-
message:
|
|
1298
|
+
message: SignalPacking.pack(input),
|
|
1109
1299
|
context: clone(EventContextSchema, input.context),
|
|
1110
1300
|
});
|
|
1111
1301
|
},
|
|
@@ -1119,7 +1309,7 @@ export const SignalEnvelopes = {
|
|
|
1119
1309
|
throw new TypeError("A Command origin requires a non-empty Command ID.");
|
|
1120
1310
|
return create(OriginSchema, {
|
|
1121
1311
|
message: create(MessageIdSchema, {
|
|
1122
|
-
id:
|
|
1312
|
+
id: AnyPacker.pack(CommandIdSchema, command.id),
|
|
1123
1313
|
typeUrl: command.message?.typeUrl ?? "",
|
|
1124
1314
|
}),
|
|
1125
1315
|
...(command.context?.actorContext === undefined
|