rekwest 4.3.0 → 4.4.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.
- package/README.md +29 -0
- package/dist/index.js +19 -23
- package/dist/postflight.js +4 -4
- package/dist/utils.js +4 -2
- package/package.json +8 -8
- package/src/index.mjs +18 -21
- package/src/postflight.mjs +4 -6
- package/src/utils.mjs +7 -3
package/README.md
CHANGED
|
@@ -175,6 +175,35 @@ The object to fulfill with default [options](#rekwesturl-options)
|
|
|
175
175
|
|
|
176
176
|
---
|
|
177
177
|
|
|
178
|
+
#### `rekwest.extend(options)`
|
|
179
|
+
|
|
180
|
+
The method to extend default [options](#rekwesturl-options) per instance
|
|
181
|
+
|
|
182
|
+
```javascript
|
|
183
|
+
import rekwest, { constants } from 'rekwest';
|
|
184
|
+
|
|
185
|
+
const {
|
|
186
|
+
HTTP_STATUS_OK,
|
|
187
|
+
} = constants;
|
|
188
|
+
|
|
189
|
+
const rk = rekwest.extend({
|
|
190
|
+
baseURL: 'https://somewhe.re',
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
const signal = AbortSignal.timeout(1e4);
|
|
194
|
+
const url = '/somewhat/endpoint';
|
|
195
|
+
|
|
196
|
+
const res = await rk(url, {
|
|
197
|
+
signal,
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
console.assert(res.statusCode === HTTP_STATUS_OK);
|
|
201
|
+
console.info(res.headers);
|
|
202
|
+
console.log(res.body);
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
---
|
|
206
|
+
|
|
178
207
|
#### `rekwest.stream(url[, options])`
|
|
179
208
|
|
|
180
209
|
The method with limited functionality to use with streams and/or pipes
|
package/dist/index.js
CHANGED
|
@@ -85,42 +85,38 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
85
85
|
const {
|
|
86
86
|
HTTP2_HEADER_CONTENT_TYPE
|
|
87
87
|
} = _nodeHttp2.default.constants;
|
|
88
|
-
function rekwest(
|
|
89
|
-
|
|
90
|
-
if (!options.redirected) {
|
|
91
|
-
options = (0, _utils.merge)(rekwest.defaults, options);
|
|
92
|
-
}
|
|
93
|
-
return (0, _utils.transfer)((0, _validation.validation)(options));
|
|
88
|
+
function rekwest(url, options) {
|
|
89
|
+
return (0, _utils.transfer)((0, _validation.validation)((0, _utils.normalize)(url, options)));
|
|
94
90
|
}
|
|
91
|
+
Reflect.defineProperty(rekwest, 'extend', {
|
|
92
|
+
enumerable: true,
|
|
93
|
+
value(options) {
|
|
94
|
+
return (url, opts) => rekwest(url, (0, _utils.merge)(options, opts));
|
|
95
|
+
}
|
|
96
|
+
});
|
|
95
97
|
Reflect.defineProperty(rekwest, 'stream', {
|
|
96
98
|
enumerable: true,
|
|
97
|
-
value(
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
}, (0, _utils.normalize)(...args))),
|
|
99
|
+
value(url, options) {
|
|
100
|
+
options = (0, _preflight.preflight)((0, _validation.validation)((0, _utils.normalize)(url, (0, _utils.merge)(options, {
|
|
101
|
+
headers: {
|
|
102
|
+
[HTTP2_HEADER_CONTENT_TYPE]: _mediatypes.APPLICATION_OCTET_STREAM
|
|
103
|
+
},
|
|
104
104
|
redirect: _constants.requestRedirect.manual
|
|
105
|
-
});
|
|
106
|
-
const {
|
|
107
|
-
h2,
|
|
108
|
-
url
|
|
109
|
-
} = options;
|
|
105
|
+
}))));
|
|
110
106
|
let client, req;
|
|
111
|
-
if (h2) {
|
|
112
|
-
client = _nodeHttp2.default.connect(url.origin, options);
|
|
107
|
+
if (options.h2) {
|
|
108
|
+
client = _nodeHttp2.default.connect(options.url.origin, options);
|
|
113
109
|
req = client.request(options.headers, options);
|
|
114
110
|
} else {
|
|
115
111
|
const {
|
|
116
112
|
request
|
|
117
|
-
} = url.protocol === 'http:' ? _nodeHttp.default : _nodeHttps.default;
|
|
118
|
-
req = request(url, options);
|
|
113
|
+
} = options.url.protocol === 'http:' ? _nodeHttp.default : _nodeHttps.default;
|
|
114
|
+
req = request(options.url, options);
|
|
119
115
|
}
|
|
120
116
|
(0, _utils.affix)(client, req, options);
|
|
121
117
|
req.once('response', res => {
|
|
122
118
|
let headers;
|
|
123
|
-
if (h2) {
|
|
119
|
+
if (options.h2) {
|
|
124
120
|
headers = res;
|
|
125
121
|
res = req;
|
|
126
122
|
}
|
package/dist/postflight.js
CHANGED
|
@@ -73,7 +73,7 @@ const postflight = (req, res, options, {
|
|
|
73
73
|
return res.emit('error', new _errors.RequestError('URL scheme must be "http" or "https".'));
|
|
74
74
|
}
|
|
75
75
|
if (!(0, _utils.sameOrigin)(location, url)) {
|
|
76
|
-
|
|
76
|
+
options.h2 = false;
|
|
77
77
|
if ([_constants.requestCredentials.omit, _constants.requestCredentials.sameOrigin].includes(credentials)) {
|
|
78
78
|
Object.keys(options.headers).filter(it => new RegExp(HTTP2_HEADER_AUTHORIZATION, 'i').test(it)).forEach(it => Reflect.deleteProperty(options.headers, it));
|
|
79
79
|
location.password = location.username = '';
|
|
@@ -82,17 +82,17 @@ const postflight = (req, res, options, {
|
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
|
-
options.url = location;
|
|
86
85
|
if (statusCode !== HTTP_STATUS_SEE_OTHER && options.body?.pipe?.constructor === Function) {
|
|
87
86
|
return res.emit('error', new _errors.RequestError(`Unable to ${redirect} redirect with streamable body.`));
|
|
88
87
|
}
|
|
89
|
-
options.follow--;
|
|
90
88
|
if ([HTTP_STATUS_MOVED_PERMANENTLY, HTTP_STATUS_FOUND].includes(statusCode) && options.method === HTTP2_METHOD_POST || statusCode === HTTP_STATUS_SEE_OTHER && ![HTTP2_METHOD_GET, HTTP2_METHOD_HEAD].includes(options.method)) {
|
|
91
89
|
Object.keys(options.headers).filter(it => /^content-/i.test(it)).forEach(it => Reflect.deleteProperty(options.headers, it));
|
|
92
90
|
options.body = null;
|
|
93
91
|
options.method = HTTP2_METHOD_GET;
|
|
94
92
|
}
|
|
95
|
-
|
|
93
|
+
options.follow--;
|
|
94
|
+
options.redirected = true;
|
|
95
|
+
options.url = location;
|
|
96
96
|
if (statusCode === HTTP_STATUS_MOVED_PERMANENTLY && res.headers[HTTP2_HEADER_RETRY_AFTER]) {
|
|
97
97
|
let interval = res.headers[HTTP2_HEADER_RETRY_AFTER];
|
|
98
98
|
interval = Number(interval) * 1000 || new Date(interval) - Date.now();
|
package/dist/utils.js
CHANGED
|
@@ -148,6 +148,9 @@ const merge = (target = {}, ...rest) => {
|
|
|
148
148
|
};
|
|
149
149
|
exports.merge = merge;
|
|
150
150
|
const normalize = (url, options = {}) => {
|
|
151
|
+
if (!options.redirected) {
|
|
152
|
+
options = merge(_index.default.defaults, options);
|
|
153
|
+
}
|
|
151
154
|
if (options.trimTrailingSlashes) {
|
|
152
155
|
url = `${url}`.replace(/(?<!:)\/+/g, '/');
|
|
153
156
|
}
|
|
@@ -171,7 +174,6 @@ async function* tap(value) {
|
|
|
171
174
|
const transfer = async options => {
|
|
172
175
|
const {
|
|
173
176
|
digest,
|
|
174
|
-
h2,
|
|
175
177
|
redirected,
|
|
176
178
|
thenable,
|
|
177
179
|
url
|
|
@@ -180,7 +182,7 @@ const transfer = async options => {
|
|
|
180
182
|
throw new _errors.RequestError(`Maximum redirect reached at: ${url.href}`);
|
|
181
183
|
}
|
|
182
184
|
if (url.protocol === 'https:') {
|
|
183
|
-
options = !h2 ? await (0, _ackn.ackn)(options) : {
|
|
185
|
+
options = !options.h2 ? await (0, _ackn.ackn)(options) : {
|
|
184
186
|
...options,
|
|
185
187
|
createConnection: null,
|
|
186
188
|
protocol: url.protocol
|
package/package.json
CHANGED
|
@@ -8,13 +8,13 @@
|
|
|
8
8
|
"url": "https://github.com/bricss/rekwest/issues"
|
|
9
9
|
},
|
|
10
10
|
"devDependencies": {
|
|
11
|
-
"@babel/cli": "^7.
|
|
12
|
-
"@babel/core": "^7.
|
|
13
|
-
"@babel/eslint-parser": "^7.
|
|
14
|
-
"@babel/preset-env": "^7.
|
|
15
|
-
"c8": "^7.
|
|
16
|
-
"eslint": "^8.
|
|
17
|
-
"eslint-config-ultra-refined": "^2.
|
|
11
|
+
"@babel/cli": "^7.21.5",
|
|
12
|
+
"@babel/core": "^7.21.8",
|
|
13
|
+
"@babel/eslint-parser": "^7.21.8",
|
|
14
|
+
"@babel/preset-env": "^7.21.5",
|
|
15
|
+
"c8": "^7.13.0",
|
|
16
|
+
"eslint": "^8.40.0",
|
|
17
|
+
"eslint-config-ultra-refined": "^2.13.0",
|
|
18
18
|
"mocha": "^10.2.0"
|
|
19
19
|
},
|
|
20
20
|
"description": "The robust request library that humanity deserves 🌐",
|
|
@@ -67,5 +67,5 @@
|
|
|
67
67
|
"test:bail": "mocha --bail",
|
|
68
68
|
"test:cover": "c8 --include=src --reporter=lcov --reporter=text npm test"
|
|
69
69
|
},
|
|
70
|
-
"version": "4.
|
|
70
|
+
"version": "4.4.1"
|
|
71
71
|
}
|
package/src/index.mjs
CHANGED
|
@@ -31,36 +31,33 @@ const {
|
|
|
31
31
|
HTTP2_HEADER_CONTENT_TYPE,
|
|
32
32
|
} = http2.constants;
|
|
33
33
|
|
|
34
|
-
export default function rekwest(
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (!options.redirected) {
|
|
38
|
-
options = merge(rekwest.defaults, options);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return transfer(validation(options));
|
|
34
|
+
export default function rekwest(url, options) {
|
|
35
|
+
return transfer(validation(normalize(url, options)));
|
|
42
36
|
}
|
|
43
37
|
|
|
38
|
+
Reflect.defineProperty(rekwest, 'extend', {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
value(options) {
|
|
41
|
+
return (url, opts) => rekwest(url, merge(options, opts));
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
|
|
44
45
|
Reflect.defineProperty(rekwest, 'stream', {
|
|
45
46
|
enumerable: true,
|
|
46
|
-
value(
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
headers: { [HTTP2_HEADER_CONTENT_TYPE]: APPLICATION_OCTET_STREAM },
|
|
50
|
-
}, normalize(...args))),
|
|
47
|
+
value(url, options) {
|
|
48
|
+
options = preflight(validation(normalize(url, merge(options, {
|
|
49
|
+
headers: { [HTTP2_HEADER_CONTENT_TYPE]: APPLICATION_OCTET_STREAM },
|
|
51
50
|
redirect: requestRedirect.manual,
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
const { h2, url } = options;
|
|
51
|
+
}))));
|
|
55
52
|
let client, req;
|
|
56
53
|
|
|
57
|
-
if (h2) {
|
|
58
|
-
client = http2.connect(url.origin, options);
|
|
54
|
+
if (options.h2) {
|
|
55
|
+
client = http2.connect(options.url.origin, options);
|
|
59
56
|
req = client.request(options.headers, options);
|
|
60
57
|
} else {
|
|
61
|
-
const { request } =
|
|
58
|
+
const { request } = options.url.protocol === 'http:' ? http : https;
|
|
62
59
|
|
|
63
|
-
req = request(url, options);
|
|
60
|
+
req = request(options.url, options);
|
|
64
61
|
}
|
|
65
62
|
|
|
66
63
|
affix(client, req, options);
|
|
@@ -68,7 +65,7 @@ Reflect.defineProperty(rekwest, 'stream', {
|
|
|
68
65
|
req.once('response', (res) => {
|
|
69
66
|
let headers;
|
|
70
67
|
|
|
71
|
-
if (h2) {
|
|
68
|
+
if (options.h2) {
|
|
72
69
|
headers = res;
|
|
73
70
|
res = req;
|
|
74
71
|
}
|
package/src/postflight.mjs
CHANGED
|
@@ -78,7 +78,7 @@ export const postflight = (req, res, options, { reject, resolve }) => {
|
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
if (!sameOrigin(location, url)) {
|
|
81
|
-
|
|
81
|
+
options.h2 = false;
|
|
82
82
|
if ([
|
|
83
83
|
requestCredentials.omit,
|
|
84
84
|
requestCredentials.sameOrigin,
|
|
@@ -92,14 +92,10 @@ export const postflight = (req, res, options, { reject, resolve }) => {
|
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
options.url = location;
|
|
96
|
-
|
|
97
95
|
if (statusCode !== HTTP_STATUS_SEE_OTHER && options.body?.pipe?.constructor === Function) {
|
|
98
96
|
return res.emit('error', new RequestError(`Unable to ${ redirect } redirect with streamable body.`));
|
|
99
97
|
}
|
|
100
98
|
|
|
101
|
-
options.follow--;
|
|
102
|
-
|
|
103
99
|
if (([
|
|
104
100
|
HTTP_STATUS_MOVED_PERMANENTLY,
|
|
105
101
|
HTTP_STATUS_FOUND,
|
|
@@ -113,7 +109,9 @@ export const postflight = (req, res, options, { reject, resolve }) => {
|
|
|
113
109
|
options.method = HTTP2_METHOD_GET;
|
|
114
110
|
}
|
|
115
111
|
|
|
116
|
-
|
|
112
|
+
options.follow--;
|
|
113
|
+
options.redirected = true;
|
|
114
|
+
options.url = location;
|
|
117
115
|
|
|
118
116
|
if (statusCode === HTTP_STATUS_MOVED_PERMANENTLY && res.headers[HTTP2_HEADER_RETRY_AFTER]) {
|
|
119
117
|
let interval = res.headers[HTTP2_HEADER_RETRY_AFTER];
|
package/src/utils.mjs
CHANGED
|
@@ -172,6 +172,10 @@ export const merge = (target = {}, ...rest) => {
|
|
|
172
172
|
};
|
|
173
173
|
|
|
174
174
|
export const normalize = (url, options = {}) => {
|
|
175
|
+
if (!options.redirected) {
|
|
176
|
+
options = merge(rekwest.defaults, options);
|
|
177
|
+
}
|
|
178
|
+
|
|
175
179
|
if (options.trimTrailingSlashes) {
|
|
176
180
|
url = `${ url }`.replace(/(?<!:)\/+/g, '/');
|
|
177
181
|
}
|
|
@@ -194,14 +198,14 @@ export async function* tap(value) {
|
|
|
194
198
|
}
|
|
195
199
|
|
|
196
200
|
export const transfer = async (options) => {
|
|
197
|
-
const { digest,
|
|
201
|
+
const { digest, redirected, thenable, url } = options;
|
|
198
202
|
|
|
199
203
|
if (options.follow === 0) {
|
|
200
204
|
throw new RequestError(`Maximum redirect reached at: ${ url.href }`);
|
|
201
205
|
}
|
|
202
206
|
|
|
203
207
|
if (url.protocol === 'https:') {
|
|
204
|
-
options = !h2 ? await ackn(options) : {
|
|
208
|
+
options = !options.h2 ? await ackn(options) : {
|
|
205
209
|
...options,
|
|
206
210
|
createConnection: null,
|
|
207
211
|
protocol: url.protocol,
|
|
@@ -229,7 +233,7 @@ export const transfer = async (options) => {
|
|
|
229
233
|
client = http2.connect(url.origin, options);
|
|
230
234
|
req = client.request(options.headers, options);
|
|
231
235
|
} else {
|
|
232
|
-
const { request } =
|
|
236
|
+
const { request } = url.protocol === 'http:' ? http : https;
|
|
233
237
|
|
|
234
238
|
req = request(url, options);
|
|
235
239
|
}
|