@moshaver/cmb-kernel 0.0.0-stage → 0.1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mobin Karam
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,18 @@
1
- # Temporary Holding Version
1
+ # @moshaver/cmb-kernel
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Small runtime-neutral CMB foundation primitives. This package defines module descriptors and stable tokens only; it has no Moshaver product or framework dependency.
4
+
5
+ ## Install and use
6
+
7
+ ```bash
8
+ npm install @moshaver/cmb-kernel
9
+ ```
10
+
11
+ ```js
12
+ const { CmbModuleRegistry, defineModule } = require("@moshaver/cmb-kernel");
13
+ const modules = new CmbModuleRegistry([
14
+ defineModule({ id: "notes", version: "0.1.0", kind: "product" }),
15
+ ]);
16
+ ```
17
+
18
+ Import only from the package root; do not deep-import implementation files.
package/package.json CHANGED
@@ -1,6 +1,45 @@
1
1
  {
2
2
  "name": "@moshaver/cmb-kernel",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Composable Modular Backend kernel primitives.",
5
+ "type": "commonjs",
6
+ "main": "./src/index.js",
7
+ "types": "./src/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./src/index.d.ts",
11
+ "require": "./src/index.js",
12
+ "default": "./src/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "src",
17
+ "README.md",
18
+ "LICENSE"
19
+ ],
20
+ "keywords": [
21
+ "cmb",
22
+ "modular-backend",
23
+ "backend",
24
+ "framework-agnostic",
25
+ "kernel"
26
+ ],
27
+ "engines": {
28
+ "node": ">=22.13.0 <23"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git+https://github.com/Mobin-Karam/moshaver.git",
33
+ "directory": "packages/cmb/kernel"
34
+ },
35
+ "bugs": {
36
+ "url": "https://github.com/Mobin-Karam/moshaver/issues"
37
+ },
38
+ "homepage": "https://github.com/Mobin-Karam/moshaver/tree/main/packages/cmb",
39
+ "publishConfig": {
40
+ "access": "public"
41
+ },
42
+ "license": "MIT",
43
+ "sideEffects": false,
44
+ "dependencies": {}
45
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,54 @@
1
+ export const CMB_MODULE_KINDS: readonly ["kernel", "foundation", "platform", "adapter", "product"];
2
+ export type CmbModuleKind = (typeof CMB_MODULE_KINDS)[number];
3
+
4
+ export type CmbModuleDescriptor = Readonly<{
5
+ id: string;
6
+ version: string;
7
+ kind: CmbModuleKind;
8
+ dependencies: readonly string[];
9
+ optionalDependencies?: readonly string[];
10
+ provides?: readonly string[];
11
+ requires?: readonly string[];
12
+ routes?: readonly string[];
13
+ migrations?: readonly string[];
14
+ health?: readonly string[];
15
+ permissions?: readonly string[];
16
+ events?: Readonly<{ publishes: readonly string[]; subscribes: readonly string[] }>;
17
+ configuration?: Readonly<Record<string, unknown>>;
18
+ }>;
19
+
20
+ export type CmbModuleRegistration<TContext = unknown> = Readonly<{
21
+ descriptor: CmbModuleDescriptor;
22
+ start?: (context: TContext) => void | Promise<void>;
23
+ stop?: (context: TContext) => void | Promise<void>;
24
+ }>;
25
+
26
+ export const KERNEL_MODULE: CmbModuleDescriptor;
27
+
28
+ export class CmbModuleRegistry<TContext = unknown> {
29
+ constructor(registrations?: Iterable<CmbModuleDescriptor | CmbModuleRegistration<TContext>>);
30
+ register(registration: CmbModuleDescriptor | CmbModuleRegistration<TContext>): this;
31
+ resolve(enabledIds?: Iterable<string>): readonly CmbModuleRegistration<TContext>[];
32
+ metadata(enabledIds?: Iterable<string>): readonly CmbModuleDescriptor[];
33
+ start(context?: TContext, enabledIds?: Iterable<string>): Promise<readonly CmbModuleDescriptor[]>;
34
+ stop(context?: TContext): Promise<void>;
35
+ }
36
+
37
+ export function defineModule(input: {
38
+ id: string;
39
+ version: string;
40
+ kind: CmbModuleKind;
41
+ dependencies?: Iterable<string>;
42
+ optionalDependencies?: Iterable<string>;
43
+ provides?: Iterable<string>;
44
+ requires?: Iterable<string>;
45
+ routes?: Iterable<string>;
46
+ migrations?: Iterable<string>;
47
+ health?: Iterable<string>;
48
+ permissions?: Iterable<string>;
49
+ publishes?: Iterable<string>;
50
+ subscribes?: Iterable<string>;
51
+ configuration?: Record<string, unknown>;
52
+ }): CmbModuleDescriptor;
53
+
54
+ export function createToken(scope: string, name: string): symbol;
package/src/index.js ADDED
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+
3
+ const CMB_MODULE_KINDS = Object.freeze(["kernel", "foundation", "platform", "adapter", "product"]);
4
+ const MODULE_ID_PATTERN = /^[a-z][a-z0-9]*(?:[-.][a-z0-9]+)*$/;
5
+ const VERSION_PATTERN = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/;
6
+
7
+ const EMPTY = Object.freeze([]);
8
+ const KERNEL_MODULE = Object.freeze({ id: "kernel", version: "0.1.0", kind: "kernel", dependencies: EMPTY, optionalDependencies: EMPTY, provides: EMPTY, requires: EMPTY, routes: EMPTY, migrations: EMPTY, health: EMPTY, permissions: EMPTY, events: Object.freeze({ publishes: EMPTY, subscribes: EMPTY }), configuration: Object.freeze({}) });
9
+
10
+ function defineModule(input) {
11
+ if (!input || typeof input !== "object") throw new TypeError("CMB module descriptor must be an object.");
12
+ const id = String(input.id ?? "").trim();
13
+ const version = String(input.version ?? "").trim();
14
+ const kind = String(input.kind ?? "").trim();
15
+ const dependencies = [...new Set(Array.from(input.dependencies ?? [], (value) => String(value).trim()).filter(Boolean))];
16
+ const optionalDependencies = [...new Set(Array.from(input.optionalDependencies ?? [], (value) => String(value).trim()).filter(Boolean))];
17
+ const list = (name) => Object.freeze([...new Set(Array.from(input[name] ?? [], (value) => String(value).trim()).filter(Boolean))]);
18
+
19
+ if (!MODULE_ID_PATTERN.test(id)) throw new TypeError(`Invalid CMB module id: ${id || "<empty>"}`);
20
+ if (!VERSION_PATTERN.test(version)) throw new TypeError(`Invalid CMB module version: ${version || "<empty>"}`);
21
+ if (!CMB_MODULE_KINDS.includes(kind)) throw new TypeError(`Invalid CMB module kind: ${kind || "<empty>"}`);
22
+ if (dependencies.includes(id)) throw new TypeError(`CMB module ${id} cannot depend on itself.`);
23
+ if (optionalDependencies.includes(id)) throw new TypeError(`CMB module ${id} cannot optionally depend on itself.`);
24
+ for (const dependency of [...dependencies, ...optionalDependencies]) {
25
+ if (!MODULE_ID_PATTERN.test(dependency)) throw new TypeError(`Invalid CMB dependency id: ${dependency}`);
26
+ }
27
+
28
+ return Object.freeze({
29
+ id,
30
+ version,
31
+ kind,
32
+ dependencies: Object.freeze(dependencies),
33
+ optionalDependencies: Object.freeze(optionalDependencies),
34
+ provides: list("provides"),
35
+ requires: list("requires"),
36
+ routes: list("routes"),
37
+ migrations: list("migrations"),
38
+ health: list("health"),
39
+ permissions: list("permissions"),
40
+ events: Object.freeze({ publishes: list("publishes"), subscribes: list("subscribes") }),
41
+ configuration: Object.freeze({ ...(input.configuration ?? {}) }),
42
+ });
43
+ }
44
+
45
+ function createToken(scope, name) {
46
+ const normalizedScope = String(scope ?? "").trim();
47
+ const normalizedName = String(name ?? "").trim();
48
+ if (!MODULE_ID_PATTERN.test(normalizedScope)) throw new TypeError("CMB token scope must be a valid module id.");
49
+ if (!normalizedName) throw new TypeError("CMB token name is required.");
50
+ return Symbol.for(`cmb:${normalizedScope}:${normalizedName}`);
51
+ }
52
+
53
+ class CmbModuleRegistry {
54
+ constructor(registrations = []) {
55
+ this.registrations = new Map([[KERNEL_MODULE.id, { descriptor: KERNEL_MODULE }]]);
56
+ this.started = [];
57
+ for (const registration of registrations) this.register(registration);
58
+ }
59
+
60
+ register(registration) {
61
+ const value = registration?.descriptor ? registration : { descriptor: registration };
62
+ const descriptor = defineModule(value.descriptor);
63
+ if (descriptor.id === KERNEL_MODULE.id || this.registrations.has(descriptor.id)) throw new Error(`Duplicate CMB module: ${descriptor.id}`);
64
+ for (const hook of ["start", "stop"]) if (value[hook] !== undefined && typeof value[hook] !== "function") throw new TypeError(`CMB module ${descriptor.id} ${hook} hook must be a function.`);
65
+ this.registrations.set(descriptor.id, Object.freeze({ descriptor, start: value.start, stop: value.stop }));
66
+ return this;
67
+ }
68
+
69
+ resolve(enabledIds) {
70
+ const enabled = enabledIds === undefined ? new Set(this.registrations.keys()) : new Set(["kernel", ...enabledIds]);
71
+ for (const id of enabled) if (!this.registrations.has(id)) throw new Error(`Unknown enabled CMB module: ${id}`);
72
+ const visiting = new Set();
73
+ const visited = new Set();
74
+ const ordered = [];
75
+ const visit = (id, path = []) => {
76
+ if (visited.has(id)) return;
77
+ if (visiting.has(id)) throw new Error(`Circular CMB module dependency: ${[...path, id].join(" -> ")}`);
78
+ visiting.add(id);
79
+ const registration = this.registrations.get(id);
80
+ const allowed = {
81
+ kernel: new Set(),
82
+ foundation: new Set(["kernel", "foundation"]),
83
+ platform: new Set(["kernel", "foundation", "platform"]),
84
+ adapter: new Set(["kernel", "foundation", "platform", "adapter"]),
85
+ product: new Set(CMB_MODULE_KINDS),
86
+ };
87
+ for (const dependency of registration.descriptor.dependencies) {
88
+ if (!this.registrations.has(dependency)) throw new Error(`CMB module ${id} requires missing dependency: ${dependency}`);
89
+ if (!enabled.has(dependency)) throw new Error(`CMB module ${id} requires disabled dependency: ${dependency}`);
90
+ const dependencyKind = this.registrations.get(dependency).descriptor.kind;
91
+ if (!allowed[registration.descriptor.kind].has(dependencyKind)) throw new Error(`Forbidden CMB dependency: ${registration.descriptor.kind} module ${id} cannot depend on ${dependencyKind} module ${dependency}`);
92
+ visit(dependency, [...path, id]);
93
+ }
94
+ visiting.delete(id);
95
+ visited.add(id);
96
+ ordered.push(registration);
97
+ };
98
+ for (const id of enabled) visit(id);
99
+ const provided = new Set(ordered.flatMap(({ descriptor }) => descriptor.provides));
100
+ for (const { descriptor } of ordered) for (const required of descriptor.requires) if (!provided.has(required)) throw new Error(`CMB module ${descriptor.id} requires missing service: ${required}`);
101
+ return Object.freeze(ordered);
102
+ }
103
+
104
+ metadata(enabledIds) {
105
+ return Object.freeze(this.resolve(enabledIds).map(({ descriptor }) => descriptor));
106
+ }
107
+
108
+ async start(context = {}, enabledIds) {
109
+ if (this.started.length) throw new Error("CMB module registry is already started.");
110
+ try {
111
+ for (const registration of this.resolve(enabledIds)) {
112
+ await registration.start?.(context);
113
+ this.started.push(registration);
114
+ }
115
+ } catch (error) {
116
+ await this.stop(context);
117
+ throw error;
118
+ }
119
+ return this.metadata(enabledIds);
120
+ }
121
+
122
+ async stop(context = {}) {
123
+ for (const registration of [...this.started].reverse()) await registration.stop?.(context);
124
+ this.started = [];
125
+ }
126
+ }
127
+
128
+ module.exports = {
129
+ CMB_MODULE_KINDS,
130
+ KERNEL_MODULE,
131
+ CmbModuleRegistry,
132
+ defineModule,
133
+ createToken,
134
+ };