Repository navigation
Conversation
🦋 Changeset detectedLatest commit: 9a5dd40 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
All contributors have signed the CLA ✍️ ✅ |
|
✅ Approved — Small, backwards-compatible addition of an optional The decision is advisory: this comment never approves, blocks or requests changes on the pull request. ✅ All checks passed📋 Code Quality: ✅ LGTMThis change is safe to merge as it is.
📝 Documentation: ✅ LGTMThe new "Custom Reviewed commit: |
|
I have read the CLA Document and I hereby sign the CLA |
|
clanky review |
Lets callers pass a fetch function in the options, used instead of the global fetch to send the batch (e.g. a VPC or service binding).
c06f4e6 to
9a5dd40
Compare
commit: |
| * fetch function, such as a VPC or service binding (pass e.g. | ||
| * `(input, init) => env.VPC.fetch(input, init)`), or for instrumenting requests. | ||
| */ | ||
| fetch?: typeof fetch; |
There was a problem hiding this comment.
typeof fetch breaks with Bun types loaded (its fetch requires preconnect), so a plain wrapper fails with TS2741. the test below hits it too, CI just doesnt typecheck it. maybe:
fetch?: (input: string | Request, init?: RequestInit) => Promise<Response>| let response = await fetch(urlOrRequest, { | ||
| // Look up the global `fetch` at send time (not at session creation) so that it behaves as it | ||
| // did before this option existed. | ||
| let fetchFunc = options?.fetch ?? fetch; |
There was a problem hiding this comment.
called unbound, so { fetch: env.VPC.fetch } typechecks but throws Illegal invocation in workerd, which is the #66 use case. could also accept a { fetch } object and call it as a method?
| .toStrictEqual([36, 5, 9]); | ||
| }); | ||
|
|
||
| it("sends the batch using a custom fetch function from options", async () => { |
There was a problem hiding this comment.
these only wrap global fetch, the binding path isnt covered. workerd.test.ts already has env.testServer (~L461) so a test there would be cheap
| let url = `http://${inject("testServerHost")}`; | ||
| let originalFetch = globalThis.fetch; | ||
| let calls = 0; | ||
| globalThis.fetch = <any>((input: any, init?: RequestInit) => { |
There was a problem hiding this comment.
swapping globalThis.fetch seems flaky, any stray fetch fails the toBe(1). a recording custom fetch or vi.spyOn filtered by URL would be sturdier
| @@ -142,9 +143,12 @@ export let newWebSocketRpcSession:<T extends RpcCompatible<T> = Empty> | |||
| * The parameters to this method have exactly the same signature as `fetch()`, but the return | |||
There was a problem hiding this comment.
nit, pre-existing: this still says "exactly the same signature as fetch()" which the new paragraph contradicts (2nd arg is session options, not RequestInit)
Fixes #66
Summary
newHttpBatchRpcSession()now accepts an optionalfetchfunction in its options and uses it instead of the globalfetch()to send the batch.Problem
The batch client always calls the global
fetch, so there is no supported way to send a batch through a VPC or service binding (they expose their ownfetch). The workaround in the issue is to overwriteglobalThis.fetchtemporarily, andnew Request(url, { fetcher })relies on an old undocumented runtime API that only works with the built-inFetchertype. Kenton agreed a customfetchinoptionsmakes sense.Solution
Add
HttpBatchRpcSessionOptions = RpcSessionOptions & { fetch?: typeof fetch }and use it only fornewHttpBatchRpcSession.fetchis looked up at send time withoptions?.fetch ?? fetch, so the default is still the globalfetch, exactly as before.Design choice: I put the option on a dedicated options type rather than on
RpcSessionOptions, because the other transports (WebSocket, MessagePort, custom) never callfetch, and the shared type would suggest otherwise. It is a superset ofRpcSessionOptions, so existing calls still type-check. Happy to move it ontoRpcSessionOptionsif you prefer.The function is called unbound as
fetch(urlOrRequest, init), so a method likeenv.VPC.fetchneeds a wrapper ((input, init) => env.VPC.fetch(input, init)); the docs example shows this.Changes
src/batch.tsHttpBatchRpcSessionOptions; useoptions.fetchwhen presentsrc/index.tsnewHttpBatchRpcSessionsignature and JSDoc__tests__/index.test.tsfetchis used (globalfetchreplaced with a throwing stub), and the globalfetchis still the defaultpackages/docs/src/content/docs/transports/http-batch.mdxfetch" section.changeset/http-batch-custom-fetch.mdcapnwebBefore / After
How to test
pnpm install && pnpm testand look at the "HTTP requests" tests.options?.fetch ??part insrc/batch.ts: "sends the batch using a custom fetch function from options" fails.Testing done
__tests__/index.test.ts(custom fetch used and called once with POST; global fetch default preserved).fetch.Notes for reviewers
Backwards compatible:
optionswithoutfetchbehaves as before. Also happy to add the option to the API reference table if wanted.