|
1 | 1 | import { type DurableObjectState, type ExecutionContext } from '@cloudflare/workers-types';
|
2 | 2 |
|
3 |
| -const kBound = Symbol.for('kBound'); |
4 |
| - |
5 |
| -const defaultPropertyOptions: PropertyDescriptor = { |
6 |
| - enumerable: true, |
7 |
| - configurable: true, |
8 |
| - writable: true, |
9 |
| -}; |
| 3 | +type ContextType = ExecutionContext | DurableObjectState; |
| 4 | +type OverridesStore = Map<string | symbol, (...args: unknown[]) => unknown>; |
10 | 5 |
|
11 | 6 | /**
|
12 |
| - * Clones the given execution context by creating a shallow copy while ensuring the binding of specific methods. |
| 7 | + * Creates a new copy of the given execution context, optionally overriding methods. |
13 | 8 | *
|
14 |
| - * @param {ExecutionContext|DurableObjectState|void} ctx - The execution context to clone. Can be void. |
15 |
| - * @return {ExecutionContext|DurableObjectState|void} A cloned execution context with bound methods, or the original void value if no context was provided. |
| 9 | + * @param {ContextType|void} ctx - The execution context to be copied. Can be of type `ContextType` or `void`. |
| 10 | + * @return {ContextType|void} A new execution context with the same properties and overridden methods if applicable. |
16 | 11 | */
|
17 |
| -export function copyExecutionContext<T extends ExecutionContext | DurableObjectState>(ctx: T): T { |
| 12 | +export function copyExecutionContext<T extends ContextType>(ctx: T): T { |
18 | 13 | if (!ctx) return ctx;
|
19 |
| - return Object.create(ctx, { |
20 |
| - waitUntil: { ...defaultPropertyOptions, value: copyAndBindMethod(ctx, 'waitUntil') }, |
21 |
| - ...('passThroughOnException' in ctx && { |
22 |
| - passThroughOnException: { ...defaultPropertyOptions, value: copyAndBindMethod(ctx, 'passThroughOnException') }, |
23 |
| - }), |
24 |
| - }); |
| 14 | + |
| 15 | + const overrides: OverridesStore = new Map(); |
| 16 | + const contextPrototype = Object.getPrototypeOf(ctx); |
| 17 | + const descriptors = Object.getOwnPropertyNames(contextPrototype).reduce((prevDescriptors, methodName) => { |
| 18 | + if (methodName === 'constructor') return prevDescriptors; |
| 19 | + const pd = makeMethodDescriptor(overrides, ctx, methodName as keyof ContextType); |
| 20 | + return { |
| 21 | + ...prevDescriptors, |
| 22 | + [methodName]: pd, |
| 23 | + }; |
| 24 | + }, {}); |
| 25 | + |
| 26 | + return Object.create(ctx, descriptors); |
25 | 27 | }
|
26 | 28 |
|
27 | 29 | /**
|
28 |
| - * Copies a method from the given object and ensures the copied method remains bound to the original object's context. |
| 30 | + * Creates a property descriptor for a given method on a context object, enabling custom getter and setter behavior. |
29 | 31 | *
|
30 |
| - * @param {object} obj - The object containing the method to be copied and bound. |
31 |
| - * @param {string|symbol} method - The key of the method within the object to be copied and bound. |
32 |
| - * @return {Function} - The copied and bound method, or the original property if it is not a function. |
| 32 | + * @param store - The OverridesStore instance used to manage method overrides. |
| 33 | + * @param ctx - The context object from which the method originates. |
| 34 | + * @param method - The key of the method on the context object to create a descriptor for. |
| 35 | + * @return A property descriptor with custom getter and setter functionalities for the specified method. |
33 | 36 | */
|
34 |
| -function copyAndBindMethod<T, K extends keyof T>(obj: T, method: K): T[K] { |
35 |
| - const methodImpl = obj[method]; |
36 |
| - if (typeof methodImpl !== 'function') return methodImpl; |
37 |
| - if ((methodImpl as T[K] & { [kBound]?: boolean })[kBound]) return methodImpl; |
38 |
| - const bound = methodImpl.bind(obj); |
| 37 | +function makeMethodDescriptor(store: OverridesStore, ctx: ContextType, method: keyof ContextType): PropertyDescriptor { |
| 38 | + return { |
| 39 | + configurable: true, |
| 40 | + enumerable: true, |
| 41 | + set: newValue => { |
| 42 | + store.set(method, newValue); |
| 43 | + return true; |
| 44 | + }, |
39 | 45 |
|
40 |
| - return new Proxy(bound, { |
41 |
| - get: (target, prop, receiver) => { |
42 |
| - if (kBound === prop) return true; |
43 |
| - if ('bind' === prop) return () => receiver; |
44 |
| - return Reflect.get(target, prop, receiver); |
| 46 | + get: () => { |
| 47 | + if (store.has(method)) return store.get(method); |
| 48 | + return Reflect.get(ctx, method).bind(ctx); |
45 | 49 | },
|
46 |
| - }); |
| 50 | + }; |
47 | 51 | }
|
0 commit comments