@cldmv/slothlet 1.0.3 → 2.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.
Files changed (48) hide show
  1. package/README.md +913 -73
  2. package/dist/lib/engine/README.md +21 -0
  3. package/dist/lib/engine/slothlet_child.mjs +58 -0
  4. package/dist/lib/engine/slothlet_engine.mjs +371 -0
  5. package/dist/lib/engine/slothlet_esm.mjs +229 -0
  6. package/dist/lib/engine/slothlet_helpers.mjs +454 -0
  7. package/dist/lib/engine/slothlet_worker.mjs +148 -0
  8. package/dist/lib/helpers/resolve-from-caller.mjs +141 -0
  9. package/dist/lib/helpers/sanitize.mjs +265 -0
  10. package/dist/lib/modes/slothlet_eager.mjs +80 -0
  11. package/dist/lib/modes/slothlet_lazy.mjs +342 -0
  12. package/dist/lib/runtime/runtime.mjs +249 -0
  13. package/dist/slothlet.mjs +1097 -0
  14. package/index.cjs +81 -0
  15. package/index.mjs +76 -0
  16. package/package.json +132 -20
  17. package/types/dist/lib/engine/slothlet_child.d.mts +2 -0
  18. package/types/dist/lib/engine/slothlet_child.d.mts.map +1 -0
  19. package/types/dist/lib/engine/slothlet_engine.d.mts +31 -0
  20. package/types/dist/lib/engine/slothlet_engine.d.mts.map +1 -0
  21. package/types/{src/lib → dist/lib/engine}/slothlet_esm.d.mts +1 -0
  22. package/types/dist/lib/engine/slothlet_esm.d.mts.map +1 -0
  23. package/types/{src/lib → dist/lib/engine}/slothlet_helpers.d.mts +2 -2
  24. package/types/dist/lib/engine/slothlet_helpers.d.mts.map +1 -0
  25. package/types/dist/lib/engine/slothlet_worker.d.mts +2 -0
  26. package/types/dist/lib/engine/slothlet_worker.d.mts.map +1 -0
  27. package/types/dist/lib/helpers/resolve-from-caller.d.mts +149 -0
  28. package/types/dist/lib/helpers/resolve-from-caller.d.mts.map +1 -0
  29. package/types/dist/lib/helpers/sanitize.d.mts +79 -0
  30. package/types/dist/lib/helpers/sanitize.d.mts.map +1 -0
  31. package/types/dist/lib/modes/slothlet_eager.d.mts +66 -0
  32. package/types/dist/lib/modes/slothlet_eager.d.mts.map +1 -0
  33. package/types/dist/lib/modes/slothlet_lazy.d.mts +32 -0
  34. package/types/dist/lib/modes/slothlet_lazy.d.mts.map +1 -0
  35. package/types/dist/lib/runtime/runtime.d.mts +49 -0
  36. package/types/dist/lib/runtime/runtime.d.mts.map +1 -0
  37. package/types/dist/slothlet.d.mts +124 -0
  38. package/types/dist/slothlet.d.mts.map +1 -0
  39. package/types/index.d.mts +23 -0
  40. package/slothlet.mjs +0 -1248
  41. package/types/debug-slothlet.d.mts +0 -1
  42. package/types/eslint.config.d.mts +0 -2
  43. package/types/jest.config.d.mts +0 -6
  44. package/types/slothlet.d.mts +0 -189
  45. package/types/src/lib/slothlet_child.d.mts +0 -1
  46. package/types/src/lib/slothlet_engine.d.mts +0 -6
  47. package/types/src/lib/slothlet_worker.d.mts +0 -1
  48. package/types/vitest.config.d.ts +0 -2
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sanitize.d.mts","sourceRoot":"","sources":["../../../../dist/lib/helpers/sanitize.mjs"],"names":[],"mappings":"AAmEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoEG;AACH,wCAjEW,MAAM,SAEd;IAAuB,UAAU,GAAzB,OAAO;IACO,KAAK,GAC3B;QAA8B,KAAK,GAA3B,MAAM,EAAE;QACc,gBAAgB,GAAtC,MAAM,EAAE;QACc,KAAK,GAA3B,MAAM,EAAE;QACc,KAAK,GAA3B,MAAM,EAAE;KAChB;CAAA,GAAU,MAAM,CA+QlB"}
@@ -0,0 +1,66 @@
1
+ /**
2
+ * @function eager_wrapWithRunCtx
3
+ * @internal
4
+ * @package
5
+ * @alias module:@cldmv/slothlet.modes.eager.eager_wrapWithRunCtx
6
+ * @memberof module:@cldmv/slothlet.modes.eager
7
+ * @param {*} obj - The object/function to wrap
8
+ * @param {object} instance - The slothlet instance that will have boundapi.__ctx attached
9
+ * @returns {*} The wrapped object/function
10
+ *
11
+ * @description
12
+ * Recursively wraps all functions in an object with runWithCtx for eager mode.
13
+ * This makes eager mode use the same call stack optimization as lazy mode.
14
+ *
15
+ * @example
16
+ * // Wrapping a function
17
+ * const wrappedFn = eager_wrapWithRunCtx(originalFunction, instance);
18
+ *
19
+ * @example
20
+ * // Wrapping an object with nested functions
21
+ * const wrappedObj = eager_wrapWithRunCtx({ method: fn }, instance);
22
+ *
23
+ * @example
24
+ * // Wrapping a function
25
+ * const wrappedFn = eager_wrapWithRunCtx(originalFunction, instance);
26
+ *
27
+ * @example
28
+ * // Wrapping an object with nested functions
29
+ * const wrappedObj = eager_wrapWithRunCtx({ method: fn }, instance);
30
+ * const wrappedFn = eager_wrapWithRunCtx(originalFunction, instance);
31
+ *
32
+ * @example
33
+ * // Wrapping an object with nested functions
34
+ * const wrappedObj = eager_wrapWithRunCtx({ method: fn }, instance);
35
+ */
36
+ /**
37
+ * @function create
38
+ * @internal
39
+ * @package
40
+ * @async
41
+ * @alias module:@cldmv/slothlet.modes.eager.create
42
+ * @memberof module:@cldmv/slothlet.modes.eager
43
+ * @param {string} dir - Directory to load
44
+ * @param {boolean} [rootLevel=true] - Is this the root level?
45
+ * @param {number} [maxDepth=Infinity] - Maximum depth to traverse
46
+ * @param {number} [currentDepth=0] - Current traversal depth
47
+ * @returns {Promise<object>} Complete API object with all modules loaded
48
+ * @throws {Error} When module loading or directory traversal fails
49
+ *
50
+ * @description
51
+ * Creates the eager API for slothlet (mode: eager).
52
+ * Immediately loads all modules and constructs the complete API structure.
53
+ *
54
+ * @example
55
+ * // Internal usage - called by slothlet core
56
+ * const api = await create('./api_test', true, 3, 0);
57
+ * // Returns: { math: { add: [Function], multiply: [Function] }, ... }
58
+ *
59
+ * @example
60
+ * // Root-level processing with function exports
61
+ * const api = await create('./api_test', true);
62
+ * // If root has default function: api becomes that function with properties
63
+ * // Otherwise: api is object with module properties
64
+ */
65
+ export function create(dir: string, rootLevel?: boolean, maxDepth?: number, currentDepth?: number): Promise<object>;
66
+ //# sourceMappingURL=slothlet_eager.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"slothlet_eager.d.mts","sourceRoot":"","sources":["../../../../dist/lib/modes/slothlet_eager.mjs"],"names":[],"mappings":"AA8HA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAsDH;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,4BAtBW,MAAM,cACN,OAAO,aACP,MAAM,iBACN,MAAM,GACJ,OAAO,CAAC,MAAM,CAAC,CAqE3B"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * @function create
3
+ * @internal
4
+ * @package
5
+ * @async
6
+ * @alias module:@cldmv/slothlet.modes.lazy.create
7
+ * @memberof module:@cldmv/slothlet.modes.lazy
8
+ * @param {string} dir - Root directory
9
+ * @param {boolean} [rootLevel=true] - Root level flag
10
+ * @param {number} [maxDepth=Infinity] - Maximum depth to traverse
11
+ * @param {number} [currentDepth=0] - Current depth (for internal recursion only)
12
+ * @returns {Promise<function|object>} Root API object or function (if default export)
13
+ * @throws {Error} When module loading or directory traversal fails
14
+ *
15
+ * @description
16
+ * Creates a lazy API structure. Root-level files are loaded immediately (mirrors eager).
17
+ * Directories become lazy proxies. Nested directories remain lazy after materialization
18
+ * via _buildCategory recursion with subdirHandler.
19
+ *
20
+ * @example
21
+ * // Internal usage - called by slothlet core
22
+ * const api = await create('./api_test', true, 3, 0);
23
+ * // Returns: { math: [Function: lazyFolder_math], ... } (lazy proxies)
24
+ *
25
+ * @example
26
+ * // Root-level processing with function exports
27
+ * const api = await create('./api_test', true);
28
+ * // If root has default function: api becomes that function with properties
29
+ * // Otherwise: api is object with lazy proxy properties
30
+ */
31
+ export function create(dir: string, rootLevel?: boolean, maxDepth?: number, currentDepth?: number): Promise<Function | object>;
32
+ //# sourceMappingURL=slothlet_lazy.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"slothlet_lazy.d.mts","sourceRoot":"","sources":["../../../../dist/lib/modes/slothlet_lazy.mjs"],"names":[],"mappings":"AAgJA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,4BAvBW,MAAM,cACN,OAAO,aACP,MAAM,iBACN,MAAM,GACJ,OAAO,CAAC,WAAS,MAAM,CAAC,CAwEpC"}
@@ -0,0 +1,49 @@
1
+ export function runWithCtx(ctx: object, fn: Function, thisArg: any, args: any[]): any;
2
+ export function getCtx(): object | null;
3
+ export function makeWrapper(ctx: object): Function;
4
+ /**
5
+ * @constant self
6
+ * @memberof module:@cldmv/slothlet/runtime
7
+ * @export module:@cldmv/slothlet/runtime
8
+ * @public
9
+ * @type {function|object}
10
+ *
11
+ * @description
12
+ * Live binding to the current instance's 'self' reference from AsyncLocalStorage context.
13
+ *
14
+ * @example
15
+ * // Access current instance self
16
+ * console.log(self); // Current slothlet instance
17
+ */
18
+ export const self: Function | object;
19
+ /**
20
+ * @constant context
21
+ * @memberof module:@cldmv/slothlet/runtime
22
+ * @export module:@cldmv/slothlet/runtime
23
+ * @public
24
+ * @type {object}
25
+ *
26
+ * @description
27
+ * Live binding to the current instance's 'context' data from AsyncLocalStorage context.
28
+ *
29
+ * @example
30
+ * // Access current context data
31
+ * console.log(context); // Current context object
32
+ */
33
+ export const context: object;
34
+ /**
35
+ * @constant reference
36
+ * @memberof module:@cldmv/slothlet/runtime
37
+ * @export module:@cldmv/slothlet/runtime
38
+ * @public
39
+ * @type {object}
40
+ *
41
+ * @description
42
+ * Live binding to the current instance's 'reference' object from AsyncLocalStorage context.
43
+ *
44
+ * @example
45
+ * // Access current reference object
46
+ * console.log(reference); // Current reference data
47
+ */
48
+ export const reference: object;
49
+ //# sourceMappingURL=runtime.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime.d.mts","sourceRoot":"","sources":["../../../../dist/lib/runtime/runtime.mjs"],"names":[],"mappings":"AA0CO,gCAdI,MAAM,yBAEN,GAAG,gBAED,GAAG,CA6Bf;AAiBM,0BAZM,MAAM,GAAC,IAAI,CAY0B;AAsB3C,iCAjBI,MAAM,YAwDhB;AA4RD;;;;;;;;;;;;;GAaG;AACH,mBATU,WAAS,MAAM,CAS6B;AAEtD;;;;;;;;;;;;;GAaG;AACH,sBATU,MAAM,CAS4C;AAE5D;;;;;;;;;;;;;GAaG;AACH,wBATU,MAAM,CASgD"}
@@ -0,0 +1,124 @@
1
+ /**
2
+ * Mutates a live-binding variable (object or function) to match a new value, preserving reference.
3
+ * @param {function|object} target - The variable to mutate (object or function).
4
+ * @param {function|object} source - The new value to copy from (object or function).
5
+ * @private
6
+ * @internal
7
+ * @example
8
+ * mutateLiveBindingFunction(self, newSelf);
9
+ * mutateLiveBindingFunction(boundapi, newApi);
10
+ */
11
+ export function mutateLiveBindingFunction(target: Function | object, source: Function | object): void;
12
+ /**
13
+ * The shared _slothlet parameter for live binding coordination.
14
+ * @type {string}
15
+ * @private
16
+ * @internal
17
+ */
18
+ export let _slothlet: string;
19
+ /**
20
+ * Live-binding reference to the current API instance.
21
+ * This is updated whenever a new API instance is created.
22
+ * Dynamically imported modules can access this at runtime.
23
+ * @type {object}
24
+ * @private
25
+ * @internal
26
+ */
27
+ export const self: object;
28
+ /**
29
+ * Live-binding reference for contextual data.
30
+ * @type {object}
31
+ * @private
32
+ * @internal
33
+ */
34
+ export const context: object;
35
+ /**
36
+ * Live-binding reference for reference data.
37
+ * @type {object}
38
+ * @private
39
+ * @internal
40
+ */
41
+ export const reference: object;
42
+ export default slothlet;
43
+ export type SlothletOptions = {
44
+ /**
45
+ * - Directory to load API modules from.
46
+ * - Can be absolute or relative path.
47
+ * - If relative, resolved from the calling file's location.
48
+ * - Defaults to "api" directory relative to caller.
49
+ */
50
+ dir?: string;
51
+ /**
52
+ * - Loading strategy:
53
+ * - `true`: Lazy loading - modules loaded on-demand when accessed (lower initial load, proxy overhead)
54
+ * - `false`: Eager loading - all modules loaded immediately (default, higher initial load, direct access)
55
+ */
56
+ lazy?: boolean;
57
+ /**
58
+ * - Directory traversal depth control:
59
+ * - `Infinity`: Traverse all subdirectories recursively (default)
60
+ * - `0`: Only load files in root directory, no subdirectories
61
+ * - `1`, `2`, etc.: Limit traversal to specified depth levels
62
+ */
63
+ apiDepth?: number;
64
+ /**
65
+ * - Debug output control:
66
+ * - `true`: Enable verbose logging for module loading, API construction, and binding operations
67
+ * - `false`: Silent operation (default)
68
+ * - Can be set via command line flag `--slothletdebug`, environment variable `SLOTHLET_DEBUG=true`, or options parameter
69
+ * - Command line and environment settings become the default for all instances unless overridden
70
+ */
71
+ debug?: boolean;
72
+ /**
73
+ * - Execution environment mode:
74
+ * - `"singleton"`: Single shared instance within current process (default, fastest)
75
+ * - `"vm"`: Isolated VM context for security/isolation
76
+ * - `"worker"`: Web Worker or Worker Thread execution
77
+ * - `"fork"`: Child process execution for complete isolation
78
+ */
79
+ mode?: string;
80
+ /**
81
+ * - API structure and calling convention:
82
+ * - `"auto"`: Auto-detect based on root module exports (function vs object) - recommended (default)
83
+ * - `"function"`: Force API to be callable as function with properties attached
84
+ * - `"object"`: Force API to be plain object with method properties
85
+ */
86
+ api_mode?: string;
87
+ /**
88
+ * - Context data object injected into live-binding `context` reference.
89
+ * - Available to all loaded modules via `import { context } from '@cldmv/slothlet/runtime'`. Useful for request data,
90
+ * - user sessions, environment configs, etc.
91
+ */
92
+ context?: object;
93
+ /**
94
+ * - Reference object merged into the API root level.
95
+ * - Properties not conflicting with loaded modules are added directly to the API.
96
+ * - Useful for utility functions, constants, or external service connections.
97
+ */
98
+ reference?: object;
99
+ /**
100
+ * - Filename sanitization options for API property names.
101
+ * - Controls how file names are converted to valid JavaScript identifiers.
102
+ * - Default behavior: camelCase conversion with lowerFirst=true.
103
+ */
104
+ sanitize?: {
105
+ lowerFirst?: boolean;
106
+ rules?: {
107
+ leave?: string[];
108
+ leaveInsensitive?: string[];
109
+ upper?: string[];
110
+ lower?: string[];
111
+ };
112
+ };
113
+ };
114
+ /**
115
+ * Creates a slothlet API instance with the specified configuration.
116
+ * This is the main entry point that can be called directly as a function.
117
+ * @async
118
+ * @alias module:@cldmv/slothlet
119
+ * @param {SlothletOptions} [options={}] - Configuration options for creating the API
120
+ * @returns {Promise<function|object>} The bound API object or function
121
+ * @public
122
+ */
123
+ export function slothlet(options?: SlothletOptions): Promise<Function | object>;
124
+ //# sourceMappingURL=slothlet.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"slothlet.d.mts","sourceRoot":"","sources":["../../dist/slothlet.mjs"],"names":[],"mappings":"AAi5CA;;;;;;;;;GASG;AACH,kDARW,WAAS,MAAM,UACf,WAAS,MAAM,QAwCzB;AAzzCD;;;;;GAKG;AACH,sBAJU,MAAM,CAIoE;AAKpF;;;;;;;GAOG;AACH,mBAJU,MAAM,CAIO;AAEvB;;;;;GAKG;AACH,sBAJU,MAAM,CAIU;AAE1B;;;;;GAKG;AACH,wBAJU,MAAM,CAIY;;;;;;;;;UAiyCd,MAAM;;;;;;WAIN,OAAO;;;;;;;eAGP,MAAM;;;;;;;;YAIN,OAAO;;;;;;;;WAKP,MAAM;;;;;;;eAKN,MAAM;;;;;;cAIN,MAAM;;;;;;gBAGN,MAAM;;;;;;eAMjB;QAA8B,UAAU,GAA7B,OAAO;QACW,KAAK,GAClC;YAAqC,KAAK,GAA/B,MAAM,EAAE;YACkB,gBAAgB,GAA1C,MAAM,EAAE;YACkB,KAAK,GAA/B,MAAM,EAAE;YACkB,KAAK,GAA/B,MAAM,EAAE;SACrB;KAAA;;AAv0CD;;;;;;;;GAQG;AACH,mCAJW,eAAe,GACb,OAAO,CAAC,WAAS,MAAM,CAAC,CAiCpC"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * TypeScript declarations for @cldmv/slothlet index
3
+ *
4
+ * This file is auto-generated by tools/build-exports.mjs
5
+ * Run: npm run build:exports to regenerate
6
+ */
7
+
8
+ // Re-export all types from the main slothlet module
9
+ export type * from "./dist/slothlet.d.mts";
10
+ // If you want runtime exports, use the implementation file instead:
11
+ export * from "./dist/slothlet.mts";
12
+
13
+ // Auto-generated named export declarations (these override the re-export above)
14
+ export const _slothlet: any;
15
+ export const context: any;
16
+ export const mutateLiveBindingFunction: any;
17
+ export const reference: any;
18
+ export const self: any;
19
+
20
+ // Main slothlet export
21
+ declare const slothlet: any;
22
+ export default slothlet;
23
+ export { slothlet };