@cldmv/slothlet 2.9.0 → 2.10.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 (43) hide show
  1. package/README.md +68 -0
  2. package/dist/lib/engine/slothlet_child.mjs +1 -1
  3. package/dist/lib/engine/slothlet_engine.mjs +1 -1
  4. package/dist/lib/engine/slothlet_esm.mjs +1 -1
  5. package/dist/lib/engine/slothlet_helpers.mjs +1 -1
  6. package/dist/lib/engine/slothlet_worker.mjs +1 -1
  7. package/dist/lib/helpers/als-eventemitter.mjs +1 -1
  8. package/dist/lib/helpers/api_builder/add_api.mjs +58 -3
  9. package/dist/lib/helpers/api_builder/analysis.mjs +13 -3
  10. package/dist/lib/helpers/api_builder/construction.mjs +1 -1
  11. package/dist/lib/helpers/api_builder/decisions.mjs +1 -1
  12. package/dist/lib/helpers/api_builder/metadata.mjs +248 -0
  13. package/dist/lib/helpers/api_builder.mjs +1 -1
  14. package/dist/lib/helpers/auto-wrap.mjs +1 -1
  15. package/dist/lib/helpers/hooks.mjs +1 -1
  16. package/dist/lib/helpers/instance-manager.mjs +1 -1
  17. package/dist/lib/helpers/metadata-api.mjs +201 -0
  18. package/dist/lib/helpers/multidefault.mjs +12 -3
  19. package/dist/lib/helpers/resolve-from-caller.mjs +1 -1
  20. package/dist/lib/helpers/sanitize.mjs +1 -1
  21. package/dist/lib/helpers/utilities.mjs +1 -1
  22. package/dist/lib/modes/slothlet_eager.mjs +1 -1
  23. package/dist/lib/modes/slothlet_lazy.mjs +10 -1
  24. package/dist/lib/runtime/runtime-asynclocalstorage.mjs +5 -1
  25. package/dist/lib/runtime/runtime-livebindings.mjs +5 -1
  26. package/dist/lib/runtime/runtime.mjs +12 -1
  27. package/dist/slothlet.mjs +19 -3
  28. package/package.json +1 -1
  29. package/types/dist/lib/helpers/api_builder/add_api.d.mts +17 -1
  30. package/types/dist/lib/helpers/api_builder/add_api.d.mts.map +1 -1
  31. package/types/dist/lib/helpers/api_builder/analysis.d.mts.map +1 -1
  32. package/types/dist/lib/helpers/api_builder/metadata.d.mts +99 -0
  33. package/types/dist/lib/helpers/api_builder/metadata.d.mts.map +1 -0
  34. package/types/dist/lib/helpers/metadata-api.d.mts +132 -0
  35. package/types/dist/lib/helpers/metadata-api.d.mts.map +1 -0
  36. package/types/dist/lib/helpers/multidefault.d.mts.map +1 -1
  37. package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts +2 -0
  38. package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts.map +1 -1
  39. package/types/dist/lib/runtime/runtime-livebindings.d.mts +2 -0
  40. package/types/dist/lib/runtime/runtime-livebindings.d.mts.map +1 -1
  41. package/types/dist/lib/runtime/runtime.d.mts +1 -0
  42. package/types/dist/lib/runtime/runtime.d.mts.map +1 -1
  43. package/types/dist/slothlet.d.mts.map +1 -1
package/README.md CHANGED
@@ -253,6 +253,73 @@ api.hooks.on(
253
253
  const result = await api.math.add(2, 3);
254
254
  ```
255
255
 
256
+ ### Dynamic API Extension with addApi()
257
+
258
+ Load additional modules at runtime and extend your API dynamically:
259
+
260
+ ```javascript
261
+ import slothlet from "@cldmv/slothlet";
262
+
263
+ const api = await slothlet({ dir: "./api" });
264
+
265
+ // Add plugins at runtime
266
+ await api.addApi("plugins", "./plugins-folder");
267
+ api.plugins.myPlugin();
268
+
269
+ // Create nested API structures
270
+ await api.addApi("runtime.plugins", "./more-plugins");
271
+ api.runtime.plugins.loader();
272
+
273
+ // Add with metadata for security/authorization
274
+ await api.addApi("plugins.trusted", "./trusted-plugins", {
275
+ trusted: true,
276
+ permissions: ["read", "write", "admin"],
277
+ version: "1.0.0"
278
+ });
279
+
280
+ await api.addApi("plugins.external", "./third-party", {
281
+ trusted: false,
282
+ permissions: ["read"]
283
+ });
284
+
285
+ // Access metadata on functions
286
+ const meta = api.plugins.trusted.someFunc.__metadata;
287
+ console.log(meta.trusted); // true
288
+ console.log(meta.permissions); // ["read", "write", "admin"]
289
+ ```
290
+
291
+ **Security & Authorization with metadataAPI:**
292
+
293
+ ```javascript
294
+ // Inside your modules, use metadataAPI for runtime introspection
295
+ import { metadataAPI } from "@cldmv/slothlet/runtime";
296
+
297
+ export async function sensitiveOperation() {
298
+ // Check caller's metadata
299
+ const caller = await metadataAPI.caller();
300
+
301
+ if (!caller?.trusted) {
302
+ throw new Error("Unauthorized: Caller is not trusted");
303
+ }
304
+
305
+ if (!caller.permissions.includes("admin")) {
306
+ throw new Error("Unauthorized: Admin permission required");
307
+ }
308
+
309
+ // Proceed with secure operation
310
+ return "Success";
311
+ }
312
+
313
+ // Get metadata by path
314
+ const meta = await metadataAPI.get("plugins.trusted.someFunc");
315
+
316
+ // Get current function's metadata
317
+ const self = await metadataAPI.self();
318
+ console.log("My version:", self.version);
319
+ ```
320
+
321
+ 🔒 **For complete metadata system documentation, see [docs/METADATA.md](https://github.com/CLDMV/slothlet/blob/master/docs/METADATA.md)**
322
+
256
323
  ---
257
324
 
258
325
  ## 📚 Configuration Options
@@ -481,6 +548,7 @@ Key highlights:
481
548
 
482
549
  - **[Hook System](https://github.com/CLDMV/slothlet/blob/master/docs/HOOKS.md)** - Complete hook system documentation with 4 hook types, pattern matching, and examples
483
550
  - **[Context Propagation](https://github.com/CLDMV/slothlet/blob/master/docs/CONTEXT-PROPAGATION.md)** - EventEmitter and class instance context preservation
551
+ - **[Metadata System](https://github.com/CLDMV/slothlet/blob/master/docs/METADATA.md)** - Function metadata tagging and runtime introspection for security, authorization, and auditing
484
552
  - **[Module Structure](https://github.com/CLDMV/slothlet/blob/master/docs/MODULE-STRUCTURE.md)** - Comprehensive module organization patterns and examples
485
553
  - **[API Flattening](https://github.com/CLDMV/slothlet/blob/master/docs/API-FLATTENING.md)** - The 5 flattening rules with decision tree and benefits
486
554
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2025 CLDMV/Shinrai
2
+ Copyright 2026 CLDMV/Shinrai
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2025 CLDMV/Shinrai
2
+ Copyright 2026 CLDMV/Shinrai
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2025 CLDMV/Shinrai
2
+ Copyright 2026 CLDMV/Shinrai
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2025 CLDMV/Shinrai
2
+ Copyright 2026 CLDMV/Shinrai
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2025 CLDMV/Shinrai
2
+ Copyright 2026 CLDMV/Shinrai
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2025 CLDMV/Shinrai
2
+ Copyright 2026 CLDMV/Shinrai
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2025 CLDMV/Shinrai
2
+ Copyright 2026 CLDMV/Shinrai
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -21,9 +21,10 @@
21
21
  import fs from "node:fs/promises";
22
22
  import path from "node:path";
23
23
  import { resolvePathFromCaller } from "@cldmv/slothlet/helpers/resolve-from-caller";
24
+ import { cleanMetadata, tagLoadedFunctions } from "./metadata.mjs";
24
25
 
25
26
 
26
- export async function addApiFromFolder({ apiPath, folderPath, instance }) {
27
+ export async function addApiFromFolder({ apiPath, folderPath, instance, metadata = {} }) {
27
28
  if (!instance.loaded) {
28
29
  throw new Error("[slothlet] Cannot add API: API not loaded. Call create() or load() first.");
29
30
  }
@@ -47,9 +48,21 @@ export async function addApiFromFolder({ apiPath, folderPath, instance }) {
47
48
  }
48
49
 
49
50
 
51
+
52
+
50
53
  let resolvedFolderPath = folderPath;
51
- if (!path.isAbsolute(folderPath)) {
54
+ const isAbsolute = path.isAbsolute(folderPath);
55
+
56
+ if (instance.config.debug) {
57
+ console.log(`[DEBUG] addApi: folderPath="${folderPath}"`);
58
+ console.log(`[DEBUG] addApi: isAbsolute=${isAbsolute}`);
59
+ }
60
+
61
+ if (!isAbsolute) {
52
62
  resolvedFolderPath = resolvePathFromCaller(folderPath);
63
+ if (instance.config.debug) {
64
+ console.log(`[DEBUG] addApi: Resolved relative path to: ${resolvedFolderPath}`);
65
+ }
53
66
  }
54
67
 
55
68
 
@@ -77,6 +90,29 @@ export async function addApiFromFolder({ apiPath, folderPath, instance }) {
77
90
  newModules = await instance.modes.eager.create.call(instance, resolvedFolderPath, instance.config.apiDepth || Infinity, 0);
78
91
  }
79
92
 
93
+
94
+
95
+
96
+ if (newModules && metadata && typeof metadata === "object" && Object.keys(metadata).length > 0) {
97
+
98
+ const fullMetadata = {
99
+ ...metadata,
100
+ sourceFolder: resolvedFolderPath
101
+ };
102
+
103
+ if (instance.config.debug) {
104
+ console.log(`[DEBUG] addApi: Tagging functions with metadata:`, Object.keys(fullMetadata));
105
+ }
106
+
107
+ tagLoadedFunctions(newModules, fullMetadata, resolvedFolderPath);
108
+ } else if (newModules) {
109
+
110
+ if (instance.config.debug) {
111
+ console.log(`[DEBUG] addApi: Cleaning metadata from functions (no metadata provided)`);
112
+ }
113
+ cleanMetadata(newModules);
114
+ }
115
+
80
116
  if (instance.config.debug) {
81
117
  if (newModules && typeof newModules === "object") {
82
118
  console.log(`[DEBUG] addApi: Loaded modules:`, Object.keys(newModules));
@@ -200,6 +236,25 @@ export async function addApiFromFolder({ apiPath, folderPath, instance }) {
200
236
  }
201
237
 
202
238
 
239
+
240
+
241
+ const targetValue = currentTarget[finalKey];
242
+ if (typeof targetValue === "function" && targetValue.__slothletPath) {
243
+
244
+
245
+ const _ = targetValue.__trigger;
246
+ await targetValue();
247
+
248
+ }
249
+
250
+ const boundTargetValue = currentBoundTarget[finalKey];
251
+ if (typeof boundTargetValue === "function" && boundTargetValue.__slothletPath) {
252
+
253
+ const _ = boundTargetValue.__trigger;
254
+ await boundTargetValue();
255
+ }
256
+
257
+
203
258
  if (!currentTarget[finalKey]) {
204
259
  currentTarget[finalKey] = {};
205
260
  }
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2025 CLDMV/Shinrai
2
+ Copyright 2026 CLDMV/Shinrai
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -55,14 +55,19 @@ export async function analyzeModule(modulePath, options = {}) {
55
55
  const moduleUrl = pathToFileURL(modulePath).href;
56
56
 
57
57
 
58
+
58
59
  let importUrl = moduleUrl;
60
+ const separator = moduleUrl.includes("?") ? "&" : "?";
61
+
62
+
63
+ importUrl = `${moduleUrl}${separator}_t=${Date.now()}_${Math.random().toString(36).slice(2)}`;
64
+
59
65
  if (instance && instance.instanceId) {
60
66
  const runtimeType = instance.config?.runtime || "async";
61
67
 
62
68
 
63
69
  if (runtimeType === "live") {
64
- const separator = moduleUrl.includes("?") ? "&" : "?";
65
- importUrl = `${moduleUrl}${separator}slothlet_instance=${instance.instanceId}`;
70
+ importUrl = `${importUrl}&slothlet_instance=${instance.instanceId}`;
66
71
  importUrl = `${importUrl}&slothlet_runtime=${runtimeType}`;
67
72
 
68
73
 
@@ -72,6 +77,11 @@ export async function analyzeModule(modulePath, options = {}) {
72
77
 
73
78
  }
74
79
 
80
+ if (debug) {
81
+ console.log(`[DEBUG] analyzeModule: Importing ${path.basename(modulePath)}`);
82
+ console.log(`[DEBUG] URL: ${importUrl}`);
83
+ }
84
+
75
85
  const rawModule = await import(importUrl);
76
86
 
77
87
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2025 CLDMV/Shinrai
2
+ Copyright 2026 CLDMV/Shinrai
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2025 CLDMV/Shinrai
2
+ Copyright 2026 CLDMV/Shinrai
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -0,0 +1,248 @@
1
+ /*
2
+ Copyright 2026 CLDMV/Shinrai
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+
18
+
19
+
20
+
21
+
22
+ export function createImmutableMetadata(initial = {}) {
23
+
24
+
25
+
26
+ function makeImmutable(obj, visited = new WeakSet()) {
27
+ if (obj === null || typeof obj !== "object") return obj;
28
+ if (visited.has(obj)) return obj;
29
+ visited.add(obj);
30
+
31
+
32
+ if (Array.isArray(obj)) {
33
+
34
+ for (let i = 0; i < obj.length; i++) {
35
+ obj[i] = makeImmutable(obj[i], visited);
36
+ }
37
+
38
+
39
+
40
+ return new Proxy(obj, {
41
+ set() {
42
+
43
+
44
+ return false;
45
+ },
46
+ deleteProperty() {
47
+
48
+ return false;
49
+ }
50
+ });
51
+ }
52
+
53
+
54
+ for (const [key, value] of Object.entries(obj)) {
55
+ const immutableValue = makeImmutable(value, visited);
56
+ Object.defineProperty(obj, key, {
57
+ value: immutableValue,
58
+ writable: false,
59
+ enumerable: true,
60
+ configurable: true
61
+ });
62
+ }
63
+
64
+
65
+ return new Proxy(obj, {
66
+ set(target, prop, value) {
67
+ const descriptor = Object.getOwnPropertyDescriptor(target, prop);
68
+
69
+
70
+ if (descriptor && !descriptor.writable) {
71
+ return false;
72
+ }
73
+
74
+
75
+ if (!descriptor) {
76
+ const immutableValue = makeImmutable(value);
77
+ Object.defineProperty(target, prop, {
78
+ value: immutableValue,
79
+ writable: false,
80
+ enumerable: true,
81
+ configurable: true
82
+ });
83
+ return true;
84
+ }
85
+
86
+ return Reflect.set(target, prop, value);
87
+ },
88
+ deleteProperty() {
89
+
90
+ return false;
91
+ }
92
+ });
93
+ }
94
+
95
+
96
+ const base = {};
97
+
98
+
99
+
100
+ for (const [key, value] of Object.entries(initial)) {
101
+ const immutableValue = makeImmutable(value);
102
+ Object.defineProperty(base, key, {
103
+ value: immutableValue,
104
+ writable: false,
105
+ enumerable: true,
106
+ configurable: true
107
+ });
108
+ }
109
+
110
+
111
+ return new Proxy(base, {
112
+ set(target, prop, value) {
113
+
114
+ const descriptor = Object.getOwnPropertyDescriptor(target, prop);
115
+
116
+
117
+ if (descriptor && !descriptor.writable) {
118
+ if (process.env.SLOTHLET_DEBUG) {
119
+ console.warn(`[slothlet] Cannot modify existing metadata property: "${String(prop)}"`);
120
+ }
121
+
122
+
123
+ return false;
124
+ }
125
+
126
+
127
+ if (!descriptor) {
128
+ Object.defineProperty(target, prop, {
129
+ value,
130
+ writable: false,
131
+ enumerable: true,
132
+ configurable: true
133
+ });
134
+ return true;
135
+ }
136
+
137
+
138
+
139
+ return Reflect.set(target, prop, value);
140
+ },
141
+
142
+ deleteProperty(target, prop) {
143
+
144
+ if (process.env.SLOTHLET_DEBUG) {
145
+ console.warn(`[slothlet] Cannot delete metadata property: "${String(prop)}"`);
146
+ }
147
+ return true;
148
+ }
149
+ });
150
+ }
151
+
152
+
153
+ export function cleanMetadata(obj, visited = new WeakSet()) {
154
+
155
+ if (!obj || (typeof obj !== "object" && typeof obj !== "function")) return;
156
+ if (visited.has(obj)) return;
157
+ visited.add(obj);
158
+
159
+
160
+ if (typeof obj === "function") {
161
+ try {
162
+ if (obj.__metadata) {
163
+ delete obj.__metadata;
164
+ }
165
+ if (obj.__sourceFolder) {
166
+ delete obj.__sourceFolder;
167
+ }
168
+ } catch (_) {
169
+
170
+ }
171
+ }
172
+
173
+
174
+ try {
175
+ const keys = Object.keys(obj);
176
+ for (const key of keys) {
177
+ if (
178
+ key.startsWith("_") ||
179
+ ["hooks", "shutdown", "addApi", "describe", "run", "__proto__", "constructor", "prototype"].includes(key)
180
+ ) {
181
+ continue;
182
+ }
183
+ cleanMetadata(obj[key], visited);
184
+ }
185
+ } catch {
186
+
187
+ }
188
+ }
189
+
190
+
191
+ export function tagLoadedFunctions(obj, metadata, baseDir, visited = new WeakSet()) {
192
+
193
+ if (!obj || (typeof obj !== "object" && typeof obj !== "function")) return;
194
+ if (visited.has(obj)) return;
195
+ visited.add(obj);
196
+
197
+
198
+ if (typeof obj === "function") {
199
+ try {
200
+
201
+ const immutableMeta = createImmutableMetadata(metadata);
202
+
203
+ Object.defineProperty(obj, "__metadata", {
204
+ value: immutableMeta,
205
+ writable: false,
206
+ enumerable: false,
207
+ configurable: true
208
+ });
209
+
210
+
211
+ if (metadata.sourceFolder) {
212
+ Object.defineProperty(obj, "__sourceFolder", {
213
+ value: metadata.sourceFolder,
214
+ writable: false,
215
+ enumerable: false,
216
+ configurable: true
217
+ });
218
+ }
219
+
220
+
221
+
222
+
223
+ } catch (error) {
224
+
225
+ if (metadata.debug) {
226
+ console.warn(`[slothlet] Could not tag function with metadata:`, error.message);
227
+ }
228
+ }
229
+ }
230
+
231
+
232
+ try {
233
+ const keys = Object.keys(obj);
234
+ for (const key of keys) {
235
+
236
+ if (
237
+ key.startsWith("_") ||
238
+ ["hooks", "shutdown", "addApi", "describe", "run", "__proto__", "constructor", "prototype"].includes(key)
239
+ ) {
240
+ continue;
241
+ }
242
+
243
+ tagLoadedFunctions(obj[key], metadata, baseDir, visited);
244
+ }
245
+ } catch {
246
+
247
+ }
248
+ }
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2025 CLDMV/Shinrai
2
+ Copyright 2026 CLDMV/Shinrai
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2025 CLDMV/Shinrai
2
+ Copyright 2026 CLDMV/Shinrai
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2025 CLDMV/Shinrai
2
+ Copyright 2026 CLDMV/Shinrai
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2025 CLDMV/Shinrai
2
+ Copyright 2026 CLDMV/Shinrai
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.