-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix/17514 #17786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0xbad0c0d3
wants to merge
6
commits into
getsentry:develop
Choose a base branch
from
0xbad0c0d3:fix/17514
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+133
−10
Open
Fix/17514 #17786
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5a84f21
Add abort signals to test runners
d789eda
Refactor `copyExecutionContext` for improved flexibility
4af573c
Improve `copyExecutionContext` method handling
6525e24
Refactor `copyExecutionContext` for type safety and clarity
ca281cc
Added a notation why do we need to do "bind()" here
cb97187
Ignore JUnit report files in version control
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,3 +61,6 @@ packages/gatsby/gatsby-node.d.ts | |
# intellij | ||
*.iml | ||
/**/.wrangler/* | ||
|
||
#junit reports | ||
packages/**/*.junit.xml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { type DurableObjectState, type ExecutionContext } from '@cloudflare/workers-types'; | ||
|
||
type ContextType = ExecutionContext | DurableObjectState; | ||
type OverridesStore<T extends ContextType> = Map<keyof T, (...args: unknown[]) => unknown>; | ||
|
||
/** | ||
* Creates a new copy of the given execution context, optionally overriding methods. | ||
* | ||
* @param {ContextType|void} ctx - The execution context to be copied. Can be of type `ContextType` or `void`. | ||
* @return {ContextType|void} A new execution context with the same properties and overridden methods if applicable. | ||
*/ | ||
export function copyExecutionContext<T extends ContextType>(ctx: T): T { | ||
if (!ctx) return ctx; | ||
|
||
const overrides: OverridesStore<T> = new Map(); | ||
const contextPrototype = Object.getPrototypeOf(ctx); | ||
const methodNames = Object.getOwnPropertyNames(contextPrototype) as unknown as (keyof T)[]; | ||
const descriptors = methodNames.reduce((prevDescriptors, methodName) => { | ||
if (methodName === 'constructor') return prevDescriptors; | ||
if (typeof ctx[methodName] !== 'function') return prevDescriptors; | ||
const overridableDescriptor = makeOverridableDescriptor(overrides, ctx, methodName); | ||
return { | ||
...prevDescriptors, | ||
[methodName]: overridableDescriptor, | ||
}; | ||
}, {}); | ||
|
||
return Object.create(ctx, descriptors); | ||
} | ||
|
||
/** | ||
* Creates a property descriptor that allows overriding of a method on the given context object. | ||
* | ||
* This descriptor supports property overriding with functions only. It delegates method calls to | ||
* the provided store if an override exists or to the original method on the context otherwise. | ||
* | ||
* @param {OverridesStore<ContextType>} store - The storage for overridden methods specific to the context type. | ||
* @param {ContextType} ctx - The context object that contains the method to be overridden. | ||
* @param {keyof ContextType} method - The method on the context object to create the overridable descriptor for. | ||
* @return {PropertyDescriptor} A property descriptor enabling the overriding of the specified method. | ||
*/ | ||
function makeOverridableDescriptor<T extends ContextType>( | ||
store: OverridesStore<T>, | ||
ctx: T, | ||
method: keyof T, | ||
): PropertyDescriptor { | ||
return { | ||
configurable: true, | ||
enumerable: true, | ||
set: newValue => { | ||
if (typeof newValue !== 'function') throw new Error('Cannot override non-function'); | ||
store.set(method, newValue); | ||
return true; | ||
}, | ||
|
||
get: () => { | ||
if (store.has(method)) return store.get(method); | ||
const methodFunction = Reflect.get(ctx, method); | ||
if (typeof methodFunction !== 'function') return methodFunction; | ||
// We should do bind() to make sure that the method is bound to the context object - otherwise it will not work | ||
return methodFunction.bind(ctx); | ||
}, | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { type Mocked, describe, expect, it, vi } from 'vitest'; | ||
import { copyExecutionContext } from '../src/utils/copyExecutionContext'; | ||
|
||
describe('Copy of the execution context', () => { | ||
describe.for([ | ||
'waitUntil', | ||
'passThroughOnException', | ||
'acceptWebSocket', | ||
'blockConcurrencyWhile', | ||
'getWebSockets', | ||
'arbitraryMethod', | ||
'anythingElse', | ||
])('%s', method => { | ||
it('Override without changing original', async () => { | ||
const context = { | ||
[method]: vi.fn(), | ||
} as any; | ||
const copy = copyExecutionContext(context); | ||
copy[method] = vi.fn(); | ||
expect(context[method]).not.toBe(copy[method]); | ||
}); | ||
|
||
it('Overridden method was called', async () => { | ||
const context = { | ||
[method]: vi.fn(), | ||
} as any; | ||
const copy = copyExecutionContext(context); | ||
const overridden = vi.fn(); | ||
copy[method] = overridden; | ||
copy[method](); | ||
expect(overridden).toBeCalled(); | ||
expect(context[method]).not.toBeCalled(); | ||
}); | ||
}); | ||
|
||
it('No side effects', async () => { | ||
const context = makeExecutionContextMock(); | ||
expect(() => copyExecutionContext(Object.freeze(context))).not.toThrow( | ||
/Cannot define property \w+, object is not extensible/, | ||
); | ||
}); | ||
it('Respects symbols', async () => { | ||
const s = Symbol('test'); | ||
const context = makeExecutionContextMock<ExecutionContext & { [s]: unknown }>(); | ||
context[s] = {}; | ||
const copy = copyExecutionContext(context); | ||
expect(copy[s]).toBe(context[s]); | ||
}); | ||
}); | ||
|
||
function makeExecutionContextMock<T extends ExecutionContext>() { | ||
return { | ||
waitUntil: vi.fn(), | ||
passThroughOnException: vi.fn(), | ||
} as unknown as Mocked<T>; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Override System Excludes Own Properties and Symbols
The
copyExecutionContext
function's new override system only applies to string-named methods found on the context object's prototype. This means methods that are own properties of the context object, or symbol-named methods on its prototype, are not correctly handled, impacting their binding and override capabilities.