Mirror: A Node.js fetch shim using built-in Request, Response, and Headers (but without native fetch)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: Add `connectTimeout` and set default to 30s for HTML requests (#39)

authored by

Phil Pluckthun and committed by
GitHub
a383cd0c 31c09e75

+19 -1
+5
.changeset/polite-loops-cross.md
··· 1 + --- 2 + 'fetch-nodeshim': patch 3 + --- 4 + 5 + Add configurable `connectTimeout` to override connection timeout. The default will now also be 30s if the request contains `text/html` in the `Accept` header
+6
src/__tests__/fetch.test.ts
··· 67 67 await expect(() => fetch('http://localhost:50000/')).rejects.toThrow(); 68 68 }, 1_000); 69 69 70 + it('should reject with error when connectTimeout is exceeded', async () => { 71 + await expect(() => 72 + fetch('http://10.255.255.1/', { connectTimeout: 100 }) 73 + ).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: Request timed out]`); 74 + }, 5_000); 75 + 70 76 it('should resolve into response', async () => { 71 77 const response = await fetch(new URL('hello', baseURL)); 72 78 expect(response.url).toBe(`${baseURL}hello`);
+7 -1
src/fetch.ts
··· 163 163 const requestHeaders = new Headers( 164 164 init?.headers ?? (initFromRequest ? input.headers : undefined) 165 165 ); 166 + 167 + let DEFAULT_TIMEOUT = 5_000; 168 + if (requestHeaders.get('accept')?.includes('text/html')) { 169 + DEFAULT_TIMEOUT = 30_000; 170 + } 171 + 166 172 const requestOptions = { 167 173 ...urlToHttpOptions(requestUrl), 168 - timeout: 5_000, 174 + timeout: init?.connectTimeout ?? DEFAULT_TIMEOUT, 169 175 method: methodToHttpOption(initFromRequest ? input.method : init?.method), 170 176 signal, 171 177 } satisfies http.RequestOptions;
+1
src/webstd.ts
··· 56 56 // Here, we have to account for global differences and split the overloads apart 57 57 58 58 interface _RequestInit extends Or<RequestInit, globalThis.RequestInit> { 59 + connectTimeout?: number; 59 60 duplex?: 'half'; 60 61 } 61 62 interface _ResponseInit extends Or<ResponseInit, globalThis.ResponseInit> {}