@e-mc/request 0.8.22 → 0.8.24

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/README.md CHANGED
@@ -9,7 +9,7 @@
9
9
 
10
10
  ## Interface
11
11
 
12
- - https://www.unpkg.com/@e-mc/types@0.8.22/lib/index.d.ts
12
+ - https://www.unpkg.com/@e-mc/types@0.8.24/lib/index.d.ts
13
13
 
14
14
  ```typescript
15
15
  import type { IModule, ModuleConstructor } from "./index";
@@ -82,9 +82,9 @@ interface RequestConstructor extends ModuleConstructor {
82
82
 
83
83
  ## References
84
84
 
85
- - https://www.unpkg.com/@e-mc/types@0.8.22/lib/http.d.ts
86
- - https://www.unpkg.com/@e-mc/types@0.8.22/lib/request.d.ts
87
- - https://www.unpkg.com/@e-mc/types@0.8.22/lib/settings.d.ts
85
+ - https://www.unpkg.com/@e-mc/types@0.8.24/lib/http.d.ts
86
+ - https://www.unpkg.com/@e-mc/types@0.8.24/lib/request.d.ts
87
+ - https://www.unpkg.com/@e-mc/types@0.8.24/lib/settings.d.ts
88
88
 
89
89
  ## LICENSE
90
90
 
@@ -5,8 +5,6 @@ const yaml = require("js-yaml");
5
5
  const types_1 = require("@e-mc/types");
6
6
  const module_1 = require("@e-mc/module");
7
7
  const util_1 = require("@e-mc/request/util");
8
- const kOutStream = Symbol('outStream');
9
- const kOptions = Symbol('options');
10
8
  let LOG_TIMEFORMAT = 'readable';
11
9
  class HttpAdapter {
12
10
  instance;
@@ -53,13 +51,13 @@ class HttpAdapter {
53
51
  resolve;
54
52
  reject;
55
53
  startTime;
56
- [kOutStream] = null;
57
- [kOptions];
54
+ #outStream = null;
55
+ #options;
58
56
  constructor(instance, state, uri, options) {
59
57
  this.instance = instance;
60
58
  this.state = state;
61
59
  this.uri = uri;
62
- this[kOptions] = options;
60
+ this.#options = options;
63
61
  this.startTime = state.log ? process.hrtime.bigint() : BigInt(0);
64
62
  this.setOpts();
65
63
  }
@@ -99,14 +97,14 @@ class HttpAdapter {
99
97
  this.retryResponse(statusCode, headers['retry-after']);
100
98
  }
101
99
  else if (HttpAdapter.isUnsupported(statusCode)) {
102
- this.retryDownload(true, this.formatNgFlags(http2.constants.NGHTTP2_PROTOCOL_ERROR, statusCode));
100
+ this.retryDownload(true, this.#formatNgFlags(http2.constants.NGHTTP2_PROTOCOL_ERROR, statusCode));
103
101
  }
104
102
  else {
105
103
  switch (flags) {
106
104
  case http2.constants.NGHTTP2_PROTOCOL_ERROR:
107
105
  case http2.constants.NGHTTP2_INADEQUATE_SECURITY:
108
106
  case http2.constants.NGHTTP2_HTTP_1_1_REQUIRED:
109
- this.retryDownload(true, this.formatNgFlags(flags, statusCode, headers.location));
107
+ this.retryDownload(true, this.#formatNgFlags(flags, statusCode, headers.location));
110
108
  break;
111
109
  default:
112
110
  this.retryDownload(false, this.formatStatus(statusCode));
@@ -194,7 +192,7 @@ class HttpAdapter {
194
192
  if (uri) {
195
193
  this.uri = uri;
196
194
  }
197
- this.opts = this.instance.opts(this.uri, this[kOptions]);
195
+ this.opts = this.instance.opts(this.uri, this.#options);
198
196
  }
199
197
  setWriteStream() {
200
198
  const pipeTo = this.pipeTo;
@@ -462,7 +460,7 @@ class HttpAdapter {
462
460
  return;
463
461
  }
464
462
  }
465
- this.sendWarning(this.formatRetry((0, util_1.fromStatusCode)(statusCode)));
463
+ this.sendWarning(this.#formatRetry((0, util_1.fromStatusCode)(statusCode)));
466
464
  if ((0, util_1.isRetryable)(statusCode, true)) {
467
465
  setImmediate(this.init.bind(this));
468
466
  }
@@ -476,7 +474,7 @@ class HttpAdapter {
476
474
  return (0, util_1.isRetryable)(value) && ++this.retries <= this.retryLimit;
477
475
  }
478
476
  retryTimeout() {
479
- this.sendWarning(this.formatRetry("HTTP connection timeout"));
477
+ this.sendWarning(this.#formatRetry("HTTP connection timeout"));
480
478
  this.init();
481
479
  }
482
480
  terminate(err) {
@@ -509,10 +507,10 @@ class HttpAdapter {
509
507
  this.outStream = null;
510
508
  }
511
509
  }
512
- formatNgFlags(value, statusCode, location) {
510
+ #formatNgFlags(value, statusCode, location) {
513
511
  return location ? `Using HTTP 1.1 for URL redirect (${location})` : this.formatStatus(statusCode, value ? 'NGHTTP2 Error ' + value : '');
514
512
  }
515
- formatRetry(message) {
513
+ #formatRetry(message) {
516
514
  return `${message} (${this.retries} / ${this.retryLimit})`;
517
515
  }
518
516
  set abortController(value) {
@@ -522,13 +520,13 @@ class HttpAdapter {
522
520
  return this.opts.outAbort || null;
523
521
  }
524
522
  set outStream(value) {
525
- this[kOutStream] = value;
523
+ this.#outStream = value;
526
524
  if (value) {
527
525
  this.opts.outStream = value;
528
526
  }
529
527
  }
530
528
  get outStream() {
531
- return this[kOutStream];
529
+ return this.#outStream;
532
530
  }
533
531
  get destroyed() {
534
532
  return this.client.destroyed || this.httpVersion === 2 && this.client.aborted;
package/index.js CHANGED
@@ -400,7 +400,7 @@ class Request extends module_1.default {
400
400
  if ((lowest_speed_limit = parseSize(lowest_speed_limit, true)) !== undefined) {
401
401
  ARIA2.LOWEST_SPEED_LIMIT = lowest_speed_limit;
402
402
  }
403
- if (always_resume) {
403
+ if (typeof always_resume === 'boolean') {
404
404
  ARIA2.ALWAYS_RESUME = always_resume;
405
405
  }
406
406
  if ((0, types_1.isString)(file_allocation)) {
@@ -414,7 +414,7 @@ class Request extends module_1.default {
414
414
  break;
415
415
  }
416
416
  }
417
- if (conf_path && this.isPath(conf_path = path.resolve(conf_path))) {
417
+ if (conf_path === '' || (0, types_1.isString)(conf_path) && this.isPath(conf_path = path.resolve(conf_path))) {
418
418
  ARIA2.CONF_PATH = conf_path;
419
419
  }
420
420
  }
@@ -1081,7 +1081,7 @@ class Request extends module_1.default {
1081
1081
  case 'string': {
1082
1082
  const value = opt.trim();
1083
1083
  if (value.startsWith('--')) {
1084
- const match = /^(--[a-z]+[a-z0-9-]*)(=)?\s*(.*)$/.exec(value);
1084
+ const match = /^(--[a-z]+[a-z0-9-]*)(=)?\s*(.*)$/s.exec(value);
1085
1085
  if (match) {
1086
1086
  switch (match[1]) {
1087
1087
  case '--daemon':
@@ -1137,8 +1137,8 @@ class Request extends module_1.default {
1137
1137
  ({ protocol, origin, username, password, href: uri } = uri);
1138
1138
  }
1139
1139
  const escapeQuote = (value) => {
1140
- value = value.replace(/"/g, '\\"');
1141
- return PLATFORM_WIN32 ? value : value.replace(/[ $`]/g, capture => (capture !== ' ' ? '\\' : '') + '\\' + capture);
1140
+ value = value.replace(/(?<!\\)"/g, '\\"');
1141
+ return PLATFORM_WIN32 ? value : value.replace(/(?<!\\)\$/g, '\\$');
1142
1142
  };
1143
1143
  const init = [
1144
1144
  `--dir="${escapeQuote(pathname)}"`,
@@ -1283,10 +1283,13 @@ class Request extends module_1.default {
1283
1283
  }
1284
1284
  if (binOpts) {
1285
1285
  for (const leading of binOpts) {
1286
- for (let i = 0; i < opts.length; ++i) {
1287
- if (opts[i].startsWith(leading)) {
1288
- opts.splice(i--, i);
1289
- break;
1286
+ if (leading.startsWith('-')) {
1287
+ const pattern = new RegExp(`^${leading}(?:=|$)`);
1288
+ for (let i = 0; i < opts.length; ++i) {
1289
+ if (pattern.test(opts[i])) {
1290
+ opts.splice(i--, i);
1291
+ break;
1292
+ }
1290
1293
  }
1291
1294
  }
1292
1295
  }
@@ -1345,10 +1348,15 @@ class Request extends module_1.default {
1345
1348
  break;
1346
1349
  }
1347
1350
  }
1348
- if (result.length && !silent && LOG_HTTP && LOG_TIMEPROCESS) {
1349
- this.writeTimeProcess("aria2", uri, startTime, { type: 1024, queue: true, messageUnit, messageUnitMinWidth: 9, bypassLog: true });
1351
+ if (result.length > 0) {
1352
+ if (!silent && LOG_HTTP && LOG_TIMEPROCESS) {
1353
+ this.writeTimeProcess("aria2", uri, startTime, { type: 1024, queue: true, messageUnit, messageUnitMinWidth: 9, bypassLog: true });
1354
+ }
1355
+ this.addLog(types_1.STATUS_TYPE.INFO, out, currentTime, currentTime - startTime, "aria2", uri);
1356
+ }
1357
+ else {
1358
+ this.addLog(types_1.STATUS_TYPE.ERROR, "No files were successfully downloaded", currentTime, currentTime - startTime, "aria2", uri);
1350
1359
  }
1351
- this.addLog(result.length ? types_1.STATUS_TYPE.INFO : types_1.STATUS_TYPE.ERROR, out, currentTime, currentTime - startTime, "aria2", uri);
1352
1360
  resolve(result);
1353
1361
  }
1354
1362
  else {
@@ -1744,7 +1752,7 @@ class Request extends module_1.default {
1744
1752
  else {
1745
1753
  options = {};
1746
1754
  }
1747
- const headers = options.headers || (options.headers = {});
1755
+ const headers = (0, util_1.parseOutgoingHeaders)(options.headers) || {};
1748
1756
  for (const attr in headers) {
1749
1757
  const name = attr.toLowerCase();
1750
1758
  if (name === 'content-type' || name === 'content-length') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/request",
3
- "version": "0.8.22",
3
+ "version": "0.8.24",
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.8.22",
24
- "@e-mc/types": "0.8.22",
23
+ "@e-mc/module": "0.8.24",
24
+ "@e-mc/types": "0.8.24",
25
25
  "combined-stream": "^1.0.8",
26
26
  "js-yaml": "^4.1.0",
27
27
  "picomatch": "^3.0.1",
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;