@kinotic-ai/core 2.0.0 → 3.0.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/index.cjs +95 -29
- package/dist/index.d.cts +60 -11
- package/dist/index.d.ts +60 -11
- package/dist/index.js +95 -29
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -64,9 +64,12 @@ var __export = (target, all) => {
|
|
|
64
64
|
// packages/core/src/index.ts
|
|
65
65
|
var exports_src = {};
|
|
66
66
|
__export(exports_src, {
|
|
67
|
+
validateZone: () => validateZone,
|
|
68
|
+
validateLabel: () => validateLabel,
|
|
67
69
|
receivesContext: () => receivesContext,
|
|
68
70
|
createCRI: () => createCRI,
|
|
69
71
|
createAuthenticatedWebSocketFactory: () => createAuthenticatedWebSocketFactory,
|
|
72
|
+
Zones: () => Zones,
|
|
70
73
|
Version: () => Version,
|
|
71
74
|
TextEventFactory: () => TextEventFactory,
|
|
72
75
|
Sort: () => Sort,
|
|
@@ -783,27 +786,61 @@ function createDebugLogger(namespace) {
|
|
|
783
786
|
class ServiceIdentifier {
|
|
784
787
|
namespace;
|
|
785
788
|
name;
|
|
789
|
+
zones;
|
|
786
790
|
scope;
|
|
787
791
|
version;
|
|
788
|
-
|
|
789
|
-
constructor(namespace, name) {
|
|
792
|
+
_cris = null;
|
|
793
|
+
constructor(namespace, name, zones) {
|
|
790
794
|
this.namespace = namespace;
|
|
791
795
|
this.name = name;
|
|
796
|
+
this.zones = zones;
|
|
792
797
|
}
|
|
793
798
|
qualifiedName() {
|
|
794
|
-
|
|
799
|
+
if (this.zones && this.zones.length > 1) {
|
|
800
|
+
throw new Error(`ServiceIdentifier has ${this.zones.length} zones so there is no single qualifiedName`);
|
|
801
|
+
}
|
|
802
|
+
return this.qualifiedNameFor(this.zones?.[0]);
|
|
803
|
+
}
|
|
804
|
+
cris() {
|
|
805
|
+
if (this._cris == null) {
|
|
806
|
+
const zones = this.zones && this.zones.length > 0 ? this.zones : [undefined];
|
|
807
|
+
this._cris = zones.map((zone) => createCRI("srv" /* SERVICE_DESTINATION_SCHEME */, this.scope || null, this.qualifiedNameFor(zone), null, this.version || null));
|
|
808
|
+
}
|
|
809
|
+
return this._cris;
|
|
795
810
|
}
|
|
796
811
|
cri() {
|
|
797
|
-
|
|
798
|
-
|
|
812
|
+
const cris = this.cris();
|
|
813
|
+
const cri = cris[0];
|
|
814
|
+
if (cris.length !== 1 || !cri) {
|
|
815
|
+
throw new Error(`ServiceIdentifier has ${cris.length} cris so there is no single cri`);
|
|
799
816
|
}
|
|
800
|
-
return
|
|
817
|
+
return cri;
|
|
818
|
+
}
|
|
819
|
+
qualifiedNameFor(zone) {
|
|
820
|
+
const name = this.namespace ? this.namespace + "." + this.name : this.name;
|
|
821
|
+
return zone ? zone + "." + name : name;
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
// packages/core/src/api/ZoneUtil.ts
|
|
826
|
+
var LABEL = "[a-z0-9]([a-z0-9_-]*[a-z0-9])?";
|
|
827
|
+
var LABEL_PATTERN = new RegExp(`^${LABEL}$`);
|
|
828
|
+
var ZONE_PATTERN = new RegExp(`^${LABEL}(\\.${LABEL})*$`);
|
|
829
|
+
function validateZone(zone) {
|
|
830
|
+
if (!zone || !ZONE_PATTERN.test(zone)) {
|
|
831
|
+
throw new Error(`Invalid zone '${zone}'. Zones must be dot separated labels of lowercase letters, digits, and interior dashes or underscores`);
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
function validateLabel(label) {
|
|
835
|
+
if (!label || !LABEL_PATTERN.test(label)) {
|
|
836
|
+
throw new Error(`Invalid zone label '${label}'. Labels must be lowercase letters, digits, and interior dashes or underscores`);
|
|
801
837
|
}
|
|
802
838
|
}
|
|
803
839
|
|
|
804
840
|
// packages/core/src/api/KinoticDecorators.ts
|
|
805
841
|
var scopeFunctions = new WeakSet;
|
|
806
842
|
var versionRegistry = new WeakMap;
|
|
843
|
+
var zonesRegistry = new WeakMap;
|
|
807
844
|
var contextMarkedFunctions = new WeakSet;
|
|
808
845
|
function findInConstructorChain(registry, constructor) {
|
|
809
846
|
let current = constructor;
|
|
@@ -846,6 +883,14 @@ function Version(version) {
|
|
|
846
883
|
versionRegistry.set(value, version);
|
|
847
884
|
};
|
|
848
885
|
}
|
|
886
|
+
function Zones(zones) {
|
|
887
|
+
for (const zone of zones) {
|
|
888
|
+
validateZone(zone);
|
|
889
|
+
}
|
|
890
|
+
return function(value, _context) {
|
|
891
|
+
zonesRegistry.set(value, zones);
|
|
892
|
+
};
|
|
893
|
+
}
|
|
849
894
|
function Context(value, _context) {
|
|
850
895
|
contextMarkedFunctions.add(value);
|
|
851
896
|
}
|
|
@@ -853,12 +898,29 @@ function receivesContext(serviceInstance, methodName) {
|
|
|
853
898
|
const method = serviceInstance[methodName];
|
|
854
899
|
return typeof method === "function" && contextMarkedFunctions.has(method);
|
|
855
900
|
}
|
|
901
|
+
function resolveEffectiveZones(constructor) {
|
|
902
|
+
const declaredZones = findInConstructorChain(zonesRegistry, constructor) ?? Kinotic.defaultZones ?? [];
|
|
903
|
+
const prefix = Kinotic.zonePrefix;
|
|
904
|
+
let effectiveZones;
|
|
905
|
+
if (prefix != null && declaredZones.length > 0) {
|
|
906
|
+
effectiveZones = declaredZones.map((zone) => `${prefix}.${zone}`);
|
|
907
|
+
} else if (prefix != null) {
|
|
908
|
+
effectiveZones = [prefix];
|
|
909
|
+
} else {
|
|
910
|
+
effectiveZones = [...declaredZones];
|
|
911
|
+
}
|
|
912
|
+
for (const zone of effectiveZones) {
|
|
913
|
+
validateZone(zone);
|
|
914
|
+
}
|
|
915
|
+
return effectiveZones;
|
|
916
|
+
}
|
|
856
917
|
function Publish(namespace, name) {
|
|
857
918
|
return function(value, _context) {
|
|
858
919
|
return class extends value {
|
|
859
920
|
constructor(...args) {
|
|
860
921
|
super(...args);
|
|
861
|
-
const
|
|
922
|
+
const zones = resolveEffectiveZones(this.constructor);
|
|
923
|
+
const serviceIdentifier = new ServiceIdentifier(namespace ?? null, name || value.name, zones.length > 0 ? zones : undefined);
|
|
862
924
|
const version = findInConstructorChain(versionRegistry, this.constructor);
|
|
863
925
|
if (version) {
|
|
864
926
|
serviceIdentifier.version = version;
|
|
@@ -883,7 +945,7 @@ class ServiceInvocationSupervisor {
|
|
|
883
945
|
returnValueConverter;
|
|
884
946
|
serviceIdentifier;
|
|
885
947
|
serviceInstance;
|
|
886
|
-
|
|
948
|
+
methodSubscriptions = [];
|
|
887
949
|
methodMap;
|
|
888
950
|
constructor(serviceIdentifier, serviceInstance, eventBusService, interceptorProvider, options = {}) {
|
|
889
951
|
if (!serviceIdentifier)
|
|
@@ -920,31 +982,33 @@ class ServiceInvocationSupervisor {
|
|
|
920
982
|
throw new Error("Service already started");
|
|
921
983
|
}
|
|
922
984
|
this.active = true;
|
|
923
|
-
const
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
985
|
+
for (const cri of this.serviceIdentifier.cris()) {
|
|
986
|
+
const criBase = cri.baseResource();
|
|
987
|
+
this.methodSubscriptions.push(this._eventBus.observe(criBase).subscribe({
|
|
988
|
+
next: async (event) => {
|
|
989
|
+
await this.processEvent(event);
|
|
990
|
+
},
|
|
991
|
+
error: (error) => {
|
|
992
|
+
this.log.error("Event listener error", error);
|
|
993
|
+
this.active = false;
|
|
994
|
+
},
|
|
995
|
+
complete: () => {
|
|
996
|
+
this.log.error("Event listener stopped unexpectedly. Setting supervisor inactive.");
|
|
997
|
+
this.active = false;
|
|
998
|
+
}
|
|
999
|
+
}));
|
|
1000
|
+
this.log.info(`ServiceInvocationSupervisor started for ${criBase}`);
|
|
1001
|
+
}
|
|
938
1002
|
}
|
|
939
1003
|
stop() {
|
|
940
1004
|
if (!this.active) {
|
|
941
1005
|
throw new Error("Service already stopped");
|
|
942
1006
|
}
|
|
943
1007
|
this.active = false;
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
this.methodSubscription = null;
|
|
1008
|
+
for (const subscription of this.methodSubscriptions) {
|
|
1009
|
+
subscription.unsubscribe();
|
|
947
1010
|
}
|
|
1011
|
+
this.methodSubscriptions = [];
|
|
948
1012
|
this.log.info("ServiceInvocationSupervisor stopped");
|
|
949
1013
|
}
|
|
950
1014
|
buildMethodMap(serviceInstance) {
|
|
@@ -1064,7 +1128,7 @@ var import_operators2 = require("rxjs/operators");
|
|
|
1064
1128
|
// packages/core/package.json
|
|
1065
1129
|
var package_default = {
|
|
1066
1130
|
name: "@kinotic-ai/core",
|
|
1067
|
-
version: "
|
|
1131
|
+
version: "3.0.0",
|
|
1068
1132
|
type: "module",
|
|
1069
1133
|
files: [
|
|
1070
1134
|
"dist"
|
|
@@ -1193,7 +1257,7 @@ class ServiceRegistry {
|
|
|
1193
1257
|
return new ServiceProxy(serviceIdentifier, this);
|
|
1194
1258
|
}
|
|
1195
1259
|
register(serviceIdentifier, service) {
|
|
1196
|
-
const criString = serviceIdentifier.
|
|
1260
|
+
const criString = serviceIdentifier.cris().map((cri) => cri.raw()).join(", ");
|
|
1197
1261
|
if (!this.supervisors.has(criString)) {
|
|
1198
1262
|
this.debugLogger(`Registering service for CRI: ${criString}`);
|
|
1199
1263
|
const supervisor = new ServiceInvocationSupervisor(serviceIdentifier, service, this.eventBus, () => this.contextInterceptor);
|
|
@@ -1202,7 +1266,7 @@ class ServiceRegistry {
|
|
|
1202
1266
|
}
|
|
1203
1267
|
}
|
|
1204
1268
|
unRegister(serviceIdentifier) {
|
|
1205
|
-
const criString = serviceIdentifier.
|
|
1269
|
+
const criString = serviceIdentifier.cris().map((cri) => cri.raw()).join(", ");
|
|
1206
1270
|
const supervisor = this.supervisors.get(criString);
|
|
1207
1271
|
if (supervisor) {
|
|
1208
1272
|
this.debugLogger(`Unregistering service for CRI: ${criString}`);
|
|
@@ -1288,6 +1352,8 @@ class ServiceProxy {
|
|
|
1288
1352
|
class KinoticSingleton {
|
|
1289
1353
|
_eventBus;
|
|
1290
1354
|
serviceRegistry;
|
|
1355
|
+
zonePrefix = null;
|
|
1356
|
+
defaultZones = null;
|
|
1291
1357
|
constructor() {
|
|
1292
1358
|
this._eventBus = new EventBus;
|
|
1293
1359
|
this.serviceRegistry = new ServiceRegistry(this._eventBus);
|
package/dist/index.d.cts
CHANGED
|
@@ -454,23 +454,34 @@ declare function createCRI(scheme: string, scope: string | null, resourceName: s
|
|
|
454
454
|
*/
|
|
455
455
|
declare function createCRI(scheme: string, scope: string | null, resourceName: string, path: string | null, version: string | null): CRI;
|
|
456
456
|
declare class ServiceIdentifier {
|
|
457
|
-
namespace: string;
|
|
457
|
+
namespace: string | null;
|
|
458
458
|
name: string;
|
|
459
|
+
zones?: string[];
|
|
459
460
|
scope?: string;
|
|
460
461
|
version?: string;
|
|
461
|
-
private
|
|
462
|
-
constructor(namespace: string, name: string);
|
|
462
|
+
private _cris;
|
|
463
|
+
constructor(namespace: string | null, name: string, zones?: string[]);
|
|
463
464
|
/**
|
|
464
|
-
* Returns the qualified name
|
|
465
|
-
* This is the namespace.name
|
|
465
|
+
* Returns the fully qualified name this {@link ServiceIdentifier} is addressed by
|
|
466
|
+
* This is the zone.namespace.name, omitting any part that is not set
|
|
466
467
|
* @return string containing the qualified name
|
|
468
|
+
* @throws if this {@link ServiceIdentifier} has more than one zone and therefore more than
|
|
469
|
+
* one qualified name
|
|
467
470
|
*/
|
|
468
471
|
qualifiedName(): string;
|
|
469
472
|
/**
|
|
473
|
+
* The {@link CRI}s this {@link ServiceIdentifier} is addressable by, one per zone
|
|
474
|
+
* @return the cris for this {@link ServiceIdentifier}, never empty
|
|
475
|
+
*/
|
|
476
|
+
cris(): CRI[];
|
|
477
|
+
/**
|
|
470
478
|
* The {@link CRI} that represents this {@link ServiceIdentifier}
|
|
471
479
|
* @return the cri for this {@link ServiceIdentifier}
|
|
480
|
+
* @throws if this {@link ServiceIdentifier} has more than one zone and therefore more than
|
|
481
|
+
* one cri
|
|
472
482
|
*/
|
|
473
483
|
cri(): CRI;
|
|
484
|
+
private qualifiedNameFor;
|
|
474
485
|
}
|
|
475
486
|
import { Observable as Observable2 } from "rxjs";
|
|
476
487
|
/**
|
|
@@ -581,6 +592,18 @@ interface IKinotic {
|
|
|
581
592
|
serviceRegistry: IServiceRegistry;
|
|
582
593
|
eventBus: IEventBus;
|
|
583
594
|
/**
|
|
595
|
+
* The zone prefix every service published by this client registers under, such as
|
|
596
|
+
* `app.<organizationId>.<applicationId>` for an application or `system` for a platform
|
|
597
|
+
* workload. Must be set from static configuration before service classes are instantiated.
|
|
598
|
+
* When null, services register at un-zoned legacy addresses.
|
|
599
|
+
*/
|
|
600
|
+
zonePrefix: string | null;
|
|
601
|
+
/**
|
|
602
|
+
* The zones applied to published services that carry no {@link Zones} declaration of their
|
|
603
|
+
* own, typically loaded from the project package.json `kinotic.zones` field.
|
|
604
|
+
*/
|
|
605
|
+
defaultZones: string[] | null;
|
|
606
|
+
/**
|
|
584
607
|
* Requests a connection to the given Stomp url
|
|
585
608
|
* @param connectionInfo provides the information needed to connect to the kinoitc server
|
|
586
609
|
* @return Promise containing the result of the initial connection attempt
|
|
@@ -617,6 +640,8 @@ declare class KinoticSingleton implements IKinotic {
|
|
|
617
640
|
* The {@link ServiceRegistry} that is used to manage the services that are available
|
|
618
641
|
*/
|
|
619
642
|
readonly serviceRegistry: ServiceRegistry;
|
|
643
|
+
zonePrefix: string | null;
|
|
644
|
+
defaultZones: string[] | null;
|
|
620
645
|
constructor();
|
|
621
646
|
get eventBus(): IEventBus;
|
|
622
647
|
set eventBus(eventBus: IEventBus);
|
|
@@ -660,6 +685,16 @@ declare function Scope(value: Function, _context: ClassGetterDecoratorContext |
|
|
|
660
685
|
*/
|
|
661
686
|
declare function Version(version: string): (value: Function, _context: ClassDecoratorContext<any>) => void;
|
|
662
687
|
/**
|
|
688
|
+
* Declares the zones a service is addressable in, relative to this client's trust context: the
|
|
689
|
+
* declared zones are appended to {@link KinoticSingleton#zonePrefix}, so an application's service
|
|
690
|
+
* can never leave its own `app.<organizationId>.<applicationId>` zone. A service declaring
|
|
691
|
+
* multiple zones registers one address per zone. When absent, {@link KinoticSingleton#defaultZones}
|
|
692
|
+
* (typically loaded from the project package.json `kinotic.zones` field) applies.
|
|
693
|
+
* @param zones each zone is one or more dot separated labels of lowercase letters, digits, and
|
|
694
|
+
* interior dashes, e.g. `billing` or `billing.internal`
|
|
695
|
+
*/
|
|
696
|
+
declare function Zones(zones: string[]): (value: Function, _context: ClassDecoratorContext<any>) => void;
|
|
697
|
+
/**
|
|
663
698
|
* Marks a service method that receives the {@link ServiceContext} produced by the registered
|
|
664
699
|
* {@link ContextInterceptor}. The context parameter MUST be the method's final parameter:
|
|
665
700
|
* callers do not pass it, and the platform appends it after the caller-supplied arguments.
|
|
@@ -672,13 +707,27 @@ declare function Context(value: Function, _context: ClassMethodDecoratorContext)
|
|
|
672
707
|
*/
|
|
673
708
|
declare function receivesContext(serviceInstance: object, methodName: string): boolean;
|
|
674
709
|
/**
|
|
675
|
-
* Registers each instance of the decorated class with the Kinotic ServiceRegistry
|
|
676
|
-
*
|
|
677
|
-
*
|
|
678
|
-
* @param namespace the namespace the service is published under
|
|
710
|
+
* Registers each instance of the decorated class with the Kinotic ServiceRegistry.
|
|
711
|
+
* The service name defaults to the class name; {@link Version}, {@link Scope}, and {@link Zones}
|
|
712
|
+
* on the same class refine the registration.
|
|
713
|
+
* @param namespace the optional namespace the service is published under
|
|
679
714
|
* @param name the service name, defaults to the class name
|
|
680
715
|
*/
|
|
681
|
-
declare function Publish(namespace
|
|
716
|
+
declare function Publish(namespace?: string | null, name?: string): <T extends new (...args: any[]) => object>(value: T, _context: ClassDecoratorContext<any>) => T;
|
|
717
|
+
/**
|
|
718
|
+
* Validates that the given zone satisfies the zone grammar
|
|
719
|
+
* @param zone the zone to validate
|
|
720
|
+
* @throws if the zone is empty or contains anything other than dot separated labels of
|
|
721
|
+
* lowercase letters, digits, and interior dashes or underscores
|
|
722
|
+
*/
|
|
723
|
+
declare function validateZone(zone: string): void;
|
|
724
|
+
/**
|
|
725
|
+
* Validates that the given value is a single zone label
|
|
726
|
+
* @param label the label to validate
|
|
727
|
+
* @throws if the label is empty or is not a single label of lowercase letters, digits, and
|
|
728
|
+
* interior dashes or underscores
|
|
729
|
+
*/
|
|
730
|
+
declare function validateLabel(label: string): void;
|
|
682
731
|
/**
|
|
683
732
|
* A page is a sublist of a list of objects.
|
|
684
733
|
* @author Navid Mitchell
|
|
@@ -1197,4 +1246,4 @@ declare class ParticipantConstants {
|
|
|
1197
1246
|
static readonly PARTICIPANT_TYPE_NODE: string;
|
|
1198
1247
|
static readonly CLI_PARTICIPANT_ID: string;
|
|
1199
1248
|
}
|
|
1200
|
-
export { receivesContext, createCRI, createAuthenticatedWebSocketFactory, WebSocketFactory, Version, TextEventFactory, Sort, SessionKeepAliveMode, ServiceRegistry, ServiceContext, ServerInfo, Scope, Publish, ParticipantConstants, Participant, Pageable, Page, Order, OffsetPageable, NullHandling, KinoticSingleton, KinoticPlugin, Kinotic, JsonEventFactory, IterablePage, Identifiable, IWebSocket, IServiceRegistry, IServiceProxy, IParticipant, IKinotic, IEventFactory, IEventBus, IEvent, IEditableDataSource, IDataSource, ICrudServiceProxyFactory, ICrudServiceProxy, IAuthProvider, FunctionalIterablePage, EventConstants, EventBus, Event, Direction, DefaultCRI, DataSourceUtils, CursorPageable, CrudServiceProxyFactory, CrudServiceProxy, ContextInterceptor, Context, ConnectionInfo, ConnectedInfo, CRI, BearerTokenAuthProvider, BasicAuthProvider, AbstractIterablePage };
|
|
1249
|
+
export { validateZone, validateLabel, receivesContext, createCRI, createAuthenticatedWebSocketFactory, Zones, WebSocketFactory, Version, TextEventFactory, Sort, SessionKeepAliveMode, ServiceRegistry, ServiceContext, ServerInfo, Scope, Publish, ParticipantConstants, Participant, Pageable, Page, Order, OffsetPageable, NullHandling, KinoticSingleton, KinoticPlugin, Kinotic, JsonEventFactory, IterablePage, Identifiable, IWebSocket, IServiceRegistry, IServiceProxy, IParticipant, IKinotic, IEventFactory, IEventBus, IEvent, IEditableDataSource, IDataSource, ICrudServiceProxyFactory, ICrudServiceProxy, IAuthProvider, FunctionalIterablePage, EventConstants, EventBus, Event, Direction, DefaultCRI, DataSourceUtils, CursorPageable, CrudServiceProxyFactory, CrudServiceProxy, ContextInterceptor, Context, ConnectionInfo, ConnectedInfo, CRI, BearerTokenAuthProvider, BasicAuthProvider, AbstractIterablePage };
|
package/dist/index.d.ts
CHANGED
|
@@ -454,23 +454,34 @@ declare function createCRI(scheme: string, scope: string | null, resourceName: s
|
|
|
454
454
|
*/
|
|
455
455
|
declare function createCRI(scheme: string, scope: string | null, resourceName: string, path: string | null, version: string | null): CRI;
|
|
456
456
|
declare class ServiceIdentifier {
|
|
457
|
-
namespace: string;
|
|
457
|
+
namespace: string | null;
|
|
458
458
|
name: string;
|
|
459
|
+
zones?: string[];
|
|
459
460
|
scope?: string;
|
|
460
461
|
version?: string;
|
|
461
|
-
private
|
|
462
|
-
constructor(namespace: string, name: string);
|
|
462
|
+
private _cris;
|
|
463
|
+
constructor(namespace: string | null, name: string, zones?: string[]);
|
|
463
464
|
/**
|
|
464
|
-
* Returns the qualified name
|
|
465
|
-
* This is the namespace.name
|
|
465
|
+
* Returns the fully qualified name this {@link ServiceIdentifier} is addressed by
|
|
466
|
+
* This is the zone.namespace.name, omitting any part that is not set
|
|
466
467
|
* @return string containing the qualified name
|
|
468
|
+
* @throws if this {@link ServiceIdentifier} has more than one zone and therefore more than
|
|
469
|
+
* one qualified name
|
|
467
470
|
*/
|
|
468
471
|
qualifiedName(): string;
|
|
469
472
|
/**
|
|
473
|
+
* The {@link CRI}s this {@link ServiceIdentifier} is addressable by, one per zone
|
|
474
|
+
* @return the cris for this {@link ServiceIdentifier}, never empty
|
|
475
|
+
*/
|
|
476
|
+
cris(): CRI[];
|
|
477
|
+
/**
|
|
470
478
|
* The {@link CRI} that represents this {@link ServiceIdentifier}
|
|
471
479
|
* @return the cri for this {@link ServiceIdentifier}
|
|
480
|
+
* @throws if this {@link ServiceIdentifier} has more than one zone and therefore more than
|
|
481
|
+
* one cri
|
|
472
482
|
*/
|
|
473
483
|
cri(): CRI;
|
|
484
|
+
private qualifiedNameFor;
|
|
474
485
|
}
|
|
475
486
|
import { Observable as Observable2 } from "rxjs";
|
|
476
487
|
/**
|
|
@@ -581,6 +592,18 @@ interface IKinotic {
|
|
|
581
592
|
serviceRegistry: IServiceRegistry;
|
|
582
593
|
eventBus: IEventBus;
|
|
583
594
|
/**
|
|
595
|
+
* The zone prefix every service published by this client registers under, such as
|
|
596
|
+
* `app.<organizationId>.<applicationId>` for an application or `system` for a platform
|
|
597
|
+
* workload. Must be set from static configuration before service classes are instantiated.
|
|
598
|
+
* When null, services register at un-zoned legacy addresses.
|
|
599
|
+
*/
|
|
600
|
+
zonePrefix: string | null;
|
|
601
|
+
/**
|
|
602
|
+
* The zones applied to published services that carry no {@link Zones} declaration of their
|
|
603
|
+
* own, typically loaded from the project package.json `kinotic.zones` field.
|
|
604
|
+
*/
|
|
605
|
+
defaultZones: string[] | null;
|
|
606
|
+
/**
|
|
584
607
|
* Requests a connection to the given Stomp url
|
|
585
608
|
* @param connectionInfo provides the information needed to connect to the kinoitc server
|
|
586
609
|
* @return Promise containing the result of the initial connection attempt
|
|
@@ -617,6 +640,8 @@ declare class KinoticSingleton implements IKinotic {
|
|
|
617
640
|
* The {@link ServiceRegistry} that is used to manage the services that are available
|
|
618
641
|
*/
|
|
619
642
|
readonly serviceRegistry: ServiceRegistry;
|
|
643
|
+
zonePrefix: string | null;
|
|
644
|
+
defaultZones: string[] | null;
|
|
620
645
|
constructor();
|
|
621
646
|
get eventBus(): IEventBus;
|
|
622
647
|
set eventBus(eventBus: IEventBus);
|
|
@@ -660,6 +685,16 @@ declare function Scope(value: Function, _context: ClassGetterDecoratorContext |
|
|
|
660
685
|
*/
|
|
661
686
|
declare function Version(version: string): (value: Function, _context: ClassDecoratorContext<any>) => void;
|
|
662
687
|
/**
|
|
688
|
+
* Declares the zones a service is addressable in, relative to this client's trust context: the
|
|
689
|
+
* declared zones are appended to {@link KinoticSingleton#zonePrefix}, so an application's service
|
|
690
|
+
* can never leave its own `app.<organizationId>.<applicationId>` zone. A service declaring
|
|
691
|
+
* multiple zones registers one address per zone. When absent, {@link KinoticSingleton#defaultZones}
|
|
692
|
+
* (typically loaded from the project package.json `kinotic.zones` field) applies.
|
|
693
|
+
* @param zones each zone is one or more dot separated labels of lowercase letters, digits, and
|
|
694
|
+
* interior dashes, e.g. `billing` or `billing.internal`
|
|
695
|
+
*/
|
|
696
|
+
declare function Zones(zones: string[]): (value: Function, _context: ClassDecoratorContext<any>) => void;
|
|
697
|
+
/**
|
|
663
698
|
* Marks a service method that receives the {@link ServiceContext} produced by the registered
|
|
664
699
|
* {@link ContextInterceptor}. The context parameter MUST be the method's final parameter:
|
|
665
700
|
* callers do not pass it, and the platform appends it after the caller-supplied arguments.
|
|
@@ -672,13 +707,27 @@ declare function Context(value: Function, _context: ClassMethodDecoratorContext)
|
|
|
672
707
|
*/
|
|
673
708
|
declare function receivesContext(serviceInstance: object, methodName: string): boolean;
|
|
674
709
|
/**
|
|
675
|
-
* Registers each instance of the decorated class with the Kinotic ServiceRegistry
|
|
676
|
-
*
|
|
677
|
-
*
|
|
678
|
-
* @param namespace the namespace the service is published under
|
|
710
|
+
* Registers each instance of the decorated class with the Kinotic ServiceRegistry.
|
|
711
|
+
* The service name defaults to the class name; {@link Version}, {@link Scope}, and {@link Zones}
|
|
712
|
+
* on the same class refine the registration.
|
|
713
|
+
* @param namespace the optional namespace the service is published under
|
|
679
714
|
* @param name the service name, defaults to the class name
|
|
680
715
|
*/
|
|
681
|
-
declare function Publish(namespace
|
|
716
|
+
declare function Publish(namespace?: string | null, name?: string): <T extends new (...args: any[]) => object>(value: T, _context: ClassDecoratorContext<any>) => T;
|
|
717
|
+
/**
|
|
718
|
+
* Validates that the given zone satisfies the zone grammar
|
|
719
|
+
* @param zone the zone to validate
|
|
720
|
+
* @throws if the zone is empty or contains anything other than dot separated labels of
|
|
721
|
+
* lowercase letters, digits, and interior dashes or underscores
|
|
722
|
+
*/
|
|
723
|
+
declare function validateZone(zone: string): void;
|
|
724
|
+
/**
|
|
725
|
+
* Validates that the given value is a single zone label
|
|
726
|
+
* @param label the label to validate
|
|
727
|
+
* @throws if the label is empty or is not a single label of lowercase letters, digits, and
|
|
728
|
+
* interior dashes or underscores
|
|
729
|
+
*/
|
|
730
|
+
declare function validateLabel(label: string): void;
|
|
682
731
|
/**
|
|
683
732
|
* A page is a sublist of a list of objects.
|
|
684
733
|
* @author Navid Mitchell
|
|
@@ -1197,4 +1246,4 @@ declare class ParticipantConstants {
|
|
|
1197
1246
|
static readonly PARTICIPANT_TYPE_NODE: string;
|
|
1198
1247
|
static readonly CLI_PARTICIPANT_ID: string;
|
|
1199
1248
|
}
|
|
1200
|
-
export { receivesContext, createCRI, createAuthenticatedWebSocketFactory, WebSocketFactory, Version, TextEventFactory, Sort, SessionKeepAliveMode, ServiceRegistry, ServiceContext, ServerInfo, Scope, Publish, ParticipantConstants, Participant, Pageable, Page, Order, OffsetPageable, NullHandling, KinoticSingleton, KinoticPlugin, Kinotic, JsonEventFactory, IterablePage, Identifiable, IWebSocket, IServiceRegistry, IServiceProxy, IParticipant, IKinotic, IEventFactory, IEventBus, IEvent, IEditableDataSource, IDataSource, ICrudServiceProxyFactory, ICrudServiceProxy, IAuthProvider, FunctionalIterablePage, EventConstants, EventBus, Event, Direction, DefaultCRI, DataSourceUtils, CursorPageable, CrudServiceProxyFactory, CrudServiceProxy, ContextInterceptor, Context, ConnectionInfo, ConnectedInfo, CRI, BearerTokenAuthProvider, BasicAuthProvider, AbstractIterablePage };
|
|
1249
|
+
export { validateZone, validateLabel, receivesContext, createCRI, createAuthenticatedWebSocketFactory, Zones, WebSocketFactory, Version, TextEventFactory, Sort, SessionKeepAliveMode, ServiceRegistry, ServiceContext, ServerInfo, Scope, Publish, ParticipantConstants, Participant, Pageable, Page, Order, OffsetPageable, NullHandling, KinoticSingleton, KinoticPlugin, Kinotic, JsonEventFactory, IterablePage, Identifiable, IWebSocket, IServiceRegistry, IServiceProxy, IParticipant, IKinotic, IEventFactory, IEventBus, IEvent, IEditableDataSource, IDataSource, ICrudServiceProxyFactory, ICrudServiceProxy, IAuthProvider, FunctionalIterablePage, EventConstants, EventBus, Event, Direction, DefaultCRI, DataSourceUtils, CursorPageable, CrudServiceProxyFactory, CrudServiceProxy, ContextInterceptor, Context, ConnectionInfo, ConnectedInfo, CRI, BearerTokenAuthProvider, BasicAuthProvider, AbstractIterablePage };
|
package/dist/index.js
CHANGED
|
@@ -682,27 +682,61 @@ function createDebugLogger(namespace) {
|
|
|
682
682
|
class ServiceIdentifier {
|
|
683
683
|
namespace;
|
|
684
684
|
name;
|
|
685
|
+
zones;
|
|
685
686
|
scope;
|
|
686
687
|
version;
|
|
687
|
-
|
|
688
|
-
constructor(namespace, name) {
|
|
688
|
+
_cris = null;
|
|
689
|
+
constructor(namespace, name, zones) {
|
|
689
690
|
this.namespace = namespace;
|
|
690
691
|
this.name = name;
|
|
692
|
+
this.zones = zones;
|
|
691
693
|
}
|
|
692
694
|
qualifiedName() {
|
|
693
|
-
|
|
695
|
+
if (this.zones && this.zones.length > 1) {
|
|
696
|
+
throw new Error(`ServiceIdentifier has ${this.zones.length} zones so there is no single qualifiedName`);
|
|
697
|
+
}
|
|
698
|
+
return this.qualifiedNameFor(this.zones?.[0]);
|
|
699
|
+
}
|
|
700
|
+
cris() {
|
|
701
|
+
if (this._cris == null) {
|
|
702
|
+
const zones = this.zones && this.zones.length > 0 ? this.zones : [undefined];
|
|
703
|
+
this._cris = zones.map((zone) => createCRI("srv" /* SERVICE_DESTINATION_SCHEME */, this.scope || null, this.qualifiedNameFor(zone), null, this.version || null));
|
|
704
|
+
}
|
|
705
|
+
return this._cris;
|
|
694
706
|
}
|
|
695
707
|
cri() {
|
|
696
|
-
|
|
697
|
-
|
|
708
|
+
const cris = this.cris();
|
|
709
|
+
const cri = cris[0];
|
|
710
|
+
if (cris.length !== 1 || !cri) {
|
|
711
|
+
throw new Error(`ServiceIdentifier has ${cris.length} cris so there is no single cri`);
|
|
698
712
|
}
|
|
699
|
-
return
|
|
713
|
+
return cri;
|
|
714
|
+
}
|
|
715
|
+
qualifiedNameFor(zone) {
|
|
716
|
+
const name = this.namespace ? this.namespace + "." + this.name : this.name;
|
|
717
|
+
return zone ? zone + "." + name : name;
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
// packages/core/src/api/ZoneUtil.ts
|
|
722
|
+
var LABEL = "[a-z0-9]([a-z0-9_-]*[a-z0-9])?";
|
|
723
|
+
var LABEL_PATTERN = new RegExp(`^${LABEL}$`);
|
|
724
|
+
var ZONE_PATTERN = new RegExp(`^${LABEL}(\\.${LABEL})*$`);
|
|
725
|
+
function validateZone(zone) {
|
|
726
|
+
if (!zone || !ZONE_PATTERN.test(zone)) {
|
|
727
|
+
throw new Error(`Invalid zone '${zone}'. Zones must be dot separated labels of lowercase letters, digits, and interior dashes or underscores`);
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
function validateLabel(label) {
|
|
731
|
+
if (!label || !LABEL_PATTERN.test(label)) {
|
|
732
|
+
throw new Error(`Invalid zone label '${label}'. Labels must be lowercase letters, digits, and interior dashes or underscores`);
|
|
700
733
|
}
|
|
701
734
|
}
|
|
702
735
|
|
|
703
736
|
// packages/core/src/api/KinoticDecorators.ts
|
|
704
737
|
var scopeFunctions = new WeakSet;
|
|
705
738
|
var versionRegistry = new WeakMap;
|
|
739
|
+
var zonesRegistry = new WeakMap;
|
|
706
740
|
var contextMarkedFunctions = new WeakSet;
|
|
707
741
|
function findInConstructorChain(registry, constructor) {
|
|
708
742
|
let current = constructor;
|
|
@@ -745,6 +779,14 @@ function Version(version) {
|
|
|
745
779
|
versionRegistry.set(value, version);
|
|
746
780
|
};
|
|
747
781
|
}
|
|
782
|
+
function Zones(zones) {
|
|
783
|
+
for (const zone of zones) {
|
|
784
|
+
validateZone(zone);
|
|
785
|
+
}
|
|
786
|
+
return function(value, _context) {
|
|
787
|
+
zonesRegistry.set(value, zones);
|
|
788
|
+
};
|
|
789
|
+
}
|
|
748
790
|
function Context(value, _context) {
|
|
749
791
|
contextMarkedFunctions.add(value);
|
|
750
792
|
}
|
|
@@ -752,12 +794,29 @@ function receivesContext(serviceInstance, methodName) {
|
|
|
752
794
|
const method = serviceInstance[methodName];
|
|
753
795
|
return typeof method === "function" && contextMarkedFunctions.has(method);
|
|
754
796
|
}
|
|
797
|
+
function resolveEffectiveZones(constructor) {
|
|
798
|
+
const declaredZones = findInConstructorChain(zonesRegistry, constructor) ?? Kinotic.defaultZones ?? [];
|
|
799
|
+
const prefix = Kinotic.zonePrefix;
|
|
800
|
+
let effectiveZones;
|
|
801
|
+
if (prefix != null && declaredZones.length > 0) {
|
|
802
|
+
effectiveZones = declaredZones.map((zone) => `${prefix}.${zone}`);
|
|
803
|
+
} else if (prefix != null) {
|
|
804
|
+
effectiveZones = [prefix];
|
|
805
|
+
} else {
|
|
806
|
+
effectiveZones = [...declaredZones];
|
|
807
|
+
}
|
|
808
|
+
for (const zone of effectiveZones) {
|
|
809
|
+
validateZone(zone);
|
|
810
|
+
}
|
|
811
|
+
return effectiveZones;
|
|
812
|
+
}
|
|
755
813
|
function Publish(namespace, name) {
|
|
756
814
|
return function(value, _context) {
|
|
757
815
|
return class extends value {
|
|
758
816
|
constructor(...args) {
|
|
759
817
|
super(...args);
|
|
760
|
-
const
|
|
818
|
+
const zones = resolveEffectiveZones(this.constructor);
|
|
819
|
+
const serviceIdentifier = new ServiceIdentifier(namespace ?? null, name || value.name, zones.length > 0 ? zones : undefined);
|
|
761
820
|
const version = findInConstructorChain(versionRegistry, this.constructor);
|
|
762
821
|
if (version) {
|
|
763
822
|
serviceIdentifier.version = version;
|
|
@@ -782,7 +841,7 @@ class ServiceInvocationSupervisor {
|
|
|
782
841
|
returnValueConverter;
|
|
783
842
|
serviceIdentifier;
|
|
784
843
|
serviceInstance;
|
|
785
|
-
|
|
844
|
+
methodSubscriptions = [];
|
|
786
845
|
methodMap;
|
|
787
846
|
constructor(serviceIdentifier, serviceInstance, eventBusService, interceptorProvider, options = {}) {
|
|
788
847
|
if (!serviceIdentifier)
|
|
@@ -819,31 +878,33 @@ class ServiceInvocationSupervisor {
|
|
|
819
878
|
throw new Error("Service already started");
|
|
820
879
|
}
|
|
821
880
|
this.active = true;
|
|
822
|
-
const
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
881
|
+
for (const cri of this.serviceIdentifier.cris()) {
|
|
882
|
+
const criBase = cri.baseResource();
|
|
883
|
+
this.methodSubscriptions.push(this._eventBus.observe(criBase).subscribe({
|
|
884
|
+
next: async (event) => {
|
|
885
|
+
await this.processEvent(event);
|
|
886
|
+
},
|
|
887
|
+
error: (error) => {
|
|
888
|
+
this.log.error("Event listener error", error);
|
|
889
|
+
this.active = false;
|
|
890
|
+
},
|
|
891
|
+
complete: () => {
|
|
892
|
+
this.log.error("Event listener stopped unexpectedly. Setting supervisor inactive.");
|
|
893
|
+
this.active = false;
|
|
894
|
+
}
|
|
895
|
+
}));
|
|
896
|
+
this.log.info(`ServiceInvocationSupervisor started for ${criBase}`);
|
|
897
|
+
}
|
|
837
898
|
}
|
|
838
899
|
stop() {
|
|
839
900
|
if (!this.active) {
|
|
840
901
|
throw new Error("Service already stopped");
|
|
841
902
|
}
|
|
842
903
|
this.active = false;
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
this.methodSubscription = null;
|
|
904
|
+
for (const subscription of this.methodSubscriptions) {
|
|
905
|
+
subscription.unsubscribe();
|
|
846
906
|
}
|
|
907
|
+
this.methodSubscriptions = [];
|
|
847
908
|
this.log.info("ServiceInvocationSupervisor stopped");
|
|
848
909
|
}
|
|
849
910
|
buildMethodMap(serviceInstance) {
|
|
@@ -966,7 +1027,7 @@ import { first, map as map2 } from "rxjs/operators";
|
|
|
966
1027
|
// packages/core/package.json
|
|
967
1028
|
var package_default = {
|
|
968
1029
|
name: "@kinotic-ai/core",
|
|
969
|
-
version: "
|
|
1030
|
+
version: "3.0.0",
|
|
970
1031
|
type: "module",
|
|
971
1032
|
files: [
|
|
972
1033
|
"dist"
|
|
@@ -1095,7 +1156,7 @@ class ServiceRegistry {
|
|
|
1095
1156
|
return new ServiceProxy(serviceIdentifier, this);
|
|
1096
1157
|
}
|
|
1097
1158
|
register(serviceIdentifier, service) {
|
|
1098
|
-
const criString = serviceIdentifier.
|
|
1159
|
+
const criString = serviceIdentifier.cris().map((cri) => cri.raw()).join(", ");
|
|
1099
1160
|
if (!this.supervisors.has(criString)) {
|
|
1100
1161
|
this.debugLogger(`Registering service for CRI: ${criString}`);
|
|
1101
1162
|
const supervisor = new ServiceInvocationSupervisor(serviceIdentifier, service, this.eventBus, () => this.contextInterceptor);
|
|
@@ -1104,7 +1165,7 @@ class ServiceRegistry {
|
|
|
1104
1165
|
}
|
|
1105
1166
|
}
|
|
1106
1167
|
unRegister(serviceIdentifier) {
|
|
1107
|
-
const criString = serviceIdentifier.
|
|
1168
|
+
const criString = serviceIdentifier.cris().map((cri) => cri.raw()).join(", ");
|
|
1108
1169
|
const supervisor = this.supervisors.get(criString);
|
|
1109
1170
|
if (supervisor) {
|
|
1110
1171
|
this.debugLogger(`Unregistering service for CRI: ${criString}`);
|
|
@@ -1190,6 +1251,8 @@ class ServiceProxy {
|
|
|
1190
1251
|
class KinoticSingleton {
|
|
1191
1252
|
_eventBus;
|
|
1192
1253
|
serviceRegistry;
|
|
1254
|
+
zonePrefix = null;
|
|
1255
|
+
defaultZones = null;
|
|
1193
1256
|
constructor() {
|
|
1194
1257
|
this._eventBus = new EventBus;
|
|
1195
1258
|
this.serviceRegistry = new ServiceRegistry(this._eventBus);
|
|
@@ -1491,9 +1554,12 @@ class ParticipantConstants {
|
|
|
1491
1554
|
static CLI_PARTICIPANT_ID = "-42-Kinotic-CLI-42-";
|
|
1492
1555
|
}
|
|
1493
1556
|
export {
|
|
1557
|
+
validateZone,
|
|
1558
|
+
validateLabel,
|
|
1494
1559
|
receivesContext,
|
|
1495
1560
|
createCRI,
|
|
1496
1561
|
createAuthenticatedWebSocketFactory,
|
|
1562
|
+
Zones,
|
|
1497
1563
|
Version,
|
|
1498
1564
|
TextEventFactory,
|
|
1499
1565
|
Sort,
|