@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.
Files changed (127) hide show
  1. package/README.md +6 -8
  2. package/REFERENCE.md +23 -0
  3. package/dist/lib/builders/api-assignment.mjs +1 -589
  4. package/dist/lib/builders/api_builder.mjs +1 -1385
  5. package/dist/lib/builders/builder.mjs +1 -78
  6. package/dist/lib/builders/modes-processor.mjs +1 -1800
  7. package/dist/lib/errors.mjs +9 -211
  8. package/dist/lib/factories/component-base.mjs +1 -80
  9. package/dist/lib/factories/context.mjs +1 -22
  10. package/dist/lib/handlers/api-cache-manager.mjs +1 -200
  11. package/dist/lib/handlers/api-manager.mjs +1 -2536
  12. package/dist/lib/handlers/context-async.mjs +1 -172
  13. package/dist/lib/handlers/context-live.mjs +1 -173
  14. package/dist/lib/handlers/hook-manager.mjs +1 -667
  15. package/dist/lib/handlers/lifecycle-token.mjs +1 -28
  16. package/dist/lib/handlers/lifecycle.mjs +1 -115
  17. package/dist/lib/handlers/materialize-manager.mjs +1 -48
  18. package/dist/lib/handlers/metadata.mjs +1 -501
  19. package/dist/lib/handlers/ownership.mjs +1 -322
  20. package/dist/lib/handlers/permission-manager.mjs +1 -392
  21. package/dist/lib/handlers/unified-wrapper.mjs +1 -3110
  22. package/dist/lib/handlers/version-manager.mjs +1 -885
  23. package/dist/lib/helpers/class-instance-wrapper.mjs +1 -109
  24. package/dist/lib/helpers/config.mjs +1 -439
  25. package/dist/lib/helpers/eventemitter-context.mjs +1 -349
  26. package/dist/lib/helpers/hint-detector.mjs +1 -47
  27. package/dist/lib/helpers/modes-utils.mjs +1 -37
  28. package/dist/lib/helpers/pattern-matcher.mjs +1 -125
  29. package/dist/lib/helpers/resolve-from-caller.mjs +1 -169
  30. package/dist/lib/helpers/sanitize.mjs +1 -340
  31. package/dist/lib/helpers/utilities.mjs +1 -70
  32. package/dist/lib/i18n/translations.mjs +1 -126
  33. package/dist/lib/modes/eager.mjs +1 -59
  34. package/dist/lib/modes/lazy.mjs +1 -81
  35. package/dist/lib/processors/flatten.mjs +1 -437
  36. package/dist/lib/processors/loader.mjs +1 -339
  37. package/dist/lib/processors/type-generator.mjs +1 -275
  38. package/dist/lib/processors/typescript.mjs +1 -172
  39. package/dist/lib/runtime/runtime-asynclocalstorage.mjs +1 -113
  40. package/dist/lib/runtime/runtime-livebindings.mjs +1 -78
  41. package/dist/lib/runtime/runtime.mjs +1 -102
  42. package/dist/slothlet.mjs +1 -817
  43. package/package.json +34 -31
  44. package/types/dist/lib/builders/api-assignment.d.mts +3 -92
  45. package/types/dist/lib/builders/api-assignment.d.mts.map +1 -1
  46. package/types/dist/lib/builders/api_builder.d.mts +102 -91
  47. package/types/dist/lib/builders/api_builder.d.mts.map +1 -1
  48. package/types/dist/lib/builders/builder.d.mts +1 -55
  49. package/types/dist/lib/builders/builder.d.mts.map +1 -1
  50. package/types/dist/lib/builders/modes-processor.d.mts +3 -27
  51. package/types/dist/lib/builders/modes-processor.d.mts.map +1 -1
  52. package/types/dist/lib/errors.d.mts +19 -109
  53. package/types/dist/lib/errors.d.mts.map +1 -1
  54. package/types/dist/lib/factories/component-base.d.mts +7 -177
  55. package/types/dist/lib/factories/component-base.d.mts.map +1 -1
  56. package/types/dist/lib/factories/context.d.mts +4 -22
  57. package/types/dist/lib/factories/context.d.mts.map +1 -1
  58. package/types/dist/lib/handlers/api-cache-manager.d.mts +20 -203
  59. package/types/dist/lib/handlers/api-cache-manager.d.mts.map +1 -1
  60. package/types/dist/lib/handlers/api-manager.d.mts +33 -408
  61. package/types/dist/lib/handlers/api-manager.d.mts.map +1 -1
  62. package/types/dist/lib/handlers/context-async.d.mts +23 -61
  63. package/types/dist/lib/handlers/context-async.d.mts.map +1 -1
  64. package/types/dist/lib/handlers/context-live.d.mts +22 -59
  65. package/types/dist/lib/handlers/context-live.d.mts.map +1 -1
  66. package/types/dist/lib/handlers/hook-manager.d.mts +46 -185
  67. package/types/dist/lib/handlers/hook-manager.d.mts.map +1 -1
  68. package/types/dist/lib/handlers/lifecycle-token.d.mts +3 -48
  69. package/types/dist/lib/handlers/lifecycle-token.d.mts.map +1 -1
  70. package/types/dist/lib/handlers/lifecycle.d.mts +5 -82
  71. package/types/dist/lib/handlers/lifecycle.d.mts.map +1 -1
  72. package/types/dist/lib/handlers/materialize-manager.d.mts +8 -70
  73. package/types/dist/lib/handlers/materialize-manager.d.mts.map +1 -1
  74. package/types/dist/lib/handlers/metadata.d.mts +17 -221
  75. package/types/dist/lib/handlers/metadata.d.mts.map +1 -1
  76. package/types/dist/lib/handlers/ownership.d.mts +44 -160
  77. package/types/dist/lib/handlers/ownership.d.mts.map +1 -1
  78. package/types/dist/lib/handlers/permission-manager.d.mts +37 -141
  79. package/types/dist/lib/handlers/permission-manager.d.mts.map +1 -1
  80. package/types/dist/lib/handlers/unified-wrapper.d.mts +26 -239
  81. package/types/dist/lib/handlers/unified-wrapper.d.mts.map +1 -1
  82. package/types/dist/lib/handlers/version-manager.d.mts +28 -225
  83. package/types/dist/lib/handlers/version-manager.d.mts.map +1 -1
  84. package/types/dist/lib/helpers/class-instance-wrapper.d.mts +2 -52
  85. package/types/dist/lib/helpers/class-instance-wrapper.d.mts.map +1 -1
  86. package/types/dist/lib/helpers/config.d.mts +125 -139
  87. package/types/dist/lib/helpers/config.d.mts.map +1 -1
  88. package/types/dist/lib/helpers/eventemitter-context.d.mts +3 -29
  89. package/types/dist/lib/helpers/eventemitter-context.d.mts.map +1 -1
  90. package/types/dist/lib/helpers/hint-detector.d.mts +2 -15
  91. package/types/dist/lib/helpers/hint-detector.d.mts.map +1 -1
  92. package/types/dist/lib/helpers/modes-utils.d.mts +3 -30
  93. package/types/dist/lib/helpers/modes-utils.d.mts.map +1 -1
  94. package/types/dist/lib/helpers/pattern-matcher.d.mts +3 -43
  95. package/types/dist/lib/helpers/pattern-matcher.d.mts.map +1 -1
  96. package/types/dist/lib/helpers/resolve-from-caller.d.mts +3 -27
  97. package/types/dist/lib/helpers/resolve-from-caller.d.mts.map +1 -1
  98. package/types/dist/lib/helpers/sanitize.d.mts +4 -92
  99. package/types/dist/lib/helpers/sanitize.d.mts.map +1 -1
  100. package/types/dist/lib/helpers/utilities.d.mts +4 -52
  101. package/types/dist/lib/helpers/utilities.d.mts.map +1 -1
  102. package/types/dist/lib/i18n/translations.d.mts +4 -37
  103. package/types/dist/lib/i18n/translations.d.mts.map +1 -1
  104. package/types/dist/lib/modes/eager.d.mts +8 -30
  105. package/types/dist/lib/modes/eager.d.mts.map +1 -1
  106. package/types/dist/lib/modes/lazy.d.mts +10 -43
  107. package/types/dist/lib/modes/lazy.d.mts.map +1 -1
  108. package/types/dist/lib/processors/flatten.d.mts +56 -107
  109. package/types/dist/lib/processors/flatten.d.mts.map +1 -1
  110. package/types/dist/lib/processors/loader.d.mts +6 -41
  111. package/types/dist/lib/processors/loader.d.mts.map +1 -1
  112. package/types/dist/lib/processors/type-generator.d.mts +2 -16
  113. package/types/dist/lib/processors/type-generator.d.mts.map +1 -1
  114. package/types/dist/lib/processors/typescript.d.mts +6 -53
  115. package/types/dist/lib/processors/typescript.d.mts.map +1 -1
  116. package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts +3 -71
  117. package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts.map +1 -1
  118. package/types/dist/lib/runtime/runtime-livebindings.d.mts +2 -37
  119. package/types/dist/lib/runtime/runtime-livebindings.d.mts.map +1 -1
  120. package/types/dist/lib/runtime/runtime.d.mts +3 -39
  121. package/types/dist/lib/runtime/runtime.d.mts.map +1 -1
  122. package/types/dist/slothlet.d.mts +3 -249
  123. package/types/dist/slothlet.d.mts.map +1 -1
  124. package/types/index.d.mts +36 -16
  125. package/types/index.d.mts.map +1 -0
  126. package/AGENT-USAGE.md +0 -736
  127. package/docs/API-RULES.md +0 -712
@@ -14,3113 +14,4 @@
14
14
  limitations under the License.
15
15
  */
16
16
 
17
-
18
-
19
-
20
-
21
-
22
- const ____COLLISION_MERGED_PROPERTY = Symbol("collisionMergedProperty");
23
- import util from "node:util";
24
- import { ComponentBase } from "@cldmv/slothlet/factories/component-base";
25
-
26
-
27
- const ERROR_HOOK_PROCESSED = Symbol.for("@cldmv/slothlet/hook-error-processed");
28
-
29
-
30
- const hasOwn = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
31
-
32
-
33
- function unwrapError(error) {
34
-
35
- if (error && error.name === "SlothletError" && error.originalError) {
36
- return error.originalError;
37
- }
38
- return error;
39
- }
40
-
41
- const wrapperDebugEnabled =
42
- process.env.SLOTHLET_DEBUG_WRAPPER === "1" ||
43
- process.env.SLOTHLET_DEBUG_WRAPPER === "true" ||
44
- process.env.SLOTHLET_DEBUG_SCRIPT_VERBOSE === "1" ||
45
- process.env.SLOTHLET_DEBUG_SCRIPT_VERBOSE === "true";
46
-
47
-
48
- export const TYPE_STATES = {
49
- UNMATERIALIZED: Symbol("unmaterialized"),
50
- IN_FLIGHT: Symbol("inFlight")
51
- };
52
-
53
-
54
- function getSafeFunctionName(apiPath, fallback) {
55
- const parts = String(apiPath || "")
56
- .split(".")
57
- .filter((part) => part && part !== "null");
58
- const baseName = parts.length > 0 ? parts[parts.length - 1] : "";
59
- let safeName = String(baseName || "").replace(/[^A-Za-z0-9_$]/g, "_");
60
- if (!safeName || !/^[A-Za-z_$]/.test(safeName[0])) {
61
- safeName = safeName ? `_${safeName}` : "";
62
- }
63
- return safeName || fallback;
64
- }
65
-
66
-
67
- function createNamedProxyTarget(nameHint, fallback) {
68
- const safeName = getSafeFunctionName(nameHint, fallback);
69
- return { [safeName]: function () {} }[safeName];
70
- }
71
-
72
-
73
- const _proxyRegistry = new WeakMap();
74
-
75
-
76
- export class UnifiedWrapper extends ComponentBase {
77
-
78
- #internal = null;
79
-
80
-
81
- get ____slothletInternal() {
82
- if (!(#internal in this)) return undefined;
83
- return this.#internal;
84
- }
85
-
86
-
87
- constructor(
88
- slothlet,
89
- {
90
- mode,
91
- apiPath,
92
- initialImpl = null,
93
- materializeFunc = null,
94
- isCallable,
95
- materializeOnCreate = false,
96
- filePath = null,
97
- moduleID = null,
98
- sourceFolder = null
99
- }
100
- ) {
101
- super(slothlet);
102
-
103
-
104
-
105
-
106
- const isCallableExplicit = typeof isCallable === "boolean";
107
- const isCallableValue = isCallableExplicit
108
- ? isCallable
109
- : typeof initialImpl === "function" || (initialImpl && typeof initialImpl.default === "function");
110
-
111
-
112
-
113
- const isCallableLocked = isCallableValue || isCallableExplicit;
114
-
115
- const internal = Object.create(null);
116
- internal.id = Math.random().toString(36).substr(2, 9);
117
- internal.mode = mode;
118
- internal.apiPath = apiPath;
119
- internal.materializeOnCreate = materializeOnCreate;
120
- internal.isCallable = isCallableValue;
121
- internal.isCallableLocked = isCallableLocked;
122
- internal.moduleID = moduleID;
123
- internal.filePath = filePath;
124
- internal.sourceFolder = sourceFolder;
125
- internal.invalid = false;
126
- internal.state = {
127
- materialized: initialImpl !== null,
128
- inFlight: false,
129
- collisionMode: "merge"
130
- };
131
- internal.displayName = apiPath ? `${String(apiPath).replace(/\./g, "__")}__UnifiedWrapper` : "UnifiedWrapper";
132
-
133
-
134
-
135
-
136
-
137
-
138
- this.#internal = internal;
139
-
140
-
141
- const wrapper = this;
142
- Object.defineProperty(internal, "wrapper", {
143
-
144
-
145
- get() {
146
- return wrapper;
147
- },
148
-
149
- enumerable: false,
150
- configurable: false
151
- });
152
-
153
-
154
-
155
- internal.callableImpl = null;
156
-
157
- internal.waitingProxyCache = new Map();
158
- internal.proxy = null;
159
-
160
-
161
-
162
- internal.impl = UnifiedWrapper._cloneImpl(initialImpl);
163
-
164
- internal.materializeFunc = materializeFunc;
165
-
166
-
167
- if (filePath && slothlet.handlers?.lifecycle) {
168
- slothlet.handlers.lifecycle.emit("impl:created", {
169
- apiPath,
170
- impl: this,
171
-
172
-
173
-
174
-
175
- wrapper: Object.freeze({ __impl: this.____slothletInternal.impl }),
176
- source: "initial",
177
- moduleID,
178
- filePath,
179
-
180
-
181
- sourceFolder: sourceFolder || slothlet.config?.dir
182
- });
183
- }
184
-
185
-
186
- if (initialImpl !== null && filePath && slothlet.handlers?.lifecycle) {
187
- slothlet.handlers.lifecycle.emit("impl:created", {
188
- apiPath,
189
- impl: initialImpl,
190
- wrapper: Object.freeze({ __impl: this.____slothletInternal.impl }),
191
- source: "initial",
192
- moduleID,
193
- filePath,
194
-
195
-
196
- sourceFolder: sourceFolder || slothlet.config?.dir
197
- });
198
- }
199
-
200
- if (initialImpl !== null) {
201
- const implKeys = Object.keys(initialImpl || {});
202
- if ((wrapperDebugEnabled || this.____config?.debug?.wrapper) && apiPath && (apiPath === "config" || apiPath.startsWith("config."))) {
203
- this.slothlet.debug("wrapper", {
204
- key: "DEBUG_MODE_WRAPPER_CONSTRUCTOR_IMPL_KEYS",
205
- apiPath,
206
- keyCount: implKeys.length,
207
- keySample: implKeys.slice(0, 5)
208
- });
209
- }
210
- this.___adoptImplChildren();
211
- if ((wrapperDebugEnabled || this.____config?.debug?.wrapper) && apiPath && (apiPath === "config" || apiPath.startsWith("config."))) {
212
- const childKeys = Object.keys(this).filter((k) => !k.startsWith("_") && !k.startsWith("__"));
213
- this.slothlet.debug("wrapper", {
214
- key: "DEBUG_MODE_WRAPPER_CONSTRUCTOR_AFTER_ADOPT",
215
- apiPath,
216
- childCount: childKeys.length,
217
- childKeySample: childKeys.slice(0, 5)
218
- });
219
- }
220
- }
221
-
222
-
223
- if (mode === "lazy") {
224
- slothlet._registerLazyWrapper();
225
-
226
-
227
- if (slothlet.config.tracking?.materialization) {
228
-
229
-
230
- setImmediate(() => {
231
- this._materialize().catch((err) => {
232
-
233
-
234
-
235
-
236
- if (slothlet.config?.debug?.materialize) {
237
- slothlet.debug("materialize", {
238
- key: "DEBUG_MODE_BACKGROUND_MATERIALIZE_ERROR",
239
- apiPath: this.____slothletInternal?.apiPath,
240
- error: err.message
241
- });
242
- }
243
- });
244
- });
245
- }
246
- }
247
- }
248
-
249
-
250
- [util.inspect.custom](____depth, ____options, ____inspect) {
251
-
252
-
253
-
254
-
255
- const w = _proxyRegistry.get(this) ?? this;
256
-
257
- const childKeys = Object.keys(w).filter((k) => !k.startsWith("_") && !k.startsWith("__"));
258
- if (childKeys.length > 0 && !w.____slothletInternal?.isCallable) {
259
- const inspectObj = {};
260
- for (const key of childKeys) {
261
-
262
- inspectObj[key] = w[key];
263
- }
264
- return inspectObj;
265
- }
266
-
267
-
268
-
269
- if (
270
- w.____slothletInternal?.mode === "lazy" &&
271
- w.____slothletInternal?.state &&
272
- !w.____slothletInternal?.state.materialized &&
273
- w.____slothletInternal?.proxy
274
- ) {
275
- return w.____slothletInternal.proxy;
276
- }
277
- return w.____slothletInternal?.impl;
278
- }
279
-
280
-
281
- get __impl() {
282
- return this.____slothletInternal.impl;
283
- }
284
-
285
-
286
- static _cloneImpl(value) {
287
- if (value && typeof value === "object" && !Array.isArray(value) && typeof value !== "function") {
288
- const isProxy = util.types.isProxy(value);
289
- if (isProxy) {
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298
- if (resolveWrapper(value)) {
299
- const clone = {};
300
- for (const key of Reflect.ownKeys(value)) {
301
- try {
302
- clone[key] = value[key];
303
- } catch {
304
-
305
- }
306
- }
307
- return clone;
308
- }
309
-
310
- return value;
311
- }
312
- const uw_cloneDescriptors = Object.getOwnPropertyDescriptors(value);
313
- return Object.create(Object.getPrototypeOf(value), uw_cloneDescriptors);
314
- }
315
- return value;
316
- }
317
-
318
-
319
- static _extractFullImpl(wrapper) {
320
- if (!wrapper) return null;
321
-
322
- const impl = wrapper.____slothletInternal.impl;
323
-
324
-
325
- if (impl === null || impl === undefined) return impl;
326
- if (typeof impl !== "object" && typeof impl !== "function") return impl;
327
-
328
-
329
- if (typeof impl === "function") return impl;
330
-
331
-
332
- const extractFullImpl_result = {};
333
-
334
-
335
- for (const key of Object.keys(impl)) {
336
-
337
-
338
- if (key.startsWith("__")) continue;
339
- extractFullImpl_result[key] = impl[key];
340
- }
341
-
342
-
343
-
344
-
345
-
346
-
347
- if (impl.__childFilePaths) {
348
- extractFullImpl_result.__childFilePaths = impl.__childFilePaths;
349
- }
350
-
351
-
352
-
353
-
354
- if (impl.__childFilePathsPreMaterialize) {
355
- extractFullImpl_result.__childFilePathsPreMaterialize = impl.__childFilePathsPreMaterialize;
356
- }
357
-
358
-
359
-
360
- for (const key of Object.keys(wrapper)) {
361
-
362
-
363
- if (UnifiedWrapper.INTERNAL_KEYS.has(key)) continue;
364
- if (key in extractFullImpl_result) continue;
365
-
366
- const extractFullImpl_child = wrapper[key];
367
- const _extractChildW = resolveWrapper(extractFullImpl_child);
368
- if (_extractChildW) {
369
-
370
- extractFullImpl_result[key] = UnifiedWrapper._extractFullImpl(_extractChildW);
371
- } else {
372
- extractFullImpl_result[key] = extractFullImpl_child;
373
- }
374
- }
375
-
376
- return extractFullImpl_result;
377
- }
378
-
379
-
380
- _applyNewImpl(newImpl, forceReuseChildren = false) {
381
-
382
-
383
- this.____slothletInternal.impl = UnifiedWrapper._cloneImpl(newImpl);
384
- this.____slothletInternal.invalid = false;
385
-
386
-
387
-
388
-
389
- if (
390
- !this.____slothletInternal.isCallableLocked &&
391
- !this.____slothletInternal.isCallable &&
392
- (typeof newImpl === "function" || (newImpl && typeof newImpl.default === "function"))
393
- ) {
394
- this.____slothletInternal.isCallable = true;
395
- this.____slothletInternal.isCallableLocked = true;
396
- }
397
-
398
-
399
-
400
-
401
-
402
-
403
- if (!this.____slothletInternal.filePath && this.____slothletInternal.impl && this.____slothletInternal.impl.__filePath) {
404
- this.____slothletInternal.filePath = this.____slothletInternal.impl.__filePath;
405
- this.slothlet.debug("wrapper", {
406
- key: "DEBUG_MODE_APPLY_IMPL_UPDATE_PATH",
407
- apiPath: this.____slothletInternal.apiPath,
408
- filePath: this.____slothletInternal.filePath
409
- });
410
- }
411
-
412
- this.___adoptImplChildren(forceReuseChildren);
413
- }
414
-
415
-
416
- ___setImpl(newImpl, moduleID = null, forceReuseChildren = false) {
417
-
418
-
419
- if ((wrapperDebugEnabled || this.____config?.debug?.wrapper) && this.____slothletInternal.apiPath === "string") {
420
- this.slothlet.debug("wrapper", {
421
- key: "DEBUG_MODE_SETIMPL_CALLED",
422
- apiPath: this.____slothletInternal.apiPath,
423
-
424
-
425
- newImplKeys: Object.keys(newImpl || {})
426
- });
427
- }
428
-
429
-
430
- this._applyNewImpl(newImpl, forceReuseChildren);
431
-
432
-
433
- if (newImpl && this.slothlet.handlers?.lifecycle) {
434
- const wrapperMetadata = this.slothlet.handlers.metadata.getMetadata(this);
435
-
436
- let extractedModuleId = moduleID || (wrapperMetadata?.moduleID ? wrapperMetadata.moduleID.split(":")[0] : null);
437
-
438
-
439
-
440
- if (extractedModuleId && typeof extractedModuleId !== "string") {
441
- extractedModuleId = extractedModuleId.moduleID || extractedModuleId.__moduleID || String(extractedModuleId);
442
- }
443
-
444
- this.slothlet.handlers.lifecycle.emit("impl:changed", {
445
- apiPath: this.____slothletInternal.apiPath,
446
- impl: newImpl,
447
- wrapper: Object.freeze({ __impl: this.____slothletInternal.impl }),
448
- source: "hot-reload",
449
- moduleID: extractedModuleId,
450
- filePath: wrapperMetadata?.filePath,
451
- sourceFolder: wrapperMetadata?.sourceFolder
452
- });
453
- }
454
-
455
- this.____slothletInternal.state.materialized = true;
456
- this.____slothletInternal.state.inFlight = false;
457
-
458
-
459
-
460
-
461
- if (this.slothlet._onWrapperMaterialized) {
462
- this.slothlet._onWrapperMaterialized();
463
- }
464
- }
465
-
466
-
467
- ___resetLazy(newMaterializeFunc) {
468
- this.slothlet.debug("wrapper", {
469
- key: "DEBUG_MODE_RESETLAZY_CALLED",
470
- apiPath: this.____slothletInternal.apiPath,
471
- hadImpl: this.____slothletInternal.impl !== null,
472
- hadChildren: Object.keys(this).filter((k) => !k.startsWith("_") && !k.startsWith("__")).length
473
- });
474
-
475
-
476
- for (const key of Reflect.ownKeys(this)) {
477
-
478
-
479
- if (typeof key === "string" && (key.startsWith("_") || key.startsWith("__"))) {
480
- continue;
481
- }
482
- const child = this[key];
483
- const childRaw = resolveWrapper(child);
484
- if (childRaw) {
485
- childRaw.___invalidate();
486
- }
487
- const descriptor = Object.getOwnPropertyDescriptor(this, key);
488
-
489
-
490
- if (descriptor?.configurable) {
491
- delete this[key];
492
- }
493
- }
494
-
495
-
496
- this.____slothletInternal.impl = null;
497
- this.____slothletInternal.invalid = false;
498
- this.____slothletInternal.state.materialized = false;
499
- this.____slothletInternal.state.inFlight = false;
500
-
501
-
502
- this.____slothletInternal.materializationPromise = null;
503
-
504
-
505
- this.____slothletInternal.materializeFunc = newMaterializeFunc;
506
-
507
-
508
-
509
-
510
- if (this.____slothletInternal.waitingProxyCache) {
511
- this.____slothletInternal.waitingProxyCache.clear();
512
- }
513
-
514
- this.slothlet.debug("wrapper", {
515
- key: "DEBUG_MODE_RESETLAZY_COMPLETE",
516
- apiPath: this.____slothletInternal.apiPath
517
- });
518
- }
519
-
520
-
521
- async ___materialize() {
522
-
523
- if (this.____slothletInternal.state.materialized) {
524
- return;
525
- }
526
-
527
-
528
-
529
- if (this.____slothletInternal.invalid) {
530
- return;
531
- }
532
-
533
-
534
-
535
- if (this.____slothletInternal.materializationPromise) {
536
- this.slothlet.debug("wrapper", {
537
- key: "DEBUG_MODE_MATERIALIZE_AWAIT",
538
- apiPath: this.____slothletInternal.apiPath
539
- });
540
- return this.____slothletInternal.materializationPromise;
541
- }
542
-
543
-
544
-
545
- if ((wrapperDebugEnabled || this.____config?.debug?.wrapper) && this.apiPath === "string") {
546
- this.slothlet.debug("wrapper", {
547
- key: "DEBUG_MODE_MATERIALIZE_START",
548
- apiPath: this.apiPath
549
- });
550
- }
551
-
552
-
553
- this.____slothletInternal.materializationPromise = (async () => {
554
- this.____slothletInternal.state.inFlight = true;
555
-
556
- try {
557
- if (this.____slothletInternal.materializeFunc) {
558
- if ((wrapperDebugEnabled || this.____config?.debug?.wrapper) && this.____slothletInternal.apiPath === "string") {
559
- this.slothlet.debug("wrapper", {
560
- key: "DEBUG_MODE_MATERIALIZE_CALLING_FUNC",
561
- apiPath: this.____slothletInternal.apiPath
562
- });
563
- }
564
-
565
-
566
- const lazy_setImpl = (value) => {
567
- this._applyNewImpl(value);
568
- };
569
- const result = await this.____slothletInternal.materializeFunc(lazy_setImpl);
570
-
571
-
572
- if (!this.____slothletInternal.impl) {
573
- this._applyNewImpl(result);
574
- }
575
-
576
- this.____slothletInternal.state.materialized = true;
577
-
578
-
579
-
580
-
581
- if (this.slothlet._onWrapperMaterialized) {
582
- this.slothlet._onWrapperMaterialized();
583
- }
584
-
585
- if ((wrapperDebugEnabled || this.____config?.debug?.wrapper) && this.____slothletInternal.apiPath === "string") {
586
- this.slothlet.debug("wrapper", {
587
- key: "DEBUG_MODE_MATERIALIZE_COMPLETE",
588
- apiPath: this.____slothletInternal.apiPath,
589
- resultType: typeof result,
590
-
591
-
592
- resultKeys: Object.keys(result || {})
593
- });
594
- }
595
- }
596
- } catch (error) {
597
-
598
-
599
- if ((wrapperDebugEnabled || this.____config?.debug?.wrapper) && this.____slothletInternal.apiPath === "string") {
600
- this.slothlet.debug("wrapper", {
601
- key: "DEBUG_MODE_MATERIALIZE_ERROR",
602
- apiPath: this.____slothletInternal.apiPath,
603
- error: error.message
604
- });
605
- }
606
- throw error;
607
- } finally {
608
-
609
- this.____slothletInternal.state.inFlight = false;
610
- this.____slothletInternal.materializationPromise = null;
611
- }
612
- })();
613
-
614
-
615
- return this.____slothletInternal.materializationPromise;
616
- }
617
-
618
-
619
- _materialize() {
620
- return this.___materialize();
621
- }
622
-
623
-
624
- ___invalidate() {
625
- this.____slothletInternal.invalid = true;
626
- this.____slothletInternal.impl = null;
627
-
628
- for (const key of Reflect.ownKeys(this)) {
629
-
630
-
631
- if (typeof key === "string" && (key.startsWith("_") || key.startsWith("__"))) {
632
- continue;
633
- }
634
- const descriptor = Object.getOwnPropertyDescriptor(this, key);
635
-
636
-
637
- if (descriptor?.configurable) {
638
- delete this[key];
639
- }
640
- }
641
- }
642
-
643
-
644
- ___adoptImplChildren(forceReuseChildren = false) {
645
- const preExistingKeys = Object.keys(this).filter((k) => !k.startsWith("_") && !k.startsWith("__"));
646
- this.slothlet.debug("wrapper", {
647
- key: "DEBUG_MODE_ADOPT_START",
648
- apiPath: this.____slothletInternal.apiPath,
649
-
650
-
651
- wrapperId: this.____slothletInternal.id || "no-id",
652
- preExistingKeys: preExistingKeys.join(","),
653
- collisionMode: this.____slothletInternal.state.collisionMode
654
- });
655
-
656
- if (
657
- !this.____slothletInternal.impl ||
658
- (typeof this.____slothletInternal.impl !== "object" && typeof this.____slothletInternal.impl !== "function")
659
- ) {
660
- return;
661
- }
662
-
663
-
664
-
665
-
666
-
667
-
668
-
669
- if (util.types.isProxy(this.____slothletInternal.impl)) return;
670
-
671
- const ownKeys = Reflect.ownKeys(this.____slothletInternal.impl);
672
-
673
- const internalKeys = new Set([
674
- "__impl",
675
- "___setImpl",
676
- "___resetLazy",
677
- "_materialize",
678
- "_impl",
679
- "_state",
680
- "_invalid",
681
- "____slothlet",
682
- "____slothletInternal"
683
- ]);
684
- const keepImplProperties =
685
- typeof this.____slothletInternal.impl === "function" ||
686
- (this.____slothletInternal.impl &&
687
- typeof this.____slothletInternal.impl === "object" &&
688
- typeof this.____slothletInternal.impl.default === "function");
689
- if (
690
- keepImplProperties &&
691
- this.____slothletInternal.impl &&
692
- typeof this.____slothletInternal.impl === "object" &&
693
- typeof this.____slothletInternal.impl.default === "function"
694
- ) {
695
- internalKeys.add("default");
696
- }
697
- const observedKeys = new Set();
698
-
699
-
700
-
701
-
702
- const existingKeys = Object.keys(this).filter((k) => !k.startsWith("_") && !k.startsWith("__"));
703
- const storedCollisionMode = this.____slothletInternal.state.collisionMode;
704
- const isMergeScenario = storedCollisionMode !== "replace" && existingKeys.length > 0;
705
-
706
- this.slothlet.debug("wrapper", {
707
- key: "DEBUG_MODE_ADOPT",
708
- apiPath: this.____slothletInternal.apiPath,
709
- mode: this.____slothletInternal.mode,
710
- storedCollisionMode,
711
- existingKeys: existingKeys.join(","),
712
- isMergeScenario
713
- });
714
-
715
-
716
-
717
-
718
- const savedChildren = new Map();
719
- if (storedCollisionMode === "replace" && existingKeys.length > 0) {
720
- this.slothlet.debug("wrapper", {
721
- key: "DEBUG_MODE_ADOPT_REPLACE_CLEARING",
722
- count: existingKeys.length
723
- });
724
-
725
- for (const key of existingKeys) {
726
- const child = this[key];
727
- if (resolveWrapper(child) !== null) {
728
- savedChildren.set(key, child);
729
- }
730
- const descriptor = Object.getOwnPropertyDescriptor(this, key);
731
- if (descriptor?.configurable) {
732
- delete this[key];
733
- }
734
- }
735
-
736
- } else {
737
-
738
- for (const key of existingKeys) {
739
- observedKeys.add(key);
740
- }
741
- }
742
-
743
-
744
- const metadataKeys = new Set(["__childFilePaths", "__filePath", "__childFilePathsPreMaterialize"]);
745
- const skipKeys = typeof this.____slothletInternal.impl === "function" ? new Set(["length", "name", "prototype"]) : null;
746
-
747
- const builtinKeys = new Set(["slothlet", "shutdown", "destroy"]);
748
-
749
- for (const key of ownKeys) {
750
- if (internalKeys.has(key)) {
751
- continue;
752
- }
753
-
754
- if (typeof key === "string" && metadataKeys.has(key)) {
755
- continue;
756
- }
757
-
758
- if (typeof key === "string" && builtinKeys.has(key)) {
759
- continue;
760
- }
761
- if (skipKeys && typeof key === "string" && skipKeys.has(key)) {
762
- continue;
763
- }
764
-
765
-
766
-
767
- if (
768
- !this.____slothletInternal.impl ||
769
- (typeof this.____slothletInternal.impl !== "object" && typeof this.____slothletInternal.impl !== "function")
770
- ) {
771
- break;
772
- }
773
-
774
- const descriptor = Object.getOwnPropertyDescriptor(this.____slothletInternal.impl, key);
775
-
776
-
777
- if (!descriptor) {
778
- continue;
779
- }
780
-
781
- const value = this.____slothletInternal.impl[key];
782
-
783
-
784
- if (value === this.____slothletInternal.impl) {
785
- continue;
786
- }
787
-
788
-
789
-
790
-
791
- if (typeof key !== "symbol") {
792
- this.slothlet.debug("wrapper", {
793
- key: "DEBUG_MODE_ADOPT_PROCESS",
794
- apiPath: this.____slothletInternal.apiPath,
795
- propKey: key,
796
- typeOf: typeof value,
797
- valueName: value?.name
798
- });
799
- }
800
- observedKeys.add(key);
801
-
802
-
803
-
804
- if (hasOwn(this, key) && !key.toString().startsWith("_")) {
805
-
806
-
807
-
808
-
809
- if (typeof key !== "symbol") {
810
- this.slothlet.debug("wrapper", {
811
- key: "DEBUG_MODE_ADOPT_CHECK",
812
- apiPath: this.____slothletInternal.apiPath,
813
- propKey: key,
814
- has__collisionMergedKeys: !!this.____slothletInternal.collisionMergedKeys,
815
- inSet: this.____slothletInternal.collisionMergedKeys?.has(key)
816
- });
817
- }
818
- const isCollisionMerged = this.____slothletInternal.collisionMergedKeys && this.____slothletInternal.collisionMergedKeys.has(key);
819
-
820
- if (isCollisionMerged && !forceReuseChildren) {
821
-
822
-
823
-
824
-
825
-
826
-
827
- if (typeof key !== "symbol") {
828
- this.slothlet.debug("wrapper", {
829
- key: "DEBUG_MODE_ADOPT_SKIP_COLLISION_MERGED",
830
- apiPath: this.____slothletInternal.apiPath,
831
- propKey: key
832
- });
833
- }
834
-
835
-
836
- if (descriptor.configurable) {
837
- delete this.____slothletInternal.impl[key];
838
- }
839
- continue;
840
- }
841
-
842
-
843
-
844
-
845
-
846
- if (typeof key !== "symbol") {
847
- this.slothlet.debug("wrapper", {
848
- key: "DEBUG_MODE_ADOPT_ALLOW_NOT_COLLISION_MERGED",
849
- apiPath: this.____slothletInternal.apiPath,
850
- propKey: key
851
- });
852
- }
853
- }
854
-
855
-
856
-
857
-
858
-
859
-
860
-
861
-
862
- const existingChild = savedChildren.get(key) || this[key];
863
- let wrapped;
864
-
865
-
866
-
867
-
868
-
869
- const skipChildReuse = !forceReuseChildren && this.____slothletInternal.mode === "lazy" && storedCollisionMode === "replace";
870
-
871
- if (!skipChildReuse && existingChild && resolveWrapper(existingChild) !== null) {
872
-
873
-
874
-
875
- if (resolveWrapper(value) !== null) {
876
-
877
- const newWrapper = resolveWrapper(value);
878
-
879
-
880
- let rawImpl = newWrapper ? newWrapper.____slothletInternal.impl : null;
881
-
882
-
883
-
884
-
885
-
886
- if (
887
- rawImpl &&
888
- typeof rawImpl === "object" &&
889
- !Array.isArray(rawImpl) &&
890
- typeof rawImpl !== "function" &&
891
- Object.keys(rawImpl).filter((k) => !k.startsWith("__")).length === 0
892
- ) {
893
- const wrapperOwnKeys = Object.keys(newWrapper).filter((k) => !k.startsWith("_") && !k.startsWith("__"));
894
-
895
-
896
- if (wrapperOwnKeys.length > 0) {
897
- rawImpl = UnifiedWrapper._extractFullImpl(newWrapper);
898
- }
899
- }
900
-
901
-
902
-
903
- if (rawImpl !== null && rawImpl !== undefined) {
904
- resolveWrapper(existingChild).___setImpl(
905
- rawImpl,
906
- this.slothlet,
907
- this.____slothletInternal.moduleID,
908
- this.____slothletInternal.filePath
909
- );
910
- } else if (newWrapper && newWrapper.____slothletInternal.materializeFunc) {
911
-
912
-
913
-
914
- const existingChildWrapper = resolveWrapper(existingChild);
915
-
916
-
917
- if (existingChildWrapper) {
918
- existingChildWrapper.___resetLazy(newWrapper.____slothletInternal.materializeFunc);
919
- }
920
- }
921
-
922
- wrapped = existingChild;
923
- } else {
924
- resolveWrapper(existingChild).___setImpl(
925
- value,
926
- this.slothlet,
927
- this.____slothletInternal.moduleID,
928
- this.____slothletInternal.filePath
929
- );
930
- wrapped = existingChild;
931
- }
932
-
933
-
934
- if (typeof key !== "symbol") {
935
- this.slothlet.debug("wrapper", {
936
- key: "DEBUG_MODE_ADOPT_REUSE_CHILD_WRAPPER",
937
- apiPath: this.____slothletInternal.apiPath,
938
- propKey: key
939
- });
940
- }
941
- } else {
942
-
943
- wrapped = this.___createChildWrapper(key, value);
944
-
945
-
946
- if (typeof key !== "symbol") {
947
- this.slothlet.debug("wrapper", {
948
- key: "DEBUG_MODE_ADOPT_WRAP",
949
- apiPath: this.____slothletInternal.apiPath,
950
- propKey: key,
951
- wrapped: wrapped ? "YES" : wrapped === null ? "NULL" : "NO"
952
- });
953
- }
954
- }
955
-
956
- if (wrapped) {
957
-
958
-
959
- const existing = this[key];
960
- const existingDescriptor = Object.getOwnPropertyDescriptor(this, key);
961
-
962
-
963
-
964
-
965
- if (existing === wrapped || (existingDescriptor && !existingDescriptor.configurable)) {
966
-
967
-
968
- if (typeof key !== "symbol") {
969
- this.slothlet.debug("wrapper", {
970
- key:
971
- existingDescriptor && !existingDescriptor.configurable
972
- ? "DEBUG_MODE_ADOPT_SKIP_NON_CONFIGURABLE"
973
- : "DEBUG_MODE_ADOPT_SKIP_SAME_WRAPPER",
974
- apiPath: this.____slothletInternal.apiPath,
975
- propKey: key
976
- });
977
- }
978
- } else {
979
-
980
-
981
- if (typeof key !== "symbol") {
982
- this.slothlet.debug("wrapper", {
983
- key: "DEBUG_MODE_ADOPT_DEFINE",
984
- apiPath: this.____slothletInternal.apiPath,
985
- propKey: key
986
- });
987
- }
988
- Object.defineProperty(this, key, {
989
- value: wrapped,
990
- writable: false,
991
- enumerable: true,
992
- configurable: true
993
- });
994
-
995
-
996
- if (typeof key !== "symbol") {
997
- this.slothlet.debug("wrapper", {
998
- key: "DEBUG_MODE_ADOPT_DEFINED",
999
- apiPath: this.____slothletInternal.apiPath,
1000
- propKey: key
1001
- });
1002
- }
1003
- }
1004
-
1005
-
1006
- if (descriptor.configurable && !keepImplProperties && this.____slothletInternal.impl) {
1007
- delete this.____slothletInternal.impl[key];
1008
- }
1009
- } else if (wrapped === null) {
1010
-
1011
- Object.defineProperty(this, key, {
1012
- value: value,
1013
- writable: false,
1014
- enumerable: true,
1015
- configurable: true
1016
- });
1017
-
1018
-
1019
- if (descriptor.configurable && !keepImplProperties && this.____slothletInternal.impl) {
1020
- delete this.____slothletInternal.impl[key];
1021
- }
1022
- } else {
1023
-
1024
- Object.defineProperty(this, key, {
1025
- value: value,
1026
- writable: false,
1027
- enumerable: true,
1028
- configurable: true
1029
- });
1030
-
1031
-
1032
- if (descriptor.configurable && !keepImplProperties && this.____slothletInternal.impl) {
1033
- delete this.____slothletInternal.impl[key];
1034
- }
1035
- }
1036
- }
1037
-
1038
-
1039
-
1040
- if (!isMergeScenario) {
1041
- const currentKeys = Object.keys(this).filter((k) => !k.startsWith("_") && !k.startsWith("__"));
1042
- for (const key of currentKeys) {
1043
- if (!observedKeys.has(key)) {
1044
- const existing = this[key];
1045
- const existingRaw = resolveWrapper(existing);
1046
-
1047
-
1048
- if (existingRaw) {
1049
- existingRaw.___invalidate();
1050
- }
1051
- const descriptor = Object.getOwnPropertyDescriptor(this, key);
1052
-
1053
-
1054
-
1055
-
1056
-
1057
-
1058
- if (descriptor?.configurable) {
1059
- delete this[key];
1060
- }
1061
- }
1062
- }
1063
- }
1064
-
1065
-
1066
- if (this._mergeAfterMaterialize) {
1067
- const { existingWrapper, isMergeReplace: ____isMergeReplace } = this._mergeAfterMaterialize;
1068
-
1069
-
1070
-
1071
- const existingKeys = Object.keys(existingWrapper).filter((k) => !k.startsWith("_") && !k.startsWith("__"));
1072
- for (const key of existingKeys) {
1073
- if (!(key in this) || key.startsWith("_") || key.startsWith("__")) {
1074
- const child = existingWrapper[key];
1075
- Object.defineProperty(this, key, {
1076
- value: child,
1077
- writable: false,
1078
- enumerable: true,
1079
- configurable: true
1080
- });
1081
- }
1082
- }
1083
-
1084
-
1085
- delete this._mergeAfterMaterialize;
1086
- }
1087
- }
1088
-
1089
-
1090
- ___createChildWrapper(key, value) {
1091
- if (value === undefined) {
1092
- return undefined;
1093
- }
1094
-
1095
- if (value && resolveWrapper(value) !== null) {
1096
- return value;
1097
- }
1098
-
1099
-
1100
-
1101
-
1102
- if (
1103
- value instanceof Map ||
1104
- value instanceof Set ||
1105
- value instanceof WeakMap ||
1106
- value instanceof WeakSet ||
1107
- value instanceof Date ||
1108
- value instanceof RegExp ||
1109
- value instanceof Promise ||
1110
- value instanceof Error ||
1111
- ArrayBuffer.isView(value) ||
1112
- value instanceof ArrayBuffer
1113
- ) {
1114
- return null;
1115
- }
1116
-
1117
- let childImpl = value;
1118
- if (this.____slothletInternal.mode === "eager" && childImpl && typeof childImpl === "object") {
1119
- if (Array.isArray(childImpl)) {
1120
- childImpl = childImpl.slice();
1121
- } else {
1122
- const descriptors = Object.getOwnPropertyDescriptors(childImpl);
1123
- childImpl = Object.create(Object.getPrototypeOf(childImpl), descriptors);
1124
- }
1125
- }
1126
-
1127
-
1128
- const parentMetadata = this.slothlet.handlers?.metadata?.getMetadata(this);
1129
-
1130
-
1131
- const childExistingMetadata = this.slothlet.handlers?.metadata?.getMetadata(value);
1132
- let childFilePath = childExistingMetadata?.filePath || null;
1133
- let childModuleId = null;
1134
-
1135
- if (!childFilePath) {
1136
-
1137
-
1138
-
1139
- const keyStr = typeof key === "symbol" ? String(key) : key;
1140
- this.slothlet.debug("wrapper", {
1141
- key: "DEBUG_MODE_WRAP_CHILD_PATH_CHECK",
1142
- apiPath: this.apiPath,
1143
- propKey: keyStr,
1144
- has_impl: !!this.____slothletInternal.impl,
1145
- has__childFilePaths: !!(this.____slothletInternal.impl && this.____slothletInternal.impl.__childFilePaths),
1146
- has__childFilePathsPreMaterialize: !!this.____slothletInternal.childFilePathsPreMaterialize,
1147
- parentFilePath: parentMetadata?.filePath
1148
- });
1149
- if (this.____slothletInternal.impl && this.____slothletInternal.impl.__childFilePaths) {
1150
- this.slothlet.debug("wrapper", {
1151
- key: "DEBUG_MODE_WRAP_CHILD_PATH_AVAILABLE",
1152
- keys: Object.keys(this.____slothletInternal.impl.__childFilePaths).join(","),
1153
- propKey: keyStr,
1154
- found: !!this.____slothletInternal.impl.__childFilePaths[key]
1155
- });
1156
- }
1157
-
1158
-
1159
- if (
1160
- this.____slothletInternal.impl &&
1161
- this.____slothletInternal.impl.__childFilePaths &&
1162
- this.____slothletInternal.impl.__childFilePaths[key]
1163
- ) {
1164
- childFilePath = this.____slothletInternal.impl.__childFilePaths[key];
1165
- this.slothlet.debug("wrapper", {
1166
- key: "DEBUG_MODE_WRAP_CHILD_PATH_USING",
1167
- childFilePath
1168
- });
1169
-
1170
-
1171
- } else if (this.____slothletInternal.childFilePathsPreMaterialize && this.____slothletInternal.childFilePathsPreMaterialize[key]) {
1172
-
1173
- childFilePath = this.____slothletInternal.childFilePathsPreMaterialize[key];
1174
- this.slothlet.debug("wrapper", {
1175
- key: "DEBUG_MODE_WRAP_CHILD_PATH_PRE_MAT",
1176
- childFilePath
1177
- });
1178
-
1179
- } else {
1180
-
1181
- childFilePath = parentMetadata?.filePath || null;
1182
- this.slothlet.debug("wrapper", {
1183
- key: "DEBUG_MODE_WRAP_CHILD_PATH_FALLBACK",
1184
- childFilePath
1185
- });
1186
- }
1187
-
1188
-
1189
- if (parentMetadata?.moduleID) {
1190
- const colonIndex = parentMetadata.moduleID.indexOf(":");
1191
-
1192
-
1193
- childModuleId = colonIndex > 0 ? parentMetadata.moduleID.substring(0, colonIndex) : parentMetadata.moduleID;
1194
- }
1195
- } else {
1196
-
1197
-
1198
-
1199
- if (childExistingMetadata?.moduleID) {
1200
- const colonIndex = childExistingMetadata.moduleID.indexOf(":");
1201
-
1202
-
1203
- childModuleId = colonIndex > 0 ? childExistingMetadata.moduleID.substring(0, colonIndex) : childExistingMetadata.moduleID;
1204
- }
1205
- }
1206
-
1207
- const childSourceFolder = childExistingMetadata?.sourceFolder || parentMetadata?.sourceFolder || null;
1208
-
1209
- const nestedWrapper = new UnifiedWrapper(this.slothlet, {
1210
- mode: "eager",
1211
-
1212
-
1213
- apiPath: this.____slothletInternal.apiPath
1214
- ? `${this.____slothletInternal.apiPath}.${typeof key === "symbol" ? String(key) : key}`
1215
- : String(key),
1216
-
1217
- initialImpl: childImpl,
1218
- isCallable: typeof childImpl === "function",
1219
- filePath: childFilePath,
1220
- moduleID: childModuleId,
1221
- sourceFolder: childSourceFolder
1222
- });
1223
-
1224
-
1225
- return nestedWrapper.createProxy();
1226
- }
1227
-
1228
-
1229
- ___createWaitingProxy(propChain = []) {
1230
- const wrapper = this;
1231
-
1232
-
1233
- const cacheKey = propChain.join(".");
1234
-
1235
-
1236
-
1237
-
1238
-
1239
- if (!wrapper.____slothletInternal.waitingProxyCache) {
1240
- wrapper.____slothletInternal.waitingProxyCache = new Map();
1241
- }
1242
-
1243
-
1244
-
1245
- if (wrapper.____slothletInternal.waitingProxyCache.has(cacheKey)) {
1246
- return wrapper.____slothletInternal.waitingProxyCache.get(cacheKey);
1247
- }
1248
-
1249
-
1250
-
1251
- const waitingTarget = createNamedProxyTarget(`${wrapper.____slothletInternal.apiPath}_waitingProxy`, "waitingProxyTarget");
1252
-
1253
-
1254
-
1255
- const waitingProxy = new Proxy(waitingTarget, {
1256
- get(___target, prop) {
1257
- if (prop === "then") {
1258
-
1259
-
1260
-
1261
- return (onFulfilled, onRejected) => {
1262
- const waitingProxy_thenResolve = async () => {
1263
-
1264
- if (!wrapper.____slothletInternal.state.materialized) {
1265
- await wrapper._materialize();
1266
- }
1267
-
1268
-
1269
- let current = wrapper;
1270
- for (const chainProp of propChain) {
1271
-
1272
-
1273
- if (!current) return undefined;
1274
-
1275
-
1276
- if (
1277
- current.____slothletInternal.impl &&
1278
- typeof current.____slothletInternal.impl === "object" &&
1279
- util.types.isProxy(current.____slothletInternal.impl)
1280
- ) {
1281
- let result = current.____slothletInternal.impl;
1282
-
1283
- const idx = propChain.indexOf(chainProp);
1284
- for (let i = idx; i < propChain.length; i++) {
1285
- result = result[propChain[i]];
1286
- }
1287
- return result;
1288
- }
1289
-
1290
-
1291
- const isInternal = typeof chainProp === "string" && (chainProp.startsWith("_") || chainProp.startsWith("__"));
1292
- if (!isInternal && hasOwn(current, chainProp)) {
1293
- const child = current[chainProp];
1294
- const _childW = resolveWrapper(child);
1295
- if (_childW) {
1296
- current = _childW;
1297
-
1298
- if (current.____slothletInternal.mode === "lazy" && !current.____slothletInternal.state.materialized) {
1299
- await current._materialize();
1300
- }
1301
- continue;
1302
- }
1303
- return child;
1304
- }
1305
-
1306
-
1307
- if (current.____slothletInternal.impl && current.____slothletInternal.impl[chainProp] !== undefined) {
1308
- return current.____slothletInternal.impl[chainProp];
1309
- }
1310
-
1311
- return undefined;
1312
- }
1313
-
1314
-
1315
- if (
1316
- current.____slothletInternal.impl &&
1317
- typeof current.____slothletInternal.impl === "object" &&
1318
- util.types.isProxy(current.____slothletInternal.impl)
1319
- ) {
1320
- return current.____slothletInternal.impl;
1321
- }
1322
- return current.____slothletInternal.impl;
1323
- };
1324
-
1325
- waitingProxy_thenResolve().then(onFulfilled, onRejected);
1326
- };
1327
- }
1328
- if (prop === "____slothletInternal") return undefined;
1329
- if (prop === "_materialize") return wrapper._materialize.bind(wrapper);
1330
- if (prop === "__mode") return wrapper.____slothletInternal.mode;
1331
- if (prop === "__materialized") return wrapper.____slothletInternal.state.materialized;
1332
- if (prop === "__inFlight") return wrapper.____slothletInternal.state.inFlight;
1333
-
1334
-
1335
-
1336
-
1337
- if (
1338
- wrapper.____slothletInternal.mode === "lazy" &&
1339
- !wrapper.____slothletInternal.state.materialized &&
1340
- !wrapper.____slothletInternal.state.inFlight
1341
- ) {
1342
-
1343
-
1344
-
1345
-
1346
-
1347
- wrapper._materialize().catch(() => {});
1348
- }
1349
-
1350
- if (prop === util.inspect.custom) {
1351
-
1352
-
1353
- if (wrapper.____slothletInternal.state.inFlight) {
1354
- return waitingTarget;
1355
- }
1356
-
1357
-
1358
-
1359
- if (!wrapper.____slothletInternal.state.materialized) {
1360
- return waitingTarget;
1361
- }
1362
-
1363
-
1364
-
1365
-
1366
- if (wrapper.____slothletInternal.impl) {
1367
- let current = wrapper.____slothletInternal.impl;
1368
- for (const chainProp of propChain) {
1369
- if (!current || current === null) {
1370
- return undefined;
1371
- }
1372
- current = current[chainProp];
1373
- }
1374
-
1375
-
1376
-
1377
- return current;
1378
- }
1379
-
1380
-
1381
- return waitingTarget;
1382
- }
1383
- if (prop === "__type") {
1384
- wrapper.slothlet.debug("wrapper", {
1385
- key: "DEBUG_MODE_WAITING_TYPE",
1386
- apiPath: wrapper.apiPath,
1387
- propChain: propChain.join(","),
1388
- materialized: wrapper.____slothletInternal.state.materialized,
1389
- hasImpl: wrapper.____slothletInternal.impl !== null
1390
- });
1391
-
1392
-
1393
-
1394
-
1395
-
1396
- if (
1397
- wrapper.____slothletInternal.state.materialized ||
1398
- (wrapper.____slothletInternal.impl !== null && wrapper.____slothletInternal.impl !== undefined)
1399
- ) {
1400
-
1401
-
1402
- let current = wrapper.createProxy();
1403
- for (const chainProp of propChain) {
1404
- if (!current) break;
1405
- const currentWrapper = resolveWrapper(current);
1406
- if (current && currentWrapper) {
1407
-
1408
-
1409
- const isInternal = typeof chainProp === "string" && (chainProp.startsWith("_") || chainProp.startsWith("__"));
1410
- if (!isInternal && chainProp in currentWrapper) {
1411
- current = currentWrapper[chainProp];
1412
- wrapper.slothlet.debug("wrapper", {
1413
- key: "DEBUG_MODE_WAITING_TYPE_WALK_WRAPPER",
1414
- chainProp: String(chainProp),
1415
- typeOf: typeof current
1416
- });
1417
- continue;
1418
- }
1419
- if (
1420
- currentWrapper.____slothletInternal.impl &&
1421
- typeof currentWrapper.____slothletInternal.impl === "object" &&
1422
- currentWrapper.____slothletInternal.impl !== null &&
1423
- chainProp in currentWrapper.____slothletInternal.impl
1424
- ) {
1425
- current = currentWrapper.____slothletInternal.impl[chainProp];
1426
- wrapper.slothlet.debug("wrapper", {
1427
- key: "DEBUG_MODE_WAITING_TYPE_WALK_IMPL",
1428
- chainProp: String(chainProp),
1429
- typeOf: typeof current
1430
- });
1431
- continue;
1432
- }
1433
- }
1434
- wrapper.slothlet.debug("wrapper", {
1435
- key: "DEBUG_MODE_WAITING_TYPE_WALK_DIRECT",
1436
- chainProp: String(chainProp),
1437
- typeOf: typeof current
1438
- });
1439
- current = current[chainProp];
1440
- }
1441
-
1442
- const resolvedType =
1443
- typeof current === "function" ? "function" : typeof current === "object" && current !== null ? "object" : typeof current;
1444
- wrapper.slothlet.debug("wrapper", {
1445
- key: "DEBUG_MODE_WAITING_TYPE_RESOLVED",
1446
- apiPath: wrapper.apiPath,
1447
- propChain: propChain.join(","),
1448
- resolvedType
1449
- });
1450
- return resolvedType;
1451
- }
1452
-
1453
- wrapper.slothlet.debug("wrapper", {
1454
- key: "DEBUG_MODE_WAITING_TYPE_INFLIGHT",
1455
- apiPath: wrapper.apiPath,
1456
- propChain: propChain.join(",")
1457
- });
1458
- return TYPE_STATES.IN_FLIGHT;
1459
- }
1460
- if (prop === "__metadata") {
1461
-
1462
-
1463
-
1464
- if (wrapper.slothlet.handlers?.metadata) {
1465
- return wrapper.slothlet.handlers.metadata.getMetadata(wrapper);
1466
- }
1467
-
1468
-
1469
- return {};
1470
- }
1471
- if (typeof prop === "symbol") return undefined;
1472
- if (prop === "length") {
1473
-
1474
-
1475
- return 0;
1476
- }
1477
-
1478
-
1479
- if (prop === "name") return waitingTarget.name || "waitingProxyTarget";
1480
- if (prop === "toString") {
1481
-
1482
- return Function.prototype.toString.bind(waitingTarget);
1483
- }
1484
- if (prop === "valueOf") {
1485
-
1486
- return Function.prototype.valueOf.bind(waitingTarget);
1487
- }
1488
-
1489
-
1490
- if (prop === "toJSON") {
1491
-
1492
-
1493
- return () => undefined;
1494
- }
1495
-
1496
- if (prop === "__slothletPath") return wrapper.____slothletInternal.apiPath;
1497
-
1498
-
1499
-
1500
-
1501
-
1502
-
1503
- if (
1504
- wrapper.____slothletInternal.impl !== null &&
1505
- wrapper.____slothletInternal.impl !== undefined &&
1506
- typeof wrapper.____slothletInternal.impl === "object" &&
1507
- util.types.isProxy(wrapper.____slothletInternal.impl)
1508
- ) {
1509
-
1510
-
1511
- let result = wrapper.____slothletInternal.impl;
1512
- for (const chainProp of propChain) {
1513
- result = result[chainProp];
1514
- }
1515
-
1516
- return result[prop];
1517
- }
1518
-
1519
-
1520
-
1521
-
1522
-
1523
-
1524
- if (wrapper.____slothletInternal.impl !== null && wrapper.____slothletInternal.impl !== undefined) {
1525
-
1526
- let current = wrapper;
1527
- let remainingChain = [...propChain];
1528
-
1529
-
1530
- for (let i = 0; i < propChain.length; i++) {
1531
- const chainProp = propChain[i];
1532
-
1533
-
1534
- if (
1535
- current.____slothletInternal.impl &&
1536
- typeof current.____slothletInternal.impl === "object" &&
1537
- util.types.isProxy(current.____slothletInternal.impl)
1538
- ) {
1539
-
1540
- let proxyResult = current.____slothletInternal.impl;
1541
- for (const remainingProp of remainingChain) {
1542
- proxyResult = proxyResult[remainingProp];
1543
- }
1544
-
1545
-
1546
- return proxyResult[prop];
1547
- }
1548
-
1549
- const isInternal = typeof chainProp === "string" && (chainProp.startsWith("_") || chainProp.startsWith("__"));
1550
- if (!isInternal && current && chainProp in current) {
1551
- const cached = current[chainProp];
1552
-
1553
-
1554
- const _cachedW2 = resolveWrapper(cached);
1555
-
1556
-
1557
- if (_cachedW2) {
1558
- current = _cachedW2;
1559
- remainingChain.shift();
1560
- } else {
1561
-
1562
- return undefined;
1563
- }
1564
-
1565
- } else {
1566
-
1567
- return undefined;
1568
- }
1569
- }
1570
-
1571
-
1572
-
1573
- if (
1574
- current &&
1575
- current.____slothletInternal.impl &&
1576
- typeof current.____slothletInternal.impl === "object" &&
1577
- util.types.isProxy(current.____slothletInternal.impl)
1578
- ) {
1579
- return current.____slothletInternal.impl[prop];
1580
- }
1581
-
1582
-
1583
- const isFinalInternal = typeof prop === "string" && (prop.startsWith("_") || prop.startsWith("__"));
1584
- if (!isFinalInternal && current && prop in current) {
1585
- return current[prop];
1586
- }
1587
-
1588
-
1589
- return undefined;
1590
- }
1591
-
1592
-
1593
-
1594
-
1595
-
1596
- const isInternal = typeof prop === "string" && (prop.startsWith("_") || prop.startsWith("__"));
1597
- if (!isInternal && hasOwn(wrapper, prop)) {
1598
- wrapper.slothlet.debug("wrapper", {
1599
- key: "DEBUG_MODE_WAITING_GET_PREMATURE",
1600
- apiPath: wrapper.____slothletInternal.apiPath,
1601
- prop
1602
- });
1603
- return wrapper[prop];
1604
- }
1605
-
1606
-
1607
-
1608
-
1609
-
1610
-
1611
-
1612
- if (
1613
- wrapper.____slothletInternal.needsImmediateChildAdoption &&
1614
- wrapper.____slothletInternal.materializeFunc &&
1615
- !wrapper.____slothletInternal.state.materialized &&
1616
- !wrapper.____slothletInternal.state.inFlight
1617
- ) {
1618
- wrapper.slothlet.debug("wrapper", {
1619
- key: "DEBUG_MODE_WAITING_GET_IMMEDIATE_MAT",
1620
- apiPath: wrapper.____slothletInternal.apiPath,
1621
- prop
1622
- });
1623
-
1624
- wrapper._materialize().catch((err) => {
1625
- wrapper.slothlet.debug("wrapper", {
1626
- key: "DEBUG_MODE_WAITING_GET_IMMEDIATE_MAT_ERROR",
1627
- apiPath: wrapper.apiPath,
1628
- error: err.message
1629
- });
1630
- });
1631
-
1632
-
1633
- const isInternal = typeof prop === "string" && (prop.startsWith("_") || prop.startsWith("__"));
1634
- if (!isInternal && hasOwn(wrapper, prop)) {
1635
- wrapper.slothlet.debug("wrapper", {
1636
- key: "DEBUG_MODE_WAITING_GET_IMMEDIATE_MAT_SUCCESS",
1637
- apiPath: wrapper.____slothletInternal.apiPath,
1638
- prop
1639
- });
1640
- return wrapper[prop];
1641
- }
1642
- }
1643
-
1644
-
1645
-
1646
- if (wrapper.____slothletInternal.state.inFlight) {
1647
-
1648
-
1649
- return wrapper.___createWaitingProxy([...propChain, prop]);
1650
- }
1651
-
1652
-
1653
- return wrapper.___createWaitingProxy([...propChain, prop]);
1654
- },
1655
-
1656
- async apply(___target, ___thisArg, args) {
1657
-
1658
-
1659
-
1660
-
1661
-
1662
- const ___capturedCallerWrapper = wrapper.slothlet.contextManager?.tryGetContext?.()?.currentWrapper ?? null;
1663
-
1664
- wrapper.slothlet.debug("wrapper", {
1665
- key: "DEBUG_MODE_WAITING_APPLY_ENTRY",
1666
- apiPath: wrapper.____slothletInternal.apiPath,
1667
- propChain: propChain.join(","),
1668
- args: args.join(",")
1669
- });
1670
- const chainLabel = propChain.map((prop) => String(prop)).join(".");
1671
-
1672
- if (
1673
- wrapper.____slothletInternal.mode === "lazy" &&
1674
- !wrapper.____slothletInternal.state.materialized &&
1675
- !wrapper.____slothletInternal.state.inFlight
1676
- ) {
1677
- wrapper.slothlet.debug("wrapper", {
1678
- key: "DEBUG_MODE_WAITING_APPLY_MATERIALIZE",
1679
- apiPath: wrapper.____slothletInternal.apiPath
1680
- });
1681
- await wrapper._materialize();
1682
- wrapper.slothlet.debug("wrapper", {
1683
- key: "DEBUG_MODE_WAITING_APPLY_MATERIALIZED",
1684
- apiPath: wrapper.____slothletInternal.apiPath
1685
- });
1686
- } else if (
1687
- wrapper.____slothletInternal.mode === "lazy" &&
1688
- !wrapper.____slothletInternal.state.materialized &&
1689
- wrapper.____slothletInternal.state.inFlight
1690
- ) {
1691
-
1692
-
1693
-
1694
-
1695
- if (wrapper.____slothletInternal.materializationPromise) {
1696
- await wrapper.____slothletInternal.materializationPromise;
1697
-
1698
-
1699
- } else {
1700
-
1701
- await wrapper._materialize();
1702
- }
1703
-
1704
- }
1705
-
1706
- wrapper.slothlet.debug("wrapper", {
1707
- key: "DEBUG_MODE_WAITING_APPLY_START_WALK",
1708
- apiPath: wrapper.____slothletInternal.apiPath,
1709
- propChain: propChain.join(",")
1710
- });
1711
- let current = wrapper.createProxy();
1712
- let lastWrapper = wrapper;
1713
- let lastObject = null;
1714
-
1715
- for (const prop of propChain) {
1716
- wrapper.slothlet.debug("wrapper", {
1717
- key: "DEBUG_MODE_WAITING_APPLY_WALK",
1718
- prop: String(prop),
1719
- typeOf: typeof current,
1720
- constructorName: current?.constructor?.name
1721
- });
1722
- if (!current) {
1723
-
1724
-
1725
-
1726
-
1727
-
1728
- if (propChain.some((p) => typeof p === "symbol")) {
1729
- return undefined;
1730
- }
1731
-
1732
-
1733
-
1734
-
1735
-
1736
- if (
1737
- wrapper.____slothletInternal.invalid ||
1738
- wrapper.____slothletInternal.impl === null ||
1739
- (lastWrapper && (lastWrapper.__invalid || lastWrapper._impl === null))
1740
- ) {
1741
- return undefined;
1742
- }
1743
-
1744
-
1745
-
1746
-
1747
- const finalProp = propChain[propChain.length - 1];
1748
- if (
1749
- finalProp === "hasAttribute" ||
1750
- finalProp === "toJSON" ||
1751
- finalProp === Symbol.toStringTag ||
1752
- finalProp === "constructor" ||
1753
- typeof finalProp === "symbol" ||
1754
- typeof prop === "symbol"
1755
- ) {
1756
- return undefined;
1757
- }
1758
- throw new wrapper.slothlet.SlothletError(
1759
- "CHAIN_ACCESS_UNDEFINED",
1760
- {
1761
- apiPath: wrapper.____slothletInternal.apiPath,
1762
- chainLabel,
1763
- prop: String(prop)
1764
- },
1765
- null,
1766
- { validationError: true }
1767
- );
1768
- }
1769
-
1770
- const currentWrapper = resolveWrapper(current);
1771
-
1772
-
1773
- if (currentWrapper) {
1774
- const state = currentWrapper.____slothletInternal.state;
1775
- if (!state.materialized) {
1776
-
1777
-
1778
- if (!state.inFlight && typeof current._materialize === "function") {
1779
- await current._materialize();
1780
- }
1781
-
1782
-
1783
-
1784
- while (!currentWrapper.____slothletInternal.state.materialized) {
1785
- const nextState = currentWrapper.____slothletInternal.state;
1786
- if (!nextState.inFlight && !nextState.materialized) {
1787
- throw new wrapper.slothlet.SlothletError(
1788
- "CHAIN_MATERIALIZE_FAILED",
1789
- {
1790
- apiPath: wrapper.____slothletInternal.apiPath,
1791
- chainLabel,
1792
- prop: String(prop)
1793
- },
1794
- null,
1795
- { validationError: true }
1796
- );
1797
- }
1798
- await new Promise((resolve) => setImmediate(resolve));
1799
- }
1800
-
1801
- }
1802
- }
1803
-
1804
-
1805
-
1806
- if (current && currentWrapper) {
1807
- lastWrapper = currentWrapper;
1808
- const isInternal = typeof prop === "string" && (prop.startsWith("_") || prop.startsWith("__"));
1809
- if (!isInternal && prop in currentWrapper) {
1810
- lastObject = current;
1811
- current = currentWrapper[prop];
1812
- continue;
1813
- }
1814
-
1815
- if (
1816
- currentWrapper.____slothletInternal.impl &&
1817
- typeof currentWrapper.____slothletInternal.impl === "object" &&
1818
- currentWrapper.____slothletInternal.impl !== null &&
1819
- prop in currentWrapper.____slothletInternal.impl
1820
- ) {
1821
- lastObject = currentWrapper.____slothletInternal.impl;
1822
- current = currentWrapper.____slothletInternal.impl[prop];
1823
- continue;
1824
- }
1825
- }
1826
-
1827
- lastObject = current;
1828
- current = current[prop];
1829
- }
1830
-
1831
- wrapper.slothlet.debug("wrapper", {
1832
- key: "DEBUG_MODE_WAITING_APPLY",
1833
- apiPath: wrapper.____slothletInternal.apiPath,
1834
- propChain: propChain.join(","),
1835
- typeOf: typeof current,
1836
- currentName: current?.name,
1837
- isFunction: typeof current === "function"
1838
- });
1839
-
1840
- if (typeof current === "function") {
1841
-
1842
-
1843
-
1844
-
1845
-
1846
-
1847
-
1848
-
1849
-
1850
- if (___capturedCallerWrapper && wrapper.slothlet.contextManager) {
1851
- const ___instanceStore = wrapper.slothlet.contextManager.instances?.get?.(wrapper.instanceID);
1852
-
1853
-
1854
-
1855
- if (___instanceStore && !___instanceStore.currentWrapper) {
1856
- return wrapper.slothlet.contextManager.runInContext(
1857
- wrapper.instanceID,
1858
- () => Reflect.apply(current, lastObject, args),
1859
- null,
1860
- [],
1861
- ___capturedCallerWrapper
1862
- );
1863
- }
1864
- }
1865
- return Reflect.apply(current, lastObject, args);
1866
- }
1867
-
1868
-
1869
-
1870
-
1871
-
1872
- const _finalChainProp = propChain[propChain.length - 1];
1873
-
1874
-
1875
- if (_finalChainProp === "hasAttribute" || _finalChainProp === "toJSON") {
1876
- return undefined;
1877
- }
1878
-
1879
-
1880
-
1881
-
1882
- if (current === undefined || current === null) {
1883
- throw new wrapper.slothlet.SlothletError(
1884
- "CHAIN_NOT_CALLABLE",
1885
- {
1886
- apiPath: wrapper.____slothletInternal.apiPath,
1887
- chainLabel
1888
- },
1889
- null,
1890
- { validationError: true }
1891
- );
1892
- }
1893
-
1894
- throw new wrapper.slothlet.SlothletError(
1895
- "CHAIN_NOT_CALLABLE",
1896
- {
1897
- apiPath: wrapper.____slothletInternal.apiPath,
1898
- chainLabel
1899
- },
1900
- null,
1901
- { validationError: true }
1902
- );
1903
- },
1904
- getPrototypeOf: () => null
1905
- });
1906
-
1907
-
1908
-
1909
-
1910
- _proxyRegistry.set(waitingProxy, wrapper);
1911
-
1912
-
1913
- wrapper.____slothletInternal.waitingProxyCache.set(cacheKey, waitingProxy);
1914
-
1915
- return waitingProxy;
1916
- }
1917
-
1918
-
1919
- createProxy() {
1920
- const wrapper = this;
1921
- if (wrapper.____slothletInternal.proxy) {
1922
- return wrapper.____slothletInternal.proxy;
1923
- }
1924
-
1925
-
1926
-
1927
- if (
1928
- wrapper.____slothletInternal.materializeOnCreate &&
1929
- wrapper.____slothletInternal.mode === "lazy" &&
1930
- !wrapper.____slothletInternal.state.materialized &&
1931
- wrapper.____slothletInternal.materializeFunc
1932
- ) {
1933
-
1934
-
1935
- wrapper._materialize().catch(() => {});
1936
- }
1937
-
1938
-
1939
-
1940
-
1941
-
1942
-
1943
-
1944
-
1945
- const mightBeCallable =
1946
- wrapper.____slothletInternal.isCallable ||
1947
- (wrapper.____slothletInternal.mode === "lazy" && !wrapper.____slothletInternal.isCallableLocked);
1948
- let proxyTarget;
1949
- if (mightBeCallable) {
1950
-
1951
-
1952
- proxyTarget = createNamedProxyTarget(wrapper.____slothletInternal.apiPath, "callableProxy");
1953
- } else {
1954
-
1955
- proxyTarget = wrapper;
1956
- }
1957
-
1958
-
1959
- if (!(util.inspect.custom in proxyTarget)) {
1960
- Object.defineProperty(proxyTarget, util.inspect.custom, {
1961
- value: function () {
1962
-
1963
- const childKeys = Object.keys(wrapper).filter((k) => !k.startsWith("_") && !k.startsWith("__"));
1964
- if (childKeys.length > 0 && !wrapper.____slothletInternal.isCallable) {
1965
- const obj = {};
1966
- for (const key of childKeys) {
1967
-
1968
- const child = wrapper[key];
1969
-
1970
-
1971
- if (child && typeof child.createProxy === "function") {
1972
- obj[key] = child.createProxy();
1973
- } else {
1974
- obj[key] = child;
1975
- }
1976
- }
1977
- return obj;
1978
- }
1979
-
1980
-
1981
-
1982
- if (
1983
- wrapper.____slothletInternal.mode === "lazy" &&
1984
- !wrapper.____slothletInternal.state.materialized &&
1985
- (wrapper.____slothletInternal.impl === null || wrapper.____slothletInternal.impl === undefined)
1986
- ) {
1987
-
1988
-
1989
-
1990
- return wrapper.____slothletInternal.proxy || wrapper;
1991
- }
1992
-
1993
- return wrapper.____slothletInternal.impl;
1994
- },
1995
- writable: false,
1996
- enumerable: false,
1997
- configurable: true
1998
- });
1999
- }
2000
-
2001
-
2002
- const getTrap = (target, prop, ____receiver) => {
2003
-
2004
-
2005
- if (target !== wrapper && prop in target) {
2006
- const desc = Object.getOwnPropertyDescriptor(target, prop);
2007
-
2008
-
2009
- if (desc && !desc.configurable) {
2010
-
2011
-
2012
- if (typeof prop === "string" && prop.startsWith("__")) {
2013
- return wrapper[prop];
2014
- }
2015
- return target[prop];
2016
- }
2017
-
2018
- }
2019
-
2020
-
2021
-
2022
-
2023
- if (target === wrapper && typeof prop === "string") {
2024
- const desc = Object.getOwnPropertyDescriptor(target, prop);
2025
- if (desc && !desc.configurable) {
2026
- return target[prop];
2027
- }
2028
- }
2029
-
2030
-
2031
-
2032
-
2033
- const isInternalProp = typeof prop === "string" && UnifiedWrapper.INTERNAL_KEYS.has(prop);
2034
-
2035
-
2036
-
2037
-
2038
- const allowedInternals = new Set([
2039
- "_materialize",
2040
- "__mode",
2041
- "__apiPath",
2042
- "__isCallable",
2043
- "__materializeOnCreate",
2044
- "__displayName",
2045
- "__type",
2046
- "__materialized",
2047
- "__inFlight",
2048
- "__slothletPath",
2049
- "__metadata",
2050
-
2051
- "__filePath",
2052
- "__sourceFolder",
2053
- "__moduleID"
2054
- ]);
2055
- if (isInternalProp && !allowedInternals.has(prop)) {
2056
- return undefined;
2057
- }
2058
-
2059
-
2060
- if (prop === "power" || prop === "add") {
2061
- this.slothlet.debug("wrapper", {
2062
- key: "DEBUG_MODE_GET_START",
2063
- apiPath: wrapper.____slothletInternal.apiPath,
2064
- prop: String(prop),
2065
- mode: wrapper.____slothletInternal.mode,
2066
-
2067
-
2068
- collisionMode: wrapper.____slothletInternal.state.collisionMode || "none",
2069
- materialized: wrapper.____slothletInternal.state.materialized,
2070
- hasImpl: wrapper.____slothletInternal.impl !== null,
2071
- inWrapper: !isInternalProp && hasOwn(wrapper, prop)
2072
- });
2073
- }
2074
-
2075
- if (prop === "__mode") return wrapper.____slothletInternal.mode;
2076
- if (prop === "__apiPath") return wrapper.____slothletInternal.apiPath;
2077
- if (prop === "__isCallable") return wrapper.____slothletInternal.isCallable;
2078
- if (prop === "__materializeOnCreate") return wrapper.____slothletInternal.materializeOnCreate;
2079
- if (prop === "__materialized") return wrapper.____slothletInternal.state.materialized;
2080
- if (prop === "__inFlight") return wrapper.____slothletInternal.state.inFlight;
2081
- if (prop === "__displayName") return wrapper.____slothletInternal.displayName;
2082
- if (prop === "__type") {
2083
-
2084
- if (
2085
- wrapper.____slothletInternal.mode === "lazy" &&
2086
- !wrapper.____slothletInternal.state.materialized &&
2087
- !wrapper.____slothletInternal.state.inFlight
2088
- ) {
2089
-
2090
-
2091
- wrapper._materialize().catch(() => {});
2092
- }
2093
-
2094
-
2095
- if (wrapper.____slothletInternal.mode === "lazy" && wrapper.____slothletInternal.state.inFlight) {
2096
- return TYPE_STATES.IN_FLIGHT;
2097
- }
2098
- if (wrapper.____slothletInternal.mode === "lazy" && !wrapper.____slothletInternal.state.materialized) {
2099
- return TYPE_STATES.UNMATERIALIZED;
2100
- }
2101
-
2102
-
2103
- const impl = wrapper.____slothletInternal.impl;
2104
- if (typeof impl === "function") {
2105
- return "function";
2106
- }
2107
- if (impl && typeof impl === "object" && typeof impl.default === "function") {
2108
- return "function";
2109
- }
2110
- if (impl && typeof impl === "object") {
2111
- return "object";
2112
- }
2113
-
2114
- if (typeof impl === "string") {
2115
- return "string";
2116
- }
2117
- if (typeof impl === "number") {
2118
- return "number";
2119
- }
2120
- if (typeof impl === "boolean") {
2121
- return "boolean";
2122
- }
2123
- if (typeof impl === "symbol") {
2124
- return "symbol";
2125
- }
2126
- if (typeof impl === "bigint") {
2127
- return "bigint";
2128
- }
2129
- return "undefined";
2130
- }
2131
- if (prop === "_materialize") return wrapper._materialize.bind(wrapper);
2132
- if (prop === "__slothletPath") return wrapper.____slothletInternal.apiPath;
2133
-
2134
- if (prop === "__metadata") {
2135
-
2136
- if (wrapper.slothlet.handlers?.metadata) {
2137
- return wrapper.slothlet.handlers.metadata.getMetadata(wrapper);
2138
- }
2139
- return {};
2140
- }
2141
-
2142
-
2143
-
2144
- if (prop === "__filePath") return wrapper.____slothletInternal.filePath ?? undefined;
2145
-
2146
-
2147
- if (prop === "__sourceFolder") return wrapper.____slothletInternal.sourceFolder ?? undefined;
2148
-
2149
-
2150
- if (prop === "__moduleID") return wrapper.____slothletInternal.moduleID ?? undefined;
2151
-
2152
-
2153
- if (prop === "__invalid") return wrapper.____slothletInternal.invalid;
2154
- if (prop === "then") return undefined;
2155
- if (prop === "constructor") return Object.prototype.constructor;
2156
- if (prop === util.inspect.custom) {
2157
-
2158
- return () => {
2159
-
2160
- const childKeys = Object.keys(wrapper).filter((k) => !k.startsWith("_") && !k.startsWith("__"));
2161
- if (childKeys.length > 0 && !wrapper.____slothletInternal.isCallable) {
2162
- const obj = {};
2163
- for (const key of childKeys) {
2164
- const child = wrapper[key];
2165
-
2166
-
2167
- if (child && typeof child.createProxy === "function") {
2168
- obj[key] = child.createProxy();
2169
- } else {
2170
- obj[key] = child;
2171
- }
2172
- }
2173
- return obj;
2174
- }
2175
-
2176
-
2177
-
2178
- if (
2179
- wrapper.____slothletInternal.mode === "lazy" &&
2180
- !wrapper.____slothletInternal.state.materialized &&
2181
- (wrapper.____slothletInternal.impl === null || wrapper.____slothletInternal.impl === undefined)
2182
- ) {
2183
-
2184
- this.slothlet.debug("wrapper", {
2185
- key: "DEBUG_MODE_INSPECT_LAZY_UNMATERIALIZED",
2186
- apiPath: wrapper.apiPath,
2187
- wrapper: wrapper,
2188
- ____proxy: wrapper.____slothletInternal.proxy
2189
- });
2190
-
2191
-
2192
- return wrapper || wrapper.____slothletInternal.proxy;
2193
- }
2194
-
2195
- return wrapper.____slothletInternal.impl;
2196
- };
2197
- }
2198
- if (prop === Symbol.toStringTag) {
2199
-
2200
- const impl = wrapper.____slothletInternal.impl;
2201
- if (typeof impl === "function") {
2202
- return "Function";
2203
- }
2204
- if (impl && typeof impl === "object" && typeof impl.default === "function") {
2205
- return "Function";
2206
- }
2207
- return "Object";
2208
- }
2209
- if (typeof prop === "symbol") return undefined;
2210
- if (prop === "length") {
2211
-
2212
- const impl = wrapper.____slothletInternal.impl;
2213
- if (typeof impl === "function") {
2214
- return impl.length;
2215
- }
2216
- if (impl && typeof impl === "object" && typeof impl.default === "function") {
2217
- return impl.default.length;
2218
- }
2219
- return 0;
2220
- }
2221
- if (prop === "name") {
2222
-
2223
-
2224
-
2225
-
2226
- if (wrapper.____slothletInternal.apiPath) {
2227
- const pathParts = wrapper.____slothletInternal.apiPath.split(".");
2228
- const lastPart = pathParts[pathParts.length - 1];
2229
-
2230
-
2231
- if (lastPart) {
2232
- return lastPart;
2233
- }
2234
- }
2235
-
2236
-
2237
- return target.name || "unifiedWrapperProxy";
2238
- }
2239
- if (prop === "toString") {
2240
-
2241
- const impl = wrapper.____slothletInternal.impl;
2242
- if (typeof impl === "function") {
2243
- return impl.toString.bind(impl);
2244
- }
2245
- if (impl && typeof impl === "object" && typeof impl.default === "function") {
2246
- return impl.default.toString.bind(impl.default);
2247
- }
2248
-
2249
- if (typeof target === "function") {
2250
- return Function.prototype.toString.bind(target);
2251
- }
2252
- return () => `[UnifiedWrapper: ${wrapper.____slothletInternal.apiPath}]`;
2253
- }
2254
- if (prop === "valueOf") {
2255
-
2256
- const impl = wrapper.____slothletInternal.impl;
2257
- if (typeof impl === "function") {
2258
- return impl.valueOf.bind(impl);
2259
- }
2260
- if (impl && typeof impl === "object" && typeof impl.default === "function") {
2261
- return impl.default.valueOf.bind(impl.default);
2262
- }
2263
- return Function.prototype.valueOf.bind(target);
2264
- }
2265
- if (prop === "toJSON") {
2266
-
2267
-
2268
-
2269
- const impl = wrapper.____slothletInternal.impl;
2270
-
2271
-
2272
- if (impl && typeof impl.toJSON === "function") {
2273
- return impl.toJSON.bind(impl);
2274
- }
2275
-
2276
- return () => undefined;
2277
- }
2278
-
2279
- if (wrapper.____slothletInternal.invalid) {
2280
- return undefined;
2281
- }
2282
-
2283
- if (
2284
- wrapper.____slothletInternal.mode === "lazy" &&
2285
- !wrapper.____slothletInternal.state.materialized &&
2286
- !wrapper.____slothletInternal.state.inFlight
2287
- ) {
2288
- wrapper._materialize().catch(() => {});
2289
- }
2290
-
2291
-
2292
-
2293
-
2294
-
2295
- const isInternal = typeof prop === "string" && (prop.startsWith("_") || prop.startsWith("__"));
2296
- if (!isInternal && hasOwn(wrapper, prop)) {
2297
-
2298
- if (wrapper.____slothletInternal.state.collisionMode === "replace" && (prop === "power" || prop === "add")) {
2299
- this.slothlet.debug("wrapper", {
2300
- key: "DEBUG_MODE_GET_CACHED_REPLACE",
2301
- apiPath: wrapper.apiPath,
2302
- prop: String(prop),
2303
- collisionMode: wrapper.____slothletInternal.state.collisionMode,
2304
- wrapperKeys: Object.keys(wrapper)
2305
- .filter((k) => !k.startsWith("_") && !k.startsWith("__"))
2306
- .join(", ")
2307
- });
2308
- }
2309
- this.slothlet.debug("wrapper", {
2310
- key: "DEBUG_MODE_GET_CACHED",
2311
- apiPath: wrapper.____slothletInternal.apiPath,
2312
- prop: String(prop),
2313
- materialized: wrapper.____slothletInternal.state.materialized,
2314
- hasImpl: wrapper.____slothletInternal.impl !== null
2315
- });
2316
- const cached = wrapper[prop];
2317
-
2318
-
2319
- const _cachedWrapper = resolveWrapper(cached);
2320
- if (_cachedWrapper?.____slothletInternal?.impl !== null && _cachedWrapper?.____slothletInternal?.impl !== undefined) {
2321
- const cachedImpl = _cachedWrapper.____slothletInternal.impl;
2322
-
2323
- const cachedType = typeof cachedImpl;
2324
- if (
2325
- cachedType === "string" ||
2326
- cachedType === "number" ||
2327
- cachedType === "boolean" ||
2328
- cachedType === "bigint" ||
2329
- cachedType === "symbol"
2330
- ) {
2331
- return cachedImpl;
2332
- }
2333
- }
2334
-
2335
- if (_cachedWrapper) {
2336
- const cachedWrapper = _cachedWrapper;
2337
- if (
2338
- cachedWrapper.____slothletInternal.mode === "lazy" &&
2339
- !cachedWrapper.____slothletInternal.state.materialized &&
2340
- !cachedWrapper.____slothletInternal.state.inFlight
2341
- ) {
2342
-
2343
-
2344
- cachedWrapper._materialize().catch(() => {});
2345
- }
2346
- }
2347
-
2348
- return cached;
2349
- }
2350
-
2351
-
2352
-
2353
-
2354
-
2355
- if (wrapper.____slothletInternal.impl !== null && wrapper.____slothletInternal.impl !== undefined) {
2356
-
2357
-
2358
- if (typeof wrapper.____slothletInternal.impl === "object" && util.types.isProxy(wrapper.____slothletInternal.impl)) {
2359
- return wrapper.____slothletInternal.impl[prop];
2360
- }
2361
-
2362
-
2363
- const isInternal = typeof prop === "string" && (prop.startsWith("_") || prop.startsWith("__"));
2364
-
2365
-
2366
- if (!isInternal && hasOwn(wrapper, prop)) {
2367
- if (prop === "power" || prop === "add") {
2368
- this.slothlet.debug("wrapper", {
2369
- key: "DEBUG_MODE_GET_PROXYGET_ACCESSING",
2370
- prop,
2371
- wrapperId: wrapper.____slothletInternal.id,
2372
- apiPath: wrapper.apiPath,
2373
- collisionMode: wrapper.____slothletInternal.state.collisionMode || "none"
2374
- });
2375
- this.slothlet.debug("wrapper", {
2376
- key: "DEBUG_MODE_GET_PROXYGET_FOUND",
2377
- wrapperKeys: Object.keys(wrapper)
2378
- .filter((k) => !k.startsWith("_") && !k.startsWith("__"))
2379
- .join(", ")
2380
- });
2381
- }
2382
- const cached = wrapper[prop];
2383
-
2384
- const _cachedWrapper4 = resolveWrapper(cached);
2385
- if (_cachedWrapper4) {
2386
- const cachedWrapper = _cachedWrapper4;
2387
- if (
2388
- cachedWrapper.____slothletInternal.mode === "lazy" &&
2389
- !cachedWrapper.____slothletInternal.state.materialized &&
2390
- !cachedWrapper.____slothletInternal.state.inFlight
2391
- ) {
2392
- cachedWrapper._materialize().catch(() => {});
2393
- }
2394
- }
2395
- return cached;
2396
- }
2397
-
2398
- }
2399
-
2400
-
2401
-
2402
-
2403
- const isInternalProp2 = typeof prop === "string" && UnifiedWrapper.INTERNAL_KEYS.has(prop);
2404
- if (
2405
- wrapper.____slothletInternal.mode === "lazy" &&
2406
- wrapper.____slothletInternal.state.materialized &&
2407
- !isInternalProp2 &&
2408
- !hasOwn(wrapper, prop) &&
2409
- wrapper.____slothletInternal.impl &&
2410
- !(prop in wrapper.____slothletInternal.impl)
2411
- ) {
2412
- return undefined;
2413
- }
2414
-
2415
- if (
2416
- wrapper.____slothletInternal.mode === "lazy" &&
2417
- (wrapper.____slothletInternal.state.inFlight || !wrapper.____slothletInternal.impl)
2418
- ) {
2419
-
2420
-
2421
-
2422
-
2423
-
2424
- if (!wrapper.____slothletInternal.state.materialized && !wrapper.____slothletInternal.state.inFlight) {
2425
- wrapper._materialize().catch(() => {});
2426
- }
2427
-
2428
- this.slothlet.debug("wrapper", {
2429
- key: "DEBUG_MODE_LAZY_GET_CREATE_WAITING_PROXY",
2430
- prop: String(prop),
2431
-
2432
-
2433
- collisionMode: wrapper.____slothletInternal.state.collisionMode || "none",
2434
- apiPath: wrapper.____slothletInternal.apiPath
2435
- });
2436
-
2437
-
2438
-
2439
-
2440
- if (
2441
- wrapper.____slothletInternal.impl &&
2442
- typeof wrapper.____slothletInternal.impl === "object" &&
2443
- util.types.isProxy(wrapper.____slothletInternal.impl)
2444
- ) {
2445
- const value = wrapper.____slothletInternal.impl[prop];
2446
- return value;
2447
- }
2448
-
2449
- return wrapper.___createWaitingProxy([prop]);
2450
- }
2451
-
2452
-
2453
-
2454
-
2455
- if (
2456
- wrapper.____slothletInternal.impl &&
2457
- typeof wrapper.____slothletInternal.impl === "object" &&
2458
- util.types.isProxy(wrapper.____slothletInternal.impl)
2459
- ) {
2460
- const value = wrapper.____slothletInternal.impl[prop];
2461
-
2462
-
2463
- return value;
2464
- }
2465
-
2466
-
2467
-
2468
-
2469
-
2470
-
2471
- let value = wrapper.____slothletInternal.impl ? wrapper.____slothletInternal.impl[prop] : undefined;
2472
-
2473
-
2474
- if (value === undefined && wrapper.____slothletInternal.impl) {
2475
- const descriptor = Object.getOwnPropertyDescriptor(wrapper.____slothletInternal.impl, prop);
2476
-
2477
-
2478
- if (descriptor && descriptor.get) {
2479
-
2480
- value = descriptor.get.call(wrapper.____slothletInternal.impl);
2481
- }
2482
-
2483
- }
2484
-
2485
-
2486
-
2487
-
2488
- if (value === undefined && Object.prototype.hasOwnProperty.call(target, prop)) {
2489
- value = target[prop];
2490
- }
2491
-
2492
- if (value === undefined) {
2493
- return undefined;
2494
- }
2495
-
2496
-
2497
-
2498
- const valueType = typeof value;
2499
- if (
2500
- value === null ||
2501
- valueType === "string" ||
2502
- valueType === "number" ||
2503
- valueType === "boolean" ||
2504
- valueType === "bigint" ||
2505
- valueType === "symbol"
2506
- ) {
2507
- return value;
2508
- }
2509
-
2510
-
2511
-
2512
-
2513
- if (
2514
- value instanceof Map ||
2515
- value instanceof Set ||
2516
- value instanceof WeakMap ||
2517
- value instanceof WeakSet ||
2518
- value instanceof Date ||
2519
- value instanceof RegExp ||
2520
- value instanceof Promise ||
2521
- value instanceof Error ||
2522
- ArrayBuffer.isView(value) ||
2523
- value instanceof ArrayBuffer
2524
- ) {
2525
- return value;
2526
- }
2527
-
2528
-
2529
-
2530
-
2531
-
2532
-
2533
-
2534
- if (value && typeof value === "object" && util.types.isProxy(value)) {
2535
- return value;
2536
- }
2537
-
2538
- if (value && (typeof value === "object" || typeof value === "function") && resolveWrapper(value) !== null) {
2539
- return value;
2540
- }
2541
-
2542
- const wrapped = wrapper.___createChildWrapper(prop, value);
2543
-
2544
-
2545
- if (wrapped) {
2546
- Object.defineProperty(wrapper, prop, {
2547
- value: wrapped,
2548
- writable: false,
2549
- enumerable: true,
2550
- configurable: true
2551
- });
2552
- return wrapped;
2553
- }
2554
-
2555
-
2556
-
2557
-
2558
-
2559
- return value;
2560
- };
2561
-
2562
-
2563
- const applyTrap = (target, thisArg, args) => {
2564
- if (wrapper.____slothletInternal.invalid) {
2565
-
2566
-
2567
- throw new TypeError(`${wrapper.____slothletInternal.apiPath || "api"} is invalidated`);
2568
- }
2569
-
2570
-
2571
-
2572
-
2573
- const permissionManager = wrapper.slothlet.handlers?.permissionManager;
2574
- if (permissionManager && permissionManager.isEnabled()) {
2575
- const ctx = wrapper.slothlet.contextManager?.tryGetContext?.();
2576
-
2577
-
2578
- const callerWrapper = ctx?.currentWrapper;
2579
-
2580
-
2581
- if (callerWrapper) {
2582
-
2583
-
2584
- const callerPath = callerWrapper.____slothletInternal?.apiPath ?? "";
2585
- const callerFilePath = callerWrapper.____slothletInternal?.filePath ?? null;
2586
- const targetPath = wrapper.____slothletInternal.apiPath;
2587
- const targetFilePath = wrapper.____slothletInternal.filePath ?? null;
2588
-
2589
-
2590
- if (!permissionManager.checkAccess(callerPath, targetPath, callerFilePath, targetFilePath)) {
2591
- throw new wrapper.SlothletError("PERMISSION_DENIED", {
2592
- caller: callerPath,
2593
- target: targetPath
2594
- });
2595
- }
2596
- }
2597
- }
2598
-
2599
-
2600
- const hookManager = wrapper.slothlet.handlers?.hookManager;
2601
-
2602
- const hasHooks = hookManager && hookManager.enabled && !wrapper.____slothletInternal.apiPath.startsWith("slothlet.hook");
2603
-
2604
-
2605
- const api = wrapper.slothlet.boundApi;
2606
- const ctx = wrapper.slothlet.config?.context || {};
2607
-
2608
-
2609
- let result;
2610
- let finalResult;
2611
- let isAsync = false;
2612
-
2613
- try {
2614
-
2615
- if (hasHooks) {
2616
- const beforeResult = hookManager.executeBeforeHooks(wrapper.____slothletInternal.apiPath, args, api, ctx);
2617
- args = beforeResult.args;
2618
-
2619
-
2620
- if (beforeResult.shortCircuit) {
2621
-
2622
- finalResult = beforeResult.value;
2623
- return beforeResult.value;
2624
- }
2625
- }
2626
-
2627
-
2628
- if (
2629
- wrapper.____slothletInternal.mode === "lazy" &&
2630
- !wrapper.____slothletInternal.state.materialized &&
2631
- !wrapper.____slothletInternal.state.inFlight
2632
- ) {
2633
-
2634
-
2635
- wrapper._materialize().catch(() => {});
2636
- }
2637
-
2638
-
2639
- if (wrapper.____slothletInternal.mode === "lazy" && wrapper.____slothletInternal.state.inFlight) {
2640
- return new Promise((resolve, reject) => {
2641
- const checkMaterialized = () => {
2642
- if (wrapper.____slothletInternal.state.materialized) {
2643
- const impl = wrapper.____slothletInternal.impl;
2644
- try {
2645
- if (typeof impl === "function") {
2646
- if (wrapper.slothlet.contextManager) {
2647
- resolve(wrapper.slothlet.contextManager.runInContext(wrapper.instanceID, impl, thisArg, args, wrapper));
2648
- } else {
2649
- resolve(impl.apply(thisArg, args));
2650
- }
2651
- } else if (impl && typeof impl === "object" && typeof impl.default === "function") {
2652
- if (wrapper.contextManager) {
2653
- resolve(wrapper.contextManager.runInContext(wrapper.instanceID, impl.default, impl, args, wrapper));
2654
- } else {
2655
- resolve(impl.default.apply(impl, args));
2656
- }
2657
- } else {
2658
- reject(
2659
- new wrapper.slothlet.SlothletError(
2660
- "INVALID_CONFIG_NOT_A_FUNCTION",
2661
- {
2662
- apiPath: wrapper.____slothletInternal.apiPath,
2663
- actualType: typeof impl
2664
- },
2665
- null,
2666
- { validationError: true }
2667
- )
2668
- );
2669
- }
2670
- } catch (err) {
2671
-
2672
-
2673
-
2674
- reject(err);
2675
- }
2676
- return;
2677
- }
2678
-
2679
-
2680
-
2681
- if (!wrapper.____slothletInternal.state.inFlight) {
2682
- reject(
2683
- new wrapper.slothlet.SlothletError(
2684
- "INVALID_CONFIG_LAZY_MATERIALIZATION_FAILED",
2685
- { apiPath: wrapper.____slothletInternal.apiPath },
2686
- null,
2687
- {
2688
- validationError: true
2689
- }
2690
- )
2691
- );
2692
- return;
2693
-
2694
- }
2695
- setImmediate(checkMaterialized);
2696
- };
2697
- checkMaterialized();
2698
- });
2699
- }
2700
-
2701
-
2702
- const impl = wrapper.____slothletInternal.impl;
2703
-
2704
- if (typeof impl === "function") {
2705
- if (wrapper.slothlet.contextManager) {
2706
- result = wrapper.slothlet.contextManager.runInContext(wrapper.instanceID, impl, thisArg, args, wrapper);
2707
- } else {
2708
- result = impl.apply(thisArg, args);
2709
- }
2710
- } else if (impl && typeof impl === "object" && typeof impl.default === "function") {
2711
- if (wrapper.slothlet.contextManager) {
2712
- result = wrapper.slothlet.contextManager.runInContext(wrapper.instanceID, impl.default, impl, args, wrapper);
2713
- } else {
2714
- result = impl.default.apply(impl, args);
2715
- }
2716
- } else {
2717
- throw new wrapper.SlothletError(
2718
- "INVALID_CONFIG_NOT_A_FUNCTION",
2719
- {
2720
- apiPath: wrapper.____slothletInternal.apiPath,
2721
- actualType: typeof impl
2722
- },
2723
- null,
2724
- { validationError: true }
2725
- );
2726
- }
2727
-
2728
-
2729
- if (result && typeof result === "object" && typeof result.then === "function") {
2730
-
2731
- isAsync = true;
2732
-
2733
- return result.then(
2734
- (resolvedResult) => {
2735
- try {
2736
-
2737
- if (hasHooks) {
2738
- const afterResult = hookManager.executeAfterHooks(wrapper.____slothletInternal.apiPath, resolvedResult, args, api, ctx);
2739
- const finalResult = afterResult.modified ? afterResult.result : resolvedResult;
2740
- hookManager.executeAlwaysHooks(wrapper.____slothletInternal.apiPath, args, finalResult, false, [], api, ctx);
2741
- return finalResult;
2742
- }
2743
- return resolvedResult;
2744
- } catch (error) {
2745
-
2746
-
2747
-
2748
- if (hasHooks) {
2749
- const originalError = unwrapError(error);
2750
- const sourceInfo = {
2751
- type: "after",
2752
- timestamp: Date.now(),
2753
- stack: originalError.stack
2754
- };
2755
- hookManager.executeErrorHooks(wrapper.____slothletInternal.apiPath, originalError, sourceInfo, args, api, ctx);
2756
- hookManager.executeAlwaysHooks(wrapper.____slothletInternal.apiPath, args, undefined, true, [originalError], api, ctx);
2757
- }
2758
-
2759
-
2760
- const suppressErrors = wrapper.slothlet.config?.hook?.suppressErrors === true;
2761
- if (suppressErrors) {
2762
- return undefined;
2763
- }
2764
-
2765
- throw error;
2766
- }
2767
- },
2768
- (error) => {
2769
-
2770
- if (hasHooks && !error[ERROR_HOOK_PROCESSED]) {
2771
- const originalError = unwrapError(error);
2772
- const sourceInfo = {
2773
- type: "function",
2774
- timestamp: Date.now(),
2775
- stack: originalError.stack
2776
- };
2777
- hookManager.executeErrorHooks(wrapper.____slothletInternal.apiPath, originalError, sourceInfo, args, api, ctx);
2778
- }
2779
-
2780
- if (hasHooks) {
2781
- const originalError = unwrapError(error);
2782
- hookManager.executeAlwaysHooks(wrapper.____slothletInternal.apiPath, args, undefined, true, [originalError], api, ctx);
2783
- }
2784
-
2785
-
2786
- const suppressErrors = wrapper.slothlet.config?.hook?.suppressErrors === true;
2787
- if (suppressErrors) {
2788
- return undefined;
2789
- }
2790
-
2791
- throw error;
2792
- }
2793
- );
2794
- }
2795
-
2796
-
2797
- finalResult = result;
2798
- if (hasHooks) {
2799
- const afterResult = hookManager.executeAfterHooks(wrapper.____slothletInternal.apiPath, result, args, api, ctx);
2800
- if (afterResult.modified) {
2801
- finalResult = afterResult.result;
2802
- }
2803
- }
2804
- return finalResult;
2805
- } catch (error) {
2806
-
2807
- this.lastSyncError = error;
2808
- if (hasHooks && !error[ERROR_HOOK_PROCESSED]) {
2809
- const originalError = unwrapError(error);
2810
- const sourceInfo = {
2811
- type: "function",
2812
- timestamp: Date.now(),
2813
- stack: originalError.stack
2814
- };
2815
- hookManager.executeErrorHooks(wrapper.____slothletInternal.apiPath, originalError, sourceInfo, args, api, ctx);
2816
- }
2817
-
2818
-
2819
- const suppressErrors = wrapper.slothlet.config?.hook?.suppressErrors === true;
2820
- if (suppressErrors) {
2821
-
2822
- return undefined;
2823
- }
2824
-
2825
- throw error;
2826
- } finally {
2827
-
2828
-
2829
-
2830
- if (hasHooks && !isAsync) {
2831
-
2832
- const syncError = this.lastSyncError;
2833
- this.lastSyncError = null;
2834
-
2835
- const resultValue = syncError ? undefined : typeof finalResult !== "undefined" ? finalResult : result;
2836
- const errors = syncError ? [unwrapError(syncError)] : [];
2837
- hookManager.executeAlwaysHooks(wrapper.____slothletInternal.apiPath, args, resultValue, !!syncError, errors, api, ctx);
2838
- }
2839
- }
2840
- };
2841
-
2842
-
2843
- const hasTrap = (target, prop) => {
2844
- if (prop === "_materialize") {
2845
- return true;
2846
- }
2847
-
2848
- if (
2849
- wrapper.____slothletInternal.mode === "lazy" &&
2850
- !wrapper.____slothletInternal.state.materialized &&
2851
- !wrapper.____slothletInternal.state.inFlight
2852
- ) {
2853
-
2854
-
2855
- wrapper._materialize().catch(() => {});
2856
- }
2857
-
2858
-
2859
- const isInternal = typeof prop === "string" && (prop.startsWith("_") || prop.startsWith("__"));
2860
- if (!isInternal && hasOwn(wrapper, prop)) {
2861
- return true;
2862
- }
2863
-
2864
- if (
2865
- wrapper.____slothletInternal.impl &&
2866
- (typeof wrapper.____slothletInternal.impl === "object" || typeof wrapper.____slothletInternal.impl === "function") &&
2867
- prop in wrapper.____slothletInternal.impl
2868
- ) {
2869
- return true;
2870
- }
2871
-
2872
- return Object.prototype.hasOwnProperty.call(target, prop);
2873
- };
2874
-
2875
-
2876
- const getOwnPropertyDescriptorTrap = (target, prop) => {
2877
- if (
2878
- wrapper.____slothletInternal.mode === "lazy" &&
2879
- !wrapper.____slothletInternal.state.materialized &&
2880
- !wrapper.____slothletInternal.state.inFlight
2881
- ) {
2882
-
2883
-
2884
- wrapper._materialize().catch(() => {});
2885
- }
2886
-
2887
- if (prop === "____slothletInternal") return undefined;
2888
-
2889
- if (prop === "prototype" && typeof target === "function") {
2890
- const desc = Object.getOwnPropertyDescriptor(target, "prototype");
2891
-
2892
-
2893
-
2894
- if (desc) {
2895
- return desc;
2896
- }
2897
- }
2898
-
2899
- if (Object.prototype.hasOwnProperty.call(target, prop)) {
2900
- return Object.getOwnPropertyDescriptor(target, prop);
2901
- }
2902
-
2903
-
2904
- const isInternal = typeof prop === "string" && (prop.startsWith("_") || prop.startsWith("__"));
2905
- if (!isInternal && hasOwn(wrapper, prop)) {
2906
- const desc = Object.getOwnPropertyDescriptor(wrapper, prop);
2907
-
2908
-
2909
-
2910
- if (desc) {
2911
- return desc;
2912
- }
2913
- }
2914
-
2915
- if (
2916
- wrapper.____slothletInternal.impl &&
2917
- (typeof wrapper.____slothletInternal.impl === "object" || typeof wrapper.____slothletInternal.impl === "function") &&
2918
- prop in wrapper.____slothletInternal.impl
2919
- ) {
2920
- return Object.getOwnPropertyDescriptor(wrapper.____slothletInternal.impl, prop);
2921
- }
2922
-
2923
- return undefined;
2924
- };
2925
-
2926
-
2927
- const ownKeysTrap = (target) => {
2928
- if (
2929
- wrapper.____slothletInternal.mode === "lazy" &&
2930
- !wrapper.____slothletInternal.state.materialized &&
2931
- !wrapper.____slothletInternal.state.inFlight
2932
- ) {
2933
-
2934
-
2935
- wrapper._materialize().catch(() => {});
2936
- }
2937
-
2938
- const keys = new Set();
2939
-
2940
-
2941
-
2942
-
2943
- if (typeof target === "function" || (target && target.__isCallable)) {
2944
- keys.add("prototype");
2945
- keys.add("length");
2946
- keys.add("name");
2947
- }
2948
-
2949
-
2950
- for (const key of Reflect.ownKeys(target)) {
2951
- const descriptor = Object.getOwnPropertyDescriptor(target, key);
2952
-
2953
-
2954
- if (!descriptor.configurable || descriptor.enumerable) {
2955
- keys.add(key);
2956
- }
2957
- }
2958
-
2959
-
2960
-
2961
- if (target !== wrapper) {
2962
- for (const key of Reflect.ownKeys(wrapper)) {
2963
- const descriptor = Object.getOwnPropertyDescriptor(wrapper, key);
2964
-
2965
-
2966
- if (descriptor && descriptor.enumerable) {
2967
- keys.add(key);
2968
- }
2969
- }
2970
- }
2971
-
2972
-
2973
- const implKeys =
2974
- wrapper.____slothletInternal.impl &&
2975
- (typeof wrapper.____slothletInternal.impl === "object" || typeof wrapper.____slothletInternal.impl === "function")
2976
- ? Reflect.ownKeys(wrapper.____slothletInternal.impl)
2977
- : [];
2978
- for (const key of implKeys) {
2979
-
2980
- if (key !== "prototype") {
2981
- keys.add(key);
2982
- }
2983
- }
2984
-
2985
- return Array.from(keys);
2986
- };
2987
-
2988
-
2989
- const setTrap = (target, prop, value) => {
2990
- if (
2991
- wrapper.____slothletInternal.mode === "lazy" &&
2992
- !wrapper.____slothletInternal.state.materialized &&
2993
- !wrapper.____slothletInternal.state.inFlight
2994
- ) {
2995
- wrapper._materialize();
2996
- }
2997
-
2998
-
2999
-
3000
-
3001
- if (UnifiedWrapper.INTERNAL_KEYS.has(prop) && prop !== "_materialize") return true;
3002
-
3003
- const internalKeys = new Set(["_materialize"]);
3004
- if (!internalKeys.has(prop)) {
3005
-
3006
- if (hasOwn(wrapper, prop)) {
3007
- delete wrapper[prop];
3008
- }
3009
- Object.defineProperty(wrapper, prop, {
3010
- value: value,
3011
- writable: false,
3012
- enumerable: true,
3013
- configurable: true
3014
- });
3015
- } else {
3016
-
3017
- target[prop] = value;
3018
- }
3019
- return true;
3020
- };
3021
-
3022
-
3023
- const deletePropertyTrap = (target, prop) => {
3024
-
3025
-
3026
-
3027
-
3028
-
3029
-
3030
-
3031
-
3032
-
3033
- const internalKeys = new Set([
3034
-
3035
- "____slothletInternal",
3036
- "__impl",
3037
- "___setImpl",
3038
- "___resetLazy",
3039
- "_materialize",
3040
- "___invalidate",
3041
- "_impl",
3042
- "___getState",
3043
- "__state",
3044
-
3045
- "__mode",
3046
- "__apiPath",
3047
- "__slothletPath",
3048
- "__isCallable",
3049
- "__materializeOnCreate",
3050
- "__displayName",
3051
- "__type",
3052
- "__metadata",
3053
- "__invalid",
3054
- "__filePath",
3055
- "__sourceFolder",
3056
- "__moduleID",
3057
- "__materialized",
3058
- "__inFlight"
3059
- ]);
3060
- if (internalKeys.has(prop)) {
3061
- return true;
3062
- }
3063
-
3064
-
3065
- const isInternal = typeof prop === "string" && (prop.startsWith("_") || prop.startsWith("__"));
3066
- if (!isInternal && hasOwn(wrapper, prop)) {
3067
- const childWrapper = wrapper[prop];
3068
- const childWrapperRaw = resolveWrapper(childWrapper);
3069
-
3070
-
3071
- if (childWrapperRaw) {
3072
- childWrapperRaw.___invalidate();
3073
- }
3074
- const descriptor = Object.getOwnPropertyDescriptor(wrapper, prop);
3075
-
3076
-
3077
- if (descriptor?.configurable) {
3078
- delete wrapper[prop];
3079
- }
3080
- }
3081
-
3082
-
3083
- if (
3084
- wrapper.____slothletInternal.impl &&
3085
- typeof wrapper.____slothletInternal.impl === "object" &&
3086
- prop in wrapper.____slothletInternal.impl
3087
- ) {
3088
- delete wrapper.____slothletInternal.impl[prop];
3089
- }
3090
-
3091
-
3092
- delete target[prop];
3093
-
3094
- return true;
3095
- };
3096
-
3097
- wrapper.____slothletInternal.proxy = new Proxy(proxyTarget, {
3098
- get: getTrap,
3099
- apply: applyTrap,
3100
- has: hasTrap,
3101
- getOwnPropertyDescriptor: getOwnPropertyDescriptorTrap,
3102
- ownKeys: ownKeysTrap,
3103
- set: setTrap,
3104
- deleteProperty: deletePropertyTrap,
3105
- getPrototypeOf: () => null
3106
- });
3107
-
3108
- _proxyRegistry.set(wrapper.____slothletInternal.proxy, wrapper);
3109
- return wrapper.____slothletInternal.proxy;
3110
- }
3111
- }
3112
-
3113
-
3114
- export function resolveWrapper(value) {
3115
- if (!value) return null;
3116
-
3117
-
3118
-
3119
- const registered = _proxyRegistry.get(value);
3120
- if (registered) return registered;
3121
-
3122
-
3123
-
3124
- if (value instanceof UnifiedWrapper && value.____slothletInternal != null) return value;
3125
- return null;
3126
- }
17
+ const ____COLLISION_MERGED_PROPERTY=Symbol("collisionMergedProperty");import util from"node:util";import{ComponentBase}from"@cldmv/slothlet/factories/component-base";const ERROR_HOOK_PROCESSED=Symbol.for("@cldmv/slothlet/hook-error-processed");const hasOwn=(obj,key)=>Object.prototype.hasOwnProperty.call(obj,key);function unwrapError(error){if(error&&error.name==="SlothletError"&&error.originalError){return error.originalError}return error}const wrapperDebugEnabled=process.env.SLOTHLET_DEBUG_WRAPPER==="1"||process.env.SLOTHLET_DEBUG_WRAPPER==="true"||process.env.SLOTHLET_DEBUG_SCRIPT_VERBOSE==="1"||process.env.SLOTHLET_DEBUG_SCRIPT_VERBOSE==="true";const TYPE_STATES={UNMATERIALIZED:Symbol("unmaterialized"),IN_FLIGHT:Symbol("inFlight")};function getSafeFunctionName(apiPath,fallback){const parts=String(apiPath||"").split(".").filter(part=>part&&part!=="null");const baseName=parts.length>0?parts[parts.length-1]:"";let safeName=String(baseName||"").replace(/[^A-Za-z0-9_$]/g,"_");if(!safeName||!/^[A-Za-z_$]/.test(safeName[0])){safeName=safeName?`_${safeName}`:""}return safeName||fallback}function createNamedProxyTarget(nameHint,fallback){const safeName=getSafeFunctionName(nameHint,fallback);return{[safeName]:function(){}}[safeName]}const _proxyRegistry=new WeakMap;class UnifiedWrapper extends ComponentBase{#internal=null;get ____slothletInternal(){if(!(#internal in this))return void 0;return this.#internal}constructor(slothlet,{mode,apiPath,initialImpl=null,materializeFunc=null,isCallable,materializeOnCreate=false,filePath=null,moduleID=null,sourceFolder=null}){super(slothlet);const isCallableExplicit=typeof isCallable==="boolean";const isCallableValue=isCallableExplicit?isCallable:typeof initialImpl==="function"||initialImpl&&typeof initialImpl.default==="function";const isCallableLocked=isCallableValue||isCallableExplicit;const internal=Object.create(null);internal.id=Math.random().toString(36).substr(2,9);internal.mode=mode;internal.apiPath=apiPath;internal.materializeOnCreate=materializeOnCreate;internal.isCallable=isCallableValue;internal.isCallableLocked=isCallableLocked;internal.moduleID=moduleID;internal.filePath=filePath;internal.sourceFolder=sourceFolder;internal.invalid=false;internal.state={materialized:initialImpl!==null,inFlight:false,collisionMode:"merge"};internal.displayName=apiPath?`${String(apiPath).replace(/\./g,"__")}__UnifiedWrapper`:"UnifiedWrapper";this.#internal=internal;const wrapper=this;Object.defineProperty(internal,"wrapper",{get(){return wrapper},enumerable:false,configurable:false});internal.callableImpl=null;internal.waitingProxyCache=new Map;internal.proxy=null;internal.impl=UnifiedWrapper._cloneImpl(initialImpl);internal.materializeFunc=materializeFunc;if(filePath&&slothlet.handlers?.lifecycle){slothlet.handlers.lifecycle.emit("impl:created",{apiPath,impl:this,wrapper:Object.freeze({__impl:this.____slothletInternal.impl}),source:"initial",moduleID,filePath,sourceFolder:sourceFolder||slothlet.config?.dir})}if(initialImpl!==null&&filePath&&slothlet.handlers?.lifecycle){slothlet.handlers.lifecycle.emit("impl:created",{apiPath,impl:initialImpl,wrapper:Object.freeze({__impl:this.____slothletInternal.impl}),source:"initial",moduleID,filePath,sourceFolder:sourceFolder||slothlet.config?.dir})}if(initialImpl!==null){const implKeys=Object.keys(initialImpl||{});if((wrapperDebugEnabled||this.____config?.debug?.wrapper)&&apiPath&&(apiPath==="config"||apiPath.startsWith("config."))){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_WRAPPER_CONSTRUCTOR_IMPL_KEYS",apiPath,keyCount:implKeys.length,keySample:implKeys.slice(0,5)})}this.___adoptImplChildren();if((wrapperDebugEnabled||this.____config?.debug?.wrapper)&&apiPath&&(apiPath==="config"||apiPath.startsWith("config."))){const childKeys=Object.keys(this).filter(k=>!k.startsWith("_")&&!k.startsWith("__"));this.slothlet.debug("wrapper",{key:"DEBUG_MODE_WRAPPER_CONSTRUCTOR_AFTER_ADOPT",apiPath,childCount:childKeys.length,childKeySample:childKeys.slice(0,5)})}}if(mode==="lazy"){slothlet._registerLazyWrapper();if(slothlet.config.tracking?.materialization){setImmediate(()=>{this._materialize().catch(err=>{if(slothlet.config?.debug?.materialize){slothlet.debug("materialize",{key:"DEBUG_MODE_BACKGROUND_MATERIALIZE_ERROR",apiPath:this.____slothletInternal?.apiPath,error:err.message})}})})}}}[util.inspect.custom](____depth,____options,____inspect){const w=_proxyRegistry.get(this)??this;const childKeys=Object.keys(w).filter(k=>!k.startsWith("_")&&!k.startsWith("__"));if(childKeys.length>0&&!w.____slothletInternal?.isCallable){const inspectObj={};for(const key of childKeys){inspectObj[key]=w[key]}return inspectObj}if(w.____slothletInternal?.mode==="lazy"&&w.____slothletInternal?.state&&!w.____slothletInternal?.state.materialized&&w.____slothletInternal?.proxy){return w.____slothletInternal.proxy}return w.____slothletInternal?.impl}get __impl(){return this.____slothletInternal.impl}static _cloneImpl(value){if(value&&typeof value==="object"&&!Array.isArray(value)&&typeof value!=="function"){const isProxy=util.types.isProxy(value);if(isProxy){if(resolveWrapper(value)){const clone={};for(const key of Reflect.ownKeys(value)){try{clone[key]=value[key]}catch{}}return clone}return value}const uw_cloneDescriptors=Object.getOwnPropertyDescriptors(value);return Object.create(Object.getPrototypeOf(value),uw_cloneDescriptors)}return value}static _extractFullImpl(wrapper){if(!wrapper)return null;const impl=wrapper.____slothletInternal.impl;if(impl===null||impl===void 0)return impl;if(typeof impl!=="object"&&typeof impl!=="function")return impl;if(typeof impl==="function")return impl;const extractFullImpl_result={};for(const key of Object.keys(impl)){if(key.startsWith("__"))continue;extractFullImpl_result[key]=impl[key]}if(impl.__childFilePaths){extractFullImpl_result.__childFilePaths=impl.__childFilePaths}if(impl.__childFilePathsPreMaterialize){extractFullImpl_result.__childFilePathsPreMaterialize=impl.__childFilePathsPreMaterialize}for(const key of Object.keys(wrapper)){if(UnifiedWrapper.INTERNAL_KEYS.has(key))continue;if(key in extractFullImpl_result)continue;const extractFullImpl_child=wrapper[key];const _extractChildW=resolveWrapper(extractFullImpl_child);if(_extractChildW){extractFullImpl_result[key]=UnifiedWrapper._extractFullImpl(_extractChildW)}else{extractFullImpl_result[key]=extractFullImpl_child}}return extractFullImpl_result}_applyNewImpl(newImpl,forceReuseChildren=false){this.____slothletInternal.impl=UnifiedWrapper._cloneImpl(newImpl);this.____slothletInternal.invalid=false;if(!this.____slothletInternal.isCallableLocked&&!this.____slothletInternal.isCallable&&(typeof newImpl==="function"||newImpl&&typeof newImpl.default==="function")){this.____slothletInternal.isCallable=true;this.____slothletInternal.isCallableLocked=true}if(!this.____slothletInternal.filePath&&this.____slothletInternal.impl&&this.____slothletInternal.impl.__filePath){this.____slothletInternal.filePath=this.____slothletInternal.impl.__filePath;this.slothlet.debug("wrapper",{key:"DEBUG_MODE_APPLY_IMPL_UPDATE_PATH",apiPath:this.____slothletInternal.apiPath,filePath:this.____slothletInternal.filePath})}this.___adoptImplChildren(forceReuseChildren)}___setImpl(newImpl,moduleID=null,forceReuseChildren=false){if((wrapperDebugEnabled||this.____config?.debug?.wrapper)&&this.____slothletInternal.apiPath==="string"){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_SETIMPL_CALLED",apiPath:this.____slothletInternal.apiPath,newImplKeys:Object.keys(newImpl||{})})}this._applyNewImpl(newImpl,forceReuseChildren);if(newImpl&&this.slothlet.handlers?.lifecycle){const wrapperMetadata=this.slothlet.handlers.metadata.getMetadata(this);let extractedModuleId=moduleID||(wrapperMetadata?.moduleID?wrapperMetadata.moduleID.split(":")[0]:null);if(extractedModuleId&&typeof extractedModuleId!=="string"){extractedModuleId=extractedModuleId.moduleID||extractedModuleId.__moduleID||String(extractedModuleId)}this.slothlet.handlers.lifecycle.emit("impl:changed",{apiPath:this.____slothletInternal.apiPath,impl:newImpl,wrapper:Object.freeze({__impl:this.____slothletInternal.impl}),source:"hot-reload",moduleID:extractedModuleId,filePath:wrapperMetadata?.filePath,sourceFolder:wrapperMetadata?.sourceFolder})}this.____slothletInternal.state.materialized=true;this.____slothletInternal.state.inFlight=false;if(this.slothlet._onWrapperMaterialized){this.slothlet._onWrapperMaterialized()}}___resetLazy(newMaterializeFunc){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_RESETLAZY_CALLED",apiPath:this.____slothletInternal.apiPath,hadImpl:this.____slothletInternal.impl!==null,hadChildren:Object.keys(this).filter(k=>!k.startsWith("_")&&!k.startsWith("__")).length});for(const key of Reflect.ownKeys(this)){if(typeof key==="string"&&(key.startsWith("_")||key.startsWith("__"))){continue}const child=this[key];const childRaw=resolveWrapper(child);if(childRaw){childRaw.___invalidate()}const descriptor=Object.getOwnPropertyDescriptor(this,key);if(descriptor?.configurable){delete this[key]}}this.____slothletInternal.impl=null;this.____slothletInternal.invalid=false;this.____slothletInternal.state.materialized=false;this.____slothletInternal.state.inFlight=false;this.____slothletInternal.materializationPromise=null;this.____slothletInternal.materializeFunc=newMaterializeFunc;if(this.____slothletInternal.waitingProxyCache){this.____slothletInternal.waitingProxyCache.clear()}this.slothlet.debug("wrapper",{key:"DEBUG_MODE_RESETLAZY_COMPLETE",apiPath:this.____slothletInternal.apiPath})}async ___materialize(){if(this.____slothletInternal.state.materialized){return}if(this.____slothletInternal.invalid){return}if(this.____slothletInternal.materializationPromise){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_MATERIALIZE_AWAIT",apiPath:this.____slothletInternal.apiPath});return this.____slothletInternal.materializationPromise}if((wrapperDebugEnabled||this.____config?.debug?.wrapper)&&this.apiPath==="string"){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_MATERIALIZE_START",apiPath:this.apiPath})}this.____slothletInternal.materializationPromise=(async()=>{this.____slothletInternal.state.inFlight=true;try{if(this.____slothletInternal.materializeFunc){if((wrapperDebugEnabled||this.____config?.debug?.wrapper)&&this.____slothletInternal.apiPath==="string"){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_MATERIALIZE_CALLING_FUNC",apiPath:this.____slothletInternal.apiPath})}const lazy_setImpl=value=>{this._applyNewImpl(value)};const result=await this.____slothletInternal.materializeFunc(lazy_setImpl);if(!this.____slothletInternal.impl){this._applyNewImpl(result)}this.____slothletInternal.state.materialized=true;if(this.slothlet._onWrapperMaterialized){this.slothlet._onWrapperMaterialized()}if((wrapperDebugEnabled||this.____config?.debug?.wrapper)&&this.____slothletInternal.apiPath==="string"){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_MATERIALIZE_COMPLETE",apiPath:this.____slothletInternal.apiPath,resultType:typeof result,resultKeys:Object.keys(result||{})})}}}catch(error){if((wrapperDebugEnabled||this.____config?.debug?.wrapper)&&this.____slothletInternal.apiPath==="string"){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_MATERIALIZE_ERROR",apiPath:this.____slothletInternal.apiPath,error:error.message})}throw error}finally{this.____slothletInternal.state.inFlight=false;this.____slothletInternal.materializationPromise=null}})();return this.____slothletInternal.materializationPromise}_materialize(){return this.___materialize()}___invalidate(){this.____slothletInternal.invalid=true;this.____slothletInternal.impl=null;for(const key of Reflect.ownKeys(this)){if(typeof key==="string"&&(key.startsWith("_")||key.startsWith("__"))){continue}const descriptor=Object.getOwnPropertyDescriptor(this,key);if(descriptor?.configurable){delete this[key]}}}___adoptImplChildren(forceReuseChildren=false){const preExistingKeys=Object.keys(this).filter(k=>!k.startsWith("_")&&!k.startsWith("__"));this.slothlet.debug("wrapper",{key:"DEBUG_MODE_ADOPT_START",apiPath:this.____slothletInternal.apiPath,wrapperId:this.____slothletInternal.id||"no-id",preExistingKeys:preExistingKeys.join(","),collisionMode:this.____slothletInternal.state.collisionMode});if(!this.____slothletInternal.impl||typeof this.____slothletInternal.impl!=="object"&&typeof this.____slothletInternal.impl!=="function"){return}if(util.types.isProxy(this.____slothletInternal.impl))return;const ownKeys=Reflect.ownKeys(this.____slothletInternal.impl);const internalKeys=new Set(["__impl","___setImpl","___resetLazy","_materialize","_impl","_state","_invalid","____slothlet","____slothletInternal"]);const keepImplProperties=typeof this.____slothletInternal.impl==="function"||this.____slothletInternal.impl&&typeof this.____slothletInternal.impl==="object"&&typeof this.____slothletInternal.impl.default==="function";if(keepImplProperties&&this.____slothletInternal.impl&&typeof this.____slothletInternal.impl==="object"&&typeof this.____slothletInternal.impl.default==="function"){internalKeys.add("default")}const observedKeys=new Set;const existingKeys=Object.keys(this).filter(k=>!k.startsWith("_")&&!k.startsWith("__"));const storedCollisionMode=this.____slothletInternal.state.collisionMode;const isMergeScenario=storedCollisionMode!=="replace"&&existingKeys.length>0;this.slothlet.debug("wrapper",{key:"DEBUG_MODE_ADOPT",apiPath:this.____slothletInternal.apiPath,mode:this.____slothletInternal.mode,storedCollisionMode,existingKeys:existingKeys.join(","),isMergeScenario});const savedChildren=new Map;if(storedCollisionMode==="replace"&&existingKeys.length>0){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_ADOPT_REPLACE_CLEARING",count:existingKeys.length});for(const key of existingKeys){const child=this[key];if(resolveWrapper(child)!==null){savedChildren.set(key,child)}const descriptor=Object.getOwnPropertyDescriptor(this,key);if(descriptor?.configurable){delete this[key]}}}else{for(const key of existingKeys){observedKeys.add(key)}}const metadataKeys=new Set(["__childFilePaths","__filePath","__childFilePathsPreMaterialize"]);const skipKeys=typeof this.____slothletInternal.impl==="function"?new Set(["length","name","prototype"]):null;const builtinKeys=new Set(["slothlet","shutdown","destroy"]);for(const key of ownKeys){if(internalKeys.has(key)){continue}if(typeof key==="string"&&metadataKeys.has(key)){continue}if(typeof key==="string"&&builtinKeys.has(key)){continue}if(skipKeys&&typeof key==="string"&&skipKeys.has(key)){continue}if(!this.____slothletInternal.impl||typeof this.____slothletInternal.impl!=="object"&&typeof this.____slothletInternal.impl!=="function"){break}const descriptor=Object.getOwnPropertyDescriptor(this.____slothletInternal.impl,key);if(!descriptor){continue}const value=this.____slothletInternal.impl[key];if(value===this.____slothletInternal.impl){continue}if(typeof key!=="symbol"){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_ADOPT_PROCESS",apiPath:this.____slothletInternal.apiPath,propKey:key,typeOf:typeof value,valueName:value?.name})}observedKeys.add(key);if(hasOwn(this,key)&&!key.toString().startsWith("_")){if(typeof key!=="symbol"){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_ADOPT_CHECK",apiPath:this.____slothletInternal.apiPath,propKey:key,has__collisionMergedKeys:!!this.____slothletInternal.collisionMergedKeys,inSet:this.____slothletInternal.collisionMergedKeys?.has(key)})}const isCollisionMerged=this.____slothletInternal.collisionMergedKeys&&this.____slothletInternal.collisionMergedKeys.has(key);if(isCollisionMerged&&!forceReuseChildren){if(typeof key!=="symbol"){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_ADOPT_SKIP_COLLISION_MERGED",apiPath:this.____slothletInternal.apiPath,propKey:key})}if(descriptor.configurable){delete this.____slothletInternal.impl[key]}continue}if(typeof key!=="symbol"){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_ADOPT_ALLOW_NOT_COLLISION_MERGED",apiPath:this.____slothletInternal.apiPath,propKey:key})}}const existingChild=savedChildren.get(key)||this[key];let wrapped;const skipChildReuse=!forceReuseChildren&&this.____slothletInternal.mode==="lazy"&&storedCollisionMode==="replace";if(!skipChildReuse&&existingChild&&resolveWrapper(existingChild)!==null){if(resolveWrapper(value)!==null){const newWrapper=resolveWrapper(value);let rawImpl=newWrapper?newWrapper.____slothletInternal.impl:null;if(rawImpl&&typeof rawImpl==="object"&&!Array.isArray(rawImpl)&&typeof rawImpl!=="function"&&Object.keys(rawImpl).filter(k=>!k.startsWith("__")).length===0){const wrapperOwnKeys=Object.keys(newWrapper).filter(k=>!k.startsWith("_")&&!k.startsWith("__"));if(wrapperOwnKeys.length>0){rawImpl=UnifiedWrapper._extractFullImpl(newWrapper)}}if(rawImpl!==null&&rawImpl!==void 0){resolveWrapper(existingChild).___setImpl(rawImpl,this.slothlet,this.____slothletInternal.moduleID,this.____slothletInternal.filePath)}else if(newWrapper&&newWrapper.____slothletInternal.materializeFunc){const existingChildWrapper=resolveWrapper(existingChild);if(existingChildWrapper){existingChildWrapper.___resetLazy(newWrapper.____slothletInternal.materializeFunc)}}wrapped=existingChild}else{resolveWrapper(existingChild).___setImpl(value,this.slothlet,this.____slothletInternal.moduleID,this.____slothletInternal.filePath);wrapped=existingChild}if(typeof key!=="symbol"){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_ADOPT_REUSE_CHILD_WRAPPER",apiPath:this.____slothletInternal.apiPath,propKey:key})}}else{wrapped=this.___createChildWrapper(key,value);if(typeof key!=="symbol"){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_ADOPT_WRAP",apiPath:this.____slothletInternal.apiPath,propKey:key,wrapped:wrapped?"YES":wrapped===null?"NULL":"NO"})}}if(wrapped){const existing=this[key];const existingDescriptor=Object.getOwnPropertyDescriptor(this,key);if(existing===wrapped||existingDescriptor&&!existingDescriptor.configurable){if(typeof key!=="symbol"){this.slothlet.debug("wrapper",{key:existingDescriptor&&!existingDescriptor.configurable?"DEBUG_MODE_ADOPT_SKIP_NON_CONFIGURABLE":"DEBUG_MODE_ADOPT_SKIP_SAME_WRAPPER",apiPath:this.____slothletInternal.apiPath,propKey:key})}}else{if(typeof key!=="symbol"){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_ADOPT_DEFINE",apiPath:this.____slothletInternal.apiPath,propKey:key})}Object.defineProperty(this,key,{value:wrapped,writable:false,enumerable:true,configurable:true});if(typeof key!=="symbol"){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_ADOPT_DEFINED",apiPath:this.____slothletInternal.apiPath,propKey:key})}}if(descriptor.configurable&&!keepImplProperties&&this.____slothletInternal.impl){delete this.____slothletInternal.impl[key]}}else if(wrapped===null){Object.defineProperty(this,key,{value,writable:false,enumerable:true,configurable:true});if(descriptor.configurable&&!keepImplProperties&&this.____slothletInternal.impl){delete this.____slothletInternal.impl[key]}}else{Object.defineProperty(this,key,{value,writable:false,enumerable:true,configurable:true});if(descriptor.configurable&&!keepImplProperties&&this.____slothletInternal.impl){delete this.____slothletInternal.impl[key]}}}if(!isMergeScenario){const currentKeys=Object.keys(this).filter(k=>!k.startsWith("_")&&!k.startsWith("__"));for(const key of currentKeys){if(!observedKeys.has(key)){const existing=this[key];const existingRaw=resolveWrapper(existing);if(existingRaw){existingRaw.___invalidate()}const descriptor=Object.getOwnPropertyDescriptor(this,key);if(descriptor?.configurable){delete this[key]}}}}if(this._mergeAfterMaterialize){const{existingWrapper,isMergeReplace:____isMergeReplace}=this._mergeAfterMaterialize;const existingKeys2=Object.keys(existingWrapper).filter(k=>!k.startsWith("_")&&!k.startsWith("__"));for(const key of existingKeys2){if(!(key in this)||key.startsWith("_")||key.startsWith("__")){const child=existingWrapper[key];Object.defineProperty(this,key,{value:child,writable:false,enumerable:true,configurable:true})}}delete this._mergeAfterMaterialize}}___createChildWrapper(key,value){if(value===void 0){return void 0}if(value&&resolveWrapper(value)!==null){return value}if(value instanceof Map||value instanceof Set||value instanceof WeakMap||value instanceof WeakSet||value instanceof Date||value instanceof RegExp||value instanceof Promise||value instanceof Error||ArrayBuffer.isView(value)||value instanceof ArrayBuffer){return null}let childImpl=value;if(this.____slothletInternal.mode==="eager"&&childImpl&&typeof childImpl==="object"){if(Array.isArray(childImpl)){childImpl=childImpl.slice()}else{const descriptors=Object.getOwnPropertyDescriptors(childImpl);childImpl=Object.create(Object.getPrototypeOf(childImpl),descriptors)}}const parentMetadata=this.slothlet.handlers?.metadata?.getMetadata(this);const childExistingMetadata=this.slothlet.handlers?.metadata?.getMetadata(value);let childFilePath=childExistingMetadata?.filePath||null;let childModuleId=null;if(!childFilePath){const keyStr=typeof key==="symbol"?String(key):key;this.slothlet.debug("wrapper",{key:"DEBUG_MODE_WRAP_CHILD_PATH_CHECK",apiPath:this.apiPath,propKey:keyStr,has_impl:!!this.____slothletInternal.impl,has__childFilePaths:!!(this.____slothletInternal.impl&&this.____slothletInternal.impl.__childFilePaths),has__childFilePathsPreMaterialize:!!this.____slothletInternal.childFilePathsPreMaterialize,parentFilePath:parentMetadata?.filePath});if(this.____slothletInternal.impl&&this.____slothletInternal.impl.__childFilePaths){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_WRAP_CHILD_PATH_AVAILABLE",keys:Object.keys(this.____slothletInternal.impl.__childFilePaths).join(","),propKey:keyStr,found:!!this.____slothletInternal.impl.__childFilePaths[key]})}if(this.____slothletInternal.impl&&this.____slothletInternal.impl.__childFilePaths&&this.____slothletInternal.impl.__childFilePaths[key]){childFilePath=this.____slothletInternal.impl.__childFilePaths[key];this.slothlet.debug("wrapper",{key:"DEBUG_MODE_WRAP_CHILD_PATH_USING",childFilePath})}else if(this.____slothletInternal.childFilePathsPreMaterialize&&this.____slothletInternal.childFilePathsPreMaterialize[key]){childFilePath=this.____slothletInternal.childFilePathsPreMaterialize[key];this.slothlet.debug("wrapper",{key:"DEBUG_MODE_WRAP_CHILD_PATH_PRE_MAT",childFilePath})}else{childFilePath=parentMetadata?.filePath||null;this.slothlet.debug("wrapper",{key:"DEBUG_MODE_WRAP_CHILD_PATH_FALLBACK",childFilePath})}if(parentMetadata?.moduleID){const colonIndex=parentMetadata.moduleID.indexOf(":");childModuleId=colonIndex>0?parentMetadata.moduleID.substring(0,colonIndex):parentMetadata.moduleID}}else{if(childExistingMetadata?.moduleID){const colonIndex=childExistingMetadata.moduleID.indexOf(":");childModuleId=colonIndex>0?childExistingMetadata.moduleID.substring(0,colonIndex):childExistingMetadata.moduleID}}const childSourceFolder=childExistingMetadata?.sourceFolder||parentMetadata?.sourceFolder||null;const nestedWrapper=new UnifiedWrapper(this.slothlet,{mode:"eager",apiPath:this.____slothletInternal.apiPath?`${this.____slothletInternal.apiPath}.${typeof key==="symbol"?String(key):key}`:String(key),initialImpl:childImpl,isCallable:typeof childImpl==="function",filePath:childFilePath,moduleID:childModuleId,sourceFolder:childSourceFolder});return nestedWrapper.createProxy()}___createWaitingProxy(propChain=[]){const wrapper=this;const cacheKey=propChain.join(".");if(!wrapper.____slothletInternal.waitingProxyCache){wrapper.____slothletInternal.waitingProxyCache=new Map}if(wrapper.____slothletInternal.waitingProxyCache.has(cacheKey)){return wrapper.____slothletInternal.waitingProxyCache.get(cacheKey)}const waitingTarget=createNamedProxyTarget(`${wrapper.____slothletInternal.apiPath}_waitingProxy`,"waitingProxyTarget");const waitingProxy=new Proxy(waitingTarget,{get(___target,prop){if(prop==="then"){return(onFulfilled,onRejected)=>{const waitingProxy_thenResolve=async()=>{if(!wrapper.____slothletInternal.state.materialized){await wrapper._materialize()}let current=wrapper;for(const chainProp of propChain){if(!current)return void 0;if(current.____slothletInternal.impl&&typeof current.____slothletInternal.impl==="object"&&util.types.isProxy(current.____slothletInternal.impl)){let result=current.____slothletInternal.impl;const idx=propChain.indexOf(chainProp);for(let i=idx;i<propChain.length;i++){result=result[propChain[i]]}return result}const isInternal2=typeof chainProp==="string"&&(chainProp.startsWith("_")||chainProp.startsWith("__"));if(!isInternal2&&hasOwn(current,chainProp)){const child=current[chainProp];const _childW=resolveWrapper(child);if(_childW){current=_childW;if(current.____slothletInternal.mode==="lazy"&&!current.____slothletInternal.state.materialized){await current._materialize()}continue}return child}if(current.____slothletInternal.impl&&current.____slothletInternal.impl[chainProp]!==void 0){return current.____slothletInternal.impl[chainProp]}return void 0}if(current.____slothletInternal.impl&&typeof current.____slothletInternal.impl==="object"&&util.types.isProxy(current.____slothletInternal.impl)){return current.____slothletInternal.impl}return current.____slothletInternal.impl};waitingProxy_thenResolve().then(onFulfilled,onRejected)}}if(prop==="____slothletInternal")return void 0;if(prop==="_materialize")return wrapper._materialize.bind(wrapper);if(prop==="__mode")return wrapper.____slothletInternal.mode;if(prop==="__materialized")return wrapper.____slothletInternal.state.materialized;if(prop==="__inFlight")return wrapper.____slothletInternal.state.inFlight;if(wrapper.____slothletInternal.mode==="lazy"&&!wrapper.____slothletInternal.state.materialized&&!wrapper.____slothletInternal.state.inFlight){wrapper._materialize().catch(()=>{})}if(prop===util.inspect.custom){if(wrapper.____slothletInternal.state.inFlight){return waitingTarget}if(!wrapper.____slothletInternal.state.materialized){return waitingTarget}if(wrapper.____slothletInternal.impl){let current=wrapper.____slothletInternal.impl;for(const chainProp of propChain){if(!current||current===null){return void 0}current=current[chainProp]}return current}return waitingTarget}if(prop==="__type"){wrapper.slothlet.debug("wrapper",{key:"DEBUG_MODE_WAITING_TYPE",apiPath:wrapper.apiPath,propChain:propChain.join(","),materialized:wrapper.____slothletInternal.state.materialized,hasImpl:wrapper.____slothletInternal.impl!==null});if(wrapper.____slothletInternal.state.materialized||wrapper.____slothletInternal.impl!==null&&wrapper.____slothletInternal.impl!==void 0){let current=wrapper.createProxy();for(const chainProp of propChain){if(!current)break;const currentWrapper=resolveWrapper(current);if(current&&currentWrapper){const isInternal2=typeof chainProp==="string"&&(chainProp.startsWith("_")||chainProp.startsWith("__"));if(!isInternal2&&chainProp in currentWrapper){current=currentWrapper[chainProp];wrapper.slothlet.debug("wrapper",{key:"DEBUG_MODE_WAITING_TYPE_WALK_WRAPPER",chainProp:String(chainProp),typeOf:typeof current});continue}if(currentWrapper.____slothletInternal.impl&&typeof currentWrapper.____slothletInternal.impl==="object"&&currentWrapper.____slothletInternal.impl!==null&&chainProp in currentWrapper.____slothletInternal.impl){current=currentWrapper.____slothletInternal.impl[chainProp];wrapper.slothlet.debug("wrapper",{key:"DEBUG_MODE_WAITING_TYPE_WALK_IMPL",chainProp:String(chainProp),typeOf:typeof current});continue}}wrapper.slothlet.debug("wrapper",{key:"DEBUG_MODE_WAITING_TYPE_WALK_DIRECT",chainProp:String(chainProp),typeOf:typeof current});current=current[chainProp]}const resolvedType=typeof current==="function"?"function":typeof current==="object"&&current!==null?"object":typeof current;wrapper.slothlet.debug("wrapper",{key:"DEBUG_MODE_WAITING_TYPE_RESOLVED",apiPath:wrapper.apiPath,propChain:propChain.join(","),resolvedType});return resolvedType}wrapper.slothlet.debug("wrapper",{key:"DEBUG_MODE_WAITING_TYPE_INFLIGHT",apiPath:wrapper.apiPath,propChain:propChain.join(",")});return TYPE_STATES.IN_FLIGHT}if(prop==="__metadata"){if(wrapper.slothlet.handlers?.metadata){return wrapper.slothlet.handlers.metadata.getMetadata(wrapper)}return{}}if(typeof prop==="symbol")return void 0;if(prop==="length"){return 0}if(prop==="name")return waitingTarget.name||"waitingProxyTarget";if(prop==="toString"){return Function.prototype.toString.bind(waitingTarget)}if(prop==="valueOf"){return Function.prototype.valueOf.bind(waitingTarget)}if(prop==="toJSON"){return()=>void 0}if(prop==="__slothletPath")return wrapper.____slothletInternal.apiPath;if(wrapper.____slothletInternal.impl!==null&&wrapper.____slothletInternal.impl!==void 0&&typeof wrapper.____slothletInternal.impl==="object"&&util.types.isProxy(wrapper.____slothletInternal.impl)){let result=wrapper.____slothletInternal.impl;for(const chainProp of propChain){result=result[chainProp]}return result[prop]}if(wrapper.____slothletInternal.impl!==null&&wrapper.____slothletInternal.impl!==void 0){let current=wrapper;let remainingChain=[...propChain];for(let i=0;i<propChain.length;i++){const chainProp=propChain[i];if(current.____slothletInternal.impl&&typeof current.____slothletInternal.impl==="object"&&util.types.isProxy(current.____slothletInternal.impl)){let proxyResult=current.____slothletInternal.impl;for(const remainingProp of remainingChain){proxyResult=proxyResult[remainingProp]}return proxyResult[prop]}const isInternal2=typeof chainProp==="string"&&(chainProp.startsWith("_")||chainProp.startsWith("__"));if(!isInternal2&&current&&chainProp in current){const cached=current[chainProp];const _cachedW2=resolveWrapper(cached);if(_cachedW2){current=_cachedW2;remainingChain.shift()}else{return void 0}}else{return void 0}}if(current&&current.____slothletInternal.impl&&typeof current.____slothletInternal.impl==="object"&&util.types.isProxy(current.____slothletInternal.impl)){return current.____slothletInternal.impl[prop]}const isFinalInternal=typeof prop==="string"&&(prop.startsWith("_")||prop.startsWith("__"));if(!isFinalInternal&&current&&prop in current){return current[prop]}return void 0}const isInternal=typeof prop==="string"&&(prop.startsWith("_")||prop.startsWith("__"));if(!isInternal&&hasOwn(wrapper,prop)){wrapper.slothlet.debug("wrapper",{key:"DEBUG_MODE_WAITING_GET_PREMATURE",apiPath:wrapper.____slothletInternal.apiPath,prop});return wrapper[prop]}if(wrapper.____slothletInternal.needsImmediateChildAdoption&&wrapper.____slothletInternal.materializeFunc&&!wrapper.____slothletInternal.state.materialized&&!wrapper.____slothletInternal.state.inFlight){wrapper.slothlet.debug("wrapper",{key:"DEBUG_MODE_WAITING_GET_IMMEDIATE_MAT",apiPath:wrapper.____slothletInternal.apiPath,prop});wrapper._materialize().catch(err=>{wrapper.slothlet.debug("wrapper",{key:"DEBUG_MODE_WAITING_GET_IMMEDIATE_MAT_ERROR",apiPath:wrapper.apiPath,error:err.message})});const isInternal2=typeof prop==="string"&&(prop.startsWith("_")||prop.startsWith("__"));if(!isInternal2&&hasOwn(wrapper,prop)){wrapper.slothlet.debug("wrapper",{key:"DEBUG_MODE_WAITING_GET_IMMEDIATE_MAT_SUCCESS",apiPath:wrapper.____slothletInternal.apiPath,prop});return wrapper[prop]}}if(wrapper.____slothletInternal.state.inFlight){return wrapper.___createWaitingProxy([...propChain,prop])}return wrapper.___createWaitingProxy([...propChain,prop])},async apply(___target,___thisArg,args){const ___capturedCallerWrapper=wrapper.slothlet.contextManager?.tryGetContext?.()?.currentWrapper??null;wrapper.slothlet.debug("wrapper",{key:"DEBUG_MODE_WAITING_APPLY_ENTRY",apiPath:wrapper.____slothletInternal.apiPath,propChain:propChain.join(","),args:args.join(",")});const chainLabel=propChain.map(prop=>String(prop)).join(".");if(wrapper.____slothletInternal.mode==="lazy"&&!wrapper.____slothletInternal.state.materialized&&!wrapper.____slothletInternal.state.inFlight){wrapper.slothlet.debug("wrapper",{key:"DEBUG_MODE_WAITING_APPLY_MATERIALIZE",apiPath:wrapper.____slothletInternal.apiPath});await wrapper._materialize();wrapper.slothlet.debug("wrapper",{key:"DEBUG_MODE_WAITING_APPLY_MATERIALIZED",apiPath:wrapper.____slothletInternal.apiPath})}else if(wrapper.____slothletInternal.mode==="lazy"&&!wrapper.____slothletInternal.state.materialized&&wrapper.____slothletInternal.state.inFlight){if(wrapper.____slothletInternal.materializationPromise){await wrapper.____slothletInternal.materializationPromise}else{await wrapper._materialize()}}wrapper.slothlet.debug("wrapper",{key:"DEBUG_MODE_WAITING_APPLY_START_WALK",apiPath:wrapper.____slothletInternal.apiPath,propChain:propChain.join(",")});let current=wrapper.createProxy();let lastWrapper=wrapper;let lastObject=null;for(const prop of propChain){wrapper.slothlet.debug("wrapper",{key:"DEBUG_MODE_WAITING_APPLY_WALK",prop:String(prop),typeOf:typeof current,constructorName:current?.constructor?.name});if(!current){if(propChain.some(p=>typeof p==="symbol")){return void 0}if(wrapper.____slothletInternal.invalid||wrapper.____slothletInternal.impl===null||lastWrapper&&(lastWrapper.__invalid||lastWrapper._impl===null)){return void 0}const finalProp=propChain[propChain.length-1];if(finalProp==="hasAttribute"||finalProp==="toJSON"||finalProp===Symbol.toStringTag||finalProp==="constructor"||typeof finalProp==="symbol"||typeof prop==="symbol"){return void 0}throw new wrapper.slothlet.SlothletError("CHAIN_ACCESS_UNDEFINED",{apiPath:wrapper.____slothletInternal.apiPath,chainLabel,prop:String(prop)},null,{validationError:true})}const currentWrapper=resolveWrapper(current);if(currentWrapper){const state=currentWrapper.____slothletInternal.state;if(!state.materialized){if(!state.inFlight&&typeof current._materialize==="function"){await current._materialize()}while(!currentWrapper.____slothletInternal.state.materialized){const nextState=currentWrapper.____slothletInternal.state;if(!nextState.inFlight&&!nextState.materialized){throw new wrapper.slothlet.SlothletError("CHAIN_MATERIALIZE_FAILED",{apiPath:wrapper.____slothletInternal.apiPath,chainLabel,prop:String(prop)},null,{validationError:true})}await new Promise(resolve=>setImmediate(resolve))}}}if(current&&currentWrapper){lastWrapper=currentWrapper;const isInternal=typeof prop==="string"&&(prop.startsWith("_")||prop.startsWith("__"));if(!isInternal&&prop in currentWrapper){lastObject=current;current=currentWrapper[prop];continue}if(currentWrapper.____slothletInternal.impl&&typeof currentWrapper.____slothletInternal.impl==="object"&&currentWrapper.____slothletInternal.impl!==null&&prop in currentWrapper.____slothletInternal.impl){lastObject=currentWrapper.____slothletInternal.impl;current=currentWrapper.____slothletInternal.impl[prop];continue}}lastObject=current;current=current[prop]}wrapper.slothlet.debug("wrapper",{key:"DEBUG_MODE_WAITING_APPLY",apiPath:wrapper.____slothletInternal.apiPath,propChain:propChain.join(","),typeOf:typeof current,currentName:current?.name,isFunction:typeof current==="function"});if(typeof current==="function"){if(___capturedCallerWrapper&&wrapper.slothlet.contextManager){const ___instanceStore=wrapper.slothlet.contextManager.instances?.get?.(wrapper.instanceID);if(___instanceStore&&!___instanceStore.currentWrapper){return wrapper.slothlet.contextManager.runInContext(wrapper.instanceID,()=>Reflect.apply(current,lastObject,args),null,[],___capturedCallerWrapper)}}return Reflect.apply(current,lastObject,args)}const _finalChainProp=propChain[propChain.length-1];if(_finalChainProp==="hasAttribute"||_finalChainProp==="toJSON"){return void 0}if(current===void 0||current===null){throw new wrapper.slothlet.SlothletError("CHAIN_NOT_CALLABLE",{apiPath:wrapper.____slothletInternal.apiPath,chainLabel},null,{validationError:true})}throw new wrapper.slothlet.SlothletError("CHAIN_NOT_CALLABLE",{apiPath:wrapper.____slothletInternal.apiPath,chainLabel},null,{validationError:true})},getPrototypeOf:()=>null});_proxyRegistry.set(waitingProxy,wrapper);wrapper.____slothletInternal.waitingProxyCache.set(cacheKey,waitingProxy);return waitingProxy}createProxy(){const wrapper=this;if(wrapper.____slothletInternal.proxy){return wrapper.____slothletInternal.proxy}if(wrapper.____slothletInternal.materializeOnCreate&&wrapper.____slothletInternal.mode==="lazy"&&!wrapper.____slothletInternal.state.materialized&&wrapper.____slothletInternal.materializeFunc){wrapper._materialize().catch(()=>{})}const mightBeCallable=wrapper.____slothletInternal.isCallable||wrapper.____slothletInternal.mode==="lazy"&&!wrapper.____slothletInternal.isCallableLocked;let proxyTarget;if(mightBeCallable){proxyTarget=createNamedProxyTarget(wrapper.____slothletInternal.apiPath,"callableProxy")}else{proxyTarget=wrapper}if(!(util.inspect.custom in proxyTarget)){Object.defineProperty(proxyTarget,util.inspect.custom,{value:function(){const childKeys=Object.keys(wrapper).filter(k=>!k.startsWith("_")&&!k.startsWith("__"));if(childKeys.length>0&&!wrapper.____slothletInternal.isCallable){const obj={};for(const key of childKeys){const child=wrapper[key];if(child&&typeof child.createProxy==="function"){obj[key]=child.createProxy()}else{obj[key]=child}}return obj}if(wrapper.____slothletInternal.mode==="lazy"&&!wrapper.____slothletInternal.state.materialized&&(wrapper.____slothletInternal.impl===null||wrapper.____slothletInternal.impl===void 0)){return wrapper.____slothletInternal.proxy||wrapper}return wrapper.____slothletInternal.impl},writable:false,enumerable:false,configurable:true})}const getTrap=(target,prop,____receiver)=>{if(target!==wrapper&&prop in target){const desc=Object.getOwnPropertyDescriptor(target,prop);if(desc&&!desc.configurable){if(typeof prop==="string"&&prop.startsWith("__")){return wrapper[prop]}return target[prop]}}if(target===wrapper&&typeof prop==="string"){const desc=Object.getOwnPropertyDescriptor(target,prop);if(desc&&!desc.configurable){return target[prop]}}const isInternalProp=typeof prop==="string"&&UnifiedWrapper.INTERNAL_KEYS.has(prop);const allowedInternals=new Set(["_materialize","__mode","__apiPath","__isCallable","__materializeOnCreate","__displayName","__type","__materialized","__inFlight","__slothletPath","__metadata","__filePath","__sourceFolder","__moduleID"]);if(isInternalProp&&!allowedInternals.has(prop)){return void 0}if(prop==="power"||prop==="add"){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_GET_START",apiPath:wrapper.____slothletInternal.apiPath,prop:String(prop),mode:wrapper.____slothletInternal.mode,collisionMode:wrapper.____slothletInternal.state.collisionMode||"none",materialized:wrapper.____slothletInternal.state.materialized,hasImpl:wrapper.____slothletInternal.impl!==null,inWrapper:!isInternalProp&&hasOwn(wrapper,prop)})}if(prop==="__mode")return wrapper.____slothletInternal.mode;if(prop==="__apiPath")return wrapper.____slothletInternal.apiPath;if(prop==="__isCallable")return wrapper.____slothletInternal.isCallable;if(prop==="__materializeOnCreate")return wrapper.____slothletInternal.materializeOnCreate;if(prop==="__materialized")return wrapper.____slothletInternal.state.materialized;if(prop==="__inFlight")return wrapper.____slothletInternal.state.inFlight;if(prop==="__displayName")return wrapper.____slothletInternal.displayName;if(prop==="__type"){if(wrapper.____slothletInternal.mode==="lazy"&&!wrapper.____slothletInternal.state.materialized&&!wrapper.____slothletInternal.state.inFlight){wrapper._materialize().catch(()=>{})}if(wrapper.____slothletInternal.mode==="lazy"&&wrapper.____slothletInternal.state.inFlight){return TYPE_STATES.IN_FLIGHT}if(wrapper.____slothletInternal.mode==="lazy"&&!wrapper.____slothletInternal.state.materialized){return TYPE_STATES.UNMATERIALIZED}const impl=wrapper.____slothletInternal.impl;if(typeof impl==="function"){return"function"}if(impl&&typeof impl==="object"&&typeof impl.default==="function"){return"function"}if(impl&&typeof impl==="object"){return"object"}if(typeof impl==="string"){return"string"}if(typeof impl==="number"){return"number"}if(typeof impl==="boolean"){return"boolean"}if(typeof impl==="symbol"){return"symbol"}if(typeof impl==="bigint"){return"bigint"}return"undefined"}if(prop==="_materialize")return wrapper._materialize.bind(wrapper);if(prop==="__slothletPath")return wrapper.____slothletInternal.apiPath;if(prop==="__metadata"){if(wrapper.slothlet.handlers?.metadata){return wrapper.slothlet.handlers.metadata.getMetadata(wrapper)}return{}}if(prop==="__filePath")return wrapper.____slothletInternal.filePath??void 0;if(prop==="__sourceFolder")return wrapper.____slothletInternal.sourceFolder??void 0;if(prop==="__moduleID")return wrapper.____slothletInternal.moduleID??void 0;if(prop==="__invalid")return wrapper.____slothletInternal.invalid;if(prop==="then")return void 0;if(prop==="constructor")return Object.prototype.constructor;if(prop===util.inspect.custom){return()=>{const childKeys=Object.keys(wrapper).filter(k=>!k.startsWith("_")&&!k.startsWith("__"));if(childKeys.length>0&&!wrapper.____slothletInternal.isCallable){const obj={};for(const key of childKeys){const child=wrapper[key];if(child&&typeof child.createProxy==="function"){obj[key]=child.createProxy()}else{obj[key]=child}}return obj}if(wrapper.____slothletInternal.mode==="lazy"&&!wrapper.____slothletInternal.state.materialized&&(wrapper.____slothletInternal.impl===null||wrapper.____slothletInternal.impl===void 0)){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_INSPECT_LAZY_UNMATERIALIZED",apiPath:wrapper.apiPath,wrapper,____proxy:wrapper.____slothletInternal.proxy});return wrapper||wrapper.____slothletInternal.proxy}return wrapper.____slothletInternal.impl}}if(prop===Symbol.toStringTag){const impl=wrapper.____slothletInternal.impl;if(typeof impl==="function"){return"Function"}if(impl&&typeof impl==="object"&&typeof impl.default==="function"){return"Function"}return"Object"}if(typeof prop==="symbol")return void 0;if(prop==="length"){const impl=wrapper.____slothletInternal.impl;if(typeof impl==="function"){return impl.length}if(impl&&typeof impl==="object"&&typeof impl.default==="function"){return impl.default.length}return 0}if(prop==="name"){if(wrapper.____slothletInternal.apiPath){const pathParts=wrapper.____slothletInternal.apiPath.split(".");const lastPart=pathParts[pathParts.length-1];if(lastPart){return lastPart}}return target.name||"unifiedWrapperProxy"}if(prop==="toString"){const impl=wrapper.____slothletInternal.impl;if(typeof impl==="function"){return impl.toString.bind(impl)}if(impl&&typeof impl==="object"&&typeof impl.default==="function"){return impl.default.toString.bind(impl.default)}if(typeof target==="function"){return Function.prototype.toString.bind(target)}return()=>`[UnifiedWrapper: ${wrapper.____slothletInternal.apiPath}]`}if(prop==="valueOf"){const impl=wrapper.____slothletInternal.impl;if(typeof impl==="function"){return impl.valueOf.bind(impl)}if(impl&&typeof impl==="object"&&typeof impl.default==="function"){return impl.default.valueOf.bind(impl.default)}return Function.prototype.valueOf.bind(target)}if(prop==="toJSON"){const impl=wrapper.____slothletInternal.impl;if(impl&&typeof impl.toJSON==="function"){return impl.toJSON.bind(impl)}return()=>void 0}if(wrapper.____slothletInternal.invalid){return void 0}if(wrapper.____slothletInternal.mode==="lazy"&&!wrapper.____slothletInternal.state.materialized&&!wrapper.____slothletInternal.state.inFlight){wrapper._materialize().catch(()=>{})}const isInternal=typeof prop==="string"&&(prop.startsWith("_")||prop.startsWith("__"));if(!isInternal&&hasOwn(wrapper,prop)){if(wrapper.____slothletInternal.state.collisionMode==="replace"&&(prop==="power"||prop==="add")){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_GET_CACHED_REPLACE",apiPath:wrapper.apiPath,prop:String(prop),collisionMode:wrapper.____slothletInternal.state.collisionMode,wrapperKeys:Object.keys(wrapper).filter(k=>!k.startsWith("_")&&!k.startsWith("__")).join(", ")})}this.slothlet.debug("wrapper",{key:"DEBUG_MODE_GET_CACHED",apiPath:wrapper.____slothletInternal.apiPath,prop:String(prop),materialized:wrapper.____slothletInternal.state.materialized,hasImpl:wrapper.____slothletInternal.impl!==null});const cached=wrapper[prop];const _cachedWrapper=resolveWrapper(cached);if(_cachedWrapper?.____slothletInternal?.impl!==null&&_cachedWrapper?.____slothletInternal?.impl!==void 0){const cachedImpl=_cachedWrapper.____slothletInternal.impl;const cachedType=typeof cachedImpl;if(cachedType==="string"||cachedType==="number"||cachedType==="boolean"||cachedType==="bigint"||cachedType==="symbol"){return cachedImpl}}if(_cachedWrapper){const cachedWrapper=_cachedWrapper;if(cachedWrapper.____slothletInternal.mode==="lazy"&&!cachedWrapper.____slothletInternal.state.materialized&&!cachedWrapper.____slothletInternal.state.inFlight){cachedWrapper._materialize().catch(()=>{})}}return cached}if(wrapper.____slothletInternal.impl!==null&&wrapper.____slothletInternal.impl!==void 0){if(typeof wrapper.____slothletInternal.impl==="object"&&util.types.isProxy(wrapper.____slothletInternal.impl)){return wrapper.____slothletInternal.impl[prop]}const isInternal2=typeof prop==="string"&&(prop.startsWith("_")||prop.startsWith("__"));if(!isInternal2&&hasOwn(wrapper,prop)){if(prop==="power"||prop==="add"){this.slothlet.debug("wrapper",{key:"DEBUG_MODE_GET_PROXYGET_ACCESSING",prop,wrapperId:wrapper.____slothletInternal.id,apiPath:wrapper.apiPath,collisionMode:wrapper.____slothletInternal.state.collisionMode||"none"});this.slothlet.debug("wrapper",{key:"DEBUG_MODE_GET_PROXYGET_FOUND",wrapperKeys:Object.keys(wrapper).filter(k=>!k.startsWith("_")&&!k.startsWith("__")).join(", ")})}const cached=wrapper[prop];const _cachedWrapper4=resolveWrapper(cached);if(_cachedWrapper4){const cachedWrapper=_cachedWrapper4;if(cachedWrapper.____slothletInternal.mode==="lazy"&&!cachedWrapper.____slothletInternal.state.materialized&&!cachedWrapper.____slothletInternal.state.inFlight){cachedWrapper._materialize().catch(()=>{})}}return cached}}const isInternalProp2=typeof prop==="string"&&UnifiedWrapper.INTERNAL_KEYS.has(prop);if(wrapper.____slothletInternal.mode==="lazy"&&wrapper.____slothletInternal.state.materialized&&!isInternalProp2&&!hasOwn(wrapper,prop)&&wrapper.____slothletInternal.impl&&!(prop in wrapper.____slothletInternal.impl)){return void 0}if(wrapper.____slothletInternal.mode==="lazy"&&(wrapper.____slothletInternal.state.inFlight||!wrapper.____slothletInternal.impl)){if(!wrapper.____slothletInternal.state.materialized&&!wrapper.____slothletInternal.state.inFlight){wrapper._materialize().catch(()=>{})}this.slothlet.debug("wrapper",{key:"DEBUG_MODE_LAZY_GET_CREATE_WAITING_PROXY",prop:String(prop),collisionMode:wrapper.____slothletInternal.state.collisionMode||"none",apiPath:wrapper.____slothletInternal.apiPath});if(wrapper.____slothletInternal.impl&&typeof wrapper.____slothletInternal.impl==="object"&&util.types.isProxy(wrapper.____slothletInternal.impl)){const value2=wrapper.____slothletInternal.impl[prop];return value2}return wrapper.___createWaitingProxy([prop])}if(wrapper.____slothletInternal.impl&&typeof wrapper.____slothletInternal.impl==="object"&&util.types.isProxy(wrapper.____slothletInternal.impl)){const value2=wrapper.____slothletInternal.impl[prop];return value2}let value=wrapper.____slothletInternal.impl?wrapper.____slothletInternal.impl[prop]:void 0;if(value===void 0&&wrapper.____slothletInternal.impl){const descriptor=Object.getOwnPropertyDescriptor(wrapper.____slothletInternal.impl,prop);if(descriptor&&descriptor.get){value=descriptor.get.call(wrapper.____slothletInternal.impl)}}if(value===void 0&&Object.prototype.hasOwnProperty.call(target,prop)){value=target[prop]}if(value===void 0){return void 0}const valueType=typeof value;if(value===null||valueType==="string"||valueType==="number"||valueType==="boolean"||valueType==="bigint"||valueType==="symbol"){return value}if(value instanceof Map||value instanceof Set||value instanceof WeakMap||value instanceof WeakSet||value instanceof Date||value instanceof RegExp||value instanceof Promise||value instanceof Error||ArrayBuffer.isView(value)||value instanceof ArrayBuffer){return value}if(value&&typeof value==="object"&&util.types.isProxy(value)){return value}if(value&&(typeof value==="object"||typeof value==="function")&&resolveWrapper(value)!==null){return value}const wrapped=wrapper.___createChildWrapper(prop,value);if(wrapped){Object.defineProperty(wrapper,prop,{value:wrapped,writable:false,enumerable:true,configurable:true});return wrapped}return value};const applyTrap=(target,thisArg,args)=>{if(wrapper.____slothletInternal.invalid){throw new TypeError(`${wrapper.____slothletInternal.apiPath||"api"} is invalidated`)}const permissionManager=wrapper.slothlet.handlers?.permissionManager;if(permissionManager&&permissionManager.isEnabled()){const ctx2=wrapper.slothlet.contextManager?.tryGetContext?.();const callerWrapper=ctx2?.currentWrapper;if(callerWrapper){const callerPath=callerWrapper.____slothletInternal?.apiPath??"";const callerFilePath=callerWrapper.____slothletInternal?.filePath??null;const targetPath=wrapper.____slothletInternal.apiPath;const targetFilePath=wrapper.____slothletInternal.filePath??null;if(!permissionManager.checkAccess(callerPath,targetPath,callerFilePath,targetFilePath)){throw new wrapper.SlothletError("PERMISSION_DENIED",{caller:callerPath,target:targetPath})}}}const hookManager=wrapper.slothlet.handlers?.hookManager;const hasHooks=hookManager&&hookManager.enabled&&!wrapper.____slothletInternal.apiPath.startsWith("slothlet.hook");const api=wrapper.slothlet.boundApi;const ctx=wrapper.slothlet.config?.context||{};let result;let finalResult;let isAsync=false;try{if(hasHooks){const beforeResult=hookManager.executeBeforeHooks(wrapper.____slothletInternal.apiPath,args,api,ctx);args=beforeResult.args;if(beforeResult.shortCircuit){finalResult=beforeResult.value;return beforeResult.value}}if(wrapper.____slothletInternal.mode==="lazy"&&!wrapper.____slothletInternal.state.materialized&&!wrapper.____slothletInternal.state.inFlight){wrapper._materialize().catch(()=>{})}if(wrapper.____slothletInternal.mode==="lazy"&&wrapper.____slothletInternal.state.inFlight){return new Promise((resolve,reject)=>{const checkMaterialized=()=>{if(wrapper.____slothletInternal.state.materialized){const impl2=wrapper.____slothletInternal.impl;try{if(typeof impl2==="function"){if(wrapper.slothlet.contextManager){resolve(wrapper.slothlet.contextManager.runInContext(wrapper.instanceID,impl2,thisArg,args,wrapper))}else{resolve(impl2.apply(thisArg,args))}}else if(impl2&&typeof impl2==="object"&&typeof impl2.default==="function"){if(wrapper.contextManager){resolve(wrapper.contextManager.runInContext(wrapper.instanceID,impl2.default,impl2,args,wrapper))}else{resolve(impl2.default.apply(impl2,args))}}else{reject(new wrapper.slothlet.SlothletError("INVALID_CONFIG_NOT_A_FUNCTION",{apiPath:wrapper.____slothletInternal.apiPath,actualType:typeof impl2},null,{validationError:true}))}}catch(err){reject(err)}return}if(!wrapper.____slothletInternal.state.inFlight){reject(new wrapper.slothlet.SlothletError("INVALID_CONFIG_LAZY_MATERIALIZATION_FAILED",{apiPath:wrapper.____slothletInternal.apiPath},null,{validationError:true}));return}setImmediate(checkMaterialized)};checkMaterialized()})}const impl=wrapper.____slothletInternal.impl;if(typeof impl==="function"){if(wrapper.slothlet.contextManager){result=wrapper.slothlet.contextManager.runInContext(wrapper.instanceID,impl,thisArg,args,wrapper)}else{result=impl.apply(thisArg,args)}}else if(impl&&typeof impl==="object"&&typeof impl.default==="function"){if(wrapper.slothlet.contextManager){result=wrapper.slothlet.contextManager.runInContext(wrapper.instanceID,impl.default,impl,args,wrapper)}else{result=impl.default.apply(impl,args)}}else{throw new wrapper.SlothletError("INVALID_CONFIG_NOT_A_FUNCTION",{apiPath:wrapper.____slothletInternal.apiPath,actualType:typeof impl},null,{validationError:true})}if(result&&typeof result==="object"&&typeof result.then==="function"){isAsync=true;return result.then(resolvedResult=>{try{if(hasHooks){const afterResult=hookManager.executeAfterHooks(wrapper.____slothletInternal.apiPath,resolvedResult,args,api,ctx);const finalResult2=afterResult.modified?afterResult.result:resolvedResult;hookManager.executeAlwaysHooks(wrapper.____slothletInternal.apiPath,args,finalResult2,false,[],api,ctx);return finalResult2}return resolvedResult}catch(error){if(hasHooks){const originalError=unwrapError(error);const sourceInfo={type:"after",timestamp:Date.now(),stack:originalError.stack};hookManager.executeErrorHooks(wrapper.____slothletInternal.apiPath,originalError,sourceInfo,args,api,ctx);hookManager.executeAlwaysHooks(wrapper.____slothletInternal.apiPath,args,void 0,true,[originalError],api,ctx)}const suppressErrors=wrapper.slothlet.config?.hook?.suppressErrors===true;if(suppressErrors){return void 0}throw error}},error=>{if(hasHooks&&!error[ERROR_HOOK_PROCESSED]){const originalError=unwrapError(error);const sourceInfo={type:"function",timestamp:Date.now(),stack:originalError.stack};hookManager.executeErrorHooks(wrapper.____slothletInternal.apiPath,originalError,sourceInfo,args,api,ctx)}if(hasHooks){const originalError=unwrapError(error);hookManager.executeAlwaysHooks(wrapper.____slothletInternal.apiPath,args,void 0,true,[originalError],api,ctx)}const suppressErrors=wrapper.slothlet.config?.hook?.suppressErrors===true;if(suppressErrors){return void 0}throw error})}finalResult=result;if(hasHooks){const afterResult=hookManager.executeAfterHooks(wrapper.____slothletInternal.apiPath,result,args,api,ctx);if(afterResult.modified){finalResult=afterResult.result}}return finalResult}catch(error){this.lastSyncError=error;if(hasHooks&&!error[ERROR_HOOK_PROCESSED]){const originalError=unwrapError(error);const sourceInfo={type:"function",timestamp:Date.now(),stack:originalError.stack};hookManager.executeErrorHooks(wrapper.____slothletInternal.apiPath,originalError,sourceInfo,args,api,ctx)}const suppressErrors=wrapper.slothlet.config?.hook?.suppressErrors===true;if(suppressErrors){return void 0}throw error}finally{if(hasHooks&&!isAsync){const syncError=this.lastSyncError;this.lastSyncError=null;const resultValue=syncError?void 0:typeof finalResult!=="undefined"?finalResult:result;const errors=syncError?[unwrapError(syncError)]:[];hookManager.executeAlwaysHooks(wrapper.____slothletInternal.apiPath,args,resultValue,!!syncError,errors,api,ctx)}}};const hasTrap=(target,prop)=>{if(prop==="_materialize"){return true}if(wrapper.____slothletInternal.mode==="lazy"&&!wrapper.____slothletInternal.state.materialized&&!wrapper.____slothletInternal.state.inFlight){wrapper._materialize().catch(()=>{})}const isInternal=typeof prop==="string"&&(prop.startsWith("_")||prop.startsWith("__"));if(!isInternal&&hasOwn(wrapper,prop)){return true}if(wrapper.____slothletInternal.impl&&(typeof wrapper.____slothletInternal.impl==="object"||typeof wrapper.____slothletInternal.impl==="function")&&prop in wrapper.____slothletInternal.impl){return true}return Object.prototype.hasOwnProperty.call(target,prop)};const getOwnPropertyDescriptorTrap=(target,prop)=>{if(wrapper.____slothletInternal.mode==="lazy"&&!wrapper.____slothletInternal.state.materialized&&!wrapper.____slothletInternal.state.inFlight){wrapper._materialize().catch(()=>{})}if(prop==="____slothletInternal")return void 0;if(prop==="prototype"&&typeof target==="function"){const desc=Object.getOwnPropertyDescriptor(target,"prototype");if(desc){return desc}}if(Object.prototype.hasOwnProperty.call(target,prop)){return Object.getOwnPropertyDescriptor(target,prop)}const isInternal=typeof prop==="string"&&(prop.startsWith("_")||prop.startsWith("__"));if(!isInternal&&hasOwn(wrapper,prop)){const desc=Object.getOwnPropertyDescriptor(wrapper,prop);if(desc){return desc}}if(wrapper.____slothletInternal.impl&&(typeof wrapper.____slothletInternal.impl==="object"||typeof wrapper.____slothletInternal.impl==="function")&&prop in wrapper.____slothletInternal.impl){return Object.getOwnPropertyDescriptor(wrapper.____slothletInternal.impl,prop)}return void 0};const ownKeysTrap=target=>{if(wrapper.____slothletInternal.mode==="lazy"&&!wrapper.____slothletInternal.state.materialized&&!wrapper.____slothletInternal.state.inFlight){wrapper._materialize().catch(()=>{})}const keys=new Set;if(typeof target==="function"||target&&target.__isCallable){keys.add("prototype");keys.add("length");keys.add("name")}for(const key of Reflect.ownKeys(target)){const descriptor=Object.getOwnPropertyDescriptor(target,key);if(!descriptor.configurable||descriptor.enumerable){keys.add(key)}}if(target!==wrapper){for(const key of Reflect.ownKeys(wrapper)){const descriptor=Object.getOwnPropertyDescriptor(wrapper,key);if(descriptor&&descriptor.enumerable){keys.add(key)}}}const implKeys=wrapper.____slothletInternal.impl&&(typeof wrapper.____slothletInternal.impl==="object"||typeof wrapper.____slothletInternal.impl==="function")?Reflect.ownKeys(wrapper.____slothletInternal.impl):[];for(const key of implKeys){if(key!=="prototype"){keys.add(key)}}return Array.from(keys)};const setTrap=(target,prop,value)=>{if(wrapper.____slothletInternal.mode==="lazy"&&!wrapper.____slothletInternal.state.materialized&&!wrapper.____slothletInternal.state.inFlight){wrapper._materialize()}if(UnifiedWrapper.INTERNAL_KEYS.has(prop)&&prop!=="_materialize")return true;const internalKeys=new Set(["_materialize"]);if(!internalKeys.has(prop)){if(hasOwn(wrapper,prop)){delete wrapper[prop]}Object.defineProperty(wrapper,prop,{value,writable:false,enumerable:true,configurable:true})}else{target[prop]=value}return true};const deletePropertyTrap=(target,prop)=>{const internalKeys=new Set(["____slothletInternal","__impl","___setImpl","___resetLazy","_materialize","___invalidate","_impl","___getState","__state","__mode","__apiPath","__slothletPath","__isCallable","__materializeOnCreate","__displayName","__type","__metadata","__invalid","__filePath","__sourceFolder","__moduleID","__materialized","__inFlight"]);if(internalKeys.has(prop)){return true}const isInternal=typeof prop==="string"&&(prop.startsWith("_")||prop.startsWith("__"));if(!isInternal&&hasOwn(wrapper,prop)){const childWrapper=wrapper[prop];const childWrapperRaw=resolveWrapper(childWrapper);if(childWrapperRaw){childWrapperRaw.___invalidate()}const descriptor=Object.getOwnPropertyDescriptor(wrapper,prop);if(descriptor?.configurable){delete wrapper[prop]}}if(wrapper.____slothletInternal.impl&&typeof wrapper.____slothletInternal.impl==="object"&&prop in wrapper.____slothletInternal.impl){delete wrapper.____slothletInternal.impl[prop]}delete target[prop];return true};const constructTrap=(target,args,newTarget)=>{if(wrapper.____slothletInternal.invalid){throw new TypeError(`${wrapper.____slothletInternal.apiPath||"api"} is invalidated`)}if(wrapper.____slothletInternal.mode==="lazy"&&!wrapper.____slothletInternal.state.materialized&&!wrapper.____slothletInternal.state.inFlight){wrapper._materialize().catch(()=>{})}if(wrapper.____slothletInternal.mode==="lazy"&&wrapper.____slothletInternal.state.inFlight){return Promise.resolve(wrapper.____slothletInternal.materializationPromise).then(()=>{if(!wrapper.____slothletInternal.state.materialized){throw new wrapper.slothlet.SlothletError("INVALID_CONFIG_LAZY_MATERIALIZATION_FAILED",{apiPath:wrapper.____slothletInternal.apiPath},null,{validationError:true})}const impl2=wrapper.____slothletInternal.impl;if(typeof impl2==="function"){const effectiveNewTarget=newTarget===wrapper.____slothletInternal.proxy?impl2:newTarget;return Reflect.construct(impl2,args,effectiveNewTarget)}if(impl2&&typeof impl2==="object"&&typeof impl2.default==="function"){const effectiveNewTarget=newTarget===wrapper.____slothletInternal.proxy?impl2.default:newTarget;return Reflect.construct(impl2.default,args,effectiveNewTarget)}throw new wrapper.slothlet.SlothletError("INVALID_CONFIG_NOT_A_FUNCTION",{apiPath:wrapper.____slothletInternal.apiPath,actualType:typeof impl2},null,{validationError:true})})}const impl=wrapper.____slothletInternal.impl;if(typeof impl==="function"){const effectiveNewTarget=newTarget===wrapper.____slothletInternal.proxy?impl:newTarget;return Reflect.construct(impl,args,effectiveNewTarget)}if(impl&&typeof impl==="object"&&typeof impl.default==="function"){const effectiveNewTarget=newTarget===wrapper.____slothletInternal.proxy?impl.default:newTarget;return Reflect.construct(impl.default,args,effectiveNewTarget)}throw new wrapper.SlothletError("INVALID_CONFIG_NOT_A_FUNCTION",{apiPath:wrapper.____slothletInternal.apiPath,actualType:typeof impl},null,{validationError:true})};wrapper.____slothletInternal.proxy=new Proxy(proxyTarget,{get:getTrap,apply:applyTrap,construct:constructTrap,has:hasTrap,getOwnPropertyDescriptor:getOwnPropertyDescriptorTrap,ownKeys:ownKeysTrap,set:setTrap,deleteProperty:deletePropertyTrap,getPrototypeOf:()=>null});_proxyRegistry.set(wrapper.____slothletInternal.proxy,wrapper);return wrapper.____slothletInternal.proxy}}function resolveWrapper(value){if(!value)return null;const registered=_proxyRegistry.get(value);if(registered)return registered;if(value instanceof UnifiedWrapper&&value.____slothletInternal!=null)return value;return null}export{TYPE_STATES,UnifiedWrapper,resolveWrapper};