@cadenza.io/service 2.12.0 → 2.16.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.d.mts CHANGED
@@ -1,6 +1,7 @@
1
1
  import * as _cadenza_io_core from '@cadenza.io/core';
2
2
  import { Task, ThrottleTagGetter, Schema, GraphContext, AnyObject, InquiryOptions, TaskResult, GraphRoutine, SchemaDefinition, SignalBroker, InquiryBroker, GraphRunner, GraphRegistry, EmitOptions, CadenzaMode, Intent, Actor, TaskOptions, ActorSpec, ActorFactoryOptions, ActorDefinition, TaskFunction, DebounceOptions, DebounceTask, EphemeralTaskOptions, EphemeralTask } from '@cadenza.io/core';
3
3
  export { Actor, ActorConsistencyProfileName, ActorDefinition, ActorFactoryOptions, ActorInvocationOptions, ActorKeyDefinition, ActorKind, ActorLoadPolicy, ActorRuntimeReadGuard, ActorSpec, ActorStateDefinition, ActorStateReducer, ActorStateStore, ActorTaskBindingDefinition, ActorTaskBindingOptions, ActorTaskContext, ActorTaskHandler, ActorTaskMode, ActorWriteContract, AnyObject, DebounceOptions, DebounceTask, EphemeralTask, EphemeralTaskOptions, GraphRoutine, IdempotencyPolicy, RetryPolicy, SessionPolicy, Task, TaskFunction, TaskOptions, ThrottleTagGetter } from '@cadenza.io/core';
4
+ import { Pool } from 'pg';
4
5
 
5
6
  /**
6
7
  * Represents a task that delegates execution of a routine to a remote system or service.
@@ -201,28 +202,13 @@ type DistributedInquiryOptions = Partial<InquiryOptions> & {
201
202
  * Mainly used by distributed deputy responders.
202
203
  */
203
204
  perResponderTimeoutMs?: number;
205
+ /**
206
+ * Optional hydration cache key used to consume one SSR-provided inquiry result
207
+ * before the live frontend runtime performs normal distributed routing.
208
+ */
209
+ hydrationKey?: string;
204
210
  };
205
211
 
206
- type RuntimeStatusState = "healthy" | "degraded" | "overloaded" | "unavailable";
207
-
208
- interface ServiceInstanceDescriptor {
209
- uuid: string;
210
- address: string;
211
- port: number;
212
- serviceName: string;
213
- numberOfRunningGraphs?: number;
214
- isPrimary: boolean;
215
- isActive: boolean;
216
- isNonResponsive: boolean;
217
- isBlocked: boolean;
218
- runtimeState?: RuntimeStatusState;
219
- acceptingWork?: boolean;
220
- reportedAt?: string;
221
- health: AnyObject;
222
- exposed: boolean;
223
- clientCreated?: boolean;
224
- isFrontend: boolean;
225
- }
226
212
  interface DeputyDescriptor {
227
213
  serviceName: string;
228
214
  remoteRoutineName?: string;
@@ -265,7 +251,9 @@ declare class ServiceRegistry {
265
251
  numberOfRunningGraphs: number;
266
252
  useSocket: boolean;
267
253
  retryCount: number;
254
+ isFrontend: boolean;
268
255
  handleInstanceUpdateTask: Task;
256
+ handleTransportUpdateTask: Task;
269
257
  handleGlobalSignalRegistrationTask: Task;
270
258
  handleGlobalIntentRegistrationTask: Task;
271
259
  handleSocketStatusUpdateTask: Task;
@@ -280,6 +268,7 @@ declare class ServiceRegistry {
280
268
  getStatusTask: Task;
281
269
  insertServiceTask: Task;
282
270
  insertServiceInstanceTask: Task;
271
+ insertServiceTransportTask: Task;
283
272
  handleServiceNotRespondingTask: Task;
284
273
  handleServiceHandshakeTask: Task;
285
274
  collectTransportDiagnosticsTask: Task;
@@ -291,6 +280,12 @@ declare class ServiceRegistry {
291
280
  getInquiryResponderDescriptor(task: Task): InquiryResponderDescriptor;
292
281
  private getInstance;
293
282
  private getLocalInstance;
283
+ private getRoutingTransportRole;
284
+ private getTransportById;
285
+ private getRouteableTransport;
286
+ private getTransportClientKey;
287
+ private hasTransportClientCreated;
288
+ private markTransportClientCreated;
294
289
  private registerDependee;
295
290
  private unregisterDependee;
296
291
  private getHeartbeatMisses;
@@ -481,6 +476,44 @@ interface DatabaseSchemaDefinition {
481
476
  };
482
477
  }
483
478
 
479
+ interface BootstrapOptions {
480
+ url?: string;
481
+ injectedGlobalKey?: string;
482
+ }
483
+ interface HydrationOptions {
484
+ initialInquiryResults?: Record<string, unknown>;
485
+ }
486
+ interface ResolvedBootstrapEndpoint {
487
+ url: string;
488
+ protocol: "http" | "https";
489
+ address: string;
490
+ port: number;
491
+ exposed: boolean;
492
+ injectedGlobalKey: string;
493
+ }
494
+
495
+ type ServiceTransportRole = "internal" | "public";
496
+ type ServiceTransportProtocol = "rest" | "socket";
497
+ type ServiceTransportSecurityProfile = "low" | "medium" | "high";
498
+ interface ServiceTransportConfig {
499
+ role: ServiceTransportRole;
500
+ origin: string;
501
+ protocols?: ServiceTransportProtocol[];
502
+ securityProfile?: ServiceTransportSecurityProfile | null;
503
+ authStrategy?: string | null;
504
+ }
505
+ interface ServiceTransportDescriptor {
506
+ uuid: string;
507
+ serviceInstanceId: string;
508
+ role: ServiceTransportRole;
509
+ origin: string;
510
+ protocols: ServiceTransportProtocol[];
511
+ securityProfile: ServiceTransportSecurityProfile | null;
512
+ authStrategy: string | null;
513
+ deleted?: boolean;
514
+ clientCreated?: boolean;
515
+ }
516
+
484
517
  type SecurityProfile = "low" | "medium" | "high";
485
518
  type NetworkMode = "internal" | "exposed" | "exposed-high-sec" | "auto" | "dev";
486
519
  type ServerOptions = {
@@ -499,6 +532,9 @@ type ServerOptions = {
499
532
  address?: string;
500
533
  port?: number;
501
534
  };
535
+ bootstrap?: BootstrapOptions;
536
+ hydration?: HydrationOptions;
537
+ transports?: ServiceTransportConfig[];
502
538
  relatedServices?: string[][];
503
539
  isDatabase?: boolean;
504
540
  isFrontend?: boolean;
@@ -507,6 +543,7 @@ interface DatabaseOptions {
507
543
  databaseType?: "postgres";
508
544
  databaseName?: string;
509
545
  poolSize?: number;
546
+ ownerServiceName?: string | null;
510
547
  }
511
548
  /**
512
549
  * The CadenzaService class serves as a central service layer providing various utility methods for managing tasks, signals, logging, and service interactions.
@@ -522,6 +559,8 @@ declare class CadenzaService {
522
559
  protected static isBootstrapped: boolean;
523
560
  protected static serviceCreated: boolean;
524
561
  protected static warnedInvalidMetaIntentResponderKeys: Set<string>;
562
+ protected static hydratedInquiryResults: Map<string, AnyObject>;
563
+ protected static frontendSyncScheduled: boolean;
525
564
  /**
526
565
  * Initializes the application by setting up necessary components and configurations.
527
566
  * This method ensures the initialization process is only executed once throughout the application lifecycle.
@@ -529,6 +568,12 @@ declare class CadenzaService {
529
568
  * @return {void} This method does not return any value.
530
569
  */
531
570
  static bootstrap(): void;
571
+ private static ensureTransportControllers;
572
+ private static setHydrationResults;
573
+ private static consumeHydratedInquiryResult;
574
+ private static ensureFrontendSyncLoop;
575
+ private static normalizeDeclaredTransports;
576
+ private static createBootstrapTransport;
532
577
  /**
533
578
  * Validates the provided service name based on specific rules.
534
579
  *
@@ -22430,28 +22475,28 @@ declare class CadenzaService {
22430
22475
  */
22431
22476
  static createCadenzaMetaService(serviceName: string, description: string, options?: ServerOptions): void;
22432
22477
  /**
22433
- * Creates and initializes a PostgresActor-backed database service.
22434
- * This is the canonical API for schema-driven postgres setup in cadenza-service.
22478
+ * Creates and initializes a specialized PostgresActor.
22479
+ * This is actor-only and does not create or register a network service.
22435
22480
  *
22436
- * @param {string} name - Logical actor/service name.
22481
+ * @param {string} name - Logical PostgresActor name.
22437
22482
  * @param {DatabaseSchemaDefinition} schema - Database schema definition.
22438
- * @param {string} [description=""] - Optional human-readable description.
22439
- * @param {ServerOptions & DatabaseOptions} [options={}] - Server/database runtime options.
22483
+ * @param {string} [description=""] - Optional human-readable actor description.
22484
+ * @param {ServerOptions & DatabaseOptions} [options={}] - Actor/database runtime options.
22440
22485
  * @return {void}
22441
22486
  */
22442
22487
  static createPostgresActor(name: string, schema: DatabaseSchemaDefinition, description?: string, options?: ServerOptions & DatabaseOptions): void;
22443
22488
  /**
22444
- * Creates a meta PostgresActor service.
22489
+ * Creates a meta PostgresActor.
22445
22490
  *
22446
- * @param {string} name - Logical actor/service name.
22491
+ * @param {string} name - Logical PostgresActor name.
22447
22492
  * @param {DatabaseSchemaDefinition} schema - Database schema definition.
22448
22493
  * @param {string} [description=""] - Optional description.
22449
- * @param {ServerOptions & DatabaseOptions} [options={}] - Optional server/database options.
22494
+ * @param {ServerOptions & DatabaseOptions} [options={}] - Optional actor/database options.
22450
22495
  * @return {void}
22451
22496
  */
22452
22497
  static createMetaPostgresActor(name: string, schema: DatabaseSchemaDefinition, description?: string, options?: ServerOptions & DatabaseOptions): void;
22453
22498
  /**
22454
- * Legacy compatibility wrapper. Prefer {@link createPostgresActor}.
22499
+ * Creates a dedicated database service by composing a PostgresActor and a Cadenza service.
22455
22500
  */
22456
22501
  static createDatabaseService(name: string, schema: DatabaseSchemaDefinition, description?: string, options?: ServerOptions & DatabaseOptions): void;
22457
22502
  /**
@@ -22464,6 +22509,9 @@ declare class CadenzaService {
22464
22509
  * @return {void} - This method does not return a value.
22465
22510
  */
22466
22511
  static createMetaDatabaseService(name: string, schema: DatabaseSchemaDefinition, description?: string, options?: ServerOptions & DatabaseOptions): void;
22512
+ private static normalizePostgresActorOptions;
22513
+ private static normalizeDatabaseServiceOptions;
22514
+ private static registerDatabaseServiceBridgeTask;
22467
22515
  static createActor<D extends Record<string, any> = AnyObject, R = AnyObject>(spec: ActorSpec<D, R>, options?: ActorFactoryOptions): Actor<D, R>;
22468
22516
  static createActorFromDefinition<D extends Record<string, any> = AnyObject, R = AnyObject>(definition: ActorDefinition<D, R>, options?: ActorFactoryOptions<D, R>): Actor<D, R>;
22469
22517
  /**
@@ -22758,14 +22806,14 @@ declare class CadenzaService {
22758
22806
  */
22759
22807
  static createEphemeralTask(name: string, func: TaskFunction, description?: string, options?: TaskOptions & EphemeralTaskOptions): EphemeralTask;
22760
22808
  /**
22761
- * Creates an ephemeral meta task with the specified name, function, description, and options.
22809
+ * Creates an ephemeral meta-task with the specified name, function, description, and options.
22762
22810
  * See {@link createEphemeralTask} and {@link createMetaTask} for more details.
22763
22811
  *
22764
22812
  * @param {string} name - The name of the task to be created.
22765
22813
  * @param {TaskFunction} func - The function to be executed as part of the task.
22766
22814
  * @param {string} [description] - An optional description of the task.
22767
22815
  * @param {TaskOptions & EphemeralTaskOptions} [options={}] - Additional options for configuring the task.
22768
- * @return {EphemeralTask} The created ephemeral meta task.
22816
+ * @return {EphemeralTask} The created ephemeral meta-task.
22769
22817
  */
22770
22818
  static createEphemeralMetaTask(name: string, func: TaskFunction, description?: string, options?: TaskOptions & EphemeralTaskOptions): EphemeralTask;
22771
22819
  /**
@@ -22911,7 +22959,6 @@ declare class SocketController {
22911
22959
  private registerSocketClientTasks;
22912
22960
  private resolveSocketServerKey;
22913
22961
  private resolveSocketClientFetchId;
22914
- private resolveServicePort;
22915
22962
  private createSocketServerRuntimeHandleFromContext;
22916
22963
  private destroySocketServerRuntimeHandle;
22917
22964
  private createSocketClientRuntimeHandle;
@@ -22961,4 +23008,135 @@ declare class SignalController {
22961
23008
  constructor();
22962
23009
  }
22963
23010
 
22964
- export { type AggregateDefinition, type AggregateFunction, DatabaseTask, type DbOperationPayload, type DbOperationType$1 as DbOperationType, type DeputyDescriptor, DeputyTask, type DistributedInquiryMeta, type DistributedInquiryOptions, GraphMetadataController, type InquiryResponderDescriptor, type InquiryResponderStatus, type JoinDefinition, type NetworkMode, type OpEffect, type QueryMode, RestController, type SecurityProfile, type ServerOptions, type ServiceInstanceDescriptor, ServiceRegistry, SignalController, SignalTransmissionTask, SocketController, type SortDirection, type SubOperation, type SubOperationType, type ValueOrSubOp, CadenzaService as default };
23011
+ interface PostgresActorSafetyPolicy {
23012
+ statementTimeoutMs: number;
23013
+ retryCount: number;
23014
+ retryDelayMs: number;
23015
+ retryDelayMaxMs: number;
23016
+ retryDelayFactor: number;
23017
+ }
23018
+ interface PostgresActorDurableState {
23019
+ actorName: string;
23020
+ actorToken: string;
23021
+ ownerServiceName: string | null;
23022
+ databaseName: string;
23023
+ status: "idle" | "initializing" | "ready" | "error";
23024
+ schemaVersion: number;
23025
+ setupStartedAt: string | null;
23026
+ setupCompletedAt: string | null;
23027
+ lastHealthCheckAt: string | null;
23028
+ lastError: string | null;
23029
+ tables: string[];
23030
+ safetyPolicy: PostgresActorSafetyPolicy;
23031
+ }
23032
+ interface PostgresActorRuntimeState {
23033
+ pool: Pool | null;
23034
+ ready: boolean;
23035
+ pendingQueries: number;
23036
+ lastHealthCheckAt: number | null;
23037
+ lastError: string | null;
23038
+ }
23039
+ interface PostgresActorRegistration {
23040
+ ownerServiceName: string | null;
23041
+ databaseName: string;
23042
+ actorName: string;
23043
+ actorToken: string;
23044
+ actorKey: string;
23045
+ setupSignal: string;
23046
+ setupDoneSignal: string;
23047
+ setupFailedSignal: string;
23048
+ actor: Actor<PostgresActorDurableState, PostgresActorRuntimeState>;
23049
+ schema: DatabaseSchemaDefinition;
23050
+ description: string;
23051
+ options: ServerOptions & DatabaseOptions;
23052
+ tasksGenerated: boolean;
23053
+ intentNames: Set<string>;
23054
+ }
23055
+ /**
23056
+ * DatabaseController now acts as a PostgresActor plugin coordinator.
23057
+ *
23058
+ * Runtime ownership lives in actor runtime state (Pool + runtime health counters).
23059
+ * Durable actor state stores setup/configuration/health snapshots.
23060
+ */
23061
+ declare class DatabaseController {
23062
+ private static _instance;
23063
+ static get instance(): DatabaseController;
23064
+ private readonly registrationsByActorName;
23065
+ private readonly registrationsByActorToken;
23066
+ private readonly adminDbClient;
23067
+ constructor();
23068
+ reset(): void;
23069
+ createPostgresActor(name: string, schema: DatabaseSchemaDefinition, description: string, options: ServerOptions & DatabaseOptions): PostgresActorRegistration;
23070
+ requestPostgresActorSetup(registrationOrName: PostgresActorRegistration | string, ctx?: AnyObject): PostgresActorRegistration | undefined;
23071
+ private resolveRegistration;
23072
+ private emitSetupDone;
23073
+ private registerSetupTask;
23074
+ private createTargetPool;
23075
+ private buildDatabaseConnectionString;
23076
+ private createDatabaseIfMissing;
23077
+ private checkPoolHealth;
23078
+ private getPoolOrThrow;
23079
+ private withTimeout;
23080
+ private runWithRetries;
23081
+ private executeWithTransaction;
23082
+ private queryFunction;
23083
+ private insertFunction;
23084
+ private updateFunction;
23085
+ private deleteFunction;
23086
+ private resolveSafetyPolicy;
23087
+ private buildOnConflictClause;
23088
+ /**
23089
+ * Validates database schema structure and content.
23090
+ */
23091
+ validateSchema(ctx: AnyObject): true;
23092
+ sortTablesByReferences(ctx: AnyObject): AnyObject;
23093
+ private buildSchemaDdlStatements;
23094
+ private fieldDefinitionToSql;
23095
+ private applyDdlStatements;
23096
+ private generateDatabaseTasks;
23097
+ private createDatabaseMacroTasks;
23098
+ private createDatabaseTask;
23099
+ private validateOperationPayload;
23100
+ private validateJoinPayload;
23101
+ toCamelCase(rows: any[]): any[];
23102
+ buildWhereClause(filter: AnyObject, params: any[]): string;
23103
+ buildJoinClause(joins: Record<string, JoinDefinition>): string;
23104
+ resolveNestedData(registration: PostgresActorRegistration, data: any, tableName: string): Promise<any>;
23105
+ executeSubOperation(registration: PostgresActorRegistration, operation: SubOperation): Promise<any>;
23106
+ private getInputSchema;
23107
+ }
23108
+
23109
+ interface SSRInquiryBridgeOptions {
23110
+ bootstrap?: BootstrapOptions;
23111
+ cadenzaDB?: {
23112
+ address?: string;
23113
+ port?: number;
23114
+ };
23115
+ }
23116
+ interface SSRInquiryBridge {
23117
+ inquire: (inquiry: string, context?: AnyObject, options?: DistributedInquiryOptions) => Promise<AnyObject>;
23118
+ dehydrate: () => HydrationOptions;
23119
+ }
23120
+ declare function createSSRInquiryBridge(options?: SSRInquiryBridgeOptions): SSRInquiryBridge;
23121
+
23122
+ type RuntimeStatusState = "healthy" | "degraded" | "overloaded" | "unavailable";
23123
+
23124
+ interface ServiceInstanceDescriptor {
23125
+ uuid: string;
23126
+ serviceName: string;
23127
+ numberOfRunningGraphs?: number;
23128
+ isPrimary: boolean;
23129
+ isActive: boolean;
23130
+ isNonResponsive: boolean;
23131
+ isBlocked: boolean;
23132
+ runtimeState?: RuntimeStatusState;
23133
+ acceptingWork?: boolean;
23134
+ reportedAt?: string;
23135
+ health: AnyObject;
23136
+ isFrontend: boolean;
23137
+ isDatabase?: boolean;
23138
+ transports: ServiceTransportDescriptor[];
23139
+ clientCreatedTransportIds?: string[];
23140
+ }
23141
+
23142
+ export { type AggregateDefinition, type AggregateFunction, type BootstrapOptions, DatabaseController, DatabaseTask, type DbOperationPayload, type DbOperationType$1 as DbOperationType, type DeputyDescriptor, DeputyTask, type DistributedInquiryMeta, type DistributedInquiryOptions, GraphMetadataController, type HydrationOptions, type InquiryResponderDescriptor, type InquiryResponderStatus, type JoinDefinition, type NetworkMode, type OpEffect, type QueryMode, type ResolvedBootstrapEndpoint, RestController, type SSRInquiryBridge, type SSRInquiryBridgeOptions, type SecurityProfile, type ServerOptions, type ServiceInstanceDescriptor, ServiceRegistry, type ServiceTransportConfig, type ServiceTransportDescriptor, type ServiceTransportProtocol, type ServiceTransportRole, type ServiceTransportSecurityProfile, SignalController, SignalTransmissionTask, SocketController, type SortDirection, type SubOperation, type SubOperationType, type ValueOrSubOp, createSSRInquiryBridge, CadenzaService as default };