@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,268 @@
|
|
|
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.BSBPluginEventsRef = exports.PluginEvents = void 0;
|
|
30
|
+
const EventValidator_1 = require("./EventValidator");
|
|
31
|
+
/**
|
|
32
|
+
* Schema-first plugin events handler with automatic validation and object parameters.
|
|
33
|
+
* @group Events
|
|
34
|
+
* @category Plugin Development Tools
|
|
35
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/PluginEvents.html | API: PluginEvents}
|
|
36
|
+
*/
|
|
37
|
+
class PluginEvents {
|
|
38
|
+
events;
|
|
39
|
+
service;
|
|
40
|
+
cachedPluginName;
|
|
41
|
+
validator;
|
|
42
|
+
eventSchemas;
|
|
43
|
+
constructor(mode, events, context, eventSchemas, observableBackend) {
|
|
44
|
+
this.events = events;
|
|
45
|
+
this.service = context;
|
|
46
|
+
this.cachedPluginName = context.pluginName;
|
|
47
|
+
this.eventSchemas = eventSchemas;
|
|
48
|
+
this.validator = new EventValidator_1.EventValidator({}, observableBackend);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Helper to extract DTrace from Observable | DTrace
|
|
52
|
+
* @hidden
|
|
53
|
+
*/
|
|
54
|
+
extractTrace(obs) {
|
|
55
|
+
if ('trace' in obs && typeof obs.trace === 'object') {
|
|
56
|
+
return obs.trace;
|
|
57
|
+
}
|
|
58
|
+
return obs;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Helper to create Observable from DTrace if needed
|
|
62
|
+
* @hidden
|
|
63
|
+
*/
|
|
64
|
+
ensureObservable(obsOrTrace) {
|
|
65
|
+
if ('trace' in obsOrTrace && 'log' in obsOrTrace) {
|
|
66
|
+
return obsOrTrace;
|
|
67
|
+
}
|
|
68
|
+
// Create Observable from DTrace
|
|
69
|
+
return this.service.createObservable(obsOrTrace);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Listen for broadcast events emitted by other plugins with full type safety.
|
|
73
|
+
* @param eventName - Name of the event to listen for (strongly typed)
|
|
74
|
+
* @param obs - Observable context (v9 BREAKING: Observable only, no longer accepts DTrace)
|
|
75
|
+
* @param listener - Function to call when event is received (receives Observable and validated input object)
|
|
76
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/PluginEvents.html#onBroadcast | API: PluginEvents#onBroadcast}
|
|
77
|
+
*/
|
|
78
|
+
async onBroadcast(eventName, obs, listener) {
|
|
79
|
+
const trace = this.extractTrace(obs);
|
|
80
|
+
const wrappedListener = async (handlerTrace, rawInput) => {
|
|
81
|
+
let validatedInput = rawInput;
|
|
82
|
+
// Validate input if schema exists
|
|
83
|
+
const schema = this.eventSchemas.onBroadcast?.[eventName];
|
|
84
|
+
if (schema) {
|
|
85
|
+
const result = this.validator.validateInput(eventName, rawInput, schema.input, handlerTrace);
|
|
86
|
+
if (!result.success) {
|
|
87
|
+
throw result.error;
|
|
88
|
+
}
|
|
89
|
+
validatedInput = result.data;
|
|
90
|
+
}
|
|
91
|
+
// Create Observable for handler
|
|
92
|
+
const handlerObs = this.ensureObservable(handlerTrace);
|
|
93
|
+
await listener(handlerObs, validatedInput);
|
|
94
|
+
};
|
|
95
|
+
return this.events.onBroadcast(this.service, trace, this.cachedPluginName, eventName, wrappedListener);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Emit broadcast events to all listening plugins with full type safety.
|
|
99
|
+
* @param eventName - Name of the event to emit (strongly typed)
|
|
100
|
+
* @param obs - Observable context (v9 BREAKING: Observable only, no longer accepts DTrace)
|
|
101
|
+
* @param input - Event input object (will be validated against schema)
|
|
102
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/PluginEvents.html#emitBroadcast | API: PluginEvents#emitBroadcast}
|
|
103
|
+
*/
|
|
104
|
+
async emitBroadcast(eventName, obs, input) {
|
|
105
|
+
const trace = this.extractTrace(obs);
|
|
106
|
+
let validatedInput = input;
|
|
107
|
+
// Validate input if schema exists
|
|
108
|
+
const schema = this.eventSchemas.emitBroadcast?.[eventName];
|
|
109
|
+
if (schema) {
|
|
110
|
+
const result = this.validator.validateInput(eventName, input, schema.input, trace);
|
|
111
|
+
if (!result.success) {
|
|
112
|
+
throw result.error;
|
|
113
|
+
}
|
|
114
|
+
validatedInput = result.data;
|
|
115
|
+
}
|
|
116
|
+
return this.events.emitBroadcast(trace, this.cachedPluginName, eventName, [validatedInput]);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Listen for fire-and-forget events from other plugins with full type safety.
|
|
120
|
+
* @param eventName - Name of the event to listen for (strongly typed)
|
|
121
|
+
* @param obs - Observable context (v9 BREAKING: Observable only, no longer accepts DTrace)
|
|
122
|
+
* @param listener - Function to call when event is received (receives Observable and validated input object)
|
|
123
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/PluginEvents.html#onEvent | API: PluginEvents#onEvent}
|
|
124
|
+
*/
|
|
125
|
+
async onEvent(eventName, obs, listener) {
|
|
126
|
+
const trace = this.extractTrace(obs);
|
|
127
|
+
const wrappedListener = async (handlerTrace, rawInput) => {
|
|
128
|
+
let validatedInput = rawInput;
|
|
129
|
+
// Validate input if schema exists
|
|
130
|
+
const schema = this.eventSchemas.onEvents?.[eventName];
|
|
131
|
+
if (schema) {
|
|
132
|
+
const result = this.validator.validateInput(eventName, rawInput, schema.input, handlerTrace);
|
|
133
|
+
if (!result.success) {
|
|
134
|
+
throw result.error;
|
|
135
|
+
}
|
|
136
|
+
validatedInput = result.data;
|
|
137
|
+
}
|
|
138
|
+
// Create Observable for handler
|
|
139
|
+
const handlerObs = this.ensureObservable(handlerTrace);
|
|
140
|
+
await listener(handlerObs, validatedInput);
|
|
141
|
+
};
|
|
142
|
+
return this.events.onEvent(trace, this.service, this.cachedPluginName, eventName, wrappedListener);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Emit fire-and-forget events to other plugins with full type safety.
|
|
146
|
+
* @param eventName - Name of the event to emit (strongly typed)
|
|
147
|
+
* @param obs - Observable context (v9 BREAKING: Observable only, no longer accepts DTrace)
|
|
148
|
+
* @param input - Event input object (will be validated against schema)
|
|
149
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/PluginEvents.html#emitEvent | API: PluginEvents#emitEvent}
|
|
150
|
+
*/
|
|
151
|
+
async emitEvent(eventName, obs, input) {
|
|
152
|
+
const trace = this.extractTrace(obs);
|
|
153
|
+
let validatedInput = input;
|
|
154
|
+
// Validate input if schema exists
|
|
155
|
+
const schema = this.eventSchemas.emitEvents?.[eventName];
|
|
156
|
+
if (schema) {
|
|
157
|
+
const result = this.validator.validateInput(eventName, input, schema.input, trace);
|
|
158
|
+
if (!result.success) {
|
|
159
|
+
throw result.error;
|
|
160
|
+
}
|
|
161
|
+
validatedInput = result.data;
|
|
162
|
+
}
|
|
163
|
+
return this.events.emitEvent(trace, this.cachedPluginName, eventName, validatedInput);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Listen for returnable events from other plugins with full type safety.
|
|
167
|
+
* @param eventName - Name of the event to listen for (strongly typed)
|
|
168
|
+
* @param obs - Observable context (v9 BREAKING: Observable only, no longer accepts DTrace)
|
|
169
|
+
* @param listener - Function to call when event is received (receives Observable), must return a value
|
|
170
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/PluginEvents.html#onReturnableEvent | API: PluginEvents#onReturnableEvent}
|
|
171
|
+
*/
|
|
172
|
+
async onReturnableEvent(eventName, obs, listener) {
|
|
173
|
+
const trace = this.extractTrace(obs);
|
|
174
|
+
const wrappedListener = async (handlerTrace, rawInput) => {
|
|
175
|
+
let validatedInput = rawInput;
|
|
176
|
+
const schema = this.eventSchemas.onReturnableEvents?.[eventName];
|
|
177
|
+
// Validate input if schema exists
|
|
178
|
+
if (schema) {
|
|
179
|
+
const inputResult = this.validator.validateInput(eventName, rawInput, schema.input, handlerTrace);
|
|
180
|
+
if (!inputResult.success) {
|
|
181
|
+
throw inputResult.error;
|
|
182
|
+
}
|
|
183
|
+
validatedInput = inputResult.data;
|
|
184
|
+
}
|
|
185
|
+
// Create Observable for handler
|
|
186
|
+
const handlerObs = this.ensureObservable(handlerTrace);
|
|
187
|
+
const result = await listener(handlerObs, validatedInput);
|
|
188
|
+
// Validate output if schema exists
|
|
189
|
+
if (schema) {
|
|
190
|
+
const outputResult = this.validator.validateOutput(eventName, result, schema.output, handlerTrace);
|
|
191
|
+
if (!outputResult.success) {
|
|
192
|
+
throw outputResult.error;
|
|
193
|
+
}
|
|
194
|
+
return outputResult.data;
|
|
195
|
+
}
|
|
196
|
+
return result;
|
|
197
|
+
};
|
|
198
|
+
return this.events.onReturnableEvent(trace, this.service, this.cachedPluginName, eventName, wrappedListener);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Emit returnable events and wait for response with full type safety.
|
|
202
|
+
* @param eventName - Name of the event to emit (strongly typed)
|
|
203
|
+
* @param obs - Observable context (v9 BREAKING: Observable only, no longer accepts DTrace)
|
|
204
|
+
* @param input - Event input object (will be validated against schema)
|
|
205
|
+
* @param timeoutSeconds - Optional timeout in seconds (default: 5)
|
|
206
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/PluginEvents.html#emitEventAndReturn | API: PluginEvents#emitEventAndReturn}
|
|
207
|
+
*/
|
|
208
|
+
async emitEventAndReturn(eventName, obs, input, timeoutSeconds = 5) {
|
|
209
|
+
const trace = this.extractTrace(obs);
|
|
210
|
+
let validatedInput = input;
|
|
211
|
+
const schema = this.eventSchemas.emitReturnableEvents?.[eventName];
|
|
212
|
+
// Validate input if schema exists
|
|
213
|
+
if (schema) {
|
|
214
|
+
const inputResult = this.validator.validateInput(eventName, input, schema.input, trace);
|
|
215
|
+
if (!inputResult.success) {
|
|
216
|
+
throw inputResult.error;
|
|
217
|
+
}
|
|
218
|
+
validatedInput = inputResult.data;
|
|
219
|
+
}
|
|
220
|
+
const result = await this.events.emitEventAndReturn(trace, this.cachedPluginName, eventName, timeoutSeconds, validatedInput);
|
|
221
|
+
// Validate output if schema exists
|
|
222
|
+
if (schema) {
|
|
223
|
+
const outputResult = this.validator.validateOutput(eventName, result, schema.output, trace);
|
|
224
|
+
if (!outputResult.success) {
|
|
225
|
+
throw outputResult.error;
|
|
226
|
+
}
|
|
227
|
+
return outputResult.data;
|
|
228
|
+
}
|
|
229
|
+
return result;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Get stream ID for receiving streamed data from another plugin.
|
|
233
|
+
* @param trace - Trace for logging context
|
|
234
|
+
* @param eventName - Name of the stream event
|
|
235
|
+
* @param listener - Function called when stream is received
|
|
236
|
+
* @param timeoutSeconds - Optional timeout in seconds
|
|
237
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/PluginEvents.html#receiveStream | API: PluginEvents#receiveStream}
|
|
238
|
+
*/
|
|
239
|
+
async receiveStream(trace, eventName, listener, timeoutSeconds) {
|
|
240
|
+
return this.events.receiveStream(trace, this.service, this.cachedPluginName, eventName, listener, timeoutSeconds);
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Send stream data to another plugin.
|
|
244
|
+
* @param trace - Trace for logging context
|
|
245
|
+
* @param eventName - Name of the stream event
|
|
246
|
+
* @param streamId - ID of the stream to send to
|
|
247
|
+
* @param stream - The readable stream to send
|
|
248
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/PluginEvents.html#sendStream | API: PluginEvents#sendStream}
|
|
249
|
+
*/
|
|
250
|
+
async sendStream(trace, eventName, streamId, stream) {
|
|
251
|
+
return this.events.sendStream(trace, this.cachedPluginName, eventName, streamId, stream);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
exports.PluginEvents = PluginEvents;
|
|
255
|
+
/**
|
|
256
|
+
* @hidden
|
|
257
|
+
* DO NOT REFERENCE/USE THIS CLASS - IT IS AN INTERNALLY REFERENCED CLASS
|
|
258
|
+
*/
|
|
259
|
+
class BSBPluginEventsRef {
|
|
260
|
+
emitEvents = {};
|
|
261
|
+
onEvents = {};
|
|
262
|
+
emitReturnableEvents = {};
|
|
263
|
+
onReturnableEvents = {};
|
|
264
|
+
emitBroadcast = {};
|
|
265
|
+
onBroadcast = {};
|
|
266
|
+
}
|
|
267
|
+
exports.BSBPluginEventsRef = BSBPluginEventsRef;
|
|
268
|
+
//# sourceMappingURL=PluginEvents.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PluginEvents.js","sourceRoot":"","sources":["../../src/base/PluginEvents.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;;;AAcH,qDAAkD;AASlD;;;;;GAKG;AACH,MAAa,YAAY;IACb,MAAM,CAAW;IACjB,OAAO,CAA+C;IACtD,gBAAgB,CAAS;IACzB,SAAS,CAAiB;IAC1B,YAAY,CAAgB;IAEpC,YACI,IAAgB,EAChB,MAAgB,EAChB,OAAsC,EACtC,YAA2B,EAC3B,iBAAqC;QAErC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,UAAU,CAAC;QAC3C,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,SAAS,GAAG,IAAI,+BAAc,CAAC,EAAE,EAAE,iBAAiB,CAAC,CAAC;IAC/D,CAAC;IAED;;;OAGG;IACK,YAAY,CAAC,GAAwB;QACzC,IAAI,OAAO,IAAI,GAAG,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,QAAQ,EAAE,CAAC;YAClD,OAAO,GAAG,CAAC,KAAK,CAAC;QACrB,CAAC;QACD,OAAO,GAAa,CAAC;IACzB,CAAC;IAED;;;OAGG;IACK,gBAAgB,CAAC,UAA+B;QACpD,IAAI,OAAO,IAAI,UAAU,IAAI,KAAK,IAAI,UAAU,EAAE,CAAC;YAC/C,OAAO,UAAwB,CAAC;QACpC,CAAC;QACD,gCAAgC;QAChC,OAAQ,IAAI,CAAC,OAAe,CAAC,gBAAgB,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;;;;;;;OAOG;IACI,KAAK,CAAC,UAAU,CACnB,KAAa,EACb,SAAiB,EACjB,QAAgB,EAChB,MAAgB;QAEhB,OAAO,IAAI,CAAC,MAAM,CAAC,UAAU,CACzB,KAAK,EACL,IAAI,CAAC,gBAAgB,EACrB,SAAS,EACT,QAAQ,EACR,MAAM,CACT,CAAC;IACN,CAAC;CACJ;AAtWD,oCAsWC;AAED;;;GAGG;AACH,MAAa,kBAAkB;IACpB,UAAU,GAAG,EAAE,CAAC;IAChB,QAAQ,GAAG,EAAE,CAAC;IACd,oBAAoB,GAAG,EAAE,CAAC;IAC1B,kBAAkB,GAAG,EAAE,CAAC;IACxB,aAAa,GAAG,EAAE,CAAC;IACnB,WAAW,GAAG,EAAE,CAAC;CAC3B;AAPD,gDAOC"}
|
|
@@ -0,0 +1,196 @@
|
|
|
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 { Observable } from '../interfaces/observable';
|
|
28
|
+
import { DTrace, Trace, IPluginObservable } from '../interfaces/metrics';
|
|
29
|
+
import { ResourceContext } from './ResourceContext';
|
|
30
|
+
import { BSBError } from './errorMessages';
|
|
31
|
+
import { SmartLogMeta } from '../interfaces/logging';
|
|
32
|
+
import { z } from 'zod';
|
|
33
|
+
/**
|
|
34
|
+
* Implementation of Observable interface that wraps DTrace with observability features
|
|
35
|
+
*
|
|
36
|
+
* @group Observability
|
|
37
|
+
* @category Plugin Development Tools
|
|
38
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/PluginObservable.html | API: PluginObservable}
|
|
39
|
+
*/
|
|
40
|
+
export declare class PluginObservable<TAttributeSchema extends z.ZodSchema = z.ZodAny> implements Observable<TAttributeSchema> {
|
|
41
|
+
private readonly _trace;
|
|
42
|
+
private readonly _resource;
|
|
43
|
+
private readonly _attributes;
|
|
44
|
+
private readonly _backend;
|
|
45
|
+
private readonly _span?;
|
|
46
|
+
/**
|
|
47
|
+
* Create a PluginObservable
|
|
48
|
+
* @param trace - DTrace object
|
|
49
|
+
* @param resource - Resource context
|
|
50
|
+
* @param backend - IPluginObservable instance (unified logging and metrics backend)
|
|
51
|
+
* @param attributes - Initial attributes
|
|
52
|
+
* @param span - Optional Trace/Span object (if created via startSpan())
|
|
53
|
+
*/
|
|
54
|
+
constructor(trace: DTrace, resource: ResourceContext, backend: IPluginObservable, attributes?: Record<string, string | number | boolean>, span?: Trace);
|
|
55
|
+
get trace(): DTrace;
|
|
56
|
+
get traceId(): string;
|
|
57
|
+
get spanId(): string;
|
|
58
|
+
get resource(): ResourceContext;
|
|
59
|
+
get attributes(): Record<string, string | number | boolean>;
|
|
60
|
+
log: {
|
|
61
|
+
debug: <T extends string>(message: T, ...meta: SmartLogMeta<T>) => void;
|
|
62
|
+
info: <T extends string>(message: T, ...meta: SmartLogMeta<T>) => void;
|
|
63
|
+
warn: <T extends string>(message: T, ...meta: SmartLogMeta<T>) => void;
|
|
64
|
+
error: <T extends string>(messageOrError: T | Error, ...meta: SmartLogMeta<T>) => void;
|
|
65
|
+
};
|
|
66
|
+
metrics: {
|
|
67
|
+
counter: <LABELS extends string | undefined>(name: string, description: string, help: string, labels?: LABELS[]) => import("../interfaces/metrics").Counter<LABELS>;
|
|
68
|
+
gauge: <LABELS extends string | undefined>(name: string, description: string, help: string, labels?: LABELS[]) => import("../interfaces/metrics").Gauge<LABELS>;
|
|
69
|
+
histogram: <LABELS extends string | undefined>(name: string, description: string, help: string, boundaries?: number[], labels?: LABELS[]) => import("../interfaces/metrics").Histogram<LABELS>;
|
|
70
|
+
timer: () => import("../interfaces/metrics").Timer;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Create a child span with inherited attributes
|
|
74
|
+
*
|
|
75
|
+
* Creates a new Observable representing a child span for distributed tracing.
|
|
76
|
+
* All attributes from the parent are automatically inherited by the child.
|
|
77
|
+
*
|
|
78
|
+
* @param name - Name of the span (e.g., "database-query", "api-call")
|
|
79
|
+
* @param attributes - Additional attributes to add to this span
|
|
80
|
+
* @returns New Observable instance representing the child span
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* public async processOrder(obs: Observable) {
|
|
85
|
+
* // Create child span for database operation
|
|
86
|
+
* const dbSpan = obs.startSpan("fetch-order", { "order.id": "123" });
|
|
87
|
+
* try {
|
|
88
|
+
* const order = await this.db.getOrder("123");
|
|
89
|
+
* dbSpan.end({ "order.status": order.status });
|
|
90
|
+
* } catch (error) {
|
|
91
|
+
* dbSpan.error(error);
|
|
92
|
+
* dbSpan.end();
|
|
93
|
+
* }
|
|
94
|
+
* }
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
startSpan(name: string, attributes?: Record<string, string | number | boolean>): Observable<TAttributeSchema>;
|
|
98
|
+
/**
|
|
99
|
+
* Create a new Observable with an additional attribute
|
|
100
|
+
*
|
|
101
|
+
* Observables are immutable - this returns a new instance with the added attribute.
|
|
102
|
+
* The attribute is propagated to all child operations (logs, spans, etc.).
|
|
103
|
+
*
|
|
104
|
+
* @param key - Attribute key (e.g., "user.id", "transaction.type")
|
|
105
|
+
* @param value - Attribute value (string, number, or boolean)
|
|
106
|
+
* @returns New Observable instance with the added attribute
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```typescript
|
|
110
|
+
* public async handleRequest(obs: Observable, userId: string) {
|
|
111
|
+
* // Add user ID to all subsequent operations
|
|
112
|
+
* const withUser = obs.setAttribute("user.id", userId);
|
|
113
|
+
*
|
|
114
|
+
* withUser.log.info("Processing request"); // Log includes user.id
|
|
115
|
+
* const span = withUser.startSpan("process"); // Span includes user.id
|
|
116
|
+
* }
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
setAttribute<K extends string, V extends string | number | boolean>(key: K, value: V): Observable<TAttributeSchema>;
|
|
120
|
+
/**
|
|
121
|
+
* Create a new Observable with multiple attributes
|
|
122
|
+
*
|
|
123
|
+
* Observables are immutable - this returns a new instance with the added attributes.
|
|
124
|
+
* All attributes are propagated to child operations.
|
|
125
|
+
*
|
|
126
|
+
* @param attrs - Object containing attributes to add
|
|
127
|
+
* @returns New Observable instance with the added attributes
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* public async handleRequest(obs: Observable, context: RequestContext) {
|
|
132
|
+
* // Add multiple attributes at once
|
|
133
|
+
* const withContext = obs.setAttributes({
|
|
134
|
+
* "user.id": context.userId,
|
|
135
|
+
* "request.id": context.requestId,
|
|
136
|
+
* "request.method": context.method
|
|
137
|
+
* });
|
|
138
|
+
*
|
|
139
|
+
* withContext.log.info("Processing request");
|
|
140
|
+
* }
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
setAttributes(attrs: Record<string, string | number | boolean>): Observable<TAttributeSchema>;
|
|
144
|
+
/**
|
|
145
|
+
* Record an error to both logs and traces
|
|
146
|
+
*
|
|
147
|
+
* This method automatically records the error to both the logging system and
|
|
148
|
+
* the active span (if this Observable was created via startSpan()). This ensures
|
|
149
|
+
* errors are captured in both systems for complete observability.
|
|
150
|
+
*
|
|
151
|
+
* @param error - Error or BSBError instance to record
|
|
152
|
+
* @param attributes - Additional attributes to attach to the error
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```typescript
|
|
156
|
+
* public async processData(obs: Observable) {
|
|
157
|
+
* const span = obs.startSpan("process-data");
|
|
158
|
+
* try {
|
|
159
|
+
* await this.riskyOperation();
|
|
160
|
+
* } catch (error) {
|
|
161
|
+
* // Record error to both logs and span
|
|
162
|
+
* span.error(error as Error, { "operation": "riskyOperation" });
|
|
163
|
+
* span.end();
|
|
164
|
+
* throw error;
|
|
165
|
+
* }
|
|
166
|
+
* }
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
error(error: Error | BSBError<any>, attributes?: Record<string, string | number | boolean>): void;
|
|
170
|
+
/**
|
|
171
|
+
* End the span (only applies if this Observable was created via startSpan())
|
|
172
|
+
*
|
|
173
|
+
* Completes the span and records the final state. If this Observable was not
|
|
174
|
+
* created via startSpan(), this method does nothing. Always call end() when the
|
|
175
|
+
* operation is complete to ensure proper trace completion.
|
|
176
|
+
*
|
|
177
|
+
* @param attributes - Final attributes to attach before ending the span
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```typescript
|
|
181
|
+
* public async fetchData(obs: Observable) {
|
|
182
|
+
* const span = obs.startSpan("fetch-data");
|
|
183
|
+
* try {
|
|
184
|
+
* const data = await this.api.fetch();
|
|
185
|
+
* span.end({ "data.size": data.length, "status": "success" });
|
|
186
|
+
* return data;
|
|
187
|
+
* } catch (error) {
|
|
188
|
+
* span.error(error as Error);
|
|
189
|
+
* span.end({ "status": "failed" });
|
|
190
|
+
* throw error;
|
|
191
|
+
* }
|
|
192
|
+
* }
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
end(attributes?: Record<string, string | number | boolean>): void;
|
|
196
|
+
}
|