@e-mc/request 0.13.8 → 0.13.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.13.8/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.13.9/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { IModule, ModuleConstructor } from "./index";
@@ -45,6 +45,7 @@ interface IRequest extends IModule {
45
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
+ blob(uri: string | URL, options?: OpenOptions): Promise<Blob | null>;
48
49
  pipe(uri: string | URL, to: Writable, options?: OpenOptions): Promise<null>;
49
50
  opts(url: string | URL, options?: OpenOptions): HostConfig;
50
51
  open(uri: string | URL, options: OpenOptions): HttpRequestClient;
@@ -252,9 +253,9 @@ instance.get("http://hostname/path/config.yml", options).then(data => {
252
253
 
253
254
  ## References
254
255
 
255
- - https://www.unpkg.com/@e-mc/types@0.13.8/lib/http.d.ts
256
- - https://www.unpkg.com/@e-mc/types@0.13.8/lib/request.d.ts
257
- - https://www.unpkg.com/@e-mc/types@0.13.8/lib/settings.d.ts
256
+ - https://www.unpkg.com/@e-mc/types@0.13.9/lib/http.d.ts
257
+ - https://www.unpkg.com/@e-mc/types@0.13.9/lib/request.d.ts
258
+ - https://www.unpkg.com/@e-mc/types@0.13.9/lib/settings.d.ts
258
259
 
259
260
  * https://www.npmjs.com/package/@types/node
260
261
 
@@ -249,6 +249,9 @@ class HttpAdapter {
249
249
  if ('outFilename' in opts) {
250
250
  opts.outFilename = (0, util_1.parseHeader)(headers, 'content-disposition');
251
251
  }
252
+ if ('outContentType' in opts) {
253
+ opts.outContentType = headers['content-type'];
254
+ }
252
255
  const pipeline = this.pipeTo ? !(0, types_1.isString)(this.pipeTo) : false;
253
256
  const enabled = opts.connected?.call(this.client, headers) !== false && !pipeline;
254
257
  const maxBufferSize = opts.maxBufferSize ? (0, types_1.alignSize)(opts.maxBufferSize) : 0;
@@ -356,7 +359,7 @@ class HttpAdapter {
356
359
  break;
357
360
  case 'toml':
358
361
  try {
359
- result = require('smol-toml').parse(buffer);
362
+ result = require('smol-toml').parse(buffer, parser);
360
363
  }
361
364
  catch {
362
365
  result = require(packageName = 'toml').parse(buffer);
@@ -378,6 +381,9 @@ class HttpAdapter {
378
381
  }
379
382
  }
380
383
  }
384
+ else if (opts.outFormat) {
385
+ opts.outFormat = undefined;
386
+ }
381
387
  if (result === undefined) {
382
388
  result = buffer;
383
389
  }
package/index.js CHANGED
@@ -23,6 +23,7 @@ const adapter_1 = require("@e-mc/request/http/adapter");
23
23
  const kRequest = Symbol.for('request:constructor');
24
24
  const SUPPORTED_NODE20 = (0, types_1.supported)(20);
25
25
  const SUPPORTED_ZSTD = (0, types_1.supported)(23, 8) || (0, types_1.supported)(22, 15, true);
26
+ const SUPPORTED_FILE = (0, types_1.supported)(19, 2) || (0, types_1.supported)(18, 13, true);
26
27
  const SUPPORTED_PROXY = (0, types_1.supported)(24, 5);
27
28
  const REGEXP_GLOBWITHIN = /\\\?|(?:(?<!\\)(?:\*|\[!?[^!\]]+\]|\{(?:[^,]+,)+[^}]+\}|[!?+*@]\((?:[^|]+\|)*[^)]+\)|\?.*\?|\?$))/;
28
29
  const REGEXP_RCLONE = /^rclone:\?/i;
@@ -114,6 +115,7 @@ let KEEP_ALIVE = null;
114
115
  let ACCEPT_ENCODING = false;
115
116
  let READ_TIMEOUT = 0;
116
117
  let AGENT_TIMEOUT = 0;
118
+ let MAX_CONCURRENT_STREAMS = 100;
117
119
  let LOG_HTTP = false;
118
120
  let LOG_TIMEPROCESS = true;
119
121
  function setDnsCache(hostname, value, expires) {
@@ -586,10 +588,13 @@ class Request extends module_1 {
586
588
  }
587
589
  }
588
590
  if (request) {
589
- let { read_timeout, agent, use, headers, certs } = request, agent_timeout;
591
+ let { read_timeout, max_concurrent_streams, agent, use, headers, certs } = request, agent_timeout;
590
592
  if ((read_timeout = (0, util_1.fromSeconds)(read_timeout)) >= 0) {
591
593
  READ_TIMEOUT = read_timeout;
592
594
  }
595
+ if ((max_concurrent_streams = (0, util_1.asInt)(max_concurrent_streams)) > 0) {
596
+ MAX_CONCURRENT_STREAMS = max_concurrent_streams;
597
+ }
593
598
  if (agent) {
594
599
  let { keep_alive, timeout, proxy_env } = agent;
595
600
  if ((agent_timeout = (0, util_1.fromSeconds)(timeout)) > 0) {
@@ -1603,6 +1608,21 @@ class Request extends module_1 {
1603
1608
  options.format = 'json';
1604
1609
  return this.get(uri, options);
1605
1610
  }
1611
+ async blob(uri, options = {}) {
1612
+ options.outContentType = undefined;
1613
+ options.outFilename = undefined;
1614
+ options.outBlob = true;
1615
+ const data = await this.get(uri, options);
1616
+ if (!data) {
1617
+ return null;
1618
+ }
1619
+ const format = options.outFormat?.out;
1620
+ const type = { type: format ? 'application/' + format : options.outContentType || (typeof data === 'string' ? 'text/plain' : undefined) };
1621
+ if (SUPPORTED_FILE && options.outFilename) {
1622
+ return new File([Buffer.from(data)], path.basename(options.outFilename), type);
1623
+ }
1624
+ return new Blob([Buffer.from(data)], type);
1625
+ }
1606
1626
  async pipe(uri, to, options = {}) {
1607
1627
  options.pipeTo = to;
1608
1628
  return this.get(uri, options);
@@ -1670,7 +1690,9 @@ class Request extends module_1 {
1670
1690
  }
1671
1691
  headers.accept += ', text/plain';
1672
1692
  options.encoding = (0, types_1.getEncoding)(encoding);
1673
- options.outFormat = { out: format, parser };
1693
+ if (!options.outBlob) {
1694
+ options.outFormat = { out: format, parser };
1695
+ }
1674
1696
  }
1675
1697
  if (typeof uri !== 'string') {
1676
1698
  url = uri;
@@ -1743,7 +1765,22 @@ class Request extends module_1 {
1743
1765
  }
1744
1766
  }
1745
1767
  if (!proxy && httpVersion !== 1 && ((httpVersion || host.version) === 2 && version !== 1 || secure && version === 2 && host.failed(2, true) === 0)) {
1746
- request = (this.#session[0][origin] ||= http2.connect(origin, { lookup: this.lookupDns(hostname), ca, cert, key, ciphers, minVersion, settings: host.localhost ? { maxFrameSize: 16777215, enablePush: false } : { enablePush: false } })).request({ ...baseHeaders, ...host_1.getBasicAuth(url), ...headers, ':path': pathname, ':method': method });
1768
+ let session = this.#session[0][origin], conn, maxConcurrentStreams = 0;
1769
+ if (session) {
1770
+ maxConcurrentStreams = session[2];
1771
+ }
1772
+ else {
1773
+ maxConcurrentStreams = options.maxConcurrentStreams || MAX_CONCURRENT_STREAMS;
1774
+ session = [[], 0, maxConcurrentStreams];
1775
+ }
1776
+ if (session[0].length * maxConcurrentStreams <= session[1]) {
1777
+ conn = http2.connect(origin, { lookup: this.lookupDns(hostname), ca, cert, key, ciphers, minVersion, settings: host.localhost ? { enablePush: false, maxConcurrentStreams, maxFrameSize: 16777215 } : { enablePush: false, maxConcurrentStreams } });
1778
+ session[0].push(conn);
1779
+ }
1780
+ else {
1781
+ conn = session[0].at(-1);
1782
+ }
1783
+ request = conn.request({ ...baseHeaders, ...host_1.getBasicAuth(url), ...headers, ':path': pathname, ':method': method });
1747
1784
  if (getting) {
1748
1785
  const listenerMap = {};
1749
1786
  const onEvent = request.on.bind(request);
@@ -1831,6 +1868,7 @@ class Request extends module_1 {
1831
1868
  };
1832
1869
  }
1833
1870
  options.httpVersion = 2;
1871
+ session[1]++;
1834
1872
  }
1835
1873
  else {
1836
1874
  let agent;
@@ -2217,7 +2255,9 @@ class Request extends module_1 {
2217
2255
  const session = this.#session;
2218
2256
  session.forEach((protocol, index) => {
2219
2257
  for (const host in protocol) {
2220
- protocol[host].close();
2258
+ for (const conn of protocol[host][0]) {
2259
+ conn.close();
2260
+ }
2221
2261
  }
2222
2262
  session[index] = Object.create(null);
2223
2263
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/request",
3
- "version": "0.13.8",
3
+ "version": "0.13.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.13.8",
23
- "@e-mc/types": "0.13.8",
22
+ "@e-mc/module": "0.13.9",
23
+ "@e-mc/types": "0.13.9",
24
24
  "combined-stream": "^1.0.8",
25
25
  "js-yaml": "^4.1.1",
26
26
  "picomatch": "^4.0.3",