@cldmv/slothlet 2.11.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENT-USAGE.md +355 -325
- package/README.md +554 -238
- package/dist/lib/builders/api-assignment.mjs +605 -0
- package/dist/lib/builders/api_builder.mjs +1073 -0
- package/dist/lib/builders/builder.mjs +94 -0
- package/dist/lib/builders/modes-processor.mjs +1816 -0
- package/dist/lib/errors.mjs +227 -0
- package/dist/lib/factories/component-base.mjs +96 -0
- package/dist/lib/factories/context.mjs +38 -0
- package/dist/lib/handlers/api-cache-manager.mjs +216 -0
- package/dist/lib/handlers/api-manager.mjs +2364 -0
- package/dist/lib/handlers/context-async.mjs +184 -0
- package/dist/lib/handlers/context-live.mjs +184 -0
- package/dist/lib/handlers/hook-manager.mjs +789 -0
- package/dist/lib/handlers/lifecycle-token.mjs +44 -0
- package/dist/lib/handlers/lifecycle.mjs +131 -0
- package/dist/lib/handlers/materialize-manager.mjs +64 -0
- package/dist/lib/handlers/metadata.mjs +500 -0
- package/dist/lib/handlers/ownership.mjs +338 -0
- package/dist/lib/handlers/unified-wrapper.mjs +3031 -0
- package/dist/lib/helpers/class-instance-wrapper.mjs +125 -0
- package/dist/lib/helpers/config.mjs +343 -0
- package/dist/lib/helpers/eventemitter-context.mjs +365 -0
- package/dist/lib/helpers/hint-detector.mjs +63 -0
- package/dist/lib/helpers/modes-utils.mjs +53 -0
- package/dist/lib/helpers/resolve-from-caller.mjs +123 -117
- package/dist/lib/helpers/sanitize.mjs +247 -168
- package/dist/lib/helpers/utilities.mjs +46 -81
- package/dist/lib/i18n/languages/de-de.json +377 -0
- package/dist/lib/i18n/languages/en-gb.json +377 -0
- package/dist/lib/i18n/languages/en-us.json +377 -0
- package/dist/lib/i18n/languages/es-mx.json +377 -0
- package/dist/lib/i18n/languages/fr-fr.json +377 -0
- package/dist/lib/i18n/languages/hi-in.json +377 -0
- package/dist/lib/i18n/languages/ja-jp.json +377 -0
- package/dist/lib/i18n/languages/ko-kr.json +377 -0
- package/dist/lib/i18n/languages/pt-br.json +377 -0
- package/dist/lib/i18n/languages/ru-ru.json +377 -0
- package/dist/lib/i18n/languages/zh-cn.json +377 -0
- package/dist/lib/i18n/translations.mjs +140 -0
- package/dist/lib/modes/eager.mjs +75 -0
- package/dist/lib/modes/lazy.mjs +97 -0
- package/dist/lib/processors/flatten.mjs +453 -0
- package/dist/lib/processors/loader.mjs +355 -0
- package/dist/lib/processors/type-generator.mjs +291 -0
- package/dist/lib/processors/typescript.mjs +188 -0
- package/dist/lib/runtime/runtime-asynclocalstorage.mjs +80 -522
- package/dist/lib/runtime/runtime-livebindings.mjs +45 -390
- package/dist/lib/runtime/runtime.mjs +39 -159
- package/dist/slothlet.mjs +525 -744
- package/docs/API-RULES.md +338 -486
- package/index.cjs +4 -4
- package/index.mjs +82 -45
- package/package.json +143 -30
- package/types/dist/lib/builders/api-assignment.d.mts +97 -0
- package/types/dist/lib/builders/api-assignment.d.mts.map +1 -0
- package/types/dist/lib/builders/api_builder.d.mts +96 -0
- package/types/dist/lib/builders/api_builder.d.mts.map +1 -0
- package/types/dist/lib/builders/builder.d.mts +60 -0
- package/types/dist/lib/builders/builder.d.mts.map +1 -0
- package/types/dist/lib/builders/modes-processor.d.mts +32 -0
- package/types/dist/lib/builders/modes-processor.d.mts.map +1 -0
- package/types/dist/lib/errors.d.mts +118 -0
- package/types/dist/lib/errors.d.mts.map +1 -0
- package/types/dist/lib/factories/component-base.d.mts +182 -0
- package/types/dist/lib/factories/component-base.d.mts.map +1 -0
- package/types/dist/lib/factories/context.d.mts +26 -0
- package/types/dist/lib/factories/context.d.mts.map +1 -0
- package/types/dist/lib/handlers/api-cache-manager.d.mts +208 -0
- package/types/dist/lib/handlers/api-cache-manager.d.mts.map +1 -0
- package/types/dist/lib/handlers/api-manager.d.mts +392 -0
- package/types/dist/lib/handlers/api-manager.d.mts.map +1 -0
- package/types/dist/lib/handlers/context-async.d.mts +66 -0
- package/types/dist/lib/handlers/context-async.d.mts.map +1 -0
- package/types/dist/lib/handlers/context-live.d.mts +65 -0
- package/types/dist/lib/handlers/context-live.d.mts.map +1 -0
- package/types/dist/lib/handlers/hook-manager.d.mts +199 -0
- package/types/dist/lib/handlers/hook-manager.d.mts.map +1 -0
- package/types/dist/lib/handlers/lifecycle-token.d.mts +49 -0
- package/types/dist/lib/handlers/lifecycle-token.d.mts.map +1 -0
- package/types/dist/lib/handlers/lifecycle.d.mts +90 -0
- package/types/dist/lib/handlers/lifecycle.d.mts.map +1 -0
- package/types/dist/lib/handlers/materialize-manager.d.mts +75 -0
- package/types/dist/lib/handlers/materialize-manager.d.mts.map +1 -0
- package/types/dist/lib/handlers/metadata.d.mts +215 -0
- package/types/dist/lib/handlers/metadata.d.mts.map +1 -0
- package/types/dist/lib/handlers/ownership.d.mts +170 -0
- package/types/dist/lib/handlers/ownership.d.mts.map +1 -0
- package/types/dist/lib/handlers/unified-wrapper.d.mts +250 -0
- package/types/dist/lib/handlers/unified-wrapper.d.mts.map +1 -0
- package/types/dist/lib/helpers/class-instance-wrapper.d.mts +54 -0
- package/types/dist/lib/helpers/class-instance-wrapper.d.mts.map +1 -0
- package/types/dist/lib/helpers/config.d.mts +96 -0
- package/types/dist/lib/helpers/config.d.mts.map +1 -0
- package/types/dist/lib/helpers/eventemitter-context.d.mts +31 -0
- package/types/dist/lib/helpers/eventemitter-context.d.mts.map +1 -0
- package/types/dist/lib/helpers/hint-detector.d.mts +20 -0
- package/types/dist/lib/helpers/hint-detector.d.mts.map +1 -0
- package/types/dist/lib/helpers/modes-utils.d.mts +35 -0
- package/types/dist/lib/helpers/modes-utils.d.mts.map +1 -0
- package/types/dist/lib/helpers/resolve-from-caller.d.mts +29 -145
- package/types/dist/lib/helpers/resolve-from-caller.d.mts.map +1 -1
- package/types/dist/lib/helpers/sanitize.d.mts +95 -94
- package/types/dist/lib/helpers/sanitize.d.mts.map +1 -1
- package/types/dist/lib/helpers/utilities.d.mts +53 -116
- package/types/dist/lib/helpers/utilities.d.mts.map +1 -1
- package/types/dist/lib/i18n/translations.d.mts +39 -0
- package/types/dist/lib/i18n/translations.d.mts.map +1 -0
- package/types/dist/lib/modes/eager.d.mts +36 -0
- package/types/dist/lib/modes/eager.d.mts.map +1 -0
- package/types/dist/lib/modes/lazy.d.mts +49 -0
- package/types/dist/lib/modes/lazy.d.mts.map +1 -0
- package/types/dist/lib/processors/flatten.d.mts +114 -0
- package/types/dist/lib/processors/flatten.d.mts.map +1 -0
- package/types/dist/lib/processors/loader.d.mts +47 -0
- package/types/dist/lib/processors/loader.d.mts.map +1 -0
- package/types/dist/lib/processors/type-generator.d.mts +19 -0
- package/types/dist/lib/processors/type-generator.d.mts.map +1 -0
- package/types/dist/lib/processors/typescript.d.mts +55 -0
- package/types/dist/lib/processors/typescript.d.mts.map +1 -0
- package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts +47 -42
- package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts.map +1 -1
- package/types/dist/lib/runtime/runtime-livebindings.d.mts +34 -65
- package/types/dist/lib/runtime/runtime-livebindings.d.mts.map +1 -1
- package/types/dist/lib/runtime/runtime.d.mts +39 -9
- package/types/dist/lib/runtime/runtime.d.mts.map +1 -1
- package/types/dist/slothlet.d.mts +184 -111
- package/types/dist/slothlet.d.mts.map +1 -1
- package/types/index.d.mts +1 -3
- package/dist/lib/engine/README.md +0 -21
- package/dist/lib/engine/slothlet_child.mjs +0 -59
- package/dist/lib/engine/slothlet_engine.mjs +0 -372
- package/dist/lib/engine/slothlet_esm.mjs +0 -230
- package/dist/lib/engine/slothlet_helpers.mjs +0 -455
- package/dist/lib/engine/slothlet_worker.mjs +0 -149
- package/dist/lib/helpers/als-eventemitter.mjs +0 -256
- package/dist/lib/helpers/api_builder/add_api.mjs +0 -553
- package/dist/lib/helpers/api_builder/analysis.mjs +0 -532
- package/dist/lib/helpers/api_builder/construction.mjs +0 -495
- package/dist/lib/helpers/api_builder/decisions.mjs +0 -748
- package/dist/lib/helpers/api_builder/metadata.mjs +0 -248
- package/dist/lib/helpers/api_builder.mjs +0 -41
- package/dist/lib/helpers/auto-wrap.mjs +0 -62
- package/dist/lib/helpers/hooks.mjs +0 -389
- package/dist/lib/helpers/instance-manager.mjs +0 -111
- package/dist/lib/helpers/metadata-api.mjs +0 -201
- package/dist/lib/helpers/multidefault.mjs +0 -216
- package/dist/lib/modes/slothlet_eager.mjs +0 -154
- package/dist/lib/modes/slothlet_lazy.mjs +0 -594
- package/docs/API-RULES-CONDITIONS.md +0 -712
- package/types/dist/lib/engine/slothlet_child.d.mts +0 -2
- package/types/dist/lib/engine/slothlet_child.d.mts.map +0 -1
- package/types/dist/lib/engine/slothlet_engine.d.mts +0 -31
- package/types/dist/lib/engine/slothlet_engine.d.mts.map +0 -1
- package/types/dist/lib/engine/slothlet_esm.d.mts +0 -19
- package/types/dist/lib/engine/slothlet_esm.d.mts.map +0 -1
- package/types/dist/lib/engine/slothlet_helpers.d.mts +0 -25
- package/types/dist/lib/engine/slothlet_helpers.d.mts.map +0 -1
- package/types/dist/lib/engine/slothlet_worker.d.mts +0 -2
- package/types/dist/lib/engine/slothlet_worker.d.mts.map +0 -1
- package/types/dist/lib/helpers/als-eventemitter.d.mts +0 -56
- package/types/dist/lib/helpers/als-eventemitter.d.mts.map +0 -1
- package/types/dist/lib/helpers/api_builder/add_api.d.mts +0 -102
- package/types/dist/lib/helpers/api_builder/add_api.d.mts.map +0 -1
- package/types/dist/lib/helpers/api_builder/analysis.d.mts +0 -189
- package/types/dist/lib/helpers/api_builder/analysis.d.mts.map +0 -1
- package/types/dist/lib/helpers/api_builder/construction.d.mts +0 -107
- package/types/dist/lib/helpers/api_builder/construction.d.mts.map +0 -1
- package/types/dist/lib/helpers/api_builder/decisions.d.mts +0 -213
- package/types/dist/lib/helpers/api_builder/decisions.d.mts.map +0 -1
- package/types/dist/lib/helpers/api_builder/metadata.d.mts +0 -99
- package/types/dist/lib/helpers/api_builder/metadata.d.mts.map +0 -1
- package/types/dist/lib/helpers/api_builder.d.mts +0 -6
- package/types/dist/lib/helpers/api_builder.d.mts.map +0 -1
- package/types/dist/lib/helpers/auto-wrap.d.mts +0 -49
- package/types/dist/lib/helpers/auto-wrap.d.mts.map +0 -1
- package/types/dist/lib/helpers/hooks.d.mts +0 -342
- package/types/dist/lib/helpers/hooks.d.mts.map +0 -1
- package/types/dist/lib/helpers/instance-manager.d.mts +0 -41
- package/types/dist/lib/helpers/instance-manager.d.mts.map +0 -1
- package/types/dist/lib/helpers/metadata-api.d.mts +0 -132
- package/types/dist/lib/helpers/metadata-api.d.mts.map +0 -1
- package/types/dist/lib/helpers/multidefault.d.mts +0 -90
- package/types/dist/lib/helpers/multidefault.d.mts.map +0 -1
- package/types/dist/lib/modes/slothlet_eager.d.mts +0 -65
- package/types/dist/lib/modes/slothlet_eager.d.mts.map +0 -1
- package/types/dist/lib/modes/slothlet_lazy.d.mts +0 -31
- package/types/dist/lib/modes/slothlet_lazy.d.mts.map +0 -1
- package/types/index.d.mts.map +0 -1
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2026 CLDMV/Shinrai
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
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
|
+
removePathMetadata(apiPath, key) {
|
|
375
|
+
if (!apiPath || typeof apiPath !== "string") return;
|
|
376
|
+
|
|
377
|
+
const entry = this.#userMetadataStore.get(apiPath);
|
|
378
|
+
if (!entry) return;
|
|
379
|
+
|
|
380
|
+
if (key === undefined) {
|
|
381
|
+
this.#userMetadataStore.delete(apiPath);
|
|
382
|
+
} else if (Array.isArray(key)) {
|
|
383
|
+
for (const k of key) {
|
|
384
|
+
if (typeof k !== "string") {
|
|
385
|
+
throw new this.SlothletError("INVALID_METADATA_KEY", {
|
|
386
|
+
key: k,
|
|
387
|
+
type: typeof k,
|
|
388
|
+
expected: "string"
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
delete entry.metadata[k];
|
|
392
|
+
}
|
|
393
|
+
} else if (typeof key === "string") {
|
|
394
|
+
delete entry.metadata[key];
|
|
395
|
+
} else {
|
|
396
|
+
throw new this.SlothletError("INVALID_METADATA_KEY", {
|
|
397
|
+
key,
|
|
398
|
+
type: typeof key,
|
|
399
|
+
expected: "string or string[]"
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
exportUserState() {
|
|
406
|
+
const storeCopy = new Map();
|
|
407
|
+
for (const [key, entry] of this.#userMetadataStore) {
|
|
408
|
+
storeCopy.set(key, {
|
|
409
|
+
metadata: { ...entry.metadata },
|
|
410
|
+
apiPaths: new Set(entry.apiPaths)
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
return {
|
|
414
|
+
globalMetadata: { ...this.#globalUserMetadata },
|
|
415
|
+
userMetadataStore: storeCopy
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
importUserState(state) {
|
|
421
|
+
if (!state) return;
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
if (state.globalMetadata) {
|
|
425
|
+
for (const [k, v] of Object.entries(state.globalMetadata)) {
|
|
426
|
+
if (!(k in this.#globalUserMetadata)) {
|
|
427
|
+
this.#globalUserMetadata[k] = v;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
if (state.userMetadataStore) {
|
|
434
|
+
for (const [key, savedEntry] of state.userMetadataStore) {
|
|
435
|
+
const existing = this.#userMetadataStore.get(key);
|
|
436
|
+
if (!existing) {
|
|
437
|
+
|
|
438
|
+
this.#userMetadataStore.set(key, {
|
|
439
|
+
metadata: { ...savedEntry.metadata },
|
|
440
|
+
apiPaths: new Set(savedEntry.apiPaths)
|
|
441
|
+
});
|
|
442
|
+
} else {
|
|
443
|
+
|
|
444
|
+
existing.metadata = { ...savedEntry.metadata, ...existing.metadata };
|
|
445
|
+
for (const p of savedEntry.apiPaths) existing.apiPaths.add(p);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
async get(path) {
|
|
453
|
+
if (typeof path !== "string") {
|
|
454
|
+
throw new this.SlothletError("INVALID_ARGUMENT", {
|
|
455
|
+
argument: "path",
|
|
456
|
+
expected: "string",
|
|
457
|
+
received: typeof path
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
const apiRoot = this.slothlet.api;
|
|
462
|
+
if (!apiRoot) return null;
|
|
463
|
+
|
|
464
|
+
const parts = path.split(".");
|
|
465
|
+
let target = apiRoot;
|
|
466
|
+
|
|
467
|
+
for (const part of parts) {
|
|
468
|
+
if (!target || (typeof target !== "object" && typeof target !== "function")) {
|
|
469
|
+
return null;
|
|
470
|
+
}
|
|
471
|
+
target = target[part];
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
if (target && typeof target._materialize === "function") {
|
|
475
|
+
await target._materialize();
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
if (typeof target === "function" || (target && resolveWrapper(target)?.____slothletInternal?.impl)) {
|
|
479
|
+
return this.getMetadata(target);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
return null;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
self() {
|
|
487
|
+
const ctx = this.slothlet.contextManager?.tryGetContext();
|
|
488
|
+
if (!ctx || !ctx.currentWrapper) {
|
|
489
|
+
throw new this.SlothletError("RUNTIME_NO_ACTIVE_CONTEXT", {}, null, { validationError: true });
|
|
490
|
+
}
|
|
491
|
+
return this.getMetadata(ctx.currentWrapper);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
caller() {
|
|
496
|
+
const ctx = this.slothlet.contextManager?.tryGetContext();
|
|
497
|
+
if (!ctx || !ctx.callerWrapper) return null;
|
|
498
|
+
return this.getMetadata(ctx.callerWrapper);
|
|
499
|
+
}
|
|
500
|
+
}
|