|
| 1 | +// Based on Remix's implementation of Fetch API |
| 2 | +// https://github.com/remix-run/web-std-io/tree/main/packages/fetch |
| 3 | + |
| 4 | +import { RemixRequest } from './types'; |
| 5 | + |
| 6 | +/* |
| 7 | + * Symbol extractor utility to be able to access internal fields of Remix requests. |
| 8 | + */ |
| 9 | +const getInternalSymbols = ( |
| 10 | + request: Record<string, unknown>, |
| 11 | +): { |
| 12 | + bodyInternalsSymbol: string; |
| 13 | + requestInternalsSymbol: string; |
| 14 | +} => { |
| 15 | + const symbols = Object.getOwnPropertySymbols(request); |
| 16 | + return { |
| 17 | + bodyInternalsSymbol: symbols.find(symbol => symbol.toString().includes('Body internals')) as any, |
| 18 | + requestInternalsSymbol: symbols.find(symbol => symbol.toString().includes('Request internals')) as any, |
| 19 | + }; |
| 20 | +}; |
| 21 | + |
| 22 | +/** |
| 23 | + * Vendored from: |
| 24 | + * https://github.com/remix-run/web-std-io/blob/f715b354c8c5b8edc550c5442dec5712705e25e7/packages/fetch/src/utils/get-search.js#L5 |
| 25 | + */ |
| 26 | +export const getSearch = (parsedURL: URL): string => { |
| 27 | + if (parsedURL.search) { |
| 28 | + return parsedURL.search; |
| 29 | + } |
| 30 | + |
| 31 | + const lastOffset = parsedURL.href.length - 1; |
| 32 | + const hash = parsedURL.hash || (parsedURL.href[lastOffset] === '#' ? '#' : ''); |
| 33 | + return parsedURL.href[lastOffset - hash.length] === '?' ? '?' : ''; |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * Convert a Request to Node.js http request options. |
| 38 | + * The options object to be passed to http.request |
| 39 | + * Vendored / modified from: |
| 40 | + * https://github.com/remix-run/web-std-io/blob/f715b354c8c5b8edc550c5442dec5712705e25e7/packages/fetch/src/request.js#L259 |
| 41 | + */ |
| 42 | +export const normalizeRemixRequest = (request: RemixRequest): Record<string, any> => { |
| 43 | + const { requestInternalsSymbol, bodyInternalsSymbol } = getInternalSymbols(request); |
| 44 | + |
| 45 | + if (!requestInternalsSymbol) { |
| 46 | + throw new Error('Could not find request internals symbol'); |
| 47 | + } |
| 48 | + |
| 49 | + const { parsedURL } = request[requestInternalsSymbol]; |
| 50 | + const headers = new Headers(request[requestInternalsSymbol].headers); |
| 51 | + |
| 52 | + // Fetch step 1.3 |
| 53 | + if (!headers.has('Accept')) { |
| 54 | + headers.set('Accept', '*/*'); |
| 55 | + } |
| 56 | + |
| 57 | + // HTTP-network-or-cache fetch steps 2.4-2.7 |
| 58 | + let contentLengthValue = null; |
| 59 | + if (request.body === null && /^(post|put)$/i.test(request.method)) { |
| 60 | + contentLengthValue = '0'; |
| 61 | + } |
| 62 | + |
| 63 | + if (request.body !== null) { |
| 64 | + const totalBytes = request[bodyInternalsSymbol].size; |
| 65 | + // Set Content-Length if totalBytes is a number (that is not NaN) |
| 66 | + if (typeof totalBytes === 'number' && !Number.isNaN(totalBytes)) { |
| 67 | + contentLengthValue = String(totalBytes); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if (contentLengthValue) { |
| 72 | + headers.set('Content-Length', contentLengthValue); |
| 73 | + } |
| 74 | + |
| 75 | + // HTTP-network-or-cache fetch step 2.11 |
| 76 | + if (!headers.has('User-Agent')) { |
| 77 | + headers.set('User-Agent', 'node-fetch'); |
| 78 | + } |
| 79 | + |
| 80 | + // HTTP-network-or-cache fetch step 2.15 |
| 81 | + if (request.compress && !headers.has('Accept-Encoding')) { |
| 82 | + headers.set('Accept-Encoding', 'gzip,deflate,br'); |
| 83 | + } |
| 84 | + |
| 85 | + let { agent } = request; |
| 86 | + |
| 87 | + if (typeof agent === 'function') { |
| 88 | + agent = agent(parsedURL); |
| 89 | + } |
| 90 | + |
| 91 | + if (!headers.has('Connection') && !agent) { |
| 92 | + headers.set('Connection', 'close'); |
| 93 | + } |
| 94 | + |
| 95 | + // HTTP-network fetch step 4.2 |
| 96 | + // chunked encoding is handled by Node.js |
| 97 | + const search = getSearch(parsedURL); |
| 98 | + |
| 99 | + // Manually spread the URL object instead of spread syntax |
| 100 | + const requestOptions = { |
| 101 | + path: parsedURL.pathname + search, |
| 102 | + pathname: parsedURL.pathname, |
| 103 | + hostname: parsedURL.hostname, |
| 104 | + protocol: parsedURL.protocol, |
| 105 | + port: parsedURL.port, |
| 106 | + hash: parsedURL.hash, |
| 107 | + search: parsedURL.search, |
| 108 | + // @ts-ignore - it does not has a query |
| 109 | + query: parsedURL.query, |
| 110 | + href: parsedURL.href, |
| 111 | + method: request.method, |
| 112 | + // @ts-ignore - not sure what this supposed to do |
| 113 | + headers: headers[Symbol.for('nodejs.util.inspect.custom')](), |
| 114 | + insecureHTTPParser: request.insecureHTTPParser, |
| 115 | + agent, |
| 116 | + |
| 117 | + // [SENTRY] For compatibility with Sentry SDK RequestData parser, adding `originalUrl` property. |
| 118 | + originalUrl: parsedURL.href, |
| 119 | + }; |
| 120 | + |
| 121 | + return requestOptions; |
| 122 | +}; |
0 commit comments