@emmett-community/emmett-google-services-bootstrap 0.1.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/README.md +164 -0
- package/dist/index.cjs +947 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +382 -0
- package/dist/index.d.ts +382 -0
- package/dist/index.js +908 -0
- package/dist/index.js.map +1 -0
- package/package.json +88 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,382 @@
|
|
|
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';
|
|
5
|
+
import { Firestore } from '@google-cloud/firestore';
|
|
6
|
+
import { PubSub } from '@google-cloud/pubsub';
|
|
7
|
+
import { Auth } from 'firebase-admin/auth';
|
|
8
|
+
import { app } from 'firebase-admin';
|
|
9
|
+
import { Database } from 'firebase-admin/database';
|
|
10
|
+
import { PubSubMessageBusLifecycle, SubscriptionOptions } from '@emmett-community/emmett-google-pubsub';
|
|
11
|
+
import { RealtimeDBInlineProjectionDefinition } from '@emmett-community/emmett-google-realtime-db';
|
|
12
|
+
export { realtimeDBInlineProjection } from '@emmett-community/emmett-google-realtime-db';
|
|
13
|
+
|
|
14
|
+
type LogLevel = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace' | 'silent';
|
|
15
|
+
/**
|
|
16
|
+
* Canonical logger contract used across Emmett packages.
|
|
17
|
+
* Matches Pino style (context, message).
|
|
18
|
+
*/
|
|
19
|
+
interface Logger {
|
|
20
|
+
debug(context: Record<string, unknown>, message?: string): void;
|
|
21
|
+
info(context: Record<string, unknown>, message?: string): void;
|
|
22
|
+
warn(context: Record<string, unknown>, message?: string): void;
|
|
23
|
+
error(context: Record<string, unknown>, message?: string): void;
|
|
24
|
+
}
|
|
25
|
+
type ObservabilityConfig = {
|
|
26
|
+
logger?: Logger;
|
|
27
|
+
logLevel?: LogLevel;
|
|
28
|
+
environment?: string;
|
|
29
|
+
createLogger?: boolean;
|
|
30
|
+
};
|
|
31
|
+
type FirebaseConfig = {
|
|
32
|
+
projectId?: string;
|
|
33
|
+
databaseURL?: string;
|
|
34
|
+
firestoreEmulatorHost?: string;
|
|
35
|
+
databaseEmulatorHost?: string;
|
|
36
|
+
};
|
|
37
|
+
type PubSubConfig = {
|
|
38
|
+
projectId?: string;
|
|
39
|
+
emulatorHost?: string;
|
|
40
|
+
topicPrefix?: string;
|
|
41
|
+
subscriptionOptions?: SubscriptionOptions;
|
|
42
|
+
autoCreateResources?: boolean;
|
|
43
|
+
cleanupOnClose?: boolean;
|
|
44
|
+
closePubSubClient?: boolean;
|
|
45
|
+
};
|
|
46
|
+
type DependencyCheck = {
|
|
47
|
+
name: string;
|
|
48
|
+
check: () => Promise<void>;
|
|
49
|
+
successMessage?: string;
|
|
50
|
+
timeoutMs?: number;
|
|
51
|
+
required?: boolean;
|
|
52
|
+
};
|
|
53
|
+
type GracefulShutdownConfig = {
|
|
54
|
+
timeoutMs?: number;
|
|
55
|
+
exitOnComplete?: boolean;
|
|
56
|
+
exitFn?: (code: number) => void;
|
|
57
|
+
};
|
|
58
|
+
type MessageBusWithLifecycle = MessageBus & EventSubscription & CommandProcessor & ScheduledMessageProcessor & PubSubMessageBusLifecycle;
|
|
59
|
+
interface FirebaseAdminLike {
|
|
60
|
+
initializeApp(options: {
|
|
61
|
+
projectId: string;
|
|
62
|
+
databaseURL?: string;
|
|
63
|
+
}, name?: string): app.App;
|
|
64
|
+
app?: () => app.App;
|
|
65
|
+
apps?: Array<app.App | null>;
|
|
66
|
+
}
|
|
67
|
+
type ServiceBootstrapOverrides = {
|
|
68
|
+
firebaseAdmin?: FirebaseAdminLike;
|
|
69
|
+
firestore?: Firestore;
|
|
70
|
+
database?: Database;
|
|
71
|
+
auth?: Auth;
|
|
72
|
+
pubsub?: PubSub;
|
|
73
|
+
eventStore?: EventStore;
|
|
74
|
+
messageBus?: MessageBusWithLifecycle;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Main configuration for ServiceBootstrap.
|
|
78
|
+
* Values can be provided explicitly or via environment variables.
|
|
79
|
+
*/
|
|
80
|
+
type ServiceBootstrapConfig = {
|
|
81
|
+
serviceName: string;
|
|
82
|
+
firebase?: FirebaseConfig;
|
|
83
|
+
pubsub?: PubSubConfig;
|
|
84
|
+
observability?: ObservabilityConfig;
|
|
85
|
+
projections?: RealtimeDBInlineProjectionDefinition[];
|
|
86
|
+
getCurrentTime?: () => Date;
|
|
87
|
+
dependencies?: DependencyCheck[];
|
|
88
|
+
includeDefaultDependencyChecks?: boolean;
|
|
89
|
+
autoStartMessageBus?: boolean;
|
|
90
|
+
shutdownOnDependencyFailure?: boolean;
|
|
91
|
+
shutdown?: GracefulShutdownConfig;
|
|
92
|
+
_testOverrides?: ServiceBootstrapOverrides;
|
|
93
|
+
};
|
|
94
|
+
type ResolvedFirebaseConfig = {
|
|
95
|
+
projectId: string;
|
|
96
|
+
databaseURL: string;
|
|
97
|
+
firestoreEmulatorHost?: string;
|
|
98
|
+
databaseEmulatorHost?: string;
|
|
99
|
+
};
|
|
100
|
+
type ResolvedPubSubConfig = {
|
|
101
|
+
projectId: string;
|
|
102
|
+
emulatorHost?: string;
|
|
103
|
+
topicPrefix: string;
|
|
104
|
+
subscriptionOptions?: SubscriptionOptions;
|
|
105
|
+
autoCreateResources: boolean;
|
|
106
|
+
cleanupOnClose: boolean;
|
|
107
|
+
closePubSubClient: boolean;
|
|
108
|
+
};
|
|
109
|
+
type ResolvedServiceBootstrapConfig = {
|
|
110
|
+
serviceName: string;
|
|
111
|
+
firebase: ResolvedFirebaseConfig;
|
|
112
|
+
pubsub: ResolvedPubSubConfig;
|
|
113
|
+
observability: ObservabilityConfig;
|
|
114
|
+
projections: RealtimeDBInlineProjectionDefinition[];
|
|
115
|
+
getCurrentTime: () => Date;
|
|
116
|
+
dependencies: DependencyCheck[];
|
|
117
|
+
includeDefaultDependencyChecks: boolean;
|
|
118
|
+
autoStartMessageBus: boolean;
|
|
119
|
+
shutdownOnDependencyFailure: boolean;
|
|
120
|
+
shutdown: GracefulShutdownConfig;
|
|
121
|
+
_testOverrides?: ServiceBootstrapOverrides;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
declare class DependencyTimeoutError extends Error {
|
|
125
|
+
constructor(timeoutMs: number);
|
|
126
|
+
}
|
|
127
|
+
type DependencyCheckResult = {
|
|
128
|
+
name: string;
|
|
129
|
+
ok: boolean;
|
|
130
|
+
required: boolean;
|
|
131
|
+
durationMs: number;
|
|
132
|
+
timedOut: boolean;
|
|
133
|
+
error?: unknown;
|
|
134
|
+
};
|
|
135
|
+
type DependencyCheckSummary = {
|
|
136
|
+
ok: boolean;
|
|
137
|
+
results: DependencyCheckResult[];
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Helper for one-off dependency checks with optional logging.
|
|
141
|
+
*/
|
|
142
|
+
declare const startDependency: (dependency: string, startFn: () => Promise<void>, options?: {
|
|
143
|
+
logger?: Logger;
|
|
144
|
+
successMessage?: string;
|
|
145
|
+
timeoutMs?: number;
|
|
146
|
+
}) => Promise<boolean>;
|
|
147
|
+
/**
|
|
148
|
+
* Executes dependency checks with optional timeouts.
|
|
149
|
+
*/
|
|
150
|
+
declare class DependencyChecker {
|
|
151
|
+
private readonly logger?;
|
|
152
|
+
private readonly defaultTimeoutMs?;
|
|
153
|
+
constructor(options?: {
|
|
154
|
+
logger?: Logger;
|
|
155
|
+
defaultTimeoutMs?: number;
|
|
156
|
+
});
|
|
157
|
+
check(dependency: DependencyCheck): Promise<DependencyCheckResult>;
|
|
158
|
+
checkAll(dependencies: DependencyCheck[]): Promise<DependencyCheckSummary>;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
declare class ShutdownTimeoutError extends Error {
|
|
162
|
+
constructor(timeoutMs: number);
|
|
163
|
+
}
|
|
164
|
+
type ShutdownTask = {
|
|
165
|
+
name: string;
|
|
166
|
+
close: () => Promise<void>;
|
|
167
|
+
timeoutMs?: number;
|
|
168
|
+
};
|
|
169
|
+
type ShutdownTaskResult = {
|
|
170
|
+
name: string;
|
|
171
|
+
ok: boolean;
|
|
172
|
+
timedOut: boolean;
|
|
173
|
+
durationMs: number;
|
|
174
|
+
error?: unknown;
|
|
175
|
+
};
|
|
176
|
+
type GracefulShutdownResult = {
|
|
177
|
+
reason: string;
|
|
178
|
+
exitCode: number;
|
|
179
|
+
tasks: ShutdownTaskResult[];
|
|
180
|
+
};
|
|
181
|
+
/**
|
|
182
|
+
* Runs shutdown tasks with timeout handling and optional exit.
|
|
183
|
+
*/
|
|
184
|
+
declare class GracefulShutdown {
|
|
185
|
+
private readonly logger?;
|
|
186
|
+
private readonly timeoutMs?;
|
|
187
|
+
private readonly exitOnComplete;
|
|
188
|
+
private readonly exitFn;
|
|
189
|
+
private readonly tasks;
|
|
190
|
+
private shuttingDown;
|
|
191
|
+
private lastResult?;
|
|
192
|
+
constructor(options?: GracefulShutdownConfig & {
|
|
193
|
+
logger?: Logger;
|
|
194
|
+
});
|
|
195
|
+
/**
|
|
196
|
+
* Register a shutdown task.
|
|
197
|
+
*/
|
|
198
|
+
register(task: ShutdownTask): void;
|
|
199
|
+
/**
|
|
200
|
+
* Execute all shutdown tasks and return the aggregated result.
|
|
201
|
+
*/
|
|
202
|
+
shutdown(reason: string, exitCode?: number): Promise<GracefulShutdownResult>;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Runtime context produced by ServiceBootstrap.
|
|
207
|
+
*/
|
|
208
|
+
type ServiceBootstrapContext = {
|
|
209
|
+
serviceName: string;
|
|
210
|
+
firebase: {
|
|
211
|
+
app: app.App;
|
|
212
|
+
firestore: Firestore;
|
|
213
|
+
database: Database;
|
|
214
|
+
auth: Auth;
|
|
215
|
+
};
|
|
216
|
+
firestore: Firestore;
|
|
217
|
+
database: Database;
|
|
218
|
+
auth: Auth;
|
|
219
|
+
pubsub: PubSub;
|
|
220
|
+
eventStore: EventStore;
|
|
221
|
+
messageBus: MessageBusWithLifecycle;
|
|
222
|
+
logger?: Logger;
|
|
223
|
+
getCurrentTime: () => Date;
|
|
224
|
+
config: ResolvedServiceBootstrapConfig;
|
|
225
|
+
};
|
|
226
|
+
/**
|
|
227
|
+
* Error thrown when required dependencies fail health checks.
|
|
228
|
+
*/
|
|
229
|
+
declare class DependencyUnavailableError extends Error {
|
|
230
|
+
readonly summary: DependencyCheckSummary;
|
|
231
|
+
constructor(summary: DependencyCheckSummary);
|
|
232
|
+
}
|
|
233
|
+
type CreateAppOptions = {
|
|
234
|
+
openApiPath: string;
|
|
235
|
+
handlersPath: string;
|
|
236
|
+
initializeHandlers: (handlers?: ImportedHandlerModules) => void | Promise<void>;
|
|
237
|
+
openApiOptions?: Partial<Omit<OpenApiValidatorOptions, 'apiSpec' | 'operationHandlers' | 'initializeHandlers'>>;
|
|
238
|
+
appOptions?: Omit<ApplicationOptions, 'openApiValidator' | 'observability'>;
|
|
239
|
+
};
|
|
240
|
+
type StartApiOptions = Omit<CreateAppOptions, 'initializeHandlers'> & {
|
|
241
|
+
initializeHandlers: (handlers: ImportedHandlerModules | undefined, ctx: ServiceBootstrapContext) => void | Promise<void>;
|
|
242
|
+
port?: number;
|
|
243
|
+
registerSignalHandlers?: boolean;
|
|
244
|
+
beforeStart?: (ctx: ServiceBootstrapContext) => void | Promise<void>;
|
|
245
|
+
};
|
|
246
|
+
/**
|
|
247
|
+
* Orchestrates Firebase, PubSub, event store, and lifecycle utilities.
|
|
248
|
+
*/
|
|
249
|
+
declare class ServiceBootstrap {
|
|
250
|
+
private readonly config;
|
|
251
|
+
private resolvedConfig?;
|
|
252
|
+
private logger?;
|
|
253
|
+
private context?;
|
|
254
|
+
private dependencyChecker?;
|
|
255
|
+
private shutdownManager?;
|
|
256
|
+
constructor(config: ServiceBootstrapConfig);
|
|
257
|
+
/**
|
|
258
|
+
* Resolve configuration with defaults and environment variables.
|
|
259
|
+
*/
|
|
260
|
+
getConfig(): ResolvedServiceBootstrapConfig;
|
|
261
|
+
/**
|
|
262
|
+
* Return the initialized context if available.
|
|
263
|
+
*/
|
|
264
|
+
getContext(): ServiceBootstrapContext | undefined;
|
|
265
|
+
/**
|
|
266
|
+
* Register process signal handlers that trigger shutdown.
|
|
267
|
+
*/
|
|
268
|
+
registerSignalHandlers(options?: {
|
|
269
|
+
signals?: NodeJS.Signals[];
|
|
270
|
+
exitCode?: number;
|
|
271
|
+
}): () => void;
|
|
272
|
+
/**
|
|
273
|
+
* Create an Express app using emmett-expressjs-with-openapi defaults.
|
|
274
|
+
*/
|
|
275
|
+
createApp(options: CreateAppOptions): Promise<Application>;
|
|
276
|
+
/**
|
|
277
|
+
* Initialize, run dependency checks, create the app, and start the HTTP server.
|
|
278
|
+
*/
|
|
279
|
+
startApi(options: StartApiOptions): Promise<{
|
|
280
|
+
app: Application;
|
|
281
|
+
server: ReturnType<typeof startAPI>;
|
|
282
|
+
context: ServiceBootstrapContext;
|
|
283
|
+
}>;
|
|
284
|
+
/**
|
|
285
|
+
* Initialize Firebase, PubSub, and event store wiring.
|
|
286
|
+
* Does not execute dependency checks.
|
|
287
|
+
*/
|
|
288
|
+
initialize(): Promise<ServiceBootstrapContext>;
|
|
289
|
+
/**
|
|
290
|
+
* Initialize and run dependency checks before returning context.
|
|
291
|
+
*/
|
|
292
|
+
start(): Promise<ServiceBootstrapContext>;
|
|
293
|
+
/**
|
|
294
|
+
* Execute graceful shutdown tasks and return the shutdown result.
|
|
295
|
+
*/
|
|
296
|
+
shutdown(reason: string, exitCode?: number): Promise<GracefulShutdownResult>;
|
|
297
|
+
private getLogger;
|
|
298
|
+
private resolveConfig;
|
|
299
|
+
private getDatabaseURL;
|
|
300
|
+
private getDependencyChecker;
|
|
301
|
+
private getShutdownManager;
|
|
302
|
+
private registerDefaultShutdownTasks;
|
|
303
|
+
private buildDefaultDependencyChecks;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
type FirebaseInitializationOptions = {
|
|
307
|
+
projectId: string;
|
|
308
|
+
databaseURL?: string;
|
|
309
|
+
firestoreEmulatorHost?: string;
|
|
310
|
+
databaseEmulatorHost?: string;
|
|
311
|
+
logger?: Logger;
|
|
312
|
+
_firebaseAdmin?: FirebaseAdminLike;
|
|
313
|
+
};
|
|
314
|
+
type FirebaseInitResult = {
|
|
315
|
+
app: app.App;
|
|
316
|
+
firestore: Firestore;
|
|
317
|
+
database: Database;
|
|
318
|
+
auth: Auth;
|
|
319
|
+
};
|
|
320
|
+
/**
|
|
321
|
+
* Initializes Firebase Admin SDK with emulator support.
|
|
322
|
+
*/
|
|
323
|
+
declare class FirebaseInitializer {
|
|
324
|
+
private readonly options;
|
|
325
|
+
constructor(options: FirebaseInitializationOptions);
|
|
326
|
+
/**
|
|
327
|
+
* Returns true if emulator hosts are configured.
|
|
328
|
+
*/
|
|
329
|
+
isEmulatorMode(): boolean;
|
|
330
|
+
/**
|
|
331
|
+
* Initialize Firebase Admin and return service handles.
|
|
332
|
+
*/
|
|
333
|
+
initialize(): Promise<FirebaseInitResult>;
|
|
334
|
+
private getOrCreateApp;
|
|
335
|
+
private getDatabaseURL;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
type PubSubInitializationOptions = {
|
|
339
|
+
projectId: string;
|
|
340
|
+
emulatorHost?: string;
|
|
341
|
+
logger?: Logger;
|
|
342
|
+
_pubsub?: PubSub;
|
|
343
|
+
};
|
|
344
|
+
/**
|
|
345
|
+
* Initializes PubSub client with optional emulator support.
|
|
346
|
+
*/
|
|
347
|
+
declare class PubSubInitializer {
|
|
348
|
+
private readonly options;
|
|
349
|
+
constructor(options: PubSubInitializationOptions);
|
|
350
|
+
/**
|
|
351
|
+
* Returns true if emulator host is configured.
|
|
352
|
+
*/
|
|
353
|
+
isEmulatorMode(): boolean;
|
|
354
|
+
/**
|
|
355
|
+
* Initialize PubSub client or return injected instance.
|
|
356
|
+
*/
|
|
357
|
+
initialize(): Promise<PubSub>;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
type SignalHandlerOptions = {
|
|
361
|
+
shutdown: (reason: string, exitCode?: number) => void | Promise<void>;
|
|
362
|
+
signals?: NodeJS.Signals[];
|
|
363
|
+
logger?: Logger;
|
|
364
|
+
exitCode?: number;
|
|
365
|
+
};
|
|
366
|
+
/**
|
|
367
|
+
* Register process signal handlers and return an unregister function.
|
|
368
|
+
*/
|
|
369
|
+
declare const registerSignalHandlers: (options: SignalHandlerOptions) => (() => void);
|
|
370
|
+
|
|
371
|
+
type EventStoreFactoryOptions = {
|
|
372
|
+
firestore: Firestore;
|
|
373
|
+
database: Database;
|
|
374
|
+
projections?: RealtimeDBInlineProjectionDefinition[];
|
|
375
|
+
logger?: Logger;
|
|
376
|
+
};
|
|
377
|
+
/**
|
|
378
|
+
* Create a Firestore-backed event store and wire RealtimeDB projections if provided.
|
|
379
|
+
*/
|
|
380
|
+
declare const createEventStore: <T extends EventStore = EventStore>(options: EventStoreFactoryOptions) => T;
|
|
381
|
+
|
|
382
|
+
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 };
|