@e-mc/request 0.6.3 → 0.6.4

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.
Files changed (4) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +1 -1
  3. package/index.js +61 -43
  4. package/package.json +3 -3
package/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2023 Ninja Scroll
1
+ Copyright 2024 An Pham
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
4
 
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @e-mc/request
2
2
 
3
- PEP 402 - Forever Kagerō
3
+ PEP 402 - Forever Any Mayo
4
4
 
5
5
  ## LICENSE
6
6
 
package/index.js CHANGED
@@ -256,6 +256,61 @@ function validateCerts(certs) {
256
256
  }
257
257
  return [text, file];
258
258
  }
259
+ function checkEncoding(request, response, statusCode, outStream, contentEncoding = '') {
260
+ switch (statusCode) {
261
+ case 206:
262
+ request.emit('error', (0, types_1.errorValue)("Aborted", 'Partial content'));
263
+ case 204:
264
+ return;
265
+ default:
266
+ contentEncoding = contentEncoding.trim();
267
+ if (!contentEncoding) {
268
+ return;
269
+ }
270
+ contentEncoding = contentEncoding.toLowerCase();
271
+ break;
272
+ }
273
+ const chunkSize = outStream === null || outStream === void 0 ? void 0 : outStream.writableHighWaterMark;
274
+ let pipeTo;
275
+ if (contentEncoding.indexOf(',') === -1) {
276
+ pipeTo = decompressEncoding(contentEncoding, chunkSize);
277
+ }
278
+ else {
279
+ for (const value of contentEncoding.split(/\s*,\s*/).reverse()) {
280
+ const next = decompressEncoding(value, chunkSize);
281
+ if (!next) {
282
+ return;
283
+ }
284
+ pipeTo = pipeTo ? pipeTo.pipe(next) : next;
285
+ }
286
+ }
287
+ if (pipeTo) {
288
+ if (outStream) {
289
+ stream.pipeline(response, pipeTo, outStream, err => err && response.emit('error', err));
290
+ }
291
+ else {
292
+ stream.pipeline(response, pipeTo, err => err && response.emit('error', err));
293
+ }
294
+ return pipeTo;
295
+ }
296
+ }
297
+ function decompressEncoding(value, chunkSize) {
298
+ switch (value) {
299
+ case 'gzip':
300
+ return zlib.createGunzip({ chunkSize });
301
+ case 'br':
302
+ return zlib.createBrotliDecompress({ chunkSize });
303
+ case 'deflate':
304
+ return zlib.createInflate({ chunkSize });
305
+ case 'deflate-raw':
306
+ return zlib.createInflateRaw({ chunkSize });
307
+ case 'zstd':
308
+ if (LIB_ZSTD) {
309
+ return new LIB_ZSTD.ZstdDecompressTransform({ writableHighWaterMark: chunkSize });
310
+ }
311
+ break;
312
+ }
313
+ }
259
314
  class Request extends module_1.default {
260
315
  static purgeMemory(percent = 1, limit = 0, parent) {
261
316
  if (percent >= 1) {
@@ -1361,44 +1416,6 @@ class Request extends module_1.default {
1361
1416
  url = new URL(uri);
1362
1417
  options.url = url;
1363
1418
  }
1364
- const checkEncoding = (response, statusCode, contentEncoding = '') => {
1365
- switch (statusCode) {
1366
- case 206:
1367
- request.emit('error', new Error('[ABORT] Partial content'));
1368
- case 204:
1369
- return;
1370
- }
1371
- const chunkSize = outStream === null || outStream === void 0 ? void 0 : outStream.writableHighWaterMark;
1372
- let pipeTo;
1373
- switch (contentEncoding.trim().toLowerCase()) {
1374
- case 'gzip':
1375
- pipeTo = zlib.createGunzip({ chunkSize });
1376
- break;
1377
- case 'br':
1378
- pipeTo = zlib.createBrotliDecompress({ chunkSize });
1379
- break;
1380
- case 'deflate':
1381
- pipeTo = zlib.createInflate({ chunkSize });
1382
- break;
1383
- case 'deflate-raw':
1384
- pipeTo = zlib.createInflateRaw({ chunkSize });
1385
- break;
1386
- case 'zstd':
1387
- if (LIB_ZSTD) {
1388
- pipeTo = new LIB_ZSTD.ZstdDecompressTransform({ writableHighWaterMark: chunkSize });
1389
- }
1390
- break;
1391
- }
1392
- if (pipeTo) {
1393
- if (outStream) {
1394
- stream.pipeline(response, pipeTo, outStream, err => err && response.emit('error', err));
1395
- }
1396
- else {
1397
- stream.pipeline(response, pipeTo, err => err && response.emit('error', err));
1398
- }
1399
- return pipeTo;
1400
- }
1401
- };
1402
1419
  const { hostname, origin, secure, localhost } = host;
1403
1420
  const pathname = url.pathname + (socketPath ? '' : url.search);
1404
1421
  const proxy = this.proxyOf(uri, localhost);
@@ -1424,7 +1441,8 @@ class Request extends module_1.default {
1424
1441
  connected = true;
1425
1442
  const statusCode = response[':status'];
1426
1443
  if (statusCode >= 200 && statusCode < 300) {
1427
- if (emitter = checkEncoding(request, statusCode, response['content-encoding'])) {
1444
+ emitter = checkEncoding(request, request, statusCode, outStream, response['content-encoding']);
1445
+ if (emitter) {
1428
1446
  for (const event in listenerMap) {
1429
1447
  listenerMap[event].forEach(listener => {
1430
1448
  const [name, type] = event.split('-');
@@ -1545,7 +1563,7 @@ class Request extends module_1.default {
1545
1563
  const statusCode = response.statusCode;
1546
1564
  if ((getting || posting) && statusCode >= 200 && statusCode < 300) {
1547
1565
  const incoming = response.headers;
1548
- let source = checkEncoding(response, statusCode, incoming['content-encoding']);
1566
+ let source = checkEncoding(request, response, statusCode, outStream, incoming['content-encoding']);
1549
1567
  if (source) {
1550
1568
  source.once('finish', () => request.emit('end'));
1551
1569
  }
@@ -1783,6 +1801,9 @@ class Request extends module_1.default {
1783
1801
  const startTime = log ? process.hrtime() : 0;
1784
1802
  let retries = 0, redirects = 0, closed, timeout, outStream;
1785
1803
  const throwError = (err, outAbort) => {
1804
+ if (timeout) {
1805
+ clearTimeout(timeout);
1806
+ }
1786
1807
  if (!closed) {
1787
1808
  closed = true;
1788
1809
  if (outStream && (0, types_1.isString)(pipeTo)) {
@@ -1790,9 +1811,6 @@ class Request extends module_1.default {
1790
1811
  }
1791
1812
  reject(typeof err === 'string' ? new Error(err) : err);
1792
1813
  }
1793
- if (timeout) {
1794
- clearTimeout(timeout);
1795
- }
1796
1814
  if (outAbort) {
1797
1815
  this[kDownloading].delete(outAbort);
1798
1816
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/request",
3
- "version": "0.6.3",
3
+ "version": "0.6.4",
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.6.3",
24
- "@e-mc/types": "0.6.3",
23
+ "@e-mc/module": "0.6.4",
24
+ "@e-mc/types": "0.6.4",
25
25
  "combined-stream": "^1.0.8",
26
26
  "js-yaml": "^4.1.0",
27
27
  "which": "^2.0.2"