-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(wasm): patch non-streaming load paths #23767
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
d2anamaria
wants to merge
6
commits into
develop
Choose a base branch
from
ana/feat/wasm-non-streaming
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.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5ae3418
feat(wasm): patch non-streaming load paths
d2anamaria 99a2a60
Forward extra arguments and guard buffer registration
andreiborza 2327538
Guard the Response prototype patching
andreiborza 8bfb52c
Drop the unnecessary double-cast
andreiborza 7799bee
Add a browser test for non-streaming registration
andreiborza 3211543
Guard every patch seam so the wasm integration never throws into user…
andreiborza 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 |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /** | ||
| * Streaming wasm registration (`instantiateStreaming` / `compileStreaming`) reads the module URL | ||
| * from `Response.url`. Non-streaming paths (`WebAssembly.instantiate` / `compile` with bytes) only | ||
| * receive a buffer — no URL — so registration would otherwise be skipped. | ||
| * | ||
| * This module patches `Response.prototype.arrayBuffer` and `bytes` so that when wasm is fetched | ||
| * and then loaded from bytes, we can map the resulting `ArrayBuffer` back to the fetch URL via | ||
| * `getWasmSourceUrl()` and register the module in `patchNonStreamingWebAssembly`. | ||
| */ | ||
| const wasmSourceUrls = new WeakMap<ArrayBuffer, string>(); | ||
|
|
||
| const PATCHED_SYMBOL = Symbol.for('__sentryWasmPatched'); | ||
|
|
||
| type MaybePatched = { [PATCHED_SYMBOL]?: boolean }; | ||
|
|
||
| /** | ||
| * Resolves a wasm source buffer back to its fetch URL, when known. | ||
| */ | ||
| export function getWasmSourceUrl(source: BufferSource): string | undefined { | ||
| const buffer = toArrayBuffer(source); | ||
| if (!buffer) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return wasmSourceUrls.get(buffer); | ||
| } | ||
|
|
||
| function toArrayBuffer(source: BufferSource): ArrayBuffer | undefined { | ||
| if (source instanceof ArrayBuffer) { | ||
| return source; | ||
| } | ||
|
|
||
| if (ArrayBuffer.isView(source)) { | ||
| const { buffer } = source; | ||
| return buffer instanceof ArrayBuffer ? buffer : undefined; | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| function looksLikeWasmResponse(response: Response): boolean { | ||
| const contentType = response.headers.get('content-type'); | ||
| if (contentType?.includes('application/wasm')) { | ||
| return true; | ||
| } | ||
|
|
||
| const { url } = response; | ||
| return Boolean(url && /\.wasm(?:\?|#|$)/i.test(url)); | ||
| } | ||
|
|
||
| function tagResponseBuffer(response: Response, buffer: ArrayBuffer): void { | ||
| if (looksLikeWasmResponse(response) && response.url) { | ||
| wasmSourceUrls.set(buffer, response.url); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Patches Response body readers so wasm bytes remember their fetch URL. | ||
| */ | ||
| export function patchWasmResponseBodyReaders(): void { | ||
| if (typeof Response === 'undefined') { | ||
| return; | ||
| } | ||
|
|
||
| const responseProto = Response.prototype as MaybePatched; | ||
| if (responseProto[PATCHED_SYMBOL]) { | ||
| return; | ||
| } | ||
|
|
||
| responseProto[PATCHED_SYMBOL] = true; | ||
|
|
||
| // oxlint-disable-next-line typescript/unbound-method | ||
| const origArrayBuffer: (this: Response) => Promise<ArrayBuffer> = Response.prototype.arrayBuffer; | ||
| Response.prototype.arrayBuffer = function arrayBuffer(this: Response): Promise<ArrayBuffer> { | ||
| const bufferPromise: Promise<ArrayBuffer> = origArrayBuffer.call(this); | ||
| return bufferPromise.then((buffer: ArrayBuffer) => { | ||
| tagResponseBuffer(this, buffer); | ||
| return buffer; | ||
| }); | ||
| }; | ||
|
|
||
| if ('bytes' in Response.prototype) { | ||
| // oxlint-disable-next-line typescript/unbound-method | ||
| const origBytes: (this: Response) => Promise<Uint8Array> = Response.prototype.bytes; | ||
| Response.prototype.bytes = function bytes(this: Response) { | ||
| const bytesPromise: Promise<Uint8Array> = origBytes.call(this); | ||
| return bytesPromise.then((bytes: Uint8Array) => { | ||
| const { buffer } = bytes; | ||
| if (buffer instanceof ArrayBuffer) { | ||
| tagResponseBuffer(this, buffer); | ||
| } | ||
| return bytes; | ||
| }); | ||
| } as typeof Response.prototype.bytes; | ||
| } | ||
| } | ||
|
|
||
| /** @internal */ | ||
| export function _resetResponsePatchForTests(): void { | ||
| if (typeof Response !== 'undefined') { | ||
| (Response.prototype as MaybePatched)[PATCHED_SYMBOL] = false; | ||
| } | ||
| } | ||
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,39 @@ | ||
| import { _resetResponsePatchForTests } from '../src/patchWasmResponse'; | ||
| import { _resetNonStreamingPatchForTests } from '../src/patchWebAssembly'; | ||
|
|
||
| export type SavedWasmGlobals = { | ||
| instantiate: typeof WebAssembly.instantiate; | ||
| compile: typeof WebAssembly.compile; | ||
| instantiateStreaming?: typeof WebAssembly.instantiateStreaming; | ||
| compileStreaming?: typeof WebAssembly.compileStreaming; | ||
| arrayBuffer: typeof Response.prototype.arrayBuffer; | ||
| bytes?: typeof Response.prototype.bytes; | ||
| }; | ||
|
|
||
| export function saveWasmGlobals(): SavedWasmGlobals { | ||
| return { | ||
| instantiate: WebAssembly.instantiate, | ||
| compile: WebAssembly.compile, | ||
| instantiateStreaming: WebAssembly.instantiateStreaming, | ||
| compileStreaming: WebAssembly.compileStreaming, | ||
| arrayBuffer: Response.prototype.arrayBuffer, | ||
| bytes: 'bytes' in Response.prototype ? Response.prototype.bytes : undefined, | ||
| }; | ||
| } | ||
|
|
||
| export function restoreWasmGlobals(saved: SavedWasmGlobals): void { | ||
| WebAssembly.instantiate = saved.instantiate; | ||
| WebAssembly.compile = saved.compile; | ||
| if (saved.instantiateStreaming) { | ||
| WebAssembly.instantiateStreaming = saved.instantiateStreaming; | ||
| } | ||
| if (saved.compileStreaming) { | ||
| WebAssembly.compileStreaming = saved.compileStreaming; | ||
| } | ||
| Response.prototype.arrayBuffer = saved.arrayBuffer; | ||
| if (saved.bytes) { | ||
| Response.prototype.bytes = saved.bytes; | ||
| } | ||
| _resetNonStreamingPatchForTests(); | ||
| _resetResponsePatchForTests(); | ||
| } |
Oops, something went wrong.
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.