Skip to content

Commit a90210e

Browse files
author
Chris Dickinson
committed
fix(jspi): JSPI should not override http calls in worker context
This fixes a bug where we'd use the system `fetch` inside a plugin run in a worker when JSPI was enabled, bypassing overrides of the `fetch` function.
1 parent ae88c5d commit a90210e

File tree

4 files changed

+9
-3
lines changed

4 files changed

+9
-3
lines changed

src/foreground-plugin.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,17 @@ async function instantiateModule(
244244
// haven't overridden the default CallContext implementation, we provide an HttpContext
245245
// on-demand.
246246
//
247-
// Unfortuantely this duplicates a little bit of logic-- in particular, we have to bind
247+
// Unfortunately this duplicates a little bit of logic-- in particular, we have to bind
248248
// CallContext to each of the HttpContext contributions (See "REBIND" below.)
249+
//
250+
// Notably, if we're calling this from a background thread, skip all of the patching:
251+
// we want to dispatch to the main thread.
249252
if (
250253
module === EXTISM_ENV &&
251254
name === 'http_request' &&
252255
promising &&
253-
imports[module][name] === context[ENV].http_request
256+
imports[module][name] === context[ENV].http_request &&
257+
!opts.executingInWorker
254258
) {
255259
const httpContext = new HttpContext(opts.fetch, opts.allowedHosts, opts.memory, opts.allowHttpResponseHeaders);
256260

src/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ export interface InternalConfig extends Required<NativeManifestOptions> {
287287
sharedArrayBufferSize: number;
288288
allowHttpResponseHeaders: boolean;
289289
nodeWorkerArgs: NodeWorkerArgs;
290+
executingInWorker: boolean;
290291
}
291292

292293
export interface InternalWasi {

src/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export async function createPlugin(
136136
}
137137

138138
const ic: InternalConfig = {
139+
executingInWorker: false,
139140
allowedHosts: opts.allowedHosts as [],
140141
allowedPaths: opts.allowedPaths,
141142
functions: opts.functions,

src/worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class Reactor {
155155

156156
// TODO: replace our internal fetch and logger
157157
this.plugin = await _createForegroundPlugin(
158-
{ ...opts, functions, fetch, logger } as InternalConfig,
158+
{ ...opts, functions, fetch, logger, executingInWorker: true } as InternalConfig,
159159
ev.names,
160160
modules,
161161
this.context,

0 commit comments

Comments
 (0)