@e-mc/request 0.6.2 → 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.
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
 
@@ -154,6 +154,7 @@ class HttpHost {
154
154
  return ++data[2];
155
155
  }
156
156
  upgrade(version, altSvc) {
157
+ var _e;
157
158
  if (altSvc && this.secure) {
158
159
  if (altSvc === 'clear') {
159
160
  this.clearAltSvc();
@@ -187,7 +188,7 @@ class HttpHost {
187
188
  break;
188
189
  }
189
190
  const address = match[1] || hostname;
190
- const ma = +(/ma=(\d+)/.exec(match[3])?.[1] || 86400);
191
+ const ma = +(((_e = /ma=(\d+)/.exec(match[3])) === null || _e === void 0 ? void 0 : _e[1]) || 86400);
191
192
  if (!excluded.includes(`h${i + 1}:${address}:${port}`)) {
192
193
  addresses.push([
193
194
  address,
package/index.js CHANGED
@@ -132,7 +132,7 @@ function setOutgoingHeaders(output, headers) {
132
132
  }
133
133
  function getProxySettings(request, agentTimeout) {
134
134
  const proxy = request.proxy;
135
- if (proxy?.address && proxy.port) {
135
+ if ((proxy === null || proxy === void 0 ? void 0 : proxy.address) && proxy.port) {
136
136
  const port = (0, util_1.asInt)(proxy.port);
137
137
  const address = (!module_1.default.isURL(proxy.address) ? port === 80 ? 'http://' : 'https://' : '') + proxy.address;
138
138
  try {
@@ -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) {
@@ -271,6 +326,7 @@ class Request extends module_1.default {
271
326
  return parent ? super.purgeMemory(percent, limit) : Promise.resolve(0);
272
327
  }
273
328
  static loadSettings(settings, password) {
329
+ var _l;
274
330
  if (!this.enabled("process.password") || super.loadSettings({ process: settings.process }, password)) {
275
331
  const { request, download } = settings;
276
332
  if (download && (0, types_1.isPlainObject)(download.aria2)) {
@@ -387,7 +443,7 @@ class Request extends module_1.default {
387
443
  [TLS.TEXT, TLS.FILE] = validateCerts(certs);
388
444
  }
389
445
  HTTP.PROXY = getProxySettings(request, agent_timeout);
390
- const time_format = request.settings?.time_format;
446
+ const time_format = (_l = request.settings) === null || _l === void 0 ? void 0 : _l.time_format;
391
447
  switch (time_format) {
392
448
  case 'readable':
393
449
  case 'relative':
@@ -609,6 +665,7 @@ class Request extends module_1.default {
609
665
  return ARIA2.BIN;
610
666
  }
611
667
  constructor(data) {
668
+ var _l;
612
669
  super();
613
670
  this.startTime = Date.now();
614
671
  this.readExpect = 'none';
@@ -638,13 +695,13 @@ class Request extends module_1.default {
638
695
  const timeout = (0, util_1.fromSeconds)(data.timeout);
639
696
  let value;
640
697
  this.readTimeout = (value = (0, util_1.fromSeconds)(read_timeout)) >= 0 ? value : READ_TIMEOUT;
641
- this.keepAlive = typeof (value = agent?.keep_alive) === 'boolean' ? value : KEEP_ALIVE;
642
- this.acceptEncoding = typeof (value = use?.accept_encoding) === 'boolean' ? value : ACCEPT_ENCODING;
643
- if ((value = (0, util_1.asInt)(use?.http_version)) === 1 || value === 2) {
698
+ this.keepAlive = typeof (value = agent === null || agent === void 0 ? void 0 : agent.keep_alive) === 'boolean' ? value : KEEP_ALIVE;
699
+ this.acceptEncoding = typeof (value = use === null || use === void 0 ? void 0 : use.accept_encoding) === 'boolean' ? value : ACCEPT_ENCODING;
700
+ if ((value = (0, util_1.asInt)(use === null || use === void 0 ? void 0 : use.http_version)) === 1 || value === 2) {
644
701
  this[kHttpVersion] = value;
645
702
  }
646
- this[kIpVersion] = (value = (0, util_1.asInt)(data.dns?.family)) && (value === 4 || value === 6) ? value : 0;
647
- if ((value = (0, util_1.fromSeconds)(agent?.timeout)) >= 0) {
703
+ this[kIpVersion] = (value = (0, util_1.asInt)((_l = data.dns) === null || _l === void 0 ? void 0 : _l.family)) && (value === 4 || value === 6) ? value : 0;
704
+ if ((value = (0, util_1.fromSeconds)(agent === null || agent === void 0 ? void 0 : agent.timeout)) >= 0) {
648
705
  this[kAgentTimeout] = value;
649
706
  }
650
707
  else {
@@ -686,6 +743,7 @@ class Request extends module_1.default {
686
743
  this.module = data;
687
744
  }
688
745
  flushLog() {
746
+ var _l;
689
747
  if (this[kSingleton]) {
690
748
  this._logQueued.length = 0;
691
749
  if (LOG_HTTP) {
@@ -717,7 +775,7 @@ class Request extends module_1.default {
717
775
  count = Math.max(count, value);
718
776
  }
719
777
  });
720
- if (!this.host?.aborted) {
778
+ if (!((_l = this.host) === null || _l === void 0 ? void 0 : _l.aborted)) {
721
779
  output.sort((a, b) => {
722
780
  if (a[2] === b[2]) {
723
781
  return a[1] < b[1] ? -1 : 1;
@@ -1016,8 +1074,9 @@ class Request extends module_1.default {
1016
1074
  if (!module_1.default.createDir(pathname)) {
1017
1075
  return Promise.reject((0, types_1.errorMessage)("aria2", "Path is not a directory", pathname));
1018
1076
  }
1019
- silent ?? (silent = this[kSingleton]);
1077
+ silent !== null && silent !== void 0 ? silent : (silent = this[kSingleton]);
1020
1078
  return new Promise((resolve, reject) => {
1079
+ var _l;
1021
1080
  let protocol, origin, username, password;
1022
1081
  if (uri instanceof URL) {
1023
1082
  ({ protocol, origin, username, password, href: uri } = uri);
@@ -1075,7 +1134,7 @@ class Request extends module_1.default {
1075
1134
  '--file-allocation=' + ARIA2.FILE_ALLOCATION,
1076
1135
  '--max-tries=' + (retryLimit + 1)
1077
1136
  ];
1078
- const ignoreOpt = (...values) => !binOpts?.some(item => values.includes(item));
1137
+ const ignoreOpt = (...values) => !(binOpts === null || binOpts === void 0 ? void 0 : binOpts.some(item => values.includes(item)));
1079
1138
  if (ARIA2.MAX_CONCURRENT_DOWNLOADS) {
1080
1139
  opts.push('--max-concurrent-downloads=' + ARIA2.MAX_CONCURRENT_DOWNLOADS);
1081
1140
  }
@@ -1124,7 +1183,7 @@ class Request extends module_1.default {
1124
1183
  }
1125
1184
  }
1126
1185
  if (origin) {
1127
- const secure = this[kCerts]?.[1][origin] || (this.host ? TLS.FILE[origin] : null);
1186
+ const secure = ((_l = this[kCerts]) === null || _l === void 0 ? void 0 : _l[1][origin]) || (this.host ? TLS.FILE[origin] : null);
1128
1187
  if (secure) {
1129
1188
  if (secure.ca && ignoreOpt('--ca-certificate')) {
1130
1189
  args.push(`--ca-certificate="${escapeQuote(secure.ca)}"`);
@@ -1269,7 +1328,7 @@ class Request extends module_1.default {
1269
1328
  }
1270
1329
  else {
1271
1330
  const current = (0, types_1.getLogCurrent)();
1272
- progressBar = current?.type === 128 && current.title === "aria2";
1331
+ progressBar = (current === null || current === void 0 ? void 0 : current.type) === 128 && current.title === "aria2";
1273
1332
  }
1274
1333
  this.formatMessage(128, "aria2", ['Downloading...', (0, types_1.formatTime)(startTime, true)], (PLATFORM_WIN32 ? 'taskkill /f /pid' : 'kill') + ` ${item[0]} -> ` + item[1], { ...module_1.default.LOG_STYLE_INFO, progressBar, broadcastId });
1275
1334
  }
@@ -1308,7 +1367,8 @@ class Request extends module_1.default {
1308
1367
  return { ...options, host, url };
1309
1368
  }
1310
1369
  open(uri, options) {
1311
- var _l, _m;
1370
+ var _l;
1371
+ var _m, _o;
1312
1372
  let { host, url, httpVersion, method = 'GET', encoding, format, headers, postData, keepAlive, agentTimeout, socketPath, timeout = this._config.connectTimeout, outStream } = options;
1313
1373
  const getting = method === 'GET';
1314
1374
  const posting = method === 'POST';
@@ -1356,60 +1416,22 @@ class Request extends module_1.default {
1356
1416
  url = new URL(uri);
1357
1417
  options.url = url;
1358
1418
  }
1359
- const checkEncoding = (response, statusCode, contentEncoding = '') => {
1360
- switch (statusCode) {
1361
- case 206:
1362
- request.emit('error', new Error('[ABORT] Partial content'));
1363
- case 204:
1364
- return;
1365
- }
1366
- const chunkSize = outStream?.writableHighWaterMark;
1367
- let pipeTo;
1368
- switch (contentEncoding.trim().toLowerCase()) {
1369
- case 'gzip':
1370
- pipeTo = zlib.createGunzip({ chunkSize });
1371
- break;
1372
- case 'br':
1373
- pipeTo = zlib.createBrotliDecompress({ chunkSize });
1374
- break;
1375
- case 'deflate':
1376
- pipeTo = zlib.createInflate({ chunkSize });
1377
- break;
1378
- case 'deflate-raw':
1379
- pipeTo = zlib.createInflateRaw({ chunkSize });
1380
- break;
1381
- case 'zstd':
1382
- if (LIB_ZSTD) {
1383
- pipeTo = new LIB_ZSTD.ZstdDecompressTransform({ writableHighWaterMark: chunkSize });
1384
- }
1385
- break;
1386
- }
1387
- if (pipeTo) {
1388
- if (outStream) {
1389
- stream.pipeline(response, pipeTo, outStream, err => err && response.emit('error', err));
1390
- }
1391
- else {
1392
- stream.pipeline(response, pipeTo, err => err && response.emit('error', err));
1393
- }
1394
- return pipeTo;
1395
- }
1396
- };
1397
1419
  const { hostname, origin, secure, localhost } = host;
1398
1420
  const pathname = url.pathname + (socketPath ? '' : url.search);
1399
1421
  const proxy = this.proxyOf(uri, localhost);
1400
1422
  const version = this.httpVersion;
1401
1423
  let request, ca, cert, key, minVersion, baseHeaders = this.headersOf(uri);
1402
- if (getting && this.acceptEncoding && !localhost && !baseHeaders?.['accept-encoding']) {
1403
- (_l = (headers || (headers = {})))['accept-encoding'] || (_l['accept-encoding'] = 'gzip, deflate, br' + (LIB_ZSTD ? ', zstd' : ''));
1424
+ if (getting && this.acceptEncoding && !localhost && !(baseHeaders === null || baseHeaders === void 0 ? void 0 : baseHeaders['accept-encoding'])) {
1425
+ (_m = (headers || (headers = {})))['accept-encoding'] || (_m['accept-encoding'] = 'gzip, deflate, br' + (LIB_ZSTD ? ', zstd' : ''));
1404
1426
  }
1405
1427
  if (secure) {
1406
- const certs = this[kCerts]?.[0][origin] || (this.host ? TLS.TEXT[origin] : null);
1428
+ const certs = ((_l = this[kCerts]) === null || _l === void 0 ? void 0 : _l[0][origin]) || (this.host ? TLS.TEXT[origin] : null);
1407
1429
  if (certs) {
1408
1430
  ({ ca, cert, key, version: minVersion } = certs);
1409
1431
  }
1410
1432
  }
1411
1433
  if (!proxy && httpVersion !== 1 && ((httpVersion || host.version) === 2 && version !== 1 || secure && version === 2 && host.failed(2, true) === 0)) {
1412
- request = ((_m = this[kSession][0])[origin] || (_m[origin] = http2.connect(origin, { lookup: this.lookupDns(hostname), ca, cert, key, minVersion, settings: localhost ? { maxFrameSize: 16777215, enablePush: false } : { enablePush: false } }))).request({ ...baseHeaders, ...host_1.default.getBasicAuth(url), ...headers, ':path': pathname, ':method': method });
1434
+ request = ((_o = this[kSession][0])[origin] || (_o[origin] = http2.connect(origin, { lookup: this.lookupDns(hostname), ca, cert, key, minVersion, settings: localhost ? { maxFrameSize: 16777215, enablePush: false } : { enablePush: false } }))).request({ ...baseHeaders, ...host_1.default.getBasicAuth(url), ...headers, ':path': pathname, ':method': method });
1413
1435
  if (getting) {
1414
1436
  const listenerMap = {};
1415
1437
  const onEvent = request.on.bind(request);
@@ -1419,7 +1441,8 @@ class Request extends module_1.default {
1419
1441
  connected = true;
1420
1442
  const statusCode = response[':status'];
1421
1443
  if (statusCode >= 200 && statusCode < 300) {
1422
- if (emitter = checkEncoding(request, statusCode, response['content-encoding'])) {
1444
+ emitter = checkEncoding(request, request, statusCode, outStream, response['content-encoding']);
1445
+ if (emitter) {
1423
1446
  for (const event in listenerMap) {
1424
1447
  listenerMap[event].forEach(listener => {
1425
1448
  const [name, type] = event.split('-');
@@ -1492,8 +1515,8 @@ class Request extends module_1.default {
1492
1515
  const pkg = secure ? 'https-proxy-agent' : 'http-proxy-agent';
1493
1516
  try {
1494
1517
  const { protocol, hostname: proxyname, port, username, password, href } = proxy.host;
1495
- keepAlive ?? (keepAlive = proxy.keepAlive || false);
1496
- agentTimeout ?? (agentTimeout = proxy.agentTimeout);
1518
+ keepAlive !== null && keepAlive !== void 0 ? keepAlive : (keepAlive = proxy.keepAlive || false);
1519
+ agentTimeout !== null && agentTimeout !== void 0 ? agentTimeout : (agentTimeout = proxy.agentTimeout);
1497
1520
  agent = require(pkg)(keepAlive || agentTimeout > 0 ? { protocol, hostname: proxyname, port, username, password, keepAlive, timeout: agentTimeout } : href);
1498
1521
  const proxyHeaders = this[kHeaders] && getBaseHeaders(href, this[kHeaders]) || getBaseHeaders(href, HTTP.HEADERS);
1499
1522
  if (proxyHeaders) {
@@ -1512,7 +1535,7 @@ class Request extends module_1.default {
1512
1535
  }
1513
1536
  else if (agentTimeout !== 0) {
1514
1537
  keepAlive = this.keepAlive || false;
1515
- agentTimeout ?? (agentTimeout = this.agentTimeout);
1538
+ agentTimeout !== null && agentTimeout !== void 0 ? agentTimeout : (agentTimeout = this.agentTimeout);
1516
1539
  if (keepAlive || agentTimeout > 0) {
1517
1540
  agent = new (secure ? https.Agent : http.Agent)({ keepAlive, timeout: agentTimeout });
1518
1541
  }
@@ -1536,10 +1559,11 @@ class Request extends module_1.default {
1536
1559
  lookup: this.lookupDns(hostname),
1537
1560
  agent
1538
1561
  }, response => {
1562
+ var _l;
1539
1563
  const statusCode = response.statusCode;
1540
1564
  if ((getting || posting) && statusCode >= 200 && statusCode < 300) {
1541
1565
  const incoming = response.headers;
1542
- let source = checkEncoding(response, statusCode, incoming['content-encoding']);
1566
+ let source = checkEncoding(request, response, statusCode, outStream, incoming['content-encoding']);
1543
1567
  if (source) {
1544
1568
  source.once('finish', () => request.emit('end'));
1545
1569
  }
@@ -1571,7 +1595,7 @@ class Request extends module_1.default {
1571
1595
  source.on('error', err => request.emit('error', err));
1572
1596
  }
1573
1597
  if (!posting) {
1574
- if (version === 2 && incoming.upgrade?.includes('h2')) {
1598
+ if (version === 2 && ((_l = incoming.upgrade) === null || _l === void 0 ? void 0 : _l.includes('h2'))) {
1575
1599
  host.version = 2;
1576
1600
  }
1577
1601
  else if (!host.didAltSvc(1)) {
@@ -1769,7 +1793,7 @@ class Request extends module_1.default {
1769
1793
  async get(uri, options = {}) {
1770
1794
  const opts = (typeof options === 'string' ? { format: options, encoding: 'utf-8' } : options);
1771
1795
  if (this.readExpect === 'string') {
1772
- opts.encoding = (0, types_1.getEncoding)(opts?.encoding);
1796
+ opts.encoding = (0, types_1.getEncoding)(opts === null || opts === void 0 ? void 0 : opts.encoding);
1773
1797
  }
1774
1798
  return new Promise((resolve, reject) => {
1775
1799
  const { silent = this[kSingleton], pipeTo } = opts;
@@ -1777,6 +1801,9 @@ class Request extends module_1.default {
1777
1801
  const startTime = log ? process.hrtime() : 0;
1778
1802
  let retries = 0, redirects = 0, closed, timeout, outStream;
1779
1803
  const throwError = (err, outAbort) => {
1804
+ if (timeout) {
1805
+ clearTimeout(timeout);
1806
+ }
1780
1807
  if (!closed) {
1781
1808
  closed = true;
1782
1809
  if (outStream && (0, types_1.isString)(pipeTo)) {
@@ -1784,9 +1811,6 @@ class Request extends module_1.default {
1784
1811
  }
1785
1812
  reject(typeof err === 'string' ? new Error(err) : err);
1786
1813
  }
1787
- if (timeout) {
1788
- clearTimeout(timeout);
1789
- }
1790
1814
  if (outAbort) {
1791
1815
  this[kDownloading].delete(outAbort);
1792
1816
  }
@@ -1843,19 +1867,21 @@ class Request extends module_1.default {
1843
1867
  downloadUri.call(this, href);
1844
1868
  };
1845
1869
  const acceptResponse = (headers) => {
1870
+ var _l;
1846
1871
  if ('outHeaders' in opts) {
1847
1872
  opts.outHeaders = headers;
1848
1873
  }
1849
1874
  if ('outFilename' in opts) {
1850
1875
  opts.outFilename = (0, util_1.parseHeader)(headers, 'content-disposition');
1851
1876
  }
1852
- const buffering = request.connected?.call(client, headers);
1877
+ const buffering = (_l = request.connected) === null || _l === void 0 ? void 0 : _l.call(client, headers);
1853
1878
  const pipeline = pipeTo ? !(0, types_1.isString)(pipeTo) : false;
1854
1879
  const enabled = buffering !== false && !pipeline;
1855
1880
  const readTimeout = this.readTimeout;
1856
1881
  let mibsTime, delayTime;
1857
1882
  if (log || readTimeout > 0) {
1858
1883
  client.once('readable', () => {
1884
+ var _l;
1859
1885
  if (readTimeout > 0) {
1860
1886
  timeout = setTimeout(() => {
1861
1887
  abortResponse();
@@ -1866,7 +1892,7 @@ class Request extends module_1.default {
1866
1892
  if (buffering === false) {
1867
1893
  mibsTime = process.hrtime();
1868
1894
  }
1869
- switch (this.settings?.time_format || LOG_TIMEFORMAT) {
1895
+ switch (((_l = this.settings) === null || _l === void 0 ? void 0 : _l.time_format) || LOG_TIMEFORMAT) {
1870
1896
  case 'readable':
1871
1897
  delayTime = process.hrtime(startTime);
1872
1898
  break;
@@ -2193,13 +2219,14 @@ class Request extends module_1.default {
2193
2219
  this[kDownloading].clear();
2194
2220
  }
2195
2221
  set agentTimeout(value) {
2222
+ var _l, _m;
2196
2223
  if (value > 0) {
2197
2224
  this[kAgentTimeout] = value;
2198
- this.keepAlive ?? (this.keepAlive = true);
2225
+ (_l = this.keepAlive) !== null && _l !== void 0 ? _l : (this.keepAlive = true);
2199
2226
  }
2200
2227
  else {
2201
2228
  this[kAgentTimeout] = 0;
2202
- this.keepAlive ?? (this.keepAlive = false);
2229
+ (_m = this.keepAlive) !== null && _m !== void 0 ? _m : (this.keepAlive = false);
2203
2230
  }
2204
2231
  }
2205
2232
  get agentTimeout() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/request",
3
- "version": "0.6.2",
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.2",
24
- "@e-mc/types": "0.6.2",
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"