@e-mc/request 0.12.13 → 0.12.15

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
- * [View Source](https://www.unpkg.com/@e-mc/types@0.12.13/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.12.15/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { IModule, ModuleConstructor } from "./index";
@@ -255,9 +255,9 @@ instance.get("http://hostname/path/config.yml", options).then(data => {
255
255
 
256
256
  ## References
257
257
 
258
- - https://www.unpkg.com/@e-mc/types@0.12.13/lib/http.d.ts
259
- - https://www.unpkg.com/@e-mc/types@0.12.13/lib/request.d.ts
260
- - https://www.unpkg.com/@e-mc/types@0.12.13/lib/settings.d.ts
258
+ - https://www.unpkg.com/@e-mc/types@0.12.15/lib/http.d.ts
259
+ - https://www.unpkg.com/@e-mc/types@0.12.15/lib/request.d.ts
260
+ - https://www.unpkg.com/@e-mc/types@0.12.15/lib/settings.d.ts
261
261
 
262
262
  * https://www.npmjs.com/package/@types/node
263
263
 
@@ -349,7 +349,12 @@ class HttpAdapter {
349
349
  result = new (require(packageName = 'fast-xml-parser').XMLParser)(parser).parse(buffer);
350
350
  break;
351
351
  case 'toml':
352
- result = require(packageName = 'toml').parse(buffer);
352
+ try {
353
+ result = require('smol-toml').parse(buffer);
354
+ }
355
+ catch {
356
+ result = require(packageName = 'toml').parse(buffer);
357
+ }
353
358
  break;
354
359
  default:
355
360
  result = JSON.parse(buffer);
package/index.js CHANGED
@@ -33,8 +33,8 @@ const HTTP = {
33
33
  PROXY: null
34
34
  };
35
35
  const TLS = {
36
- TEXT: {},
37
- FILE: {}
36
+ TEXT: Object.create(null),
37
+ FILE: Object.create(null)
38
38
  };
39
39
  const DNS = {
40
40
  CACHE: Object.create(null),
@@ -61,7 +61,7 @@ const ARIA2 = {
61
61
  LOWEST_SPEED_LIMIT: null,
62
62
  ALWAYS_RESUME: false,
63
63
  FILE_ALLOCATION: 'none',
64
- PROXY: {},
64
+ PROXY: Object.create(null),
65
65
  NO_PROXY: '',
66
66
  CONF_PATH: ''
67
67
  };
@@ -116,21 +116,6 @@ try {
116
116
  }
117
117
  catch {
118
118
  }
119
- function getBaseHeaders(uri, headers) {
120
- let result;
121
- uri = (0, util_1.trimPath)(uri);
122
- for (const pathname in headers) {
123
- if (pathname === uri || uri.startsWith(pathname + '/')) {
124
- (result ||= []).push([pathname, headers[pathname]]);
125
- }
126
- }
127
- if (result) {
128
- if (result.length > 1) {
129
- result.sort((a, b) => b[0].length - a[0].length);
130
- }
131
- return result[0][1];
132
- }
133
- }
134
119
  function setDnsCache(hostname, value, expires) {
135
120
  expires ??= DNS.EXPIRES;
136
121
  if (expires > 0 && !DNS.CACHE[hostname]) {
@@ -175,11 +160,9 @@ function getProxySettings(request, agentTimeout) {
175
160
  return null;
176
161
  }
177
162
  function closeTorrent(pid) {
178
- if (typeof pid === 'number') {
179
- const index = ARIA2.PID_QUEUE.findIndex(value => pid === value[0]);
180
- if (index !== -1) {
181
- ARIA2.PID_QUEUE.splice(index, 1);
182
- }
163
+ const index = ARIA2.PID_QUEUE.findIndex(value => value[0] === pid);
164
+ if (index !== -1) {
165
+ ARIA2.PID_QUEUE.splice(index, 1);
183
166
  }
184
167
  }
185
168
  function clearDnsLookup() {
@@ -201,7 +184,7 @@ function resetHttpHost(version) {
201
184
  const host = HTTP.HOST[origin];
202
185
  if (host.secure && host.version === 1) {
203
186
  const failed = host.failed(2, true);
204
- if (failed === 0 && host.failed(2, true) < 10 || failed < 3 && host.success(2, true) > 0) {
187
+ if (failed === 0 && host.error(2, true) < 10 || failed < 3 && host.success(2, true) > 0) {
205
188
  host.version = version;
206
189
  }
207
190
  }
@@ -362,8 +345,10 @@ function decompressEncoding(value, chunkSize) {
362
345
  }
363
346
  }
364
347
  function resetAria2() {
365
- clearInterval(ARIA2.PID_TIMER);
366
- ARIA2.PID_TIMER = null;
348
+ if (ARIA2.PID_TIMER) {
349
+ clearInterval(ARIA2.PID_TIMER);
350
+ ARIA2.PID_TIMER = null;
351
+ }
367
352
  }
368
353
  function escapeShellQuote(value) {
369
354
  value = value.replace(/(?<!\\)"/g, '\\"');
@@ -846,9 +831,9 @@ class Request extends module_1 {
846
831
  };
847
832
  #singleton = false;
848
833
  #httpVersion = null;
834
+ #headers = null;
849
835
  #ipVersion;
850
836
  #agentTimeout;
851
- #headers = null;
852
837
  #baseUrl = null;
853
838
  #connectDns = Object.create(null);
854
839
  #pendingDns = Object.create(null);
@@ -858,7 +843,7 @@ class Request extends module_1 {
858
843
  #adapter = HTTP_ADAPTER;
859
844
  #certs = null;
860
845
  #downloading = new Set();
861
- #hostInfo = {};
846
+ #hostInfo = Object.create(null);
862
847
  #session = [Object.create(null)];
863
848
  constructor(data) {
864
849
  super();
@@ -884,9 +869,7 @@ class Request extends module_1 {
884
869
  if (proxy) {
885
870
  this.proxy = proxy;
886
871
  }
887
- if ((0, types_1.isObject)(headers)) {
888
- setOutgoingHeaders(this.#headers = {}, headers);
889
- }
872
+ this.parseHeaders(headers);
890
873
  if ((0, types_1.isObject)(certs)) {
891
874
  this.#certs = validateCerts(certs);
892
875
  }
@@ -994,9 +977,7 @@ class Request extends module_1 {
994
977
  init(config) {
995
978
  if (config) {
996
979
  const { headers, httpVersion, ipVersion, readTimeout } = config;
997
- if ((0, types_1.isObject)(headers)) {
998
- setOutgoingHeaders(this.#headers ||= {}, headers);
999
- }
980
+ this.parseHeaders(headers);
1000
981
  if (httpVersion !== undefined) {
1001
982
  this.httpVersion = httpVersion;
1002
983
  }
@@ -1220,8 +1201,7 @@ class Request extends module_1 {
1220
1201
  }
1221
1202
  }
1222
1203
  headersOf(uri) {
1223
- const headers = this.#headers;
1224
- return headers && getBaseHeaders(uri, headers) || (this.host ? getBaseHeaders(uri, HTTP.HEADERS) : undefined);
1204
+ return this.findHeadersByUri(uri) || (this.host ? this.findHeadersByUri(uri, HTTP.HEADERS) : undefined);
1225
1205
  }
1226
1206
  async aria2c(uri, options = {}) {
1227
1207
  if (!ARIA2.BIN) {
@@ -1239,7 +1219,7 @@ class Request extends module_1 {
1239
1219
  ({ pathname, headers, binOpts } = this.parseBinOpts(options, ['--daemon'], ['--input-file'], options.shellExpansion));
1240
1220
  }
1241
1221
  try {
1242
- if (typeof uri === 'string' && module_1.isURL(uri)) {
1222
+ if ((0, types_1.isString)(uri) && module_1.isURL(uri)) {
1243
1223
  uri = new URL(uri);
1244
1224
  }
1245
1225
  pathname = checkBinTarget(this, "aria2", uri, pathname, 'aria2', binOpts);
@@ -1942,7 +1922,7 @@ class Request extends module_1 {
1942
1922
  try {
1943
1923
  keepAlive ??= proxy.keepAlive;
1944
1924
  agentTimeout ??= proxy.agentTimeout;
1945
- const proxyHeaders = this.#headers && getBaseHeaders(proxy.host.href, this.#headers) || getBaseHeaders(proxy.host.href, HTTP.HEADERS);
1925
+ const proxyHeaders = this.findHeadersByUri(proxy.host.href) || this.findHeadersByUri(proxy.host.href, HTTP.HEADERS);
1946
1926
  agent = require(pkg)(proxy.host, (typeof keepAlive === 'boolean' || agentTimeout > 0) && agentTimeout !== 0 ? { keepAlive: keepAlive ?? true, timeout: agentTimeout, headers: proxyHeaders } : { headers: proxyHeaders });
1947
1927
  }
1948
1928
  catch (err) {
@@ -2391,7 +2371,7 @@ class Request extends module_1 {
2391
2371
  }
2392
2372
  if ((0, types_1.isArray)(options.binOpts)) {
2393
2373
  let next = false;
2394
- binOpts = options.binOpts.filter((opt) => !((0, types_1.isString)(opt) && /^-[a-z].*$/i.test(opt.trim()))).map((opt) => {
2374
+ binOpts = options.binOpts.filter((opt) => !((0, types_1.isString)(opt) && /^-[a-z].*$/i.test(opt))).map((opt) => {
2395
2375
  if (next) {
2396
2376
  if (!module_1.asString(opt).startsWith('--')) {
2397
2377
  return [];
@@ -2441,6 +2421,26 @@ class Request extends module_1 {
2441
2421
  }
2442
2422
  return { pathname, headers: (0, util_1.parseOutgoingHeaders)(options.headers), binOpts };
2443
2423
  }
2424
+ parseHeaders(outgoing) {
2425
+ if ((0, types_1.isObject)(outgoing)) {
2426
+ setOutgoingHeaders(this.#headers ||= {}, outgoing);
2427
+ }
2428
+ }
2429
+ findHeadersByUri(uri, outgoing = this.#headers) {
2430
+ if (outgoing) {
2431
+ const result = [];
2432
+ uri = (0, util_1.trimPath)(uri);
2433
+ for (const pathname in outgoing) {
2434
+ if (pathname === uri || uri.startsWith(pathname + '/')) {
2435
+ result.push([pathname, outgoing[pathname]]);
2436
+ }
2437
+ }
2438
+ if (result.length > 0) {
2439
+ result.sort((a, b) => b[0].length - a[0].length);
2440
+ return result[0][1];
2441
+ }
2442
+ }
2443
+ }
2444
2444
  set adapter(value) {
2445
2445
  if (isConstructor(value) && value.prototype instanceof adapter_1) {
2446
2446
  this.#adapter = value;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/request",
3
- "version": "0.12.13",
3
+ "version": "0.12.15",
4
4
  "description": "Request constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -19,8 +19,8 @@
19
19
  "license": "BSD-3-Clause",
20
20
  "homepage": "https://github.com/anpham6/e-mc#readme",
21
21
  "dependencies": {
22
- "@e-mc/module": "0.12.13",
23
- "@e-mc/types": "0.12.13",
22
+ "@e-mc/module": "0.12.15",
23
+ "@e-mc/types": "0.12.15",
24
24
  "combined-stream": "^1.0.8",
25
25
  "js-yaml": "^4.1.1",
26
26
  "picomatch": "^4.0.3",
package/util.d.ts CHANGED
@@ -14,7 +14,7 @@ declare namespace util {
14
14
  function checkRetryable(err: unknown): boolean;
15
15
  function isRetryable(value: number, timeout?: boolean): boolean;
16
16
  function parseHttpProxy(value?: string): HttpProxySettings | undefined;
17
- function trimPath(value: string): string;
17
+ function trimPath(value: string, char?: string): string;
18
18
  function asInt(value: unknown): number;
19
19
  function asFloat(value: unknown): number;
20
20
  function fromSeconds(value: unknown): number;
package/util.js CHANGED
@@ -182,9 +182,11 @@ function parseHttpProxy(value, ignoreEnv) {
182
182
  }
183
183
  }
184
184
  }
185
- function trimPath(value) {
186
- const length = value.length - 1;
187
- return value[length] === '/' ? value.substring(0, length) : value;
185
+ function trimPath(value, char = '/') {
186
+ while (value.at(-1) === char) {
187
+ value = value.slice(0, -1);
188
+ }
189
+ return value;
188
190
  }
189
191
  function asInt(value) {
190
192
  switch (typeof value) {