@e-mc/request 0.10.15 → 0.10.17
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 +4 -4
- package/http/adapter/index.js +29 -11
- package/index.js +33 -12
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
## Interface
|
|
11
11
|
|
|
12
|
-
* [View Source](https://www.unpkg.com/@e-mc/types@0.10.
|
|
12
|
+
* [View Source](https://www.unpkg.com/@e-mc/types@0.10.17/lib/index.d.ts)
|
|
13
13
|
|
|
14
14
|
```typescript
|
|
15
15
|
import type { IModule, ModuleConstructor } from "./index";
|
|
@@ -205,9 +205,9 @@ instance.get("http://hostname/path/config.yml", options).then(data => {
|
|
|
205
205
|
|
|
206
206
|
## References
|
|
207
207
|
|
|
208
|
-
- https://www.unpkg.com/@e-mc/types@0.10.
|
|
209
|
-
- https://www.unpkg.com/@e-mc/types@0.10.
|
|
210
|
-
- https://www.unpkg.com/@e-mc/types@0.10.
|
|
208
|
+
- https://www.unpkg.com/@e-mc/types@0.10.17/lib/http.d.ts
|
|
209
|
+
- https://www.unpkg.com/@e-mc/types@0.10.17/lib/request.d.ts
|
|
210
|
+
- https://www.unpkg.com/@e-mc/types@0.10.17/lib/settings.d.ts
|
|
211
211
|
|
|
212
212
|
* https://www.npmjs.com/package/@types/node
|
|
213
213
|
|
package/http/adapter/index.js
CHANGED
|
@@ -79,13 +79,21 @@ class HttpAdapter {
|
|
|
79
79
|
return;
|
|
80
80
|
}
|
|
81
81
|
const statusCode = headers[':status'];
|
|
82
|
-
if (statusCode
|
|
83
|
-
this.acceptResponse(headers);
|
|
82
|
+
if (statusCode === 204 || statusCode === 304) {
|
|
83
|
+
this.acceptResponse(headers, true);
|
|
84
84
|
}
|
|
85
|
-
else if (statusCode <
|
|
85
|
+
else if (statusCode < 300) {
|
|
86
|
+
this.acceptResponse(headers, false);
|
|
87
|
+
}
|
|
88
|
+
else if (statusCode === 301 ||
|
|
89
|
+
statusCode === 302 ||
|
|
90
|
+
statusCode === 303 ||
|
|
91
|
+
statusCode === 307 ||
|
|
92
|
+
statusCode === 308) {
|
|
86
93
|
this.redirectResponse(statusCode, headers.location);
|
|
87
94
|
}
|
|
88
|
-
else if (statusCode ===
|
|
95
|
+
else if (statusCode === 305 ||
|
|
96
|
+
statusCode === 401 ||
|
|
89
97
|
statusCode === 402 ||
|
|
90
98
|
statusCode === 403 ||
|
|
91
99
|
statusCode === 404 ||
|
|
@@ -151,10 +159,17 @@ class HttpAdapter {
|
|
|
151
159
|
return;
|
|
152
160
|
}
|
|
153
161
|
const statusCode = res.statusCode;
|
|
154
|
-
if (statusCode
|
|
155
|
-
this.acceptResponse(res.headers);
|
|
162
|
+
if (statusCode === 204 || statusCode === 304) {
|
|
163
|
+
this.acceptResponse(res.headers, true);
|
|
164
|
+
}
|
|
165
|
+
else if (statusCode < 300) {
|
|
166
|
+
this.acceptResponse(res.headers, false);
|
|
156
167
|
}
|
|
157
|
-
else if (statusCode
|
|
168
|
+
else if (statusCode === 301 ||
|
|
169
|
+
statusCode === 302 ||
|
|
170
|
+
statusCode === 303 ||
|
|
171
|
+
statusCode === 307 ||
|
|
172
|
+
statusCode === 308) {
|
|
158
173
|
this.redirectResponse(statusCode, res.headers.location);
|
|
159
174
|
}
|
|
160
175
|
else if (this.isRetry(statusCode)) {
|
|
@@ -226,7 +241,7 @@ class HttpAdapter {
|
|
|
226
241
|
this.opts.httpVersion = 1;
|
|
227
242
|
this.init();
|
|
228
243
|
}
|
|
229
|
-
acceptResponse(headers) {
|
|
244
|
+
acceptResponse(headers, empty = false) {
|
|
230
245
|
const opts = this.opts;
|
|
231
246
|
if ('outHeaders' in opts) {
|
|
232
247
|
opts.outHeaders = headers;
|
|
@@ -238,7 +253,7 @@ class HttpAdapter {
|
|
|
238
253
|
const enabled = opts.connected?.call(this.client, headers) !== false && !pipeline;
|
|
239
254
|
const maxBufferSize = opts.maxBufferSize ? (0, types_1.alignSize)(opts.maxBufferSize) : 0;
|
|
240
255
|
this.contentLength = parseInt(headers['content-length'] || '0');
|
|
241
|
-
const updating = opts.progressId !== undefined && !!this.instance.host && this.contentLength > 0;
|
|
256
|
+
const updating = opts.progressId !== undefined && !!this.instance.host && this.contentLength > 0 && !empty;
|
|
242
257
|
const readTimeout = this.instance.readTimeout;
|
|
243
258
|
let log = this.state.log, buffer = null, dataLength = 0;
|
|
244
259
|
this.client.once('readable', () => {
|
|
@@ -267,7 +282,7 @@ class HttpAdapter {
|
|
|
267
282
|
this.updateProgress(0, 0);
|
|
268
283
|
}
|
|
269
284
|
});
|
|
270
|
-
if (enabled) {
|
|
285
|
+
if (enabled && !empty) {
|
|
271
286
|
const encoding = opts.encoding;
|
|
272
287
|
this.client.on('data', (chunk) => {
|
|
273
288
|
if (buffer) {
|
|
@@ -366,7 +381,7 @@ class HttpAdapter {
|
|
|
366
381
|
if (updating) {
|
|
367
382
|
this.updateProgress(0, this.contentLength);
|
|
368
383
|
}
|
|
369
|
-
if (enabled && this.instance.readExpect === 'always') {
|
|
384
|
+
if (enabled && this.instance.readExpect === 'always' && !empty) {
|
|
370
385
|
this.terminate("No data received");
|
|
371
386
|
return;
|
|
372
387
|
}
|
|
@@ -402,6 +417,9 @@ class HttpAdapter {
|
|
|
402
417
|
}
|
|
403
418
|
else if (++this.redirects <= this.redirectLimit) {
|
|
404
419
|
this.abortResponse();
|
|
420
|
+
if (this.opts.method?.toUpperCase() === 'PUT' && statusCode !== 307 && statusCode !== 308) {
|
|
421
|
+
this.#options.method = 'GET';
|
|
422
|
+
}
|
|
405
423
|
this.setOpts((0, util_1.fromURL)(this.opts.url, location));
|
|
406
424
|
this.init();
|
|
407
425
|
}
|
package/index.js
CHANGED
|
@@ -276,6 +276,8 @@ function checkEncoding(request, response, statusCode, outStream, contentEncoding
|
|
|
276
276
|
case 206:
|
|
277
277
|
request.emit('error', (0, types_1.errorValue)("Aborted", 'Partial content'));
|
|
278
278
|
case 204:
|
|
279
|
+
case 205:
|
|
280
|
+
case 304:
|
|
279
281
|
return;
|
|
280
282
|
}
|
|
281
283
|
if (!contentEncoding) {
|
|
@@ -375,6 +377,7 @@ function successDns(instance, value, pending, connected) {
|
|
|
375
377
|
}
|
|
376
378
|
pending.length = 0;
|
|
377
379
|
}
|
|
380
|
+
const hasResponse = (value) => value >= 200 && (value < 300 || value === 304);
|
|
378
381
|
const configureDns = (family, options) => family === 0 ? options : { family, hints: family === 6 ? dns.V4MAPPED : 0 };
|
|
379
382
|
const ignoreOpt = (opts, ...values) => !opts?.some(item => values.includes(item));
|
|
380
383
|
const escapeQuote = (value) => value.replace(/[\\"]/g, capture => '\\' + capture);
|
|
@@ -431,13 +434,21 @@ class Fetch {
|
|
|
431
434
|
return;
|
|
432
435
|
}
|
|
433
436
|
const statusCode = headers[':status'];
|
|
434
|
-
if (statusCode
|
|
435
|
-
this.acceptResponse(headers);
|
|
437
|
+
if (statusCode === 204 || statusCode === 304) {
|
|
438
|
+
this.acceptResponse(headers, true);
|
|
436
439
|
}
|
|
437
|
-
else if (statusCode <
|
|
440
|
+
else if (statusCode < 300) {
|
|
441
|
+
this.acceptResponse(headers, false);
|
|
442
|
+
}
|
|
443
|
+
else if (statusCode === 301 ||
|
|
444
|
+
statusCode === 302 ||
|
|
445
|
+
statusCode === 303 ||
|
|
446
|
+
statusCode === 307 ||
|
|
447
|
+
statusCode === 308) {
|
|
438
448
|
this.redirectResponse(statusCode, headers.location);
|
|
439
449
|
}
|
|
440
|
-
else if (statusCode ===
|
|
450
|
+
else if (statusCode === 305 ||
|
|
451
|
+
statusCode === 401 ||
|
|
441
452
|
statusCode === 402 ||
|
|
442
453
|
statusCode === 403 ||
|
|
443
454
|
statusCode === 404 ||
|
|
@@ -502,10 +513,17 @@ class Fetch {
|
|
|
502
513
|
return;
|
|
503
514
|
}
|
|
504
515
|
const statusCode = res.statusCode;
|
|
505
|
-
if (statusCode
|
|
506
|
-
this.acceptResponse(res.headers);
|
|
516
|
+
if (statusCode === 204 || statusCode === 304) {
|
|
517
|
+
this.acceptResponse(res.headers, true);
|
|
518
|
+
}
|
|
519
|
+
else if (statusCode < 300) {
|
|
520
|
+
this.acceptResponse(res.headers, false);
|
|
507
521
|
}
|
|
508
|
-
else if (statusCode
|
|
522
|
+
else if (statusCode === 301 ||
|
|
523
|
+
statusCode === 302 ||
|
|
524
|
+
statusCode === 303 ||
|
|
525
|
+
statusCode === 307 ||
|
|
526
|
+
statusCode === 308) {
|
|
509
527
|
this.redirectResponse(statusCode, res.headers.location);
|
|
510
528
|
}
|
|
511
529
|
else if (this.isRetry(statusCode)) {
|
|
@@ -586,7 +604,7 @@ class Fetch {
|
|
|
586
604
|
this.opts.httpVersion = 1;
|
|
587
605
|
this.init();
|
|
588
606
|
}
|
|
589
|
-
acceptResponse(headers) {
|
|
607
|
+
acceptResponse(headers, empty = false) {
|
|
590
608
|
const { instance, config, client, opts, pipeTo } = this;
|
|
591
609
|
const parent = this.instance.host;
|
|
592
610
|
const progressId = config.progressId;
|
|
@@ -629,7 +647,7 @@ class Fetch {
|
|
|
629
647
|
}
|
|
630
648
|
});
|
|
631
649
|
}
|
|
632
|
-
if (enabled) {
|
|
650
|
+
if (enabled && !empty) {
|
|
633
651
|
const encoding = opts.encoding;
|
|
634
652
|
client.on('data', (chunk) => {
|
|
635
653
|
if (buffer) {
|
|
@@ -726,7 +744,7 @@ class Fetch {
|
|
|
726
744
|
if (contentLength > 0) {
|
|
727
745
|
parent.updateProgress("request", progressId, 0, contentLength);
|
|
728
746
|
}
|
|
729
|
-
if (enabled && instance.readExpect === 'always') {
|
|
747
|
+
if (enabled && instance.readExpect === 'always' && !empty) {
|
|
730
748
|
this.terminate("No data received");
|
|
731
749
|
return;
|
|
732
750
|
}
|
|
@@ -750,6 +768,9 @@ class Fetch {
|
|
|
750
768
|
}
|
|
751
769
|
else if (++this.redirects <= this.redirectLimit) {
|
|
752
770
|
this.abortResponse();
|
|
771
|
+
if (this.opts.method?.toUpperCase() === 'PUT' && statusCode !== 307 && statusCode !== 308) {
|
|
772
|
+
this.opts.method = 'GET';
|
|
773
|
+
}
|
|
753
774
|
this.setConfig(Request.fromURL(this.config.url, location));
|
|
754
775
|
this.init();
|
|
755
776
|
}
|
|
@@ -2102,7 +2123,7 @@ class Request extends module_1 {
|
|
|
2102
2123
|
request.on('response', response => {
|
|
2103
2124
|
connected = true;
|
|
2104
2125
|
const statusCode = response[':status'];
|
|
2105
|
-
if (this.matchStatus(statusCode, url, response, request, options) && statusCode
|
|
2126
|
+
if (this.matchStatus(statusCode, url, response, request, options) && hasResponse(statusCode)) {
|
|
2106
2127
|
if (emitter = checkEncoding(request, request, statusCode, outStream, response['content-encoding'])) {
|
|
2107
2128
|
for (const event in listenerMap) {
|
|
2108
2129
|
const [name, type] = event.split('-');
|
|
@@ -2224,7 +2245,7 @@ class Request extends module_1 {
|
|
|
2224
2245
|
}, response => {
|
|
2225
2246
|
const statusCode = response.statusCode;
|
|
2226
2247
|
const incoming = response.headers;
|
|
2227
|
-
if (this.matchStatus(statusCode, url, incoming, request, options) && (getting || posting) && statusCode
|
|
2248
|
+
if (this.matchStatus(statusCode, url, incoming, request, options) && (getting || posting) && hasResponse(statusCode)) {
|
|
2228
2249
|
let source = checkEncoding(request, response, statusCode, outStream, incoming['content-encoding']);
|
|
2229
2250
|
if (source) {
|
|
2230
2251
|
source.once('finish', () => request.emit('end'));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/request",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.17",
|
|
4
4
|
"description": "Request constructor for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -20,11 +20,11 @@
|
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/module": "0.10.
|
|
24
|
-
"@e-mc/types": "0.10.
|
|
23
|
+
"@e-mc/module": "0.10.17",
|
|
24
|
+
"@e-mc/types": "0.10.17",
|
|
25
25
|
"combined-stream": "^1.0.8",
|
|
26
|
-
"js-yaml": "^4.1.
|
|
27
|
-
"picomatch": "^4.0.
|
|
26
|
+
"js-yaml": "^4.1.1",
|
|
27
|
+
"picomatch": "^4.0.3",
|
|
28
28
|
"which": "^2.0.2"
|
|
29
29
|
}
|
|
30
30
|
}
|