@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,32 @@
1
+ Commercial License
2
+
3
+ This Commercial License (the "License") is a legal agreement between you (either an individual or a single entity) and the copyright holder(s) of this software.
4
+
5
+ 1. Definitions
6
+ "Software" refers to the software and associated documentation files.
7
+ "License" refers to this Commercial License agreement.
8
+ "Licensor" refers to the copyright holder(s).
9
+ "You" refers to the individual or entity exercising permissions granted by this License.
10
+
11
+ 2. Grant of License
12
+ Subject to the terms and conditions of this License, the Licensor hereby grants you a worldwide, non-exclusive, royalty-bearing license to use, reproduce, modify, display, perform, sublicense and distribute the Software.
13
+
14
+ 3. Redistribution
15
+ You may reproduce and distribute copies of the Software in any medium, with or without modifications, provided that you meet the following conditions:
16
+ a) You must give any recipients of the Software a copy of this License;
17
+ b) You must retain all copyright, patent, trademark, and attribution notices;
18
+ c) You must not distribute the Software under the AGPL or any other open source license.
19
+
20
+ 4. Patent License
21
+ Subject to the terms and conditions of this License, each Licensor hereby grants to you a perpetual, worldwide, non-exclusive, royalty-bearing patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Software.
22
+
23
+ 5. Disclaimer of Warranty
24
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25
+
26
+ 6. Limitation of Liability
27
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
+
29
+ 7. Termination
30
+ This License and the rights granted hereunder will terminate automatically if you fail to comply with its terms and conditions.
31
+
32
+ For licensing terms, pricing, and other inquiries, please contact the copyright holder.
package/README.md ADDED
@@ -0,0 +1,263 @@
1
+ ### BSB Node.js Service Base – Project Guide
2
+
3
+ Better Service Base (BSB) is an event‑bus based microservice framework for Node.js/TypeScript. This package provides the Node.js implementation of the core runtime, plugin system, and developer tooling.
4
+
5
+ **Version 9.0** introduces breaking changes with improved type safety, cross-language support, and automated code generation. See [Plugin Development Guide](./PLUGIN_DEVELOPMENT.md) for v9 patterns.
6
+
7
+ ### Intended Usage (Container‑first)
8
+ - This project is designed to run standalone inside a Docker container and execute plugins authored and published separately.
9
+ - It is not intended to be embedded or imported as a library into another application package.
10
+ - Deploy the container and supply plugins via `BSB_PLUGIN_DIR` (recommended) or `BSB_PLUGINS` installation at container startup.
11
+
12
+ #### Requirements
13
+ - Node.js >= 23.0.0, npm >= 11.0.0
14
+ - TypeScript 5.x for development
15
+
16
+ ### Project Structure
17
+ - `src/`
18
+ - `index.ts`: Public exports for the package (base classes, interfaces, controllers).
19
+ - `cli.ts`: Production CLI entry (also exposed as `bin` → `bsb`).
20
+ - `dev.ts`: Development runner with hot-reload and restart controls.
21
+ - `client.ts`: Legacy helper for embedding a client; avoid in new code.
22
+ - `base/`: Core building blocks used by plugins and services
23
+ - `BSBService`, `BSBServiceClient`: Base classes for service plugins and their clients
24
+ - `PluginLogging`, `PluginMetrics`, `PluginEvents`: Per‑plugin facades into logging/metrics/events
25
+ - `BSBConfig`, `BSBLogging`, `BSBMetrics`, `BSBEvents`: Base plugin contracts
26
+ - `factory.ts`: Option resolution and presets for `ServiceBase`
27
+ - `interfaces/`: Strong TypeScript contracts for options, logging, metrics, events, results, tools
28
+ - `serviceBase/`: Runtime controllers that orchestrate the system
29
+ - `serviceBase.ts`: Main runtime (`ServiceBase`) – boot/init/run/dispose pipeline
30
+ - `config.ts`: Loads and initializes the configuration plugin
31
+ - `logging.ts`: Manages logging plugins and log dispatch
32
+ - `metrics.ts`: Manages metrics plugins via an internal event bus
33
+ - `events.ts`: Manages event plugins and exposes event APIs (broadcast, emit, return, streams)
34
+ - `plugins.ts`: Resolves and loads plugins from local build or external locations
35
+ - `services.ts`: Loads, orders, and runs service plugins + their clients
36
+ - `plugins/`: Built‑in plugins
37
+ - `config-default/`: Default configuration plugin
38
+ - `events-default/`: Default event bus
39
+ - `logging-default/`: Console logger with colors
40
+ - `metrics-default/`: Basic metrics implementation
41
+ - `service-default{0..4}/`, `service-benchmarkify/`: Example/demo service plugins
42
+ - `tests/`: Mocha + ts-node test suite
43
+ - `lib/`: Compiled JavaScript output (generated by `tsc`)
44
+ - `templates/`: Scaffolding for new plugins (`plugin.ts`, `pluginClient.ts`, `events.ts`, `logger.ts`)
45
+ - `Dockerfile`, `entrypoint.sh`, `entrypoint.js`: Container build/runtime assets
46
+ - `typedoc.json`, `docs.json`, `typedoc-theme/`: API docs generation configuration/theme
47
+
48
+ ### What's New in v9
49
+
50
+ v9 introduces breaking changes focused on type safety, developer experience, and cross-language support:
51
+
52
+ **Type Safety Improvements:**
53
+ - `createEventSchemas()` - No more `as const` required, automatic type inference
54
+ - Type branding - Compile-time validation that event types match categories
55
+ - Duplicate name detection - Warns about confusing duplicate event names
56
+
57
+ **Simplified Configuration:**
58
+ - `createConfigSchema()` - Single function replaces class pattern
59
+ - Plugin metadata - Define once, auto-generates PLUGIN_CLIENT and bsb-plugin.json
60
+ - Centralized schemas - All generated JSON in lib/schemas/ with JSON $ref references
61
+
62
+ **Cross-Language Support:**
63
+ - Type helpers - int32, int64, uuid, datetime for precise type mapping
64
+ - Schema export - Auto-generates JSON schemas for client code generation
65
+ - Multi-language clients - Generate type-safe clients in TypeScript, C#, Go, Java
66
+ - Cross-plugin events - Type-safe communication between plugins (no `any` types)
67
+
68
+ See [Plugin Development Guide](./PLUGIN_DEVELOPMENT.md) for migration details and examples.
69
+
70
+ ### Runtime Architecture
71
+ The `ServiceBase` class is the primary entry point. It coordinates the framework subsystems and plugin lifecycle.
72
+
73
+ Boot flow (high level):
74
+ 1) Construct `ServiceBase` (select mode, cwd, and controller implementations)
75
+ 2) `init()` sequence
76
+ - `SBConfig.init()` → choose and init configuration plugin
77
+ - `SBLogging.init()` → load logging plugins
78
+ - `SBMetrics.init()` → load metrics plugins
79
+ - `SBEvents.init()` → load events plugins (+ always adds `events-default` first)
80
+ - `SBServices.setup()` → discover service plugins from config, create instances, and map dependencies; then `SBServices.init()` respecting declared ordering
81
+ 3) `run()` sequence
82
+ - Start logging, events, then `SBServices.run()` (ordered)
83
+ - Dispose config for safety, start heartbeat metric
84
+ 4) `dispose()`
85
+ - Disposes services, events, metrics, logging, and config; exits the process
86
+
87
+ Timekeeping metrics are recorded for each step and logged as timers. A heartbeat counter runs hourly.
88
+
89
+ ### ServiceBase APIs (for BSB contributors only)
90
+ - Construction (modern):
91
+ ```ts
92
+ import { ServiceBase, BSBPreset } from "@bettercorp/service-base";
93
+
94
+ // Simple defaults (local dev only)
95
+ const app = ServiceBase.development();
96
+
97
+ // Preset with overrides (local dev only)
98
+ const prod = ServiceBase.fromPreset(BSBPreset.PRODUCTION, { cwd: "/app" });
99
+
100
+ // Fully custom options (local dev only)
101
+ const custom = ServiceBase.create({ cwd: process.cwd(), plugins: ["logging-default", "events-default"] });
102
+ ```
103
+ - Lifecycle:
104
+ ```ts
105
+ await app.init();
106
+ await app.run();
107
+ // app.dispose(code, reason) is called automatically on signals/errors
108
+ ```
109
+ - Add a service plugin programmatically (local dev only, before services init):
110
+ ```ts
111
+ import { BSBService } from "@bettercorp/service-base";
112
+
113
+ class MyService extends BSBService<{ greeting: string }> { /* ... */ }
114
+ await app.addService("service-my", MyService as any, { greeting: "hi" });
115
+ ```
116
+
117
+ ### Subsystems
118
+ - `SBConfig` (configuration)
119
+ - Defaults to `config-default` plugin; can be replaced via environment variables
120
+ - Provides resolved plugin lists: services, events, logging, metrics
121
+ - Exposes `getPluginConfig()` for per‑plugin configuration
122
+ - `SBLogging` (logging)
123
+ - Manages logger plugins; routes debug/info/warn/error via an internal EventEmitter
124
+ - Supports rich filtering (all, by event, by plugin, detailed rules)
125
+ - `SBMetrics` (metrics)
126
+ - Broadcasts metric operations (counters, gauges, histograms) and tracing (traces/spans) to all metrics plugins
127
+ - `SBEvents` (events)
128
+ - Loads events plugins and always includes `events-default` as a fallback
129
+ - Offers APIs for broadcast, fire-and-forget, request/response, and streaming
130
+ - `SBServices` (services)
131
+ - Loads service plugins from config, re-maps declared `init/run` before/after dependencies, and initializes/runs them in order
132
+
133
+ ### Plugin Resolution & Layout
134
+ `SBPlugins` looks for plugins in the following order (container usage prefers the first external option):
135
+ - Local project (dev): `src/plugins/<type>-<name>/index.ts`
136
+ - Local build: `lib/plugins/<type>-<name>/index.js`
137
+ - External plugin directory (`BSB_PLUGIN_DIR`) [preferred in container]: `<dir>/<npmPackage>/<version or latest>/lib/plugins/<type>-<name>/index.js`
138
+ - Node modules: `node_modules/<npmPackage>/lib/plugins/<type>-<name>/index.js`
139
+
140
+ Each plugin folder must export at least a `Plugin` class. Optionally export a `Config` class that extends `BSBPluginConfig` to provide validation and structured config.
141
+
142
+ Built‑in plugin types include: `config-*`, `logging-*`, `metrics-*`, `events-*`, `service-*`.
143
+
144
+ ### Development vs Production
145
+ - Container runtime (production): The container runs `lib/cli.js` (bin: `bsb`) and is the supported production path.
146
+ - Runs `new ServiceBase(false, true, CWD)` (legacy signature → optimized for production) inside the container entrypoint.
147
+ - Development runner: `src/dev.ts`
148
+ - Runs `new ServiceBase(true, false, CWD)` with file watching
149
+ - Creates `.bsbdevwatch` on first run; supports include/exclude patterns
150
+ - Interactive controls:
151
+ - `Ctrl+R` or typing `rs` to restart
152
+ - `Ctrl+C`/`Ctrl+D` to dispose and exit
153
+
154
+ ### NPM Scripts
155
+ - `npm run dev`: Start development runner with hot-reload
156
+ - `npm start`: Run production CLI (`lib/cli.js` or `bsb`)
157
+ - `npm run tsc`: Clean and compile TypeScript to `lib/`
158
+ - `npm run build`: Clean → tsc → tests → generate docs → export schemas → generate plugin metadata
159
+ - `npm run build-release`: Compile using `tsconfig-release.json`
160
+ - `npm run lint`: ESLint over `src/`
161
+ - `npm test`: Mocha + NYC coverage in TS mode
162
+ - `npm run testDev`: Run tests without coverage (faster for development)
163
+ - `npm run generate-docs`: Generate TypeDoc JSON to `docs.json`
164
+ - `npm run docs`: Build static docs to `../docs/dist/languages/nodejs/types`
165
+ - `npm run export-schemas`: Export event schemas to `lib/schemas/{plugin-name}.json`
166
+ - `npm run generate-plugin-json`: Generate plugin metadata in `lib/schemas/`
167
+
168
+ ### Docker
169
+ Multi‑stage build produces a minimal runtime image:
170
+ - `ENV BSB_LIVE=true`, `ENV BSB_CONTAINER=true`, `ENV BSB_PLUGIN_DIR=/mnt/plugins`
171
+ - Volumes: `/mnt/plugins` (external plugins), `/mnt/temp`
172
+ - Entrypoint runs `node lib/cli.js` as an unprivileged `node` user
173
+ - Optional plugin install/update at startup:
174
+ - `BSB_PLUGINS="@scope/plugin-a:1.2.3,@scope/plugin-b"` → installs/updates listed packages
175
+ - `BSB_PLUGIN_UPDATE=yes` → runs `npm update`
176
+
177
+ Example run (with mounted plugins directory):
178
+ ```bash
179
+ docker run --rm \
180
+ -e BSB_PLUGINS="@bettercorp/your-plugin" \
181
+ -v $(pwd)/plugins:/mnt/plugins \
182
+ bettercorp/bsb-node:latest
183
+ ```
184
+
185
+ Recommended plugin directory layout (when using `BSB_PLUGIN_DIR`):
186
+ ```
187
+ /mnt/plugins/
188
+ @org/plugin-a/
189
+ latest/
190
+ package.json # version field used when resolving
191
+ lib/plugins/service-plugin-a/index.js
192
+ lib/plugins/logging-xyz/index.js
193
+ 1.2.3/
194
+ package.json
195
+ lib/plugins/service-plugin-a/index.js
196
+ @org/plugin-b/
197
+ latest/
198
+ package.json
199
+ lib/plugins/events-abc/index.js
200
+ ```
201
+
202
+ Notes
203
+ - In container deployments, prefer placing prebuilt plugins under `BSB_PLUGIN_DIR` as above. This avoids network installs on boot and ensures deterministic versions via versioned folders or `latest`.
204
+ - `BSB_PLUGINS` is available for dynamic `npm install` at startup, but mounting a curated plugin repository via `BSB_PLUGIN_DIR` is recommended for production.
205
+
206
+ ### Environment Variables
207
+ - `APP_DIR`: Override working directory (mainly used in local development/testing)
208
+ - `BSB_PLUGIN_DIR`: External plugin repository root (e.g., container volume `/mnt/plugins`)
209
+ - `BSB_PLUGINS`: Comma‑separated list of npm packages to install at container start (entrypoint.js)
210
+ - `BSB_PLUGIN_UPDATE`: `yes|y|true` to run `npm update` at container start
211
+ - Config plugin override (advanced):
212
+ - `BSB_LOGGER_PLUGIN`: Name of config plugin (must start with `config-`)
213
+ - `BSB_LOGGER_PLUGIN_PACKAGE`: npm package name hosting the config plugin
214
+
215
+ ### Documentation
216
+
217
+ #### Plugin Development (v9)
218
+ - [Plugin Development Guide](./PLUGIN_DEVELOPMENT.md) - Complete guide for creating BSB plugins
219
+ - [Type System Guide](./TYPE_SYSTEM.md) - Cross-language type system reference
220
+
221
+ #### API Documentation
222
+ - API docs are generated with TypeDoc (`typedoc.json`).
223
+ - `npm run generate-docs` → emits `docs.json`
224
+ - `npm run docs` → builds static site under `../docs/dist/languages/nodejs/types`
225
+
226
+ ### Testing
227
+ - Tests: Mocha + ts-node with NYC coverage
228
+ - `npm test` → CI‑style JSON + lcov reports (`coverage/`)
229
+ - `npm run testDev` → dev‑friendly TS execution
230
+
231
+ ### Creating Plugins
232
+
233
+ **For v9 plugin development, see the [Plugin Development Guide](./PLUGIN_DEVELOPMENT.md) for complete examples and best practices.**
234
+
235
+ Quick reference:
236
+ - Use `createEventSchemas()` to define typed events with compile-time validation
237
+ - Use `createConfigSchema()` to define plugin configuration with metadata
238
+ - Use cross-language type helpers (`uuid`, `int32`, `datetime`, etc.) for better code generation
239
+ - Plugin metadata auto-generates `PLUGIN_CLIENT` and schema files during build
240
+
241
+ Legacy templates are available under `templates/` but use outdated v8 patterns:
242
+ - `templates/plugin.ts`, `templates/pluginClient.ts` (service plugin + client)
243
+ - `templates/events.ts` (events plugin)
244
+ - `templates/logger.ts` (logging plugin)
245
+
246
+ At minimum, export a `Plugin` class in `lib/plugins/<type>-<name>/index.js` (or `src/plugins/.../index.ts` in dev). For configurable plugins, export a `Config` created with `createConfigSchema()`. Publish your plugin as an npm package or ship its prebuilt folder structure under `BSB_PLUGIN_DIR`.
247
+
248
+ ### Quick Start (Container)
249
+ ```bash
250
+ # Provide prebuilt plugins under ./plugins, matching the recommended layout
251
+ docker run --rm \
252
+ -v $(pwd)/plugins:/mnt/plugins:ro \
253
+ -e BSB_PLUGIN_DIR=/mnt/plugins \
254
+ bettercorp/bsb-node:latest
255
+ ```
256
+
257
+ Local development (for contributors only):
258
+ ```bash
259
+ npm install
260
+ npm run dev
261
+ ```
262
+
263
+
@@ -0,0 +1,62 @@
1
+ {
2
+ "nodejs": [
3
+ {
4
+ "id": "config-default",
5
+ "name": "config-default",
6
+ "basePath": "./",
7
+ "description": "Default configuration plugin for profile and plugin resolution",
8
+ "tags": [
9
+ "core",
10
+ "config",
11
+ "default"
12
+ ],
13
+ "documentation": [
14
+ "./docs/core-plugins/config-default.md"
15
+ ],
16
+ "pluginPath": "src/plugins/config-default/",
17
+ "image": "../docs/public/assets/images/bsb-logo.png",
18
+ "links": {
19
+ "github": "git+https://github.com/BetterCorp/better-service-base.git"
20
+ }
21
+ },
22
+ {
23
+ "id": "events-default",
24
+ "name": "events-default",
25
+ "basePath": "./",
26
+ "description": "Default in-process events plugin for BSB event routing",
27
+ "tags": [
28
+ "core",
29
+ "events",
30
+ "default"
31
+ ],
32
+ "documentation": [
33
+ "./docs/core-plugins/events-default.md"
34
+ ],
35
+ "pluginPath": "src/plugins/events-default/",
36
+ "image": "../docs/public/assets/images/bsb-logo.png",
37
+ "links": {
38
+ "github": "git+https://github.com/BetterCorp/better-service-base.git"
39
+ }
40
+ },
41
+ {
42
+ "id": "observable-default",
43
+ "name": "observable-default",
44
+ "basePath": "./",
45
+ "description": "Default console observable plugin for logging output",
46
+ "tags": [
47
+ "core",
48
+ "observable",
49
+ "default",
50
+ "console"
51
+ ],
52
+ "documentation": [
53
+ "./docs/core-plugins/observable-default.md"
54
+ ],
55
+ "pluginPath": "src/plugins/observable-default/",
56
+ "image": "../docs/public/assets/images/bsb-logo.png",
57
+ "links": {
58
+ "github": "git+https://github.com/BetterCorp/better-service-base.git"
59
+ }
60
+ }
61
+ ]
62
+ }
@@ -0,0 +1,130 @@
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, EventsConfig, ObservableConfig, PluginDefinition, PluginType } from "../interfaces";
28
+ import { BaseWithObservable, BaseWithObservableConfig } from "./base";
29
+ import { BSBReferencePluginConfigDefinition, BSBReferencePluginConfigType } from "./PluginConfig";
30
+ /**
31
+ * @hidden
32
+ */
33
+ type BSBConfigResolvedConfig<ReferencedConfig extends BSBReferencePluginConfigType> = ReferencedConfig extends null ? null : BSBReferencePluginConfigDefinition<ReferencedConfig>;
34
+ type BSBConfigPropertyTypeSafe<T> = [T] extends [never] ? never : T extends undefined | null ? never : T;
35
+ type BSBConfigConstructorTypeSafe<T> = [T] extends [never] ? undefined : T extends undefined | null ? undefined : T;
36
+ /**
37
+ * @hidden
38
+ */
39
+ export interface BSBConfigConstructor<ReferencedConfig extends BSBReferencePluginConfigType = any> extends BaseWithObservableConfig {
40
+ config: BSBConfigConstructorTypeSafe<BSBConfigResolvedConfig<ReferencedConfig>>;
41
+ }
42
+ /**
43
+ * @group Config
44
+ * @category Plugins
45
+ * @template T - The type of config for the plugin
46
+ * Abstract class representing the configuration for the Better Service Base.
47
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/BSBConfig.html | API: BSBConfig}
48
+ */
49
+ export declare abstract class BSBConfig<ReferencedConfig extends BSBReferencePluginConfigType = any> extends BaseWithObservable {
50
+ readonly config: BSBConfigPropertyTypeSafe<BSBConfigResolvedConfig<ReferencedConfig>>;
51
+ constructor(config: BSBConfigConstructor<ReferencedConfig>);
52
+ /**
53
+ * Run lifecycle method for configuration plugins.
54
+ *
55
+ * This method is inherited from the base plugin class but is not used by configuration plugins.
56
+ * Configuration plugins are initialized during the init phase and provide configuration data
57
+ * to other plugins. They do not require a separate run phase.
58
+ *
59
+ * @remarks
60
+ * Configuration plugins are typically disposed after all other plugins have been initialized
61
+ * to free up memory, as configuration data is cached by individual plugins. Therefore, this
62
+ * method intentionally performs no operation.
63
+ *
64
+ * @returns void
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * // Configuration plugins do not need to implement run()
69
+ * // The base class provides this no-op implementation
70
+ * export class MyConfigPlugin extends BSBConfig {
71
+ * // No run() override needed
72
+ * }
73
+ * ```
74
+ *
75
+ * @see {@link BSBConfig.init} for the initialization lifecycle method
76
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/BSBConfig.html#run | API: BSBConfig#run}
77
+ */
78
+ run(): void;
79
+ /**
80
+ * Returns the observable plugins configuration (unified logging, metrics, tracing).
81
+ * @returns Promise resolving to an object containing the observable configuration for each plugin.
82
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/BSBConfig.html#getObservablePlugins | API: BSBConfig#getObservablePlugins}
83
+ */
84
+ abstract getObservablePlugins(obs: Observable): Promise<Record<string, ObservableConfig>>;
85
+ /**
86
+ * Returns the events plugins configuration.
87
+ * @returns Promise resolving to an object containing the events configuration for each plugin.
88
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/BSBConfig.html#getEventsPlugins | API: BSBConfig#getEventsPlugins}
89
+ */
90
+ abstract getEventsPlugins(obs: Observable): Promise<Record<string, EventsConfig>>;
91
+ /**
92
+ * Returns the service plugins configuration.
93
+ * @returns Promise resolving to an object containing the configuration for each plugin.
94
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/BSBConfig.html#getServicePlugins | API: BSBConfig#getServicePlugins}
95
+ */
96
+ abstract getServicePlugins(obs: Observable): Promise<Record<string, PluginDefinition>>;
97
+ /**
98
+ * Returns a mapped plugin name and whether the plugin is enabled or not
99
+ * @returns string of the plugin name and if it is enabled or not
100
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/BSBConfig.html#getServicePluginDefinition | API: BSBConfig#getServicePluginDefinition}
101
+ */
102
+ abstract getServicePluginDefinition(obs: Observable, pluginName: string): Promise<{
103
+ name: string;
104
+ enabled: boolean;
105
+ }>;
106
+ /**
107
+ * Returns the configuration for a specific plugin.
108
+ * @template T - The type of the configuration object.
109
+ * @param plugin - The name of the plugin to retrieve the configuration for.
110
+ * @returns Promise resolving to the configuration object for the specified plugin, or null if the plugin is not found.
111
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/BSBConfig.html#getPluginConfig | API: BSBConfig#getPluginConfig}
112
+ */
113
+ abstract getPluginConfig(obs: Observable, pluginType: PluginType, plugin: string): Promise<object | null>;
114
+ }
115
+ /**
116
+ * @hidden
117
+ */
118
+ export declare class BSBConfigRef extends BSBConfig<null> {
119
+ getObservablePlugins(obs: Observable): Promise<Record<string, ObservableConfig>>;
120
+ getEventsPlugins(obs: Observable): Promise<Record<string, EventsConfig>>;
121
+ getServicePlugins(obs: Observable): Promise<Record<string, PluginDefinition>>;
122
+ getPluginConfig(obs: Observable, pluginType: PluginType, plugin: string): Promise<object | null>;
123
+ getServicePluginDefinition(obs: Observable, pluginName: string): Promise<{
124
+ name: string;
125
+ enabled: boolean;
126
+ }>;
127
+ dispose?(): void;
128
+ init?(obs: Observable): void;
129
+ }
130
+ export {};
@@ -0,0 +1,95 @@
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.BSBConfigRef = exports.BSBConfig = void 0;
30
+ const base_1 = require("./base");
31
+ const errorMessages_1 = require("./errorMessages");
32
+ /**
33
+ * @group Config
34
+ * @category Plugins
35
+ * @template T - The type of config for the plugin
36
+ * Abstract class representing the configuration for the Better Service Base.
37
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/BSBConfig.html | API: BSBConfig}
38
+ */
39
+ class BSBConfig extends base_1.BaseWithObservable {
40
+ config;
41
+ constructor(config) {
42
+ super(config);
43
+ this.config = config.config;
44
+ }
45
+ /**
46
+ * Run lifecycle method for configuration plugins.
47
+ *
48
+ * This method is inherited from the base plugin class but is not used by configuration plugins.
49
+ * Configuration plugins are initialized during the init phase and provide configuration data
50
+ * to other plugins. They do not require a separate run phase.
51
+ *
52
+ * @remarks
53
+ * Configuration plugins are typically disposed after all other plugins have been initialized
54
+ * to free up memory, as configuration data is cached by individual plugins. Therefore, this
55
+ * method intentionally performs no operation.
56
+ *
57
+ * @returns void
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * // Configuration plugins do not need to implement run()
62
+ * // The base class provides this no-op implementation
63
+ * export class MyConfigPlugin extends BSBConfig {
64
+ * // No run() override needed
65
+ * }
66
+ * ```
67
+ *
68
+ * @see {@link BSBConfig.init} for the initialization lifecycle method
69
+ * @see {@link https://bsbcode.dev/languages/nodejs/types/classes/BSBConfig.html#run | API: BSBConfig#run}
70
+ */
71
+ run() { }
72
+ }
73
+ exports.BSBConfig = BSBConfig;
74
+ /**
75
+ * @hidden
76
+ */
77
+ class BSBConfigRef extends BSBConfig {
78
+ getObservablePlugins(obs) {
79
+ throw (0, errorMessages_1.BSB_ERROR_METHOD_NOT_IMPLEMENTED)("BSBConfigRef", "getObservablePlugins");
80
+ }
81
+ getEventsPlugins(obs) {
82
+ throw (0, errorMessages_1.BSB_ERROR_METHOD_NOT_IMPLEMENTED)("BSBConfigRef", "getEventsPlugins");
83
+ }
84
+ getServicePlugins(obs) {
85
+ throw (0, errorMessages_1.BSB_ERROR_METHOD_NOT_IMPLEMENTED)("BSBConfigRef", "getServicePlugins");
86
+ }
87
+ getPluginConfig(obs, pluginType, plugin) {
88
+ throw (0, errorMessages_1.BSB_ERROR_METHOD_NOT_IMPLEMENTED)("BSBConfigRef", "getPluginConfig");
89
+ }
90
+ getServicePluginDefinition(obs, pluginName) {
91
+ throw (0, errorMessages_1.BSB_ERROR_METHOD_NOT_IMPLEMENTED)("BSBConfigRef", "getServicePluginName");
92
+ }
93
+ }
94
+ exports.BSBConfigRef = BSBConfigRef;
95
+ //# sourceMappingURL=BSBConfig.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BSBConfig.js","sourceRoot":"","sources":["../../src/base/BSBConfig.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;;;AAIH,iCAAsE;AACtE,mDAAmE;AAkCnE;;;;;;GAMG;AACH,MAAsB,SAEpB,SAAQ,yBAAkB;IACV,MAAM,CAAuE;IAE7F,YAAY,MAA8C;QACxD,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAA8E,CAAC;IACtG,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACI,GAAG,KAAU,CAAC;CA6CtB;AAjFD,8BAiFC;AAED;;GAEG;AACH,MAAa,YACX,SAAQ,SAAe;IACvB,oBAAoB,CAAC,GAAe;QAClC,MAAM,IAAA,gDAAgC,EAAC,cAAc,EAAE,sBAAsB,CAAC,CAAC;IACjF,CAAC;IAED,gBAAgB,CAAC,GAAe;QAC9B,MAAM,IAAA,gDAAgC,EAAC,cAAc,EAAE,kBAAkB,CAAC,CAAC;IAC7E,CAAC;IAED,iBAAiB,CAAC,GAAe;QAC/B,MAAM,IAAA,gDAAgC,EAAC,cAAc,EAAE,mBAAmB,CAAC,CAAC;IAC9E,CAAC;IAED,eAAe,CACb,GAAe,EACf,UAAsB,EACtB,MAAc;QAEd,MAAM,IAAA,gDAAgC,EAAC,cAAc,EAAE,iBAAiB,CAAC,CAAC;IAC5E,CAAC;IAED,0BAA0B,CACxB,GAAe,EACf,UAAkB;QAElB,MAAM,IAAA,gDAAgC,EACpC,cAAc,EACd,sBAAsB,CACvB,CAAC;IACJ,CAAC;CAKF;AAnCD,oCAmCC"}