-
Notifications
You must be signed in to change notification settings - Fork 90
fix: preserve vendor API method receivers #843
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
harlan-zw
wants to merge
2
commits into
main
Choose a base branch
from
fix/vendor-api-receivers
base: main
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.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,65 @@ | ||
| type ScriptApi = Record<PropertyKey, any> | ||
|
|
||
| function bindMethod(owner: ScriptApi, method: (...args: any[]) => any): (...args: any[]) => any { | ||
| const wrapped: (...args: any[]) => any = new Proxy(method, { | ||
| apply(target, _receiver, args) { | ||
| return Reflect.apply(target, owner, args) | ||
| }, | ||
| construct(target, args, newTarget): object { | ||
| return Reflect.construct(target, args, newTarget === wrapped ? target : newTarget) | ||
| }, | ||
| get(target, property) { | ||
| return Reflect.get(target, property, target) | ||
| }, | ||
| set(target, property, value) { | ||
| return Reflect.set(target, property, value, target) | ||
| }, | ||
| }) | ||
| return wrapped | ||
| } | ||
|
|
||
| /** | ||
| * Keep vendor methods attached to the object returned by `use()`. | ||
| * | ||
| * Unhead's loaded script proxy forwards methods with the forwarding proxy as | ||
| * `this`. Vendor methods can then reach recursively proxied platform objects, | ||
| * which fail native brand checks such as Firefox's Element checks. Returning | ||
| * stable method wrappers preserves the vendor API as the receiver while | ||
| * retaining queued proxy calls and constructable function properties. | ||
| */ | ||
| export function bindScriptApiMethods<T>(api: T): T { | ||
| if ((typeof api !== 'object' && typeof api !== 'function') || api === null) | ||
| return api | ||
|
|
||
| const target = api as ScriptApi | ||
| const methods = new Map<PropertyKey, { method: (...args: any[]) => any, wrapped: (...args: any[]) => any }>() | ||
|
|
||
| return new Proxy(target, { | ||
| get(innerTarget, property) { | ||
| const value = Reflect.get(innerTarget, property, innerTarget) | ||
| if (typeof value !== 'function') | ||
| return value | ||
|
|
||
| const cached = methods.get(property) | ||
| if (cached && cached.method === value) | ||
| return cached.wrapped | ||
|
|
||
| const wrapped = bindMethod(innerTarget, value) | ||
| methods.set(property, { method: value, wrapped }) | ||
| return wrapped | ||
| }, | ||
| set(innerTarget, property, value) { | ||
| methods.delete(property) | ||
| return Reflect.set(innerTarget, property, value, innerTarget) | ||
| }, | ||
| }) as T | ||
| } | ||
|
|
||
| export function bindScriptApiResolver<T>(resolve: () => T | Promise<T>): () => T | Promise<T> { | ||
| return () => { | ||
| const result = resolve() | ||
| return result instanceof Promise | ||
| ? result.then(bindScriptApiMethods) | ||
| : bindScriptApiMethods(result) | ||
| } | ||
| } | ||
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,26 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { useScript } from '../../packages/script/src/runtime/composables/useScript' | ||
|
|
||
| describe('script API proxy receivers', () => { | ||
| it('preserves vendor private state when a proxy method is called', () => { | ||
| const brandedApis = new WeakSet<object>() | ||
| const api = { | ||
| readCanvasWidth() { | ||
| if (!brandedApis.has(this)) | ||
| throw new TypeError('Illegal invocation') | ||
| return 120 | ||
| }, | ||
| } | ||
| brandedApis.add(api) | ||
| const script = useScript({ | ||
| key: 'strict-vendor-api', | ||
| innerHTML: '', | ||
| }, { | ||
| trigger: 'manual', | ||
| use: () => api, | ||
| }) | ||
|
|
||
| expect(script.proxy.readCanvasWidth()).toBe(120) | ||
| script.remove() | ||
| }) | ||
| }) |
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,49 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { bindScriptApiMethods } from '../../packages/script/src/runtime/script-api' | ||
|
|
||
| function createForwardingProxy<T extends object>(target: T): T { | ||
| const handler: ProxyHandler<object> = { | ||
| get(innerTarget, property, receiver) { | ||
| const value = Reflect.get(innerTarget, property, receiver) | ||
| return typeof value === 'object' && value !== null | ||
| ? new Proxy(value, handler) | ||
| : value | ||
| }, | ||
| } | ||
| return new Proxy(target, handler) as T | ||
| } | ||
|
|
||
| describe('bindScriptApiMethods', () => { | ||
| it('preserves the vendor instance as a method receiver through a forwarding proxy', () => { | ||
| const brandedCanvas = new WeakSet<object>() | ||
| const canvas = { | ||
| getBoundingClientRect() { | ||
| if (!brandedCanvas.has(this)) | ||
| throw new TypeError('Illegal invocation') | ||
| return { width: 120 } | ||
| }, | ||
| } | ||
| brandedCanvas.add(canvas) | ||
| const api = { | ||
| canvas, | ||
| addConfetti() { | ||
| return this.canvas.getBoundingClientRect().width | ||
| }, | ||
| } | ||
| const proxy = createForwardingProxy(bindScriptApiMethods(api)) | ||
|
|
||
| expect(proxy.addConfetti()).toBe(120) | ||
| }) | ||
|
|
||
| it('keeps bound method identity stable', () => { | ||
| const api = { | ||
| call() { | ||
| return this | ||
| }, | ||
| } | ||
| const bound = bindScriptApiMethods(api) | ||
|
|
||
| expect(bound.call).toBe(bound.call) | ||
| expect(bound.call()).toBe(api) | ||
| }) | ||
| }) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.