|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off - the test runs a real TLS h2 server to see which protocol fetch negotiates. |
| 2 | +import * as NodeHttp2 from "node:http2"; |
| 3 | +import * as NodeNet from "node:net"; |
| 4 | +import * as NodeTls from "node:tls"; |
| 5 | + |
| 6 | +import { it } from "@effect/vitest"; |
| 7 | +import * as Effect from "effect/Effect"; |
| 8 | +import { FetchHttpClient, HttpClient } from "effect/http"; |
| 9 | +import * as Undici from "undici"; |
| 10 | +import { afterAll, afterEach, beforeAll, describe, expect } from "vite-plus/test"; |
| 11 | + |
| 12 | +import { builtinFetchUsesHttp2, installHttp1FetchDispatcher } from "./outboundHttp.ts"; |
| 13 | + |
| 14 | +// Test-only self-signed P-256 certificate for 127.0.0.1, valid until 2126; not a |
| 15 | +// secret. Node cannot mint X.509 certificates without a library, and a local |
| 16 | +// server only offers h2 over ALPN, which needs TLS. |
| 17 | +const TEST_KEY = `-----BEGIN PRIVATE KEY----- |
| 18 | +MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg3EwCztQysKTvzdcR |
| 19 | +g0LWq+axzUd4tHi9PyMdqh7PlGShRANCAARfQpoLgv/tOsHWCLriDza7Ruaey2b7 |
| 20 | +mR/nS6mLu6W7+agkEqDm0mE3F/ngpPjL4Jj0IGadeMcKNKFRSBgiLdfn |
| 21 | +-----END PRIVATE KEY----- |
| 22 | +`; |
| 23 | +const TEST_CERT = `-----BEGIN CERTIFICATE----- |
| 24 | +MIIBsDCCAVagAwIBAgIUWbn2Wt5kTxii1UU57NJMt/wTaUgwCgYIKoZIzj0EAwIw |
| 25 | +JDEiMCAGA1UEAwwZdDNjb2RlIG91dGJvdW5kIGh0dHAgdGVzdDAgFw0yNjEwMTEy |
| 26 | +MjQyMzZaGA8yMTI2MDkxNzIyNDIzNlowJDEiMCAGA1UEAwwZdDNjb2RlIG91dGJv |
| 27 | +dW5kIGh0dHAgdGVzdDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABF9CmguC/+06 |
| 28 | +wdYIuuIPNrtG5p7LZvuZH+dLqYu7pbv5qCQSoObSYTcX+eCk+MvgmPQgZp14xwo0 |
| 29 | +oVFIGCIt1+ejZDBiMB0GA1UdDgQWBBSsekAc30Q/zdIE/tvVDQKJL5oiCDAfBgNV |
| 30 | +HSMEGDAWgBSsekAc30Q/zdIE/tvVDQKJL5oiCDAPBgNVHRMBAf8EBTADAQH/MA8G |
| 31 | +A1UdEQQIMAaHBH8AAAEwCgYIKoZIzj0EAwIDSAAwRQIgebUrkyP56242EJB7nCV7 |
| 32 | +A+Ke/q0rNPya3fjQa9O5B+kCIQC1SfmdNaFcNJ4JINlus4FfrRnfKiADS2MIJR9f |
| 33 | +99BN0A== |
| 34 | +-----END CERTIFICATE----- |
| 35 | +`; |
| 36 | + |
| 37 | +// An h2-capable origin, like api.github.com, that answers with the HTTP |
| 38 | +// version the client negotiated. |
| 39 | +const server = NodeHttp2.createSecureServer( |
| 40 | + { allowHTTP1: true, key: TEST_KEY, cert: TEST_CERT }, |
| 41 | + (request, response) => response.end(request.httpVersion), |
| 42 | +); |
| 43 | +let origin = ""; |
| 44 | +const defaultCaCertificates = NodeTls.getCACertificates("default"); |
| 45 | +const defaultDispatcher = Undici.getGlobalDispatcher(); |
| 46 | + |
| 47 | +beforeAll(async () => { |
| 48 | + NodeTls.setDefaultCACertificates([...defaultCaCertificates, TEST_CERT]); |
| 49 | + await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve)); |
| 50 | + origin = `https://127.0.0.1:${(server.address() as NodeNet.AddressInfo).port}/`; |
| 51 | +}); |
| 52 | + |
| 53 | +afterEach(() => { |
| 54 | + Undici.setGlobalDispatcher(defaultDispatcher); |
| 55 | +}); |
| 56 | + |
| 57 | +afterAll(async () => { |
| 58 | + NodeTls.setDefaultCACertificates(defaultCaCertificates); |
| 59 | + await new Promise<void>((resolve) => server.close(() => resolve())); |
| 60 | +}); |
| 61 | + |
| 62 | +const versionViaGlobalDispatcher = async () => (await Undici.request(origin)).body.text(); |
| 63 | + |
| 64 | +// Node's built-in fetch, the path the server's own requests take. |
| 65 | +const versionViaBuiltinFetch = () => |
| 66 | + HttpClient.get(origin).pipe( |
| 67 | + Effect.flatMap((response) => response.text), |
| 68 | + Effect.provide(FetchHttpClient.layer), |
| 69 | + ); |
| 70 | + |
| 71 | +describe("installHttp1FetchDispatcher", () => { |
| 72 | + it("makes the global dispatcher negotiate HTTP/1.1 and keeps the env proxy agent", async () => { |
| 73 | + // undici 8's default agent, the one Node 26's fetch uses. |
| 74 | + Undici.setGlobalDispatcher(new Undici.Agent()); |
| 75 | + expect(await versionViaGlobalDispatcher()).toBe("2.0"); |
| 76 | + |
| 77 | + installHttp1FetchDispatcher(); |
| 78 | + expect(await versionViaGlobalDispatcher()).toBe("1.1"); |
| 79 | + |
| 80 | + // Node installs this agent for --use-env-proxy; replacing it would drop the proxy. |
| 81 | + Undici.setGlobalDispatcher(new Undici.EnvHttpProxyAgent()); |
| 82 | + installHttp1FetchDispatcher(); |
| 83 | + expect(Undici.getGlobalDispatcher()).toBeInstanceOf(Undici.EnvHttpProxyAgent); |
| 84 | + }); |
| 85 | + |
| 86 | + // Only a runtime that bundles undici 8 (Node 26+) offers h2 through the |
| 87 | + // built-in fetch, so only there can this prove fetch reads the new dispatcher. |
| 88 | + it.effect.runIf(builtinFetchUsesHttp2(process.versions.undici))( |
| 89 | + "moves the built-in fetch off HTTP/2", |
| 90 | + () => |
| 91 | + Effect.gen(function* () { |
| 92 | + expect(yield* versionViaBuiltinFetch()).toBe("2.0"); |
| 93 | + installHttp1FetchDispatcher(); |
| 94 | + expect(yield* versionViaBuiltinFetch()).toBe("1.1"); |
| 95 | + }), |
| 96 | + ); |
| 97 | +}); |
0 commit comments