@e-mc/request 0.7.6 → 0.7.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 +61 -43
- package/package.json +3 -3
package/README.md
CHANGED
package/index.js
CHANGED
|
@@ -257,6 +257,61 @@ function validateCerts(certs) {
|
|
|
257
257
|
}
|
|
258
258
|
return [text, file];
|
|
259
259
|
}
|
|
260
|
+
function checkEncoding(request, response, statusCode, outStream, contentEncoding = '') {
|
|
261
|
+
switch (statusCode) {
|
|
262
|
+
case 206:
|
|
263
|
+
request.emit('error', (0, types_1.errorValue)("Aborted", 'Partial content'));
|
|
264
|
+
case 204:
|
|
265
|
+
return;
|
|
266
|
+
default:
|
|
267
|
+
contentEncoding = contentEncoding.trim();
|
|
268
|
+
if (!contentEncoding) {
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
contentEncoding = contentEncoding.toLowerCase();
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
const chunkSize = outStream === null || outStream === void 0 ? void 0 : outStream.writableHighWaterMark;
|
|
275
|
+
let pipeTo;
|
|
276
|
+
if (contentEncoding.indexOf(',') === -1) {
|
|
277
|
+
pipeTo = decompressEncoding(contentEncoding, chunkSize);
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
for (const value of contentEncoding.split(/\s*,\s*/).reverse()) {
|
|
281
|
+
const next = decompressEncoding(value, chunkSize);
|
|
282
|
+
if (!next) {
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
pipeTo = pipeTo ? pipeTo.pipe(next) : next;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
if (pipeTo) {
|
|
289
|
+
if (outStream) {
|
|
290
|
+
stream.pipeline(response, pipeTo, outStream, err => err && response.emit('error', err));
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
stream.pipeline(response, pipeTo, err => err && response.emit('error', err));
|
|
294
|
+
}
|
|
295
|
+
return pipeTo;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
function decompressEncoding(value, chunkSize) {
|
|
299
|
+
switch (value) {
|
|
300
|
+
case 'gzip':
|
|
301
|
+
return zlib.createGunzip({ chunkSize });
|
|
302
|
+
case 'br':
|
|
303
|
+
return zlib.createBrotliDecompress({ chunkSize });
|
|
304
|
+
case 'deflate':
|
|
305
|
+
return zlib.createInflate({ chunkSize });
|
|
306
|
+
case 'deflate-raw':
|
|
307
|
+
return zlib.createInflateRaw({ chunkSize });
|
|
308
|
+
case 'zstd':
|
|
309
|
+
if (LIB_ZSTD) {
|
|
310
|
+
return new LIB_ZSTD.ZstdDecompressTransform({ writableHighWaterMark: chunkSize });
|
|
311
|
+
}
|
|
312
|
+
break;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
260
315
|
class Request extends module_1.default {
|
|
261
316
|
static purgeMemory(percent = 1, limit = 0, parent) {
|
|
262
317
|
if (percent >= 1) {
|
|
@@ -1366,44 +1421,6 @@ class Request extends module_1.default {
|
|
|
1366
1421
|
url = new URL(uri);
|
|
1367
1422
|
options.url = url;
|
|
1368
1423
|
}
|
|
1369
|
-
const checkEncoding = (response, statusCode, contentEncoding = '') => {
|
|
1370
|
-
switch (statusCode) {
|
|
1371
|
-
case 206:
|
|
1372
|
-
request.emit('error', new Error('[ABORT] Partial content'));
|
|
1373
|
-
case 204:
|
|
1374
|
-
return;
|
|
1375
|
-
}
|
|
1376
|
-
const chunkSize = outStream === null || outStream === void 0 ? void 0 : outStream.writableHighWaterMark;
|
|
1377
|
-
let pipeTo;
|
|
1378
|
-
switch (contentEncoding.trim().toLowerCase()) {
|
|
1379
|
-
case 'gzip':
|
|
1380
|
-
pipeTo = zlib.createGunzip({ chunkSize });
|
|
1381
|
-
break;
|
|
1382
|
-
case 'br':
|
|
1383
|
-
pipeTo = zlib.createBrotliDecompress({ chunkSize });
|
|
1384
|
-
break;
|
|
1385
|
-
case 'deflate':
|
|
1386
|
-
pipeTo = zlib.createInflate({ chunkSize });
|
|
1387
|
-
break;
|
|
1388
|
-
case 'deflate-raw':
|
|
1389
|
-
pipeTo = zlib.createInflateRaw({ chunkSize });
|
|
1390
|
-
break;
|
|
1391
|
-
case 'zstd':
|
|
1392
|
-
if (LIB_ZSTD) {
|
|
1393
|
-
pipeTo = new LIB_ZSTD.ZstdDecompressTransform({ writableHighWaterMark: chunkSize });
|
|
1394
|
-
}
|
|
1395
|
-
break;
|
|
1396
|
-
}
|
|
1397
|
-
if (pipeTo) {
|
|
1398
|
-
if (outStream) {
|
|
1399
|
-
stream.pipeline(response, pipeTo, outStream, err => err && response.emit('error', err));
|
|
1400
|
-
}
|
|
1401
|
-
else {
|
|
1402
|
-
stream.pipeline(response, pipeTo, err => err && response.emit('error', err));
|
|
1403
|
-
}
|
|
1404
|
-
return pipeTo;
|
|
1405
|
-
}
|
|
1406
|
-
};
|
|
1407
1424
|
const { hostname, origin, secure, localhost } = host;
|
|
1408
1425
|
const pathname = url.pathname + (socketPath ? '' : url.search);
|
|
1409
1426
|
const proxy = this.proxyOf(uri, localhost);
|
|
@@ -1429,7 +1446,8 @@ class Request extends module_1.default {
|
|
|
1429
1446
|
connected = true;
|
|
1430
1447
|
const statusCode = response[':status'];
|
|
1431
1448
|
if (statusCode >= 200 && statusCode < 300) {
|
|
1432
|
-
|
|
1449
|
+
emitter = checkEncoding(request, request, statusCode, outStream, response['content-encoding']);
|
|
1450
|
+
if (emitter) {
|
|
1433
1451
|
for (const event in listenerMap) {
|
|
1434
1452
|
listenerMap[event].forEach(listener => {
|
|
1435
1453
|
const [name, type] = event.split('-');
|
|
@@ -1550,7 +1568,7 @@ class Request extends module_1.default {
|
|
|
1550
1568
|
const statusCode = response.statusCode;
|
|
1551
1569
|
if ((getting || posting) && statusCode >= 200 && statusCode < 300) {
|
|
1552
1570
|
const incoming = response.headers;
|
|
1553
|
-
let source = checkEncoding(response, statusCode, incoming['content-encoding']);
|
|
1571
|
+
let source = checkEncoding(request, response, statusCode, outStream, incoming['content-encoding']);
|
|
1554
1572
|
if (source) {
|
|
1555
1573
|
source.once('finish', () => request.emit('end'));
|
|
1556
1574
|
}
|
|
@@ -1788,6 +1806,9 @@ class Request extends module_1.default {
|
|
|
1788
1806
|
const startTime = log ? process.hrtime() : 0;
|
|
1789
1807
|
let retries = 0, redirects = 0, closed, timeout, outStream;
|
|
1790
1808
|
const throwError = (err, outAbort) => {
|
|
1809
|
+
if (timeout) {
|
|
1810
|
+
clearTimeout(timeout);
|
|
1811
|
+
}
|
|
1791
1812
|
if (!closed) {
|
|
1792
1813
|
closed = true;
|
|
1793
1814
|
if (outStream && (0, types_1.isString)(pipeTo)) {
|
|
@@ -1795,9 +1816,6 @@ class Request extends module_1.default {
|
|
|
1795
1816
|
}
|
|
1796
1817
|
reject(typeof err === 'string' ? new Error(err) : err);
|
|
1797
1818
|
}
|
|
1798
|
-
if (timeout) {
|
|
1799
|
-
clearTimeout(timeout);
|
|
1800
|
-
}
|
|
1801
1819
|
if (outAbort) {
|
|
1802
1820
|
this[kDownloading].delete(outAbort);
|
|
1803
1821
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/request",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.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.7.
|
|
24
|
-
"@e-mc/types": "0.7.
|
|
23
|
+
"@e-mc/module": "0.7.7",
|
|
24
|
+
"@e-mc/types": "0.7.7",
|
|
25
25
|
"combined-stream": "^1.0.8",
|
|
26
26
|
"js-yaml": "^4.1.0",
|
|
27
27
|
"which": "^2.0.2"
|