fix(liveshare-protocol): keep the replaced fetch's receiver

The carrier called the captured page fetch as a plain function, so a browser fetch that checks its receiver would fail on every non-forwarded request. Call it with globalThis as the receiver, as the repository's other fetch wrappers do, and pin the receiver in a spec.
This commit is contained in:
mektpoy committed 2026-10-23 16:13:22 +08:00
1 parent ace985e94f
commit d218ba88f0
2 files changed
+13 -1

No files matched your search

@@ -179,7 +179,7 @@ export function installViewerCarrier(options: ViewerCarrierOptions): void {
pathname = undefined
}
const path = pathname === undefined ? undefined : forwardedRoute(pathname, method)
return path === undefined ? await pageFetch(input, init) : await forwardRoute(path, init)
return path === undefined ? await pageFetch.call(globalThis, input, init) : await forwardRoute(path, init)
}
globalThis.fetch = carrierFetch
@@ -547,6 +547,18 @@ describe('viewer carrier forwarded page routes', () => {
expect(direct).toHaveBeenCalledExactlyOnceWith('health', undefined)
})
it('keeps the receiver of the page fetch it replaced', async () => {
const receivers: unknown[] = []
const direct = vi.fn(function (this: unknown, input: unknown) {
receivers.push(this)
return Promise.resolve(new Response(`direct:${String(input)}`))
})
vi.stubGlobal('fetch', direct)
installViewerCarrier(OPTIONS)
await globalThis.fetch('health')
expect(receivers).toEqual([globalThis])
})
it('does not forward a route that acts on the owner\u2019s machine', async () => {
const direct = directFetch()
expect(await (await globalThis.fetch('open-in-app/open', { method: 'POST', body: '{"app":"finder"}' })).text())