nock 14.0.0-beta.3 → 14.0.0-beta.4

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.
@@ -1,7 +1,9 @@
1
1
  'use strict'
2
2
 
3
+ const zlib = require('node:zlib')
3
4
  const { headersArrayToObject } = require('./common')
4
5
  const { STATUS_CODES } = require('http')
6
+ const { pipeline, Readable } = require('node:stream')
5
7
 
6
8
  /**
7
9
  * Creates a Fetch API `Response` instance from the given
@@ -16,17 +18,57 @@ const { STATUS_CODES } = require('http')
16
18
  const responseStatusCodesWithoutBody = [204, 205, 304]
17
19
 
18
20
  /**
19
- * @param {IncomingMessage} message
21
+ * @param {import('node:http').IncomingMessage} message
20
22
  */
21
23
  function createResponse(message) {
24
+ // https://github.com/Uzlopak/undici/blob/main/lib/fetch/index.js#L2031
25
+ const decoders = []
26
+ const codings =
27
+ message.headers['content-encoding']
28
+ ?.toLowerCase()
29
+ .split(',')
30
+ .map(x => x.trim())
31
+ .reverse() || []
32
+ for (const coding of codings) {
33
+ if (coding === 'gzip' || coding === 'x-gzip') {
34
+ decoders.push(
35
+ zlib.createGunzip({
36
+ flush: zlib.constants.Z_SYNC_FLUSH,
37
+ finishFlush: zlib.constants.Z_SYNC_FLUSH,
38
+ }),
39
+ )
40
+ } else if (coding === 'deflate') {
41
+ decoders.push(zlib.createInflate())
42
+ } else if (coding === 'br') {
43
+ decoders.push(zlib.createBrotliDecompress())
44
+ } else {
45
+ decoders.length = 0
46
+ break
47
+ }
48
+ }
49
+
50
+ const chunks = []
22
51
  const responseBodyOrNull = responseStatusCodesWithoutBody.includes(
23
52
  message.statusCode,
24
53
  )
25
54
  ? null
26
55
  : new ReadableStream({
27
56
  start(controller) {
28
- message.on('data', chunk => controller.enqueue(chunk))
29
- message.on('end', () => controller.close())
57
+ message.on('data', chunk => chunks.push(chunk))
58
+ message.on('end', () => {
59
+ pipeline(
60
+ Readable.from(chunks),
61
+ ...decoders,
62
+ async function* (source) {
63
+ for await (const chunk of source) {
64
+ yield controller.enqueue(chunk)
65
+ }
66
+ },
67
+ error => {
68
+ error ? controller.error(error) : controller.close()
69
+ },
70
+ )
71
+ })
30
72
 
31
73
  /**
32
74
  * @todo Should also listen to the "error" on the message
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "testing",
8
8
  "isolation"
9
9
  ],
10
- "version": "14.0.0-beta.3",
10
+ "version": "14.0.0-beta.4",
11
11
  "author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
12
12
  "repository": {
13
13
  "type": "git",
@@ -17,7 +17,7 @@
17
17
  "url": "https://github.com/nock/nock/issues"
18
18
  },
19
19
  "engines": {
20
- "node": ">= 10.13"
20
+ "node": ">= 18"
21
21
  },
22
22
  "main": "./index.js",
23
23
  "types": "types",