@e-mc/request 0.7.16 → 0.7.17

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 2024 An Pham
1
+ Copyright 2023 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/index.js CHANGED
@@ -1006,13 +1006,14 @@ class Request extends module_1.default {
1006
1006
  return Promise.reject(err);
1007
1007
  }
1008
1008
  }
1009
- let pathname, headers, binOpts, silent;
1009
+ let pathname, headers, binOpts, signal, silent;
1010
1010
  if (options) {
1011
1011
  if (typeof options === 'string') {
1012
1012
  pathname = options;
1013
1013
  }
1014
1014
  else {
1015
- ({ pathname, headers, binOpts, silent } = options);
1015
+ ({ pathname, binOpts, signal, silent } = options);
1016
+ headers = (0, util_1.parseOutgoingHeaders)(options.headers);
1016
1017
  if ((0, types_1.isArray)(binOpts)) {
1017
1018
  let next = false;
1018
1019
  binOpts = binOpts.filter(opt => !((0, types_1.isString)(opt) && /^-[a-z][\S\s]*$/i.test(opt.trim()))).map((opt) => {
@@ -1316,7 +1317,13 @@ class Request extends module_1.default {
1316
1317
  }
1317
1318
  for (const item of ARIA2.PID_QUEUE) {
1318
1319
  try {
1319
- process.kill(item[0], 0);
1320
+ if (pid === item[0] && (signal === null || signal === void 0 ? void 0 : signal.aborted)) {
1321
+ process.kill(item[0]);
1322
+ closeTorrent(item[0]);
1323
+ }
1324
+ else {
1325
+ process.kill(item[0], 0);
1326
+ }
1320
1327
  }
1321
1328
  catch {
1322
1329
  closeTorrent(item[0]);
@@ -1374,7 +1381,7 @@ class Request extends module_1.default {
1374
1381
  open(uri, options) {
1375
1382
  var _l;
1376
1383
  var _m, _o;
1377
- let { host, url, httpVersion, method = 'GET', encoding, format, headers, postData, keepAlive, agentTimeout, socketPath, timeout = this._config.connectTimeout, outStream } = options;
1384
+ let { host, url, httpVersion, method = 'GET', encoding, format, headers: outgoing, postData, keepAlive, agentTimeout, socketPath, timeout = this._config.connectTimeout, outStream } = options, headers = (0, util_1.parseOutgoingHeaders)(outgoing);
1378
1385
  const getting = method === 'GET';
1379
1386
  const posting = method === 'POST';
1380
1387
  if (format) {
@@ -1629,6 +1636,9 @@ class Request extends module_1.default {
1629
1636
  const ac = new AbortController();
1630
1637
  this[kDownloading].add(options.outAbort = ac);
1631
1638
  stream.addAbortSignal(ac.signal, request);
1639
+ if (options.signal) {
1640
+ stream.addAbortSignal(options.signal, request);
1641
+ }
1632
1642
  this.signal.addEventListener('abort', () => ac.abort(), { once: true });
1633
1643
  }
1634
1644
  if (posting) {
@@ -1678,7 +1688,7 @@ class Request extends module_1.default {
1678
1688
  else {
1679
1689
  options = {};
1680
1690
  }
1681
- const headers = options.headers || (options.headers = {});
1691
+ const headers = (0, util_1.parseOutgoingHeaders)(options.headers || (options.headers = {}));
1682
1692
  for (const attr in headers) {
1683
1693
  const name = attr.toLowerCase();
1684
1694
  if (name === 'content-type' || name === 'content-length') {
@@ -1841,7 +1851,7 @@ class Request extends module_1.default {
1841
1851
  }
1842
1852
  const client = this.open(href, request);
1843
1853
  const { host, url, encoding, outFormat } = request;
1844
- let buffer, aborted;
1854
+ let buffer, aborted = false;
1845
1855
  ({ httpVersion, outAbort } = request);
1846
1856
  const isAborted = () => client.destroyed || httpVersion === 2 && client.aborted;
1847
1857
  const isRetry = (value) => (0, util_1.isRetryable)(value) && ++retries <= this._config.retryLimit;
@@ -1858,12 +1868,6 @@ class Request extends module_1.default {
1858
1868
  }
1859
1869
  buffer = null;
1860
1870
  aborted = true;
1861
- if (outAbort) {
1862
- if (!client.aborted) {
1863
- outAbort.abort();
1864
- }
1865
- this[kDownloading].delete(outAbort);
1866
- }
1867
1871
  client.destroy();
1868
1872
  };
1869
1873
  const retryTimeout = () => {
@@ -1888,7 +1892,6 @@ class Request extends module_1.default {
1888
1892
  var _l;
1889
1893
  if (readTimeout > 0) {
1890
1894
  timeout = setTimeout(() => {
1891
- abortResponse();
1892
1895
  throwError((0, types_1.errorValue)("Timeout was exceeded", href.toString()));
1893
1896
  }, readTimeout);
1894
1897
  }
@@ -2017,9 +2020,9 @@ class Request extends module_1.default {
2017
2020
  host.success(httpVersion);
2018
2021
  };
2019
2022
  const redirectResponse = (statusCode, location) => {
2020
- abortResponse();
2021
2023
  if (location) {
2022
2024
  if (++redirects <= this._config.redirectLimit) {
2025
+ abortResponse();
2023
2026
  downloadUri.call(this, Request.fromURL(url, location));
2024
2027
  }
2025
2028
  else {
@@ -2031,8 +2034,8 @@ class Request extends module_1.default {
2031
2034
  }
2032
2035
  };
2033
2036
  const errorResponse = (err) => {
2034
- abortResponse();
2035
2037
  if ((0, util_1.checkRetryable)(err) && ++retries <= this._config.retryLimit) {
2038
+ abortResponse();
2036
2039
  if (isConnectionTimeout(err)) {
2037
2040
  retryTimeout();
2038
2041
  }
@@ -2175,7 +2178,6 @@ class Request extends module_1.default {
2175
2178
  retryResponse(statusCode, res.headers['retry-after']);
2176
2179
  }
2177
2180
  else {
2178
- abortResponse();
2179
2181
  throwError(formatStatus(statusCode));
2180
2182
  }
2181
2183
  })
@@ -2193,8 +2195,8 @@ class Request extends module_1.default {
2193
2195
  if (aborted) {
2194
2196
  return;
2195
2197
  }
2196
- abortResponse();
2197
2198
  if (++retries <= this._config.retryLimit) {
2199
+ abortResponse();
2198
2200
  retryTimeout();
2199
2201
  }
2200
2202
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/request",
3
- "version": "0.7.16",
3
+ "version": "0.7.17",
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.16",
24
- "@e-mc/types": "0.7.16",
23
+ "@e-mc/module": "0.7.17",
24
+ "@e-mc/types": "0.7.17",
25
25
  "combined-stream": "^1.0.8",
26
26
  "js-yaml": "^4.1.0",
27
27
  "which": "^2.0.2"
package/util.d.ts CHANGED
@@ -4,6 +4,7 @@ import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http';
4
4
 
5
5
  declare namespace util {
6
6
  function parseHeader<T = unknown>(headers: IncomingHttpHeaders, name: string): T | undefined;
7
+ function parseOutgoingHeaders(headers: OutgoingHttpHeaders | Headers | undefined): OutgoingHttpHeaders | undefined;
7
8
  function normalizeHeaders(headers: OutgoingHttpHeaders): OutgoingHttpHeaders;
8
9
  function getBasicAuth(auth: AuthValue): string;
9
10
  function getBasicAuth(username: unknown, password?: unknown): string;
package/util.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fromSeconds = exports.asFloat = exports.asInt = exports.trimPath = exports.isRetryable = exports.checkRetryable = exports.hasBasicAuth = exports.getBasicAuth = exports.normalizeHeaders = exports.parseHeader = void 0;
3
+ exports.fromSeconds = exports.asFloat = exports.asInt = exports.trimPath = exports.isRetryable = exports.checkRetryable = exports.hasBasicAuth = exports.getBasicAuth = exports.normalizeHeaders = exports.parseOutgoingHeaders = exports.parseHeader = void 0;
4
4
  const util = require("util");
5
5
  const module_1 = require("@e-mc/module");
6
6
  const types_1 = require("@e-mc/types");
@@ -25,6 +25,24 @@ function parseHeader(headers, name) {
25
25
  }
26
26
  }
27
27
  exports.parseHeader = parseHeader;
28
+ function parseOutgoingHeaders(headers) {
29
+ if (headers) {
30
+ if (globalThis.Headers && headers instanceof Headers) {
31
+ const result = {};
32
+ headers.forEach((value, key) => {
33
+ if (key === 'set-cookie') {
34
+ (result[key] || (result[key] = [])).push(value);
35
+ }
36
+ else {
37
+ result[key] = value;
38
+ }
39
+ });
40
+ return result;
41
+ }
42
+ return headers;
43
+ }
44
+ }
45
+ exports.parseOutgoingHeaders = parseOutgoingHeaders;
28
46
  function normalizeHeaders(headers) {
29
47
  const result = Object.create(null);
30
48
  for (const name in headers) {
@@ -37,11 +55,11 @@ function normalizeHeaders(headers) {
37
55
  value = value.trim();
38
56
  break;
39
57
  default:
40
- if (Array.isArray(value)) {
41
- value = value.map(out => module_1.default.asString(out).trim());
42
- break;
58
+ if (!(0, types_1.isArray)(value)) {
59
+ continue;
43
60
  }
44
- continue;
61
+ value = value.map(out => module_1.default.asString(out).trim());
62
+ break;
45
63
  }
46
64
  if (value) {
47
65
  result[trimPath(name.trim().toLowerCase())] = value;