@e-mc/request 0.5.5 → 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/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2023 Anya Forger
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
3
+ PEP 402 - Forever Any Ketchup
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();
@@ -186,7 +187,7 @@ class HttpHost {
186
187
  break;
187
188
  }
188
189
  const address = match[1] || hostname;
189
- const ma = +(/ma=(\d+)/.exec(match[3])?.[1] || 86400);
190
+ const ma = +(((_e = /ma=(\d+)/.exec(match[3])) === null || _e === void 0 ? void 0 : _e[1]) || 86400);
190
191
  if (!this[kAltSvcError].includes(`h${i + 1}:${address}:${port}`)) {
191
192
  addresses.push([
192
193
  address,
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) {
@@ -263,6 +313,7 @@ class Request extends module_1.default {
263
313
  return parent ? super.purgeMemory(percent, limit) : Promise.resolve(0);
264
314
  }
265
315
  static loadSettings(settings, password) {
316
+ var _l;
266
317
  if (!this.enabled("process.password") || super.loadSettings({ process: settings.process }, password)) {
267
318
  const { request, download } = settings;
268
319
  if (download && (0, types_1.isPlainObject)(download.aria2)) {
@@ -374,7 +425,7 @@ class Request extends module_1.default {
374
425
  [TLS.TEXT, TLS.FILE] = validateCerts(certs);
375
426
  }
376
427
  HTTP.PROXY = getProxySettings(request, agent_timeout);
377
- const time_format = request.settings?.time_format;
428
+ const time_format = (_l = request.settings) === null || _l === void 0 ? void 0 : _l.time_format;
378
429
  switch (time_format) {
379
430
  case 'readable':
380
431
  case 'relative':
@@ -579,6 +630,7 @@ class Request extends module_1.default {
579
630
  return ARIA2.BIN;
580
631
  }
581
632
  constructor(data) {
633
+ var _l;
582
634
  super();
583
635
  this.startTime = Date.now();
584
636
  this.readExpect = 'none';
@@ -608,16 +660,16 @@ class Request extends module_1.default {
608
660
  const timeout = (0, util_1.fromSeconds)(data.timeout);
609
661
  let value;
610
662
  this.readTimeout = (value = (0, util_1.fromSeconds)(read_timeout)) >= 0 ? value : READ_TIMEOUT;
611
- this.keepAlive = typeof (value = agent?.keep_alive) === 'boolean' ? value : KEEP_ALIVE;
612
- this.acceptEncoding = typeof (value = use?.accept_encoding) === 'boolean' ? value : ACCEPT_ENCODING;
613
- switch (value = (0, util_1.asInt)(use?.http_version)) {
663
+ this.keepAlive = typeof (value = agent === null || agent === void 0 ? void 0 : agent.keep_alive) === 'boolean' ? value : KEEP_ALIVE;
664
+ this.acceptEncoding = typeof (value = use === null || use === void 0 ? void 0 : use.accept_encoding) === 'boolean' ? value : ACCEPT_ENCODING;
665
+ switch (value = (0, util_1.asInt)(use === null || use === void 0 ? void 0 : use.http_version)) {
614
666
  case 1:
615
667
  case 2:
616
668
  this[kHttpVersion] = value;
617
669
  break;
618
670
  }
619
- this[kIpVersion] = (value = (0, util_1.asInt)(data.dns?.family)) && (value === 4 || value === 6) ? value : 0;
620
- if ((value = (0, util_1.fromSeconds)(agent?.timeout)) >= 0) {
671
+ this[kIpVersion] = (value = (0, util_1.asInt)((_l = data.dns) === null || _l === void 0 ? void 0 : _l.family)) && (value === 4 || value === 6) ? value : 0;
672
+ if ((value = (0, util_1.fromSeconds)(agent === null || agent === void 0 ? void 0 : agent.timeout)) >= 0) {
621
673
  this[kAgentTimeout] = value;
622
674
  }
623
675
  else {
@@ -659,6 +711,7 @@ class Request extends module_1.default {
659
711
  this.module = data;
660
712
  }
661
713
  flushLog() {
714
+ var _l;
662
715
  if (this[kSingleton]) {
663
716
  this._logQueued.length = 0;
664
717
  if (LOG_HTTP) {
@@ -690,7 +743,7 @@ class Request extends module_1.default {
690
743
  count = Math.max(count, value);
691
744
  }
692
745
  });
693
- if (!this.host?.aborted) {
746
+ if (!((_l = this.host) === null || _l === void 0 ? void 0 : _l.aborted)) {
694
747
  output.sort((a, b) => {
695
748
  if (a[2] === b[2]) {
696
749
  return a[1] < b[1] ? -1 : 1;
@@ -990,8 +1043,9 @@ class Request extends module_1.default {
990
1043
  if (!module_1.default.createDir(pathname)) {
991
1044
  return Promise.reject((0, types_1.errorMessage)("aria2", "Path is not a directory", pathname));
992
1045
  }
993
- silent ?? (silent = this[kSingleton]);
1046
+ silent !== null && silent !== void 0 ? silent : (silent = this[kSingleton]);
994
1047
  return new Promise((resolve, reject) => {
1048
+ var _l;
995
1049
  let protocol, origin, username, password;
996
1050
  if (uri instanceof URL) {
997
1051
  ({ protocol, origin, username, password, href: uri } = uri);
@@ -1049,7 +1103,7 @@ class Request extends module_1.default {
1049
1103
  '--file-allocation=' + ARIA2.FILE_ALLOCATION,
1050
1104
  '--max-tries=' + (retryLimit + 1)
1051
1105
  ];
1052
- const ignoreOpt = (...values) => !binOpts?.some(item => values.includes(item));
1106
+ const ignoreOpt = (...values) => !(binOpts === null || binOpts === void 0 ? void 0 : binOpts.some(item => values.includes(item)));
1053
1107
  if (ARIA2.MAX_CONCURRENT_DOWNLOADS) {
1054
1108
  opts.push('--max-concurrent-downloads=' + ARIA2.MAX_CONCURRENT_DOWNLOADS);
1055
1109
  }
@@ -1098,7 +1152,7 @@ class Request extends module_1.default {
1098
1152
  }
1099
1153
  }
1100
1154
  if (origin) {
1101
- const secure = this[kCerts]?.[1][origin] || (this.host ? TLS.FILE[origin] : null);
1155
+ const secure = ((_l = this[kCerts]) === null || _l === void 0 ? void 0 : _l[1][origin]) || (this.host ? TLS.FILE[origin] : null);
1102
1156
  if (secure) {
1103
1157
  if (secure.ca && ignoreOpt('--ca-certificate')) {
1104
1158
  args.push(`--ca-certificate="${escapeQuote(secure.ca)}"`);
@@ -1243,7 +1297,7 @@ class Request extends module_1.default {
1243
1297
  }
1244
1298
  else {
1245
1299
  const current = (0, types_1.getLogCurrent)();
1246
- progressBar = current?.type === 128 && current.title === "aria2";
1300
+ progressBar = (current === null || current === void 0 ? void 0 : current.type) === 128 && current.title === "aria2";
1247
1301
  }
1248
1302
  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 });
1249
1303
  }
@@ -1282,7 +1336,8 @@ class Request extends module_1.default {
1282
1336
  return { ...options, host, url };
1283
1337
  }
1284
1338
  open(uri, options) {
1285
- var _l, _m;
1339
+ var _l;
1340
+ var _m, _o;
1286
1341
  let { host, url, httpVersion, method = 'GET', encoding, format, headers, postData, keepAlive, agentTimeout, socketPath, timeout = this._config.connectTimeout, outStream } = options;
1287
1342
  const getting = method === 'GET';
1288
1343
  const posting = method === 'POST';
@@ -1330,54 +1385,21 @@ class Request extends module_1.default {
1330
1385
  url = new URL(uri);
1331
1386
  options.url = url;
1332
1387
  }
1333
- const checkEncoding = (response, statusCode, contentEncoding = '') => {
1334
- switch (statusCode) {
1335
- case 206:
1336
- request.emit('error', new Error('[ABORT] Partial content'));
1337
- case 204:
1338
- return;
1339
- }
1340
- const chunkSize = outStream?.writableHighWaterMark;
1341
- let pipeTo;
1342
- switch (contentEncoding.trim().toLowerCase()) {
1343
- case 'gzip':
1344
- pipeTo = zlib.createGunzip({ chunkSize });
1345
- break;
1346
- case 'br':
1347
- pipeTo = zlib.createBrotliDecompress({ chunkSize });
1348
- break;
1349
- case 'deflate':
1350
- pipeTo = zlib.createInflate({ chunkSize });
1351
- break;
1352
- case 'deflate-raw':
1353
- pipeTo = zlib.createInflateRaw({ chunkSize });
1354
- break;
1355
- }
1356
- if (pipeTo) {
1357
- if (outStream) {
1358
- stream.pipeline(response, pipeTo, outStream, err => err && response.emit('error', err));
1359
- }
1360
- else {
1361
- stream.pipeline(response, pipeTo, err => err && response.emit('error', err));
1362
- }
1363
- return pipeTo;
1364
- }
1365
- };
1366
1388
  const { hostname, origin } = host;
1367
1389
  const pathname = url.pathname + (socketPath ? '' : url.search);
1368
1390
  const proxy = this.proxyOf(uri, host.localhost);
1369
1391
  let request, ca, cert, key, minVersion, baseHeaders = this.headersOf(uri);
1370
- if (getting && this.acceptEncoding && !host.localhost && !baseHeaders?.['accept-encoding']) {
1371
- (_l = (headers || (headers = {})))['accept-encoding'] || (_l['accept-encoding'] = 'gzip, deflate, br');
1392
+ if (getting && this.acceptEncoding && !host.localhost && !(baseHeaders === null || baseHeaders === void 0 ? void 0 : baseHeaders['accept-encoding'])) {
1393
+ (_m = (headers || (headers = {})))['accept-encoding'] || (_m['accept-encoding'] = 'gzip, deflate, br');
1372
1394
  }
1373
1395
  if (host.secure) {
1374
- const secure = this[kCerts]?.[0][origin] || (this.host ? TLS.TEXT[origin] : null);
1396
+ const secure = ((_l = this[kCerts]) === null || _l === void 0 ? void 0 : _l[0][origin]) || (this.host ? TLS.TEXT[origin] : null);
1375
1397
  if (secure) {
1376
1398
  ({ ca, cert, key, version: minVersion } = secure);
1377
1399
  }
1378
1400
  }
1379
1401
  if (!proxy && httpVersion !== 1 && ((httpVersion || host.version) === 2 && this.httpVersion !== 1 || host.secure && this.httpVersion === 2 && host.failed(2, true) === 0)) {
1380
- request = ((_m = this[kSession][0])[origin] || (_m[origin] = http2.connect(origin, { lookup: this.lookupDns(hostname), ca, cert, key, minVersion }))).request({ ...baseHeaders, ...host_1.default.getBasicAuth(url), ...headers, ':path': pathname, ':method': method });
1402
+ request = ((_o = this[kSession][0])[origin] || (_o[origin] = http2.connect(origin, { lookup: this.lookupDns(hostname), ca, cert, key, minVersion }))).request({ ...baseHeaders, ...host_1.default.getBasicAuth(url), ...headers, ':path': pathname, ':method': method });
1381
1403
  if (getting) {
1382
1404
  const listenerMap = {};
1383
1405
  const onEvent = request.on.bind(request);
@@ -1387,7 +1409,8 @@ class Request extends module_1.default {
1387
1409
  connected = true;
1388
1410
  const statusCode = response[':status'];
1389
1411
  if (statusCode >= 200 && statusCode < 300) {
1390
- if (emitter = checkEncoding(request, statusCode, response['content-encoding'])) {
1412
+ emitter = checkEncoding(request, request, statusCode, outStream, response['content-encoding']);
1413
+ if (emitter) {
1391
1414
  for (const event in listenerMap) {
1392
1415
  listenerMap[event].forEach(listener => {
1393
1416
  const [name, type] = event.split('-');
@@ -1460,8 +1483,8 @@ class Request extends module_1.default {
1460
1483
  const pkg = host.secure ? 'https-proxy-agent' : 'http-proxy-agent';
1461
1484
  try {
1462
1485
  const { protocol, hostname: proxyname, port, username, password, href } = proxy.host;
1463
- keepAlive ?? (keepAlive = proxy.keepAlive || false);
1464
- agentTimeout ?? (agentTimeout = proxy.agentTimeout);
1486
+ keepAlive !== null && keepAlive !== void 0 ? keepAlive : (keepAlive = proxy.keepAlive || false);
1487
+ agentTimeout !== null && agentTimeout !== void 0 ? agentTimeout : (agentTimeout = proxy.agentTimeout);
1465
1488
  agent = require(pkg)(keepAlive || agentTimeout > 0 ? { protocol, hostname: proxyname, port, username, password, keepAlive, timeout: agentTimeout } : href);
1466
1489
  const proxyHeaders = this[kHeaders] && getBaseHeaders(href, this[kHeaders]) || getBaseHeaders(href, HTTP.HEADERS);
1467
1490
  if (proxyHeaders) {
@@ -1480,7 +1503,7 @@ class Request extends module_1.default {
1480
1503
  }
1481
1504
  else if (agentTimeout !== 0) {
1482
1505
  keepAlive = this.keepAlive || false;
1483
- agentTimeout ?? (agentTimeout = this.agentTimeout);
1506
+ agentTimeout !== null && agentTimeout !== void 0 ? agentTimeout : (agentTimeout = this.agentTimeout);
1484
1507
  if (keepAlive || agentTimeout > 0) {
1485
1508
  agent = new (host.secure ? https.Agent : http.Agent)({ keepAlive, timeout: agentTimeout });
1486
1509
  }
@@ -1506,7 +1529,7 @@ class Request extends module_1.default {
1506
1529
  }, response => {
1507
1530
  const statusCode = response.statusCode;
1508
1531
  if ((getting || posting) && statusCode >= 200 && statusCode < 300) {
1509
- let source = checkEncoding(response, statusCode, response.headers['content-encoding']);
1532
+ let source = checkEncoding(request, response, statusCode, outStream, response.headers['content-encoding']);
1510
1533
  if (source) {
1511
1534
  source.once('finish', () => request.emit('end'));
1512
1535
  }
@@ -1734,7 +1757,7 @@ class Request extends module_1.default {
1734
1757
  async get(uri, options = {}) {
1735
1758
  const opts = (typeof options === 'string' ? { format: options, encoding: 'utf-8' } : options);
1736
1759
  if (this.readExpect === 'string') {
1737
- opts.encoding = (0, types_1.getEncoding)(opts?.encoding);
1760
+ opts.encoding = (0, types_1.getEncoding)(opts === null || opts === void 0 ? void 0 : opts.encoding);
1738
1761
  }
1739
1762
  return new Promise((resolve, reject) => {
1740
1763
  const { silent = this[kSingleton], pipeTo } = opts;
@@ -1742,6 +1765,9 @@ class Request extends module_1.default {
1742
1765
  const startTime = log ? process.hrtime() : 0;
1743
1766
  let retries = 0, redirects = 0, closed, timeout, outStream;
1744
1767
  const throwError = (err, outAbort) => {
1768
+ if (timeout) {
1769
+ clearTimeout(timeout);
1770
+ }
1745
1771
  if (!closed) {
1746
1772
  closed = true;
1747
1773
  if (outStream && (0, types_1.isString)(pipeTo)) {
@@ -1749,9 +1775,6 @@ class Request extends module_1.default {
1749
1775
  }
1750
1776
  reject(typeof err === 'string' ? new Error(err) : err);
1751
1777
  }
1752
- if (timeout) {
1753
- clearTimeout(timeout);
1754
- }
1755
1778
  if (outAbort) {
1756
1779
  this[kDownloading].delete(outAbort);
1757
1780
  }
@@ -1808,18 +1831,20 @@ class Request extends module_1.default {
1808
1831
  downloadUri.call(this, href);
1809
1832
  };
1810
1833
  const acceptResponse = (headers) => {
1834
+ var _l;
1811
1835
  if ('outHeaders' in opts) {
1812
1836
  opts.outHeaders = headers;
1813
1837
  }
1814
1838
  if ('outFilename' in opts) {
1815
1839
  opts.outFilename = (0, util_1.parseHeader)(headers, 'content-disposition');
1816
1840
  }
1817
- const buffering = request.connected?.call(client, headers);
1841
+ const buffering = (_l = request.connected) === null || _l === void 0 ? void 0 : _l.call(client, headers);
1818
1842
  const pipeline = pipeTo ? !(0, types_1.isString)(pipeTo) : false;
1819
1843
  const enabled = buffering !== false && !pipeline;
1820
1844
  let mibsTime, delayTime;
1821
1845
  if (log || this.readTimeout > 0) {
1822
1846
  client.once('readable', () => {
1847
+ var _l;
1823
1848
  if (this.readTimeout > 0) {
1824
1849
  timeout = setTimeout(() => {
1825
1850
  abortResponse();
@@ -1830,7 +1855,7 @@ class Request extends module_1.default {
1830
1855
  if (buffering === false) {
1831
1856
  mibsTime = process.hrtime();
1832
1857
  }
1833
- switch (this.settings?.time_format || LOG_TIMEFORMAT) {
1858
+ switch (((_l = this.settings) === null || _l === void 0 ? void 0 : _l.time_format) || LOG_TIMEFORMAT) {
1834
1859
  case 'readable':
1835
1860
  delayTime = process.hrtime(startTime);
1836
1861
  break;
@@ -2157,13 +2182,14 @@ class Request extends module_1.default {
2157
2182
  this[kDownloading].clear();
2158
2183
  }
2159
2184
  set agentTimeout(value) {
2185
+ var _l, _m;
2160
2186
  if (value > 0) {
2161
2187
  this[kAgentTimeout] = value;
2162
- this.keepAlive ?? (this.keepAlive = true);
2188
+ (_l = this.keepAlive) !== null && _l !== void 0 ? _l : (this.keepAlive = true);
2163
2189
  }
2164
2190
  else {
2165
2191
  this[kAgentTimeout] = 0;
2166
- this.keepAlive ?? (this.keepAlive = false);
2192
+ (_m = this.keepAlive) !== null && _m !== void 0 ? _m : (this.keepAlive = false);
2167
2193
  }
2168
2194
  }
2169
2195
  get agentTimeout() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/request",
3
- "version": "0.5.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.5",
24
- "@e-mc/types": "0.5.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"