@cldmv/slothlet 3.2.3 → 3.3.2

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 (138) hide show
  1. package/README.md +22 -9
  2. package/REFERENCE.md +23 -0
  3. package/dist/lib/builders/api-assignment.mjs +1 -589
  4. package/dist/lib/builders/api_builder.mjs +1 -1155
  5. package/dist/lib/builders/builder.mjs +1 -78
  6. package/dist/lib/builders/modes-processor.mjs +1 -1800
  7. package/dist/lib/errors.mjs +9 -211
  8. package/dist/lib/factories/component-base.mjs +1 -80
  9. package/dist/lib/factories/context.mjs +1 -22
  10. package/dist/lib/handlers/api-cache-manager.mjs +1 -200
  11. package/dist/lib/handlers/api-manager.mjs +1 -2513
  12. package/dist/lib/handlers/context-async.mjs +1 -168
  13. package/dist/lib/handlers/context-live.mjs +1 -168
  14. package/dist/lib/handlers/hook-manager.mjs +1 -773
  15. package/dist/lib/handlers/lifecycle-token.mjs +1 -28
  16. package/dist/lib/handlers/lifecycle.mjs +1 -115
  17. package/dist/lib/handlers/materialize-manager.mjs +1 -48
  18. package/dist/lib/handlers/metadata.mjs +1 -501
  19. package/dist/lib/handlers/ownership.mjs +1 -322
  20. package/dist/lib/handlers/permission-manager.mjs +17 -0
  21. package/dist/lib/handlers/unified-wrapper.mjs +1 -3042
  22. package/dist/lib/handlers/version-manager.mjs +1 -885
  23. package/dist/lib/helpers/class-instance-wrapper.mjs +1 -109
  24. package/dist/lib/helpers/config.mjs +1 -355
  25. package/dist/lib/helpers/eventemitter-context.mjs +1 -349
  26. package/dist/lib/helpers/hint-detector.mjs +1 -47
  27. package/dist/lib/helpers/modes-utils.mjs +1 -37
  28. package/dist/lib/helpers/pattern-matcher.mjs +17 -0
  29. package/dist/lib/helpers/resolve-from-caller.mjs +1 -169
  30. package/dist/lib/helpers/sanitize.mjs +1 -340
  31. package/dist/lib/helpers/utilities.mjs +1 -70
  32. package/dist/lib/i18n/languages/de-de.json +21 -1
  33. package/dist/lib/i18n/languages/en-gb.json +21 -1
  34. package/dist/lib/i18n/languages/en-us.json +21 -1
  35. package/dist/lib/i18n/languages/es-mx.json +21 -1
  36. package/dist/lib/i18n/languages/fr-fr.json +21 -1
  37. package/dist/lib/i18n/languages/hi-in.json +21 -1
  38. package/dist/lib/i18n/languages/ja-jp.json +21 -1
  39. package/dist/lib/i18n/languages/ko-kr.json +21 -1
  40. package/dist/lib/i18n/languages/pt-br.json +21 -1
  41. package/dist/lib/i18n/languages/ru-ru.json +21 -1
  42. package/dist/lib/i18n/languages/zh-cn.json +21 -1
  43. package/dist/lib/i18n/translations.mjs +1 -126
  44. package/dist/lib/modes/eager.mjs +1 -59
  45. package/dist/lib/modes/lazy.mjs +1 -81
  46. package/dist/lib/processors/flatten.mjs +1 -437
  47. package/dist/lib/processors/loader.mjs +1 -339
  48. package/dist/lib/processors/type-generator.mjs +1 -275
  49. package/dist/lib/processors/typescript.mjs +1 -172
  50. package/dist/lib/runtime/runtime-asynclocalstorage.mjs +1 -113
  51. package/dist/lib/runtime/runtime-livebindings.mjs +1 -78
  52. package/dist/lib/runtime/runtime.mjs +1 -102
  53. package/dist/slothlet.mjs +1 -808
  54. package/package.json +37 -31
  55. package/types/dist/lib/builders/api-assignment.d.mts +3 -92
  56. package/types/dist/lib/builders/api-assignment.d.mts.map +1 -1
  57. package/types/dist/lib/builders/api_builder.d.mts +102 -91
  58. package/types/dist/lib/builders/api_builder.d.mts.map +1 -1
  59. package/types/dist/lib/builders/builder.d.mts +1 -55
  60. package/types/dist/lib/builders/builder.d.mts.map +1 -1
  61. package/types/dist/lib/builders/modes-processor.d.mts +3 -27
  62. package/types/dist/lib/builders/modes-processor.d.mts.map +1 -1
  63. package/types/dist/lib/errors.d.mts +19 -109
  64. package/types/dist/lib/errors.d.mts.map +1 -1
  65. package/types/dist/lib/factories/component-base.d.mts +7 -177
  66. package/types/dist/lib/factories/component-base.d.mts.map +1 -1
  67. package/types/dist/lib/factories/context.d.mts +4 -22
  68. package/types/dist/lib/factories/context.d.mts.map +1 -1
  69. package/types/dist/lib/handlers/api-cache-manager.d.mts +20 -203
  70. package/types/dist/lib/handlers/api-cache-manager.d.mts.map +1 -1
  71. package/types/dist/lib/handlers/api-manager.d.mts +33 -408
  72. package/types/dist/lib/handlers/api-manager.d.mts.map +1 -1
  73. package/types/dist/lib/handlers/context-async.d.mts +23 -61
  74. package/types/dist/lib/handlers/context-async.d.mts.map +1 -1
  75. package/types/dist/lib/handlers/context-live.d.mts +22 -59
  76. package/types/dist/lib/handlers/context-live.d.mts.map +1 -1
  77. package/types/dist/lib/handlers/hook-manager.d.mts +46 -185
  78. package/types/dist/lib/handlers/hook-manager.d.mts.map +1 -1
  79. package/types/dist/lib/handlers/lifecycle-token.d.mts +3 -48
  80. package/types/dist/lib/handlers/lifecycle-token.d.mts.map +1 -1
  81. package/types/dist/lib/handlers/lifecycle.d.mts +5 -82
  82. package/types/dist/lib/handlers/lifecycle.d.mts.map +1 -1
  83. package/types/dist/lib/handlers/materialize-manager.d.mts +8 -70
  84. package/types/dist/lib/handlers/materialize-manager.d.mts.map +1 -1
  85. package/types/dist/lib/handlers/metadata.d.mts +17 -221
  86. package/types/dist/lib/handlers/metadata.d.mts.map +1 -1
  87. package/types/dist/lib/handlers/ownership.d.mts +44 -160
  88. package/types/dist/lib/handlers/ownership.d.mts.map +1 -1
  89. package/types/dist/lib/handlers/permission-manager.d.mts +47 -0
  90. package/types/dist/lib/handlers/permission-manager.d.mts.map +1 -0
  91. package/types/dist/lib/handlers/unified-wrapper.d.mts +26 -239
  92. package/types/dist/lib/handlers/unified-wrapper.d.mts.map +1 -1
  93. package/types/dist/lib/handlers/version-manager.d.mts +28 -225
  94. package/types/dist/lib/handlers/version-manager.d.mts.map +1 -1
  95. package/types/dist/lib/helpers/class-instance-wrapper.d.mts +2 -52
  96. package/types/dist/lib/helpers/class-instance-wrapper.d.mts.map +1 -1
  97. package/types/dist/lib/helpers/config.d.mts +125 -123
  98. package/types/dist/lib/helpers/config.d.mts.map +1 -1
  99. package/types/dist/lib/helpers/eventemitter-context.d.mts +3 -29
  100. package/types/dist/lib/helpers/eventemitter-context.d.mts.map +1 -1
  101. package/types/dist/lib/helpers/hint-detector.d.mts +2 -15
  102. package/types/dist/lib/helpers/hint-detector.d.mts.map +1 -1
  103. package/types/dist/lib/helpers/modes-utils.d.mts +3 -30
  104. package/types/dist/lib/helpers/modes-utils.d.mts.map +1 -1
  105. package/types/dist/lib/helpers/pattern-matcher.d.mts +4 -0
  106. package/types/dist/lib/helpers/pattern-matcher.d.mts.map +1 -0
  107. package/types/dist/lib/helpers/resolve-from-caller.d.mts +3 -27
  108. package/types/dist/lib/helpers/resolve-from-caller.d.mts.map +1 -1
  109. package/types/dist/lib/helpers/sanitize.d.mts +4 -92
  110. package/types/dist/lib/helpers/sanitize.d.mts.map +1 -1
  111. package/types/dist/lib/helpers/utilities.d.mts +4 -52
  112. package/types/dist/lib/helpers/utilities.d.mts.map +1 -1
  113. package/types/dist/lib/i18n/translations.d.mts +4 -37
  114. package/types/dist/lib/i18n/translations.d.mts.map +1 -1
  115. package/types/dist/lib/modes/eager.d.mts +8 -30
  116. package/types/dist/lib/modes/eager.d.mts.map +1 -1
  117. package/types/dist/lib/modes/lazy.d.mts +10 -43
  118. package/types/dist/lib/modes/lazy.d.mts.map +1 -1
  119. package/types/dist/lib/processors/flatten.d.mts +56 -107
  120. package/types/dist/lib/processors/flatten.d.mts.map +1 -1
  121. package/types/dist/lib/processors/loader.d.mts +6 -41
  122. package/types/dist/lib/processors/loader.d.mts.map +1 -1
  123. package/types/dist/lib/processors/type-generator.d.mts +2 -16
  124. package/types/dist/lib/processors/type-generator.d.mts.map +1 -1
  125. package/types/dist/lib/processors/typescript.d.mts +6 -53
  126. package/types/dist/lib/processors/typescript.d.mts.map +1 -1
  127. package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts +3 -71
  128. package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts.map +1 -1
  129. package/types/dist/lib/runtime/runtime-livebindings.d.mts +2 -37
  130. package/types/dist/lib/runtime/runtime-livebindings.d.mts.map +1 -1
  131. package/types/dist/lib/runtime/runtime.d.mts +3 -39
  132. package/types/dist/lib/runtime/runtime.d.mts.map +1 -1
  133. package/types/dist/slothlet.d.mts +3 -249
  134. package/types/dist/slothlet.d.mts.map +1 -1
  135. package/types/index.d.mts +36 -16
  136. package/types/index.d.mts.map +1 -0
  137. package/AGENT-USAGE.md +0 -736
  138. package/docs/API-RULES.md +0 -712
@@ -14,504 +14,4 @@
14
14
  limitations under the License.
15
15
  */
16
16
 
17
-
18
-
19
-
20
-
21
- import { ComponentBase } from "@cldmv/slothlet/factories/component-base";
22
- import { resolveWrapper } from "@cldmv/slothlet/handlers/unified-wrapper";
23
- import { verifyToken } from "@cldmv/slothlet/handlers/lifecycle-token";
24
-
25
-
26
- export class Metadata extends ComponentBase {
27
- static slothletProperty = "metadata";
28
-
29
-
30
- #secureMetadata = new WeakMap();
31
-
32
-
33
- #userMetadataStore = new Map();
34
- #globalUserMetadata = {};
35
-
36
- _instanceId = null;
37
-
38
-
39
- constructor(slothlet) {
40
- super(slothlet);
41
- this._instanceId = `metadata_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
42
- }
43
-
44
-
45
- #deepFreeze(obj) {
46
-
47
- if (Object.isFrozen(obj)) return obj;
48
-
49
-
50
- Object.freeze(obj);
51
-
52
-
53
- Object.getOwnPropertyNames(obj).forEach((prop) => {
54
- if (obj[prop] !== null && typeof obj[prop] === "object") {
55
- this.#deepFreeze(obj[prop]);
56
- }
57
- });
58
-
59
- return obj;
60
- }
61
-
62
-
63
- tagSystemMetadata(target, systemData, token) {
64
-
65
-
66
-
67
-
68
- if (!verifyToken(this.slothlet, token)) {
69
- throw new this.SlothletError("METADATA_LIFECYCLE_BYPASS", {}, null, { validationError: true });
70
- }
71
-
72
- if (!target) return;
73
-
74
-
75
- if (typeof target !== "object" && typeof target !== "function") {
76
- return;
77
- }
78
-
79
-
80
- let fullModuleID = systemData.moduleID;
81
- if (systemData.apiPath && systemData.moduleID) {
82
- const apiPathSlashes = systemData.apiPath.replace(/\./g, "/");
83
- fullModuleID = `${systemData.moduleID}:${apiPathSlashes}`;
84
- }
85
-
86
-
87
- let sourceFolder = systemData.sourceFolder;
88
- if (!sourceFolder && systemData.filePath) {
89
-
90
- const pathModule = this.slothlet.helpers.resolver.path;
91
- sourceFolder = pathModule.dirname(systemData.filePath);
92
- }
93
-
94
-
95
- const frozenSystem = Object.freeze({
96
- filePath: systemData.filePath,
97
- sourceFolder: sourceFolder,
98
- apiPath: systemData.apiPath,
99
- moduleID: fullModuleID,
100
- taggedAt: Date.now()
101
- });
102
-
103
- this.#secureMetadata.set(target, frozenSystem);
104
- }
105
-
106
-
107
- getSystemMetadata(target) {
108
- if (!target) return null;
109
-
110
-
111
- const actualTarget = resolveWrapper(target) ?? target;
112
-
113
-
114
- const systemData = this.#secureMetadata.get(actualTarget.____slothletInternal?.impl || actualTarget);
115
- return systemData || null;
116
- }
117
-
118
-
119
- getMetadata(target) {
120
- if (!target) return {};
121
-
122
-
123
- const actualTarget = resolveWrapper(target) ?? target;
124
-
125
-
126
-
127
- const systemData = this.#secureMetadata.get(actualTarget) || this.#secureMetadata.get(actualTarget.____slothletInternal?.impl) || {};
128
-
129
-
130
-
131
-
132
- const moduleID = systemData.moduleID || systemData.moduleID;
133
- const apiPath = systemData.apiPath;
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
- const collectMetadataFromParents = (path) => {
142
- const parts = path.split(".");
143
- const collected = {};
144
-
145
-
146
- for (let i = 1; i <= parts.length; i++) {
147
- const parentPath = parts.slice(0, i).join(".");
148
- const parentMeta = this.#userMetadataStore.get(parentPath);
149
- if (parentMeta?.metadata) {
150
- Object.assign(collected, parentMeta.metadata);
151
- }
152
- }
153
-
154
- return collected;
155
- };
156
-
157
- const userMetadataByModule = moduleID ? this.#userMetadataStore.get(moduleID) : null;
158
- const userMetadataByPath = apiPath ? collectMetadataFromParents(apiPath) : {};
159
-
160
-
161
- const userData = {
162
- ...userMetadataByPath,
163
- ...(userMetadataByModule?.metadata || {})
164
- };
165
-
166
-
167
- const combined = {
168
- ...this.#globalUserMetadata,
169
- ...userData,
170
- ...systemData
171
- };
172
-
173
-
174
- if (combined.metadata && typeof combined.metadata === "object") {
175
- const { metadata, ...rest } = combined;
176
- return this.#deepFreeze({
177
- ...rest,
178
- ...metadata
179
- });
180
- }
181
-
182
- return this.#deepFreeze(combined);
183
- }
184
-
185
-
186
- setGlobalMetadata(key, value) {
187
- this.#globalUserMetadata[key] = value;
188
- }
189
-
190
-
191
- setUserMetadata(target, key, value) {
192
- if (typeof target !== "function" && typeof target !== "object") {
193
- throw new this.SlothletError("INVALID_METADATA_TARGET", {
194
- target: typeof target,
195
- expected: "function or object"
196
- });
197
- }
198
-
199
-
200
- const actualTarget = resolveWrapper(target) ?? target;
201
-
202
-
203
-
204
-
205
- const systemData = this.#secureMetadata.get(actualTarget) || this.#secureMetadata.get(actualTarget.____slothletInternal?.impl) || {};
206
- const moduleID = systemData.moduleID;
207
-
208
- if (!moduleID) {
209
- throw new this.SlothletError("METADATA_NO_MODULE_ID", {}, null, { validationError: true });
210
- }
211
-
212
-
213
- let entry = this.#userMetadataStore.get(moduleID);
214
- if (!entry) {
215
- entry = { metadata: {}, apiPaths: new Set() };
216
- this.#userMetadataStore.set(moduleID, entry);
217
- }
218
-
219
-
220
- entry.metadata[key] = value;
221
-
222
-
223
-
224
-
225
- const apiPath = systemData.apiPath;
226
- if (apiPath) {
227
- let pathEntry = this.#userMetadataStore.get(apiPath);
228
- if (!pathEntry) {
229
- pathEntry = { metadata: {}, apiPaths: new Set() };
230
- this.#userMetadataStore.set(apiPath, pathEntry);
231
- }
232
- pathEntry.metadata[key] = value;
233
- pathEntry.apiPaths.add(apiPath);
234
- }
235
- }
236
-
237
-
238
- removeUserMetadata(target, key) {
239
- if (typeof target !== "function" && typeof target !== "object") {
240
- throw new this.SlothletError("INVALID_METADATA_TARGET", {
241
- target: typeof target,
242
- expected: "function or object"
243
- });
244
- }
245
-
246
-
247
- const actualTarget = resolveWrapper(target) ?? target;
248
-
249
-
250
- const systemData = this.#secureMetadata.get(actualTarget) || this.#secureMetadata.get(actualTarget.____slothletInternal?.impl) || {};
251
- const moduleID = systemData.moduleID;
252
- const apiPath = systemData.apiPath;
253
-
254
- if (!moduleID) return;
255
-
256
-
257
- const applyRemoval = (storeKey) => {
258
- const storeEntry = this.#userMetadataStore.get(storeKey);
259
- if (!storeEntry) return;
260
-
261
- if (key === undefined) {
262
- this.#userMetadataStore.delete(storeKey);
263
- } else if (Array.isArray(key)) {
264
- for (const k of key) {
265
- if (typeof k !== "string") {
266
- throw new this.SlothletError("INVALID_METADATA_KEY", {
267
- key: k,
268
- type: typeof k,
269
- expected: "string"
270
- });
271
- }
272
- delete storeEntry.metadata[k];
273
- }
274
- } else if (typeof key === "object" && key !== null) {
275
- for (const [metadataKey, nestedKeys] of Object.entries(key)) {
276
- if (!Array.isArray(nestedKeys)) {
277
- throw new this.SlothletError("INVALID_METADATA_KEY", {
278
- key: metadataKey,
279
- type: typeof nestedKeys,
280
- expected: "array"
281
- });
282
- }
283
- const metadataValue = storeEntry.metadata[metadataKey];
284
- if (metadataValue && typeof metadataValue === "object") {
285
- for (const nestedKey of nestedKeys) {
286
- if (typeof nestedKey !== "string") {
287
- throw new this.SlothletError("INVALID_METADATA_KEY", {
288
- key: nestedKey,
289
- type: typeof nestedKey,
290
- expected: "string"
291
- });
292
- }
293
- delete metadataValue[nestedKey];
294
- }
295
- }
296
- }
297
- } else if (typeof key === "string") {
298
- delete storeEntry.metadata[key];
299
- } else {
300
- throw new this.SlothletError("INVALID_METADATA_KEY", {
301
- key: key,
302
- type: typeof key,
303
- expected: "string, string[], or object"
304
- });
305
- }
306
- };
307
-
308
-
309
-
310
- applyRemoval(moduleID);
311
- if (apiPath && apiPath !== moduleID) {
312
- applyRemoval(apiPath);
313
- }
314
- }
315
-
316
-
317
- registerUserMetadata(identifier, metadata) {
318
- if (!identifier || typeof identifier !== "string") {
319
- throw new this.SlothletError(
320
- "INVALID_ARGUMENT",
321
- {
322
- argument: "identifier",
323
- expected: "non-empty string",
324
- received: typeof identifier
325
- },
326
- null,
327
- { validationError: true }
328
- );
329
- }
330
-
331
- let entry = this.#userMetadataStore.get(identifier);
332
- if (!entry) {
333
- entry = { metadata: {}, apiPaths: new Set() };
334
- this.#userMetadataStore.set(identifier, entry);
335
- }
336
-
337
- entry.metadata = { ...entry.metadata, ...metadata };
338
-
339
- entry.apiPaths.add(identifier);
340
- }
341
-
342
-
343
- removeUserMetadataByApiPath(apiPath) {
344
- if (!apiPath) return;
345
- this.#userMetadataStore.delete(apiPath);
346
- }
347
-
348
-
349
- setPathMetadata(apiPath, keyOrObj, value) {
350
- if (typeof apiPath !== "string" || !apiPath) {
351
- throw new this.SlothletError(
352
- "INVALID_ARGUMENT",
353
- { argument: "apiPath", expected: "non-empty string", received: typeof apiPath },
354
- null,
355
- { validationError: true }
356
- );
357
- }
358
-
359
- const metadataObj = typeof keyOrObj === "string" ? { [keyOrObj]: value } : keyOrObj;
360
-
361
- if (!metadataObj || typeof metadataObj !== "object" || Array.isArray(metadataObj)) {
362
- throw new this.SlothletError(
363
- "INVALID_ARGUMENT",
364
- { argument: "keyOrObj", expected: "string key or plain object", received: typeof keyOrObj },
365
- null,
366
- { validationError: true }
367
- );
368
- }
369
-
370
- this.registerUserMetadata(apiPath, metadataObj);
371
- }
372
-
373
-
374
- getPathMetadata(apiPath) {
375
-
376
-
377
- if (!apiPath || typeof apiPath !== "string") return {};
378
- const parts = apiPath.split(".");
379
- const collected = {};
380
- for (let i = 1; i <= parts.length; i++) {
381
- const parentPath = parts.slice(0, i).join(".");
382
- const parentMeta = this.#userMetadataStore.get(parentPath);
383
- if (parentMeta?.metadata) {
384
- Object.assign(collected, parentMeta.metadata);
385
- }
386
- }
387
- return { ...this.#globalUserMetadata, ...collected };
388
- }
389
-
390
-
391
- removePathMetadata(apiPath, key) {
392
- if (!apiPath || typeof apiPath !== "string") return;
393
-
394
- const entry = this.#userMetadataStore.get(apiPath);
395
- if (!entry) return;
396
-
397
- if (key === undefined) {
398
- this.#userMetadataStore.delete(apiPath);
399
- } else if (Array.isArray(key)) {
400
- for (const k of key) {
401
- if (typeof k !== "string") {
402
- throw new this.SlothletError("INVALID_METADATA_KEY", {
403
- key: k,
404
- type: typeof k,
405
- expected: "string"
406
- });
407
- }
408
- delete entry.metadata[k];
409
- }
410
- } else if (typeof key === "string") {
411
- delete entry.metadata[key];
412
- } else {
413
- throw new this.SlothletError("INVALID_METADATA_KEY", {
414
- key,
415
- type: typeof key,
416
- expected: "string or string[]"
417
- });
418
- }
419
- }
420
-
421
-
422
- exportUserState() {
423
- const storeCopy = new Map();
424
- for (const [key, entry] of this.#userMetadataStore) {
425
- storeCopy.set(key, {
426
- metadata: { ...entry.metadata },
427
- apiPaths: new Set(entry.apiPaths)
428
- });
429
- }
430
- return {
431
- globalMetadata: { ...this.#globalUserMetadata },
432
- userMetadataStore: storeCopy
433
- };
434
- }
435
-
436
-
437
- importUserState(state) {
438
- if (!state) return;
439
-
440
-
441
- if (state.globalMetadata) {
442
- for (const [k, v] of Object.entries(state.globalMetadata)) {
443
- if (!(k in this.#globalUserMetadata)) {
444
- this.#globalUserMetadata[k] = v;
445
- }
446
- }
447
- }
448
-
449
-
450
- if (state.userMetadataStore) {
451
- for (const [key, savedEntry] of state.userMetadataStore) {
452
- const existing = this.#userMetadataStore.get(key);
453
- if (!existing) {
454
-
455
- this.#userMetadataStore.set(key, {
456
- metadata: { ...savedEntry.metadata },
457
- apiPaths: new Set(savedEntry.apiPaths)
458
- });
459
- } else {
460
-
461
- existing.metadata = { ...savedEntry.metadata, ...existing.metadata };
462
- for (const p of savedEntry.apiPaths) existing.apiPaths.add(p);
463
- }
464
- }
465
- }
466
- }
467
-
468
-
469
- async get(path) {
470
- if (typeof path !== "string") {
471
- throw new this.SlothletError("INVALID_ARGUMENT", {
472
- argument: "path",
473
- expected: "string",
474
- received: typeof path
475
- });
476
- }
477
-
478
- const apiRoot = this.slothlet.api;
479
- if (!apiRoot) return null;
480
-
481
- const parts = path.split(".");
482
- let target = apiRoot;
483
-
484
- for (const part of parts) {
485
- if (!target || (typeof target !== "object" && typeof target !== "function")) {
486
- return null;
487
- }
488
- target = target[part];
489
- }
490
-
491
- if (target && typeof target._materialize === "function") {
492
- await target._materialize();
493
- }
494
-
495
- if (typeof target === "function" || (target && resolveWrapper(target)?.____slothletInternal?.impl)) {
496
- return this.getMetadata(target);
497
- }
498
-
499
- return null;
500
- }
501
-
502
-
503
- self() {
504
- const ctx = this.slothlet.contextManager?.tryGetContext();
505
- if (!ctx || !ctx.currentWrapper) {
506
- throw new this.SlothletError("RUNTIME_NO_ACTIVE_CONTEXT", {}, null, { validationError: true });
507
- }
508
- return this.getMetadata(ctx.currentWrapper);
509
- }
510
-
511
-
512
- caller() {
513
- const ctx = this.slothlet.contextManager?.tryGetContext();
514
- if (!ctx || !ctx.callerWrapper) return null;
515
- return this.getMetadata(ctx.callerWrapper);
516
- }
517
- }
17
+ import{ComponentBase}from"@cldmv/slothlet/factories/component-base";import{resolveWrapper}from"@cldmv/slothlet/handlers/unified-wrapper";import{verifyToken}from"@cldmv/slothlet/handlers/lifecycle-token";class Metadata extends ComponentBase{static slothletProperty="metadata";#secureMetadata=new WeakMap;#userMetadataStore=new Map;#globalUserMetadata={};_instanceId=null;constructor(slothlet){super(slothlet);this._instanceId=`metadata_${Date.now()}_${Math.random().toString(36).substr(2,9)}`}#deepFreeze(obj){if(Object.isFrozen(obj))return obj;Object.freeze(obj);Object.getOwnPropertyNames(obj).forEach(prop=>{if(obj[prop]!==null&&typeof obj[prop]==="object"){this.#deepFreeze(obj[prop])}});return obj}tagSystemMetadata(target,systemData,token){if(!verifyToken(this.slothlet,token)){throw new this.SlothletError("METADATA_LIFECYCLE_BYPASS",{},null,{validationError:true})}if(!target)return;if(typeof target!=="object"&&typeof target!=="function"){return}let fullModuleID=systemData.moduleID;if(systemData.apiPath&&systemData.moduleID){const apiPathSlashes=systemData.apiPath.replace(/\./g,"/");fullModuleID=`${systemData.moduleID}:${apiPathSlashes}`}let sourceFolder=systemData.sourceFolder;if(!sourceFolder&&systemData.filePath){const pathModule=this.slothlet.helpers.resolver.path;sourceFolder=pathModule.dirname(systemData.filePath)}const frozenSystem=Object.freeze({filePath:systemData.filePath,sourceFolder,apiPath:systemData.apiPath,moduleID:fullModuleID,taggedAt:Date.now()});this.#secureMetadata.set(target,frozenSystem)}getSystemMetadata(target){if(!target)return null;const actualTarget=resolveWrapper(target)??target;const systemData=this.#secureMetadata.get(actualTarget.____slothletInternal?.impl||actualTarget);return systemData||null}getMetadata(target){if(!target)return{};const actualTarget=resolveWrapper(target)??target;const systemData=this.#secureMetadata.get(actualTarget)||this.#secureMetadata.get(actualTarget.____slothletInternal?.impl)||{};const moduleID=systemData.moduleID||systemData.moduleID;const apiPath=systemData.apiPath;const collectMetadataFromParents=path=>{const parts=path.split(".");const collected={};for(let i=1;i<=parts.length;i++){const parentPath=parts.slice(0,i).join(".");const parentMeta=this.#userMetadataStore.get(parentPath);if(parentMeta?.metadata){Object.assign(collected,parentMeta.metadata)}}return collected};const userMetadataByModule=moduleID?this.#userMetadataStore.get(moduleID):null;const userMetadataByPath=apiPath?collectMetadataFromParents(apiPath):{};const userData={...userMetadataByPath,...userMetadataByModule?.metadata||{}};const combined={...this.#globalUserMetadata,...userData,...systemData};if(combined.metadata&&typeof combined.metadata==="object"){const{metadata,...rest}=combined;return this.#deepFreeze({...rest,...metadata})}return this.#deepFreeze(combined)}setGlobalMetadata(key,value){this.#globalUserMetadata[key]=value}setUserMetadata(target,key,value){if(typeof target!=="function"&&typeof target!=="object"){throw new this.SlothletError("INVALID_METADATA_TARGET",{target:typeof target,expected:"function or object"})}const actualTarget=resolveWrapper(target)??target;const systemData=this.#secureMetadata.get(actualTarget)||this.#secureMetadata.get(actualTarget.____slothletInternal?.impl)||{};const moduleID=systemData.moduleID;if(!moduleID){throw new this.SlothletError("METADATA_NO_MODULE_ID",{},null,{validationError:true})}let entry=this.#userMetadataStore.get(moduleID);if(!entry){entry={metadata:{},apiPaths:new Set};this.#userMetadataStore.set(moduleID,entry)}entry.metadata[key]=value;const apiPath=systemData.apiPath;if(apiPath){let pathEntry=this.#userMetadataStore.get(apiPath);if(!pathEntry){pathEntry={metadata:{},apiPaths:new Set};this.#userMetadataStore.set(apiPath,pathEntry)}pathEntry.metadata[key]=value;pathEntry.apiPaths.add(apiPath)}}removeUserMetadata(target,key){if(typeof target!=="function"&&typeof target!=="object"){throw new this.SlothletError("INVALID_METADATA_TARGET",{target:typeof target,expected:"function or object"})}const actualTarget=resolveWrapper(target)??target;const systemData=this.#secureMetadata.get(actualTarget)||this.#secureMetadata.get(actualTarget.____slothletInternal?.impl)||{};const moduleID=systemData.moduleID;const apiPath=systemData.apiPath;if(!moduleID)return;const applyRemoval=storeKey=>{const storeEntry=this.#userMetadataStore.get(storeKey);if(!storeEntry)return;if(key===void 0){this.#userMetadataStore.delete(storeKey)}else if(Array.isArray(key)){for(const k of key){if(typeof k!=="string"){throw new this.SlothletError("INVALID_METADATA_KEY",{key:k,type:typeof k,expected:"string"})}delete storeEntry.metadata[k]}}else if(typeof key==="object"&&key!==null){for(const[metadataKey,nestedKeys]of Object.entries(key)){if(!Array.isArray(nestedKeys)){throw new this.SlothletError("INVALID_METADATA_KEY",{key:metadataKey,type:typeof nestedKeys,expected:"array"})}const metadataValue=storeEntry.metadata[metadataKey];if(metadataValue&&typeof metadataValue==="object"){for(const nestedKey of nestedKeys){if(typeof nestedKey!=="string"){throw new this.SlothletError("INVALID_METADATA_KEY",{key:nestedKey,type:typeof nestedKey,expected:"string"})}delete metadataValue[nestedKey]}}}}else if(typeof key==="string"){delete storeEntry.metadata[key]}else{throw new this.SlothletError("INVALID_METADATA_KEY",{key,type:typeof key,expected:"string, string[], or object"})}};applyRemoval(moduleID);if(apiPath&&apiPath!==moduleID){applyRemoval(apiPath)}}registerUserMetadata(identifier,metadata){if(!identifier||typeof identifier!=="string"){throw new this.SlothletError("INVALID_ARGUMENT",{argument:"identifier",expected:"non-empty string",received:typeof identifier},null,{validationError:true})}let entry=this.#userMetadataStore.get(identifier);if(!entry){entry={metadata:{},apiPaths:new Set};this.#userMetadataStore.set(identifier,entry)}entry.metadata={...entry.metadata,...metadata};entry.apiPaths.add(identifier)}removeUserMetadataByApiPath(apiPath){if(!apiPath)return;this.#userMetadataStore.delete(apiPath)}setPathMetadata(apiPath,keyOrObj,value){if(typeof apiPath!=="string"||!apiPath){throw new this.SlothletError("INVALID_ARGUMENT",{argument:"apiPath",expected:"non-empty string",received:typeof apiPath},null,{validationError:true})}const metadataObj=typeof keyOrObj==="string"?{[keyOrObj]:value}:keyOrObj;if(!metadataObj||typeof metadataObj!=="object"||Array.isArray(metadataObj)){throw new this.SlothletError("INVALID_ARGUMENT",{argument:"keyOrObj",expected:"string key or plain object",received:typeof keyOrObj},null,{validationError:true})}this.registerUserMetadata(apiPath,metadataObj)}getPathMetadata(apiPath){if(!apiPath||typeof apiPath!=="string")return{};const parts=apiPath.split(".");const collected={};for(let i=1;i<=parts.length;i++){const parentPath=parts.slice(0,i).join(".");const parentMeta=this.#userMetadataStore.get(parentPath);if(parentMeta?.metadata){Object.assign(collected,parentMeta.metadata)}}return{...this.#globalUserMetadata,...collected}}removePathMetadata(apiPath,key){if(!apiPath||typeof apiPath!=="string")return;const entry=this.#userMetadataStore.get(apiPath);if(!entry)return;if(key===void 0){this.#userMetadataStore.delete(apiPath)}else if(Array.isArray(key)){for(const k of key){if(typeof k!=="string"){throw new this.SlothletError("INVALID_METADATA_KEY",{key:k,type:typeof k,expected:"string"})}delete entry.metadata[k]}}else if(typeof key==="string"){delete entry.metadata[key]}else{throw new this.SlothletError("INVALID_METADATA_KEY",{key,type:typeof key,expected:"string or string[]"})}}exportUserState(){const storeCopy=new Map;for(const[key,entry]of this.#userMetadataStore){storeCopy.set(key,{metadata:{...entry.metadata},apiPaths:new Set(entry.apiPaths)})}return{globalMetadata:{...this.#globalUserMetadata},userMetadataStore:storeCopy}}importUserState(state){if(!state)return;if(state.globalMetadata){for(const[k,v]of Object.entries(state.globalMetadata)){if(!(k in this.#globalUserMetadata)){this.#globalUserMetadata[k]=v}}}if(state.userMetadataStore){for(const[key,savedEntry]of state.userMetadataStore){const existing=this.#userMetadataStore.get(key);if(!existing){this.#userMetadataStore.set(key,{metadata:{...savedEntry.metadata},apiPaths:new Set(savedEntry.apiPaths)})}else{existing.metadata={...savedEntry.metadata,...existing.metadata};for(const p of savedEntry.apiPaths)existing.apiPaths.add(p)}}}}async get(path){if(typeof path!=="string"){throw new this.SlothletError("INVALID_ARGUMENT",{argument:"path",expected:"string",received:typeof path})}const apiRoot=this.slothlet.api;if(!apiRoot)return null;const parts=path.split(".");let target=apiRoot;for(const part of parts){if(!target||typeof target!=="object"&&typeof target!=="function"){return null}target=target[part]}if(target&&typeof target._materialize==="function"){await target._materialize()}if(typeof target==="function"||target&&resolveWrapper(target)?.____slothletInternal?.impl){return this.getMetadata(target)}return null}self(){const ctx=this.slothlet.contextManager?.tryGetContext();if(!ctx||!ctx.currentWrapper){throw new this.SlothletError("RUNTIME_NO_ACTIVE_CONTEXT",{},null,{validationError:true})}return this.getMetadata(ctx.currentWrapper)}caller(){const ctx=this.slothlet.contextManager?.tryGetContext();if(!ctx||!ctx.callerWrapper)return null;return this.getMetadata(ctx.callerWrapper)}}export{Metadata};