@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,250 @@
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.PluginObservable = void 0;
30
+ const errorMessages_1 = require("./errorMessages");
31
+ /**
32
+ * Implementation of Observable interface that wraps DTrace with observability features
33
+ *
34
+ * @group Observability
35
+ * @category Plugin Development Tools
36
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/PluginObservable.html | API: PluginObservable}
37
+ */
38
+ class PluginObservable {
39
+ _trace;
40
+ _resource;
41
+ _attributes;
42
+ _backend;
43
+ _span;
44
+ /**
45
+ * Create a PluginObservable
46
+ * @param trace - DTrace object
47
+ * @param resource - Resource context
48
+ * @param backend - IPluginObservable instance (unified logging and metrics backend)
49
+ * @param attributes - Initial attributes
50
+ * @param span - Optional Trace/Span object (if created via startSpan())
51
+ */
52
+ constructor(trace, resource, backend, attributes = {}, span) {
53
+ this._trace = trace;
54
+ this._resource = resource;
55
+ this._backend = backend;
56
+ this._attributes = attributes;
57
+ this._span = span;
58
+ }
59
+ get trace() {
60
+ return this._trace;
61
+ }
62
+ get traceId() {
63
+ return this._trace.t;
64
+ }
65
+ get spanId() {
66
+ return this._trace.s;
67
+ }
68
+ get resource() {
69
+ return this._resource;
70
+ }
71
+ get attributes() {
72
+ return { ...this._attributes };
73
+ }
74
+ // Delegate logging to unified backend
75
+ log = {
76
+ debug: (message, ...meta) => this._backend.debug(this._trace, message, ...meta),
77
+ info: (message, ...meta) => this._backend.info(this._trace, message, ...meta),
78
+ warn: (message, ...meta) => this._backend.warn(this._trace, message, ...meta),
79
+ error: (messageOrError, ...meta) => {
80
+ if (messageOrError instanceof errorMessages_1.BSBError) {
81
+ this._backend.error(messageOrError);
82
+ }
83
+ else if (messageOrError instanceof Error) {
84
+ this._backend.error(this._trace, messageOrError.message);
85
+ }
86
+ else {
87
+ this._backend.error(this._trace, messageOrError, ...meta);
88
+ }
89
+ }
90
+ };
91
+ // Delegate metrics to unified backend
92
+ metrics = {
93
+ counter: (name, description, help, labels) => this._backend.createCounter(name, description, help, labels),
94
+ gauge: (name, description, help, labels) => this._backend.createGauge(name, description, help, labels),
95
+ histogram: (name, description, help, boundaries, labels) => this._backend.createHistogram(name, description, help, boundaries, labels),
96
+ timer: () => this._backend.createTimer()
97
+ };
98
+ /**
99
+ * Create a child span with inherited attributes
100
+ *
101
+ * Creates a new Observable representing a child span for distributed tracing.
102
+ * All attributes from the parent are automatically inherited by the child.
103
+ *
104
+ * @param name - Name of the span (e.g., "database-query", "api-call")
105
+ * @param attributes - Additional attributes to add to this span
106
+ * @returns New Observable instance representing the child span
107
+ *
108
+ * @example
109
+ * ```typescript
110
+ * public async processOrder(obs: Observable) {
111
+ * // Create child span for database operation
112
+ * const dbSpan = obs.startSpan("fetch-order", { "order.id": "123" });
113
+ * try {
114
+ * const order = await this.db.getOrder("123");
115
+ * dbSpan.end({ "order.status": order.status });
116
+ * } catch (error) {
117
+ * dbSpan.error(error);
118
+ * dbSpan.end();
119
+ * }
120
+ * }
121
+ * ```
122
+ */
123
+ startSpan(name, attributes) {
124
+ const mergedAttributes = { ...this._attributes, ...attributes };
125
+ const childSpan = this._backend.createSpan(this._trace, name, mergedAttributes);
126
+ return new PluginObservable(childSpan.trace, this._resource, this._backend, mergedAttributes, childSpan);
127
+ }
128
+ /**
129
+ * Create a new Observable with an additional attribute
130
+ *
131
+ * Observables are immutable - this returns a new instance with the added attribute.
132
+ * The attribute is propagated to all child operations (logs, spans, etc.).
133
+ *
134
+ * @param key - Attribute key (e.g., "user.id", "transaction.type")
135
+ * @param value - Attribute value (string, number, or boolean)
136
+ * @returns New Observable instance with the added attribute
137
+ *
138
+ * @example
139
+ * ```typescript
140
+ * public async handleRequest(obs: Observable, userId: string) {
141
+ * // Add user ID to all subsequent operations
142
+ * const withUser = obs.setAttribute("user.id", userId);
143
+ *
144
+ * withUser.log.info("Processing request"); // Log includes user.id
145
+ * const span = withUser.startSpan("process"); // Span includes user.id
146
+ * }
147
+ * ```
148
+ */
149
+ setAttribute(key, value) {
150
+ return new PluginObservable(this._trace, this._resource, this._backend, { ...this._attributes, [key]: value }, this._span);
151
+ }
152
+ /**
153
+ * Create a new Observable with multiple attributes
154
+ *
155
+ * Observables are immutable - this returns a new instance with the added attributes.
156
+ * All attributes are propagated to child operations.
157
+ *
158
+ * @param attrs - Object containing attributes to add
159
+ * @returns New Observable instance with the added attributes
160
+ *
161
+ * @example
162
+ * ```typescript
163
+ * public async handleRequest(obs: Observable, context: RequestContext) {
164
+ * // Add multiple attributes at once
165
+ * const withContext = obs.setAttributes({
166
+ * "user.id": context.userId,
167
+ * "request.id": context.requestId,
168
+ * "request.method": context.method
169
+ * });
170
+ *
171
+ * withContext.log.info("Processing request");
172
+ * }
173
+ * ```
174
+ */
175
+ setAttributes(attrs) {
176
+ return new PluginObservable(this._trace, this._resource, this._backend, { ...this._attributes, ...attrs }, this._span);
177
+ }
178
+ /**
179
+ * Record an error to both logs and traces
180
+ *
181
+ * This method automatically records the error to both the logging system and
182
+ * the active span (if this Observable was created via startSpan()). This ensures
183
+ * errors are captured in both systems for complete observability.
184
+ *
185
+ * @param error - Error or BSBError instance to record
186
+ * @param attributes - Additional attributes to attach to the error
187
+ *
188
+ * @example
189
+ * ```typescript
190
+ * public async processData(obs: Observable) {
191
+ * const span = obs.startSpan("process-data");
192
+ * try {
193
+ * await this.riskyOperation();
194
+ * } catch (error) {
195
+ * // Record error to both logs and span
196
+ * span.error(error as Error, { "operation": "riskyOperation" });
197
+ * span.end();
198
+ * throw error;
199
+ * }
200
+ * }
201
+ * ```
202
+ */
203
+ error(error, attributes) {
204
+ const mergedAttrs = { ...this._attributes, ...attributes };
205
+ // Record to span
206
+ if (this._span) {
207
+ this._span.error(error, mergedAttrs);
208
+ }
209
+ // Record to logs
210
+ if (error instanceof errorMessages_1.BSBError) {
211
+ this._backend.error(error);
212
+ }
213
+ else {
214
+ this._backend.error(this._trace, error.message);
215
+ }
216
+ }
217
+ /**
218
+ * End the span (only applies if this Observable was created via startSpan())
219
+ *
220
+ * Completes the span and records the final state. If this Observable was not
221
+ * created via startSpan(), this method does nothing. Always call end() when the
222
+ * operation is complete to ensure proper trace completion.
223
+ *
224
+ * @param attributes - Final attributes to attach before ending the span
225
+ *
226
+ * @example
227
+ * ```typescript
228
+ * public async fetchData(obs: Observable) {
229
+ * const span = obs.startSpan("fetch-data");
230
+ * try {
231
+ * const data = await this.api.fetch();
232
+ * span.end({ "data.size": data.length, "status": "success" });
233
+ * return data;
234
+ * } catch (error) {
235
+ * span.error(error as Error);
236
+ * span.end({ "status": "failed" });
237
+ * throw error;
238
+ * }
239
+ * }
240
+ * ```
241
+ */
242
+ end(attributes) {
243
+ if (this._span) {
244
+ const mergedAttrs = { ...this._attributes, ...attributes };
245
+ this._span.end(mergedAttrs);
246
+ }
247
+ }
248
+ }
249
+ exports.PluginObservable = PluginObservable;
250
+ //# sourceMappingURL=PluginObservable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PluginObservable.js","sourceRoot":"","sources":["../../src/base/PluginObservable.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;;;AAKH,mDAA2C;AAI3C;;;;;;GAMG;AACH,MAAa,gBAAgB;IAGV,MAAM,CAAS;IACf,SAAS,CAAkB;IAC3B,WAAW,CAA4C;IACvD,QAAQ,CAAoB;IAC5B,KAAK,CAAS;IAE/B;;;;;;;OAOG;IACH,YACE,KAAa,EACb,QAAyB,EACzB,OAA0B,EAC1B,aAAwD,EAAE,EAC1D,IAAY;QAEZ,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;QAC1B,IAAI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C;IACH,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACH,GAAG,CAAC,UAAsD;QACxD,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YACf,MAAM,WAAW,GAAG,EAAE,GAAG,IAAI,CAAC,WAAW,EAAE,GAAG,UAAU,EAAE,CAAC;YAC3D,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;QAC9B,CAAC;IACH,CAAC;CACF;AA7RD,4CA6RC"}
@@ -0,0 +1,70 @@
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 { MainBaseConfig } from "./base";
28
+ /**
29
+ * Resource context following OpenTelemetry semantic conventions
30
+ * @group Observability
31
+ * @category Resource Context
32
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/interfaces/ResourceContext.html | API: ResourceContext}
33
+ */
34
+ export interface ResourceContext {
35
+ /**
36
+ * Service name (plugin name)
37
+ */
38
+ 'service.name': string;
39
+ /**
40
+ * Service version (plugin version)
41
+ */
42
+ 'service.version': string;
43
+ /**
44
+ * Service instance ID (app ID)
45
+ */
46
+ 'service.instance.id': string;
47
+ /**
48
+ * Deployment environment (mode)
49
+ */
50
+ 'deployment.environment': string;
51
+ /**
52
+ * Deployment region (optional)
53
+ */
54
+ 'deployment.region'?: string;
55
+ }
56
+ /**
57
+ * Builder for creating ResourceContext from MainBaseConfig
58
+ * @group Observability
59
+ * @category Resource Context
60
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/ResourceContextBuilder.html | API: ResourceContextBuilder}
61
+ */
62
+ export declare class ResourceContextBuilder {
63
+ /**
64
+ * Build a ResourceContext from MainBaseConfig
65
+ * @param config - The main base config
66
+ * @param region - Optional deployment region
67
+ * @returns ResourceContext object
68
+ */
69
+ static build(config: MainBaseConfig, region?: string): ResourceContext;
70
+ }
@@ -0,0 +1,54 @@
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.ResourceContextBuilder = void 0;
30
+ /**
31
+ * Builder for creating ResourceContext from MainBaseConfig
32
+ * @group Observability
33
+ * @category Resource Context
34
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/ResourceContextBuilder.html | API: ResourceContextBuilder}
35
+ */
36
+ class ResourceContextBuilder {
37
+ /**
38
+ * Build a ResourceContext from MainBaseConfig
39
+ * @param config - The main base config
40
+ * @param region - Optional deployment region
41
+ * @returns ResourceContext object
42
+ */
43
+ static build(config, region) {
44
+ return {
45
+ 'service.name': config.pluginName,
46
+ 'service.version': config.pluginVersion,
47
+ 'service.instance.id': config.appId,
48
+ 'deployment.environment': config.mode,
49
+ ...(region && { 'deployment.region': region })
50
+ };
51
+ }
52
+ }
53
+ exports.ResourceContextBuilder = ResourceContextBuilder;
54
+ //# sourceMappingURL=ResourceContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ResourceContext.js","sourceRoot":"","sources":["../../src/base/ResourceContext.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;;;AAiCH;;;;;GAKG;AACH,MAAa,sBAAsB;IACjC;;;;;OAKG;IACH,MAAM,CAAC,KAAK,CAAC,MAAsB,EAAE,MAAe;QAClD,OAAO;YACL,cAAc,EAAE,MAAM,CAAC,UAAU;YACjC,iBAAiB,EAAE,MAAM,CAAC,aAAa;YACvC,qBAAqB,EAAE,MAAM,CAAC,KAAK;YACnC,wBAAwB,EAAE,MAAM,CAAC,IAAI;YACrC,GAAG,CAAC,MAAM,IAAI,EAAE,mBAAmB,EAAE,MAAM,EAAE,CAAC;SAC/C,CAAC;IACJ,CAAC;CACF;AAhBD,wDAgBC"}
@@ -0,0 +1,264 @@
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 { DEBUG_MODE, IPluginObservable, Observable } from "../interfaces";
28
+ import { SBObservable } from "../serviceBase";
29
+ import { BSBReferenceConfigType } from "./PluginConfig";
30
+ /**
31
+ * Main base config
32
+ * @property appId - The unique app id of the app that is running
33
+ * @property mode - The mode the app is running in
34
+ * @property pluginName - The name of the plugin
35
+ * @property cwd - The current working directory of the app
36
+ * @property packageCwd - The directory of the package that contains the plugin
37
+ * @property pluginCwd - The directory of the plugin (src/plugins/{pluginName} or lib/plugins/{pluginName})
38
+ * @property region - The deployment region for resource context
39
+ */
40
+ export interface MainBaseConfig {
41
+ appId: string;
42
+ mode: DEBUG_MODE;
43
+ pluginName: string;
44
+ cwd: string;
45
+ packageCwd: string;
46
+ pluginCwd: string;
47
+ pluginVersion: string;
48
+ region?: string;
49
+ }
50
+ /**
51
+ * @hidden
52
+ */
53
+ export declare abstract class MainBase {
54
+ /**
55
+ * The unique app id of the app that is running
56
+ * @readonly
57
+ * @type {string}
58
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/MainBase.html#appId | API: MainBase.appId}
59
+ */
60
+ readonly appId: string;
61
+ /**
62
+ * The mode the app is running in
63
+ * @readonly
64
+ * @type {DEBUG_MODE}
65
+ * @example production (production mode - no debug)
66
+ * @example production-debug (production mode - debug)
67
+ * @example development (development mode - debug)
68
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/MainBase.html#mode | API: MainBase.mode}
69
+ */
70
+ readonly mode: DEBUG_MODE;
71
+ /**
72
+ * The current working directory of the app
73
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/MainBase.html#cwd | API: MainBase.cwd}
74
+ */
75
+ readonly cwd: string;
76
+ /**
77
+ * The current working directory of the plugin
78
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/MainBase.html#packageCwd | API: MainBase.packageCwd}
79
+ */
80
+ readonly packageCwd: string;
81
+ /**
82
+ * The current working directory of the service
83
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/MainBase.html#pluginCwd | API: MainBase.pluginCwd}
84
+ */
85
+ readonly pluginCwd: string;
86
+ /**
87
+ * The name of the plugin
88
+ * This is also the mapped name, or the name defined in the config rather than it's original defined name
89
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/MainBase.html#pluginName | API: MainBase.pluginName}
90
+ */
91
+ readonly pluginName: string;
92
+ /**
93
+ * The deployment region for resource context
94
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/MainBase.html#region | API: MainBase.region}
95
+ */
96
+ readonly region?: string;
97
+ constructor(config: MainBaseConfig);
98
+ /**
99
+ * Dispose
100
+ * Optional function to be called when the plugin is being disposed
101
+ *
102
+ * @example dispose?(): void; //to not use it
103
+ * @example dispose() { your code here };
104
+ */
105
+ dispose?(): void;
106
+ }
107
+ /**
108
+ * @hidden
109
+ */
110
+ export declare abstract class Base extends MainBase {
111
+ /**
112
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/Base.html | API: Base}
113
+ */
114
+ constructor(config: MainBaseConfig);
115
+ /**
116
+ * Dispose
117
+ * Optional function to be called when the plugin is being disposed
118
+ *
119
+ * @example dispose?(): void; //to not use it
120
+ * @example dispose() { your code here };
121
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/Base.html#dispose | API: Base.dispose}
122
+ */
123
+ abstract dispose?(): void;
124
+ /**
125
+ * Init
126
+ * Optional function to be called when the plugin is being initialized
127
+ * Can be sync or async
128
+ *
129
+ * @remarks
130
+ * **v9 BREAKING CHANGE**: Now requires Observable instead of DTrace.
131
+ * Observable provides unified access to logging, metrics, and tracing with automatic context propagation.
132
+ *
133
+ * @param obs - Observable context with logging, metrics, and trace information
134
+ *
135
+ * @example
136
+ * ```typescript
137
+ * async init(obs: Observable) {
138
+ * obs.log.info("Initializing plugin");
139
+ * // Set attributes for all child operations
140
+ * const withVersion = obs.setAttribute("plugin.version", "1.0.0");
141
+ * }
142
+ * ```
143
+ *
144
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/Base.html#init | API: Base.init}
145
+ */
146
+ abstract init?(obs: Observable): Promise<void> | void;
147
+ /**
148
+ * Run
149
+ * Optional function to be called when the plugin is being run
150
+ * Can be sync or async
151
+ *
152
+ * @remarks
153
+ * **v9 BREAKING CHANGE**: Now requires Observable instead of DTrace.
154
+ * Observable provides unified access to logging, metrics, and tracing with automatic context propagation.
155
+ *
156
+ * @param obs - Observable context with logging, metrics, and trace information
157
+ *
158
+ * @example
159
+ * ```typescript
160
+ * async run(obs: Observable) {
161
+ * obs.log.info("Running plugin");
162
+ * // Create child span for work
163
+ * const workObs = obs.startSpan("do-work");
164
+ * // ... do work ...
165
+ * workObs.end();
166
+ * }
167
+ * ```
168
+ *
169
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/Base.html#run | API: Base.run}
170
+ */
171
+ abstract run?(obs: Observable): Promise<void> | void;
172
+ }
173
+ /**
174
+ * @hidden
175
+ */
176
+ export type ConfigPropertyTypeSafe<ReferencedConfig extends BSBReferenceConfigType> = [ReferencedConfig] extends [never] ? never : ReferencedConfig extends undefined | null ? never : ReferencedConfig;
177
+ /**
178
+ * @hidden
179
+ */
180
+ export type ConfigConstructorTypeSafe<ReferencedConfig extends BSBReferenceConfigType> = [ReferencedConfig] extends [never] ? undefined : ReferencedConfig extends undefined | null ? undefined : ReferencedConfig;
181
+ /**
182
+ * @hidden
183
+ */
184
+ export interface BaseWithConfigConfig<ReferencedConfig extends BSBReferenceConfigType> extends MainBaseConfig {
185
+ config: ConfigConstructorTypeSafe<ReferencedConfig>;
186
+ }
187
+ /**
188
+ * @hidden
189
+ * used by logging plugins (does not need events or logging since logging logs its own logs)
190
+ */
191
+ export declare abstract class BaseWithConfig<ReferencedConfig extends BSBReferenceConfigType> extends Base {
192
+ /**
193
+ * The config of the plugin
194
+ * @type {PluginConfig}
195
+ * @readonly
196
+ */
197
+ readonly config: ConfigPropertyTypeSafe<ReferencedConfig>;
198
+ constructor(config: BaseWithConfigConfig<ReferencedConfig>);
199
+ }
200
+ /**
201
+ * BaseWithObservableConfig
202
+ * @property sbObservable - Passed in observable base - can be used to create new plugin observable backend
203
+ */
204
+ export interface BaseWithObservableConfig extends MainBaseConfig {
205
+ sbObservable: SBObservable;
206
+ }
207
+ /**
208
+ * @hidden
209
+ * Base class with internal observable support for Observable creation.
210
+ *
211
+ * **v9 Architecture:**
212
+ * - Logging and metrics accessed via Observable: `obs.log.info("message")`, `obs.metrics.counter(...)`
213
+ * - Use `this.createObservable()` to create new root traces
214
+ * - Pass Observable through all methods for trace context
215
+ *
216
+ * @see {@link Observable} for the unified observable interface
217
+ * @see {@link BSBService.createObservable} for creating Observables
218
+ */
219
+ export declare abstract class BaseWithObservable extends Base {
220
+ /**
221
+ * @hidden
222
+ * Internal ObservableBackend instance for creating Observables.
223
+ * NOT accessible to plugin code - use Observable instead.
224
+ */
225
+ private _observable;
226
+ constructor(config: BaseWithObservableConfig);
227
+ /**
228
+ * @hidden
229
+ * Get internal observable backend for Observable creation.
230
+ * NOT for direct plugin use.
231
+ */
232
+ protected get __internalObservable(): IPluginObservable;
233
+ }
234
+ /**
235
+ * @hidden
236
+ */
237
+ export interface BaseWithObservableAndConfigConfig<ReferencedConfig extends BSBReferenceConfigType> extends BaseWithObservableConfig, BaseWithConfigConfig<ReferencedConfig> {
238
+ }
239
+ /**
240
+ * @hidden
241
+ * Base class with config and internal observable support.
242
+ * Use Observable for all logging and metrics operations.
243
+ */
244
+ export declare abstract class BaseWithObservableAndConfig<ReferencedConfig extends BSBReferenceConfigType> extends BaseWithConfig<ReferencedConfig> {
245
+ /**
246
+ * @hidden
247
+ * Internal ObservableBackend instance for creating Observables.
248
+ */
249
+ private _observable;
250
+ constructor(config: BaseWithObservableAndConfigConfig<ReferencedConfig>);
251
+ /**
252
+ * @hidden
253
+ * Get internal observable backend for Observable creation.
254
+ */
255
+ protected get __internalObservable(): IPluginObservable;
256
+ }
257
+ /**
258
+ * @hidden
259
+ */
260
+ export declare const NS_PER_SEC = 1000000000;
261
+ /**
262
+ * @hidden
263
+ */
264
+ export declare const MS_PER_NS = 0.000001;