@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,325 +14,4 @@
14
14
  limitations under the License.
15
15
  */
16
16
 
17
-
18
-
19
-
20
- import { ComponentBase } from "@cldmv/slothlet/factories/component-base";
21
- import { resolveWrapper } from "@cldmv/slothlet/handlers/unified-wrapper";
22
-
23
-
24
-
25
-
26
- export class OwnershipManager extends ComponentBase {
27
- static slothletProperty = "ownership";
28
-
29
- constructor(slothlet) {
30
- super(slothlet);
31
- this.moduleToPath = new Map();
32
- this.pathToModule = new Map();
33
- this._unregisteredModules = new Set();
34
- }
35
-
36
-
37
- register({ moduleID, apiPath, value, source = "core", collisionMode = "error", config = null, filePath = null }) {
38
-
39
- if (!moduleID || typeof moduleID !== "string") {
40
- throw new this.SlothletError("OWNERSHIP_INVALID_MODULE_ID", { moduleID }, null, { validationError: true });
41
- }
42
-
43
- if (apiPath !== "" && (!apiPath || typeof apiPath !== "string")) {
44
- throw new this.SlothletError("OWNERSHIP_INVALID_API_PATH", { apiPath }, null, { validationError: true });
45
- }
46
-
47
-
48
-
49
-
50
- if (this._unregisteredModules.has(moduleID)) {
51
- return null;
52
- }
53
-
54
-
55
- const currentOwner = this.getCurrentOwner(apiPath);
56
- if (currentOwner && currentOwner.moduleID !== moduleID) {
57
-
58
- if (collisionMode === "merge" || collisionMode === "replace" || collisionMode === "merge-replace") {
59
-
60
- } else if (collisionMode === "skip") {
61
-
62
- return null;
63
- } else if (collisionMode === "warn") {
64
-
65
- if (!config?.silent) {
66
- new this.SlothletWarning("WARNING_OWNERSHIP_CONFLICT", {
67
- apiPath,
68
- existingModuleId: currentOwner.moduleID,
69
- newModuleId: moduleID
70
- });
71
- }
72
- return null;
73
- } else {
74
-
75
- throw new this.SlothletError("OWNERSHIP_CONFLICT", {
76
- apiPath,
77
- existingModuleId: currentOwner.moduleID,
78
- newModuleId: moduleID,
79
- validationError: true
80
- });
81
- }
82
- }
83
-
84
-
85
- if (!this.moduleToPath.has(moduleID)) {
86
- this.moduleToPath.set(moduleID, new Set());
87
- }
88
- this.moduleToPath.get(moduleID).add(apiPath);
89
-
90
-
91
- if (!this.pathToModule.has(apiPath)) {
92
- this.pathToModule.set(apiPath, []);
93
- }
94
-
95
-
96
- const stack = this.pathToModule.get(apiPath);
97
- const existingEntry = stack.find((entry) => entry.moduleID === moduleID);
98
- if (existingEntry) {
99
-
100
- existingEntry.source = source;
101
- existingEntry.timestamp = Date.now();
102
- existingEntry.value = value;
103
- if (filePath !== null) {
104
- existingEntry.filePath = filePath;
105
- }
106
- return existingEntry;
107
- }
108
-
109
- const entry = {
110
- moduleID,
111
- source,
112
- timestamp: Date.now(),
113
- value,
114
- filePath
115
- };
116
-
117
- this.pathToModule.get(apiPath).push(entry);
118
-
119
- return entry;
120
- }
121
-
122
-
123
- unregister(moduleID) {
124
- const paths = this.moduleToPath.get(moduleID);
125
- if (!paths) {
126
- return { removed: [], rolledBack: [] };
127
- }
128
-
129
-
130
-
131
- this._unregisteredModules.add(moduleID);
132
-
133
- const removed = [];
134
- const rolledBack = [];
135
-
136
- for (const apiPath of paths) {
137
- const result = this.removePath(apiPath, moduleID);
138
-
139
- if (result.action === "delete") {
140
- removed.push(apiPath);
141
- } else if (result.action === "restore") {
142
- rolledBack.push({
143
- apiPath,
144
- restoredTo: result.restoreModuleId
145
- });
146
- }
147
- }
148
-
149
- this.moduleToPath.delete(moduleID);
150
-
151
- return { removed, rolledBack };
152
- }
153
-
154
-
155
- removePath(apiPath, moduleID = null) {
156
- const stack = this.pathToModule.get(apiPath);
157
- if (!stack) {
158
- return { action: "none", removedModuleId: null, restoreModuleId: null };
159
- }
160
-
161
-
162
- const index = moduleID ? stack.findIndex((entry) => entry.moduleID === moduleID) : stack.length - 1;
163
- if (index === -1) {
164
- return { action: "none", removedModuleId: null, restoreModuleId: null };
165
- }
166
- const [removed] = stack.splice(index, 1);
167
- const removedModuleId = removed.moduleID;
168
- if (removedModuleId && this.moduleToPath.has(removedModuleId)) {
169
- const pathSet = this.moduleToPath.get(removedModuleId);
170
- pathSet.delete(apiPath);
171
- if (pathSet.size === 0) {
172
- this.moduleToPath.delete(removedModuleId);
173
- }
174
- }
175
-
176
-
177
- if (stack.length === 0) {
178
- this.pathToModule.delete(apiPath);
179
- return { action: "delete", removedModuleId, restoreModuleId: null };
180
- }
181
-
182
-
183
- const previous = stack[stack.length - 1];
184
- return {
185
- action: "restore",
186
- removedModuleId,
187
- restoreModuleId: previous.moduleID
188
- };
189
- }
190
-
191
-
192
- getCurrentOwner(apiPath) {
193
- const stack = this.pathToModule.get(apiPath);
194
- if (!stack || stack.length === 0) return null;
195
- return stack[stack.length - 1];
196
- }
197
-
198
-
199
- getCurrentValue(apiPath) {
200
- const owner = this.getCurrentOwner(apiPath);
201
- if (!owner) return undefined;
202
-
203
- const value = owner.value;
204
-
205
-
206
-
207
-
208
- const rawWrapper = resolveWrapper(value);
209
- if (rawWrapper) {
210
- return rawWrapper.__impl;
211
- }
212
-
213
- return value;
214
- }
215
-
216
-
217
- getModulePaths(moduleID) {
218
- return Array.from(this.moduleToPath.get(moduleID) || []);
219
- }
220
-
221
-
222
- getPathHistory(apiPath) {
223
- return this.pathToModule.get(apiPath) || [];
224
- }
225
-
226
-
227
- ownsPath(moduleID, apiPath) {
228
- const owner = this.getCurrentOwner(apiPath);
229
- return owner && owner.moduleID === moduleID;
230
- }
231
-
232
-
233
- getDiagnostics() {
234
- return {
235
- totalModules: this.moduleToPath.size,
236
- totalPaths: this.pathToModule.size,
237
- modules: Array.from(this.moduleToPath.entries()).map(([id, paths]) => ({
238
- moduleID: id,
239
- pathCount: paths.size
240
- })),
241
- conflictedPaths: Array.from(this.pathToModule.entries())
242
- .filter(([_, stack]) => stack.length > 1)
243
- .map(([path, stack]) => ({
244
- apiPath: path,
245
- ownerStack: stack.map((e) => e.moduleID)
246
- }))
247
- };
248
- }
249
-
250
-
251
- getPathOwnership(apiPath) {
252
- const stack = this.pathToModule.get(apiPath);
253
- if (!stack || stack.length === 0) {
254
- return null;
255
- }
256
- return new Set(stack.map((entry) => entry.moduleID));
257
- }
258
-
259
-
260
- registerSubtree(api, moduleID, path, visited = new WeakSet()) {
261
- if (!api || typeof api !== "object") return;
262
-
263
-
264
- if (visited.has(api)) {
265
- return;
266
- }
267
- visited.add(api);
268
-
269
-
270
- if (path) {
271
- this.register({
272
- moduleID,
273
- apiPath: path,
274
- value: api,
275
- source: "core",
276
- collisionMode: "merge",
277
- filePath: null
278
- });
279
- }
280
-
281
-
282
- for (const [key, value] of Object.entries(api)) {
283
-
284
- const skipProps = ["__metadata", "__type", "_materialize", "_impl", "____slothletInternal"];
285
- if (skipProps.includes(key)) {
286
- continue;
287
- }
288
-
289
- const childPath = path ? `${path}.${key}` : key;
290
- if (typeof value === "function" || (value && typeof value === "object")) {
291
- this.register({
292
- moduleID,
293
- apiPath: childPath,
294
- value,
295
- source: "core",
296
- collisionMode: "merge",
297
- filePath: null
298
- });
299
-
300
-
301
- if (typeof value === "object" && !Array.isArray(value)) {
302
- this.registerSubtree(value, moduleID, childPath, visited);
303
- }
304
- }
305
- }
306
- }
307
-
308
-
309
- clear() {
310
- this.moduleToPath.clear();
311
- this.pathToModule.clear();
312
- this._unregisteredModules.clear();
313
- }
314
-
315
-
316
- exportState() {
317
- return {
318
- moduleToPath: Array.from(this.moduleToPath.entries()).map(([id, paths]) => [id, Array.from(paths)]),
319
- pathToModule: Array.from(this.pathToModule.entries())
320
- };
321
- }
322
-
323
-
324
- importState(state) {
325
-
326
- this.clear();
327
-
328
-
329
- for (const [id, paths] of state.moduleToPath) {
330
- this.moduleToPath.set(id, new Set(paths));
331
- }
332
-
333
-
334
- for (const [path, stack] of state.pathToModule) {
335
- this.pathToModule.set(path, stack);
336
- }
337
- }
338
- }
17
+ import{ComponentBase}from"@cldmv/slothlet/factories/component-base";import{resolveWrapper}from"@cldmv/slothlet/handlers/unified-wrapper";class OwnershipManager extends ComponentBase{static slothletProperty="ownership";constructor(slothlet){super(slothlet);this.moduleToPath=new Map;this.pathToModule=new Map;this._unregisteredModules=new Set}register({moduleID,apiPath,value,source="core",collisionMode="error",config=null,filePath=null}){if(!moduleID||typeof moduleID!=="string"){throw new this.SlothletError("OWNERSHIP_INVALID_MODULE_ID",{moduleID},null,{validationError:true})}if(apiPath!==""&&(!apiPath||typeof apiPath!=="string")){throw new this.SlothletError("OWNERSHIP_INVALID_API_PATH",{apiPath},null,{validationError:true})}if(this._unregisteredModules.has(moduleID)){return null}const currentOwner=this.getCurrentOwner(apiPath);if(currentOwner&&currentOwner.moduleID!==moduleID){if(collisionMode==="merge"||collisionMode==="replace"||collisionMode==="merge-replace"){}else if(collisionMode==="skip"){return null}else if(collisionMode==="warn"){if(!config?.silent){new this.SlothletWarning("WARNING_OWNERSHIP_CONFLICT",{apiPath,existingModuleId:currentOwner.moduleID,newModuleId:moduleID})}return null}else{throw new this.SlothletError("OWNERSHIP_CONFLICT",{apiPath,existingModuleId:currentOwner.moduleID,newModuleId:moduleID,validationError:true})}}if(!this.moduleToPath.has(moduleID)){this.moduleToPath.set(moduleID,new Set)}this.moduleToPath.get(moduleID).add(apiPath);if(!this.pathToModule.has(apiPath)){this.pathToModule.set(apiPath,[])}const stack=this.pathToModule.get(apiPath);const existingEntry=stack.find(entry2=>entry2.moduleID===moduleID);if(existingEntry){existingEntry.source=source;existingEntry.timestamp=Date.now();existingEntry.value=value;if(filePath!==null){existingEntry.filePath=filePath}return existingEntry}const entry={moduleID,source,timestamp:Date.now(),value,filePath};this.pathToModule.get(apiPath).push(entry);return entry}unregister(moduleID){const paths=this.moduleToPath.get(moduleID);if(!paths){return{removed:[],rolledBack:[]}}this._unregisteredModules.add(moduleID);const removed=[];const rolledBack=[];for(const apiPath of paths){const result=this.removePath(apiPath,moduleID);if(result.action==="delete"){removed.push(apiPath)}else if(result.action==="restore"){rolledBack.push({apiPath,restoredTo:result.restoreModuleId})}}this.moduleToPath.delete(moduleID);return{removed,rolledBack}}removePath(apiPath,moduleID=null){const stack=this.pathToModule.get(apiPath);if(!stack){return{action:"none",removedModuleId:null,restoreModuleId:null}}const index=moduleID?stack.findIndex(entry=>entry.moduleID===moduleID):stack.length-1;if(index===-1){return{action:"none",removedModuleId:null,restoreModuleId:null}}const[removed]=stack.splice(index,1);const removedModuleId=removed.moduleID;if(removedModuleId&&this.moduleToPath.has(removedModuleId)){const pathSet=this.moduleToPath.get(removedModuleId);pathSet.delete(apiPath);if(pathSet.size===0){this.moduleToPath.delete(removedModuleId)}}if(stack.length===0){this.pathToModule.delete(apiPath);return{action:"delete",removedModuleId,restoreModuleId:null}}const previous=stack[stack.length-1];return{action:"restore",removedModuleId,restoreModuleId:previous.moduleID}}getCurrentOwner(apiPath){const stack=this.pathToModule.get(apiPath);if(!stack||stack.length===0)return null;return stack[stack.length-1]}getCurrentValue(apiPath){const owner=this.getCurrentOwner(apiPath);if(!owner)return void 0;const value=owner.value;const rawWrapper=resolveWrapper(value);if(rawWrapper){return rawWrapper.__impl}return value}getModulePaths(moduleID){return Array.from(this.moduleToPath.get(moduleID)||[])}getPathHistory(apiPath){return this.pathToModule.get(apiPath)||[]}ownsPath(moduleID,apiPath){const owner=this.getCurrentOwner(apiPath);return owner&&owner.moduleID===moduleID}getDiagnostics(){return{totalModules:this.moduleToPath.size,totalPaths:this.pathToModule.size,modules:Array.from(this.moduleToPath.entries()).map(([id,paths])=>({moduleID:id,pathCount:paths.size})),conflictedPaths:Array.from(this.pathToModule.entries()).filter(([_,stack])=>stack.length>1).map(([path,stack])=>({apiPath:path,ownerStack:stack.map(e=>e.moduleID)}))}}getPathOwnership(apiPath){const stack=this.pathToModule.get(apiPath);if(!stack||stack.length===0){return null}return new Set(stack.map(entry=>entry.moduleID))}registerSubtree(api,moduleID,path,visited=new WeakSet){if(!api||typeof api!=="object")return;if(visited.has(api)){return}visited.add(api);if(path){this.register({moduleID,apiPath:path,value:api,source:"core",collisionMode:"merge",filePath:null})}for(const[key,value]of Object.entries(api)){const skipProps=["__metadata","__type","_materialize","_impl","____slothletInternal"];if(skipProps.includes(key)){continue}const childPath=path?`${path}.${key}`:key;if(typeof value==="function"||value&&typeof value==="object"){this.register({moduleID,apiPath:childPath,value,source:"core",collisionMode:"merge",filePath:null});if(typeof value==="object"&&!Array.isArray(value)){this.registerSubtree(value,moduleID,childPath,visited)}}}}clear(){this.moduleToPath.clear();this.pathToModule.clear();this._unregisteredModules.clear()}exportState(){return{moduleToPath:Array.from(this.moduleToPath.entries()).map(([id,paths])=>[id,Array.from(paths)]),pathToModule:Array.from(this.pathToModule.entries())}}importState(state){this.clear();for(const[id,paths]of state.moduleToPath){this.moduleToPath.set(id,new Set(paths))}for(const[path,stack]of state.pathToModule){this.pathToModule.set(path,stack)}}}export{OwnershipManager};
@@ -0,0 +1,17 @@
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
+ import{ComponentBase}from"@cldmv/slothlet/factories/component-base";import{compilePattern}from"@cldmv/slothlet/helpers/pattern-matcher";import{translate}from"@cldmv/slothlet/i18n";let ruleIdCounter=0;class PermissionManager extends ComponentBase{static slothletProperty="permissionManager";#rules=new Map;#defaultPolicy="allow";#enabled=false;#audit="default";#resolvedCache=new Map;#compiledCache=new Map;constructor(slothlet){super(slothlet);const permConfig=slothlet.config?.permissions;if(permConfig){this.#defaultPolicy=permConfig.defaultPolicy||"allow";this.#enabled=permConfig.enabled!==false;this.#audit=permConfig.audit||"default";if(Array.isArray(permConfig.rules)){for(const rule of permConfig.rules){this.addRule(rule,null)}}}this.addRule({caller:"**",target:"slothlet.permissions.control.**",effect:"deny"},"__builtin__")}addRule(rule,ownerModuleID=null,ruleId=null){this.#validateRule(rule);const id=ruleId||`perm-${++ruleIdCounter}`;const entry={id,caller:rule.caller,target:rule.target,effect:rule.effect,ownerModuleID,registeredAt:Date.now()};this.#rules.set(id,entry);this.#clearCache();this.debug("permissions",{key:"DEBUG_PERMISSION_RULE_ADDED",ruleId:id,caller:rule.caller,target:rule.target,effect:rule.effect,ownerModuleID});return id}removeRule(ruleId,callerModuleID=null){const entry=this.#rules.get(ruleId);if(!entry)return false;if(callerModuleID&&entry.ownerModuleID&&callerModuleID===entry.ownerModuleID){throw new this.SlothletError("PERMISSION_SELF_MODIFY",{ruleId,moduleID:callerModuleID})}this.#rules.delete(ruleId);this.#clearCache();this.debug("permissions",{key:"DEBUG_PERMISSION_RULE_REMOVED",ruleId,caller:entry.caller,target:entry.target,effect:entry.effect});return true}checkAccess(callerPath,targetPath,callerFilePath=null,targetFilePath=null){const isControlTarget=targetPath?.startsWith("slothlet.permissions.control.");if(!this.#enabled&&!isControlTarget)return true;if(callerFilePath&&targetFilePath&&callerFilePath===targetFilePath){this.#emitAuditEvent("permission:self-bypass",{caller:callerPath,target:targetPath,filePath:callerFilePath});return true}const cacheKey=`${callerPath}::${targetPath}`;if(this.#resolvedCache.has(cacheKey)){const cached=this.#resolvedCache.get(cacheKey);this.#emitAuditEvent(cached.event,cached.payload);return cached.allowed}const entry=this.#evaluate(callerPath,targetPath);this.#resolvedCache.set(cacheKey,entry);this.#emitAuditEvent(entry.event,entry.payload);return entry.allowed}getRulesForPath(targetPath){const matching=[];for(const entry of this.#rules.values()){const targetMatcher=this.#getCompiledPattern(entry.target);if(targetMatcher(targetPath)){matching.push(this.#serializeRule(entry))}}return matching}getRulesByModule(moduleID){const matching=[];for(const entry of this.#rules.values()){if(entry.ownerModuleID===moduleID){matching.push(this.#serializeRule(entry))}}return matching}getRulesForCaller(callerPath){const matching=[];for(const entry of this.#rules.values()){const callerMatcher=this.#getCompiledPattern(entry.caller);if(callerMatcher(callerPath)){matching.push(this.#serializeRule(entry))}}return matching}enable(){this.#enabled=true;this.#clearCache()}disable(){this.#enabled=false;this.#clearCache()}isEnabled(){return this.#enabled}exportRules(){const rules=[];for(const entry of this.#rules.values()){rules.push({rule:{caller:entry.caller,target:entry.target,effect:entry.effect},ownerModuleID:entry.ownerModuleID})}return rules}importRules(registrations){if(!Array.isArray(registrations))return;for(const reg of registrations){this.addRule(reg.rule,reg.ownerModuleID)}}async shutdown(){this.#rules.clear();this.#resolvedCache.clear();this.#compiledCache.clear();this.#enabled=false;this.#defaultPolicy="allow";this.#audit="default"}#validateRule(rule){if(!rule||typeof rule!=="object"){throw new this.SlothletError("INVALID_PERMISSION_RULE",{reason:translate("PERM_RULE_NOT_OBJECT"),received:typeof rule})}if(typeof rule.caller!=="string"||!rule.caller){throw new this.SlothletError("INVALID_PERMISSION_RULE",{reason:translate("PERM_RULE_CALLER_REQUIRED"),received:typeof rule.caller})}if(typeof rule.target!=="string"||!rule.target){throw new this.SlothletError("INVALID_PERMISSION_RULE",{reason:translate("PERM_RULE_TARGET_REQUIRED"),received:typeof rule.target})}if(rule.effect!=="allow"&&rule.effect!=="deny"){throw new this.SlothletError("INVALID_PERMISSION_RULE",{reason:translate("PERM_RULE_EFFECT_INVALID"),received:rule.effect})}}#evaluate(callerPath,targetPath){const matches=[];for(const entry of this.#rules.values()){const callerMatcher=this.#getCompiledPattern(entry.caller);const targetMatcher=this.#getCompiledPattern(entry.target);if(callerMatcher(callerPath)&&targetMatcher(targetPath)){matches.push(entry)}}if(matches.length===0){const allowed2=this.#defaultPolicy==="allow";return{allowed:allowed2,event:"permission:default",payload:{caller:callerPath,target:targetPath,policy:this.#defaultPolicy}}}matches.sort((a,b)=>{const specA=this.#computeSpecificity(a,callerPath,targetPath);const specB=this.#computeSpecificity(b,callerPath,targetPath);if(specA!==specB)return specB-specA;return a.registeredAt-b.registeredAt});const highestSpec=this.#computeSpecificity(matches[0],callerPath,targetPath);const topTier=matches.filter(m=>this.#computeSpecificity(m,callerPath,targetPath)===highestSpec);const winner=topTier[topTier.length-1];const allowed=winner.effect==="allow";return{allowed,event:allowed?"permission:allowed":"permission:denied",payload:{caller:callerPath,target:targetPath,rule:this.#serializeRule(winner)}}}#computeSpecificity(entry,callerPath,targetPath){return this.#patternSpecificity(entry.caller,callerPath)+this.#patternSpecificity(entry.target,targetPath)}#patternSpecificity(pattern,_path){if(!pattern.includes("*")&&!pattern.includes("?")&&!pattern.includes("{")){return 3}if(pattern.includes("**")){return 1}return 2}#getCompiledPattern(pattern){let matcher=this.#compiledCache.get(pattern);if(!matcher){matcher=compilePattern(pattern);this.#compiledCache.set(pattern,matcher)}return matcher}#clearCache(){this.#resolvedCache.clear()}#emitAuditEvent(event,payload){this.debug("permissions",{key:event==="permission:denied"?"DEBUG_PERMISSION_DENIED":event==="permission:allowed"?"DEBUG_PERMISSION_ALLOWED":event==="permission:self-bypass"?"DEBUG_PERMISSION_SELF_BYPASS":"DEBUG_PERMISSION_DEFAULT",...payload});const alwaysEmit=event==="permission:denied"||event==="permission:self-bypass";if(!alwaysEmit&&this.#audit!=="verbose")return;const lifecycle=this.slothlet.handlers?.lifecycle;if(lifecycle){lifecycle.emit(event,{...payload,timestamp:Date.now()})}}#serializeRule(entry){return{id:entry.id,caller:entry.caller,target:entry.target,effect:entry.effect,ownerModuleID:entry.ownerModuleID,registeredAt:entry.registeredAt}}debug(category,data){this.slothlet.debug(category,data)}}export{PermissionManager};