@bsb/base 9.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/LICENSE +665 -0
- package/LICENSE.commercial +32 -0
- package/README.md +263 -0
- package/bsb-plugin.json +62 -0
- package/lib/base/BSBConfig.d.ts +130 -0
- package/lib/base/BSBConfig.js +95 -0
- package/lib/base/BSBConfig.js.map +1 -0
- package/lib/base/BSBEvents.d.ts +207 -0
- package/lib/base/BSBEvents.js +101 -0
- package/lib/base/BSBEvents.js.map +1 -0
- package/lib/base/BSBObservable.d.ts +178 -0
- package/lib/base/BSBObservable.js +91 -0
- package/lib/base/BSBObservable.js.map +1 -0
- package/lib/base/BSBService.d.ts +277 -0
- package/lib/base/BSBService.js +366 -0
- package/lib/base/BSBService.js.map +1 -0
- package/lib/base/BSBServiceClient.d.ts +135 -0
- package/lib/base/BSBServiceClient.js +130 -0
- package/lib/base/BSBServiceClient.js.map +1 -0
- package/lib/base/EventValidator.d.ts +137 -0
- package/lib/base/EventValidator.js +210 -0
- package/lib/base/EventValidator.js.map +1 -0
- package/lib/base/ObservableBackend.d.ts +281 -0
- package/lib/base/ObservableBackend.js +515 -0
- package/lib/base/ObservableBackend.js.map +1 -0
- package/lib/base/PluginConfig.d.ts +196 -0
- package/lib/base/PluginConfig.js +96 -0
- package/lib/base/PluginConfig.js.map +1 -0
- package/lib/base/PluginEvents.d.ts +140 -0
- package/lib/base/PluginEvents.js +268 -0
- package/lib/base/PluginEvents.js.map +1 -0
- package/lib/base/PluginObservable.d.ts +196 -0
- package/lib/base/PluginObservable.js +250 -0
- package/lib/base/PluginObservable.js.map +1 -0
- package/lib/base/ResourceContext.d.ts +70 -0
- package/lib/base/ResourceContext.js +54 -0
- package/lib/base/ResourceContext.js.map +1 -0
- package/lib/base/base.d.ts +264 -0
- package/lib/base/base.js +182 -0
- package/lib/base/base.js.map +1 -0
- package/lib/base/errorMessages.d.ts +56 -0
- package/lib/base/errorMessages.js +70 -0
- package/lib/base/errorMessages.js.map +1 -0
- package/lib/base/factory.d.ts +58 -0
- package/lib/base/factory.js +167 -0
- package/lib/base/factory.js.map +1 -0
- package/lib/base/functions.d.ts +117 -0
- package/lib/base/functions.js +152 -0
- package/lib/base/functions.js.map +1 -0
- package/lib/base/index.d.ts +44 -0
- package/lib/base/index.js +64 -0
- package/lib/base/index.js.map +1 -0
- package/lib/base/logFormatter.d.ts +50 -0
- package/lib/base/logFormatter.js +105 -0
- package/lib/base/logFormatter.js.map +1 -0
- package/lib/base/tools.d.ts +316 -0
- package/lib/base/tools.js +666 -0
- package/lib/base/tools.js.map +1 -0
- package/lib/cli.d.ts +28 -0
- package/lib/cli.js +254 -0
- package/lib/cli.js.map +1 -0
- package/lib/dev.d.ts +27 -0
- package/lib/dev.js +200 -0
- package/lib/dev.js.map +1 -0
- package/lib/index.d.ts +32 -0
- package/lib/index.js +49 -0
- package/lib/index.js.map +1 -0
- package/lib/interfaces/events.d.ts +67 -0
- package/lib/interfaces/events.js +44 -0
- package/lib/interfaces/events.js.map +1 -0
- package/lib/interfaces/index.d.ts +38 -0
- package/lib/interfaces/index.js +59 -0
- package/lib/interfaces/index.js.map +1 -0
- package/lib/interfaces/logging.d.ts +106 -0
- package/lib/interfaces/logging.js +39 -0
- package/lib/interfaces/logging.js.map +1 -0
- package/lib/interfaces/metrics.d.ts +365 -0
- package/lib/interfaces/metrics.js +46 -0
- package/lib/interfaces/metrics.js.map +1 -0
- package/lib/interfaces/observable-types.d.ts +63 -0
- package/lib/interfaces/observable-types.js +49 -0
- package/lib/interfaces/observable-types.js.map +1 -0
- package/lib/interfaces/observable.d.ts +297 -0
- package/lib/interfaces/observable.js +29 -0
- package/lib/interfaces/observable.js.map +1 -0
- package/lib/interfaces/options.d.ts +164 -0
- package/lib/interfaces/options.js +56 -0
- package/lib/interfaces/options.js.map +1 -0
- package/lib/interfaces/plugins.d.ts +143 -0
- package/lib/interfaces/plugins.js +45 -0
- package/lib/interfaces/plugins.js.map +1 -0
- package/lib/interfaces/result.d.ts +129 -0
- package/lib/interfaces/result.js +162 -0
- package/lib/interfaces/result.js.map +1 -0
- package/lib/interfaces/schema-events.d.ts +378 -0
- package/lib/interfaces/schema-events.js +247 -0
- package/lib/interfaces/schema-events.js.map +1 -0
- package/lib/interfaces/schema-types.d.ts +407 -0
- package/lib/interfaces/schema-types.js +581 -0
- package/lib/interfaces/schema-types.js.map +1 -0
- package/lib/interfaces/service.d.ts +48 -0
- package/lib/interfaces/service.js +29 -0
- package/lib/interfaces/service.js.map +1 -0
- package/lib/interfaces/tools.d.ts +65 -0
- package/lib/interfaces/tools.js +50 -0
- package/lib/interfaces/tools.js.map +1 -0
- package/lib/plugins/config-default/index.d.ts +59 -0
- package/lib/plugins/config-default/index.js +197 -0
- package/lib/plugins/config-default/index.js.map +1 -0
- package/lib/plugins/config-default/interfaces.d.ts +92 -0
- package/lib/plugins/config-default/interfaces.js +36 -0
- package/lib/plugins/config-default/interfaces.js.map +1 -0
- package/lib/plugins/events-default/events/broadcast.d.ts +36 -0
- package/lib/plugins/events-default/events/broadcast.js +85 -0
- package/lib/plugins/events-default/events/broadcast.js.map +1 -0
- package/lib/plugins/events-default/events/emit.d.ts +38 -0
- package/lib/plugins/events-default/events/emit.js +104 -0
- package/lib/plugins/events-default/events/emit.js.map +1 -0
- package/lib/plugins/events-default/events/emitAndReturn.d.ts +36 -0
- package/lib/plugins/events-default/events/emitAndReturn.js +100 -0
- package/lib/plugins/events-default/events/emitAndReturn.js.map +1 -0
- package/lib/plugins/events-default/events/emitStreamAndReceiveStream.d.ts +38 -0
- package/lib/plugins/events-default/events/emitStreamAndReceiveStream.js +134 -0
- package/lib/plugins/events-default/events/emitStreamAndReceiveStream.js.map +1 -0
- package/lib/plugins/events-default/events/index.d.ts +30 -0
- package/lib/plugins/events-default/events/index.js +38 -0
- package/lib/plugins/events-default/events/index.js.map +1 -0
- package/lib/plugins/events-default/index.d.ts +57 -0
- package/lib/plugins/events-default/index.js +86 -0
- package/lib/plugins/events-default/index.js.map +1 -0
- package/lib/plugins/observable-default/index.d.ts +43 -0
- package/lib/plugins/observable-default/index.js +151 -0
- package/lib/plugins/observable-default/index.js.map +1 -0
- package/lib/schemas/config-default.json +34 -0
- package/lib/schemas/config-default.plugin.json +36 -0
- package/lib/schemas/events-default.json +18 -0
- package/lib/schemas/events-default.plugin.json +17 -0
- package/lib/schemas/observable-default.json +33 -0
- package/lib/schemas/observable-default.plugin.json +24 -0
- package/lib/scripts/bsb-client-cli.d.ts +21 -0
- package/lib/scripts/bsb-client-cli.js +701 -0
- package/lib/scripts/bsb-client-cli.js.map +1 -0
- package/lib/scripts/bsb-plugin-cli.d.ts +15 -0
- package/lib/scripts/bsb-plugin-cli.js +547 -0
- package/lib/scripts/bsb-plugin-cli.js.map +1 -0
- package/lib/scripts/export-schemas.d.ts +17 -0
- package/lib/scripts/export-schemas.js +205 -0
- package/lib/scripts/export-schemas.js.map +1 -0
- package/lib/scripts/extract-schemas-from-source.d.ts +23 -0
- package/lib/scripts/extract-schemas-from-source.js +604 -0
- package/lib/scripts/extract-schemas-from-source.js.map +1 -0
- package/lib/scripts/generate-client-types.d.ts +22 -0
- package/lib/scripts/generate-client-types.js +537 -0
- package/lib/scripts/generate-client-types.js.map +1 -0
- package/lib/scripts/generate-plugin-json.d.ts +17 -0
- package/lib/scripts/generate-plugin-json.js +219 -0
- package/lib/scripts/generate-plugin-json.js.map +1 -0
- package/lib/serviceBase/config.d.ts +83 -0
- package/lib/serviceBase/config.js +236 -0
- package/lib/serviceBase/config.js.map +1 -0
- package/lib/serviceBase/events.d.ts +91 -0
- package/lib/serviceBase/events.js +519 -0
- package/lib/serviceBase/events.js.map +1 -0
- package/lib/serviceBase/index.d.ts +33 -0
- package/lib/serviceBase/index.js +50 -0
- package/lib/serviceBase/index.js.map +1 -0
- package/lib/serviceBase/observable.d.ts +249 -0
- package/lib/serviceBase/observable.js +551 -0
- package/lib/serviceBase/observable.js.map +1 -0
- package/lib/serviceBase/plugins.d.ts +48 -0
- package/lib/serviceBase/plugins.js +184 -0
- package/lib/serviceBase/plugins.js.map +1 -0
- package/lib/serviceBase/serviceBase.d.ts +228 -0
- package/lib/serviceBase/serviceBase.js +420 -0
- package/lib/serviceBase/serviceBase.js.map +1 -0
- package/lib/serviceBase/services.d.ts +63 -0
- package/lib/serviceBase/services.js +346 -0
- package/lib/serviceBase/services.js.map +1 -0
- package/lib/tests.d.ts +27 -0
- package/lib/tests.js +44 -0
- package/lib/tests.js.map +1 -0
- package/package.json +91 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BSB (Better-Service-Base) is an event-bus based microservice framework.
|
|
3
|
+
* Copyright (C) 2016 - 2025 BetterCorp (PTY) Ltd
|
|
4
|
+
*
|
|
5
|
+
* This program is free software: you can redistribute it and/or modify
|
|
6
|
+
* it under the terms of the GNU Affero General Public License as published
|
|
7
|
+
* by the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
* (at your option) any later version.
|
|
9
|
+
*
|
|
10
|
+
* Alternatively, you may obtain a commercial license for this program.
|
|
11
|
+
* The commercial license allows you to use the Program in a closed-source manner,
|
|
12
|
+
* including the right to create derivative works that are not subject to the terms
|
|
13
|
+
* of the AGPL.
|
|
14
|
+
*
|
|
15
|
+
* To obtain a commercial license, please contact the copyright holders at
|
|
16
|
+
* https://www.bettercorp.dev. The terms and conditions of the commercial license
|
|
17
|
+
* will be provided upon request.
|
|
18
|
+
*
|
|
19
|
+
* This program is distributed in the hope that it will be useful,
|
|
20
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
21
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
22
|
+
* GNU Affero General Public License for more details.
|
|
23
|
+
*
|
|
24
|
+
* You should have received a copy of the GNU Affero General Public License
|
|
25
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
26
|
+
*/
|
|
27
|
+
import { DTrace } from './metrics';
|
|
28
|
+
import { SmartLogMeta } from './logging';
|
|
29
|
+
import { Counter, Gauge, Histogram, Timer } from './metrics';
|
|
30
|
+
import { ResourceContext } from '../base/ResourceContext';
|
|
31
|
+
import { z } from 'zod';
|
|
32
|
+
/**
|
|
33
|
+
* Observable context for unified observability across logging, metrics, and tracing.
|
|
34
|
+
*
|
|
35
|
+
* Observable replaces DTrace in the public API and provides a unified interface for:
|
|
36
|
+
* - Structured logging with automatic trace context
|
|
37
|
+
* - Metrics creation with resource context
|
|
38
|
+
* - Distributed tracing with W3C trace context
|
|
39
|
+
* - Attribute propagation to child spans
|
|
40
|
+
*
|
|
41
|
+
* @group Observability
|
|
42
|
+
* @category Core
|
|
43
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/interfaces/Observable.html | API: Observable}
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* // In a service plugin
|
|
48
|
+
* public async run(obs: Observable) {
|
|
49
|
+
* // Logging
|
|
50
|
+
* obs.log.info("Starting service");
|
|
51
|
+
*
|
|
52
|
+
* // Set attributes for all child operations
|
|
53
|
+
* const withUser = obs.setAttribute("user.id", "123");
|
|
54
|
+
*
|
|
55
|
+
* // Create child span
|
|
56
|
+
* const childObs = withUser.startSpan("database-query");
|
|
57
|
+
* childObs.log.debug("Querying database");
|
|
58
|
+
* // ... do work ...
|
|
59
|
+
* childObs.end();
|
|
60
|
+
* }
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export interface Observable<TAttributeSchema extends z.ZodSchema = z.ZodAny> {
|
|
64
|
+
/**
|
|
65
|
+
* Core trace information (W3C compatible)
|
|
66
|
+
* @readonly
|
|
67
|
+
*/
|
|
68
|
+
readonly trace: DTrace;
|
|
69
|
+
/**
|
|
70
|
+
* Trace ID (unique identifier for the entire trace)
|
|
71
|
+
* @readonly
|
|
72
|
+
*/
|
|
73
|
+
readonly traceId: string;
|
|
74
|
+
/**
|
|
75
|
+
* Span ID (unique identifier for this specific span)
|
|
76
|
+
* @readonly
|
|
77
|
+
*/
|
|
78
|
+
readonly spanId: string;
|
|
79
|
+
/**
|
|
80
|
+
* Resource context (service name, version, instance ID, environment, region)
|
|
81
|
+
* Automatically populated from plugin configuration
|
|
82
|
+
* @readonly
|
|
83
|
+
*/
|
|
84
|
+
readonly resource: ResourceContext;
|
|
85
|
+
/**
|
|
86
|
+
* Custom attributes attached to this observable
|
|
87
|
+
* Attributes are immutable - use setAttribute() to create new observable with additional attributes
|
|
88
|
+
* @readonly
|
|
89
|
+
*/
|
|
90
|
+
readonly attributes: Record<string, string | number | boolean>;
|
|
91
|
+
/**
|
|
92
|
+
* Logging methods with automatic trace context
|
|
93
|
+
*
|
|
94
|
+
* All log methods automatically include trace context (trace ID, span ID) in the output.
|
|
95
|
+
* Logs support placeholder syntax for structured logging.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```typescript
|
|
99
|
+
* obs.log.debug("Processing {count} items", { count: 10 });
|
|
100
|
+
* obs.log.info("Request started");
|
|
101
|
+
* obs.log.warn("Rate limit approaching");
|
|
102
|
+
* obs.log.error("Failed to connect to database");
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
log: {
|
|
106
|
+
/**
|
|
107
|
+
* Log a debug message
|
|
108
|
+
* @param message - The message to log with optional {placeholder} syntax
|
|
109
|
+
* @param meta - Metadata to replace placeholders and add to log
|
|
110
|
+
*/
|
|
111
|
+
debug<T extends string>(message: T, ...meta: SmartLogMeta<T>): void;
|
|
112
|
+
/**
|
|
113
|
+
* Log an info message
|
|
114
|
+
* @param message - The message to log with optional {placeholder} syntax
|
|
115
|
+
* @param meta - Metadata to replace placeholders and add to log
|
|
116
|
+
*/
|
|
117
|
+
info<T extends string>(message: T, ...meta: SmartLogMeta<T>): void;
|
|
118
|
+
/**
|
|
119
|
+
* Log a warning message
|
|
120
|
+
* @param message - The message to log with optional {placeholder} syntax
|
|
121
|
+
* @param meta - Metadata to replace placeholders and add to log
|
|
122
|
+
*/
|
|
123
|
+
warn<T extends string>(message: T, ...meta: SmartLogMeta<T>): void;
|
|
124
|
+
/**
|
|
125
|
+
* Log an error message or BSBError
|
|
126
|
+
* @param messageOrError - Either a message string or BSBError instance
|
|
127
|
+
* @param meta - Metadata (only used if first arg is a message)
|
|
128
|
+
*/
|
|
129
|
+
error<T extends string>(messageOrError: T | Error, ...meta: SmartLogMeta<T>): void;
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Metrics methods for creating counters, gauges, histograms, and timers
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```typescript
|
|
136
|
+
* // Counter for tracking requests
|
|
137
|
+
* const requests = obs.metrics.counter(
|
|
138
|
+
* "requests_total",
|
|
139
|
+
* "Total requests",
|
|
140
|
+
* "Count of all incoming requests",
|
|
141
|
+
* ["method", "status"]
|
|
142
|
+
* );
|
|
143
|
+
* requests.increment(1, { method: "GET", status: "200" });
|
|
144
|
+
*
|
|
145
|
+
* // Timer for measuring duration
|
|
146
|
+
* const timer = obs.metrics.timer();
|
|
147
|
+
* await processRequest();
|
|
148
|
+
* const duration = timer.stop();
|
|
149
|
+
* obs.log.info("Request took {ms}ms", { ms: duration });
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
metrics: {
|
|
153
|
+
/**
|
|
154
|
+
* Create a counter metric
|
|
155
|
+
* @param name - Metric name
|
|
156
|
+
* @param description - Short description
|
|
157
|
+
* @param help - Detailed help text
|
|
158
|
+
* @param labels - Optional label names
|
|
159
|
+
*/
|
|
160
|
+
counter<LABELS extends string | undefined>(name: string, description: string, help: string, labels?: LABELS[]): Counter<LABELS>;
|
|
161
|
+
/**
|
|
162
|
+
* Create a gauge metric
|
|
163
|
+
* @param name - Metric name
|
|
164
|
+
* @param description - Short description
|
|
165
|
+
* @param help - Detailed help text
|
|
166
|
+
* @param labels - Optional label names
|
|
167
|
+
*/
|
|
168
|
+
gauge<LABELS extends string | undefined>(name: string, description: string, help: string, labels?: LABELS[]): Gauge<LABELS>;
|
|
169
|
+
/**
|
|
170
|
+
* Create a histogram metric
|
|
171
|
+
* @param name - Metric name
|
|
172
|
+
* @param description - Short description
|
|
173
|
+
* @param help - Detailed help text
|
|
174
|
+
* @param boundaries - Optional histogram boundaries
|
|
175
|
+
* @param labels - Optional label names
|
|
176
|
+
*/
|
|
177
|
+
histogram<LABELS extends string | undefined>(name: string, description: string, help: string, boundaries?: number[], labels?: LABELS[]): Histogram<LABELS>;
|
|
178
|
+
/**
|
|
179
|
+
* Create a timer to measure elapsed time
|
|
180
|
+
*/
|
|
181
|
+
timer(): Timer;
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* Create a child span with inherited attributes
|
|
185
|
+
*
|
|
186
|
+
* Child spans inherit all attributes from the parent and form a parent-child
|
|
187
|
+
* relationship in the distributed trace. Always call end() when done.
|
|
188
|
+
*
|
|
189
|
+
* @param name - Name of the span
|
|
190
|
+
* @param attributes - Additional attributes for this span
|
|
191
|
+
* @returns New Observable representing the child span
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```typescript
|
|
195
|
+
* public async fetchUser(obs: Observable, userId: string) {
|
|
196
|
+
* const span = obs.startSpan("fetch-user", { "user.id": userId });
|
|
197
|
+
* try {
|
|
198
|
+
* const user = await this.db.query("SELECT * FROM users WHERE id = ?", [userId]);
|
|
199
|
+
* span.end({ "user.found": true });
|
|
200
|
+
* return user;
|
|
201
|
+
* } catch (error) {
|
|
202
|
+
* span.error(error as Error);
|
|
203
|
+
* span.end({ "user.found": false });
|
|
204
|
+
* throw error;
|
|
205
|
+
* }
|
|
206
|
+
* }
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
startSpan(name: string, attributes?: Record<string, string | number | boolean>): Observable<TAttributeSchema>;
|
|
210
|
+
/**
|
|
211
|
+
* Create a new Observable with an additional attribute
|
|
212
|
+
*
|
|
213
|
+
* Observables are immutable - this returns a new instance.
|
|
214
|
+
* Attributes are propagated to all child operations (logs, spans, metrics).
|
|
215
|
+
*
|
|
216
|
+
* @param key - Attribute key
|
|
217
|
+
* @param value - Attribute value
|
|
218
|
+
* @returns New Observable with the added attribute
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```typescript
|
|
222
|
+
* public async handleRequest(obs: Observable, req: Request) {
|
|
223
|
+
* // Add request ID to all subsequent operations
|
|
224
|
+
* const withRequestId = obs.setAttribute("request.id", req.id);
|
|
225
|
+
* withRequestId.log.info("Handling request");
|
|
226
|
+
*
|
|
227
|
+
* // Chain multiple attributes
|
|
228
|
+
* const withUser = withRequestId.setAttribute("user.id", req.userId);
|
|
229
|
+
* const span = withUser.startSpan("process-request");
|
|
230
|
+
* }
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
233
|
+
setAttribute<K extends string, V extends string | number | boolean>(key: K, value: V): Observable<TAttributeSchema>;
|
|
234
|
+
/**
|
|
235
|
+
* Create a new Observable with multiple attributes
|
|
236
|
+
*
|
|
237
|
+
* Observables are immutable - this returns a new instance.
|
|
238
|
+
* More efficient than calling setAttribute multiple times.
|
|
239
|
+
*
|
|
240
|
+
* @param attrs - Attributes to add
|
|
241
|
+
* @returns New Observable with the added attributes
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```typescript
|
|
245
|
+
* public async handleRequest(obs: Observable, req: Request) {
|
|
246
|
+
* const withContext = obs.setAttributes({
|
|
247
|
+
* "request.id": req.id,
|
|
248
|
+
* "user.id": req.userId,
|
|
249
|
+
* "request.method": req.method,
|
|
250
|
+
* "request.path": req.path
|
|
251
|
+
* });
|
|
252
|
+
* withContext.log.info("Processing request");
|
|
253
|
+
* }
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
256
|
+
setAttributes(attrs: Record<string, string | number | boolean>): Observable<TAttributeSchema>;
|
|
257
|
+
/**
|
|
258
|
+
* Record an error to both logs and traces
|
|
259
|
+
*
|
|
260
|
+
* Automatically records the error to both the logging system and the active span.
|
|
261
|
+
* This ensures errors are captured in both systems for complete observability.
|
|
262
|
+
*
|
|
263
|
+
* @param error - Error or BSBError to record
|
|
264
|
+
* @param attributes - Additional attributes to attach
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```typescript
|
|
268
|
+
* const span = obs.startSpan("risky-operation");
|
|
269
|
+
* try {
|
|
270
|
+
* await this.doSomethingRisky();
|
|
271
|
+
* span.end({ "status": "success" });
|
|
272
|
+
* } catch (error) {
|
|
273
|
+
* span.error(error as Error, { "status": "failed", "retry": true });
|
|
274
|
+
* span.end();
|
|
275
|
+
* throw error;
|
|
276
|
+
* }
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
279
|
+
error(error: Error, attributes?: Record<string, string | number | boolean>): void;
|
|
280
|
+
/**
|
|
281
|
+
* End the span (only applies if this Observable was created via span())
|
|
282
|
+
*
|
|
283
|
+
* Completes the span and records the final state. If this Observable was not
|
|
284
|
+
* created via span(), this method does nothing.
|
|
285
|
+
*
|
|
286
|
+
* @param attributes - Final attributes to attach before ending
|
|
287
|
+
*
|
|
288
|
+
* @example
|
|
289
|
+
* ```typescript
|
|
290
|
+
* const span = obs.startSpan("process-batch");
|
|
291
|
+
* const items = await this.fetchItems();
|
|
292
|
+
* await this.processItems(items);
|
|
293
|
+
* span.end({ "items.processed": items.length, "status": "complete" });
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
end(attributes?: Record<string, string | number | boolean>): void;
|
|
297
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* BSB (Better-Service-Base) is an event-bus based microservice framework.
|
|
4
|
+
* Copyright (C) 2016 - 2025 BetterCorp (PTY) Ltd
|
|
5
|
+
*
|
|
6
|
+
* This program is free software: you can redistribute it and/or modify
|
|
7
|
+
* it under the terms of the GNU Affero General Public License as published
|
|
8
|
+
* by the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
* (at your option) any later version.
|
|
10
|
+
*
|
|
11
|
+
* Alternatively, you may obtain a commercial license for this program.
|
|
12
|
+
* The commercial license allows you to use the Program in a closed-source manner,
|
|
13
|
+
* including the right to create derivative works that are not subject to the terms
|
|
14
|
+
* of the AGPL.
|
|
15
|
+
*
|
|
16
|
+
* To obtain a commercial license, please contact the copyright holders at
|
|
17
|
+
* https://www.bettercorp.dev. The terms and conditions of the commercial license
|
|
18
|
+
* will be provided upon request.
|
|
19
|
+
*
|
|
20
|
+
* This program is distributed in the hope that it will be useful,
|
|
21
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
22
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
23
|
+
* GNU Affero General Public License for more details.
|
|
24
|
+
*
|
|
25
|
+
* You should have received a copy of the GNU Affero General Public License
|
|
26
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
27
|
+
*/
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
//# sourceMappingURL=observable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"observable.js","sourceRoot":"","sources":["../../src/interfaces/observable.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG"}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BSB (Better-Service-Base) is an event-bus based microservice framework.
|
|
3
|
+
* Copyright (C) 2016 - 2025 BetterCorp (PTY) Ltd
|
|
4
|
+
*
|
|
5
|
+
* This program is free software: you can redistribute it and/or modify
|
|
6
|
+
* it under the terms of the GNU Affero General Public License as published
|
|
7
|
+
* by the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
* (at your option) any later version.
|
|
9
|
+
*
|
|
10
|
+
* Alternatively, you may obtain a commercial license for this program.
|
|
11
|
+
* The commercial license allows you to use the Program in a closed-source manner,
|
|
12
|
+
* including the right to create derivative works that are not subject to the terms
|
|
13
|
+
* of the AGPL.
|
|
14
|
+
*
|
|
15
|
+
* To obtain a commercial license, please contact the copyright holders at
|
|
16
|
+
* https://www.bettercorp.dev. The terms and conditions of the commercial license
|
|
17
|
+
* will be provided upon request.
|
|
18
|
+
*
|
|
19
|
+
* This program is distributed in the hope that it will be useful,
|
|
20
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
21
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
22
|
+
* GNU Affero General Public License for more details.
|
|
23
|
+
*
|
|
24
|
+
* You should have received a copy of the GNU Affero General Public License
|
|
25
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
26
|
+
*/
|
|
27
|
+
import type { DEBUG_MODE } from "./logging";
|
|
28
|
+
import type { SBConfig } from "../serviceBase/config";
|
|
29
|
+
import type { SBPlugins } from "../serviceBase/plugins";
|
|
30
|
+
import type { SBObservable } from "../serviceBase/observable";
|
|
31
|
+
import type { SBEvents } from "../serviceBase/events";
|
|
32
|
+
import type { SBServices } from "../serviceBase/services";
|
|
33
|
+
/**
|
|
34
|
+
* Configuration options for ServiceBase constructor
|
|
35
|
+
*
|
|
36
|
+
* @group Main
|
|
37
|
+
* @category Configuration
|
|
38
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/interfaces/BSBOptions.html | API: BSBOptions}
|
|
39
|
+
*/
|
|
40
|
+
export interface BSBOptions {
|
|
41
|
+
/**
|
|
42
|
+
* Enable debug logging
|
|
43
|
+
* @default true
|
|
44
|
+
*/
|
|
45
|
+
debug?: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Disable development mode (affects plugin loading)
|
|
48
|
+
* @default false
|
|
49
|
+
*/
|
|
50
|
+
live?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Current working directory
|
|
53
|
+
* @default process.cwd()
|
|
54
|
+
*/
|
|
55
|
+
cwd?: string;
|
|
56
|
+
/**
|
|
57
|
+
* Custom application ID (overrides auto-generated ID)
|
|
58
|
+
* @default undefined (auto-generated from hostname + UUID)
|
|
59
|
+
*/
|
|
60
|
+
appId?: string;
|
|
61
|
+
/**
|
|
62
|
+
* Deployment region for resource context
|
|
63
|
+
* @default undefined (resolved from BSB_REGION env var)
|
|
64
|
+
*/
|
|
65
|
+
region?: string;
|
|
66
|
+
/**
|
|
67
|
+
* Override default configuration handler
|
|
68
|
+
* @default SBConfig
|
|
69
|
+
*/
|
|
70
|
+
config?: typeof SBConfig;
|
|
71
|
+
/**
|
|
72
|
+
* Override default plugins handler
|
|
73
|
+
* @default SBPlugins
|
|
74
|
+
*/
|
|
75
|
+
plugins?: typeof SBPlugins;
|
|
76
|
+
/**
|
|
77
|
+
* Override default observable handler (unified logging, metrics, tracing)
|
|
78
|
+
* @default SBObservable
|
|
79
|
+
*/
|
|
80
|
+
observable?: typeof SBObservable;
|
|
81
|
+
/**
|
|
82
|
+
* Override default events handler
|
|
83
|
+
* @default SBEvents
|
|
84
|
+
*/
|
|
85
|
+
events?: typeof SBEvents;
|
|
86
|
+
/**
|
|
87
|
+
* Override default services handler
|
|
88
|
+
* @default SBServices
|
|
89
|
+
*/
|
|
90
|
+
services?: typeof SBServices;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Resolved options with all defaults applied
|
|
94
|
+
*
|
|
95
|
+
* @group Main
|
|
96
|
+
* @category Configuration
|
|
97
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/interfaces/ResolvedBSBOptions.html | API: ResolvedBSBOptions}
|
|
98
|
+
*/
|
|
99
|
+
export interface ResolvedBSBOptions {
|
|
100
|
+
debug: boolean;
|
|
101
|
+
live: boolean;
|
|
102
|
+
cwd: string;
|
|
103
|
+
appId: string;
|
|
104
|
+
region?: string;
|
|
105
|
+
mode: DEBUG_MODE;
|
|
106
|
+
config: typeof SBConfig;
|
|
107
|
+
plugins: typeof SBPlugins;
|
|
108
|
+
observable: typeof SBObservable;
|
|
109
|
+
events: typeof SBEvents;
|
|
110
|
+
services: typeof SBServices;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Simple configuration for common use cases
|
|
114
|
+
*
|
|
115
|
+
* @group Main
|
|
116
|
+
* @category Configuration
|
|
117
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/interfaces/SimpleBSBOptions.html | API: SimpleBSBOptions}
|
|
118
|
+
*/
|
|
119
|
+
export interface SimpleBSBOptions {
|
|
120
|
+
/**
|
|
121
|
+
* Working directory
|
|
122
|
+
* @default process.cwd()
|
|
123
|
+
*/
|
|
124
|
+
cwd?: string;
|
|
125
|
+
/**
|
|
126
|
+
* Configuration file path or object
|
|
127
|
+
*/
|
|
128
|
+
config?: string | object;
|
|
129
|
+
/**
|
|
130
|
+
* List of plugins to enable
|
|
131
|
+
* @default ['observable-default', 'events-default']
|
|
132
|
+
*/
|
|
133
|
+
plugins?: string[];
|
|
134
|
+
/**
|
|
135
|
+
* Debug mode
|
|
136
|
+
* @default true in development, false in production
|
|
137
|
+
*/
|
|
138
|
+
debug?: boolean;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Factory preset configurations
|
|
142
|
+
*
|
|
143
|
+
* @group Main
|
|
144
|
+
* @category Configuration
|
|
145
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/interfaces/BSBPreset.html | API: BSBPreset}
|
|
146
|
+
*/
|
|
147
|
+
export declare enum BSBPreset {
|
|
148
|
+
/**
|
|
149
|
+
* Minimal setup with basic plugins
|
|
150
|
+
*/
|
|
151
|
+
MINIMAL = "minimal",
|
|
152
|
+
/**
|
|
153
|
+
* Development setup with debug logging
|
|
154
|
+
*/
|
|
155
|
+
DEVELOPMENT = "development",
|
|
156
|
+
/**
|
|
157
|
+
* Production setup with optimized settings
|
|
158
|
+
*/
|
|
159
|
+
PRODUCTION = "production",
|
|
160
|
+
/**
|
|
161
|
+
* Testing setup with mocked components
|
|
162
|
+
*/
|
|
163
|
+
TESTING = "testing"
|
|
164
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* BSB (Better-Service-Base) is an event-bus based microservice framework.
|
|
4
|
+
* Copyright (C) 2016 - 2025 BetterCorp (PTY) Ltd
|
|
5
|
+
*
|
|
6
|
+
* This program is free software: you can redistribute it and/or modify
|
|
7
|
+
* it under the terms of the GNU Affero General Public License as published
|
|
8
|
+
* by the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
* (at your option) any later version.
|
|
10
|
+
*
|
|
11
|
+
* Alternatively, you may obtain a commercial license for this program.
|
|
12
|
+
* The commercial license allows you to use the Program in a closed-source manner,
|
|
13
|
+
* including the right to create derivative works that are not subject to the terms
|
|
14
|
+
* of the AGPL.
|
|
15
|
+
*
|
|
16
|
+
* To obtain a commercial license, please contact the copyright holders at
|
|
17
|
+
* https://www.bettercorp.dev. The terms and conditions of the commercial license
|
|
18
|
+
* will be provided upon request.
|
|
19
|
+
*
|
|
20
|
+
* This program is distributed in the hope that it will be useful,
|
|
21
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
22
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
23
|
+
* GNU Affero General Public License for more details.
|
|
24
|
+
*
|
|
25
|
+
* You should have received a copy of the GNU Affero General Public License
|
|
26
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
27
|
+
*/
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.BSBPreset = void 0;
|
|
30
|
+
/**
|
|
31
|
+
* Factory preset configurations
|
|
32
|
+
*
|
|
33
|
+
* @group Main
|
|
34
|
+
* @category Configuration
|
|
35
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/interfaces/BSBPreset.html | API: BSBPreset}
|
|
36
|
+
*/
|
|
37
|
+
var BSBPreset;
|
|
38
|
+
(function (BSBPreset) {
|
|
39
|
+
/**
|
|
40
|
+
* Minimal setup with basic plugins
|
|
41
|
+
*/
|
|
42
|
+
BSBPreset["MINIMAL"] = "minimal";
|
|
43
|
+
/**
|
|
44
|
+
* Development setup with debug logging
|
|
45
|
+
*/
|
|
46
|
+
BSBPreset["DEVELOPMENT"] = "development";
|
|
47
|
+
/**
|
|
48
|
+
* Production setup with optimized settings
|
|
49
|
+
*/
|
|
50
|
+
BSBPreset["PRODUCTION"] = "production";
|
|
51
|
+
/**
|
|
52
|
+
* Testing setup with mocked components
|
|
53
|
+
*/
|
|
54
|
+
BSBPreset["TESTING"] = "testing";
|
|
55
|
+
})(BSBPreset || (exports.BSBPreset = BSBPreset = {}));
|
|
56
|
+
//# sourceMappingURL=options.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.js","sourceRoot":"","sources":["../../src/interfaces/options.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;;;AAmIH;;;;;;GAMG;AACH,IAAY,SAoBX;AApBD,WAAY,SAAS;IACnB;;OAEG;IACH,gCAAmB,CAAA;IAEnB;;OAEG;IACH,wCAA2B,CAAA;IAE3B;;OAEG;IACH,sCAAyB,CAAA;IAEzB;;OAEG;IACH,gCAAmB,CAAA;AACrB,CAAC,EApBW,SAAS,yBAAT,SAAS,QAoBpB"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BSB (Better-Service-Base) is an event-bus based microservice framework.
|
|
3
|
+
* Copyright (C) 2016 - 2025 BetterCorp (PTY) Ltd
|
|
4
|
+
*
|
|
5
|
+
* This program is free software: you can redistribute it and/or modify
|
|
6
|
+
* it under the terms of the GNU Affero General Public License as published
|
|
7
|
+
* by the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
* (at your option) any later version.
|
|
9
|
+
*
|
|
10
|
+
* Alternatively, you may obtain a commercial license for this program.
|
|
11
|
+
* The commercial license allows you to use the Program in a closed-source manner,
|
|
12
|
+
* including the right to create derivative works that are not subject to the terms
|
|
13
|
+
* of the AGPL.
|
|
14
|
+
*
|
|
15
|
+
* To obtain a commercial license, please contact the copyright holders at
|
|
16
|
+
* https://www.bettercorp.dev. The terms and conditions of the commercial license
|
|
17
|
+
* will be provided upon request.
|
|
18
|
+
*
|
|
19
|
+
* This program is distributed in the hope that it will be useful,
|
|
20
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
21
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
22
|
+
* GNU Affero General Public License for more details.
|
|
23
|
+
*
|
|
24
|
+
* You should have received a copy of the GNU Affero General Public License
|
|
25
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
26
|
+
*/
|
|
27
|
+
import { BSBConfig, BSBConfigRef, BSBEvents, BSBEventsRef, BSBObservable, BSBObservableRef, BSBPluginConfig, BSBService, BSBServiceRef } from "../base";
|
|
28
|
+
import { EventsEventTypes } from "./events";
|
|
29
|
+
/**
|
|
30
|
+
* @hidden
|
|
31
|
+
*/
|
|
32
|
+
/**
|
|
33
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/modules.html#module-interfaces_plugins | API: interfaces/plugins}
|
|
34
|
+
*/
|
|
35
|
+
export declare const PluginTypes: {
|
|
36
|
+
readonly config: "config";
|
|
37
|
+
readonly events: "events";
|
|
38
|
+
readonly observable: "observable";
|
|
39
|
+
readonly service: "service";
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* @hidden
|
|
43
|
+
*/
|
|
44
|
+
export type PluginType = (typeof PluginTypes)[keyof typeof PluginTypes];
|
|
45
|
+
/**
|
|
46
|
+
* Marks all properties of an object read only and all nested objects read only
|
|
47
|
+
* @hidden
|
|
48
|
+
*/
|
|
49
|
+
export type DeepReadonly<T> = {
|
|
50
|
+
readonly [P in keyof T]: T[P] extends (infer R)[] ? DeepReadonlyArray<R> : T[P] extends Function ? T[P] : T[P] extends object ? DeepReadonly<T[P]> : T[P];
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Marks all properties of an array read only and all nested objects read only
|
|
54
|
+
* @hidden
|
|
55
|
+
*/
|
|
56
|
+
export interface DeepReadonlyArray<T> extends ReadonlyArray<DeepReadonly<T>> {
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* @hidden
|
|
60
|
+
*/
|
|
61
|
+
export interface IPluginDefinition {
|
|
62
|
+
package?: string | null;
|
|
63
|
+
plugin: string;
|
|
64
|
+
name: string;
|
|
65
|
+
version: string;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* @hidden
|
|
69
|
+
*/
|
|
70
|
+
export interface IPluginBuilder {
|
|
71
|
+
name: string;
|
|
72
|
+
pluginName: string;
|
|
73
|
+
version: string;
|
|
74
|
+
pluginFile: string;
|
|
75
|
+
pluginDir: string;
|
|
76
|
+
installerFile: string | null;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* @hidden
|
|
80
|
+
*/
|
|
81
|
+
export type PluginTypeDefinition<T extends PluginType> = T extends typeof PluginTypes.service ? BSBService : T extends typeof PluginTypes.observable ? BSBObservable<any> : T extends typeof PluginTypes.config ? BSBConfig : T extends typeof PluginTypes.events ? BSBEvents : never;
|
|
82
|
+
/**
|
|
83
|
+
* @hidden
|
|
84
|
+
*/
|
|
85
|
+
export type PluginTypeDefinitionRef<T extends PluginType> = T extends typeof PluginTypes.service ? typeof BSBServiceRef : T extends typeof PluginTypes.observable ? typeof BSBObservableRef : T extends typeof PluginTypes.config ? typeof BSBConfigRef : T extends typeof PluginTypes.events ? typeof BSBEventsRef : never;
|
|
86
|
+
/**
|
|
87
|
+
* @hidden
|
|
88
|
+
*/
|
|
89
|
+
export interface IPluginBuilt<T extends PluginType> extends IPluginBuilder {
|
|
90
|
+
config: any;
|
|
91
|
+
plugin: PluginTypeDefinition<T>;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* @hidden
|
|
95
|
+
*/
|
|
96
|
+
export interface PluginDefinition {
|
|
97
|
+
package?: string | null;
|
|
98
|
+
version: string | null;
|
|
99
|
+
plugin: string;
|
|
100
|
+
enabled: boolean;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* @hidden
|
|
104
|
+
*/
|
|
105
|
+
export type FilterDetailed<T extends string | number | symbol = any> = Record<T, {
|
|
106
|
+
plugins: Array<string>;
|
|
107
|
+
enabled: boolean;
|
|
108
|
+
}>;
|
|
109
|
+
/**
|
|
110
|
+
* @hidden
|
|
111
|
+
*/
|
|
112
|
+
export type EventsFilterDetailed = FilterDetailed<EventsEventTypes>;
|
|
113
|
+
/**
|
|
114
|
+
* @hidden
|
|
115
|
+
*/
|
|
116
|
+
export type EventsFilter = EventsFilterDetailed | Record<EventsEventTypes, boolean> | Record<EventsEventTypes, Array<string>> | Array<EventsEventTypes>;
|
|
117
|
+
/**
|
|
118
|
+
* @hidden
|
|
119
|
+
*/
|
|
120
|
+
export interface EventsConfig extends PluginDefinition {
|
|
121
|
+
filter?: EventsFilter;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* @hidden
|
|
125
|
+
*/
|
|
126
|
+
export type FilterOnType = // see EventsFilter and LoggingFilter for more details
|
|
127
|
+
"all" | "events" | "eventsState" | "eventsPlugins" | "eventsDetailed";
|
|
128
|
+
/**
|
|
129
|
+
* @hidden
|
|
130
|
+
*/
|
|
131
|
+
export interface LoadedPlugin<NamedType extends PluginType, ClassType extends PluginTypeDefinitionRef<NamedType> = PluginTypeDefinitionRef<NamedType>> {
|
|
132
|
+
name: string;
|
|
133
|
+
ref: string;
|
|
134
|
+
version: string;
|
|
135
|
+
serviceConfig: BSBPluginConfig<any> | null;
|
|
136
|
+
plugin: ClassType;
|
|
137
|
+
packageCwd: string;
|
|
138
|
+
pluginCwd: string;
|
|
139
|
+
pluginPath: string;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/modules.html#module-interfaces_plugins | API: interfaces/plugins}
|
|
143
|
+
*/
|