@emmett-community/emmett-google-services-bootstrap 0.3.0 → 0.5.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.ts CHANGED
@@ -1,314 +1,31 @@
1
- import { Application } from 'express';
2
- import { ImportedHandlerModules, OpenApiValidatorOptions, ApplicationOptions, startAPI } from '@emmett-community/emmett-expressjs-with-openapi';
3
- export { ApiE2ESpecification, ApiSpecification, ImportedHandlerModules, NoContent, OK, TestRequest, createFirebaseAuthSecurityHandlers, existingStream, expectNewEvents, expectResponse, on } from '@emmett-community/emmett-expressjs-with-openapi';
4
- import { MessageBus, EventSubscription, CommandProcessor, ScheduledMessageProcessor, EventStore } from '@event-driven-io/emmett';
1
+ import { S as ServiceBootstrapContext, L as Logger, F as FirebaseAdminLike } from './ServiceBootstrap-BjFoQDd_.js';
2
+ export { C as CreateAppOptions, e as DependencyCheck, k as DependencyCheckResult, l as DependencyCheckSummary, m as DependencyChecker, j as DependencyTimeoutError, D as DependencyUnavailableError, d as FirebaseConfig, r as GracefulShutdown, G as GracefulShutdownConfig, q as GracefulShutdownResult, c as LogLevel, M as MessageBusWithLifecycle, O as ObservabilityConfig, P as PubSubConfig, R as ResolvedFirebaseConfig, h as ResolvedPubSubConfig, i as ResolvedServiceBootstrapConfig, b as ServiceBootstrap, g as ServiceBootstrapConfig, f as ServiceBootstrapOverrides, o as ShutdownTask, p as ShutdownTaskResult, n as ShutdownTimeoutError, a as StartApiOptions, s as startDependency } from './ServiceBootstrap-BjFoQDd_.js';
3
+ import * as express from 'express';
4
+ import * as qs from 'qs';
5
+ import * as express_serve_static_core from 'express-serve-static-core';
5
6
  import { Firestore } from '@google-cloud/firestore';
6
- import { PubSub } from '@google-cloud/pubsub';
7
7
  import { Auth } from 'firebase-admin/auth';
8
8
  import { app } from 'firebase-admin';
9
9
  import { Database } from 'firebase-admin/database';
10
- import { PubSubMessageBusLifecycle, SubscriptionOptions } from '@emmett-community/emmett-google-pubsub';
10
+ import { PubSub } from '@google-cloud/pubsub';
11
+ import { FirestoreEventStoreOptions } from '@emmett-community/emmett-google-firestore';
11
12
  import { RealtimeDBInlineProjectionDefinition } from '@emmett-community/emmett-google-realtime-db';
12
13
  export { realtimeDBInlineProjection } from '@emmett-community/emmett-google-realtime-db';
13
- import { FirestoreEventStoreOptions } from '@emmett-community/emmett-google-firestore';
14
-
15
- type LogLevel = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace' | 'silent';
16
- /**
17
- * Canonical logger contract used across Emmett packages.
18
- * Matches Pino style (context, message).
19
- */
20
- interface Logger {
21
- debug(context: Record<string, unknown>, message?: string): void;
22
- info(context: Record<string, unknown>, message?: string): void;
23
- warn(context: Record<string, unknown>, message?: string): void;
24
- error(context: Record<string, unknown>, message?: string): void;
25
- }
26
- type ObservabilityConfig = {
27
- logger?: Logger;
28
- logLevel?: LogLevel;
29
- environment?: string;
30
- createLogger?: boolean;
31
- };
32
- type FirebaseConfig = {
33
- projectId?: string;
34
- databaseURL?: string;
35
- firestoreEmulatorHost?: string;
36
- databaseEmulatorHost?: string;
37
- };
38
- type PubSubConfig = {
39
- projectId?: string;
40
- emulatorHost?: string;
41
- topicPrefix?: string;
42
- subscriptionOptions?: SubscriptionOptions;
43
- autoCreateResources?: boolean;
44
- cleanupOnClose?: boolean;
45
- closePubSubClient?: boolean;
46
- };
47
- type DependencyCheck = {
48
- name: string;
49
- check: () => Promise<void>;
50
- successMessage?: string;
51
- timeoutMs?: number;
52
- required?: boolean;
53
- };
54
- type GracefulShutdownConfig = {
55
- timeoutMs?: number;
56
- exitOnComplete?: boolean;
57
- exitFn?: (code: number) => void;
58
- };
59
- type MessageBusWithLifecycle = MessageBus & EventSubscription & CommandProcessor & ScheduledMessageProcessor & PubSubMessageBusLifecycle;
60
- interface FirebaseAdminLike {
61
- initializeApp(options: {
62
- projectId: string;
63
- databaseURL?: string;
64
- }, name?: string): app.App;
65
- app?: () => app.App;
66
- apps?: Array<app.App | null>;
67
- }
68
- type ServiceBootstrapOverrides = {
69
- firebaseAdmin?: FirebaseAdminLike;
70
- firestore?: Firestore;
71
- database?: Database;
72
- auth?: Auth;
73
- pubsub?: PubSub;
74
- eventStore?: EventStore;
75
- messageBus?: MessageBusWithLifecycle;
76
- };
77
- /**
78
- * Main configuration for ServiceBootstrap.
79
- * Values can be provided explicitly or via environment variables.
80
- */
81
- type ServiceBootstrapConfig = {
82
- serviceName: string;
83
- firebase?: FirebaseConfig;
84
- pubsub?: PubSubConfig;
85
- eventStore?: {
86
- collections?: FirestoreEventStoreOptions['collections'];
87
- };
88
- observability?: ObservabilityConfig;
89
- projections?: RealtimeDBInlineProjectionDefinition[];
90
- getCurrentTime?: () => Date;
91
- dependencies?: DependencyCheck[];
92
- includeDefaultDependencyChecks?: boolean;
93
- autoStartMessageBus?: boolean;
94
- shutdownOnDependencyFailure?: boolean;
95
- shutdown?: GracefulShutdownConfig;
96
- _testOverrides?: ServiceBootstrapOverrides;
97
- };
98
- type ResolvedFirebaseConfig = {
99
- projectId: string;
100
- databaseURL: string;
101
- firestoreEmulatorHost?: string;
102
- databaseEmulatorHost?: string;
103
- };
104
- type ResolvedPubSubConfig = {
105
- projectId: string;
106
- emulatorHost?: string;
107
- topicPrefix: string;
108
- subscriptionOptions?: SubscriptionOptions;
109
- autoCreateResources: boolean;
110
- cleanupOnClose: boolean;
111
- closePubSubClient: boolean;
112
- };
113
- type ResolvedServiceBootstrapConfig = {
114
- serviceName: string;
115
- firebase: ResolvedFirebaseConfig;
116
- pubsub: ResolvedPubSubConfig;
117
- eventStore: {
118
- collections?: FirestoreEventStoreOptions['collections'];
119
- };
120
- observability: ObservabilityConfig;
121
- projections: RealtimeDBInlineProjectionDefinition[];
122
- getCurrentTime: () => Date;
123
- dependencies: DependencyCheck[];
124
- includeDefaultDependencyChecks: boolean;
125
- autoStartMessageBus: boolean;
126
- shutdownOnDependencyFailure: boolean;
127
- shutdown: GracefulShutdownConfig;
128
- _testOverrides?: ServiceBootstrapOverrides;
129
- };
130
-
131
- declare class DependencyTimeoutError extends Error {
132
- constructor(timeoutMs: number);
133
- }
134
- type DependencyCheckResult = {
135
- name: string;
136
- ok: boolean;
137
- required: boolean;
138
- durationMs: number;
139
- timedOut: boolean;
140
- error?: unknown;
141
- };
142
- type DependencyCheckSummary = {
143
- ok: boolean;
144
- results: DependencyCheckResult[];
145
- };
146
- /**
147
- * Helper for one-off dependency checks with optional logging.
148
- */
149
- declare const startDependency: (dependency: string, startFn: () => Promise<void>, options?: {
150
- logger?: Logger;
151
- successMessage?: string;
152
- timeoutMs?: number;
153
- }) => Promise<boolean>;
154
- /**
155
- * Executes dependency checks with optional timeouts.
156
- */
157
- declare class DependencyChecker {
158
- private readonly logger?;
159
- private readonly defaultTimeoutMs?;
160
- constructor(options?: {
161
- logger?: Logger;
162
- defaultTimeoutMs?: number;
163
- });
164
- check(dependency: DependencyCheck): Promise<DependencyCheckResult>;
165
- checkAll(dependencies: DependencyCheck[]): Promise<DependencyCheckSummary>;
166
- }
14
+ import { EventStore } from '@event-driven-io/emmett';
15
+ export { ImportedHandlerModules, NoContent, OK, createFirebaseAuthSecurityHandlers, lazyApplication, on } from '@emmett-community/emmett-expressjs-with-openapi';
16
+ import '@emmett-community/emmett-google-pubsub';
167
17
 
168
- declare class ShutdownTimeoutError extends Error {
169
- constructor(timeoutMs: number);
170
- }
171
- type ShutdownTask = {
172
- name: string;
173
- close: () => Promise<void>;
174
- timeoutMs?: number;
175
- };
176
- type ShutdownTaskResult = {
177
- name: string;
178
- ok: boolean;
179
- timedOut: boolean;
180
- durationMs: number;
181
- error?: unknown;
182
- };
183
- type GracefulShutdownResult = {
184
- reason: string;
185
- exitCode: number;
186
- tasks: ShutdownTaskResult[];
187
- };
188
18
  /**
189
- * Runs shutdown tasks with timeout handling and optional exit.
19
+ * Handler for `GET /health` (liveness): the process is up and serving requests.
20
+ * Wire it to the `getHealth` operation of the service contract.
190
21
  */
191
- declare class GracefulShutdown {
192
- private readonly logger?;
193
- private readonly timeoutMs?;
194
- private readonly exitOnComplete;
195
- private readonly exitFn;
196
- private readonly tasks;
197
- private shuttingDown;
198
- private lastResult?;
199
- constructor(options?: GracefulShutdownConfig & {
200
- logger?: Logger;
201
- });
202
- /**
203
- * Register a shutdown task.
204
- */
205
- register(task: ShutdownTask): void;
206
- /**
207
- * Execute all shutdown tasks and return the aggregated result.
208
- */
209
- shutdown(reason: string, exitCode?: number): Promise<GracefulShutdownResult>;
210
- }
211
-
22
+ declare const getHealth: (request: express.Request<express_serve_static_core.ParamsDictionary, any, any, qs.ParsedQs, Record<string, any>>, response: express.Response, _next: express.NextFunction) => Promise<void>;
212
23
  /**
213
- * Runtime context produced by ServiceBootstrap.
24
+ * Handler for `GET /ready` (readiness): answers 200 when the stores, the message bus and the
25
+ * configured dependencies answer, and 503 with the failed checks otherwise.
26
+ * Wire it to the `getReady` operation of the service contract.
214
27
  */
215
- type ServiceBootstrapContext = {
216
- serviceName: string;
217
- firebase: {
218
- app: app.App;
219
- firestore: Firestore;
220
- database: Database;
221
- auth: Auth;
222
- };
223
- firestore: Firestore;
224
- database: Database;
225
- auth: Auth;
226
- pubsub: PubSub;
227
- eventStore: EventStore;
228
- messageBus: MessageBusWithLifecycle;
229
- logger?: Logger;
230
- getCurrentTime: () => Date;
231
- config: ResolvedServiceBootstrapConfig;
232
- };
233
- /**
234
- * Error thrown when required dependencies fail health checks.
235
- */
236
- declare class DependencyUnavailableError extends Error {
237
- readonly summary: DependencyCheckSummary;
238
- constructor(summary: DependencyCheckSummary);
239
- }
240
- type CreateAppOptions = {
241
- openApiPath: string;
242
- handlersPath: string;
243
- initializeHandlers: (handlers?: ImportedHandlerModules) => void | Promise<void>;
244
- openApiOptions?: Partial<Omit<OpenApiValidatorOptions, 'apiSpec' | 'operationHandlers' | 'initializeHandlers'>>;
245
- appOptions?: Omit<ApplicationOptions, 'openApiValidator' | 'observability'>;
246
- };
247
- type StartApiOptions = Omit<CreateAppOptions, 'initializeHandlers'> & {
248
- initializeHandlers: (handlers: ImportedHandlerModules | undefined, ctx: ServiceBootstrapContext) => void | Promise<void>;
249
- port?: number;
250
- registerSignalHandlers?: boolean;
251
- beforeStart?: (ctx: ServiceBootstrapContext) => void | Promise<void>;
252
- };
253
- /**
254
- * Orchestrates Firebase, PubSub, event store, and lifecycle utilities.
255
- */
256
- declare class ServiceBootstrap {
257
- private readonly config;
258
- private resolvedConfig?;
259
- private logger?;
260
- private context?;
261
- private dependencyChecker?;
262
- private shutdownManager?;
263
- constructor(config: ServiceBootstrapConfig);
264
- /**
265
- * Resolve configuration with defaults and environment variables.
266
- */
267
- getConfig(): ResolvedServiceBootstrapConfig;
268
- /**
269
- * Return the initialized context if available.
270
- */
271
- getContext(): ServiceBootstrapContext | undefined;
272
- /**
273
- * Register process signal handlers that trigger shutdown.
274
- */
275
- registerSignalHandlers(options?: {
276
- signals?: NodeJS.Signals[];
277
- exitCode?: number;
278
- }): () => void;
279
- /**
280
- * Create an Express app using emmett-expressjs-with-openapi defaults.
281
- */
282
- createApp(options: CreateAppOptions): Promise<Application>;
283
- /**
284
- * Initialize, run dependency checks, create the app, and start the HTTP server.
285
- */
286
- startApi(options: StartApiOptions): Promise<{
287
- app: Application;
288
- server: ReturnType<typeof startAPI>;
289
- context: ServiceBootstrapContext;
290
- }>;
291
- /**
292
- * Initialize Firebase, PubSub, and event store wiring.
293
- * Does not execute dependency checks.
294
- */
295
- initialize(): Promise<ServiceBootstrapContext>;
296
- /**
297
- * Initialize and run dependency checks before returning context.
298
- */
299
- start(): Promise<ServiceBootstrapContext>;
300
- /**
301
- * Execute graceful shutdown tasks and return the shutdown result.
302
- */
303
- shutdown(reason: string, exitCode?: number): Promise<GracefulShutdownResult>;
304
- private getLogger;
305
- private resolveConfig;
306
- private getDatabaseURL;
307
- private getDependencyChecker;
308
- private getShutdownManager;
309
- private registerDefaultShutdownTasks;
310
- private buildDefaultDependencyChecks;
311
- }
28
+ declare const getReady: (ctx: Pick<ServiceBootstrapContext, "checkReadiness">) => (request: express.Request<express_serve_static_core.ParamsDictionary, any, any, qs.ParsedQs, Record<string, any>>, response: express.Response, _next: express.NextFunction) => Promise<void>;
312
29
 
313
30
  type FirebaseInitializationOptions = {
314
31
  projectId: string;
@@ -381,10 +98,11 @@ type EventStoreFactoryOptions = {
381
98
  projections?: RealtimeDBInlineProjectionDefinition[];
382
99
  logger?: Logger;
383
100
  collections?: FirestoreEventStoreOptions['collections'];
101
+ hooks?: FirestoreEventStoreOptions['hooks'];
384
102
  };
385
103
  /**
386
104
  * Create a Firestore-backed event store and wire RealtimeDB projections if provided.
387
105
  */
388
106
  declare const createEventStore: <T extends EventStore = EventStore>(options: EventStoreFactoryOptions) => T;
389
107
 
390
- export { type CreateAppOptions, type DependencyCheck, type DependencyCheckResult, type DependencyCheckSummary, DependencyChecker, DependencyTimeoutError, DependencyUnavailableError, type EventStoreFactoryOptions, type FirebaseAdminLike, type FirebaseConfig, type FirebaseInitResult, type FirebaseInitializationOptions, FirebaseInitializer, GracefulShutdown, type GracefulShutdownConfig, type GracefulShutdownResult, type LogLevel, type Logger, type MessageBusWithLifecycle, type ObservabilityConfig, type PubSubConfig, type PubSubInitializationOptions, PubSubInitializer, type ResolvedFirebaseConfig, type ResolvedPubSubConfig, type ResolvedServiceBootstrapConfig, ServiceBootstrap, type ServiceBootstrapConfig, type ServiceBootstrapContext, type ServiceBootstrapOverrides, type ShutdownTask, type ShutdownTaskResult, ShutdownTimeoutError, type SignalHandlerOptions, type StartApiOptions, createEventStore, registerSignalHandlers, startDependency };
108
+ export { type EventStoreFactoryOptions, FirebaseAdminLike, type FirebaseInitResult, type FirebaseInitializationOptions, FirebaseInitializer, Logger, type PubSubInitializationOptions, PubSubInitializer, ServiceBootstrapContext, type SignalHandlerOptions, createEventStore, getHealth, getReady, registerSignalHandlers };