@spine-event-engine/core 2.0.0-snapshot.24 → 2.0.0-snapshot.26
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 +24 -14
- package/REFERENCE.md +28 -17
- package/dist/index.d.ts +70 -21
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +292 -80
- 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/spi/reader-forwarding.d.ts +87 -0
- package/dist/spi/reader-forwarding.d.ts.map +1 -0
- package/dist/spi/reader-forwarding.js +162 -0
- package/dist/spi/reader-forwarding.js.map +1 -0
- package/dist/spi/trusted-reader.d.ts +86 -0
- package/dist/spi/trusted-reader.d.ts.map +1 -0
- package/dist/spi/trusted-reader.js +145 -0
- package/dist/spi/trusted-reader.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/validation/registry-validation.d.ts +141 -0
- package/dist/validation/registry-validation.d.ts.map +1 -0
- package/dist/validation/registry-validation.js +342 -0
- package/dist/validation/registry-validation.js.map +1 -0
- package/package.json +10 -2
package/dist/index.js
CHANGED
|
@@ -15,6 +15,8 @@ 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 { RegistryValidation } from "./validation/registry-validation.js";
|
|
19
|
+
import { registeredProtoModules } from "@spine-event-engine/proto/codegen";
|
|
18
20
|
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
21
|
export { Time } from "./time/index.js";
|
|
20
22
|
export { EntityColumn, } from "./entity/entity-column.js";
|
|
@@ -314,16 +316,19 @@ export const Validate = {
|
|
|
314
316
|
* @typeParam Schema Generated schema of the message being validated.
|
|
315
317
|
* @param schema The message schema.
|
|
316
318
|
* @param message The message to validate.
|
|
319
|
+
* @param registry Optional type lookup overriding the registered Proto-module inventory.
|
|
317
320
|
* @returns The sanitized validation result.
|
|
321
|
+
* @throws Error if the validator or its configuration fails.
|
|
318
322
|
*/
|
|
319
|
-
message(schema, message) {
|
|
323
|
+
message(schema, message, registry) {
|
|
320
324
|
try {
|
|
321
|
-
return ValidationResults.from(
|
|
325
|
+
return ValidationResults.from([
|
|
326
|
+
...validateWithSpine(schema, RegistryValidation.view(schema, message)),
|
|
327
|
+
...RegistryValidation.collect(schema, message, registry ?? GeneratedDescriptors.types()),
|
|
328
|
+
].map((violation) => ValidationResults.violation(violation)));
|
|
322
329
|
}
|
|
323
330
|
catch {
|
|
324
|
-
|
|
325
|
-
ValidationResults.failure(schema.typeName, VALIDATION_RUNTIME_FAILURE_MESSAGE),
|
|
326
|
-
]);
|
|
331
|
+
throw new Error(VALIDATION_RUNTIME_FAILURE_MESSAGE);
|
|
327
332
|
}
|
|
328
333
|
},
|
|
329
334
|
/**
|
|
@@ -331,10 +336,12 @@ export const Validate = {
|
|
|
331
336
|
* @typeParam Schema Generated schema of the message being validated.
|
|
332
337
|
* @param schema The message schema.
|
|
333
338
|
* @param message The message to validate.
|
|
339
|
+
* @param registry Optional type lookup overriding the registered Proto-module inventory.
|
|
334
340
|
* @returns The validated message.
|
|
341
|
+
* @throws ValidationException for field constraints, or Error for validator failures.
|
|
335
342
|
*/
|
|
336
|
-
check(schema, message) {
|
|
337
|
-
const result = Validate.message(schema, message);
|
|
343
|
+
check(schema, message, registry) {
|
|
344
|
+
const result = Validate.message(schema, message, registry);
|
|
338
345
|
if (!result.valid)
|
|
339
346
|
throw new ValidationException(result.error);
|
|
340
347
|
return message;
|
|
@@ -418,34 +425,70 @@ export const TypeUrls = {
|
|
|
418
425
|
};
|
|
419
426
|
Object.freeze(TypeUrls);
|
|
420
427
|
/**
|
|
421
|
-
* Packs and unpacks
|
|
428
|
+
* Packs and unpacks Protobuf messages in `google.protobuf.Any` envelopes.
|
|
422
429
|
*/
|
|
423
|
-
|
|
424
|
-
|
|
430
|
+
// eslint-disable-next-line @typescript-eslint/no-extraneous-class -- Static overloads mirror the JVM utility API.
|
|
431
|
+
export class AnyPacker {
|
|
425
432
|
/**
|
|
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.
|
|
433
|
+
* Creates no public instances of this utility class.
|
|
432
434
|
*/
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
435
|
+
constructor() {
|
|
436
|
+
throw new Error("AnyPacker cannot be constructed.");
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Packs a typed message or returns an existing Any unchanged.
|
|
440
|
+
* @param schemaOrPacked Generated schema or existing Any message.
|
|
441
|
+
* @param message Message to pack when a schema is supplied.
|
|
442
|
+
* @returns Packed Any message.
|
|
443
|
+
*/
|
|
444
|
+
static pack(schemaOrPacked, message) {
|
|
445
|
+
if ("$typeName" in schemaOrPacked)
|
|
446
|
+
return schemaOrPacked;
|
|
447
|
+
if (message === undefined)
|
|
448
|
+
throw new TypeError("AnyPacker.pack requires a message.");
|
|
449
|
+
const schema = schemaOrPacked;
|
|
450
|
+
if (schema.typeName === AnySchema.typeName && message.$typeName === AnySchema.typeName)
|
|
451
|
+
return message;
|
|
436
452
|
return create(AnySchema, {
|
|
437
453
|
typeUrl: TypeUrls.derive(schema),
|
|
438
|
-
value: toBinary(schema, message
|
|
454
|
+
value: toBinary(schema, message),
|
|
439
455
|
});
|
|
440
|
-
}
|
|
456
|
+
}
|
|
441
457
|
/**
|
|
442
458
|
* Unpacks an `Any` when its type URL exactly matches the requested schema.
|
|
443
459
|
* @typeParam Schema Generated schema expected in the envelope.
|
|
444
460
|
* @param packed The packed message.
|
|
445
461
|
* @param schema The expected schema.
|
|
446
|
-
* @returns The
|
|
462
|
+
* @returns The decoded message.
|
|
463
|
+
*/
|
|
464
|
+
static unpack(packed, schema) {
|
|
465
|
+
if (packed.typeUrl !== TypeUrls.derive(schema)) {
|
|
466
|
+
throw new TypeError(`Expected Any type URL "${TypeUrls.derive(schema)}", got "${packed.typeUrl}".`);
|
|
467
|
+
}
|
|
468
|
+
return fromBinary(schema, packed.value);
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Unpacks an `Any` when its exact type URL is registered.
|
|
472
|
+
* @param registry The schema registry.
|
|
473
|
+
* @param packed The packed message.
|
|
474
|
+
* @returns The decoded message.
|
|
475
|
+
*/
|
|
476
|
+
static unpackUsing(registry, packed) {
|
|
477
|
+
const metadata = registry.findByTypeUrl(packed.typeUrl);
|
|
478
|
+
if (metadata === undefined)
|
|
479
|
+
throw new TypeError(`Unregistered Any type URL "${packed.typeUrl}".`);
|
|
480
|
+
return fromBinary(metadata.schema, packed.value);
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Decodes a matching `Any`, returning `undefined` for mismatch or malformed bytes.
|
|
484
|
+
* @typeParam Schema Expected generated message schema.
|
|
485
|
+
* @param packed Packed message to decode, or absent message.
|
|
486
|
+
* @param schema Expected message schema.
|
|
487
|
+
* @returns Decoded message or `undefined`.
|
|
447
488
|
*/
|
|
448
|
-
|
|
489
|
+
static tryUnpack(packed, schema) {
|
|
490
|
+
if (packed === undefined)
|
|
491
|
+
return undefined;
|
|
449
492
|
if (packed.typeUrl !== TypeUrls.derive(schema))
|
|
450
493
|
return undefined;
|
|
451
494
|
try {
|
|
@@ -454,14 +497,16 @@ export const AnyMessages = {
|
|
|
454
497
|
catch {
|
|
455
498
|
return undefined;
|
|
456
499
|
}
|
|
457
|
-
}
|
|
500
|
+
}
|
|
458
501
|
/**
|
|
459
|
-
*
|
|
460
|
-
* @param registry
|
|
461
|
-
* @param packed
|
|
462
|
-
* @returns
|
|
502
|
+
* Decodes an exactly registered `Any` when available and well formed.
|
|
503
|
+
* @param registry Generated schema registry.
|
|
504
|
+
* @param packed Packed message to decode, or absent message.
|
|
505
|
+
* @returns Decoded message or `undefined`.
|
|
463
506
|
*/
|
|
464
|
-
|
|
507
|
+
static tryUnpackUsing(registry, packed) {
|
|
508
|
+
if (packed === undefined)
|
|
509
|
+
return undefined;
|
|
465
510
|
const metadata = registry.findByTypeUrl(packed.typeUrl);
|
|
466
511
|
if (metadata === undefined)
|
|
467
512
|
return undefined;
|
|
@@ -471,34 +516,44 @@ export const AnyMessages = {
|
|
|
471
516
|
catch {
|
|
472
517
|
return undefined;
|
|
473
518
|
}
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
Object.freeze(
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
Object.freeze(AnyPacker);
|
|
477
522
|
function isProtobufRegistry(types) {
|
|
478
523
|
return "kind" in types;
|
|
479
524
|
}
|
|
525
|
+
const RegistrySources = Object.freeze({
|
|
526
|
+
/**
|
|
527
|
+
* Resolves a fixed or late-bound Protobuf descriptor registry.
|
|
528
|
+
*
|
|
529
|
+
* @param source Registry value or provider.
|
|
530
|
+
* @returns The current registry, if available.
|
|
531
|
+
*/
|
|
532
|
+
current(source) {
|
|
533
|
+
return typeof source === "function" ? source() : source;
|
|
534
|
+
},
|
|
535
|
+
});
|
|
480
536
|
/**
|
|
481
537
|
* Creates reversible compact Proto JSON conversion for a generated message.
|
|
482
538
|
*
|
|
483
539
|
* @typeParam Schema Generated schema used for conversion.
|
|
484
540
|
* @param schema Message schema used for Proto JSON.
|
|
485
|
-
* @param
|
|
486
|
-
* @param typeUrls Canonical URLs restored on parsed Any messages.
|
|
541
|
+
* @param registrySource Registry or late-bound registry for expanding packed Any messages.
|
|
487
542
|
* @returns Schema-bound stringifier.
|
|
488
543
|
*/
|
|
489
|
-
function defaultMessageStringifier(schema,
|
|
544
|
+
function defaultMessageStringifier(schema, registrySource) {
|
|
490
545
|
return Object.freeze({
|
|
491
546
|
/**
|
|
492
|
-
* Parses compact Proto JSON and restores
|
|
547
|
+
* Parses compact Proto JSON and restores each original Any type URL.
|
|
493
548
|
*
|
|
494
549
|
* @param value Compact Proto JSON text.
|
|
495
550
|
* @returns Parsed generated message.
|
|
496
551
|
*/
|
|
497
552
|
fromString(value) {
|
|
498
|
-
const
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
553
|
+
const registry = RegistrySources.current(registrySource);
|
|
554
|
+
const options = registry === undefined ? undefined : { registry };
|
|
555
|
+
const message = fromJsonString(schema, value, options);
|
|
556
|
+
AnyJsonUrls.restore(schema, message, JSON.parse(value), registry);
|
|
502
557
|
return message;
|
|
503
558
|
},
|
|
504
559
|
/**
|
|
@@ -508,32 +563,131 @@ function defaultMessageStringifier(schema, registry, typeUrls) {
|
|
|
508
563
|
* @returns Compact Proto JSON text.
|
|
509
564
|
*/
|
|
510
565
|
toString(value) {
|
|
566
|
+
const registry = RegistrySources.current(registrySource);
|
|
511
567
|
return registry === undefined
|
|
512
568
|
? toJsonString(schema, value)
|
|
513
569
|
: toJsonString(schema, value, { registry });
|
|
514
570
|
},
|
|
515
571
|
});
|
|
516
572
|
}
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
573
|
+
const AnyJsonUrls = Object.freeze({
|
|
574
|
+
/**
|
|
575
|
+
* WKT payloads represented under an Any JSON `value` key.
|
|
576
|
+
*/
|
|
577
|
+
customJsonTypes: new Set([
|
|
578
|
+
"google.protobuf.Any",
|
|
579
|
+
"google.protobuf.Timestamp",
|
|
580
|
+
"google.protobuf.Duration",
|
|
581
|
+
"google.protobuf.FieldMask",
|
|
582
|
+
"google.protobuf.Struct",
|
|
583
|
+
"google.protobuf.Value",
|
|
584
|
+
"google.protobuf.ListValue",
|
|
585
|
+
"google.protobuf.DoubleValue",
|
|
586
|
+
"google.protobuf.FloatValue",
|
|
587
|
+
"google.protobuf.Int64Value",
|
|
588
|
+
"google.protobuf.UInt64Value",
|
|
589
|
+
"google.protobuf.Int32Value",
|
|
590
|
+
"google.protobuf.UInt32Value",
|
|
591
|
+
"google.protobuf.BoolValue",
|
|
592
|
+
"google.protobuf.StringValue",
|
|
593
|
+
"google.protobuf.BytesValue",
|
|
594
|
+
]),
|
|
595
|
+
/**
|
|
596
|
+
* Restores original Any URLs by following real Protobuf message fields.
|
|
597
|
+
*
|
|
598
|
+
* @param schema Current message descriptor.
|
|
599
|
+
* @param message Decoded current message.
|
|
600
|
+
* @param json Corresponding Proto JSON value.
|
|
601
|
+
* @param registry Descriptor registry for packed messages.
|
|
602
|
+
* @returns Whether any URL in this message changed.
|
|
603
|
+
*/
|
|
604
|
+
restore(schema, message, json, registry) {
|
|
605
|
+
if (typeof json !== "object" || json === null || Array.isArray(json))
|
|
606
|
+
return false;
|
|
607
|
+
const source = json;
|
|
608
|
+
if (schema.typeName === AnySchema.typeName)
|
|
609
|
+
return this.any(message, source, registry);
|
|
610
|
+
if (["google.protobuf.Struct", "google.protobuf.Value", "google.protobuf.ListValue"].includes(schema.typeName))
|
|
611
|
+
return false;
|
|
612
|
+
let changed = false;
|
|
613
|
+
for (const field of schema.fields) {
|
|
614
|
+
const value = source[field.jsonName] ?? source[field.name];
|
|
615
|
+
if (value === undefined)
|
|
616
|
+
continue;
|
|
617
|
+
changed = this.field(field, message, value, registry) || changed;
|
|
532
618
|
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
619
|
+
return changed;
|
|
620
|
+
},
|
|
621
|
+
/**
|
|
622
|
+
* Restores one Any and descends into its parsed packed payload.
|
|
623
|
+
* @param message Decoded Any message.
|
|
624
|
+
* @param source Its Proto JSON value.
|
|
625
|
+
* @param registry Descriptor registry for the payload.
|
|
626
|
+
* @returns Whether its URL or nested payload changed.
|
|
627
|
+
*/
|
|
628
|
+
any(message, source, registry) {
|
|
629
|
+
const typeUrl = source["@type"];
|
|
630
|
+
if (typeof typeUrl !== "string")
|
|
631
|
+
return false;
|
|
632
|
+
const changedUrl = message.typeUrl !== typeUrl;
|
|
633
|
+
message.typeUrl = typeUrl;
|
|
634
|
+
const schema = registry?.getMessage(typeUrl.slice(typeUrl.lastIndexOf("/") + 1));
|
|
635
|
+
if (schema === undefined)
|
|
636
|
+
return changedUrl;
|
|
637
|
+
const payload = fromBinary(schema, message.value);
|
|
638
|
+
const json = this.customJsonTypes.has(schema.typeName) ? source.value : source;
|
|
639
|
+
const changedPayload = this.restore(schema, payload, json, registry);
|
|
640
|
+
if (changedPayload)
|
|
641
|
+
message.value = toBinary(schema, payload);
|
|
642
|
+
return changedUrl || changedPayload;
|
|
643
|
+
},
|
|
644
|
+
/**
|
|
645
|
+
* Restores nested message, list, map, or selected oneof field values.
|
|
646
|
+
* @param field Current message field descriptor.
|
|
647
|
+
* @param parent Decoded parent message.
|
|
648
|
+
* @param json Corresponding field JSON.
|
|
649
|
+
* @param registry Descriptor registry for packed messages.
|
|
650
|
+
* @returns Whether any nested URL changed.
|
|
651
|
+
*/
|
|
652
|
+
field(field, parent, json, registry) {
|
|
653
|
+
const value = this.selected(field, parent);
|
|
654
|
+
if (value === undefined)
|
|
655
|
+
return false;
|
|
656
|
+
if (field.fieldKind === "message")
|
|
657
|
+
return this.restore(field.message, value, json, registry);
|
|
658
|
+
if (field.fieldKind === "list" && field.listKind === "message" && Array.isArray(json))
|
|
659
|
+
return json
|
|
660
|
+
.map((item, index) => {
|
|
661
|
+
const nested = value[index];
|
|
662
|
+
return nested === undefined ? false : this.restore(field.message, nested, item, registry);
|
|
663
|
+
})
|
|
664
|
+
.some(Boolean);
|
|
665
|
+
if (field.fieldKind === "map" &&
|
|
666
|
+
field.mapKind === "message" &&
|
|
667
|
+
typeof json === "object" &&
|
|
668
|
+
json !== null)
|
|
669
|
+
return Object.entries(json)
|
|
670
|
+
.map(([key, item]) => {
|
|
671
|
+
const nested = value[key];
|
|
672
|
+
return nested === undefined ? false : this.restore(field.message, nested, item, registry);
|
|
673
|
+
})
|
|
674
|
+
.some(Boolean);
|
|
675
|
+
return false;
|
|
676
|
+
},
|
|
677
|
+
/**
|
|
678
|
+
* Returns a field's decoded value, respecting oneof cases.
|
|
679
|
+
* @param field Field descriptor.
|
|
680
|
+
* @param parent Decoded parent message.
|
|
681
|
+
* @returns Selected value, if set.
|
|
682
|
+
*/
|
|
683
|
+
selected(field, parent) {
|
|
684
|
+
const source = parent;
|
|
685
|
+
if (field.oneof === undefined)
|
|
686
|
+
return source[field.localName];
|
|
687
|
+
const oneof = source[field.oneof.localName];
|
|
688
|
+
return oneof?.case === field.localName ? oneof.value : undefined;
|
|
689
|
+
},
|
|
690
|
+
});
|
|
537
691
|
const FieldStringifiers = Object.freeze({
|
|
538
692
|
/**
|
|
539
693
|
* Returns reversible conversion for one singular message, enum, or scalar field.
|
|
@@ -898,10 +1052,7 @@ export const Stringifiers = {
|
|
|
898
1052
|
: isProtobufRegistry(types)
|
|
899
1053
|
? types
|
|
900
1054
|
: 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);
|
|
1055
|
+
return defaultMessageStringifier(schema, registry);
|
|
905
1056
|
},
|
|
906
1057
|
/**
|
|
907
1058
|
* Creates a reversible stringifier for one supported singular field.
|
|
@@ -924,8 +1075,8 @@ Object.freeze(Stringifiers);
|
|
|
924
1075
|
*/
|
|
925
1076
|
export class StringifierRegistry {
|
|
926
1077
|
#registered = new Map();
|
|
1078
|
+
#selected = new Set();
|
|
927
1079
|
#types;
|
|
928
|
-
#typeUrls = new Map();
|
|
929
1080
|
/**
|
|
930
1081
|
* Creates an empty registry or a snapshot of another registry.
|
|
931
1082
|
*
|
|
@@ -937,7 +1088,6 @@ export class StringifierRegistry {
|
|
|
937
1088
|
this.#registered.set(typeName, stringifier);
|
|
938
1089
|
}
|
|
939
1090
|
this.#types = source.#types;
|
|
940
|
-
this.#typeUrls = new Map(source.#typeUrls);
|
|
941
1091
|
}
|
|
942
1092
|
}
|
|
943
1093
|
/**
|
|
@@ -947,6 +1097,10 @@ export class StringifierRegistry {
|
|
|
947
1097
|
* @param stringifier The reversible stringifier.
|
|
948
1098
|
*/
|
|
949
1099
|
register(schema, stringifier) {
|
|
1100
|
+
if (this === defaultStringifierRegistry &&
|
|
1101
|
+
this.#selected.has(schema.typeName) &&
|
|
1102
|
+
this.#registered.get(schema.typeName) !== stringifier)
|
|
1103
|
+
throw new Error(`Stringifier choice is already in use for ${schema.typeName}.`);
|
|
950
1104
|
this.#registered.set(schema.typeName, stringifier);
|
|
951
1105
|
}
|
|
952
1106
|
/**
|
|
@@ -955,9 +1109,10 @@ export class StringifierRegistry {
|
|
|
955
1109
|
* @param types Resolves message types embedded in `Any` values.
|
|
956
1110
|
*/
|
|
957
1111
|
setTypeRegistry(types) {
|
|
1112
|
+
if (this === defaultStringifierRegistry)
|
|
1113
|
+
throw new Error("The shared stringifier registry uses generated module descriptors.");
|
|
958
1114
|
const metadata = types.list();
|
|
959
1115
|
this.#types = createRegistry(...metadata.map((item) => item.descriptor));
|
|
960
|
-
this.#typeUrls = new Map(metadata.map((item) => [item.schema.typeName, item.typeUrl]));
|
|
961
1116
|
}
|
|
962
1117
|
/**
|
|
963
1118
|
* Returns the custom stringifier or the default compact Proto JSON mapping.
|
|
@@ -967,8 +1122,10 @@ export class StringifierRegistry {
|
|
|
967
1122
|
*/
|
|
968
1123
|
forMessage(schema) {
|
|
969
1124
|
const registered = this.#registered.get(schema.typeName);
|
|
1125
|
+
if (this === defaultStringifierRegistry)
|
|
1126
|
+
this.#selected.add(schema.typeName);
|
|
970
1127
|
return registered === undefined
|
|
971
|
-
? defaultMessageStringifier(schema, this.#types
|
|
1128
|
+
? defaultMessageStringifier(schema, () => this.#types ?? GeneratedDescriptors.current())
|
|
972
1129
|
: registered;
|
|
973
1130
|
}
|
|
974
1131
|
/**
|
|
@@ -986,6 +1143,41 @@ export class StringifierRegistry {
|
|
|
986
1143
|
return FieldStringifiers.create(field, (schema) => this.forMessage(schema));
|
|
987
1144
|
}
|
|
988
1145
|
}
|
|
1146
|
+
let descriptorSnapshot;
|
|
1147
|
+
let descriptorRegistry;
|
|
1148
|
+
let descriptorTypes;
|
|
1149
|
+
/**
|
|
1150
|
+
* Reuses descriptor composition until another generated module registers.
|
|
1151
|
+
*/
|
|
1152
|
+
const GeneratedDescriptors = Object.freeze({
|
|
1153
|
+
/**
|
|
1154
|
+
* Resolves descriptors from the current generated-module inventory.
|
|
1155
|
+
*
|
|
1156
|
+
* @returns The composed registry, if available.
|
|
1157
|
+
*/
|
|
1158
|
+
current() {
|
|
1159
|
+
const current = registeredProtoModules();
|
|
1160
|
+
if (current !== descriptorSnapshot) {
|
|
1161
|
+
const types = TypeRegistry.from(...current);
|
|
1162
|
+
descriptorRegistry = createRegistry(...types.list().map((item) => item.descriptor));
|
|
1163
|
+
descriptorTypes = types;
|
|
1164
|
+
descriptorSnapshot = current;
|
|
1165
|
+
}
|
|
1166
|
+
return descriptorRegistry;
|
|
1167
|
+
},
|
|
1168
|
+
/**
|
|
1169
|
+
* Resolves generated types for annotated packed-message validation.
|
|
1170
|
+
* @returns The current module-derived type lookup.
|
|
1171
|
+
*/
|
|
1172
|
+
types() {
|
|
1173
|
+
GeneratedDescriptors.current();
|
|
1174
|
+
return descriptorTypes;
|
|
1175
|
+
},
|
|
1176
|
+
});
|
|
1177
|
+
/**
|
|
1178
|
+
* Shared converter policy for application storage factories and mappings.
|
|
1179
|
+
*/
|
|
1180
|
+
export const defaultStringifierRegistry = new StringifierRegistry();
|
|
989
1181
|
/**
|
|
990
1182
|
* Packs and unpacks the identifier types supported by Spine JVM storage.
|
|
991
1183
|
*/
|
|
@@ -1030,33 +1222,37 @@ const IdentifierValues = Object.freeze({
|
|
|
1030
1222
|
return value;
|
|
1031
1223
|
},
|
|
1032
1224
|
});
|
|
1225
|
+
/**
|
|
1226
|
+
* Implements packing for registered message and primitive identifier kinds.
|
|
1227
|
+
*/
|
|
1033
1228
|
function packIdentifier(type, value) {
|
|
1034
1229
|
if (typeof type !== "string") {
|
|
1035
|
-
return
|
|
1230
|
+
return AnyPacker.pack(type, value);
|
|
1036
1231
|
}
|
|
1037
1232
|
switch (type) {
|
|
1038
1233
|
case "string":
|
|
1039
1234
|
if (typeof value !== "string")
|
|
1040
1235
|
throw new TypeError("Identifier must be a string.");
|
|
1041
|
-
return
|
|
1042
|
-
validate: false,
|
|
1043
|
-
});
|
|
1236
|
+
return AnyPacker.pack(StringValueSchema, create(StringValueSchema, { value }));
|
|
1044
1237
|
case "int32":
|
|
1045
|
-
return
|
|
1238
|
+
return AnyPacker.pack(Int32ValueSchema, create(Int32ValueSchema, { value: IdentifierValues.int32(value) }));
|
|
1046
1239
|
case "int64":
|
|
1047
|
-
return
|
|
1240
|
+
return AnyPacker.pack(Int64ValueSchema, create(Int64ValueSchema, { value: IdentifierValues.int64(value) }));
|
|
1048
1241
|
}
|
|
1049
1242
|
}
|
|
1243
|
+
/**
|
|
1244
|
+
* Implements decoding for registered message and primitive identifier kinds.
|
|
1245
|
+
*/
|
|
1050
1246
|
function unpackIdentifier(type, value) {
|
|
1051
1247
|
if (typeof type !== "string")
|
|
1052
|
-
return
|
|
1248
|
+
return type.typeName === AnySchema.typeName ? value : AnyPacker.tryUnpack(value, type);
|
|
1053
1249
|
switch (type) {
|
|
1054
1250
|
case "string":
|
|
1055
|
-
return
|
|
1251
|
+
return AnyPacker.tryUnpack(value, StringValueSchema)?.value;
|
|
1056
1252
|
case "int32":
|
|
1057
|
-
return
|
|
1253
|
+
return AnyPacker.tryUnpack(value, Int32ValueSchema)?.value;
|
|
1058
1254
|
case "int64":
|
|
1059
|
-
return
|
|
1255
|
+
return AnyPacker.tryUnpack(value, Int64ValueSchema)?.value;
|
|
1060
1256
|
}
|
|
1061
1257
|
}
|
|
1062
1258
|
function freshSignalId() {
|
|
@@ -1078,6 +1274,22 @@ function freshSignalId() {
|
|
|
1078
1274
|
const hex = [...bytes].map((value) => value.toString(16).padStart(2, "0")).join("");
|
|
1079
1275
|
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
1080
1276
|
}
|
|
1277
|
+
/**
|
|
1278
|
+
* Keeps signal validation at the semantic envelope boundary.
|
|
1279
|
+
*/
|
|
1280
|
+
const SignalPacking = Object.freeze({
|
|
1281
|
+
/**
|
|
1282
|
+
* Validates and packs a domain signal.
|
|
1283
|
+
* @typeParam Schema Generated signal schema.
|
|
1284
|
+
* @param input Domain message and validation setting.
|
|
1285
|
+
* @returns Packed signal message.
|
|
1286
|
+
*/
|
|
1287
|
+
pack(input) {
|
|
1288
|
+
if (input.validate !== false)
|
|
1289
|
+
Validate.check(input.schema, input.message);
|
|
1290
|
+
return AnyPacker.pack(input.schema, input.message);
|
|
1291
|
+
},
|
|
1292
|
+
});
|
|
1081
1293
|
/**
|
|
1082
1294
|
* Creates generated Spine command and event envelopes.
|
|
1083
1295
|
*/
|
|
@@ -1092,7 +1304,7 @@ export const SignalEnvelopes = {
|
|
|
1092
1304
|
command(input) {
|
|
1093
1305
|
return create(CommandSchema, {
|
|
1094
1306
|
id: create(CommandIdSchema, { uuid: freshSignalId() }),
|
|
1095
|
-
message:
|
|
1307
|
+
message: SignalPacking.pack(input),
|
|
1096
1308
|
context: clone(CommandContextSchema, input.context),
|
|
1097
1309
|
});
|
|
1098
1310
|
},
|
|
@@ -1105,7 +1317,7 @@ export const SignalEnvelopes = {
|
|
|
1105
1317
|
event(input) {
|
|
1106
1318
|
return create(EventSchema, {
|
|
1107
1319
|
id: create(EventIdSchema, { value: freshSignalId() }),
|
|
1108
|
-
message:
|
|
1320
|
+
message: SignalPacking.pack(input),
|
|
1109
1321
|
context: clone(EventContextSchema, input.context),
|
|
1110
1322
|
});
|
|
1111
1323
|
},
|
|
@@ -1119,7 +1331,7 @@ export const SignalEnvelopes = {
|
|
|
1119
1331
|
throw new TypeError("A Command origin requires a non-empty Command ID.");
|
|
1120
1332
|
return create(OriginSchema, {
|
|
1121
1333
|
message: create(MessageIdSchema, {
|
|
1122
|
-
id:
|
|
1334
|
+
id: AnyPacker.pack(CommandIdSchema, command.id),
|
|
1123
1335
|
typeUrl: command.message?.typeUrl ?? "",
|
|
1124
1336
|
}),
|
|
1125
1337
|
...(command.context?.actorContext === undefined
|