@e-mc/request 0.9.0 → 0.9.2

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,11 +1,11 @@
1
- Copyright 2024 An Pham
2
-
3
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
-
5
- 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
-
7
- 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
-
9
- 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
-
1
+ Copyright 2024 An Pham
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
11
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @e-mc/request
2
2
 
3
- * NodeJS 14/16
3
+ * NodeJS 16
4
4
  * ES2020
5
5
 
6
6
  ## General Usage
@@ -9,7 +9,7 @@
9
9
 
10
10
  ## Interface
11
11
 
12
- * [View Source](https://www.unpkg.com/@e-mc/types@0.9.0/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.9.2/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { IModule, ModuleConstructor } from "./index";
@@ -199,27 +199,11 @@ instance.get("http://hostname/path/config.yml", options).then(data => {
199
199
  });
200
200
  ```
201
201
 
202
- ## NodeJS 14 LTS
203
-
204
- Any optional fail safe dependencies were removed as of `E-mc 0.9`. The code itself will still be *ES2020* and will continue to work equivalently when self-installing these dependencies:
205
-
206
- ### Under 15.4 + 16.0
207
-
208
- ```sh
209
- npm i abort-controller event-target-shim
210
- ```
211
-
212
- ### Under 14.17 + 15.6
213
-
214
- ```sh
215
- npm i uuid
216
- ```
217
-
218
202
  ## References
219
203
 
220
- - https://www.unpkg.com/@e-mc/types@0.9.0/lib/http.d.ts
221
- - https://www.unpkg.com/@e-mc/types@0.9.0/lib/request.d.ts
222
- - https://www.unpkg.com/@e-mc/types@0.9.0/lib/settings.d.ts
204
+ - https://www.unpkg.com/@e-mc/types@0.9.2/lib/http.d.ts
205
+ - https://www.unpkg.com/@e-mc/types@0.9.2/lib/request.d.ts
206
+ - https://www.unpkg.com/@e-mc/types@0.9.2/lib/settings.d.ts
223
207
 
224
208
  * https://www.npmjs.com/package/@types/node
225
209
 
@@ -1,5 +1,5 @@
1
- import type { HttpHostConstructor } from '../../../types/lib/request';
2
-
3
- declare const HttpHost: HttpHostConstructor;
4
-
1
+ import type { HttpHostConstructor } from '../../../types/lib/request';
2
+
3
+ declare const HttpHost: HttpHostConstructor;
4
+
5
5
  export = HttpHost;
package/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { RequestConstructor } from '../types/lib';
2
-
3
- declare const Request: RequestConstructor;
4
-
1
+ import type { RequestConstructor } from '../types/lib';
2
+
3
+ declare const Request: RequestConstructor;
4
+
5
5
  export = Request;
package/index.js CHANGED
@@ -35,7 +35,6 @@ const kStatusOn = Symbol('statusOn');
35
35
  const kHeadersOn = Symbol('headersOn');
36
36
  const PLATFORM_WIN32 = process.platform === 'win32';
37
37
  const SUPPORT_NODEJS20 = module_1.supported(20);
38
- const SUPPORT_ABORTSIGNAL = module_1.supported(15, 3) || module_1.supported(14, 17, 0, true);
39
38
  const HTTP = {
40
39
  HOST: {},
41
40
  HEADERS: {},
@@ -878,7 +877,12 @@ class Request extends module_1 {
878
877
  const resolved = this[kConnectDns][hostname] || DNS.CACHE[hostname];
879
878
  if (resolved) {
880
879
  return (...args) => {
881
- return SUPPORT_NODEJS20 ? args[2](null, resolved) : args[2](null, resolved[0].address, resolved[0].family);
880
+ if (SUPPORT_NODEJS20) {
881
+ args[2](null, resolved);
882
+ }
883
+ else {
884
+ args[2](null, resolved[0].address, resolved[0].family);
885
+ }
882
886
  };
883
887
  }
884
888
  const pending = (_o = this[kPendingDns])[hostname] || (_o[hostname] = []);
@@ -1658,7 +1662,7 @@ class Request extends module_1 {
1658
1662
  if (timeout > 0) {
1659
1663
  request.setTimeout(timeout);
1660
1664
  }
1661
- if ((getting || posting) && SUPPORT_ABORTSIGNAL) {
1665
+ if (getting || posting) {
1662
1666
  const ac = new AbortController();
1663
1667
  this[kDownloading].add(options.outAbort = ac);
1664
1668
  stream.addAbortSignal(ac.signal, request);
@@ -1833,11 +1837,14 @@ class Request extends module_1 {
1833
1837
  opts.encoding = (0, types_1.getEncoding)(opts.encoding);
1834
1838
  }
1835
1839
  return new Promise((resolve, reject) => {
1836
- const { silent = this[kSingleton], pipeTo } = opts;
1837
- const log = LOG_HTTP && LOG_TIMEPROCESS && !silent;
1840
+ const pipeTo = opts.pipeTo;
1841
+ const status = opts.silent === false || !opts.silent && !this[kSingleton];
1842
+ let log = status && LOG_HTTP && LOG_TIMEPROCESS, retries = 0, redirects = 0, closed, timeout, outStream;
1838
1843
  const startTime = log ? process.hrtime() : 0;
1839
- let retries = 0, redirects = 0, closed, timeout, outStream;
1840
1844
  const throwError = (err, outAbort) => {
1845
+ if (timeout) {
1846
+ clearTimeout(timeout);
1847
+ }
1841
1848
  if (!closed) {
1842
1849
  closed = true;
1843
1850
  if (outStream && (0, types_1.isString)(pipeTo)) {
@@ -1845,9 +1852,6 @@ class Request extends module_1 {
1845
1852
  }
1846
1853
  reject(typeof err === 'string' ? new Error(err) : err);
1847
1854
  }
1848
- if (timeout) {
1849
- clearTimeout(timeout);
1850
- }
1851
1855
  if (outAbort) {
1852
1856
  this[kDownloading].delete(outAbort);
1853
1857
  }
@@ -1881,7 +1885,8 @@ class Request extends module_1 {
1881
1885
  const isUnsupported = (value) => value === 421 || value === 505;
1882
1886
  const isDowngrade = (err) => err instanceof Error && (err.code === 'ERR_HTTP2_ERROR' || isUnsupported(Math.abs(err.errno)));
1883
1887
  const wasAborted = (err) => err instanceof Error && err.message.startsWith("Aborted");
1884
- const formatWarning = (message) => LOG_HTTP && !silent && this.formatMessage(1024, 'HTTP' + httpVersion, [message, host.origin], url.toString(), { titleBgColor: 'bgGrey', titleColor: 'yellow' });
1888
+ const sendWarning = (message) => status && LOG_HTTP && this.formatMessage(1024, 'HTTP' + httpVersion, [message, host.origin], url.toString(), { ...module_1.LOG_STYLE_WARN });
1889
+ const formatRetry = (message) => message + ` (${retries} / ${this._config.retryLimit})`;
1885
1890
  const formatNgFlags = (value, statusCode, location) => location ? `Using HTTP 1.1 for URL redirect (${location})` : formatStatus(statusCode, value ? 'NGHTTP2 Error ' + value : '');
1886
1891
  const abortResponse = () => {
1887
1892
  if (closed) {
@@ -1901,7 +1906,7 @@ class Request extends module_1 {
1901
1906
  client.destroy();
1902
1907
  };
1903
1908
  const retryTimeout = () => {
1904
- formatWarning(`Connection timeout (${retries} / ${this._config.retryLimit})`);
1909
+ sendWarning(formatRetry('Connection timeout'));
1905
1910
  downloadUri.call(this, href);
1906
1911
  };
1907
1912
  const acceptResponse = (headers) => {
@@ -1911,9 +1916,8 @@ class Request extends module_1 {
1911
1916
  if ('outFilename' in opts) {
1912
1917
  opts.outFilename = (0, util_1.parseHeader)(headers, 'content-disposition');
1913
1918
  }
1914
- const buffering = request.connected?.call(client, headers);
1915
1919
  const pipeline = pipeTo ? !(0, types_1.isString)(pipeTo) : false;
1916
- const enabled = buffering !== false && !pipeline;
1920
+ const enabled = request.connected?.call(client, headers) !== false && !pipeline;
1917
1921
  const maxBufferSize = request.maxBufferSize ? (0, types_1.alignSize)(request.maxBufferSize) : 0;
1918
1922
  const { host: parent, readTimeout } = this;
1919
1923
  const contentLength = parent && progressId !== undefined ? parseInt(headers['content-length'] || '0') : 0;
@@ -1927,13 +1931,18 @@ class Request extends module_1 {
1927
1931
  }, readTimeout);
1928
1932
  }
1929
1933
  if (log) {
1930
- switch (this.settings?.time_format || LOG_TIMEFORMAT) {
1934
+ switch (this.settings.time_format || LOG_TIMEFORMAT) {
1931
1935
  case 'readable':
1932
1936
  delayTime = process.hrtime(startTime);
1933
1937
  break;
1934
1938
  case 'relative':
1935
1939
  delayTime = Date.now() - this.startTime;
1936
1940
  break;
1941
+ case 'none':
1942
+ if (opts.silent !== false) {
1943
+ log = false;
1944
+ }
1945
+ break;
1937
1946
  }
1938
1947
  }
1939
1948
  mibsTime = process.hrtime();
@@ -1984,8 +1993,7 @@ class Request extends module_1 {
1984
1993
  this[kDownloading].delete(outAbort);
1985
1994
  }
1986
1995
  closed = true;
1987
- let messageUnit, titleBgColor;
1988
- let result;
1996
+ let result, messageUnit, titleBgColor;
1989
1997
  if (buffer) {
1990
1998
  if (Array.isArray(buffer)) {
1991
1999
  buffer = Buffer.concat(buffer);
@@ -2030,7 +2038,7 @@ class Request extends module_1 {
2030
2038
  }
2031
2039
  }
2032
2040
  catch (err) {
2033
- if (!silent && !this[kSingleton] && !(packageName && this.checkPackage(err, packageName))) {
2041
+ if (status && !(packageName && this.checkPackage(err, packageName))) {
2034
2042
  this.writeFail(['Unable to parse URI response', format], err, 1024);
2035
2043
  }
2036
2044
  result = null;
@@ -2106,7 +2114,7 @@ class Request extends module_1 {
2106
2114
  }
2107
2115
  if (offset > 0) {
2108
2116
  if (offset <= this._config.retryAfter) {
2109
- formatWarning(`Retry After (${retryAfter})`);
2117
+ sendWarning(`Retry After (${retryAfter})`);
2110
2118
  setTimeout(() => downloadUri.call(this, href), offset);
2111
2119
  }
2112
2120
  else {
@@ -2115,7 +2123,7 @@ class Request extends module_1 {
2115
2123
  return;
2116
2124
  }
2117
2125
  }
2118
- formatWarning(Request.fromStatusCode(statusCode) + ` (${retries} / ${this._config.retryLimit})`);
2126
+ sendWarning(formatRetry(Request.fromStatusCode(statusCode)));
2119
2127
  if ((0, util_1.isRetryable)(statusCode, true)) {
2120
2128
  process.nextTick(downloadUri.bind(this), href);
2121
2129
  }
@@ -2132,7 +2140,7 @@ class Request extends module_1 {
2132
2140
  if (downgrade) {
2133
2141
  host.failed(2);
2134
2142
  if (host.version > 1) {
2135
- if (!silent) {
2143
+ if (status && LOG_HTTP) {
2136
2144
  this.formatMessage(1024, 'HTTP2', ['Unsupported protocol', host.origin], message, { failed: true });
2137
2145
  }
2138
2146
  host.version = 1;
@@ -2194,18 +2202,19 @@ class Request extends module_1 {
2194
2202
  }
2195
2203
  if (wasAborted(err)) {
2196
2204
  errorResponse(err);
2197
- return;
2198
2205
  }
2199
- switch (!isDowngrade(err) && await host.hasProtocol(2)) {
2200
- case 1:
2201
- errorResponse(err);
2202
- break;
2203
- case 2:
2204
- retryDownload(false, err);
2205
- break;
2206
- default:
2207
- retryDownload(true, err);
2208
- break;
2206
+ else {
2207
+ switch (!isDowngrade(err) && await host.hasProtocol(2)) {
2208
+ case 1:
2209
+ errorResponse(err);
2210
+ break;
2211
+ case 2:
2212
+ retryDownload(false, err);
2213
+ break;
2214
+ default:
2215
+ retryDownload(true, err);
2216
+ break;
2217
+ }
2209
2218
  }
2210
2219
  });
2211
2220
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/request",
3
- "version": "0.9.0",
3
+ "version": "0.9.2",
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": "BSD 3-Clause",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/module": "0.9.0",
24
- "@e-mc/types": "0.9.0",
23
+ "@e-mc/module": "0.9.2",
24
+ "@e-mc/types": "0.9.2",
25
25
  "combined-stream": "^1.0.8",
26
26
  "js-yaml": "^4.1.0",
27
27
  "picomatch": "^4.0.2",