@e-mc/request 0.12.7 → 0.12.9

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.7/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.12.9/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { IModule, ModuleConstructor } from "./index";
@@ -40,9 +40,9 @@ interface IRequest extends IModule {
40
40
  headersOn(name: string | string[], callback: HeadersOnCallback): void;
41
41
  headersOn(name: string | string[], globUrl: string, callback: HeadersOnCallback): void;
42
42
  headersOf(uri: string): OutgoingHttpHeaders | undefined;
43
- aria2c(uri: string | URL, pathname: string): Promise<string[]>;
43
+ aria2c(uri: string | URL, pathname: string | URL): Promise<string[]>;
44
44
  aria2c(uri: string | URL, options?: Aria2Options): Promise<string[]>;
45
- rclone(uri: string | URL, pathname: string): Promise<string[]>;
45
+ rclone(uri: string | URL, pathname: string | URL): Promise<string[]>;
46
46
  rclone(uri: string | URL, options?: RcloneOptions): Promise<string[]>;
47
47
  json(uri: string | URL, options?: OpenOptions): Promise<object | null>;
48
48
  pipe(uri: string | URL, to: Writable, options?: OpenOptions): Promise<null>;
@@ -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.7/lib/http.d.ts
259
- - https://www.unpkg.com/@e-mc/types@0.12.7/lib/request.d.ts
260
- - https://www.unpkg.com/@e-mc/types@0.12.7/lib/settings.d.ts
258
+ - https://www.unpkg.com/@e-mc/types@0.12.9/lib/http.d.ts
259
+ - https://www.unpkg.com/@e-mc/types@0.12.9/lib/request.d.ts
260
+ - https://www.unpkg.com/@e-mc/types@0.12.9/lib/settings.d.ts
261
261
 
262
262
  * https://www.npmjs.com/package/@types/node
263
263
 
@@ -14,19 +14,13 @@ class HttpAdapter {
14
14
  return value === 421 || value === 505;
15
15
  }
16
16
  static isDowngrade(err) {
17
- return err instanceof Error && (err.code === 'ERR_HTTP2_ERROR' || this.isUnsupported(Math.abs(err.errno)));
17
+ return module_1.isErrorCode(err, 'ERR_HTTP2_ERROR') || err instanceof Error && this.isUnsupported(Math.abs(err.errno));
18
18
  }
19
19
  static wasAborted(err) {
20
20
  return err instanceof Error && err.message.startsWith("Aborted");
21
21
  }
22
22
  static isConnectionError(err) {
23
- switch (err instanceof Error && err.code) {
24
- case 'ETIMEDOUT':
25
- case 'ECONNRESET':
26
- return true;
27
- default:
28
- return false;
29
- }
23
+ return module_1.isErrorCode(err, 'ETIMEDOUT', 'ECONNRESET');
30
24
  }
31
25
  static defineHostConfig({ settings }) {
32
26
  const time_format = settings?.time_format;
package/index.js CHANGED
@@ -14,6 +14,7 @@ const qs = require("node:querystring");
14
14
  const combined = require("combined-stream");
15
15
  const pm = require("picomatch");
16
16
  const which = require("which");
17
+ const node_url_1 = require("node:url");
17
18
  const types_1 = require("@e-mc/types");
18
19
  const module_1 = require("@e-mc/module");
19
20
  const util_1 = require("@e-mc/request/util");
@@ -391,68 +392,8 @@ function setBinExec(settings, target) {
391
392
  target.EXEC_GID = (gid = (0, util_1.asInt)(gid)) >= 0 ? gid : undefined;
392
393
  }
393
394
  }
394
- function parseBinOpts(instance, options, ignore, skip, doubleQuote) {
395
- let pathname = options.pathname, binOpts;
396
- if (!(0, types_1.isString)(pathname)) {
397
- if (!instance.host) {
398
- return [];
399
- }
400
- pathname = process.cwd();
401
- }
402
- if ((0, types_1.isArray)(options.binOpts)) {
403
- let next = false;
404
- binOpts = options.binOpts.filter(opt => !((0, types_1.isString)(opt) && /^-[a-z].*$/i.test(opt.trim()))).map((opt) => {
405
- if (next) {
406
- if (!module_1.asString(opt).startsWith('--')) {
407
- return [];
408
- }
409
- next = false;
410
- }
411
- switch (typeof opt) {
412
- case 'string': {
413
- const value = opt.trim();
414
- if (value.startsWith('--')) {
415
- const match = /^(--[a-z]+[a-z0-9-]*)(=)?\s*(.*)$/s.exec(value);
416
- if (match) {
417
- if (ignore.includes(match[1])) {
418
- return [];
419
- }
420
- if (skip.includes(match[1])) {
421
- if (!match[2]) {
422
- next = true;
423
- }
424
- return [];
425
- }
426
- switch (match[1]) {
427
- case '--version':
428
- case '--help':
429
- return [];
430
- default:
431
- return match[3] ? [match[1] + '=' + (0, types_1.sanitizeArgs)(match[3], doubleQuote)] : [match[1]];
432
- }
433
- }
434
- }
435
- else if (value) {
436
- return [(0, types_1.sanitizeArgs)(value, doubleQuote)];
437
- }
438
- break;
439
- }
440
- case 'number':
441
- case 'boolean':
442
- return [opt.toString()];
443
- default:
444
- if ((0, types_1.isArray)(opt)) {
445
- return opt.filter(item => (0, types_1.isString)(item)).map(item => (0, types_1.sanitizeArgs)(item, doubleQuote));
446
- }
447
- break;
448
- }
449
- return [];
450
- }).flat();
451
- }
452
- return [pathname, (0, util_1.parseOutgoingHeaders)(options.headers), binOpts];
453
- }
454
395
  function checkBinTarget(instance, name, uri, pathname, command, binOpts) {
455
- if (!(0, types_1.isString)(pathname)) {
396
+ if (!pathname) {
456
397
  throw (0, types_1.errorMessage)(name, "Invalid parameters", 'pathname');
457
398
  }
458
399
  pathname = path.resolve(pathname);
@@ -482,24 +423,25 @@ function checkBinTarget(instance, name, uri, pathname, command, binOpts) {
482
423
  return pathname;
483
424
  }
484
425
  function checkBinOpts(init, binOpts) {
485
- if ((0, types_1.isArray)(binOpts)) {
486
- for (let i = 0; i < binOpts.length; ++i) {
487
- const leading = binOpts[i] + '=';
488
- for (const arg of init) {
489
- if (arg.startsWith(leading)) {
490
- const items = [];
491
- for (let k = i + 1; k < binOpts.length; ++k) {
492
- const next = binOpts[k];
493
- if (next.startsWith('--')) {
494
- break;
495
- }
496
- items.push(next);
426
+ if (!binOpts) {
427
+ return;
428
+ }
429
+ for (let i = 0; i < binOpts.length; ++i) {
430
+ const leading = binOpts[i] + '=';
431
+ for (const arg of init) {
432
+ if (arg.startsWith(leading)) {
433
+ const items = [];
434
+ for (let k = i + 1; k < binOpts.length; ++k) {
435
+ const next = binOpts[k];
436
+ if (next.startsWith('--')) {
437
+ break;
497
438
  }
498
- const l = items.length + 1;
499
- binOpts.splice(i, l);
500
- i += l;
501
- break;
439
+ items.push(next);
502
440
  }
441
+ const l = items.length + 1;
442
+ binOpts.splice(i, l);
443
+ i += l;
444
+ break;
503
445
  }
504
446
  }
505
447
  }
@@ -514,7 +456,7 @@ function setBinHeaders(args, headers) {
514
456
  }
515
457
  }
516
458
  function finalizeBinArgs(instance, name, bin, args, opts, binOpts, cmd = []) {
517
- if ((0, types_1.isArray)(binOpts)) {
459
+ if (binOpts) {
518
460
  for (const leading of binOpts) {
519
461
  if (leading.startsWith('-')) {
520
462
  const pattern = new RegExp(`^${leading}(?:=|$)`);
@@ -554,7 +496,7 @@ const trimCharEnd = (value) => value.substring(0, value.length - 1);
554
496
  const configureDns = (family, options) => family === 0 ? options : { family, hints: family === 6 ? dns.V4MAPPED : 0 };
555
497
  const ignoreOpt = (opts, ...values) => !opts?.some(opt => values.some(value => opt === value || opt.startsWith(value + '=')));
556
498
  const escapeQuote = (value) => value.replace(/[\\"]/g, capture => '\\' + capture);
557
- const rcloneSize = (value) => (0, types_1.isString)(value) ? /^\d+(?:B|K|M|G|T|P)$/.exec(value)?.[0] : undefined;
499
+ const rcloneSize = (value) => (0, types_1.isString)(value) ? /^\d+(?:B|[KMGTP]i?)$/i.exec(value)?.[0] : undefined;
558
500
  const rcloneDuration = (value) => (0, types_1.isString)(value) ? /^\d+(?:h|m|s|ms|ns)$/.exec(value)?.[0] : undefined;
559
501
  class Request extends module_1 {
560
502
  static [kRequest] = true;
@@ -643,7 +585,7 @@ class Request extends module_1 {
643
585
  else if (proxy === false) {
644
586
  ARIA2.PROXY = {};
645
587
  }
646
- if (no_proxy === '' || (0, types_1.isString)(no_proxy)) {
588
+ if (typeof no_proxy === 'string') {
647
589
  ARIA2.NO_PROXY = no_proxy;
648
590
  }
649
591
  if (conf_path === '' || (0, types_1.isString)(conf_path) && this.isPath(conf_path = path.resolve(conf_path))) {
@@ -854,7 +796,7 @@ class Request extends module_1 {
854
796
  break;
855
797
  }
856
798
  if (expires !== undefined) {
857
- if (typeof expires === 'string') {
799
+ if ((0, types_1.isString)(expires)) {
858
800
  expires = (0, types_1.parseExpires)(expires);
859
801
  }
860
802
  if (expires >= 0) {
@@ -1103,8 +1045,12 @@ class Request extends module_1 {
1103
1045
  }
1104
1046
  break;
1105
1047
  case 'readExpect':
1106
- if ((0, types_1.isString)(value)) {
1107
- this[name] = value;
1048
+ switch (value) {
1049
+ case 'string':
1050
+ case 'always':
1051
+ case 'none':
1052
+ this[name] = value;
1053
+ break;
1108
1054
  }
1109
1055
  break;
1110
1056
  }
@@ -1279,12 +1225,15 @@ class Request extends module_1 {
1279
1225
  return Promise.reject((0, types_1.errorMessage)("aria2", "Binary not found"));
1280
1226
  }
1281
1227
  let pathname, headers, binOpts, signal, silent;
1282
- if ((0, types_1.isString)(options)) {
1228
+ if (typeof options === 'string') {
1283
1229
  pathname = options;
1284
1230
  }
1231
+ else if (options instanceof URL) {
1232
+ pathname = (0, node_url_1.fileURLToPath)(options);
1233
+ }
1285
1234
  else {
1286
1235
  ({ signal, silent } = options);
1287
- [pathname, headers, binOpts] = parseBinOpts(this, options, ['--daemon'], ['--input-file'], options.shellExpansion);
1236
+ ({ pathname, headers, binOpts } = this.parseBinOpts(options, ['--daemon'], ['--input-file'], options.shellExpansion));
1288
1237
  }
1289
1238
  try {
1290
1239
  if (typeof uri === 'string' && module_1.isURL(uri)) {
@@ -1536,13 +1485,17 @@ class Request extends module_1 {
1536
1485
  }
1537
1486
  uri = uri.toString();
1538
1487
  let pathname, headers, binOpts, silent;
1539
- if ((0, types_1.isString)(options)) {
1488
+ if (typeof options === 'string') {
1540
1489
  pathname = options;
1541
1490
  options = {};
1542
1491
  }
1492
+ else if (options instanceof URL) {
1493
+ pathname = (0, node_url_1.fileURLToPath)(options);
1494
+ options = {};
1495
+ }
1543
1496
  else {
1544
1497
  silent = options.silent;
1545
- [pathname, headers, binOpts] = parseBinOpts(this, options, ['--interactive', '--dry-run'], ['--partial-suffix', '--verbose'], options.shellExpansion);
1498
+ ({ pathname, headers, binOpts } = this.parseBinOpts(options, ['--interactive', '--dry-run'], ['–name-transform', '--partial-suffix', '--verbose'], options.shellExpansion));
1546
1499
  }
1547
1500
  const command = options.command || 'copy';
1548
1501
  let source;
@@ -2422,6 +2375,69 @@ class Request extends module_1 {
2422
2375
  }
2423
2376
  request.destroy(reason);
2424
2377
  }
2378
+ parseBinOpts(options, ignore, skip, doubleQuote = false) {
2379
+ let pathname = options.pathname, binOpts;
2380
+ if (pathname instanceof URL) {
2381
+ pathname = (0, node_url_1.fileURLToPath)(pathname);
2382
+ }
2383
+ else if (!(0, types_1.isString)(pathname)) {
2384
+ if (!this.host) {
2385
+ return {};
2386
+ }
2387
+ pathname = process.cwd();
2388
+ }
2389
+ if ((0, types_1.isArray)(options.binOpts)) {
2390
+ let next = false;
2391
+ binOpts = options.binOpts.filter((opt) => !((0, types_1.isString)(opt) && /^-[a-z].*$/i.test(opt.trim()))).map((opt) => {
2392
+ if (next) {
2393
+ if (!module_1.asString(opt).startsWith('--')) {
2394
+ return [];
2395
+ }
2396
+ next = false;
2397
+ }
2398
+ switch (typeof opt) {
2399
+ case 'string': {
2400
+ const value = opt.trim();
2401
+ if (value.startsWith('--')) {
2402
+ const match = /^(--[a-z]+[a-z0-9-]*)(=)?\s*(.*)$/s.exec(value);
2403
+ if (match) {
2404
+ if (ignore.includes(match[1])) {
2405
+ return [];
2406
+ }
2407
+ if (skip.includes(match[1])) {
2408
+ if (!match[2]) {
2409
+ next = true;
2410
+ }
2411
+ return [];
2412
+ }
2413
+ switch (match[1]) {
2414
+ case '--version':
2415
+ case '--help':
2416
+ return [];
2417
+ default:
2418
+ return match[3] ? [match[1] + '=' + (0, types_1.sanitizeArgs)(match[3], doubleQuote)] : [match[1]];
2419
+ }
2420
+ }
2421
+ }
2422
+ else if (value) {
2423
+ return [(0, types_1.sanitizeArgs)(value, doubleQuote)];
2424
+ }
2425
+ break;
2426
+ }
2427
+ case 'number':
2428
+ case 'boolean':
2429
+ return [opt.toString()];
2430
+ default:
2431
+ if ((0, types_1.isArray)(opt)) {
2432
+ return opt.filter(item => (0, types_1.isString)(item)).map(item => (0, types_1.sanitizeArgs)(item, doubleQuote));
2433
+ }
2434
+ break;
2435
+ }
2436
+ return [];
2437
+ }).flat();
2438
+ }
2439
+ return { pathname, headers: (0, util_1.parseOutgoingHeaders)(options.headers), binOpts };
2440
+ }
2425
2441
  set adapter(value) {
2426
2442
  if (isConstructor(value) && value.prototype instanceof adapter_1) {
2427
2443
  this.#adapter = value;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/request",
3
- "version": "0.12.7",
3
+ "version": "0.12.9",
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.7",
23
- "@e-mc/types": "0.12.7",
22
+ "@e-mc/module": "0.12.9",
23
+ "@e-mc/types": "0.12.9",
24
24
  "combined-stream": "^1.0.8",
25
25
  "js-yaml": "^4.1.0",
26
26
  "picomatch": "^4.0.3",