@e-mc/request 0.5.6 → 0.5.7
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 +1 -1
- package/index.js +56 -38
- package/package.json +3 -3
package/README.md
CHANGED
package/index.js
CHANGED
|
@@ -248,6 +248,56 @@ function validateCerts(certs) {
|
|
|
248
248
|
}
|
|
249
249
|
return [text, file];
|
|
250
250
|
}
|
|
251
|
+
function checkEncoding(request, response, statusCode, outStream, contentEncoding = '') {
|
|
252
|
+
switch (statusCode) {
|
|
253
|
+
case 206:
|
|
254
|
+
request.emit('error', (0, types_1.errorValue)("Aborted", 'Partial content'));
|
|
255
|
+
case 204:
|
|
256
|
+
return;
|
|
257
|
+
default:
|
|
258
|
+
contentEncoding = contentEncoding.trim();
|
|
259
|
+
if (!contentEncoding) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
contentEncoding = contentEncoding.toLowerCase();
|
|
263
|
+
break;
|
|
264
|
+
}
|
|
265
|
+
const chunkSize = outStream === null || outStream === void 0 ? void 0 : outStream.writableHighWaterMark;
|
|
266
|
+
let pipeTo;
|
|
267
|
+
if (contentEncoding.indexOf(',') === -1) {
|
|
268
|
+
pipeTo = decompressEncoding(contentEncoding, chunkSize);
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
for (const value of contentEncoding.split(/\s*,\s*/).reverse()) {
|
|
272
|
+
const next = decompressEncoding(value, chunkSize);
|
|
273
|
+
if (!next) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
pipeTo = pipeTo ? pipeTo.pipe(next) : next;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (pipeTo) {
|
|
280
|
+
if (outStream) {
|
|
281
|
+
stream.pipeline(response, pipeTo, outStream, err => err && response.emit('error', err));
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
stream.pipeline(response, pipeTo, err => err && response.emit('error', err));
|
|
285
|
+
}
|
|
286
|
+
return pipeTo;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
function decompressEncoding(value, chunkSize) {
|
|
290
|
+
switch (value) {
|
|
291
|
+
case 'gzip':
|
|
292
|
+
return zlib.createGunzip({ chunkSize });
|
|
293
|
+
case 'br':
|
|
294
|
+
return zlib.createBrotliDecompress({ chunkSize });
|
|
295
|
+
case 'deflate':
|
|
296
|
+
return zlib.createInflate({ chunkSize });
|
|
297
|
+
case 'deflate-raw':
|
|
298
|
+
return zlib.createInflateRaw({ chunkSize });
|
|
299
|
+
}
|
|
300
|
+
}
|
|
251
301
|
class Request extends module_1.default {
|
|
252
302
|
static purgeMemory(percent = 1, limit = 0, parent) {
|
|
253
303
|
if (percent >= 1) {
|
|
@@ -1335,39 +1385,6 @@ class Request extends module_1.default {
|
|
|
1335
1385
|
url = new URL(uri);
|
|
1336
1386
|
options.url = url;
|
|
1337
1387
|
}
|
|
1338
|
-
const checkEncoding = (response, statusCode, contentEncoding = '') => {
|
|
1339
|
-
switch (statusCode) {
|
|
1340
|
-
case 206:
|
|
1341
|
-
request.emit('error', new Error('[ABORT] Partial content'));
|
|
1342
|
-
case 204:
|
|
1343
|
-
return;
|
|
1344
|
-
}
|
|
1345
|
-
const chunkSize = outStream === null || outStream === void 0 ? void 0 : outStream.writableHighWaterMark;
|
|
1346
|
-
let pipeTo;
|
|
1347
|
-
switch (contentEncoding.trim().toLowerCase()) {
|
|
1348
|
-
case 'gzip':
|
|
1349
|
-
pipeTo = zlib.createGunzip({ chunkSize });
|
|
1350
|
-
break;
|
|
1351
|
-
case 'br':
|
|
1352
|
-
pipeTo = zlib.createBrotliDecompress({ chunkSize });
|
|
1353
|
-
break;
|
|
1354
|
-
case 'deflate':
|
|
1355
|
-
pipeTo = zlib.createInflate({ chunkSize });
|
|
1356
|
-
break;
|
|
1357
|
-
case 'deflate-raw':
|
|
1358
|
-
pipeTo = zlib.createInflateRaw({ chunkSize });
|
|
1359
|
-
break;
|
|
1360
|
-
}
|
|
1361
|
-
if (pipeTo) {
|
|
1362
|
-
if (outStream) {
|
|
1363
|
-
stream.pipeline(response, pipeTo, outStream, err => err && response.emit('error', err));
|
|
1364
|
-
}
|
|
1365
|
-
else {
|
|
1366
|
-
stream.pipeline(response, pipeTo, err => err && response.emit('error', err));
|
|
1367
|
-
}
|
|
1368
|
-
return pipeTo;
|
|
1369
|
-
}
|
|
1370
|
-
};
|
|
1371
1388
|
const { hostname, origin } = host;
|
|
1372
1389
|
const pathname = url.pathname + (socketPath ? '' : url.search);
|
|
1373
1390
|
const proxy = this.proxyOf(uri, host.localhost);
|
|
@@ -1392,7 +1409,8 @@ class Request extends module_1.default {
|
|
|
1392
1409
|
connected = true;
|
|
1393
1410
|
const statusCode = response[':status'];
|
|
1394
1411
|
if (statusCode >= 200 && statusCode < 300) {
|
|
1395
|
-
|
|
1412
|
+
emitter = checkEncoding(request, request, statusCode, outStream, response['content-encoding']);
|
|
1413
|
+
if (emitter) {
|
|
1396
1414
|
for (const event in listenerMap) {
|
|
1397
1415
|
listenerMap[event].forEach(listener => {
|
|
1398
1416
|
const [name, type] = event.split('-');
|
|
@@ -1511,7 +1529,7 @@ class Request extends module_1.default {
|
|
|
1511
1529
|
}, response => {
|
|
1512
1530
|
const statusCode = response.statusCode;
|
|
1513
1531
|
if ((getting || posting) && statusCode >= 200 && statusCode < 300) {
|
|
1514
|
-
let source = checkEncoding(response, statusCode, response.headers['content-encoding']);
|
|
1532
|
+
let source = checkEncoding(request, response, statusCode, outStream, response.headers['content-encoding']);
|
|
1515
1533
|
if (source) {
|
|
1516
1534
|
source.once('finish', () => request.emit('end'));
|
|
1517
1535
|
}
|
|
@@ -1747,6 +1765,9 @@ class Request extends module_1.default {
|
|
|
1747
1765
|
const startTime = log ? process.hrtime() : 0;
|
|
1748
1766
|
let retries = 0, redirects = 0, closed, timeout, outStream;
|
|
1749
1767
|
const throwError = (err, outAbort) => {
|
|
1768
|
+
if (timeout) {
|
|
1769
|
+
clearTimeout(timeout);
|
|
1770
|
+
}
|
|
1750
1771
|
if (!closed) {
|
|
1751
1772
|
closed = true;
|
|
1752
1773
|
if (outStream && (0, types_1.isString)(pipeTo)) {
|
|
@@ -1754,9 +1775,6 @@ class Request extends module_1.default {
|
|
|
1754
1775
|
}
|
|
1755
1776
|
reject(typeof err === 'string' ? new Error(err) : err);
|
|
1756
1777
|
}
|
|
1757
|
-
if (timeout) {
|
|
1758
|
-
clearTimeout(timeout);
|
|
1759
|
-
}
|
|
1760
1778
|
if (outAbort) {
|
|
1761
1779
|
this[kDownloading].delete(outAbort);
|
|
1762
1780
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/request",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.7",
|
|
4
4
|
"description": "Request constructor for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/module": "0.5.
|
|
24
|
-
"@e-mc/types": "0.5.
|
|
23
|
+
"@e-mc/module": "0.5.7",
|
|
24
|
+
"@e-mc/types": "0.5.7",
|
|
25
25
|
"combined-stream": "^1.0.8",
|
|
26
26
|
"js-yaml": "^4.1.0",
|
|
27
27
|
"which": "^2.0.2"
|