@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.
Files changed (182) hide show
  1. package/LICENSE +665 -0
  2. package/LICENSE.commercial +32 -0
  3. package/README.md +263 -0
  4. package/bsb-plugin.json +62 -0
  5. package/lib/base/BSBConfig.d.ts +130 -0
  6. package/lib/base/BSBConfig.js +95 -0
  7. package/lib/base/BSBConfig.js.map +1 -0
  8. package/lib/base/BSBEvents.d.ts +207 -0
  9. package/lib/base/BSBEvents.js +101 -0
  10. package/lib/base/BSBEvents.js.map +1 -0
  11. package/lib/base/BSBObservable.d.ts +178 -0
  12. package/lib/base/BSBObservable.js +91 -0
  13. package/lib/base/BSBObservable.js.map +1 -0
  14. package/lib/base/BSBService.d.ts +277 -0
  15. package/lib/base/BSBService.js +366 -0
  16. package/lib/base/BSBService.js.map +1 -0
  17. package/lib/base/BSBServiceClient.d.ts +135 -0
  18. package/lib/base/BSBServiceClient.js +130 -0
  19. package/lib/base/BSBServiceClient.js.map +1 -0
  20. package/lib/base/EventValidator.d.ts +137 -0
  21. package/lib/base/EventValidator.js +210 -0
  22. package/lib/base/EventValidator.js.map +1 -0
  23. package/lib/base/ObservableBackend.d.ts +281 -0
  24. package/lib/base/ObservableBackend.js +515 -0
  25. package/lib/base/ObservableBackend.js.map +1 -0
  26. package/lib/base/PluginConfig.d.ts +196 -0
  27. package/lib/base/PluginConfig.js +96 -0
  28. package/lib/base/PluginConfig.js.map +1 -0
  29. package/lib/base/PluginEvents.d.ts +140 -0
  30. package/lib/base/PluginEvents.js +268 -0
  31. package/lib/base/PluginEvents.js.map +1 -0
  32. package/lib/base/PluginObservable.d.ts +196 -0
  33. package/lib/base/PluginObservable.js +250 -0
  34. package/lib/base/PluginObservable.js.map +1 -0
  35. package/lib/base/ResourceContext.d.ts +70 -0
  36. package/lib/base/ResourceContext.js +54 -0
  37. package/lib/base/ResourceContext.js.map +1 -0
  38. package/lib/base/base.d.ts +264 -0
  39. package/lib/base/base.js +182 -0
  40. package/lib/base/base.js.map +1 -0
  41. package/lib/base/errorMessages.d.ts +56 -0
  42. package/lib/base/errorMessages.js +70 -0
  43. package/lib/base/errorMessages.js.map +1 -0
  44. package/lib/base/factory.d.ts +58 -0
  45. package/lib/base/factory.js +167 -0
  46. package/lib/base/factory.js.map +1 -0
  47. package/lib/base/functions.d.ts +117 -0
  48. package/lib/base/functions.js +152 -0
  49. package/lib/base/functions.js.map +1 -0
  50. package/lib/base/index.d.ts +44 -0
  51. package/lib/base/index.js +64 -0
  52. package/lib/base/index.js.map +1 -0
  53. package/lib/base/logFormatter.d.ts +50 -0
  54. package/lib/base/logFormatter.js +105 -0
  55. package/lib/base/logFormatter.js.map +1 -0
  56. package/lib/base/tools.d.ts +316 -0
  57. package/lib/base/tools.js +666 -0
  58. package/lib/base/tools.js.map +1 -0
  59. package/lib/cli.d.ts +28 -0
  60. package/lib/cli.js +254 -0
  61. package/lib/cli.js.map +1 -0
  62. package/lib/dev.d.ts +27 -0
  63. package/lib/dev.js +200 -0
  64. package/lib/dev.js.map +1 -0
  65. package/lib/index.d.ts +32 -0
  66. package/lib/index.js +49 -0
  67. package/lib/index.js.map +1 -0
  68. package/lib/interfaces/events.d.ts +67 -0
  69. package/lib/interfaces/events.js +44 -0
  70. package/lib/interfaces/events.js.map +1 -0
  71. package/lib/interfaces/index.d.ts +38 -0
  72. package/lib/interfaces/index.js +59 -0
  73. package/lib/interfaces/index.js.map +1 -0
  74. package/lib/interfaces/logging.d.ts +106 -0
  75. package/lib/interfaces/logging.js +39 -0
  76. package/lib/interfaces/logging.js.map +1 -0
  77. package/lib/interfaces/metrics.d.ts +365 -0
  78. package/lib/interfaces/metrics.js +46 -0
  79. package/lib/interfaces/metrics.js.map +1 -0
  80. package/lib/interfaces/observable-types.d.ts +63 -0
  81. package/lib/interfaces/observable-types.js +49 -0
  82. package/lib/interfaces/observable-types.js.map +1 -0
  83. package/lib/interfaces/observable.d.ts +297 -0
  84. package/lib/interfaces/observable.js +29 -0
  85. package/lib/interfaces/observable.js.map +1 -0
  86. package/lib/interfaces/options.d.ts +164 -0
  87. package/lib/interfaces/options.js +56 -0
  88. package/lib/interfaces/options.js.map +1 -0
  89. package/lib/interfaces/plugins.d.ts +143 -0
  90. package/lib/interfaces/plugins.js +45 -0
  91. package/lib/interfaces/plugins.js.map +1 -0
  92. package/lib/interfaces/result.d.ts +129 -0
  93. package/lib/interfaces/result.js +162 -0
  94. package/lib/interfaces/result.js.map +1 -0
  95. package/lib/interfaces/schema-events.d.ts +378 -0
  96. package/lib/interfaces/schema-events.js +247 -0
  97. package/lib/interfaces/schema-events.js.map +1 -0
  98. package/lib/interfaces/schema-types.d.ts +407 -0
  99. package/lib/interfaces/schema-types.js +581 -0
  100. package/lib/interfaces/schema-types.js.map +1 -0
  101. package/lib/interfaces/service.d.ts +48 -0
  102. package/lib/interfaces/service.js +29 -0
  103. package/lib/interfaces/service.js.map +1 -0
  104. package/lib/interfaces/tools.d.ts +65 -0
  105. package/lib/interfaces/tools.js +50 -0
  106. package/lib/interfaces/tools.js.map +1 -0
  107. package/lib/plugins/config-default/index.d.ts +59 -0
  108. package/lib/plugins/config-default/index.js +197 -0
  109. package/lib/plugins/config-default/index.js.map +1 -0
  110. package/lib/plugins/config-default/interfaces.d.ts +92 -0
  111. package/lib/plugins/config-default/interfaces.js +36 -0
  112. package/lib/plugins/config-default/interfaces.js.map +1 -0
  113. package/lib/plugins/events-default/events/broadcast.d.ts +36 -0
  114. package/lib/plugins/events-default/events/broadcast.js +85 -0
  115. package/lib/plugins/events-default/events/broadcast.js.map +1 -0
  116. package/lib/plugins/events-default/events/emit.d.ts +38 -0
  117. package/lib/plugins/events-default/events/emit.js +104 -0
  118. package/lib/plugins/events-default/events/emit.js.map +1 -0
  119. package/lib/plugins/events-default/events/emitAndReturn.d.ts +36 -0
  120. package/lib/plugins/events-default/events/emitAndReturn.js +100 -0
  121. package/lib/plugins/events-default/events/emitAndReturn.js.map +1 -0
  122. package/lib/plugins/events-default/events/emitStreamAndReceiveStream.d.ts +38 -0
  123. package/lib/plugins/events-default/events/emitStreamAndReceiveStream.js +134 -0
  124. package/lib/plugins/events-default/events/emitStreamAndReceiveStream.js.map +1 -0
  125. package/lib/plugins/events-default/events/index.d.ts +30 -0
  126. package/lib/plugins/events-default/events/index.js +38 -0
  127. package/lib/plugins/events-default/events/index.js.map +1 -0
  128. package/lib/plugins/events-default/index.d.ts +57 -0
  129. package/lib/plugins/events-default/index.js +86 -0
  130. package/lib/plugins/events-default/index.js.map +1 -0
  131. package/lib/plugins/observable-default/index.d.ts +43 -0
  132. package/lib/plugins/observable-default/index.js +151 -0
  133. package/lib/plugins/observable-default/index.js.map +1 -0
  134. package/lib/schemas/config-default.json +34 -0
  135. package/lib/schemas/config-default.plugin.json +36 -0
  136. package/lib/schemas/events-default.json +18 -0
  137. package/lib/schemas/events-default.plugin.json +17 -0
  138. package/lib/schemas/observable-default.json +33 -0
  139. package/lib/schemas/observable-default.plugin.json +24 -0
  140. package/lib/scripts/bsb-client-cli.d.ts +21 -0
  141. package/lib/scripts/bsb-client-cli.js +701 -0
  142. package/lib/scripts/bsb-client-cli.js.map +1 -0
  143. package/lib/scripts/bsb-plugin-cli.d.ts +15 -0
  144. package/lib/scripts/bsb-plugin-cli.js +547 -0
  145. package/lib/scripts/bsb-plugin-cli.js.map +1 -0
  146. package/lib/scripts/export-schemas.d.ts +17 -0
  147. package/lib/scripts/export-schemas.js +205 -0
  148. package/lib/scripts/export-schemas.js.map +1 -0
  149. package/lib/scripts/extract-schemas-from-source.d.ts +23 -0
  150. package/lib/scripts/extract-schemas-from-source.js +604 -0
  151. package/lib/scripts/extract-schemas-from-source.js.map +1 -0
  152. package/lib/scripts/generate-client-types.d.ts +22 -0
  153. package/lib/scripts/generate-client-types.js +537 -0
  154. package/lib/scripts/generate-client-types.js.map +1 -0
  155. package/lib/scripts/generate-plugin-json.d.ts +17 -0
  156. package/lib/scripts/generate-plugin-json.js +219 -0
  157. package/lib/scripts/generate-plugin-json.js.map +1 -0
  158. package/lib/serviceBase/config.d.ts +83 -0
  159. package/lib/serviceBase/config.js +236 -0
  160. package/lib/serviceBase/config.js.map +1 -0
  161. package/lib/serviceBase/events.d.ts +91 -0
  162. package/lib/serviceBase/events.js +519 -0
  163. package/lib/serviceBase/events.js.map +1 -0
  164. package/lib/serviceBase/index.d.ts +33 -0
  165. package/lib/serviceBase/index.js +50 -0
  166. package/lib/serviceBase/index.js.map +1 -0
  167. package/lib/serviceBase/observable.d.ts +249 -0
  168. package/lib/serviceBase/observable.js +551 -0
  169. package/lib/serviceBase/observable.js.map +1 -0
  170. package/lib/serviceBase/plugins.d.ts +48 -0
  171. package/lib/serviceBase/plugins.js +184 -0
  172. package/lib/serviceBase/plugins.js.map +1 -0
  173. package/lib/serviceBase/serviceBase.d.ts +228 -0
  174. package/lib/serviceBase/serviceBase.js +420 -0
  175. package/lib/serviceBase/serviceBase.js.map +1 -0
  176. package/lib/serviceBase/services.d.ts +63 -0
  177. package/lib/serviceBase/services.js +346 -0
  178. package/lib/serviceBase/services.js.map +1 -0
  179. package/lib/tests.d.ts +27 -0
  180. package/lib/tests.js +44 -0
  181. package/lib/tests.js.map +1 -0
  182. package/package.json +91 -0
@@ -0,0 +1,249 @@
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 { EventEmitter } from "node:events";
28
+ import { BSBError } from "../base";
29
+ import { DEBUG_MODE, LogMeta } from "../interfaces";
30
+ import { DTrace } from "../interfaces/metrics";
31
+ import { SBConfig } from "./config";
32
+ import { SBPlugins } from "./plugins";
33
+ /**
34
+ * BSB Observable Controller - Unified logging, metrics, and tracing
35
+ *
36
+ * This class is responsible for managing all observability in the BSB framework.
37
+ * It coordinates observable plugins (like OpenTelemetry, Prometheus, etc.) and
38
+ * routes events from the Observable API to the configured backends.
39
+ *
40
+ * If you have a specific way of managing observability, you can extend this class
41
+ * and use your own class when creating the ServiceBase instance.
42
+ *
43
+ * @group Observable
44
+ * @category Core
45
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/SBObservable.html | API: SBObservable}
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * // Custom observable controller
50
+ * class MyObservable extends SBObservable {
51
+ * // Override methods as needed
52
+ * }
53
+ *
54
+ * // Use custom controller
55
+ * const serviceBase = new ServiceBase(appId, {
56
+ * observableClass: MyObservable
57
+ * });
58
+ * ```
59
+ */
60
+ export declare class SBObservable {
61
+ private observablePlugins;
62
+ observableBus: EventEmitter;
63
+ private mode;
64
+ private appId;
65
+ private cwd;
66
+ private sbPlugins;
67
+ private observableBackend;
68
+ private _ready;
69
+ /**
70
+ * Check if observable plugins are ready
71
+ * @returns true if all plugins have been initialized
72
+ */
73
+ get isReady(): boolean;
74
+ /**
75
+ * Create an SBObservable instance
76
+ * @param appId - Application ID
77
+ * @param mode - Debug mode (development, production, production-debug)
78
+ * @param cwd - Current working directory
79
+ * @param sbPlugins - Plugin manager instance
80
+ */
81
+ constructor(appId: string, mode: DEBUG_MODE, cwd: string, sbPlugins: SBPlugins);
82
+ private setupCounterEvents;
83
+ private setupGaugeEvents;
84
+ private setupHistogramEvents;
85
+ private setupSpanEvents;
86
+ private triggerEvent;
87
+ private triggerMetricEvent;
88
+ private triggerSpanEvent;
89
+ private shouldTriggerForPlugin;
90
+ private determineFilterType;
91
+ /**
92
+ * Setup and load observable plugins from configuration
93
+ *
94
+ * Loads configured observable plugins and initializes their filters.
95
+ * This is called during ServiceBase initialization.
96
+ *
97
+ * @param sbConfig - Configuration manager instance
98
+ */
99
+ setupObservablePlugins(sbConfig: SBConfig): Promise<void>;
100
+ /**
101
+ * Initialize all observable plugins
102
+ *
103
+ * Calls the init lifecycle method on all loaded observable plugins.
104
+ * This is called during ServiceBase initialization.
105
+ *
106
+ * @param trace - DTrace for tracking initialization
107
+ * @param sbConfig - Configuration manager instance
108
+ */
109
+ init(trace: DTrace, sbConfig: SBConfig): Promise<void>;
110
+ /**
111
+ * Run all observable plugins
112
+ *
113
+ * Calls the run lifecycle method on all loaded observable plugins.
114
+ * This is called during ServiceBase startup.
115
+ *
116
+ * @param trace - DTrace for tracking the run phase
117
+ */
118
+ run(trace: DTrace): Promise<void>;
119
+ /**
120
+ * Dispose all observable plugins
121
+ *
122
+ * Calls the dispose lifecycle method on all loaded observable plugins
123
+ * and clears the plugin list. This is called during ServiceBase shutdown.
124
+ */
125
+ dispose(): Promise<void>;
126
+ /**
127
+ * Emit a debug log event
128
+ * @param plugin - Plugin name
129
+ * @param trace - DTrace object
130
+ * @param message - Log message
131
+ * @param meta - Log metadata
132
+ */
133
+ debug(plugin: string, trace: DTrace, message: string, meta: LogMeta<any>): void;
134
+ /**
135
+ * Emit an info log event
136
+ * @param plugin - Plugin name
137
+ * @param trace - DTrace object
138
+ * @param message - Log message
139
+ * @param meta - Log metadata
140
+ */
141
+ info(plugin: string, trace: DTrace, message: string, meta: LogMeta<any>): void;
142
+ /**
143
+ * Emit a warn log event
144
+ * @param plugin - Plugin name
145
+ * @param trace - DTrace object
146
+ * @param message - Log message
147
+ * @param meta - Log metadata
148
+ */
149
+ warn(plugin: string, trace: DTrace, message: string, meta: LogMeta<any>): void;
150
+ /**
151
+ * Emit an error log event
152
+ * @param plugin - Plugin name
153
+ * @param trace - DTrace object
154
+ * @param message - Log message or BSBError
155
+ * @param meta - Log metadata (optional)
156
+ */
157
+ error(plugin: string, trace: DTrace, message: string | BSBError<any>, meta?: LogMeta<any>): void;
158
+ /**
159
+ * Emit counter creation event
160
+ * @param timestamp - Event timestamp
161
+ * @param pluginName - Plugin name
162
+ * @param name - Metric name
163
+ * @param description - Short description
164
+ * @param help - Detailed help text
165
+ * @param labels - Optional label names
166
+ */
167
+ createCounter(timestamp: number, pluginName: string, name: string, description: string, help: string, labels?: string[]): void;
168
+ /**
169
+ * Emit counter increment event
170
+ * @param timestamp - Event timestamp
171
+ * @param pluginName - Plugin name
172
+ * @param name - Metric name
173
+ * @param value - Increment value
174
+ * @param labels - Optional label values
175
+ */
176
+ incrementCounter(timestamp: number, pluginName: string, name: string, value: number, labels?: Record<string, string>): void;
177
+ /**
178
+ * Emit gauge creation event
179
+ * @param timestamp - Event timestamp
180
+ * @param pluginName - Plugin name
181
+ * @param name - Metric name
182
+ * @param description - Short description
183
+ * @param help - Detailed help text
184
+ * @param labels - Optional label names
185
+ */
186
+ createGauge(timestamp: number, pluginName: string, name: string, description: string, help: string, labels?: string[]): void;
187
+ /**
188
+ * Emit gauge set event
189
+ * @param timestamp - Event timestamp
190
+ * @param pluginName - Plugin name
191
+ * @param name - Metric name
192
+ * @param value - New gauge value
193
+ * @param labels - Optional label values
194
+ */
195
+ setGauge(timestamp: number, pluginName: string, name: string, value: number, labels?: Record<string, string>): void;
196
+ /**
197
+ * Emit histogram creation event
198
+ * @param timestamp - Event timestamp
199
+ * @param pluginName - Plugin name
200
+ * @param name - Metric name
201
+ * @param description - Short description
202
+ * @param help - Detailed help text
203
+ * @param boundaries - Optional histogram boundaries
204
+ * @param labels - Optional label names
205
+ */
206
+ createHistogram(timestamp: number, pluginName: string, name: string, description: string, help: string, boundaries?: number[], labels?: string[]): void;
207
+ /**
208
+ * Emit histogram observation event
209
+ * @param timestamp - Event timestamp
210
+ * @param pluginName - Plugin name
211
+ * @param name - Metric name
212
+ * @param value - Observed value
213
+ * @param labels - Optional label values
214
+ */
215
+ observeHistogram(timestamp: number, pluginName: string, name: string, value: number, labels?: Record<string, string>): void;
216
+ /**
217
+ * Emit span start event
218
+ * @param timestamp - Event timestamp
219
+ * @param appId - Application ID
220
+ * @param pluginName - Plugin name
221
+ * @param traceId - Trace ID
222
+ * @param parentSpanId - Parent span ID (null for root spans)
223
+ * @param spanId - This span's ID
224
+ * @param name - Span name
225
+ * @param attributes - Optional span attributes
226
+ */
227
+ startSpan(timestamp: number, appId: string, pluginName: string, traceId: string, parentSpanId: string | null, spanId: string, name: string, attributes?: Record<string, string | number | boolean>): void;
228
+ /**
229
+ * Emit span end event
230
+ * @param timestamp - Event timestamp
231
+ * @param appId - Application ID
232
+ * @param pluginName - Plugin name
233
+ * @param traceId - Trace ID
234
+ * @param spanId - Span ID
235
+ * @param attributes - Optional final attributes
236
+ */
237
+ endSpan(timestamp: number, appId: string, pluginName: string, traceId: string, spanId: string, attributes?: Record<string, string | number | boolean>): void;
238
+ /**
239
+ * Emit span error event
240
+ * @param timestamp - Event timestamp
241
+ * @param appId - Application ID
242
+ * @param pluginName - Plugin name
243
+ * @param traceId - Trace ID
244
+ * @param spanId - Span ID
245
+ * @param error - Error to record
246
+ * @param attributes - Optional error attributes
247
+ */
248
+ errorSpan(timestamp: number, appId: string, pluginName: string, traceId: string, spanId: string, error: Error, attributes?: Record<string, string | number | boolean>): void;
249
+ }