@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
@@ -1,594 +0,0 @@
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
- import fs from "node:fs/promises";
21
- import { readdirSync } from "node:fs";
22
- import path from "node:path";
23
- import { types as utilTypes } from "node:util";
24
- import { processModuleForAPI } from "@cldmv/slothlet/helpers/api_builder";
25
- import { multidefault_analyzeModules } from "@cldmv/slothlet/helpers/multidefault";
26
-
27
-
28
- export async function create(dir, maxDepth = Infinity, currentDepth = 0) {
29
- const instance = this;
30
-
31
-
32
- const runtimePath = instance.config.runtime === "live" ? "@cldmv/slothlet/runtime/live" : "@cldmv/slothlet/runtime/async";
33
- const { runWithCtx } = await import(runtimePath);
34
-
35
- const entries = await fs.readdir(dir, { withFileTypes: true });
36
- let api = {};
37
- let rootDefaultFn = null;
38
-
39
-
40
-
41
- const moduleFiles = entries.filter((e) => instance._shouldIncludeFile(e));
42
- const defaultExportFiles = [];
43
-
44
-
45
- const analysis = await multidefault_analyzeModules(moduleFiles, dir, { debug: instance.config.debug, instance });
46
-
47
- const { totalDefaultExports, hasMultipleDefaultExports, selfReferentialFiles, defaultExportFiles: analysisDefaults } = analysis;
48
-
49
-
50
- defaultExportFiles.length = 0;
51
- for (const { fileName } of analysisDefaults) {
52
- const entry = moduleFiles.find((f) => path.basename(f.name, path.extname(f.name)) === fileName);
53
- if (entry) {
54
-
55
- const moduleResult = await instance._loadSingleModule(path.join(dir, entry.name), true);
56
- const mod = moduleResult.mod;
57
- const moduleAnalysis = moduleResult.analysis;
58
- defaultExportFiles.push({ entry, fileName, mod, analysis: moduleAnalysis });
59
- }
60
- }
61
-
62
- if (instance.config.debug) {
63
- console.log(`[DEBUG] Lazy mode: Using shared multidefault utility results`);
64
- console.log(`[DEBUG] - totalDefaultExports: ${totalDefaultExports}`);
65
- console.log(`[DEBUG] - hasMultipleDefaultExports: ${hasMultipleDefaultExports}`);
66
- console.log(`[DEBUG] - selfReferentialFiles: ${Array.from(selfReferentialFiles)}`);
67
- }
68
-
69
-
70
- const processedModuleCache = new Map();
71
-
72
- for (const entry of moduleFiles) {
73
- const ext = path.extname(entry.name);
74
- const fileName = path.basename(entry.name, ext);
75
- const apiPathKey = instance._toapiPathKey(fileName);
76
-
77
-
78
- let mod = null;
79
- let analysis = null;
80
- const existingDefault = defaultExportFiles.find((def) => def.fileName === fileName);
81
- if (existingDefault) {
82
- mod = existingDefault.mod;
83
- analysis = existingDefault.analysis;
84
- } else {
85
-
86
- const moduleResult = await instance._loadSingleModule(path.join(dir, entry.name), true);
87
- mod = moduleResult.mod;
88
- analysis = moduleResult.analysis;
89
- processedModuleCache.set(entry.name, mod);
90
- }
91
-
92
-
93
- const isSelfReferential = selfReferentialFiles.has(fileName);
94
-
95
-
96
- processModuleForAPI({
97
- mod,
98
- fileName,
99
- apiPathKey,
100
- hasMultipleDefaultExports,
101
- isSelfReferential,
102
- api,
103
- getRootDefault: () => rootDefaultFn,
104
- setRootDefault: (fn) => {
105
- rootDefaultFn = fn;
106
- },
107
- context: {
108
- debug: instance.config.debug,
109
- mode: "root",
110
- totalModules: moduleFiles.length
111
- },
112
- originalAnalysis: analysis
113
- });
114
- }
115
-
116
-
117
- if (rootDefaultFn) {
118
- Object.assign(rootDefaultFn, api);
119
- api = rootDefaultFn;
120
- }
121
-
122
-
123
- for (const entry of entries) {
124
- if (entry.isDirectory() && !entry.name.startsWith(".") && currentDepth < maxDepth) {
125
- const key = instance._toapiPathKey(entry.name);
126
- const subDirPath = path.join(dir, entry.name);
127
- const parent = api;
128
- const depth = 1;
129
-
130
-
131
- try {
132
- const subEntries = readdirSync(subDirPath, { withFileTypes: true });
133
- const hasFiles = subEntries.some(
134
- (subEntry) =>
135
- subEntry.isFile() &&
136
- !subEntry.name.startsWith(".") &&
137
- (subEntry.name.endsWith(".mjs") || subEntry.name.endsWith(".cjs") || subEntry.name.endsWith(".js"))
138
- );
139
- const hasSubdirs = subEntries.some((subEntry) => subEntry.isDirectory() && !subEntry.name.startsWith("."));
140
-
141
-
142
- if (!hasFiles && !hasSubdirs) {
143
- if (instance?.config?.debug) {
144
- console.log(`[lazy][debug] empty folder detected during traversal: ${subDirPath} -> adding {}`);
145
- }
146
- parent[key] = {};
147
- continue;
148
- }
149
- } catch (error) {
150
-
151
- if (instance?.config?.debug) {
152
- console.log(`[lazy][debug] error reading directory ${subDirPath}: ${error.message}, creating lazy proxy`);
153
- }
154
- }
155
-
156
- const proxy = createFolderProxy({
157
- subDirPath,
158
- key,
159
- parent,
160
- instance,
161
- depth,
162
- maxDepth,
163
- pathParts: [key],
164
- runWithCtx,
165
- existingContent: parent[key]
166
- });
167
- parent[key] = proxy;
168
- }
169
- }
170
-
171
- return api;
172
- }
173
-
174
-
175
- function replacePlaceholder(parent, key, placeholder, value, instance, depth) {
176
- if (!parent || !key) return;
177
-
178
-
179
- if (
180
- parent[key] !== placeholder &&
181
- parent[key] &&
182
- typeof parent[key] === "object" &&
183
- typeof value === "object" &&
184
- !Array.isArray(parent[key]) &&
185
- !Array.isArray(value)
186
- ) {
187
- if (instance?.config?.debug) {
188
- console.log(`[lazy] MERGE CASE DETECTED for '${key}' - existing:`, Object.keys(parent[key]), "new:", Object.keys(value));
189
- console.log(`[lazy] Before merge - parent[${key}]:`, parent[key]);
190
- }
191
-
192
- Object.assign(parent[key], value);
193
- if (instance?.config?.debug) {
194
- console.log(`[lazy] After merge - parent[${key}]:`, Object.keys(parent[key]));
195
- }
196
- return;
197
- }
198
-
199
- if (parent[key] !== placeholder) return;
200
-
201
-
202
-
203
-
204
-
205
- const isCustomProxy = value && typeof value === "object" && (utilTypes?.isProxy?.(value) ?? false);
206
-
207
- if (isCustomProxy) {
208
- if (instance?.config?.debug) {
209
- console.log(`[lazy][materialize] detected custom Proxy for ${key}, replacing placeholder to avoid memory leaks (${typeof value})`);
210
- }
211
-
212
- try {
213
-
214
- const testAccess = value && typeof value === "object";
215
- if (!testAccess) {
216
- console.warn(`[lazy][materialize] Custom proxy for ${key} failed basic validation, but continuing with replacement`);
217
- }
218
- } catch (error) {
219
- console.warn(`[lazy][materialize] Custom proxy for ${key} validation failed:`, error.message, "but continuing with replacement");
220
- }
221
-
222
- }
223
-
224
-
225
- let finalKey = key;
226
- if (typeof value === "function" && value.name && value.name.toLowerCase() === key.toLowerCase() && value.name !== key) {
227
-
228
- finalKey = value.name;
229
-
230
-
231
- if (finalKey !== key && key in parent) {
232
- try {
233
- delete parent[key];
234
- } catch {
235
-
236
- }
237
- }
238
- }
239
-
240
- try {
241
-
242
- if (
243
- parent[finalKey] &&
244
- typeof parent[finalKey] === "object" &&
245
- typeof value === "object" &&
246
- !Array.isArray(parent[finalKey]) &&
247
- !Array.isArray(value)
248
- ) {
249
- if (instance?.config?.debug) {
250
- console.log(`[lazy] Merging subdirectory '${finalKey}' - existing:`, Object.keys(parent[finalKey]), "new:", Object.keys(value));
251
- }
252
-
253
- Object.assign(parent[finalKey], value);
254
- } else {
255
- Object.defineProperty(parent, finalKey, { value, writable: true, enumerable: true, configurable: true });
256
- }
257
- } catch {
258
-
259
- if (
260
- parent[finalKey] &&
261
- typeof parent[finalKey] === "object" &&
262
- typeof value === "object" &&
263
- !Array.isArray(parent[finalKey]) &&
264
- !Array.isArray(value)
265
- ) {
266
- if (instance?.config?.debug) {
267
- console.log(
268
- `[lazy] Merging subdirectory '${finalKey}' (fallback) - existing:`,
269
- Object.keys(parent[finalKey]),
270
- "new:",
271
- Object.keys(value)
272
- );
273
- }
274
- Object.assign(parent[finalKey], value);
275
- } else {
276
- parent[finalKey] = value;
277
- }
278
- }
279
- if (instance?.config?.debug) {
280
- console.log(`[lazy][materialize] replaced ${key}${finalKey !== key ? ` -> ${finalKey}` : ""} (${typeof value})`);
281
- }
282
- if (depth === 1 && typeof instance?.updateBoundApiProperty === "function") {
283
- instance.updateBoundApiProperty(finalKey, parent[finalKey]);
284
- }
285
- }
286
-
287
-
288
- function createFolderProxy({ subDirPath, key, parent, instance, depth, maxDepth, pathParts, runWithCtx, existingContent }) {
289
- let materialized = null;
290
- let inFlight = null;
291
-
292
-
293
- async function _materialize() {
294
- if (materialized) return materialized;
295
- if (inFlight) return inFlight;
296
-
297
-
298
- const lazy_materializeCategory = async () => {
299
- const value = await instance._buildCategory(subDirPath, {
300
- currentDepth: depth,
301
- maxDepth,
302
- mode: "lazy",
303
- existingApi: parent,
304
- subdirHandler: ({ subDirPath: nestedPath, key: nestedKey, categoryModules, currentDepth: cd, maxDepth: md }) =>
305
- createFolderProxy({
306
- subDirPath: nestedPath,
307
- key: nestedKey,
308
- parent: categoryModules,
309
- instance,
310
- depth: cd + 1,
311
- maxDepth: md,
312
- pathParts: [...pathParts, nestedKey],
313
- runWithCtx
314
- })
315
- });
316
- materialized = value;
317
-
318
-
319
- if (
320
- existingContent &&
321
- typeof existingContent === "object" &&
322
- typeof materialized === "object" &&
323
- !Array.isArray(existingContent) &&
324
- !Array.isArray(materialized)
325
- ) {
326
- if (instance?.config?.debug) {
327
- console.log(
328
- `[lazy] Merging existing content with materialized subdirectory '${key}' - existing:`,
329
- Object.keys(existingContent),
330
- "new:",
331
- Object.keys(materialized)
332
- );
333
- }
334
-
335
- materialized = Object.assign({}, existingContent, materialized);
336
- } else if (existingContent && !materialized) {
337
-
338
- materialized = existingContent;
339
- }
340
- if (instance?.config?.debug) {
341
- try {
342
- const infoKeys = materialized && typeof materialized === "object" ? Object.keys(materialized) : [];
343
- const todayType = materialized && materialized.today ? typeof materialized.today : "n/a";
344
- console.log(
345
- `[lazy][debug] materialized key='${key}' path='${subDirPath}' type=${typeof materialized} keys=${JSON.stringify(
346
- infoKeys
347
- )} todayType=${todayType}`
348
- );
349
- } catch {
350
-
351
- }
352
- }
353
-
354
- replacePlaceholder(parent, key, placeholder, materialized, instance, depth);
355
-
356
- if (instance.config.debug) {
357
- try {
358
- const type = typeof materialized;
359
- const keys = type === "object" && materialized ? Object.keys(materialized) : [];
360
- console.log(
361
- `[lazy][debug] materialized '${pathParts.join("/")}' -> type=${type} keys=${JSON.stringify(keys)} parentHas=${
362
- parent[key] === materialized
363
- }`
364
- );
365
- } catch {
366
-
367
- }
368
- }
369
- placeholder.__materialized = materialized;
370
- return materialized;
371
- };
372
- inFlight = lazy_materializeCategory();
373
- try {
374
- return await inFlight;
375
- } finally {
376
- inFlight = null;
377
- }
378
- }
379
-
380
-
381
- async function lazy_invoke(thisArg, ...args) {
382
- const value = await _materialize();
383
- if (typeof value === "function") {
384
-
385
- const ctx = instance.boundapi?.__ctx;
386
- if (ctx) {
387
- return runWithCtx(ctx, value, thisArg, args);
388
- } else {
389
- return value.apply(thisArg, args);
390
- }
391
- }
392
-
393
-
394
- return value;
395
- }
396
-
397
-
398
-
399
- function lazy_lazyTarget(...args) {
400
- return lazy_invoke(this, ...args);
401
- }
402
-
403
- Object.defineProperty(lazy_lazyTarget, "__materialized", { value: null, writable: true, enumerable: false, configurable: true });
404
-
405
- Object.defineProperty(lazy_lazyTarget, "__slothletPath", { value: subDirPath, enumerable: false, configurable: true });
406
-
407
- Object.defineProperty(lazy_lazyTarget, "_materialize", { value: _materialize, enumerable: false, configurable: true, writable: true });
408
-
409
-
410
- try {
411
- Object.defineProperty(lazy_lazyTarget, "name", { value: `lazyFolder_${key}`, configurable: true });
412
- } catch {
413
-
414
- }
415
-
416
- const placeholder = new Proxy(lazy_lazyTarget, {
417
- apply(_t, thisArg, args) {
418
- return lazy_invoke(thisArg, ...args);
419
- },
420
- get(_t, prop, _) {
421
- if (prop === "__materialized") return materialized;
422
- if (prop === "_materialize") return _materialize;
423
- if (prop === "then") return undefined;
424
-
425
- if (prop === "__slothletPath") {
426
- return pathParts.length > 0 ? pathParts.join(".") : undefined;
427
- }
428
-
429
- if (prop === "__metadata" || prop === "__sourceFolder") {
430
-
431
- return Reflect.get(_t, prop);
432
- }
433
-
434
- if (materialized) {
435
- if (materialized && (typeof materialized === "object" || typeof materialized === "function")) return materialized[prop];
436
- return undefined;
437
- }
438
-
439
- if (!inFlight) inFlight = _materialize();
440
-
441
- const apiPath = pathParts.length > 0 ? `${pathParts.join(".")}.${String(prop)}` : String(prop);
442
-
443
-
444
-
445
-
446
- const propertyProxy = new Proxy(
447
-
448
- function lazy_propertyAccessor(...args) {
449
- return inFlight.then(
450
-
451
- function lazy_handleResolvedValue(resolved) {
452
- const value = resolved ? resolved[prop] : undefined;
453
- if (typeof value === "function") {
454
-
455
- return value.apply(this, args);
456
- }
457
- return value;
458
- }
459
- );
460
- },
461
- {
462
-
463
- get(target, subProp) {
464
- if (subProp === "name") return `lazy_${prop}`;
465
- if (subProp === "length") return 0;
466
-
467
- if (subProp === "__slothletPath") {
468
- return pathParts.length > 0 ? `${pathParts.join(".")}.${String(prop)}` : String(prop);
469
- }
470
-
471
- return new Proxy(
472
- function lazy_deepPropertyAccessor() {},
473
- {
474
- get(target, nextProp) {
475
- if (nextProp === "name") return `lazy_${prop}_${subProp}`;
476
- if (nextProp === "length") return 0;
477
-
478
- return new Proxy(function lazy_deeperPropertyAccessor() {}, {
479
- apply(target, thisArg, args) {
480
- if (materialized) {
481
- const value = materialized[prop];
482
- const subValue = value ? value[subProp] : undefined;
483
- if (subValue && typeof subValue[nextProp] === "function") {
484
-
485
- return subValue[nextProp].apply(thisArg, args);
486
- }
487
- return subValue ? subValue[nextProp] : undefined;
488
- }
489
-
490
- if (!inFlight) inFlight = _materialize();
491
- return inFlight.then(function lazy_handleDeeperResolvedValue(resolved) {
492
- const value = resolved ? resolved[prop] : undefined;
493
- const subValue = value ? value[subProp] : undefined;
494
- if (subValue && typeof subValue[nextProp] === "function") {
495
- const ctx = instance.boundapi?.__ctx;
496
- if (ctx) {
497
- return runWithCtx(ctx, subValue[nextProp], thisArg, args);
498
- } else {
499
- return subValue[nextProp].apply(thisArg, args);
500
- }
501
- }
502
- return subValue ? subValue[nextProp] : undefined;
503
- });
504
- }
505
- });
506
- },
507
- apply(target, thisArg, args) {
508
-
509
- if (materialized) {
510
- const value = materialized[prop];
511
- if (value && typeof value[subProp] === "function") {
512
-
513
- return value[subProp].apply(thisArg, args);
514
- }
515
- return value ? value[subProp] : undefined;
516
- }
517
-
518
-
519
- if (!inFlight) inFlight = _materialize();
520
- return inFlight.then(
521
-
522
- function lazy_handleDeepResolvedValue(resolved) {
523
- const value = resolved ? resolved[prop] : undefined;
524
- if (value && typeof value[subProp] === "function") {
525
-
526
- const ctx = instance.boundapi?.__ctx;
527
- if (ctx) {
528
- return runWithCtx(ctx, value[subProp], thisArg, args);
529
- } else {
530
- return value[subProp].apply(thisArg, args);
531
- }
532
- }
533
- return value ? value[subProp] : undefined;
534
- }
535
- );
536
- }
537
- }
538
- );
539
- }
540
- }
541
- );
542
-
543
-
544
- Object.defineProperty(propertyProxy, "__slothletPath", {
545
- value: apiPath,
546
- writable: false,
547
- enumerable: false,
548
- configurable: true
549
- });
550
-
551
- return propertyProxy;
552
- },
553
- has(_t, prop) {
554
- if (materialized && (typeof materialized === "object" || typeof materialized === "function")) return prop in materialized;
555
- return false;
556
- },
557
- ownKeys() {
558
- const baseKeys = Reflect.ownKeys(lazy_lazyTarget);
559
- if (!materialized) return baseKeys;
560
- if (typeof materialized === "object" || typeof materialized === "function") {
561
- const matKeys = Reflect.ownKeys(materialized);
562
- return Array.from(new Set([...baseKeys, ...matKeys]));
563
- }
564
- return baseKeys;
565
- },
566
- getOwnPropertyDescriptor(_t, prop) {
567
- if (prop === "_materialize") {
568
-
569
- return Reflect.getOwnPropertyDescriptor(lazy_lazyTarget, "_materialize");
570
- }
571
- if (prop === "__materialized") {
572
- return { configurable: true, enumerable: false, writable: true, value: materialized };
573
- }
574
-
575
- if (prop === "__metadata" || prop === "__sourceFolder") {
576
- return Reflect.getOwnPropertyDescriptor(lazy_lazyTarget, prop);
577
- }
578
- if (prop === "prototype") {
579
-
580
- return Object.getOwnPropertyDescriptor(lazy_lazyTarget, "prototype");
581
- }
582
- if (materialized && (typeof materialized === "object" || typeof materialized === "function")) {
583
- const d = Object.getOwnPropertyDescriptor(materialized, prop);
584
- if (d) return { ...d, configurable: true };
585
- }
586
-
587
- const td = Object.getOwnPropertyDescriptor(lazy_lazyTarget, prop);
588
- if (td) return { ...td, configurable: true };
589
- return undefined;
590
- }
591
- });
592
-
593
- return placeholder;
594
- }