@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
|
@@ -14,558 +14,116 @@
|
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
|
|
18
17
|
|
|
19
18
|
|
|
20
19
|
|
|
21
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
22
|
-
import util from "node:util";
|
|
23
|
-
import { enableAlsForEventEmitters } from "@cldmv/slothlet/helpers/als-eventemitter";
|
|
24
|
-
import { metadataAPI } from "@cldmv/slothlet/helpers/metadata-api";
|
|
25
20
|
|
|
26
|
-
|
|
21
|
+
import { asyncRuntime } from "@cldmv/slothlet/factories/context";
|
|
22
|
+
import { SlothletError } from "@cldmv/slothlet/errors";
|
|
27
23
|
|
|
28
24
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
enableAlsForEventEmitters(als);
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
export const runWithCtx = (ctx, fn, thisArg, args) => {
|
|
39
|
-
|
|
40
|
-
if (!ctx.hookManager?.enabled || !fn.__slothletPath) {
|
|
41
|
-
const runtime_runInALS = () => {
|
|
42
|
-
const result = Reflect.apply(fn, thisArg, args);
|
|
43
|
-
return result;
|
|
44
|
-
};
|
|
45
|
-
return als.run(ctx, runtime_runInALS);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const requestContext = requestALS.getStore();
|
|
50
|
-
const mergedCtx = requestContext ? { ...ctx, context: { ...ctx.context, ...requestContext } } : ctx;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const path = fn.__slothletPath;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const runtime_runInALS = () => {
|
|
57
|
-
try {
|
|
58
|
-
|
|
59
|
-
const beforeResult = mergedCtx.hookManager.executeBeforeHooks(path, args);
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (beforeResult.cancelled) {
|
|
63
|
-
mergedCtx.hookManager.executeAlwaysHooks(path, beforeResult.value, []);
|
|
64
|
-
return beforeResult.value;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const actualArgs = beforeResult.args;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const result = Reflect.apply(fn, thisArg, actualArgs);
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (result && typeof result === "object" && typeof result.then === "function") {
|
|
75
|
-
return result.then(
|
|
76
|
-
(resolvedResult) => {
|
|
77
|
-
|
|
78
|
-
const finalResult = mergedCtx.hookManager.executeAfterHooks(path, resolvedResult);
|
|
79
|
-
mergedCtx.hookManager.executeAlwaysHooks(path, finalResult, []);
|
|
80
|
-
return finalResult;
|
|
81
|
-
},
|
|
82
|
-
(error) => {
|
|
83
|
-
|
|
84
|
-
if (!mergedCtx.hookManager.reportedErrors.has(error)) {
|
|
85
|
-
mergedCtx.hookManager.reportedErrors.add(error);
|
|
86
|
-
mergedCtx.hookManager.executeErrorHooks(path, error, { type: "function" });
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
mergedCtx.hookManager.executeAlwaysHooks(path, undefined, [error]);
|
|
90
|
-
|
|
91
|
-
if (!mergedCtx.hookManager.suppressErrors) {
|
|
92
|
-
throw error;
|
|
93
|
-
}
|
|
94
|
-
return undefined;
|
|
95
|
-
}
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const finalResult = mergedCtx.hookManager.executeAfterHooks(path, result);
|
|
101
|
-
mergedCtx.hookManager.executeAlwaysHooks(path, finalResult, []);
|
|
102
|
-
return finalResult;
|
|
103
|
-
} catch (error) {
|
|
104
|
-
|
|
105
|
-
if (!mergedCtx.hookManager.reportedErrors.has(error)) {
|
|
106
|
-
mergedCtx.hookManager.reportedErrors.add(error);
|
|
107
|
-
mergedCtx.hookManager.executeErrorHooks(path, error, { type: "function" });
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
mergedCtx.hookManager.executeAlwaysHooks(path, undefined, [error]);
|
|
111
|
-
|
|
112
|
-
if (!mergedCtx.hookManager.suppressErrors) {
|
|
113
|
-
throw error;
|
|
114
|
-
}
|
|
115
|
-
return undefined;
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
return als.run(mergedCtx, runtime_runInALS);
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
export const getCtx = () => als.getStore() || null;
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
const EXCLUDED_CONSTRUCTORS = new Set([Object, Array, Promise, Date, RegExp, Error]);
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const EXCLUDED_INSTANCEOF_CLASSES = [ArrayBuffer, Map, Set, WeakMap, WeakSet];
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
const PROMISE_METHODS = new Set(["then", "catch", "finally"]);
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
function runtime_shouldWrapMethod(value, prop) {
|
|
136
|
-
return (
|
|
137
|
-
typeof value === "function" &&
|
|
138
|
-
typeof prop === "string" &&
|
|
139
|
-
prop !== "constructor" &&
|
|
140
|
-
!(prop in Object.prototype) &&
|
|
141
|
-
!prop.startsWith("__")
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
function runtime_shouldExcludeFromProxy(val) {
|
|
147
|
-
if (val == null || typeof val !== "object") {
|
|
148
|
-
return false;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
for (const cls of EXCLUDED_INSTANCEOF_CLASSES) {
|
|
154
|
-
if (typeof cls === "function" && val instanceof cls) {
|
|
155
|
-
return true;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
return false;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
function runtime_isClassInstance(val) {
|
|
164
|
-
if (
|
|
165
|
-
val == null ||
|
|
166
|
-
typeof val !== "object" ||
|
|
167
|
-
!val.constructor ||
|
|
168
|
-
typeof val.constructor !== "function" ||
|
|
169
|
-
EXCLUDED_CONSTRUCTORS.has(val.constructor)
|
|
170
|
-
) {
|
|
171
|
-
return false;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
for (const cls of EXCLUDED_INSTANCEOF_CLASSES) {
|
|
175
|
-
if (typeof cls === "function" && val instanceof cls) {
|
|
176
|
-
return false;
|
|
177
|
-
}
|
|
25
|
+
function safeGetContext() {
|
|
26
|
+
try {
|
|
27
|
+
return asyncRuntime.getContext();
|
|
28
|
+
} catch {
|
|
29
|
+
return null;
|
|
178
30
|
}
|
|
179
|
-
|
|
180
|
-
return true;
|
|
181
31
|
}
|
|
182
32
|
|
|
183
33
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const wrappedInstance = new Proxy(instance, {
|
|
193
|
-
get(target, prop, receiver) {
|
|
194
|
-
|
|
195
|
-
if (methodCache.has(prop)) {
|
|
196
|
-
return methodCache.get(prop);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const value = Reflect.get(target, prop, receiver);
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
if (runtime_shouldWrapMethod(value, prop)) {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
const runtime_contextPreservingMethod = function (...args) {
|
|
207
|
-
const result = runWithCtx(ctx, value, target, args);
|
|
208
|
-
|
|
209
|
-
return wrapFn(result);
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
methodCache.set(prop, runtime_contextPreservingMethod);
|
|
214
|
-
return runtime_contextPreservingMethod;
|
|
34
|
+
export const self = new Proxy(
|
|
35
|
+
{},
|
|
36
|
+
{
|
|
37
|
+
get(_, prop) {
|
|
38
|
+
const ctx = safeGetContext();
|
|
39
|
+
if (!ctx || !ctx.self) {
|
|
40
|
+
throw new SlothletError("RUNTIME_NO_ACTIVE_CONTEXT_SELF", {}, null, { validationError: true });
|
|
215
41
|
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
return wrapFn(value);
|
|
42
|
+
return ctx.self[prop];
|
|
219
43
|
},
|
|
220
|
-
|
|
221
|
-
|
|
44
|
+
ownKeys() {
|
|
45
|
+
const ctx = safeGetContext();
|
|
46
|
+
if (!ctx || !ctx.self) return [];
|
|
47
|
+
return Reflect.ownKeys(ctx.self);
|
|
48
|
+
},
|
|
49
|
+
has(_, prop) {
|
|
50
|
+
const ctx = safeGetContext();
|
|
51
|
+
if (!ctx || !ctx.self) return false;
|
|
52
|
+
return prop in ctx.self;
|
|
53
|
+
},
|
|
54
|
+
getOwnPropertyDescriptor(_, prop) {
|
|
55
|
+
const ctx = safeGetContext();
|
|
56
|
+
if (!ctx || !ctx.self) return undefined;
|
|
57
|
+
const desc = Reflect.getOwnPropertyDescriptor(ctx.self, prop);
|
|
58
|
+
|
|
222
59
|
|
|
223
|
-
if (
|
|
224
|
-
|
|
60
|
+
if (desc) {
|
|
61
|
+
return { ...desc, configurable: true };
|
|
225
62
|
}
|
|
226
|
-
return
|
|
227
|
-
}
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
instanceCache.set(instance, wrappedInstance);
|
|
231
|
-
return wrappedInstance;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
export const makeWrapper = (ctx) => {
|
|
236
|
-
const cache = new WeakMap();
|
|
237
|
-
const instanceCache = new WeakMap();
|
|
238
|
-
const promiseMethodCache = new WeakMap();
|
|
239
|
-
const wrap = (val, currentPath = "") => {
|
|
240
|
-
if (val == null || (typeof val !== "object" && typeof val !== "function")) return val;
|
|
241
|
-
if (cache.has(val)) return cache.get(val);
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
if (runtime_shouldExcludeFromProxy(val)) {
|
|
246
|
-
cache.set(val, val);
|
|
247
|
-
return val;
|
|
63
|
+
return undefined;
|
|
248
64
|
}
|
|
249
|
-
|
|
250
|
-
const proxied = new Proxy(val, {
|
|
251
|
-
apply(target, thisArg, args) {
|
|
252
|
-
const result = runWithCtx(ctx, target, thisArg, args);
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
if (runtime_isClassInstance(result)) {
|
|
256
|
-
return runtime_wrapClassInstance(result, ctx, wrap, instanceCache);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
return result;
|
|
260
|
-
},
|
|
261
|
-
construct(target, args, newTarget) {
|
|
262
|
-
|
|
263
|
-
const result = runWithCtx(ctx, Reflect.construct, undefined, [target, args, newTarget]);
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
if (runtime_isClassInstance(result)) {
|
|
267
|
-
return runtime_wrapClassInstance(result, ctx, wrap, instanceCache);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
return result;
|
|
271
|
-
},
|
|
272
|
-
get(target, prop, receiver) {
|
|
273
|
-
const value = Reflect.get(target, prop, receiver);
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
const newPath = currentPath ? `${currentPath}.${String(prop)}` : String(prop);
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
const isInternalProperty = currentPath === "" && ["hooks", "__ctx", "shutdown", "_impl"].includes(String(prop));
|
|
280
|
-
const isInternalPath = newPath.startsWith("hooks.") || newPath.startsWith("__ctx.") || newPath.startsWith("shutdown.");
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
if (typeof value === "function" && !value.__slothletPath && !isInternalProperty && !isInternalPath) {
|
|
284
|
-
try {
|
|
285
|
-
Object.defineProperty(value, "__slothletPath", {
|
|
286
|
-
value: newPath,
|
|
287
|
-
writable: false,
|
|
288
|
-
enumerable: false,
|
|
289
|
-
configurable: true
|
|
290
|
-
});
|
|
291
|
-
} catch {
|
|
292
|
-
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
const isPromiseMethod = typeof value === "function" && PROMISE_METHODS.has(prop);
|
|
299
|
-
const isNativePromise = util.types.isPromise(target);
|
|
300
|
-
const hasThen = typeof target?.then === "function";
|
|
301
|
-
|
|
302
|
-
if (isPromiseMethod && (isNativePromise || hasThen)) {
|
|
303
|
-
|
|
304
|
-
let targetMethodCache = promiseMethodCache.get(target);
|
|
305
|
-
if (!targetMethodCache) {
|
|
306
|
-
targetMethodCache = new Map();
|
|
307
|
-
promiseMethodCache.set(target, targetMethodCache);
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
if (targetMethodCache.has(prop)) {
|
|
311
|
-
return targetMethodCache.get(prop);
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
const wrappedMethod = function (...args) {
|
|
315
|
-
|
|
316
|
-
const wrappedArgs = args.map((arg) => {
|
|
317
|
-
if (typeof arg === "function") {
|
|
318
|
-
return function (...callbackArgs) {
|
|
319
|
-
return runWithCtx(ctx, arg, undefined, callbackArgs);
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
return arg;
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
const result = Reflect.apply(value, target, wrappedArgs);
|
|
327
|
-
|
|
328
|
-
return wrap(result, newPath);
|
|
329
|
-
};
|
|
330
|
-
|
|
331
|
-
targetMethodCache.set(prop, wrappedMethod);
|
|
332
|
-
return wrappedMethod;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
return wrap(value, newPath);
|
|
336
|
-
},
|
|
337
|
-
set(target, prop, value, receiver) {
|
|
338
|
-
|
|
339
|
-
const methodCache = promiseMethodCache.get(target);
|
|
340
|
-
if (methodCache && methodCache.has(prop)) {
|
|
341
|
-
methodCache.delete(prop);
|
|
342
|
-
}
|
|
343
|
-
return Reflect.set(target, prop, value, receiver);
|
|
344
|
-
},
|
|
345
|
-
defineProperty: Reflect.defineProperty,
|
|
346
|
-
deleteProperty(target, prop) {
|
|
347
|
-
|
|
348
|
-
const methodCache = promiseMethodCache.get(target);
|
|
349
|
-
if (methodCache && methodCache.has(prop)) {
|
|
350
|
-
methodCache.delete(prop);
|
|
351
|
-
}
|
|
352
|
-
return Reflect.deleteProperty(target, prop);
|
|
353
|
-
},
|
|
354
|
-
ownKeys: Reflect.ownKeys,
|
|
355
|
-
getOwnPropertyDescriptor: Reflect.getOwnPropertyDescriptor,
|
|
356
|
-
has: Reflect.has
|
|
357
|
-
});
|
|
358
|
-
|
|
359
|
-
cache.set(val, proxied);
|
|
360
|
-
return proxied;
|
|
361
|
-
};
|
|
362
|
-
return wrap;
|
|
363
|
-
};
|
|
364
|
-
|
|
365
|
-
function runtime_mutateLiveBinding(target, contextKey) {
|
|
366
|
-
const ctx = getCtx();
|
|
367
|
-
const source = ctx?.[contextKey];
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
if (!source) {
|
|
371
|
-
for (const key of Object.keys(target)) {
|
|
372
|
-
if (key !== "_impl") delete target[key];
|
|
373
|
-
}
|
|
374
|
-
return;
|
|
375
65
|
}
|
|
66
|
+
);
|
|
376
67
|
|
|
377
|
-
if (typeof source === "function") {
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
const runtime_forwardToSource = (...args) => source(...args);
|
|
381
|
-
target._impl = runtime_forwardToSource;
|
|
382
68
|
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
if (
|
|
389
|
-
|
|
390
|
-
target[key] = source[key];
|
|
391
|
-
} catch {
|
|
392
|
-
|
|
393
|
-
}
|
|
69
|
+
export const context = new Proxy(
|
|
70
|
+
{},
|
|
71
|
+
{
|
|
72
|
+
get(_, prop) {
|
|
73
|
+
const ctx = safeGetContext();
|
|
74
|
+
if (!ctx || !ctx.context) {
|
|
75
|
+
return undefined;
|
|
394
76
|
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
if (
|
|
400
|
-
|
|
401
|
-
for (const [key, value] of Object.entries(source)) {
|
|
402
|
-
try {
|
|
403
|
-
target[key] = value;
|
|
404
|
-
} catch (error) {
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
if (error instanceof TypeError && error.message.includes("read only")) {
|
|
409
|
-
continue;
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
throw error;
|
|
77
|
+
return ctx.context[prop];
|
|
78
|
+
},
|
|
79
|
+
set(_, prop, value) {
|
|
80
|
+
const ctx = safeGetContext();
|
|
81
|
+
if (!ctx || !ctx.context) {
|
|
82
|
+
throw new SlothletError("RUNTIME_NO_ACTIVE_CONTEXT_CONTEXT", {}, null, { validationError: true });
|
|
413
83
|
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
84
|
+
ctx.context[prop] = value;
|
|
85
|
+
return true;
|
|
86
|
+
},
|
|
87
|
+
ownKeys() {
|
|
88
|
+
const ctx = safeGetContext();
|
|
89
|
+
if (!ctx || !ctx.context) return [];
|
|
90
|
+
return Reflect.ownKeys(ctx.context);
|
|
91
|
+
},
|
|
92
|
+
has(_, prop) {
|
|
93
|
+
const ctx = safeGetContext();
|
|
94
|
+
if (!ctx || !ctx.context) return false;
|
|
95
|
+
return prop in ctx.context;
|
|
96
|
+
},
|
|
97
|
+
getOwnPropertyDescriptor(_, prop) {
|
|
98
|
+
const ctx = safeGetContext();
|
|
99
|
+
if (!ctx || !ctx.context) return undefined;
|
|
100
|
+
return Reflect.getOwnPropertyDescriptor(ctx.context, prop);
|
|
417
101
|
}
|
|
418
102
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
function runtime_createLiveBinding(contextKey) {
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
function runtime_liveBindingTarget() {}
|
|
426
|
-
const liveBinding = runtime_liveBindingTarget;
|
|
103
|
+
);
|
|
427
104
|
|
|
428
|
-
|
|
429
|
-
const runtime_syncWithContext = () => runtime_mutateLiveBinding(liveBinding, contextKey);
|
|
430
105
|
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
const runtime_renderSnapshot = (val) => {
|
|
434
|
-
if (typeof val === "function") {
|
|
435
|
-
const name = val.name || "anonymous";
|
|
436
|
-
const props = {};
|
|
437
|
-
for (const k of Object.keys(val)) props[k] = val[k];
|
|
438
|
-
|
|
439
|
-
return { [`[Function: ${name}]`]: true, ...props };
|
|
440
|
-
}
|
|
441
|
-
return val;
|
|
442
|
-
};
|
|
443
106
|
|
|
444
|
-
const proxy = new Proxy(liveBinding, {
|
|
445
|
-
|
|
446
|
-
apply(_t, thisArg, args) {
|
|
447
|
-
const cur = getCtx()?.[contextKey];
|
|
448
|
-
if (typeof cur === "function") {
|
|
449
|
-
return Reflect.apply(cur, thisArg, args);
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
return cur;
|
|
453
|
-
},
|
|
454
|
-
construct(_t, args, newTarget) {
|
|
455
|
-
const cur = getCtx()?.[contextKey];
|
|
456
|
-
if (typeof cur === "function") {
|
|
457
|
-
return Reflect.construct(cur, args, newTarget);
|
|
458
|
-
}
|
|
459
|
-
throw new TypeError(`${contextKey} is not a constructor`);
|
|
460
|
-
},
|
|
461
107
|
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
108
|
+
export const instanceID = new Proxy(
|
|
109
|
+
{},
|
|
110
|
+
{
|
|
111
|
+
get(_, prop) {
|
|
112
|
+
const ctx = safeGetContext();
|
|
113
|
+
if (!ctx || !ctx.instanceID) {
|
|
468
114
|
return undefined;
|
|
469
115
|
}
|
|
470
|
-
|
|
471
116
|
|
|
472
|
-
if (prop ===
|
|
473
|
-
|
|
474
|
-
if (prop === "$value") return runtime_toJSONHandler;
|
|
475
|
-
if (prop === Symbol.toPrimitive) {
|
|
476
|
-
|
|
477
|
-
const runtime_toPrimitiveHandler = (hint) => {
|
|
478
|
-
const v = getCtx()?.[contextKey];
|
|
479
|
-
return hint === "string" ? String(v) : v;
|
|
480
|
-
};
|
|
481
|
-
return runtime_toPrimitiveHandler;
|
|
117
|
+
if (prop === Symbol.toPrimitive || prop === "toString" || prop === "valueOf") {
|
|
118
|
+
return () => ctx.instanceID;
|
|
482
119
|
}
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
if (contextKey === "context") {
|
|
489
|
-
const ctx = getCtx();
|
|
490
|
-
const baseContext = ctx?.context || {};
|
|
491
|
-
const requestContext = requestALS.getStore() || {};
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
const isDeepMerge = ctx?.config?.scope?.merge === "deep";
|
|
496
|
-
|
|
497
|
-
if (isDeepMerge && Object.keys(requestContext).length > 0) {
|
|
498
|
-
|
|
499
|
-
return requestContext[prop];
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
const merged = { ...baseContext, ...requestContext };
|
|
504
|
-
return merged[prop];
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
runtime_syncWithContext();
|
|
509
|
-
return target[prop];
|
|
120
|
+
return ctx.instanceID[prop];
|
|
510
121
|
},
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
target[prop] = value;
|
|
515
|
-
|
|
122
|
+
has(_, prop) {
|
|
123
|
+
const ctx = safeGetContext();
|
|
124
|
+
if (!ctx || !ctx.instanceID) return false;
|
|
516
125
|
|
|
517
|
-
|
|
518
|
-
if (ctx && ctx[contextKey] && typeof ctx[contextKey] === "object") {
|
|
519
|
-
ctx[contextKey][prop] = value;
|
|
520
|
-
}
|
|
521
|
-
return true;
|
|
522
|
-
},
|
|
523
|
-
|
|
524
|
-
has(target, prop) {
|
|
525
|
-
runtime_syncWithContext();
|
|
526
|
-
return prop in target;
|
|
527
|
-
},
|
|
528
|
-
|
|
529
|
-
ownKeys(target) {
|
|
530
|
-
runtime_syncWithContext();
|
|
531
|
-
return Reflect.ownKeys(target);
|
|
532
|
-
},
|
|
533
|
-
|
|
534
|
-
getOwnPropertyDescriptor(target, prop) {
|
|
535
|
-
runtime_syncWithContext();
|
|
536
|
-
return Object.getOwnPropertyDescriptor(target, prop);
|
|
126
|
+
return prop in Object(ctx.instanceID);
|
|
537
127
|
}
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
const runtime_inspectHandler = () => runtime_renderSnapshot(getCtx()?.[contextKey]);
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
const runtime_toJSONHandler = () => getCtx()?.[contextKey];
|
|
546
|
-
|
|
547
|
-
Object.defineProperty(liveBinding, util.inspect.custom, { value: runtime_inspectHandler, enumerable: false });
|
|
548
|
-
Object.defineProperty(liveBinding, "toJSON", { value: runtime_toJSONHandler, enumerable: false });
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
return proxy;
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
export const self = runtime_createLiveBinding("self");
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
export const context = runtime_createLiveBinding("context");
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
export const reference = runtime_createLiveBinding("reference");
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
export { metadataAPI };
|
|
570
|
-
|
|
571
|
-
|
|
128
|
+
}
|
|
129
|
+
);
|