@cldmv/slothlet 2.11.0 → 3.0.1

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 (189) hide show
  1. package/AGENT-USAGE.md +355 -325
  2. package/README.md +554 -238
  3. package/dist/lib/builders/api-assignment.mjs +605 -0
  4. package/dist/lib/builders/api_builder.mjs +1073 -0
  5. package/dist/lib/builders/builder.mjs +94 -0
  6. package/dist/lib/builders/modes-processor.mjs +1816 -0
  7. package/dist/lib/errors.mjs +227 -0
  8. package/dist/lib/factories/component-base.mjs +96 -0
  9. package/dist/lib/factories/context.mjs +38 -0
  10. package/dist/lib/handlers/api-cache-manager.mjs +216 -0
  11. package/dist/lib/handlers/api-manager.mjs +2364 -0
  12. package/dist/lib/handlers/context-async.mjs +184 -0
  13. package/dist/lib/handlers/context-live.mjs +184 -0
  14. package/dist/lib/handlers/hook-manager.mjs +789 -0
  15. package/dist/lib/handlers/lifecycle-token.mjs +44 -0
  16. package/dist/lib/handlers/lifecycle.mjs +131 -0
  17. package/dist/lib/handlers/materialize-manager.mjs +64 -0
  18. package/dist/lib/handlers/metadata.mjs +500 -0
  19. package/dist/lib/handlers/ownership.mjs +338 -0
  20. package/dist/lib/handlers/unified-wrapper.mjs +3031 -0
  21. package/dist/lib/helpers/class-instance-wrapper.mjs +125 -0
  22. package/dist/lib/helpers/config.mjs +343 -0
  23. package/dist/lib/helpers/eventemitter-context.mjs +365 -0
  24. package/dist/lib/helpers/hint-detector.mjs +63 -0
  25. package/dist/lib/helpers/modes-utils.mjs +53 -0
  26. package/dist/lib/helpers/resolve-from-caller.mjs +123 -117
  27. package/dist/lib/helpers/sanitize.mjs +247 -168
  28. package/dist/lib/helpers/utilities.mjs +46 -81
  29. package/dist/lib/i18n/languages/de-de.json +377 -0
  30. package/dist/lib/i18n/languages/en-gb.json +377 -0
  31. package/dist/lib/i18n/languages/en-us.json +377 -0
  32. package/dist/lib/i18n/languages/es-mx.json +377 -0
  33. package/dist/lib/i18n/languages/fr-fr.json +377 -0
  34. package/dist/lib/i18n/languages/hi-in.json +377 -0
  35. package/dist/lib/i18n/languages/ja-jp.json +377 -0
  36. package/dist/lib/i18n/languages/ko-kr.json +377 -0
  37. package/dist/lib/i18n/languages/pt-br.json +377 -0
  38. package/dist/lib/i18n/languages/ru-ru.json +377 -0
  39. package/dist/lib/i18n/languages/zh-cn.json +377 -0
  40. package/dist/lib/i18n/translations.mjs +140 -0
  41. package/dist/lib/modes/eager.mjs +75 -0
  42. package/dist/lib/modes/lazy.mjs +97 -0
  43. package/dist/lib/processors/flatten.mjs +453 -0
  44. package/dist/lib/processors/loader.mjs +355 -0
  45. package/dist/lib/processors/type-generator.mjs +291 -0
  46. package/dist/lib/processors/typescript.mjs +188 -0
  47. package/dist/lib/runtime/runtime-asynclocalstorage.mjs +80 -522
  48. package/dist/lib/runtime/runtime-livebindings.mjs +45 -390
  49. package/dist/lib/runtime/runtime.mjs +39 -159
  50. package/dist/slothlet.mjs +525 -744
  51. package/docs/API-RULES.md +338 -486
  52. package/index.cjs +4 -4
  53. package/index.mjs +82 -45
  54. package/package.json +143 -30
  55. package/types/dist/lib/builders/api-assignment.d.mts +97 -0
  56. package/types/dist/lib/builders/api-assignment.d.mts.map +1 -0
  57. package/types/dist/lib/builders/api_builder.d.mts +96 -0
  58. package/types/dist/lib/builders/api_builder.d.mts.map +1 -0
  59. package/types/dist/lib/builders/builder.d.mts +60 -0
  60. package/types/dist/lib/builders/builder.d.mts.map +1 -0
  61. package/types/dist/lib/builders/modes-processor.d.mts +32 -0
  62. package/types/dist/lib/builders/modes-processor.d.mts.map +1 -0
  63. package/types/dist/lib/errors.d.mts +118 -0
  64. package/types/dist/lib/errors.d.mts.map +1 -0
  65. package/types/dist/lib/factories/component-base.d.mts +182 -0
  66. package/types/dist/lib/factories/component-base.d.mts.map +1 -0
  67. package/types/dist/lib/factories/context.d.mts +26 -0
  68. package/types/dist/lib/factories/context.d.mts.map +1 -0
  69. package/types/dist/lib/handlers/api-cache-manager.d.mts +208 -0
  70. package/types/dist/lib/handlers/api-cache-manager.d.mts.map +1 -0
  71. package/types/dist/lib/handlers/api-manager.d.mts +392 -0
  72. package/types/dist/lib/handlers/api-manager.d.mts.map +1 -0
  73. package/types/dist/lib/handlers/context-async.d.mts +66 -0
  74. package/types/dist/lib/handlers/context-async.d.mts.map +1 -0
  75. package/types/dist/lib/handlers/context-live.d.mts +65 -0
  76. package/types/dist/lib/handlers/context-live.d.mts.map +1 -0
  77. package/types/dist/lib/handlers/hook-manager.d.mts +199 -0
  78. package/types/dist/lib/handlers/hook-manager.d.mts.map +1 -0
  79. package/types/dist/lib/handlers/lifecycle-token.d.mts +49 -0
  80. package/types/dist/lib/handlers/lifecycle-token.d.mts.map +1 -0
  81. package/types/dist/lib/handlers/lifecycle.d.mts +90 -0
  82. package/types/dist/lib/handlers/lifecycle.d.mts.map +1 -0
  83. package/types/dist/lib/handlers/materialize-manager.d.mts +75 -0
  84. package/types/dist/lib/handlers/materialize-manager.d.mts.map +1 -0
  85. package/types/dist/lib/handlers/metadata.d.mts +215 -0
  86. package/types/dist/lib/handlers/metadata.d.mts.map +1 -0
  87. package/types/dist/lib/handlers/ownership.d.mts +170 -0
  88. package/types/dist/lib/handlers/ownership.d.mts.map +1 -0
  89. package/types/dist/lib/handlers/unified-wrapper.d.mts +250 -0
  90. package/types/dist/lib/handlers/unified-wrapper.d.mts.map +1 -0
  91. package/types/dist/lib/helpers/class-instance-wrapper.d.mts +54 -0
  92. package/types/dist/lib/helpers/class-instance-wrapper.d.mts.map +1 -0
  93. package/types/dist/lib/helpers/config.d.mts +96 -0
  94. package/types/dist/lib/helpers/config.d.mts.map +1 -0
  95. package/types/dist/lib/helpers/eventemitter-context.d.mts +31 -0
  96. package/types/dist/lib/helpers/eventemitter-context.d.mts.map +1 -0
  97. package/types/dist/lib/helpers/hint-detector.d.mts +20 -0
  98. package/types/dist/lib/helpers/hint-detector.d.mts.map +1 -0
  99. package/types/dist/lib/helpers/modes-utils.d.mts +35 -0
  100. package/types/dist/lib/helpers/modes-utils.d.mts.map +1 -0
  101. package/types/dist/lib/helpers/resolve-from-caller.d.mts +29 -145
  102. package/types/dist/lib/helpers/resolve-from-caller.d.mts.map +1 -1
  103. package/types/dist/lib/helpers/sanitize.d.mts +95 -94
  104. package/types/dist/lib/helpers/sanitize.d.mts.map +1 -1
  105. package/types/dist/lib/helpers/utilities.d.mts +53 -116
  106. package/types/dist/lib/helpers/utilities.d.mts.map +1 -1
  107. package/types/dist/lib/i18n/translations.d.mts +39 -0
  108. package/types/dist/lib/i18n/translations.d.mts.map +1 -0
  109. package/types/dist/lib/modes/eager.d.mts +36 -0
  110. package/types/dist/lib/modes/eager.d.mts.map +1 -0
  111. package/types/dist/lib/modes/lazy.d.mts +49 -0
  112. package/types/dist/lib/modes/lazy.d.mts.map +1 -0
  113. package/types/dist/lib/processors/flatten.d.mts +114 -0
  114. package/types/dist/lib/processors/flatten.d.mts.map +1 -0
  115. package/types/dist/lib/processors/loader.d.mts +47 -0
  116. package/types/dist/lib/processors/loader.d.mts.map +1 -0
  117. package/types/dist/lib/processors/type-generator.d.mts +19 -0
  118. package/types/dist/lib/processors/type-generator.d.mts.map +1 -0
  119. package/types/dist/lib/processors/typescript.d.mts +55 -0
  120. package/types/dist/lib/processors/typescript.d.mts.map +1 -0
  121. package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts +47 -42
  122. package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts.map +1 -1
  123. package/types/dist/lib/runtime/runtime-livebindings.d.mts +34 -65
  124. package/types/dist/lib/runtime/runtime-livebindings.d.mts.map +1 -1
  125. package/types/dist/lib/runtime/runtime.d.mts +39 -9
  126. package/types/dist/lib/runtime/runtime.d.mts.map +1 -1
  127. package/types/dist/slothlet.d.mts +184 -111
  128. package/types/dist/slothlet.d.mts.map +1 -1
  129. package/types/index.d.mts +1 -3
  130. package/dist/lib/engine/README.md +0 -21
  131. package/dist/lib/engine/slothlet_child.mjs +0 -59
  132. package/dist/lib/engine/slothlet_engine.mjs +0 -372
  133. package/dist/lib/engine/slothlet_esm.mjs +0 -230
  134. package/dist/lib/engine/slothlet_helpers.mjs +0 -455
  135. package/dist/lib/engine/slothlet_worker.mjs +0 -149
  136. package/dist/lib/helpers/als-eventemitter.mjs +0 -256
  137. package/dist/lib/helpers/api_builder/add_api.mjs +0 -553
  138. package/dist/lib/helpers/api_builder/analysis.mjs +0 -532
  139. package/dist/lib/helpers/api_builder/construction.mjs +0 -495
  140. package/dist/lib/helpers/api_builder/decisions.mjs +0 -748
  141. package/dist/lib/helpers/api_builder/metadata.mjs +0 -248
  142. package/dist/lib/helpers/api_builder.mjs +0 -41
  143. package/dist/lib/helpers/auto-wrap.mjs +0 -62
  144. package/dist/lib/helpers/hooks.mjs +0 -389
  145. package/dist/lib/helpers/instance-manager.mjs +0 -111
  146. package/dist/lib/helpers/metadata-api.mjs +0 -201
  147. package/dist/lib/helpers/multidefault.mjs +0 -216
  148. package/dist/lib/modes/slothlet_eager.mjs +0 -154
  149. package/dist/lib/modes/slothlet_lazy.mjs +0 -594
  150. package/docs/API-RULES-CONDITIONS.md +0 -712
  151. package/types/dist/lib/engine/slothlet_child.d.mts +0 -2
  152. package/types/dist/lib/engine/slothlet_child.d.mts.map +0 -1
  153. package/types/dist/lib/engine/slothlet_engine.d.mts +0 -31
  154. package/types/dist/lib/engine/slothlet_engine.d.mts.map +0 -1
  155. package/types/dist/lib/engine/slothlet_esm.d.mts +0 -19
  156. package/types/dist/lib/engine/slothlet_esm.d.mts.map +0 -1
  157. package/types/dist/lib/engine/slothlet_helpers.d.mts +0 -25
  158. package/types/dist/lib/engine/slothlet_helpers.d.mts.map +0 -1
  159. package/types/dist/lib/engine/slothlet_worker.d.mts +0 -2
  160. package/types/dist/lib/engine/slothlet_worker.d.mts.map +0 -1
  161. package/types/dist/lib/helpers/als-eventemitter.d.mts +0 -56
  162. package/types/dist/lib/helpers/als-eventemitter.d.mts.map +0 -1
  163. package/types/dist/lib/helpers/api_builder/add_api.d.mts +0 -102
  164. package/types/dist/lib/helpers/api_builder/add_api.d.mts.map +0 -1
  165. package/types/dist/lib/helpers/api_builder/analysis.d.mts +0 -189
  166. package/types/dist/lib/helpers/api_builder/analysis.d.mts.map +0 -1
  167. package/types/dist/lib/helpers/api_builder/construction.d.mts +0 -107
  168. package/types/dist/lib/helpers/api_builder/construction.d.mts.map +0 -1
  169. package/types/dist/lib/helpers/api_builder/decisions.d.mts +0 -213
  170. package/types/dist/lib/helpers/api_builder/decisions.d.mts.map +0 -1
  171. package/types/dist/lib/helpers/api_builder/metadata.d.mts +0 -99
  172. package/types/dist/lib/helpers/api_builder/metadata.d.mts.map +0 -1
  173. package/types/dist/lib/helpers/api_builder.d.mts +0 -6
  174. package/types/dist/lib/helpers/api_builder.d.mts.map +0 -1
  175. package/types/dist/lib/helpers/auto-wrap.d.mts +0 -49
  176. package/types/dist/lib/helpers/auto-wrap.d.mts.map +0 -1
  177. package/types/dist/lib/helpers/hooks.d.mts +0 -342
  178. package/types/dist/lib/helpers/hooks.d.mts.map +0 -1
  179. package/types/dist/lib/helpers/instance-manager.d.mts +0 -41
  180. package/types/dist/lib/helpers/instance-manager.d.mts.map +0 -1
  181. package/types/dist/lib/helpers/metadata-api.d.mts +0 -132
  182. package/types/dist/lib/helpers/metadata-api.d.mts.map +0 -1
  183. package/types/dist/lib/helpers/multidefault.d.mts +0 -90
  184. package/types/dist/lib/helpers/multidefault.d.mts.map +0 -1
  185. package/types/dist/lib/modes/slothlet_eager.d.mts +0 -65
  186. package/types/dist/lib/modes/slothlet_eager.d.mts.map +0 -1
  187. package/types/dist/lib/modes/slothlet_lazy.d.mts +0 -31
  188. package/types/dist/lib/modes/slothlet_lazy.d.mts.map +0 -1
  189. package/types/index.d.mts.map +0 -1
@@ -20,160 +20,166 @@
20
20
 
21
21
  import fs from "node:fs";
22
22
  import path from "node:path";
23
- import { fileURLToPath, pathToFileURL } from "node:url";
23
+ import { fileURLToPath } from "node:url";
24
+ import { ComponentBase } from "@cldmv/slothlet/factories/component-base";
24
25
 
25
26
 
27
+ const __filename = fileURLToPath(import.meta.url);
28
+ const __dirname = path.dirname(__filename);
29
+ const SLOTHLET_LIB_ROOT = path.resolve(__dirname, "../..");
30
+ const SLOTHLET_PKG_ROOT = path.normalize(path.resolve(__dirname, "../../.."));
26
31
 
32
+ export class Resolver extends ComponentBase {
33
+ static slothletProperty = "resolver";
27
34
 
28
- export const toFsPath = (v) => (v && String(v).startsWith("file://") ? fileURLToPath(String(v)) : v ? String(v) : null);
29
-
30
-
31
- export function getStack(skipFn) {
32
- const orig = Error.prepareStackTrace;
33
- try {
34
- Error.prepareStackTrace = (_, s) => s;
35
- const e = new Error("Stack trace");
36
- if (skipFn) Error.captureStackTrace(e, skipFn);
37
- return e.stack || [];
38
- } finally {
39
- Error.prepareStackTrace = orig;
35
+
36
+ getStack(skipFn) {
37
+ const orig = Error.prepareStackTrace;
38
+ try {
39
+ Error.prepareStackTrace = (_, s) => s;
40
+ const e = new Error("Stack trace");
41
+ if (skipFn) Error.captureStackTrace(e, skipFn);
42
+ return e.stack;
43
+ } finally {
44
+ Error.prepareStackTrace = orig;
45
+ }
40
46
  }
41
- }
42
-
43
- const THIS_FILE = fileURLToPath(import.meta.url);
44
- const THIS_DIR = path.dirname(THIS_FILE);
45
-
46
-
47
-
48
47
 
49
-
50
-
51
-
52
-
53
- function pickPrimaryBaseFile() {
54
- const files = [];
55
- for (const cs of getStack(pickPrimaryBaseFile)) {
56
- const f = toFsPath(cs?.getFileName?.());
57
- if (!f) continue;
58
-
59
- if (f.startsWith?.("node:")) continue;
60
- files.push(f);
48
+
49
+ #getStack() {
50
+ const originalPrepare = Error.prepareStackTrace;
51
+ Error.prepareStackTrace = (___, stack) => stack;
52
+ const stack = new Error().stack;
53
+ Error.prepareStackTrace = originalPrepare;
54
+ return stack;
61
55
  }
62
56
 
63
- console.log(`[DEBUG_RESOLVE] pickPrimaryBaseFile - Total stack frames: ${files.length}`);
64
- console.log(`[DEBUG_RESOLVE] ALL stack frames:`);
65
- for (let i = 0; i < files.length; i++) {
66
- console.log(`[DEBUG_RESOLVE] [${i}] ${files[i]}`);
57
+
58
+ toFsPath(v) {
59
+ if (!v) return null;
60
+ const str = String(v);
61
+ return str.startsWith("file://") ? fileURLToPath(str) : str;
67
62
  }
68
63
 
69
- let iSloth = -1;
70
- for (let i = 0; i < files.length; i++) {
71
- if (path.basename(files[i]).toLowerCase() === "slothlet.mjs") {
72
- iSloth = i;
73
- }
74
- }
64
+
65
+ #isSlothletInternal(filePath) {
66
+
67
+ const normalized = path.normalize(filePath);
68
+ const normalizedLibRoot = path.normalize(SLOTHLET_LIB_ROOT);
75
69
 
76
- console.log(`[DEBUG_RESOLVE] Found slothlet.mjs at index: ${iSloth}, total files: ${files.length}`);
77
- if (iSloth !== -1) {
78
- console.log(`[DEBUG_RESOLVE] Files after slothlet.mjs:`);
79
- for (let k = iSloth + 1; k < Math.min(files.length, iSloth + 5); k++) {
80
- console.log(`[DEBUG_RESOLVE] [${k}] ${files[k]}`);
70
+
71
+ if (normalized.startsWith(normalizedLibRoot)) {
72
+ return true;
81
73
  }
82
- }
83
74
 
84
- if (iSloth !== -1) {
85
75
 
86
- let j = iSloth + 1;
87
-
88
76
 
89
- function isSlothletInternalFile(filePath) {
90
- if (!filePath) return true;
91
-
92
-
93
- if (filePath.includes(path.sep + "src" + path.sep + "lib" + path.sep)) return true;
77
+
78
+
79
+
80
+ const basename = path.basename(normalized);
81
+ if (basename === "index.mjs" || basename === "index.cjs") {
82
+
83
+
84
+
85
+
86
+ if (path.dirname(normalized) === SLOTHLET_PKG_ROOT) return true;
87
+ }
94
88
 
95
-
96
- if (path.basename(filePath).toLowerCase() === "slothlet.mjs") return true;
89
+ return false;
90
+ }
97
91
 
98
92
 
99
- if (filePath.startsWith(THIS_DIR + path.sep)) return true;
93
+ #findCallerBase() {
94
+ const stack = this.#getStack();
95
+ const files = stack.map((s) => this.toFsPath(s.getFileName())).filter(Boolean);
100
96
 
101
- return false;
102
- }
103
-
104
-
105
- function pickFallbackBaseFile() {
106
- console.log("[DEBUG_RESOLVE] pickFallbackBaseFile called");
107
- for (const cs of getStack(pickFallbackBaseFile)) {
108
- const f = toFsPath(cs?.getFileName?.());
109
- if (!f) continue;
110
97
 
111
- if (f.startsWith?.("node:")) continue;
112
- if (f === THIS_FILE) continue;
113
- if (f.startsWith(THIS_DIR + path.sep)) continue;
114
- if (path.basename(f).toLowerCase() === "slothlet.mjs") continue;
115
- if (isSlothletInternalFile(f)) {
116
- console.log(`[DEBUG_RESOLVE] Fallback skipping internal file: ${f}`);
117
- continue;
98
+ const slothletIndex = files.findIndex((f) => path.basename(f).toLowerCase() === "slothlet.mjs");
99
+
100
+ if (slothletIndex === -1) {
101
+
102
+ for (const file of files) {
103
+ if (!this.#isSlothletInternal(file)) {
104
+ return file;
105
+ }
106
+ }
107
+
108
+
109
+
110
+
111
+
112
+ return null;
118
113
  }
119
- console.log(`[DEBUG_RESOLVE] Fallback considering: ${f}`);
120
- return f;
121
- }
122
-
123
-
124
- console.log(`[DEBUG_RESOLVE] Fallback: No user code found in stack, using process.cwd(): ${process.cwd()}`);
125
- return process.cwd();
126
- }
127
-
128
-
129
-
130
114
 
131
- function resolveWith(rel, makePrimary, exists, makeFallback) {
132
- if (typeof rel !== "string") throw new TypeError("rel must be a string");
115
+
116
+ for (let i = slothletIndex + 1; i < files.length; i++) {
117
+ const file = files[i];
133
118
 
134
-
135
- const primaryBase = pickPrimaryBaseFile() ?? pickFallbackBaseFile();
136
- const primary = makePrimary(primaryBase, rel);
137
- if (exists(primary)) return primary;
119
+
120
+ if (file.startsWith?.("node:")) continue;
138
121
 
139
- const fbBase = pickFallbackBaseFile();
140
- return makeFallback(fbBase, rel);
141
- }
122
+
123
+ if (this.#isSlothletInternal(file)) continue;
142
124
 
125
+
126
+ return file;
127
+ }
143
128
 
129
+
130
+
131
+
132
+
133
+
134
+
135
+ for (const file of files) {
136
+ if (!this.#isSlothletInternal(file)) {
137
+ return file;
138
+ }
139
+ }
144
140
 
141
+
142
+
143
+
144
+
145
+ return null;
146
+ }
145
147
 
146
- export function resolvePathFromCaller(rel) {
147
148
 
148
- if (rel.startsWith?.("file://")) return fileURLToPath(rel);
149
- if (path.isAbsolute(rel)) return rel;
149
+ resolvePathFromCaller(rel) {
150
+
151
+ if (rel.startsWith?.("file://")) return fileURLToPath(rel);
152
+ if (path.isAbsolute(rel)) return rel;
150
153
 
151
- return resolveWith(
152
- rel,
153
154
 
154
- (baseFile, r) => path.resolve(path.dirname(baseFile), r),
155
+ const callerFile = this.#findCallerBase();
156
+
155
157
 
156
- (candidate) => fs.existsSync(candidate),
157
158
 
158
- (baseFile, r) => path.resolve(path.dirname(baseFile), r)
159
- );
160
- }
159
+
160
+
161
+ if (!callerFile) {
162
+
163
+ return path.resolve(process.cwd(), rel);
164
+ }
161
165
 
166
+
167
+ const callerDir = path.dirname(callerFile);
168
+ const resolved = path.resolve(callerDir, rel);
162
169
 
163
- export function resolveUrlFromCaller(rel) {
164
-
165
- if (rel.startsWith?.("file://")) return rel;
166
- if (path.isAbsolute(rel)) return pathToFileURL(rel).href;
170
+
171
+ if (fs.existsSync(resolved)) {
172
+ return resolved;
173
+ }
167
174
 
168
- return resolveWith(
169
- rel,
170
175
 
171
- (baseFile, r) => new URL(r, pathToFileURL(baseFile)).href,
176
+ const cwdResolved = path.resolve(process.cwd(), rel);
177
+ if (fs.existsSync(cwdResolved)) {
178
+ return cwdResolved;
179
+ }
180
+
172
181
 
173
- (href) => fs.existsSync(fileURLToPath(href)),
174
182
 
175
- (baseFile, r) => new URL(r, pathToFileURL(baseFile)).href
176
- );
183
+ return resolved;
184
+ }
177
185
  }
178
-
179
-