@cldmv/slothlet 3.3.0 → 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.
- package/README.md +6 -8
- package/REFERENCE.md +23 -0
- package/dist/lib/builders/api-assignment.mjs +1 -589
- package/dist/lib/builders/api_builder.mjs +1 -1385
- package/dist/lib/builders/builder.mjs +1 -78
- package/dist/lib/builders/modes-processor.mjs +1 -1800
- package/dist/lib/errors.mjs +9 -211
- package/dist/lib/factories/component-base.mjs +1 -80
- package/dist/lib/factories/context.mjs +1 -22
- package/dist/lib/handlers/api-cache-manager.mjs +1 -200
- package/dist/lib/handlers/api-manager.mjs +1 -2536
- package/dist/lib/handlers/context-async.mjs +1 -172
- package/dist/lib/handlers/context-live.mjs +1 -173
- package/dist/lib/handlers/hook-manager.mjs +1 -667
- package/dist/lib/handlers/lifecycle-token.mjs +1 -28
- package/dist/lib/handlers/lifecycle.mjs +1 -115
- package/dist/lib/handlers/materialize-manager.mjs +1 -48
- package/dist/lib/handlers/metadata.mjs +1 -501
- package/dist/lib/handlers/ownership.mjs +1 -322
- package/dist/lib/handlers/permission-manager.mjs +1 -392
- package/dist/lib/handlers/unified-wrapper.mjs +1 -3110
- package/dist/lib/handlers/version-manager.mjs +1 -885
- package/dist/lib/helpers/class-instance-wrapper.mjs +1 -109
- package/dist/lib/helpers/config.mjs +1 -439
- package/dist/lib/helpers/eventemitter-context.mjs +1 -349
- package/dist/lib/helpers/hint-detector.mjs +1 -47
- package/dist/lib/helpers/modes-utils.mjs +1 -37
- package/dist/lib/helpers/pattern-matcher.mjs +1 -125
- package/dist/lib/helpers/resolve-from-caller.mjs +1 -169
- package/dist/lib/helpers/sanitize.mjs +1 -340
- package/dist/lib/helpers/utilities.mjs +1 -70
- package/dist/lib/i18n/translations.mjs +1 -126
- package/dist/lib/modes/eager.mjs +1 -59
- package/dist/lib/modes/lazy.mjs +1 -81
- package/dist/lib/processors/flatten.mjs +1 -437
- package/dist/lib/processors/loader.mjs +1 -339
- package/dist/lib/processors/type-generator.mjs +1 -275
- package/dist/lib/processors/typescript.mjs +1 -172
- package/dist/lib/runtime/runtime-asynclocalstorage.mjs +1 -113
- package/dist/lib/runtime/runtime-livebindings.mjs +1 -78
- package/dist/lib/runtime/runtime.mjs +1 -102
- package/dist/slothlet.mjs +1 -817
- package/package.json +34 -31
- package/types/dist/lib/builders/api-assignment.d.mts +3 -92
- package/types/dist/lib/builders/api-assignment.d.mts.map +1 -1
- package/types/dist/lib/builders/api_builder.d.mts +102 -91
- package/types/dist/lib/builders/api_builder.d.mts.map +1 -1
- package/types/dist/lib/builders/builder.d.mts +1 -55
- package/types/dist/lib/builders/builder.d.mts.map +1 -1
- package/types/dist/lib/builders/modes-processor.d.mts +3 -27
- package/types/dist/lib/builders/modes-processor.d.mts.map +1 -1
- package/types/dist/lib/errors.d.mts +19 -109
- package/types/dist/lib/errors.d.mts.map +1 -1
- package/types/dist/lib/factories/component-base.d.mts +7 -177
- package/types/dist/lib/factories/component-base.d.mts.map +1 -1
- package/types/dist/lib/factories/context.d.mts +4 -22
- package/types/dist/lib/factories/context.d.mts.map +1 -1
- package/types/dist/lib/handlers/api-cache-manager.d.mts +20 -203
- package/types/dist/lib/handlers/api-cache-manager.d.mts.map +1 -1
- package/types/dist/lib/handlers/api-manager.d.mts +33 -408
- package/types/dist/lib/handlers/api-manager.d.mts.map +1 -1
- package/types/dist/lib/handlers/context-async.d.mts +23 -61
- package/types/dist/lib/handlers/context-async.d.mts.map +1 -1
- package/types/dist/lib/handlers/context-live.d.mts +22 -59
- package/types/dist/lib/handlers/context-live.d.mts.map +1 -1
- package/types/dist/lib/handlers/hook-manager.d.mts +46 -185
- package/types/dist/lib/handlers/hook-manager.d.mts.map +1 -1
- package/types/dist/lib/handlers/lifecycle-token.d.mts +3 -48
- package/types/dist/lib/handlers/lifecycle-token.d.mts.map +1 -1
- package/types/dist/lib/handlers/lifecycle.d.mts +5 -82
- package/types/dist/lib/handlers/lifecycle.d.mts.map +1 -1
- package/types/dist/lib/handlers/materialize-manager.d.mts +8 -70
- package/types/dist/lib/handlers/materialize-manager.d.mts.map +1 -1
- package/types/dist/lib/handlers/metadata.d.mts +17 -221
- package/types/dist/lib/handlers/metadata.d.mts.map +1 -1
- package/types/dist/lib/handlers/ownership.d.mts +44 -160
- package/types/dist/lib/handlers/ownership.d.mts.map +1 -1
- package/types/dist/lib/handlers/permission-manager.d.mts +37 -141
- package/types/dist/lib/handlers/permission-manager.d.mts.map +1 -1
- package/types/dist/lib/handlers/unified-wrapper.d.mts +26 -239
- package/types/dist/lib/handlers/unified-wrapper.d.mts.map +1 -1
- package/types/dist/lib/handlers/version-manager.d.mts +28 -225
- package/types/dist/lib/handlers/version-manager.d.mts.map +1 -1
- package/types/dist/lib/helpers/class-instance-wrapper.d.mts +2 -52
- package/types/dist/lib/helpers/class-instance-wrapper.d.mts.map +1 -1
- package/types/dist/lib/helpers/config.d.mts +125 -139
- package/types/dist/lib/helpers/config.d.mts.map +1 -1
- package/types/dist/lib/helpers/eventemitter-context.d.mts +3 -29
- package/types/dist/lib/helpers/eventemitter-context.d.mts.map +1 -1
- package/types/dist/lib/helpers/hint-detector.d.mts +2 -15
- package/types/dist/lib/helpers/hint-detector.d.mts.map +1 -1
- package/types/dist/lib/helpers/modes-utils.d.mts +3 -30
- package/types/dist/lib/helpers/modes-utils.d.mts.map +1 -1
- package/types/dist/lib/helpers/pattern-matcher.d.mts +3 -43
- package/types/dist/lib/helpers/pattern-matcher.d.mts.map +1 -1
- package/types/dist/lib/helpers/resolve-from-caller.d.mts +3 -27
- package/types/dist/lib/helpers/resolve-from-caller.d.mts.map +1 -1
- package/types/dist/lib/helpers/sanitize.d.mts +4 -92
- package/types/dist/lib/helpers/sanitize.d.mts.map +1 -1
- package/types/dist/lib/helpers/utilities.d.mts +4 -52
- package/types/dist/lib/helpers/utilities.d.mts.map +1 -1
- package/types/dist/lib/i18n/translations.d.mts +4 -37
- package/types/dist/lib/i18n/translations.d.mts.map +1 -1
- package/types/dist/lib/modes/eager.d.mts +8 -30
- package/types/dist/lib/modes/eager.d.mts.map +1 -1
- package/types/dist/lib/modes/lazy.d.mts +10 -43
- package/types/dist/lib/modes/lazy.d.mts.map +1 -1
- package/types/dist/lib/processors/flatten.d.mts +56 -107
- package/types/dist/lib/processors/flatten.d.mts.map +1 -1
- package/types/dist/lib/processors/loader.d.mts +6 -41
- package/types/dist/lib/processors/loader.d.mts.map +1 -1
- package/types/dist/lib/processors/type-generator.d.mts +2 -16
- package/types/dist/lib/processors/type-generator.d.mts.map +1 -1
- package/types/dist/lib/processors/typescript.d.mts +6 -53
- package/types/dist/lib/processors/typescript.d.mts.map +1 -1
- package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts +3 -71
- package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts.map +1 -1
- package/types/dist/lib/runtime/runtime-livebindings.d.mts +2 -37
- package/types/dist/lib/runtime/runtime-livebindings.d.mts.map +1 -1
- package/types/dist/lib/runtime/runtime.d.mts +3 -39
- package/types/dist/lib/runtime/runtime.d.mts.map +1 -1
- package/types/dist/slothlet.d.mts +3 -249
- package/types/dist/slothlet.d.mts.map +1 -1
- package/types/index.d.mts +36 -16
- package/types/index.d.mts.map +1 -0
- package/AGENT-USAGE.md +0 -736
- package/docs/API-RULES.md +0 -712
|
@@ -14,118 +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 { getInstanceToken } from "@cldmv/slothlet/handlers/lifecycle-token";
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
export class Lifecycle extends ComponentBase {
|
|
26
|
-
|
|
27
|
-
static slothletProperty = "lifecycle";
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
constructor(slothlet) {
|
|
31
|
-
super(slothlet);
|
|
32
|
-
this.subscribers = new Map();
|
|
33
|
-
this.eventLog = [];
|
|
34
|
-
this.maxLogSize = 1000;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
subscribe(event, handler) {
|
|
39
|
-
if (!this.subscribers.has(event)) {
|
|
40
|
-
this.subscribers.set(event, new Set());
|
|
41
|
-
}
|
|
42
|
-
this.subscribers.get(event).add(handler);
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return () => {
|
|
46
|
-
const handlers = this.subscribers.get(event);
|
|
47
|
-
if (handlers) {
|
|
48
|
-
handlers.delete(handler);
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
on(event, handler) {
|
|
55
|
-
return this.subscribe(event, handler);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
off(event, handler) {
|
|
60
|
-
const handlers = this.subscribers.get(event);
|
|
61
|
-
if (handlers) {
|
|
62
|
-
handlers.delete(handler);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
unsubscribe(event, handler) {
|
|
68
|
-
this.off(event, handler);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
async emit(event, data) {
|
|
73
|
-
|
|
74
|
-
if (this.____config?.debug?.lifecycle) {
|
|
75
|
-
this.eventLog.push({
|
|
76
|
-
event,
|
|
77
|
-
data: { ...data },
|
|
78
|
-
timestamp: Date.now()
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (this.eventLog.length > this.maxLogSize) {
|
|
83
|
-
this.eventLog.shift();
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
this.slothlet.debug("lifecycle", {
|
|
87
|
-
key: "DEBUG_MODE_LIFECYCLE_EVENT",
|
|
88
|
-
event,
|
|
89
|
-
apiPath: data.apiPath,
|
|
90
|
-
source: data.source,
|
|
91
|
-
moduleID: data.moduleID
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const handlers = this.subscribers.get(event);
|
|
97
|
-
if (handlers) {
|
|
98
|
-
|
|
99
|
-
const handlerPromises = [];
|
|
100
|
-
|
|
101
|
-
const token = getInstanceToken(this.slothlet);
|
|
102
|
-
for (const handler of handlers) {
|
|
103
|
-
try {
|
|
104
|
-
const result = handler(data, token);
|
|
105
|
-
|
|
106
|
-
if (result && typeof result.then === "function") {
|
|
107
|
-
handlerPromises.push(
|
|
108
|
-
result.catch((error) => {
|
|
109
|
-
|
|
110
|
-
if (!this.____config?.silent) {
|
|
111
|
-
new this.SlothletWarning("WARNING_LIFECYCLE_HANDLER_ERROR", { event }, error);
|
|
112
|
-
}
|
|
113
|
-
})
|
|
114
|
-
);
|
|
115
|
-
}
|
|
116
|
-
} catch (error) {
|
|
117
|
-
|
|
118
|
-
if (!this.____config?.silent) {
|
|
119
|
-
new this.SlothletWarning("WARNING_LIFECYCLE_HANDLER_ERROR", { event }, error);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if (handlerPromises.length > 0) {
|
|
126
|
-
await Promise.all(handlerPromises);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
}
|
|
17
|
+
import{ComponentBase}from"@cldmv/slothlet/factories/component-base";import{getInstanceToken}from"@cldmv/slothlet/handlers/lifecycle-token";class Lifecycle extends ComponentBase{static slothletProperty="lifecycle";constructor(slothlet){super(slothlet);this.subscribers=new Map;this.eventLog=[];this.maxLogSize=1e3}subscribe(event,handler){if(!this.subscribers.has(event)){this.subscribers.set(event,new Set)}this.subscribers.get(event).add(handler);return()=>{const handlers=this.subscribers.get(event);if(handlers){handlers.delete(handler)}}}on(event,handler){return this.subscribe(event,handler)}off(event,handler){const handlers=this.subscribers.get(event);if(handlers){handlers.delete(handler)}}unsubscribe(event,handler){this.off(event,handler)}async emit(event,data){if(this.____config?.debug?.lifecycle){this.eventLog.push({event,data:{...data},timestamp:Date.now()});if(this.eventLog.length>this.maxLogSize){this.eventLog.shift()}this.slothlet.debug("lifecycle",{key:"DEBUG_MODE_LIFECYCLE_EVENT",event,apiPath:data.apiPath,source:data.source,moduleID:data.moduleID})}const handlers=this.subscribers.get(event);if(handlers){const handlerPromises=[];const token=getInstanceToken(this.slothlet);for(const handler of handlers){try{const result=handler(data,token);if(result&&typeof result.then==="function"){handlerPromises.push(result.catch(error=>{if(!this.____config?.silent){new this.SlothletWarning("WARNING_LIFECYCLE_HANDLER_ERROR",{event},error)}}))}}catch(error){if(!this.____config?.silent){new this.SlothletWarning("WARNING_LIFECYCLE_HANDLER_ERROR",{event},error)}}}if(handlerPromises.length>0){await Promise.all(handlerPromises)}}}}export{Lifecycle};
|
|
@@ -14,51 +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
|
-
|
|
23
|
-
|
|
24
|
-
export class MaterializeManager extends ComponentBase {
|
|
25
|
-
static slothletProperty = "materialize";
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
constructor(slothlet) {
|
|
29
|
-
super(slothlet);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
get materialized() {
|
|
34
|
-
return this.slothlet._unmaterializedLazyCount === 0;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
get() {
|
|
39
|
-
const total = this.slothlet._totalLazyCount;
|
|
40
|
-
const remaining = this.slothlet._unmaterializedLazyCount;
|
|
41
|
-
const materialized = total - remaining;
|
|
42
|
-
const percentage = total > 0 ? Math.round((materialized / total) * 100) : 100;
|
|
43
|
-
|
|
44
|
-
return {
|
|
45
|
-
total,
|
|
46
|
-
materialized,
|
|
47
|
-
remaining,
|
|
48
|
-
percentage
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
async wait() {
|
|
54
|
-
|
|
55
|
-
if (this.slothlet._unmaterializedLazyCount === 0) {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
return new Promise((resolve) => {
|
|
61
|
-
this.slothlet._materializationWaiters.push(resolve);
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
}
|
|
17
|
+
import{ComponentBase}from"@cldmv/slothlet/factories/component-base";class MaterializeManager extends ComponentBase{static slothletProperty="materialize";constructor(slothlet){super(slothlet)}get materialized(){return this.slothlet._unmaterializedLazyCount===0}get(){const total=this.slothlet._totalLazyCount;const remaining=this.slothlet._unmaterializedLazyCount;const materialized=total-remaining;const percentage=total>0?Math.round(materialized/total*100):100;return{total,materialized,remaining,percentage}}async wait(){if(this.slothlet._unmaterializedLazyCount===0){return}return new Promise(resolve=>{this.slothlet._materializationWaiters.push(resolve)})}}export{MaterializeManager};
|
|
@@ -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};
|