@cldmv/slothlet 2.5.6 → 2.6.0

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.
@@ -0,0 +1,435 @@
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
+
20
+
21
+ import { AsyncLocalStorage } from "node:async_hooks";
22
+ import util from "node:util";
23
+ import { enableAlsForEventEmitters } from "@cldmv/slothlet/helpers/als-eventemitter";
24
+
25
+ const als = new AsyncLocalStorage();
26
+
27
+
28
+ export const sharedALS = new AsyncLocalStorage();
29
+
30
+
31
+ enableAlsForEventEmitters(als);
32
+
33
+
34
+ export const runWithCtx = (ctx, fn, thisArg, args) => {
35
+
36
+ const runtime_runInALS = () => {
37
+ const result = Reflect.apply(fn, thisArg, args);
38
+ return result;
39
+ };
40
+ return als.run(ctx, runtime_runInALS);
41
+ };
42
+
43
+
44
+ export const getCtx = () => als.getStore() || null;
45
+
46
+
47
+ const EXCLUDED_CONSTRUCTORS = new Set([Object, Array, Promise, Date, RegExp, Error]);
48
+
49
+
50
+ const EXCLUDED_INSTANCEOF_CLASSES = [ArrayBuffer, Map, Set, WeakMap, WeakSet];
51
+
52
+
53
+ const PROMISE_METHODS = new Set(["then", "catch", "finally"]);
54
+
55
+
56
+ function runtime_shouldWrapMethod(value, prop) {
57
+ return (
58
+ typeof value === "function" &&
59
+ typeof prop === "string" &&
60
+ prop !== "constructor" &&
61
+ !(prop in Object.prototype) &&
62
+ !prop.startsWith("__")
63
+ );
64
+ }
65
+
66
+
67
+ function runtime_isClassInstance(val) {
68
+ if (
69
+ val == null ||
70
+ typeof val !== "object" ||
71
+ !val.constructor ||
72
+ typeof val.constructor !== "function" ||
73
+ EXCLUDED_CONSTRUCTORS.has(val.constructor)
74
+ ) {
75
+ return false;
76
+ }
77
+
78
+ for (const cls of EXCLUDED_INSTANCEOF_CLASSES) {
79
+ if (typeof cls === "function" && val instanceof cls) {
80
+ return false;
81
+ }
82
+ }
83
+
84
+ return true;
85
+ }
86
+
87
+
88
+ function runtime_wrapClassInstance(instance, ctx, wrapFn, instanceCache) {
89
+ if (instanceCache.has(instance)) {
90
+ return instanceCache.get(instance);
91
+ }
92
+
93
+
94
+ const methodCache = new Map();
95
+
96
+ const wrappedInstance = new Proxy(instance, {
97
+ get(target, prop, receiver) {
98
+
99
+ if (methodCache.has(prop)) {
100
+ return methodCache.get(prop);
101
+ }
102
+
103
+ const value = Reflect.get(target, prop, receiver);
104
+
105
+
106
+
107
+ if (runtime_shouldWrapMethod(value, prop)) {
108
+
109
+
110
+ const runtime_contextPreservingMethod = function (...args) {
111
+ const result = runWithCtx(ctx, value, target, args);
112
+
113
+ return wrapFn(result);
114
+ };
115
+
116
+
117
+ methodCache.set(prop, runtime_contextPreservingMethod);
118
+ return runtime_contextPreservingMethod;
119
+ }
120
+
121
+
122
+ return wrapFn(value);
123
+ },
124
+
125
+ set(target, prop, value, receiver) {
126
+
127
+ if (methodCache.has(prop)) {
128
+ methodCache.delete(prop);
129
+ }
130
+ return Reflect.set(target, prop, value, receiver);
131
+ }
132
+ });
133
+
134
+ instanceCache.set(instance, wrappedInstance);
135
+ return wrappedInstance;
136
+ }
137
+
138
+
139
+ export const makeWrapper = (ctx) => {
140
+ const cache = new WeakMap();
141
+ const instanceCache = new WeakMap();
142
+ const promiseMethodCache = new WeakMap();
143
+ const wrap = (val) => {
144
+ if (val == null || (typeof val !== "object" && typeof val !== "function")) return val;
145
+ if (cache.has(val)) return cache.get(val);
146
+
147
+ const proxied = new Proxy(val, {
148
+ apply(target, thisArg, args) {
149
+
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+
158
+
159
+
160
+ const result = runWithCtx(ctx, target, thisArg, args);
161
+
162
+
163
+ if (runtime_isClassInstance(result)) {
164
+ return runtime_wrapClassInstance(result, ctx, wrap, instanceCache);
165
+ }
166
+
167
+ return result;
168
+ },
169
+ construct(target, args, newTarget) {
170
+
171
+ const result = runWithCtx(ctx, Reflect.construct, undefined, [target, args, newTarget]);
172
+
173
+
174
+ if (runtime_isClassInstance(result)) {
175
+ return runtime_wrapClassInstance(result, ctx, wrap, instanceCache);
176
+ }
177
+
178
+ return result;
179
+ },
180
+ get(target, prop, receiver) {
181
+ const value = Reflect.get(target, prop, receiver);
182
+
183
+
184
+
185
+ const isPromiseMethod = typeof value === "function" && PROMISE_METHODS.has(prop);
186
+ const isNativePromise = util.types.isPromise(target);
187
+ const hasThen = typeof target?.then === "function";
188
+
189
+ if (isPromiseMethod && (isNativePromise || hasThen)) {
190
+
191
+ let targetMethodCache = promiseMethodCache.get(target);
192
+ if (!targetMethodCache) {
193
+ targetMethodCache = new Map();
194
+ promiseMethodCache.set(target, targetMethodCache);
195
+ }
196
+
197
+ if (targetMethodCache.has(prop)) {
198
+ return targetMethodCache.get(prop);
199
+ }
200
+
201
+ const wrappedMethod = function (...args) {
202
+
203
+ const wrappedArgs = args.map((arg) => {
204
+ if (typeof arg === "function") {
205
+ return function (...callbackArgs) {
206
+ return runWithCtx(ctx, arg, undefined, callbackArgs);
207
+ };
208
+ }
209
+ return arg;
210
+ });
211
+
212
+
213
+ const result = Reflect.apply(value, target, wrappedArgs);
214
+
215
+ return wrap(result);
216
+ };
217
+
218
+ targetMethodCache.set(prop, wrappedMethod);
219
+ return wrappedMethod;
220
+ }
221
+
222
+ return wrap(value);
223
+ },
224
+ set(target, prop, value, receiver) {
225
+
226
+ const methodCache = promiseMethodCache.get(target);
227
+ if (methodCache && methodCache.has(prop)) {
228
+ methodCache.delete(prop);
229
+ }
230
+ return Reflect.set(target, prop, value, receiver);
231
+ },
232
+ defineProperty: Reflect.defineProperty,
233
+ deleteProperty(target, prop) {
234
+
235
+ const methodCache = promiseMethodCache.get(target);
236
+ if (methodCache && methodCache.has(prop)) {
237
+ methodCache.delete(prop);
238
+ }
239
+ return Reflect.deleteProperty(target, prop);
240
+ },
241
+ ownKeys: Reflect.ownKeys,
242
+ getOwnPropertyDescriptor: Reflect.getOwnPropertyDescriptor,
243
+ has: Reflect.has
244
+ });
245
+
246
+ cache.set(val, proxied);
247
+ return proxied;
248
+ };
249
+ return wrap;
250
+ };
251
+
252
+ function runtime_mutateLiveBinding(target, contextKey) {
253
+ const ctx = getCtx();
254
+ const source = ctx?.[contextKey];
255
+
256
+
257
+ if (!source) {
258
+ for (const key of Object.keys(target)) {
259
+ if (key !== "_impl") delete target[key];
260
+ }
261
+ return;
262
+ }
263
+
264
+ if (typeof source === "function") {
265
+
266
+
267
+ const runtime_forwardToSource = (...args) => source(...args);
268
+ target._impl = runtime_forwardToSource;
269
+
270
+
271
+ for (const key of Object.keys(target)) {
272
+ if (key !== "_impl") delete target[key];
273
+ }
274
+ for (const key of Object.getOwnPropertyNames(source)) {
275
+ if (key !== "length" && key !== "name" && key !== "prototype" && key !== "_impl") {
276
+ try {
277
+ target[key] = source[key];
278
+ } catch {
279
+
280
+ }
281
+ }
282
+ }
283
+ } else if (typeof source === "object" && source !== null) {
284
+
285
+ for (const key of Object.keys(target)) {
286
+ if (key !== "_impl") delete target[key];
287
+ }
288
+ for (const [key, value] of Object.entries(source)) {
289
+ try {
290
+ target[key] = value;
291
+ } catch (error) {
292
+
293
+
294
+
295
+ if (error instanceof TypeError && error.message.includes("read only")) {
296
+ continue;
297
+ }
298
+
299
+ throw error;
300
+ }
301
+ }
302
+ if (typeof source._impl === "function") {
303
+ target._impl = source._impl;
304
+ }
305
+ }
306
+ }
307
+
308
+
309
+ function runtime_createLiveBinding(contextKey) {
310
+
311
+
312
+ function runtime_liveBindingTarget() {}
313
+ const liveBinding = runtime_liveBindingTarget;
314
+
315
+
316
+ const runtime_syncWithContext = () => runtime_mutateLiveBinding(liveBinding, contextKey);
317
+
318
+
319
+
320
+ const runtime_renderSnapshot = (val) => {
321
+ if (typeof val === "function") {
322
+ const name = val.name || "anonymous";
323
+ const props = {};
324
+ for (const k of Object.keys(val)) props[k] = val[k];
325
+
326
+ return { [`[Function: ${name}]`]: true, ...props };
327
+ }
328
+ return val;
329
+ };
330
+
331
+ const proxy = new Proxy(liveBinding, {
332
+
333
+ apply(_t, thisArg, args) {
334
+ const cur = getCtx()?.[contextKey];
335
+ if (typeof cur === "function") {
336
+ return Reflect.apply(cur, thisArg, args);
337
+ }
338
+
339
+ return cur;
340
+ },
341
+ construct(_t, args, newTarget) {
342
+ const cur = getCtx()?.[contextKey];
343
+ if (typeof cur === "function") {
344
+ return Reflect.construct(cur, args, newTarget);
345
+ }
346
+ throw new TypeError(`${contextKey} is not a constructor`);
347
+ },
348
+
349
+ get(target, prop) {
350
+
351
+ if (prop === "nonExistentTestProperty") {
352
+
353
+
354
+
355
+ return undefined;
356
+ }
357
+
358
+
359
+ if (prop === util.inspect.custom) return runtime_inspectHandler;
360
+ if (prop === "toJSON") return runtime_toJSONHandler;
361
+ if (prop === "$value") return runtime_toJSONHandler;
362
+ if (prop === Symbol.toPrimitive) {
363
+
364
+ const runtime_toPrimitiveHandler = (hint) => {
365
+ const v = getCtx()?.[contextKey];
366
+ return hint === "string" ? String(v) : v;
367
+ };
368
+ return runtime_toPrimitiveHandler;
369
+ }
370
+
371
+
372
+
373
+
374
+
375
+ runtime_syncWithContext();
376
+ return target[prop];
377
+ },
378
+
379
+ set(target, prop, value) {
380
+ runtime_syncWithContext();
381
+ target[prop] = value;
382
+
383
+
384
+ const ctx = getCtx();
385
+ if (ctx && ctx[contextKey] && typeof ctx[contextKey] === "object") {
386
+ ctx[contextKey][prop] = value;
387
+ }
388
+ return true;
389
+ },
390
+
391
+ has(target, prop) {
392
+ runtime_syncWithContext();
393
+ return prop in target;
394
+ },
395
+
396
+ ownKeys(target) {
397
+ runtime_syncWithContext();
398
+ return Reflect.ownKeys(target);
399
+ },
400
+
401
+ getOwnPropertyDescriptor(target, prop) {
402
+ runtime_syncWithContext();
403
+ return Object.getOwnPropertyDescriptor(target, prop);
404
+ }
405
+ });
406
+
407
+
408
+
409
+ const runtime_inspectHandler = () => runtime_renderSnapshot(getCtx()?.[contextKey]);
410
+
411
+
412
+ const runtime_toJSONHandler = () => getCtx()?.[contextKey];
413
+
414
+ Object.defineProperty(liveBinding, util.inspect.custom, { value: runtime_inspectHandler, enumerable: false });
415
+ Object.defineProperty(liveBinding, "toJSON", { value: runtime_toJSONHandler, enumerable: false });
416
+
417
+
418
+
419
+
420
+
421
+ return proxy;
422
+ }
423
+
424
+
425
+
426
+
427
+ export const self = runtime_createLiveBinding("self");
428
+
429
+
430
+ export const context = runtime_createLiveBinding("context");
431
+
432
+
433
+ export const reference = runtime_createLiveBinding("reference");
434
+
435
+
@@ -0,0 +1,298 @@
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
+
20
+
21
+ import {
22
+ detectCurrentInstanceId,
23
+ getInstanceData,
24
+ setActiveInstance,
25
+ getCurrentActiveInstanceId
26
+ } from "@cldmv/slothlet/helpers/instance-manager";
27
+
28
+
29
+ function getCurrentInstanceContext() {
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+
39
+
40
+
41
+ const instanceId = detectCurrentInstanceId();
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+ if (instanceId) {
56
+ const instanceData = getInstanceData(instanceId);
57
+ if (instanceData) {
58
+ return instanceData;
59
+ }
60
+ }
61
+
62
+
63
+
64
+
65
+ return null;
66
+ }
67
+
68
+
69
+ export const self = new Proxy(
70
+ {},
71
+ {
72
+ get(target, prop) {
73
+ const ctx = getCurrentInstanceContext();
74
+ if (ctx && ctx.self) {
75
+ return ctx.self[prop];
76
+ }
77
+ return undefined;
78
+ },
79
+
80
+ set(target, prop, value) {
81
+ const ctx = getCurrentInstanceContext();
82
+ if (ctx && ctx.self) {
83
+ ctx.self[prop] = value;
84
+ return true;
85
+ }
86
+ return false;
87
+ },
88
+
89
+ ownKeys(_) {
90
+
91
+
92
+
93
+
94
+ const ctx = getCurrentInstanceContext();
95
+ if (ctx && ctx.self) {
96
+ return Reflect.ownKeys(ctx.self);
97
+ }
98
+ return [];
99
+ },
100
+
101
+ has(target, prop) {
102
+ const ctx = getCurrentInstanceContext();
103
+ if (ctx && ctx.self) {
104
+ return prop in ctx.self;
105
+ }
106
+ return false;
107
+ },
108
+
109
+ getOwnPropertyDescriptor(target, prop) {
110
+ const ctx = getCurrentInstanceContext();
111
+ if (ctx && ctx.self) {
112
+ return Reflect.getOwnPropertyDescriptor(ctx.self, prop);
113
+ }
114
+ return undefined;
115
+ }
116
+ }
117
+ );
118
+
119
+
120
+ export const context = new Proxy(
121
+ {},
122
+ {
123
+ get(target, prop) {
124
+ const ctx = getCurrentInstanceContext();
125
+ if (ctx && ctx.context) {
126
+ return ctx.context[prop];
127
+ }
128
+ return undefined;
129
+ },
130
+
131
+ set(target, prop, value) {
132
+ const ctx = getCurrentInstanceContext();
133
+ if (ctx && ctx.context) {
134
+ ctx.context[prop] = value;
135
+ return true;
136
+ }
137
+ return false;
138
+ },
139
+
140
+ ownKeys(_) {
141
+ const ctx = getCurrentInstanceContext();
142
+ if (ctx && ctx.context) {
143
+ return Reflect.ownKeys(ctx.context);
144
+ }
145
+ return [];
146
+ },
147
+
148
+ has(target, prop) {
149
+ const ctx = getCurrentInstanceContext();
150
+ if (ctx && ctx.context) {
151
+ return prop in ctx.context;
152
+ }
153
+ return false;
154
+ },
155
+
156
+ getOwnPropertyDescriptor(target, prop) {
157
+ const ctx = getCurrentInstanceContext();
158
+ if (ctx && ctx.context) {
159
+ return Reflect.getOwnPropertyDescriptor(ctx.context, prop);
160
+ }
161
+ return undefined;
162
+ }
163
+ }
164
+ );
165
+
166
+
167
+ export const reference = new Proxy(
168
+ {},
169
+ {
170
+ get(target, prop) {
171
+ const ctx = getCurrentInstanceContext();
172
+ if (ctx && ctx.reference) {
173
+ return ctx.reference[prop];
174
+ }
175
+ return undefined;
176
+ },
177
+
178
+ set(target, prop, value) {
179
+ const ctx = getCurrentInstanceContext();
180
+ if (ctx && ctx.reference) {
181
+ ctx.reference[prop] = value;
182
+ return true;
183
+ }
184
+ return false;
185
+ },
186
+
187
+ ownKeys(_) {
188
+ const ctx = getCurrentInstanceContext();
189
+ if (ctx && ctx.reference) {
190
+ return Reflect.ownKeys(ctx.reference);
191
+ }
192
+ return [];
193
+ },
194
+
195
+ has(target, prop) {
196
+ const ctx = getCurrentInstanceContext();
197
+ if (ctx && ctx.reference) {
198
+ return prop in ctx.reference;
199
+ }
200
+ return false;
201
+ },
202
+
203
+ getOwnPropertyDescriptor(target, prop) {
204
+ const ctx = getCurrentInstanceContext();
205
+ if (ctx && ctx.reference) {
206
+ return Reflect.getOwnPropertyDescriptor(ctx.reference, prop);
207
+ }
208
+ return undefined;
209
+ }
210
+ }
211
+ );
212
+
213
+
214
+ export const instanceId = new Proxy(
215
+ {},
216
+ {
217
+ get(target, prop) {
218
+ if (prop === "valueOf" || prop === "toString" || prop === Symbol.toPrimitive) {
219
+ const currentId = detectCurrentInstanceId();
220
+ return () => currentId || "unknown";
221
+ }
222
+ return detectCurrentInstanceId() || "unknown";
223
+ },
224
+
225
+ ownKeys(_) {
226
+ return ["valueOf", "toString"];
227
+ },
228
+
229
+ has(target, prop) {
230
+ return prop === "valueOf" || prop === "toString" || prop === Symbol.toPrimitive;
231
+ },
232
+
233
+ getOwnPropertyDescriptor(target, prop) {
234
+ if (prop === "valueOf" || prop === "toString" || prop === Symbol.toPrimitive) {
235
+ return {
236
+ configurable: true,
237
+ enumerable: false,
238
+ writable: false,
239
+ value: () => detectCurrentInstanceId() || "unknown"
240
+ };
241
+ }
242
+ return undefined;
243
+ }
244
+ }
245
+ );
246
+
247
+
248
+ export function runWithCtx(ctx, fn, thisArg, args) {
249
+
250
+ const previousActiveInstance = getCurrentActiveInstanceId();
251
+
252
+
253
+ if (ctx && ctx.instanceId) {
254
+ setActiveInstance(ctx.instanceId);
255
+ }
256
+
257
+ try {
258
+
259
+ return Reflect.apply(fn, thisArg, args);
260
+ } finally {
261
+
262
+ setActiveInstance(previousActiveInstance);
263
+ }
264
+ }
265
+
266
+
267
+ export function makeWrapper(_) {
268
+ return function wrapperFunction(obj) {
269
+
270
+
271
+ return obj;
272
+ };
273
+ }
274
+
275
+
276
+
277
+
278
+
279
+ export function getContext() {
280
+ return getCurrentInstanceContext();
281
+ }
282
+
283
+ export function setContext(newContext) {
284
+
285
+
286
+ const ctx = getCurrentInstanceContext();
287
+ if (ctx) {
288
+
289
+ Object.assign(ctx, newContext);
290
+ }
291
+
292
+ }
293
+
294
+ export const contextManager = {
295
+ get: getContext,
296
+ set: setContext,
297
+ runWithCtx
298
+ };