File size: 9,201 Bytes
cd6720a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import { canParseUrl } from './canParseUrl'
import { getValueBySymbol } from './getValueBySymbol'

interface UndiciRequestState extends RequestInit {}

interface FetchRequestInit extends Omit<RequestInit, 'mode'> {
  mode?: RequestMode | 'websocket' | 'webtransport'
  duplex?: 'half' | 'full'
}

export class FetchRequest extends Request {
  static #resolveProperty<T extends keyof FetchRequestInit & keyof Request>(
    input: RequestInfo | URL,
    init: FetchRequestInit = {},
    key: T
  ): FetchRequestInit[T] {
    return init[key] ?? (input instanceof Request ? input[key] : undefined)
  }

  /**
   * Check if the given request method is configurable.
   * @see https://fetch.spec.whatwg.org/#methods
   */
  static isConfigurableMethod(method: string): boolean {
    return method !== 'CONNECT' && method !== 'TRACE' && method !== 'TRACK'
  }

  static isMethodWithBody(method: string): boolean {
    return (
      method !== 'HEAD' &&
      method !== 'GET' &&
      FetchRequest.isConfigurableMethod(method)
    )
  }

  /**
   * Check if the given request `mode` is configurable.
   * @see https://fetch.spec.whatwg.org/#concept-request-mode
   */
  static isConfigurableMode(mode: string): boolean {
    return (
      mode !== 'navigate' && mode !== 'websocket' && mode !== 'webtransport'
    )
  }

  constructor(input: URL | RequestInfo, init?: FetchRequestInit) {
    const method = FetchRequest.#resolveProperty(input, init, 'method') || 'GET'
    const safeMethod = FetchRequest.isConfigurableMethod(method)
      ? method
      : 'GET'

    const hasExplicitBody = init != null && 'body' in init

    /**
     * Only include `body` in the super init when it needs to be overridden.
     * When `input` is a Request and no explicit body is in `init`, let the
     * Request constructor handle body transfer naturally so it properly
     * marks the original request's body as consumed (bodyUsed = true).
     */
    const bodyInit: { body?: BodyInit | null } = !FetchRequest.isMethodWithBody(
      method
    )
      ? { body: undefined }
      : hasExplicitBody
        ? { body: init.body }
        : {}

    const mode =
      (FetchRequest.#resolveProperty(input, init, 'mode') as RequestMode) ??
      undefined
    const safeMode = FetchRequest.isConfigurableMode(mode) ? mode : undefined

    super(input, {
      ...(init || {}),
      method: safeMethod,
      mode: safeMode,
      // @ts-expect-error Untyped Node.js property.
      duplex:
        init?.duplex ??
        (FetchRequest.isMethodWithBody(method) ? 'half' : undefined),
      ...bodyInit,
    })

    if (method !== safeMethod) {
      this.#setInternalProperty('method', method)
    }

    if (method === 'CONNECT') {
      const url = new URL(input instanceof Request ? input.url : input)

      let authority: string

      /**
       * @note Node.js has a bug parsing raw CONNECT requests URLs like
       * "http://127.0.0.1:1337/localhost:80". It would treat "localhost:" as a protocol.
       */
      if (url.protocol === 'localhost:') {
        authority = url.href
      } else {
        authority = url.pathname.replace(/^\/+/, '')
      }

      /**
       * @note Define "url" as a getter because Undici uses their own
       * logic to resolve the "request.url" property. Simply reassigning
       * its value doesn't do anything. This is a destructive action
       * but it's safe because "CONNECT" requests are forbidden per fetch.
       */
      Object.defineProperty(this, 'url', {
        get: () => authority,
        enumerable: true,
        configurable: true,
      })
    }

    if (mode != null && mode !== safeMode) {
      this.#setInternalProperty('mode', mode)
    }
  }

  #setInternalProperty<T extends keyof Request>(
    key: T,
    value: Request[T]
  ): void {
    const internalState = getValueBySymbol<UndiciRequestState>('state', this)

    if (internalState) {
      Reflect.set(internalState, key, value)
    } else {
      Object.defineProperty(this, key, {
        value,
        enumerable: true,
        configurable: true,
        writable: false,
      })
    }
  }
}

export interface FetchResponseInit extends ResponseInit {
  url?: string
}

interface UndiciResponseState {
  aborted: boolean
  rangeRequested: boolean
  timingAllowPassed: boolean
  requestIncludesCredentials: boolean
  type: ResponseType
  status: number
  statusText: string
  timingInfo: unknown
  cacheState: unknown
  headersList: Record<symbol, Map<string, unknown>>
  urlList: Array<URL>
  body?: {
    stream: ReadableStream
    source: unknown
    length: number
  }
}

const kStatus = Symbol('kStatus')
const kUrl = Symbol('kUrl')

export class FetchResponse extends Response {
  /**
   * Response status codes for responses that cannot have body.
   * @see https://fetch.spec.whatwg.org/#statuses
   */
  static readonly STATUS_CODES_WITHOUT_BODY = [101, 103, 204, 205, 304]

  static readonly STATUS_CODES_WITH_REDIRECT = [301, 302, 303, 307, 308]

  static isConfigurableStatusCode(status: number): boolean {
    return status >= 200 && status <= 599
  }

  static isRedirectResponse(status: number): boolean {
    return FetchResponse.STATUS_CODES_WITH_REDIRECT.includes(status)
  }

  /**
   * Returns a boolean indicating whether the given response status
   * code represents a response that can have a body.
   */
  static isResponseWithBody(status: number): boolean {
    return !FetchResponse.STATUS_CODES_WITHOUT_BODY.includes(status)
  }

  static setStatus(status: number, response: Response): void {
    /**
     * @note Undici keeps an internal "Symbol(state)" that holds
     * the actual value of response status. Update that in Node.js.
     */
    const internalState = getValueBySymbol<UndiciResponseState>(
      'state',
      response
    )

    if (internalState) {
      internalState.status = status
    } else {
      Object.defineProperty(response, 'status', {
        value: status,
        enumerable: true,
        configurable: true,
        writable: false,
      })
    }

    Object.defineProperty(response, kStatus, {
      value: status,
      enumerable: false,
    })
  }

  static setUrl(url: string | undefined, response: Response): void {
    if (!url || url === 'about:' || !canParseUrl(url)) {
      return
    }

    const state = getValueBySymbol<UndiciResponseState>('state', response)

    if (state) {
      // In Undici, push the URL to the internal list of URLs.
      // This will respect the `response.url` getter logic correctly.
      state.urlList.push(new URL(url))
    } else {
      // In other libraries, redefine the `url` property directly.
      Object.defineProperty(response, 'url', {
        value: url,
        enumerable: true,
        configurable: true,
        writable: false,
      })
    }

    Object.defineProperty(response, kUrl, {
      value: url,
      enumerable: false,
    })
  }

  /**
   * Parses the given raw HTTP headers into a Fetch API `Headers` instance.
   */
  static parseRawHeaders(rawHeaders: Array<string>): Headers {
    const headers = new Headers()

    for (let line = 0; line < rawHeaders.length; line += 2) {
      headers.append(rawHeaders[line], rawHeaders[line + 1])
    }

    return headers
  }

  /**
   * Safely clones the given `Response`.
   * Coerces response clone exceptions into 500 mocked responses.
   * Handy in the environments that introduce arbitrary response
   * cloning restrictions, like "101 Switching Protocols" cloning
   * in "miniflare".
   */
  static clone(response: Response): Response {
    try {
      const clone = response.clone()
      return clone
    } catch (error) {
      return Response.json(
        error instanceof Error
          ? {
              name: error.name,
              message: error.message,
              stack: error.stack,
            }
          : {},
        {
          status: 500,
          statusText: 'Unclonable Response',
        }
      )
    }
  }

  constructor(body?: BodyInit | null, init: FetchResponseInit = {}) {
    const status = init.status ?? 200
    const safeStatus = FetchResponse.isConfigurableStatusCode(status)
      ? status
      : 200
    const finalBody = FetchResponse.isResponseWithBody(status) ? body : null

    super(finalBody, {
      status: safeStatus,
      statusText: init.statusText,
      headers: init.headers,
    })

    /**
     * Since Node.js v24, Undici stores the Response state in an inaccessible field "#state".
     * Forward the modified status/URL to the cloned response manually.
     * @see https://github.com/nodejs/undici/blob/f734c87280e626c75f59aad55b65eb6a89cef392/lib/web/fetch/response.js#L242
     */
    if (status !== safeStatus) {
      FetchResponse.setStatus(status, this)
    }

    FetchResponse.setUrl(init.url, this)
  }

  public clone() {
    const clonedResponse = super.clone()

    const customStatus = Reflect.get(this, kStatus) as number | undefined

    if (customStatus) {
      FetchResponse.setStatus(customStatus, clonedResponse)
    }

    const customUrl = Reflect.get(this, kUrl) as string | undefined

    if (customUrl) {
      FetchResponse.setUrl(customUrl, clonedResponse)
    }

    return clonedResponse
  }
}