@cldmv/slothlet 1.0.1 → 2.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/README.md +862 -73
- package/dist/lib/engine/README.md +21 -0
- package/dist/lib/engine/slothlet_child.mjs +58 -0
- package/dist/lib/engine/slothlet_engine.mjs +371 -0
- package/dist/lib/engine/slothlet_esm.mjs +229 -0
- package/dist/lib/engine/slothlet_helpers.mjs +454 -0
- package/dist/lib/engine/slothlet_worker.mjs +148 -0
- package/dist/lib/helpers/resolve-from-caller.mjs +141 -0
- package/dist/lib/helpers/sanitize.mjs +78 -0
- package/dist/lib/modes/slothlet_eager.mjs +80 -0
- package/dist/lib/modes/slothlet_lazy.mjs +342 -0
- package/dist/lib/runtime/runtime.mjs +249 -0
- package/dist/slothlet.mjs +1092 -0
- package/index.cjs +81 -0
- package/index.mjs +76 -0
- package/package.json +136 -14
- package/types/dist/lib/engine/slothlet_child.d.mts +2 -0
- package/types/dist/lib/engine/slothlet_child.d.mts.map +1 -0
- package/types/dist/lib/engine/slothlet_engine.d.mts +31 -0
- package/types/dist/lib/engine/slothlet_engine.d.mts.map +1 -0
- package/types/dist/lib/engine/slothlet_esm.d.mts +19 -0
- package/types/dist/lib/engine/slothlet_esm.d.mts.map +1 -0
- package/types/dist/lib/engine/slothlet_helpers.d.mts +24 -0
- package/types/dist/lib/engine/slothlet_helpers.d.mts.map +1 -0
- package/types/dist/lib/engine/slothlet_worker.d.mts +2 -0
- package/types/dist/lib/engine/slothlet_worker.d.mts.map +1 -0
- package/types/dist/lib/helpers/resolve-from-caller.d.mts +149 -0
- package/types/dist/lib/helpers/resolve-from-caller.d.mts.map +1 -0
- package/types/dist/lib/helpers/sanitize.d.mts +138 -0
- package/types/dist/lib/helpers/sanitize.d.mts.map +1 -0
- package/types/dist/lib/modes/slothlet_eager.d.mts +66 -0
- package/types/dist/lib/modes/slothlet_eager.d.mts.map +1 -0
- package/types/dist/lib/modes/slothlet_lazy.d.mts +32 -0
- package/types/dist/lib/modes/slothlet_lazy.d.mts.map +1 -0
- package/types/dist/lib/runtime/runtime.d.mts +49 -0
- package/types/dist/lib/runtime/runtime.d.mts.map +1 -0
- package/types/dist/slothlet.d.mts +110 -0
- package/types/dist/slothlet.d.mts.map +1 -0
- package/types/index.d.mts +23 -0
- package/slothlet.mjs +0 -1218
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2025 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
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
20
|
+
import util from "node:util";
|
|
21
|
+
|
|
22
|
+
const als = new AsyncLocalStorage();
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
export const runWithCtx = (ctx, fn, thisArg, args) => {
|
|
26
|
+
|
|
27
|
+
const runtime_runInALS = () => {
|
|
28
|
+
const result = Reflect.apply(fn, thisArg, args);
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
return als.run(ctx, runtime_runInALS);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
export const getCtx = () => als.getStore() || null;
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
export const makeWrapper = (ctx) => {
|
|
39
|
+
const cache = new WeakMap();
|
|
40
|
+
const wrap = (val) => {
|
|
41
|
+
if (val == null || (typeof val !== "object" && typeof val !== "function")) return val;
|
|
42
|
+
if (cache.has(val)) return cache.get(val);
|
|
43
|
+
|
|
44
|
+
const proxied = new Proxy(val, {
|
|
45
|
+
apply(target, thisArg, args) {
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
return runWithCtx(ctx, target, thisArg, args);
|
|
57
|
+
},
|
|
58
|
+
construct(target, args, newTarget) {
|
|
59
|
+
|
|
60
|
+
return runWithCtx(ctx, Reflect.construct, undefined, [target, args, newTarget]);
|
|
61
|
+
},
|
|
62
|
+
get(target, prop, receiver) {
|
|
63
|
+
return wrap(Reflect.get(target, prop, receiver));
|
|
64
|
+
},
|
|
65
|
+
set: Reflect.set,
|
|
66
|
+
defineProperty: Reflect.defineProperty,
|
|
67
|
+
deleteProperty: Reflect.deleteProperty,
|
|
68
|
+
ownKeys: Reflect.ownKeys,
|
|
69
|
+
getOwnPropertyDescriptor: Reflect.getOwnPropertyDescriptor,
|
|
70
|
+
has: Reflect.has
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
cache.set(val, proxied);
|
|
74
|
+
return proxied;
|
|
75
|
+
};
|
|
76
|
+
return wrap;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
function runtime_mutateLiveBinding(target, contextKey) {
|
|
80
|
+
const ctx = getCtx();
|
|
81
|
+
const source = ctx?.[contextKey];
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
if (!source) {
|
|
85
|
+
for (const key of Object.keys(target)) {
|
|
86
|
+
if (key !== "_impl") delete target[key];
|
|
87
|
+
}
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (typeof source === "function") {
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
const runtime_forwardToSource = (...args) => source(...args);
|
|
95
|
+
target._impl = runtime_forwardToSource;
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
for (const key of Object.keys(target)) {
|
|
99
|
+
if (key !== "_impl") delete target[key];
|
|
100
|
+
}
|
|
101
|
+
for (const key of Object.getOwnPropertyNames(source)) {
|
|
102
|
+
if (key !== "length" && key !== "name" && key !== "prototype" && key !== "_impl") {
|
|
103
|
+
try {
|
|
104
|
+
target[key] = source[key];
|
|
105
|
+
} catch {
|
|
106
|
+
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
} else if (typeof source === "object" && source !== null) {
|
|
111
|
+
|
|
112
|
+
for (const key of Object.keys(target)) {
|
|
113
|
+
if (key !== "_impl") delete target[key];
|
|
114
|
+
}
|
|
115
|
+
for (const [key, value] of Object.entries(source)) {
|
|
116
|
+
target[key] = value;
|
|
117
|
+
}
|
|
118
|
+
if (typeof source._impl === "function") {
|
|
119
|
+
target._impl = source._impl;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
function runtime_createLiveBinding(contextKey) {
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
function runtime_liveBindingTarget() {}
|
|
129
|
+
const liveBinding = runtime_liveBindingTarget;
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
const runtime_syncWithContext = () => runtime_mutateLiveBinding(liveBinding, contextKey);
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
const runtime_renderSnapshot = (val) => {
|
|
137
|
+
if (typeof val === "function") {
|
|
138
|
+
const name = val.name || "anonymous";
|
|
139
|
+
const props = {};
|
|
140
|
+
for (const k of Object.keys(val)) props[k] = val[k];
|
|
141
|
+
|
|
142
|
+
return { [`[Function: ${name}]`]: true, ...props };
|
|
143
|
+
}
|
|
144
|
+
return val;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const proxy = new Proxy(liveBinding, {
|
|
148
|
+
|
|
149
|
+
apply(_t, thisArg, args) {
|
|
150
|
+
const cur = getCtx()?.[contextKey];
|
|
151
|
+
if (typeof cur === "function") {
|
|
152
|
+
return Reflect.apply(cur, thisArg, args);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return cur;
|
|
156
|
+
},
|
|
157
|
+
construct(_t, args, newTarget) {
|
|
158
|
+
const cur = getCtx()?.[contextKey];
|
|
159
|
+
if (typeof cur === "function") {
|
|
160
|
+
return Reflect.construct(cur, args, newTarget);
|
|
161
|
+
}
|
|
162
|
+
throw new TypeError(`${contextKey} is not a constructor`);
|
|
163
|
+
},
|
|
164
|
+
|
|
165
|
+
get(target, prop) {
|
|
166
|
+
|
|
167
|
+
if (prop === "nonExistentTestProperty") {
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
if (prop === util.inspect.custom) return runtime_inspectHandler;
|
|
176
|
+
if (prop === "toJSON") return runtime_toJSONHandler;
|
|
177
|
+
if (prop === "$value") return runtime_toJSONHandler;
|
|
178
|
+
if (prop === Symbol.toPrimitive) {
|
|
179
|
+
|
|
180
|
+
const runtime_toPrimitiveHandler = (hint) => {
|
|
181
|
+
const v = getCtx()?.[contextKey];
|
|
182
|
+
return hint === "string" ? String(v) : v;
|
|
183
|
+
};
|
|
184
|
+
return runtime_toPrimitiveHandler;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
runtime_syncWithContext();
|
|
192
|
+
return target[prop];
|
|
193
|
+
},
|
|
194
|
+
|
|
195
|
+
set(target, prop, value) {
|
|
196
|
+
runtime_syncWithContext();
|
|
197
|
+
target[prop] = value;
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
const ctx = getCtx();
|
|
201
|
+
if (ctx && ctx[contextKey] && typeof ctx[contextKey] === "object") {
|
|
202
|
+
ctx[contextKey][prop] = value;
|
|
203
|
+
}
|
|
204
|
+
return true;
|
|
205
|
+
},
|
|
206
|
+
|
|
207
|
+
has(target, prop) {
|
|
208
|
+
runtime_syncWithContext();
|
|
209
|
+
return prop in target;
|
|
210
|
+
},
|
|
211
|
+
|
|
212
|
+
ownKeys(target) {
|
|
213
|
+
runtime_syncWithContext();
|
|
214
|
+
return Reflect.ownKeys(target);
|
|
215
|
+
},
|
|
216
|
+
|
|
217
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
218
|
+
runtime_syncWithContext();
|
|
219
|
+
return Object.getOwnPropertyDescriptor(target, prop);
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
const runtime_inspectHandler = () => runtime_renderSnapshot(getCtx()?.[contextKey]);
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
const runtime_toJSONHandler = () => getCtx()?.[contextKey];
|
|
229
|
+
|
|
230
|
+
Object.defineProperty(liveBinding, util.inspect.custom, { value: runtime_inspectHandler, enumerable: false });
|
|
231
|
+
Object.defineProperty(liveBinding, "toJSON", { value: runtime_toJSONHandler, enumerable: false });
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
return proxy;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
export const self = runtime_createLiveBinding("self");
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
export const context = runtime_createLiveBinding("context");
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
export const reference = runtime_createLiveBinding("reference");
|