undici 6.16.0 → 6.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -44,6 +44,7 @@ class RequestHandler extends AsyncResource {
44
44
  throw err
45
45
  }
46
46
 
47
+ this.method = method
47
48
  this.responseHeaders = responseHeaders || null
48
49
  this.opaque = opaque || null
49
50
  this.callback = callback
@@ -114,7 +115,15 @@ class RequestHandler extends AsyncResource {
114
115
  const parsedHeaders = responseHeaders === 'raw' ? util.parseHeaders(rawHeaders) : headers
115
116
  const contentType = parsedHeaders['content-type']
116
117
  const contentLength = parsedHeaders['content-length']
117
- const res = new Readable({ resume, abort, contentType, contentLength, highWaterMark })
118
+ const res = new Readable({
119
+ resume,
120
+ abort,
121
+ contentType,
122
+ contentLength: this.method !== 'HEAD' && contentLength
123
+ ? Number(contentLength)
124
+ : null,
125
+ highWaterMark
126
+ })
118
127
 
119
128
  if (this.removeAbortListener) {
120
129
  res.on('close', this.removeAbortListener)
@@ -165,7 +165,7 @@ function setupTimeout (onConnectTimeout, timeout) {
165
165
  let s1 = null
166
166
  let s2 = null
167
167
  const timeoutId = setTimeout(() => {
168
- // setImmediate is added to make sure that we priotorise socket error events over timeouts
168
+ // setImmediate is added to make sure that we prioritize socket error events over timeouts
169
169
  s1 = setImmediate(() => {
170
170
  if (process.platform === 'win32') {
171
171
  // Windows needs an extra setImmediate probably due to implementation differences in the socket logic
@@ -444,8 +444,7 @@ function fetching ({
444
444
  // 9. If request’s origin is "client", then set request’s origin to request’s
445
445
  // client’s origin.
446
446
  if (request.origin === 'client') {
447
- // TODO: What if request.client is null?
448
- request.origin = request.client?.origin
447
+ request.origin = request.client.origin
449
448
  }
450
449
 
451
450
  // 10. If all of the following conditions are true:
@@ -255,16 +255,23 @@ function appendFetchMetadata (httpRequest) {
255
255
 
256
256
  // https://fetch.spec.whatwg.org/#append-a-request-origin-header
257
257
  function appendRequestOriginHeader (request) {
258
- // 1. Let serializedOrigin be the result of byte-serializing a request origin with request.
258
+ // 1. Let serializedOrigin be the result of byte-serializing a request origin
259
+ // with request.
260
+ // TODO: implement "byte-serializing a request origin"
259
261
  let serializedOrigin = request.origin
260
262
 
261
- // 2. If request’s response tainting is "cors" or request’s mode is "websocket", then append (`Origin`, serializedOrigin) to request’s header list.
262
- if (request.responseTainting === 'cors' || request.mode === 'websocket') {
263
- if (serializedOrigin) {
264
- request.headersList.append('origin', serializedOrigin, true)
265
- }
263
+ // "'client' is changed to an origin during fetching."
264
+ // This doesn't happen in undici (in most cases) because undici, by default,
265
+ // has no concept of origin.
266
+ if (serializedOrigin === 'client') {
267
+ return
268
+ }
266
269
 
270
+ // 2. If request’s response tainting is "cors" or request’s mode is "websocket",
271
+ // then append (`Origin`, serializedOrigin) to request’s header list.
267
272
  // 3. Otherwise, if request’s method is neither `GET` nor `HEAD`, then:
273
+ if (request.responseTainting === 'cors' || request.mode === 'websocket') {
274
+ request.headersList.append('origin', serializedOrigin, true)
268
275
  } else if (request.method !== 'GET' && request.method !== 'HEAD') {
269
276
  // 1. Switch on request’s referrer policy:
270
277
  switch (request.referrerPolicy) {
@@ -275,13 +282,16 @@ function appendRequestOriginHeader (request) {
275
282
  case 'no-referrer-when-downgrade':
276
283
  case 'strict-origin':
277
284
  case 'strict-origin-when-cross-origin':
278
- // If request’s origin is a tuple origin, its scheme is "https", and request’s current URL’s scheme is not "https", then set serializedOrigin to `null`.
285
+ // If request’s origin is a tuple origin, its scheme is "https", and
286
+ // request’s current URL’s scheme is not "https", then set
287
+ // serializedOrigin to `null`.
279
288
  if (request.origin && urlHasHttpsScheme(request.origin) && !urlHasHttpsScheme(requestCurrentURL(request))) {
280
289
  serializedOrigin = null
281
290
  }
282
291
  break
283
292
  case 'same-origin':
284
- // If request’s origin is not same origin with request’s current URL’s origin, then set serializedOrigin to `null`.
293
+ // If request’s origin is not same origin with request’s current URL’s
294
+ // origin, then set serializedOrigin to `null`.
285
295
  if (!sameOrigin(request, requestCurrentURL(request))) {
286
296
  serializedOrigin = null
287
297
  }
@@ -290,10 +300,8 @@ function appendRequestOriginHeader (request) {
290
300
  // Do nothing.
291
301
  }
292
302
 
293
- if (serializedOrigin) {
294
- // 2. Append (`Origin`, serializedOrigin) to request’s header list.
295
- request.headersList.append('origin', serializedOrigin, true)
296
- }
303
+ // 2. Append (`Origin`, serializedOrigin) to request’s header list.
304
+ request.headersList.append('origin', serializedOrigin, true)
297
305
  }
298
306
  }
299
307
 
@@ -35,7 +35,7 @@ try {
35
35
  * @param {(response: any) => void} onEstablish
36
36
  * @param {Partial<import('../../types/websocket').WebSocketInit>} options
37
37
  */
38
- function establishWebSocketConnection (url, protocols, ws, onEstablish, options) {
38
+ function establishWebSocketConnection (url, protocols, client, ws, onEstablish, options) {
39
39
  // 1. Let requestURL be a copy of url, with its scheme set to "http", if url’s
40
40
  // scheme is "ws", and to "https" otherwise.
41
41
  const requestURL = url
@@ -48,6 +48,7 @@ function establishWebSocketConnection (url, protocols, ws, onEstablish, options)
48
48
  // and redirect mode is "error".
49
49
  const request = makeRequest({
50
50
  urlList: [requestURL],
51
+ client,
51
52
  serviceWorkers: 'none',
52
53
  referrer: 'no-referrer',
53
54
  mode: 'websocket',
@@ -212,7 +212,7 @@ class ByteParser extends Writable {
212
212
  const buffer = this.consume(8)
213
213
  const upper = buffer.readUInt32BE(0)
214
214
 
215
- // 2^31 is the maxinimum bytes an arraybuffer can contain
215
+ // 2^31 is the maximum bytes an arraybuffer can contain
216
216
  // on 32-bit systems. Although, on 64-bit systems, this is
217
217
  // 2^53-1 bytes.
218
218
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Invalid_array_length
@@ -314,6 +314,10 @@ class ByteParser extends Writable {
314
314
  code = data.readUInt16BE(0)
315
315
  }
316
316
 
317
+ if (code !== undefined && !isValidStatusCode(code)) {
318
+ return { code: 1002, reason: 'Invalid status code', error: true }
319
+ }
320
+
317
321
  // https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.6
318
322
  /** @type {Buffer} */
319
323
  let reason = data.subarray(2)
@@ -323,10 +327,6 @@ class ByteParser extends Writable {
323
327
  reason = reason.subarray(3)
324
328
  }
325
329
 
326
- if (code !== undefined && !isValidStatusCode(code)) {
327
- return { code: 1002, reason: 'Invalid status code', error: true }
328
- }
329
-
330
330
  try {
331
331
  reason = utf8Decode(reason)
332
332
  } catch {
@@ -124,6 +124,7 @@ class WebSocket extends EventTarget {
124
124
  this[kWebSocketURL] = new URL(urlRecord.href)
125
125
 
126
126
  // 11. Let client be this's relevant settings object.
127
+ const client = environmentSettingsObject.settingsObject
127
128
 
128
129
  // 12. Run this step in parallel:
129
130
 
@@ -132,6 +133,7 @@ class WebSocket extends EventTarget {
132
133
  this[kController] = establishWebSocketConnection(
133
134
  urlRecord,
134
135
  protocols,
136
+ client,
135
137
  this,
136
138
  (response) => this.#onConnectionEstablished(response),
137
139
  options
@@ -547,7 +549,7 @@ webidl.converters['DOMString or sequence<DOMString>'] = function (V, prefix, arg
547
549
  return webidl.converters.DOMString(V, prefix, argument)
548
550
  }
549
551
 
550
- // This implements the propsal made in https://github.com/whatwg/websockets/issues/42
552
+ // This implements the proposal made in https://github.com/whatwg/websockets/issues/42
551
553
  webidl.converters.WebSocketInit = webidl.dictionaryConverter([
552
554
  {
553
555
  key: 'protocols',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "undici",
3
- "version": "6.16.0",
3
+ "version": "6.16.1",
4
4
  "description": "An HTTP/1.1 client, written from scratch for Node.js",
5
5
  "homepage": "https://undici.nodejs.org",
6
6
  "bugs": {