@cldmv/slothlet 2.7.1 → 2.9.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/AGENT-USAGE.md +1 -1
- package/README.md +253 -1475
- package/dist/lib/helpers/als-eventemitter.mjs +4 -5
- package/dist/lib/helpers/api_builder/add_api.mjs +237 -0
- package/dist/lib/helpers/api_builder/analysis.mjs +522 -0
- package/dist/lib/helpers/api_builder/construction.mjs +457 -0
- package/dist/lib/helpers/api_builder/decisions.mjs +737 -0
- package/dist/lib/helpers/api_builder.mjs +16 -1567
- package/dist/lib/helpers/utilities.mjs +121 -0
- package/dist/lib/runtime/runtime-asynclocalstorage.mjs +44 -17
- package/dist/lib/runtime/runtime-livebindings.mjs +18 -3
- package/dist/lib/runtime/runtime.mjs +3 -3
- package/dist/slothlet.mjs +197 -547
- package/docs/API-RULES-CONDITIONS.md +508 -0
- package/{API-RULES.md → docs/API-RULES.md} +127 -72
- package/index.cjs +2 -1
- package/index.mjs +2 -1
- package/package.json +11 -9
- package/types/dist/lib/helpers/als-eventemitter.d.mts.map +1 -1
- package/types/dist/lib/helpers/api_builder/add_api.d.mts +60 -0
- package/types/dist/lib/helpers/api_builder/add_api.d.mts.map +1 -0
- package/types/dist/lib/helpers/api_builder/analysis.d.mts +189 -0
- package/types/dist/lib/helpers/api_builder/analysis.d.mts.map +1 -0
- package/types/dist/lib/helpers/api_builder/construction.d.mts +107 -0
- package/types/dist/lib/helpers/api_builder/construction.d.mts.map +1 -0
- package/types/dist/lib/helpers/api_builder/decisions.d.mts +213 -0
- package/types/dist/lib/helpers/api_builder/decisions.d.mts.map +1 -0
- package/types/dist/lib/helpers/api_builder.d.mts +5 -448
- package/types/dist/lib/helpers/api_builder.d.mts.map +1 -1
- package/types/dist/lib/helpers/utilities.d.mts +120 -0
- package/types/dist/lib/helpers/utilities.d.mts.map +1 -0
- package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts +7 -0
- package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts.map +1 -1
- package/types/dist/lib/runtime/runtime-livebindings.d.mts +8 -0
- package/types/dist/lib/runtime/runtime-livebindings.d.mts.map +1 -1
- package/types/dist/slothlet.d.mts +23 -13
- package/types/dist/slothlet.d.mts.map +1 -1
- package/types/index.d.mts +0 -1
- package/API-RULES-CONDITIONS.md +0 -367
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2025 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
|
+
import fs from "node:fs/promises";
|
|
22
|
+
import path from "node:path";
|
|
23
|
+
import { types as utilTypes } from "node:util";
|
|
24
|
+
import { sanitizePathName } from "@cldmv/slothlet/helpers/sanitize";
|
|
25
|
+
import { multidefault_analyzeModules } from "@cldmv/slothlet/helpers/multidefault";
|
|
26
|
+
import { analyzeModule, processModuleFromAnalysis } from "@cldmv/slothlet/helpers/api_builder/analysis";
|
|
27
|
+
import { processModuleForAPI, buildCategoryDecisions } from "@cldmv/slothlet/helpers/api_builder/decisions";
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
export function toapiPathKey(name, sanitizeConfig = {}) {
|
|
35
|
+
return sanitizePathName(name, sanitizeConfig);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
export function shouldIncludeFile(entry) {
|
|
40
|
+
|
|
41
|
+
if (!entry.isFile()) return false;
|
|
42
|
+
|
|
43
|
+
if (!(entry.name.endsWith(".mjs") || entry.name.endsWith(".cjs") || entry.name.endsWith(".js"))) return false;
|
|
44
|
+
|
|
45
|
+
if (entry.name.startsWith(".")) return false;
|
|
46
|
+
|
|
47
|
+
if (entry.name.startsWith("__slothlet_")) return false;
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
export async function buildCategoryStructure(categoryPath, options = {}) {
|
|
57
|
+
const { currentDepth = 0, maxDepth = Infinity, mode = "eager", subdirHandler, instance } = options;
|
|
58
|
+
|
|
59
|
+
if (!instance || typeof instance._toapiPathKey !== "function") {
|
|
60
|
+
throw new Error("buildCategoryStructure requires a valid slothlet instance");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
const decisions = await buildCategoryDecisions(categoryPath, {
|
|
65
|
+
currentDepth,
|
|
66
|
+
maxDepth,
|
|
67
|
+
mode,
|
|
68
|
+
subdirHandler,
|
|
69
|
+
instance
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
if (decisions.type === "single-file") {
|
|
74
|
+
const { singleFile } = decisions;
|
|
75
|
+
const { mod, moduleName } = singleFile;
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
if (decisions.shouldFlatten) {
|
|
79
|
+
switch (decisions.flattenType) {
|
|
80
|
+
case "function-folder-match":
|
|
81
|
+
case "default-function":
|
|
82
|
+
try {
|
|
83
|
+
Object.defineProperty(mod, "name", { value: decisions.preferredName, configurable: true });
|
|
84
|
+
} catch {
|
|
85
|
+
|
|
86
|
+
}
|
|
87
|
+
return mod;
|
|
88
|
+
|
|
89
|
+
case "default-export-flatten":
|
|
90
|
+
|
|
91
|
+
return mod;
|
|
92
|
+
|
|
93
|
+
case "object-auto-flatten":
|
|
94
|
+
|
|
95
|
+
return mod[decisions.preferredName];
|
|
96
|
+
|
|
97
|
+
case "parent-level-flatten": {
|
|
98
|
+
|
|
99
|
+
const exportValue = mod[Object.keys(mod).filter((k) => k !== "default")[0]];
|
|
100
|
+
return { [decisions.preferredName]: exportValue };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
case "filename-folder-match-flatten":
|
|
104
|
+
|
|
105
|
+
return mod;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
if (decisions.preferredName && decisions.preferredName !== moduleName) {
|
|
111
|
+
return { [decisions.preferredName]: mod };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
return { [moduleName]: mod };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
const categoryModules = {};
|
|
120
|
+
const { categoryName, processedModules, subdirectoryDecisions } = decisions;
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
for (const moduleDecision of processedModules) {
|
|
124
|
+
const { moduleName, mod, type, apiPathKey, shouldFlatten, flattenType, specialHandling, processedExports } = moduleDecision;
|
|
125
|
+
|
|
126
|
+
if (specialHandling === "category-merge") {
|
|
127
|
+
|
|
128
|
+
if (Object.prototype.hasOwnProperty.call(mod, categoryName) && typeof mod[categoryName] === "object" && mod[categoryName] !== null) {
|
|
129
|
+
Object.assign(categoryModules, mod[categoryName]);
|
|
130
|
+
for (const [key, value] of Object.entries(mod)) {
|
|
131
|
+
if (key !== categoryName) categoryModules[instance._toapiPathKey(key)] = value;
|
|
132
|
+
}
|
|
133
|
+
} else {
|
|
134
|
+
Object.assign(categoryModules, mod);
|
|
135
|
+
}
|
|
136
|
+
} else if (type === "function") {
|
|
137
|
+
|
|
138
|
+
if (specialHandling === "multi-default-filename") {
|
|
139
|
+
try {
|
|
140
|
+
Object.defineProperty(mod, "name", { value: moduleName, configurable: true });
|
|
141
|
+
} catch {
|
|
142
|
+
|
|
143
|
+
}
|
|
144
|
+
categoryModules[moduleName] = mod;
|
|
145
|
+
} else if (specialHandling === "prefer-function-name") {
|
|
146
|
+
categoryModules[apiPathKey] = mod;
|
|
147
|
+
} else {
|
|
148
|
+
|
|
149
|
+
categoryModules[apiPathKey] = mod;
|
|
150
|
+
}
|
|
151
|
+
} else if (type === "self-referential") {
|
|
152
|
+
|
|
153
|
+
categoryModules[moduleName] = mod[moduleName] || mod;
|
|
154
|
+
} else if (type === "object") {
|
|
155
|
+
|
|
156
|
+
if (specialHandling === "preferred-export-names") {
|
|
157
|
+
Object.assign(categoryModules, processedExports);
|
|
158
|
+
} else if (shouldFlatten) {
|
|
159
|
+
switch (flattenType) {
|
|
160
|
+
case "single-default-object": {
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
let flattened;
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
const defaultExport = mod.default;
|
|
167
|
+
const hasNamedExports = Object.keys(mod).some((k) => k !== "default");
|
|
168
|
+
|
|
169
|
+
if (hasNamedExports && defaultExport && typeof defaultExport === "object") {
|
|
170
|
+
|
|
171
|
+
const isProxy = utilTypes?.isProxy?.(defaultExport) ?? false;
|
|
172
|
+
|
|
173
|
+
if (isProxy) {
|
|
174
|
+
|
|
175
|
+
flattened = defaultExport;
|
|
176
|
+
let assignmentFailed = false;
|
|
177
|
+
|
|
178
|
+
const failedMap = new Map();
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
for (const [key, value] of Object.entries(mod)) {
|
|
182
|
+
if (key !== "default") {
|
|
183
|
+
try {
|
|
184
|
+
flattened[key] = value;
|
|
185
|
+
} catch (e) {
|
|
186
|
+
|
|
187
|
+
assignmentFailed = true;
|
|
188
|
+
failedMap.set(key, value);
|
|
189
|
+
if (instance.config?.debug) {
|
|
190
|
+
console.warn(
|
|
191
|
+
`Could not assign '${key}' to proxy object in module '${moduleName}' at '${categoryPath}':`,
|
|
192
|
+
e.message
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
if (assignmentFailed) {
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
const originalProxy = flattened;
|
|
211
|
+
flattened = new Proxy(originalProxy, {
|
|
212
|
+
get(target, prop, receiver) {
|
|
213
|
+
|
|
214
|
+
if (failedMap.has(prop)) return failedMap.get(prop);
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
return Reflect.get(target, prop, receiver);
|
|
218
|
+
},
|
|
219
|
+
has(target, prop) {
|
|
220
|
+
|
|
221
|
+
if (failedMap.has(prop)) return true;
|
|
222
|
+
return Reflect.has(target, prop);
|
|
223
|
+
},
|
|
224
|
+
ownKeys(target) {
|
|
225
|
+
const originalKeys = Reflect.ownKeys(target);
|
|
226
|
+
const failedKeys = Array.from(failedMap.keys());
|
|
227
|
+
return [...new Set([...originalKeys, ...failedKeys])];
|
|
228
|
+
},
|
|
229
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
230
|
+
if (failedMap.has(prop)) {
|
|
231
|
+
return { configurable: true, enumerable: true, value: failedMap.get(prop) };
|
|
232
|
+
}
|
|
233
|
+
return Reflect.getOwnPropertyDescriptor(target, prop);
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
} else {
|
|
238
|
+
|
|
239
|
+
flattened = { ...defaultExport };
|
|
240
|
+
for (const [key, value] of Object.entries(mod)) {
|
|
241
|
+
if (key !== "default") {
|
|
242
|
+
flattened[key] = value;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
} else {
|
|
247
|
+
|
|
248
|
+
flattened = defaultExport;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
categoryModules[apiPathKey] = flattened;
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
case "multi-default-no-default": {
|
|
255
|
+
|
|
256
|
+
const moduleKeys = Object.keys(mod).filter((k) => k !== "default");
|
|
257
|
+
for (const key of moduleKeys) {
|
|
258
|
+
categoryModules[key] = mod[key];
|
|
259
|
+
}
|
|
260
|
+
break;
|
|
261
|
+
}
|
|
262
|
+
case "single-named-export-match":
|
|
263
|
+
|
|
264
|
+
categoryModules[apiPathKey] = mod[apiPathKey];
|
|
265
|
+
break;
|
|
266
|
+
case "category-name-match-flatten": {
|
|
267
|
+
|
|
268
|
+
const moduleKeys = Object.keys(mod).filter((k) => k !== "default");
|
|
269
|
+
for (const key of moduleKeys) {
|
|
270
|
+
categoryModules[key] = mod[key];
|
|
271
|
+
}
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
} else {
|
|
276
|
+
|
|
277
|
+
categoryModules[apiPathKey] = mod;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
for (const subDirDecision of subdirectoryDecisions) {
|
|
284
|
+
if (subDirDecision.shouldRecurse) {
|
|
285
|
+
const { name, path: subDirPath, apiPathKey } = subDirDecision;
|
|
286
|
+
let subModule;
|
|
287
|
+
|
|
288
|
+
if (mode === "lazy" && typeof subdirHandler === "function") {
|
|
289
|
+
subModule = subdirHandler({
|
|
290
|
+
subDirEntry: { name },
|
|
291
|
+
subDirPath,
|
|
292
|
+
key: apiPathKey,
|
|
293
|
+
categoryModules,
|
|
294
|
+
currentDepth,
|
|
295
|
+
maxDepth
|
|
296
|
+
});
|
|
297
|
+
} else {
|
|
298
|
+
subModule = await buildCategoryStructure(subDirPath, {
|
|
299
|
+
currentDepth: currentDepth + 1,
|
|
300
|
+
maxDepth,
|
|
301
|
+
mode: "eager",
|
|
302
|
+
instance
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
if (
|
|
309
|
+
typeof subModule === "function" &&
|
|
310
|
+
subModule.name &&
|
|
311
|
+
subModule.name.toLowerCase() === apiPathKey.toLowerCase() &&
|
|
312
|
+
subModule.name !== apiPathKey
|
|
313
|
+
) {
|
|
314
|
+
|
|
315
|
+
categoryModules[subModule.name] = subModule;
|
|
316
|
+
} else {
|
|
317
|
+
categoryModules[apiPathKey] = subModule;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
const keys = Object.keys(categoryModules);
|
|
324
|
+
if (keys.length === 1) {
|
|
325
|
+
const singleKey = keys[0];
|
|
326
|
+
if (singleKey === categoryName) {
|
|
327
|
+
const single = categoryModules[singleKey];
|
|
328
|
+
if (typeof single === "function") {
|
|
329
|
+
if (single.name !== categoryName) {
|
|
330
|
+
try {
|
|
331
|
+
Object.defineProperty(single, "name", { value: categoryName, configurable: true });
|
|
332
|
+
} catch {
|
|
333
|
+
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
return single;
|
|
337
|
+
} else if (single && typeof single === "object" && !Array.isArray(single)) {
|
|
338
|
+
return single;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
return categoryModules;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
export async function buildRootAPI(dir, options = {}) {
|
|
352
|
+
const { lazy = false, maxDepth = Infinity, instance } = options;
|
|
353
|
+
|
|
354
|
+
if (!instance || typeof instance._shouldIncludeFile !== "function" || typeof instance._loadCategory !== "function") {
|
|
355
|
+
throw new Error("buildRootAPI requires a valid slothlet instance");
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const debug = instance.config?.debug || false;
|
|
359
|
+
|
|
360
|
+
if (debug) {
|
|
361
|
+
console.log(`[DEBUG] buildRootAPI called with dir: ${dir}, lazy: ${lazy}, maxDepth: ${maxDepth}`);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
365
|
+
const api = {};
|
|
366
|
+
let rootDefaultFunction = null;
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
const moduleFiles = entries.filter((e) => instance._shouldIncludeFile(e));
|
|
370
|
+
|
|
371
|
+
if (moduleFiles.length > 0) {
|
|
372
|
+
|
|
373
|
+
const analysis = await multidefault_analyzeModules(moduleFiles, dir, { debug, instance });
|
|
374
|
+
const { hasMultipleDefaultExports, selfReferentialFiles } = analysis;
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
for (const entry of moduleFiles) {
|
|
378
|
+
const ext = path.extname(entry.name);
|
|
379
|
+
const fileName = path.basename(entry.name, ext);
|
|
380
|
+
const apiPathKey = instance._toapiPathKey(fileName);
|
|
381
|
+
|
|
382
|
+
const analysis = await analyzeModule(path.join(dir, entry.name), {
|
|
383
|
+
debug,
|
|
384
|
+
instance
|
|
385
|
+
});
|
|
386
|
+
const mod = processModuleFromAnalysis(analysis, {
|
|
387
|
+
debug,
|
|
388
|
+
instance
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
processModuleForAPI({
|
|
393
|
+
mod,
|
|
394
|
+
fileName,
|
|
395
|
+
apiPathKey,
|
|
396
|
+
hasMultipleDefaultExports,
|
|
397
|
+
isSelfReferential: selfReferentialFiles.has(fileName),
|
|
398
|
+
api,
|
|
399
|
+
getRootDefault: () => rootDefaultFunction,
|
|
400
|
+
setRootDefault: (fn) => {
|
|
401
|
+
rootDefaultFunction = fn;
|
|
402
|
+
},
|
|
403
|
+
context: {
|
|
404
|
+
debug,
|
|
405
|
+
mode: "root",
|
|
406
|
+
totalModules: moduleFiles.length
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
for (const entry of entries) {
|
|
414
|
+
if (entry.isDirectory() && !entry.name.startsWith(".")) {
|
|
415
|
+
const categoryPath = path.join(dir, entry.name);
|
|
416
|
+
|
|
417
|
+
if (lazy) {
|
|
418
|
+
|
|
419
|
+
api[instance._toapiPathKey(entry.name)] = await instance._loadCategory(categoryPath, 0, maxDepth);
|
|
420
|
+
} else {
|
|
421
|
+
|
|
422
|
+
api[instance._toapiPathKey(entry.name)] = await buildCategoryStructure(categoryPath, {
|
|
423
|
+
currentDepth: 1,
|
|
424
|
+
maxDepth,
|
|
425
|
+
mode: "eager",
|
|
426
|
+
instance
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
let finalApi;
|
|
434
|
+
if (debug) {
|
|
435
|
+
console.log(`[DEBUG] Final assembly: rootDefaultFunction=${!!rootDefaultFunction}`);
|
|
436
|
+
console.log(`[DEBUG] API object keys before final assembly:`, Object.keys(api));
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
if (rootDefaultFunction) {
|
|
440
|
+
|
|
441
|
+
Object.assign(rootDefaultFunction, api);
|
|
442
|
+
finalApi = rootDefaultFunction;
|
|
443
|
+
|
|
444
|
+
if (debug) {
|
|
445
|
+
console.log(`[DEBUG] Applied root contributor pattern - final API is function`);
|
|
446
|
+
}
|
|
447
|
+
} else {
|
|
448
|
+
|
|
449
|
+
finalApi = api;
|
|
450
|
+
|
|
451
|
+
if (debug) {
|
|
452
|
+
console.log(`[DEBUG] No root function - final API is object`);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
return finalApi;
|
|
457
|
+
}
|