rekwest 6.0.1 → 6.2.0
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.
- package/README.md +8 -4
- package/dist/config.js +13 -0
- package/dist/mixin.js +4 -1
- package/dist/transform.js +2 -2
- package/dist/utils.js +16 -12
- package/package.json +8 -8
- package/src/config.mjs +13 -0
- package/src/mixin.mjs +117 -117
- package/src/transform.mjs +2 -2
- package/src/utils.mjs +12 -12
package/README.md
CHANGED
|
@@ -120,6 +120,8 @@ console.log(res.body);
|
|
|
120
120
|
& [http2.ClientSessionRequestOptions](https://nodejs.org/api/http2.html#clienthttp2sessionrequestheaders-options)
|
|
121
121
|
and [tls.ConnectionOptions](https://nodejs.org/api/tls.html#tlsconnectoptions-callback)
|
|
122
122
|
for HTTP/2 attunes
|
|
123
|
+
* `compression` **{Object}** Configures compression options, e.g.: `brotliOptions`, `zstdOptions`,
|
|
124
|
+
`zlibOptions`
|
|
123
125
|
* `baseURL` **{string | URL}** The base URL to use in cases where `url` is a relative URL
|
|
124
126
|
* `body` **{string | Array | ArrayBuffer | ArrayBufferView | AsyncIterator | Blob | Buffer | DataView | File |
|
|
125
127
|
FormData | Iterator | Object | Readable | ReadableStream | SharedArrayBuffer | URLSearchParams}** The body to send
|
|
@@ -130,12 +132,14 @@ console.log(res.body);
|
|
|
130
132
|
* `cookiesTTL` **{boolean}** `Default: false` Controls enablement of TTL for the cookies cache
|
|
131
133
|
* `credentials` **{include | omit | same-origin}** `Default: same-origin` Controls credentials in case of cross-origin
|
|
132
134
|
redirects
|
|
133
|
-
* `
|
|
135
|
+
* `decompression` **{Object}** Configures decompression options, e.g.: `brotliOptions`, `zstdOptions`,
|
|
136
|
+
`zlibOptions`
|
|
137
|
+
* `digest` **{boolean}** `Default: true` Controls whether to read the response stream or add a mixin
|
|
134
138
|
* `follow` **{number}** `Default: 20` The number of redirects to follow
|
|
135
139
|
* `h2` **{boolean}** `Default: false` Forces the use of HTTP/2 protocol
|
|
136
140
|
* `headers` **{Object}** The headers to add to the request
|
|
137
141
|
* `maxRetryAfter` **{number}** The upper limit of `retry-after` header. If unset, it will use `timeout` value
|
|
138
|
-
* `parse` **{boolean}** `Default: true` Controls whether to parse response body or
|
|
142
|
+
* `parse` **{boolean}** `Default: true` Controls whether to parse response body or return a buffer
|
|
139
143
|
* `redirect` **{error | follow | manual}** `Default: follow` Controls the redirect flows
|
|
140
144
|
* `retry` **{Object}** Represents the retry options
|
|
141
145
|
* `attempts` **{number}** `Default: 0` The number of retry attempts
|
|
@@ -164,10 +168,10 @@ console.log(res.body);
|
|
|
164
168
|
* `bytes` **{AsyncFunction}** Reads the response and returns **Uint8Array**
|
|
165
169
|
* `json` **{AsyncFunction}** Reads the response and returns **Object**
|
|
166
170
|
* `text` **{AsyncFunction}** Reads the response and returns **String**
|
|
167
|
-
* `bodyUsed` **{boolean}** Indicates whether the response
|
|
171
|
+
* `bodyUsed` **{boolean}** Indicates whether the response was read or not
|
|
168
172
|
* `cookies` **{undefined | Cookies}** The cookies sent and received with the response
|
|
169
173
|
* `headers` **{Object}** The headers received with the response
|
|
170
|
-
* `httpVersion` **{string}** Indicates protocol version negotiated with the server
|
|
174
|
+
* `httpVersion` **{string}** Indicates a protocol version negotiated with the server
|
|
171
175
|
* `ok` **{boolean}** Indicates if the response was successful (statusCode: **200-299**)
|
|
172
176
|
* `redirected` **{boolean}** Indicates if the response is the result of a redirect
|
|
173
177
|
* `statusCode` **{number}** Indicates the status code of the response
|
package/dist/config.js
CHANGED
|
@@ -22,8 +22,21 @@ const {
|
|
|
22
22
|
} = _nodeHttp.default.constants;
|
|
23
23
|
const isZstdSupported = exports.isZstdSupported = !!_nodeZlib.default.constants.ZSTD_CLEVEL_DEFAULT;
|
|
24
24
|
const defaults = {
|
|
25
|
+
compression: {
|
|
26
|
+
brotliOptions: {
|
|
27
|
+
params: {
|
|
28
|
+
[_nodeZlib.default.constants.BROTLI_PARAM_QUALITY]: 4
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
zstdOptions: {
|
|
32
|
+
params: {
|
|
33
|
+
[_nodeZlib.default.constants.ZSTD_c_compressionLevel]: 6
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
},
|
|
25
37
|
cookiesTTL: false,
|
|
26
38
|
credentials: _constants.requestCredentials.sameOrigin,
|
|
39
|
+
decompression: {},
|
|
27
40
|
digest: true,
|
|
28
41
|
follow: 20,
|
|
29
42
|
h2: false,
|
package/dist/mixin.js
CHANGED
|
@@ -13,6 +13,7 @@ const {
|
|
|
13
13
|
HTTP2_HEADER_CONTENT_TYPE
|
|
14
14
|
} = _nodeHttp.default.constants;
|
|
15
15
|
const mixin = (res, {
|
|
16
|
+
decompression,
|
|
16
17
|
digest = false,
|
|
17
18
|
parse = false
|
|
18
19
|
} = {}) => {
|
|
@@ -73,7 +74,9 @@ const mixin = (res, {
|
|
|
73
74
|
throw new TypeError('Response stream already read');
|
|
74
75
|
}
|
|
75
76
|
let body = [];
|
|
76
|
-
for await (const chunk of (0, _utils.decompress)(this, this.headers[HTTP2_HEADER_CONTENT_ENCODING]
|
|
77
|
+
for await (const chunk of (0, _utils.decompress)(this, this.headers[HTTP2_HEADER_CONTENT_ENCODING], {
|
|
78
|
+
decompression
|
|
79
|
+
})) {
|
|
77
80
|
body.push(chunk);
|
|
78
81
|
}
|
|
79
82
|
body = Buffer.concat(body);
|
package/dist/transform.js
CHANGED
|
@@ -58,9 +58,9 @@ const transform = async options => {
|
|
|
58
58
|
const encodings = options.headers[HTTP2_HEADER_CONTENT_ENCODING];
|
|
59
59
|
if (body === Object(body) && (Reflect.has(body, Symbol.asyncIterator) || !Array.isArray(body) && Reflect.has(body, Symbol.iterator))) {
|
|
60
60
|
body = (0, _nodeStream.isReadable)(body) ? (0, _utils.isReadableStream)(body) ? _nodeStream.Readable.fromWeb(body) : body : _nodeStream.Readable.from(body);
|
|
61
|
-
body = encodings ? (0, _utils.compress)(body, encodings) : body;
|
|
61
|
+
body = encodings ? (0, _utils.compress)(body, encodings, options) : body;
|
|
62
62
|
} else if (encodings) {
|
|
63
|
-
body = await (0, _consumers.buffer)((0, _utils.compress)(_nodeStream.Readable.from(body), encodings));
|
|
63
|
+
body = await (0, _consumers.buffer)((0, _utils.compress)(_nodeStream.Readable.from(body), encodings, options));
|
|
64
64
|
}
|
|
65
65
|
Object.assign(options.headers, {
|
|
66
66
|
...headers,
|
package/dist/utils.js
CHANGED
|
@@ -64,20 +64,22 @@ const brandCheck = (value, ctor) => {
|
|
|
64
64
|
}
|
|
65
65
|
};
|
|
66
66
|
exports.brandCheck = brandCheck;
|
|
67
|
-
const compress = (readable, encodings = ''
|
|
67
|
+
const compress = (readable, encodings = '', {
|
|
68
|
+
compression
|
|
69
|
+
} = {}) => {
|
|
68
70
|
const encoders = [];
|
|
69
71
|
encodings = unwind(encodings);
|
|
70
72
|
for (const encoding of encodings) {
|
|
71
73
|
if (/\bbr\b/i.test(encoding)) {
|
|
72
|
-
encoders.push(_nodeZlib.default.createBrotliCompress());
|
|
74
|
+
encoders.push(_nodeZlib.default.createBrotliCompress(compression?.brotliOptions));
|
|
73
75
|
} else if (/\bdeflate(?!-(?:\w+)?)\b/i.test(encoding)) {
|
|
74
|
-
encoders.push(_nodeZlib.default.createDeflate());
|
|
76
|
+
encoders.push(_nodeZlib.default.createDeflate(compression?.zlibOptions));
|
|
75
77
|
} else if (/\bdeflate-raw\b/i.test(encoding)) {
|
|
76
|
-
encoders.push(_nodeZlib.default.createDeflateRaw());
|
|
78
|
+
encoders.push(_nodeZlib.default.createDeflateRaw(compression?.zlibOptions));
|
|
77
79
|
} else if (/\bgzip\b/i.test(encoding)) {
|
|
78
|
-
encoders.push(_nodeZlib.default.createGzip());
|
|
80
|
+
encoders.push(_nodeZlib.default.createGzip(compression?.zlibOptions));
|
|
79
81
|
} else if (_config.isZstdSupported && /\bzstd\b/i.test(encoding)) {
|
|
80
|
-
encoders.push(_nodeZlib.default.createZstdCompress());
|
|
82
|
+
encoders.push(_nodeZlib.default.createZstdCompress(compression?.zstdOptions));
|
|
81
83
|
} else {
|
|
82
84
|
return readable;
|
|
83
85
|
}
|
|
@@ -93,20 +95,22 @@ const copyWithMerge = (target, ...rest) => {
|
|
|
93
95
|
return merge(target, ...rest);
|
|
94
96
|
};
|
|
95
97
|
exports.copyWithMerge = copyWithMerge;
|
|
96
|
-
const decompress = (readable, encodings = ''
|
|
98
|
+
const decompress = (readable, encodings = '', {
|
|
99
|
+
decompression
|
|
100
|
+
} = {}) => {
|
|
97
101
|
const decoders = [];
|
|
98
102
|
encodings = unwind(encodings);
|
|
99
103
|
for (const encoding of encodings) {
|
|
100
104
|
if (/\bbr\b/i.test(encoding)) {
|
|
101
|
-
decoders.push(_nodeZlib.default.createBrotliDecompress());
|
|
105
|
+
decoders.push(_nodeZlib.default.createBrotliDecompress(decompression?.brotliOptions));
|
|
102
106
|
} else if (/\bdeflate(?!-(?:\w+)?)\b/i.test(encoding)) {
|
|
103
|
-
decoders.push(_nodeZlib.default.createInflate());
|
|
107
|
+
decoders.push(_nodeZlib.default.createInflate(decompression?.zlibOptions));
|
|
104
108
|
} else if (/\bdeflate-raw\b/i.test(encoding)) {
|
|
105
|
-
decoders.push(_nodeZlib.default.createInflateRaw());
|
|
109
|
+
decoders.push(_nodeZlib.default.createInflateRaw(decompression?.zlibOptions));
|
|
106
110
|
} else if (/\bgzip\b/i.test(encoding)) {
|
|
107
|
-
decoders.push(_nodeZlib.default.createGunzip());
|
|
111
|
+
decoders.push(_nodeZlib.default.createGunzip(decompression?.zlibOptions));
|
|
108
112
|
} else if (_config.isZstdSupported && /\bzstd\b/i.test(encoding)) {
|
|
109
|
-
decoders.push(_nodeZlib.default.createZstdDecompress());
|
|
113
|
+
decoders.push(_nodeZlib.default.createZstdDecompress(decompression?.zstdOptions));
|
|
110
114
|
} else {
|
|
111
115
|
return readable;
|
|
112
116
|
}
|
package/package.json
CHANGED
|
@@ -8,14 +8,14 @@
|
|
|
8
8
|
"url": "https://github.com/bricss/rekwest/issues"
|
|
9
9
|
},
|
|
10
10
|
"devDependencies": {
|
|
11
|
-
"@babel/cli": "^7.28.
|
|
12
|
-
"@babel/core": "^7.28.
|
|
13
|
-
"@babel/eslint-parser": "^7.28.
|
|
14
|
-
"@babel/preset-env": "^7.28.
|
|
11
|
+
"@babel/cli": "^7.28.3",
|
|
12
|
+
"@babel/core": "^7.28.4",
|
|
13
|
+
"@babel/eslint-parser": "^7.28.4",
|
|
14
|
+
"@babel/preset-env": "^7.28.3",
|
|
15
15
|
"c8": "^10.1.3",
|
|
16
|
-
"eslint": "^9.
|
|
17
|
-
"eslint-config-ultra-refined": "^3.
|
|
18
|
-
"mocha": "^11.7.
|
|
16
|
+
"eslint": "^9.35.0",
|
|
17
|
+
"eslint-config-ultra-refined": "^3.8.0",
|
|
18
|
+
"mocha": "^11.7.2"
|
|
19
19
|
},
|
|
20
20
|
"description": "The robust request library that humanity deserves 🌐",
|
|
21
21
|
"engines": {
|
|
@@ -71,5 +71,5 @@
|
|
|
71
71
|
"test:bail": "mocha --bail",
|
|
72
72
|
"test:cover": "c8 --include=src --reporter=lcov --reporter=text npm test"
|
|
73
73
|
},
|
|
74
|
-
"version": "6.0
|
|
74
|
+
"version": "6.2.0"
|
|
75
75
|
}
|
package/src/config.mjs
CHANGED
|
@@ -25,8 +25,21 @@ const {
|
|
|
25
25
|
export const isZstdSupported = !!zlib.constants.ZSTD_CLEVEL_DEFAULT;
|
|
26
26
|
|
|
27
27
|
const defaults = {
|
|
28
|
+
compression: {
|
|
29
|
+
brotliOptions: {
|
|
30
|
+
params: {
|
|
31
|
+
[zlib.constants.BROTLI_PARAM_QUALITY]: 4,
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
zstdOptions: {
|
|
35
|
+
params: {
|
|
36
|
+
[zlib.constants.ZSTD_c_compressionLevel]: 6,
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
},
|
|
28
40
|
cookiesTTL: false,
|
|
29
41
|
credentials: requestCredentials.sameOrigin,
|
|
42
|
+
decompression: {},
|
|
30
43
|
digest: true,
|
|
31
44
|
follow: 20,
|
|
32
45
|
h2: false,
|
package/src/mixin.mjs
CHANGED
|
@@ -1,117 +1,117 @@
|
|
|
1
|
-
import { Blob } from 'node:buffer';
|
|
2
|
-
import http2 from 'node:http2';
|
|
3
|
-
import {
|
|
4
|
-
brandCheck,
|
|
5
|
-
decompress,
|
|
6
|
-
} from './utils.mjs';
|
|
7
|
-
|
|
8
|
-
const {
|
|
9
|
-
HTTP2_HEADER_CONTENT_ENCODING,
|
|
10
|
-
HTTP2_HEADER_CONTENT_TYPE,
|
|
11
|
-
} = http2.constants;
|
|
12
|
-
|
|
13
|
-
export const mixin = (res, { digest = false, parse = false } = {}) => {
|
|
14
|
-
if (!digest) {
|
|
15
|
-
Object.defineProperties(res, {
|
|
16
|
-
arrayBuffer: {
|
|
17
|
-
enumerable: true,
|
|
18
|
-
value: async function () {
|
|
19
|
-
brandCheck(this, res?.constructor);
|
|
20
|
-
parse &&= false;
|
|
21
|
-
const { buffer, byteLength, byteOffset } = await this.body();
|
|
22
|
-
|
|
23
|
-
return buffer.slice(byteOffset, byteOffset + byteLength);
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
blob: {
|
|
27
|
-
enumerable: true,
|
|
28
|
-
value: async function () {
|
|
29
|
-
brandCheck(this, res?.constructor);
|
|
30
|
-
const val = await this.arrayBuffer();
|
|
31
|
-
|
|
32
|
-
return new Blob([val]);
|
|
33
|
-
},
|
|
34
|
-
},
|
|
35
|
-
bytes: {
|
|
36
|
-
enumerable: true,
|
|
37
|
-
value: async function () {
|
|
38
|
-
brandCheck(this, res?.constructor);
|
|
39
|
-
|
|
40
|
-
return new Uint8Array(await this.arrayBuffer());
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
json: {
|
|
44
|
-
enumerable: true,
|
|
45
|
-
value: async function () {
|
|
46
|
-
brandCheck(this, res?.constructor);
|
|
47
|
-
const val = await this.text();
|
|
48
|
-
|
|
49
|
-
return JSON.parse(val);
|
|
50
|
-
},
|
|
51
|
-
},
|
|
52
|
-
text: {
|
|
53
|
-
enumerable: true,
|
|
54
|
-
value: async function () {
|
|
55
|
-
brandCheck(this, res?.constructor);
|
|
56
|
-
const blob = await this.blob();
|
|
57
|
-
|
|
58
|
-
return blob.text();
|
|
59
|
-
},
|
|
60
|
-
},
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return Object.defineProperties(res, {
|
|
65
|
-
body: {
|
|
66
|
-
enumerable: true,
|
|
67
|
-
value: async function () {
|
|
68
|
-
brandCheck(this, res?.constructor);
|
|
69
|
-
|
|
70
|
-
if (this.bodyUsed) {
|
|
71
|
-
throw new TypeError('Response stream already read');
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
let body = [];
|
|
75
|
-
|
|
76
|
-
for await (const chunk of decompress(this, this.headers[HTTP2_HEADER_CONTENT_ENCODING])) {
|
|
77
|
-
body.push(chunk);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
body = Buffer.concat(body);
|
|
81
|
-
|
|
82
|
-
if (!body.length && parse) {
|
|
83
|
-
return null;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (body.length && parse) {
|
|
87
|
-
const contentType = this.headers[HTTP2_HEADER_CONTENT_TYPE] ?? '';
|
|
88
|
-
const charset = contentType.split(';')
|
|
89
|
-
.find((it) => /charset=/i.test(it))
|
|
90
|
-
?.toLowerCase()
|
|
91
|
-
.replace('charset=', '')
|
|
92
|
-
.replace('iso-8859-1', 'latin1')
|
|
93
|
-
.trim() || 'utf-8';
|
|
94
|
-
|
|
95
|
-
if (/\bjson\b/i.test(contentType)) {
|
|
96
|
-
body = JSON.parse(body.toString(charset));
|
|
97
|
-
} else if (/\b(?:text|xml)\b/i.test(contentType)) {
|
|
98
|
-
if (/\b(?:latin1|ucs-2|utf-(?:8|16le))\b/i.test(charset)) {
|
|
99
|
-
body = body.toString(charset);
|
|
100
|
-
} else {
|
|
101
|
-
body = new TextDecoder(charset).decode(body);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return body;
|
|
107
|
-
},
|
|
108
|
-
writable: true,
|
|
109
|
-
},
|
|
110
|
-
bodyUsed: {
|
|
111
|
-
enumerable: true,
|
|
112
|
-
get() {
|
|
113
|
-
return this.readableEnded;
|
|
114
|
-
},
|
|
115
|
-
},
|
|
116
|
-
});
|
|
117
|
-
};
|
|
1
|
+
import { Blob } from 'node:buffer';
|
|
2
|
+
import http2 from 'node:http2';
|
|
3
|
+
import {
|
|
4
|
+
brandCheck,
|
|
5
|
+
decompress,
|
|
6
|
+
} from './utils.mjs';
|
|
7
|
+
|
|
8
|
+
const {
|
|
9
|
+
HTTP2_HEADER_CONTENT_ENCODING,
|
|
10
|
+
HTTP2_HEADER_CONTENT_TYPE,
|
|
11
|
+
} = http2.constants;
|
|
12
|
+
|
|
13
|
+
export const mixin = (res, { decompression, digest = false, parse = false } = {}) => {
|
|
14
|
+
if (!digest) {
|
|
15
|
+
Object.defineProperties(res, {
|
|
16
|
+
arrayBuffer: {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
value: async function () {
|
|
19
|
+
brandCheck(this, res?.constructor);
|
|
20
|
+
parse &&= false;
|
|
21
|
+
const { buffer, byteLength, byteOffset } = await this.body();
|
|
22
|
+
|
|
23
|
+
return buffer.slice(byteOffset, byteOffset + byteLength);
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
blob: {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
value: async function () {
|
|
29
|
+
brandCheck(this, res?.constructor);
|
|
30
|
+
const val = await this.arrayBuffer();
|
|
31
|
+
|
|
32
|
+
return new Blob([val]);
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
bytes: {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
value: async function () {
|
|
38
|
+
brandCheck(this, res?.constructor);
|
|
39
|
+
|
|
40
|
+
return new Uint8Array(await this.arrayBuffer());
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
json: {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
value: async function () {
|
|
46
|
+
brandCheck(this, res?.constructor);
|
|
47
|
+
const val = await this.text();
|
|
48
|
+
|
|
49
|
+
return JSON.parse(val);
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
text: {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
value: async function () {
|
|
55
|
+
brandCheck(this, res?.constructor);
|
|
56
|
+
const blob = await this.blob();
|
|
57
|
+
|
|
58
|
+
return blob.text();
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return Object.defineProperties(res, {
|
|
65
|
+
body: {
|
|
66
|
+
enumerable: true,
|
|
67
|
+
value: async function () {
|
|
68
|
+
brandCheck(this, res?.constructor);
|
|
69
|
+
|
|
70
|
+
if (this.bodyUsed) {
|
|
71
|
+
throw new TypeError('Response stream already read');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let body = [];
|
|
75
|
+
|
|
76
|
+
for await (const chunk of decompress(this, this.headers[HTTP2_HEADER_CONTENT_ENCODING], { decompression })) {
|
|
77
|
+
body.push(chunk);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
body = Buffer.concat(body);
|
|
81
|
+
|
|
82
|
+
if (!body.length && parse) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (body.length && parse) {
|
|
87
|
+
const contentType = this.headers[HTTP2_HEADER_CONTENT_TYPE] ?? '';
|
|
88
|
+
const charset = contentType.split(';')
|
|
89
|
+
.find((it) => /charset=/i.test(it))
|
|
90
|
+
?.toLowerCase()
|
|
91
|
+
.replace('charset=', '')
|
|
92
|
+
.replace('iso-8859-1', 'latin1')
|
|
93
|
+
.trim() || 'utf-8';
|
|
94
|
+
|
|
95
|
+
if (/\bjson\b/i.test(contentType)) {
|
|
96
|
+
body = JSON.parse(body.toString(charset));
|
|
97
|
+
} else if (/\b(?:text|xml)\b/i.test(contentType)) {
|
|
98
|
+
if (/\b(?:latin1|ucs-2|utf-(?:8|16le))\b/i.test(charset)) {
|
|
99
|
+
body = body.toString(charset);
|
|
100
|
+
} else {
|
|
101
|
+
body = new TextDecoder(charset).decode(body);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return body;
|
|
107
|
+
},
|
|
108
|
+
writable: true,
|
|
109
|
+
},
|
|
110
|
+
bodyUsed: {
|
|
111
|
+
enumerable: true,
|
|
112
|
+
get() {
|
|
113
|
+
return this.readableEnded;
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
};
|
package/src/transform.mjs
CHANGED
|
@@ -60,9 +60,9 @@ export const transform = async (options) => {
|
|
|
60
60
|
if (body === Object(body)
|
|
61
61
|
&& (Reflect.has(body, Symbol.asyncIterator) || (!Array.isArray(body) && Reflect.has(body, Symbol.iterator)))) {
|
|
62
62
|
body = isReadable(body) ? (isReadableStream(body) ? Readable.fromWeb(body) : body) : Readable.from(body);
|
|
63
|
-
body = encodings ? compress(body, encodings) : body;
|
|
63
|
+
body = encodings ? compress(body, encodings, options) : body;
|
|
64
64
|
} else if (encodings) {
|
|
65
|
-
body = await buffer(compress(Readable.from(body), encodings));
|
|
65
|
+
body = await buffer(compress(Readable.from(body), encodings, options));
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
Object.assign(options.headers, {
|
package/src/utils.mjs
CHANGED
|
@@ -65,22 +65,22 @@ export const brandCheck = (value, ctor) => {
|
|
|
65
65
|
}
|
|
66
66
|
};
|
|
67
67
|
|
|
68
|
-
export const compress = (readable, encodings = '') => {
|
|
68
|
+
export const compress = (readable, encodings = '', { compression } = {}) => {
|
|
69
69
|
const encoders = [];
|
|
70
70
|
|
|
71
71
|
encodings = unwind(encodings);
|
|
72
72
|
|
|
73
73
|
for (const encoding of encodings) {
|
|
74
74
|
if (/\bbr\b/i.test(encoding)) {
|
|
75
|
-
encoders.push(zlib.createBrotliCompress());
|
|
75
|
+
encoders.push(zlib.createBrotliCompress(compression?.brotliOptions));
|
|
76
76
|
} else if (/\bdeflate(?!-(?:\w+)?)\b/i.test(encoding)) {
|
|
77
|
-
encoders.push(zlib.createDeflate());
|
|
77
|
+
encoders.push(zlib.createDeflate(compression?.zlibOptions));
|
|
78
78
|
} else if (/\bdeflate-raw\b/i.test(encoding)) {
|
|
79
|
-
encoders.push(zlib.createDeflateRaw());
|
|
79
|
+
encoders.push(zlib.createDeflateRaw(compression?.zlibOptions));
|
|
80
80
|
} else if (/\bgzip\b/i.test(encoding)) {
|
|
81
|
-
encoders.push(zlib.createGzip());
|
|
81
|
+
encoders.push(zlib.createGzip(compression?.zlibOptions));
|
|
82
82
|
} else if (isZstdSupported && /\bzstd\b/i.test(encoding)) {
|
|
83
|
-
encoders.push(zlib.createZstdCompress());
|
|
83
|
+
encoders.push(zlib.createZstdCompress(compression?.zstdOptions));
|
|
84
84
|
} else {
|
|
85
85
|
return readable;
|
|
86
86
|
}
|
|
@@ -98,22 +98,22 @@ export const copyWithMerge = (target, ...rest) => {
|
|
|
98
98
|
return merge(target, ...rest);
|
|
99
99
|
};
|
|
100
100
|
|
|
101
|
-
export const decompress = (readable, encodings = '') => {
|
|
101
|
+
export const decompress = (readable, encodings = '', { decompression } = {}) => {
|
|
102
102
|
const decoders = [];
|
|
103
103
|
|
|
104
104
|
encodings = unwind(encodings);
|
|
105
105
|
|
|
106
106
|
for (const encoding of encodings) {
|
|
107
107
|
if (/\bbr\b/i.test(encoding)) {
|
|
108
|
-
decoders.push(zlib.createBrotliDecompress());
|
|
108
|
+
decoders.push(zlib.createBrotliDecompress(decompression?.brotliOptions));
|
|
109
109
|
} else if (/\bdeflate(?!-(?:\w+)?)\b/i.test(encoding)) {
|
|
110
|
-
decoders.push(zlib.createInflate());
|
|
110
|
+
decoders.push(zlib.createInflate(decompression?.zlibOptions));
|
|
111
111
|
} else if (/\bdeflate-raw\b/i.test(encoding)) {
|
|
112
|
-
decoders.push(zlib.createInflateRaw());
|
|
112
|
+
decoders.push(zlib.createInflateRaw(decompression?.zlibOptions));
|
|
113
113
|
} else if (/\bgzip\b/i.test(encoding)) {
|
|
114
|
-
decoders.push(zlib.createGunzip());
|
|
114
|
+
decoders.push(zlib.createGunzip(decompression?.zlibOptions));
|
|
115
115
|
} else if (isZstdSupported && /\bzstd\b/i.test(encoding)) {
|
|
116
|
-
decoders.push(zlib.createZstdDecompress());
|
|
116
|
+
decoders.push(zlib.createZstdDecompress(decompression?.zstdOptions));
|
|
117
117
|
} else {
|
|
118
118
|
return readable;
|
|
119
119
|
}
|