@e-mc/request 0.14.1 → 0.14.3

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,12 +9,12 @@
9
9
 
10
10
  ## Interface
11
11
 
12
- * [View Source](https://www.unpkg.com/@e-mc/types@0.14.1/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.14.3/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { IModule, ModuleConstructor } from "./index";
16
16
  import type { HttpAgentSettings, HttpProtocolVersion, HttpRequestClient, InternetProtocolVersion } from "./http";
17
- import type { ApplyOptions, Aria2Options, HeadersOnCallback, HostConfig, IHttpAdapter, OpenOptions, PostFileParts, PostOptions, ProxySettings, PutOptions, RcloneOptions, ReadExpectType, RequestInit, StatusOnCallback } from "./request";
17
+ import type { ApplyOptions, Aria2Options, HeadersOnCallback, HostConfig, IHttpAdapter, OpenOptions, PatchOptions, PostFileParts, PostOptions, ProxySettings, PutOptions, RcloneOptions, ReadExpectType, RequestInit, StatusOnCallback } from "./request";
18
18
  import type { DnsLookupSettings, RequestModule, RequestSettings } from "./settings";
19
19
 
20
20
  import type { ClientRequest, OutgoingHttpHeaders } from "node:http";
@@ -52,6 +52,8 @@ interface IRequest extends IModule {
52
52
  head(uri: string | URL, options?: OpenOptions): ClientRequest;
53
53
  put(uri: string | URL, data: unknown, options: PutOptions): Promise<Buffer | string | null>;
54
54
  put(uri: string | URL, data: unknown, contentType?: string, options?: PutOptions): Promise<Buffer | string | null>;
55
+ patch(uri: string | URL, data: unknown, options: PatchOptions): Promise<Buffer | string | null>;
56
+ patch(uri: string | URL, data: unknown, contentType?: string, options?: PatchOptions): Promise<Buffer | string | null>;
55
57
  post(uri: string | URL, parts: PostFileParts): Promise<Buffer | string | null>;
56
58
  post(uri: string | URL, form: Record<string, unknown>, parts: PostFileParts): Promise<Buffer | string | null>;
57
59
  post(uri: string | URL, data: unknown, options: PostOptions): Promise<Buffer | string | null>;
@@ -253,9 +255,9 @@ instance.get("http://hostname/path/config.yml", options).then(data => {
253
255
 
254
256
  ## References
255
257
 
256
- - https://www.unpkg.com/@e-mc/types@0.14.1/lib/http.d.ts
257
- - https://www.unpkg.com/@e-mc/types@0.14.1/lib/request.d.ts
258
- - https://www.unpkg.com/@e-mc/types@0.14.1/lib/settings.d.ts
258
+ - https://www.unpkg.com/@e-mc/types@0.14.3/lib/http.d.ts
259
+ - https://www.unpkg.com/@e-mc/types@0.14.3/lib/request.d.ts
260
+ - https://www.unpkg.com/@e-mc/types@0.14.3/lib/settings.d.ts
259
261
 
260
262
  * https://www.npmjs.com/package/@types/node
261
263
 
@@ -388,7 +388,7 @@ class HttpAdapter {
388
388
  if (updating) {
389
389
  this.updateProgress(0, this.contentLength);
390
390
  }
391
- if (enabled && this.instance.readExpect === 'always' && !empty) {
391
+ if (enabled && this.instance.readExpect === 'always' && !empty || opts.throwsEmpty) {
392
392
  this.terminate("No data received");
393
393
  return;
394
394
  }
package/index.js CHANGED
@@ -37,7 +37,8 @@ const HTTP = {
37
37
  };
38
38
  const TLS = {
39
39
  TEXT: Object.create(null),
40
- FILE: Object.create(null)
40
+ FILE: Object.create(null),
41
+ EXTRA_CA_CERTS: isString(process.env.NODE_EXTRA_CA_CERTS) && path.isAbsolute(process.env.NODE_EXTRA_CA_CERTS) && Module.isFile(process.env.NODE_EXTRA_CA_CERTS)
41
42
  };
42
43
  const DNS = {
43
44
  CACHE: Object.create(null),
@@ -200,7 +201,7 @@ function resetHttpHost(version) {
200
201
  break;
201
202
  }
202
203
  }
203
- function validateCerts(certs) {
204
+ function validateCerts(certs, global) {
204
205
  const text = {};
205
206
  const file = {};
206
207
  for (let origin in certs) {
@@ -211,7 +212,7 @@ function validateCerts(certs) {
211
212
  catch {
212
213
  continue;
213
214
  }
214
- if (ca) {
215
+ if (ca && (!TLS.EXTRA_CA_CERTS || !global)) {
215
216
  if (!Array.isArray(ca)) {
216
217
  ca = [ca];
217
218
  }
@@ -282,6 +283,19 @@ function copySearchParams(url, base) {
282
283
  }
283
284
  });
284
285
  }
286
+ function setOutgoingMethod(method, contentType, options) {
287
+ if (isPlainObject(contentType)) {
288
+ options = contentType;
289
+ }
290
+ else {
291
+ (options ||= {}).contentType = contentType;
292
+ }
293
+ options.method = method;
294
+ if (options.contentType === "multipart/form-data") {
295
+ options.contentType = "application/x-www-form-urlencoded";
296
+ }
297
+ return options;
298
+ }
285
299
  function checkEncoding(request, statusCode, contentEncoding) {
286
300
  switch (statusCode) {
287
301
  case 206:
@@ -648,7 +662,7 @@ class Request extends Module {
648
662
  }
649
663
  }
650
664
  if (isPlainObject(certs)) {
651
- [TLS.TEXT, TLS.FILE] = validateCerts(certs);
665
+ [TLS.TEXT, TLS.FILE] = validateCerts(certs, true);
652
666
  }
653
667
  HTTP.PROXY = getProxySettings(request, agent_timeout);
654
668
  HttpHost.defineHostConfig(request);
@@ -779,6 +793,9 @@ class Request extends Module {
779
793
  this.readTimeout = (value = fromSeconds(read_timeout)) >= 0 ? value : READ_TIMEOUT;
780
794
  this.keepAlive = typeof (value = agent?.keep_alive) === 'boolean' ? value : KEEP_ALIVE;
781
795
  this.acceptEncoding = typeof (value = use?.accept_encoding) === 'boolean' ? value : ACCEPT_ENCODING;
796
+ if ((value = asInt(use?.http_version)) === 1 || value === 2) {
797
+ this.#httpVersion = value;
798
+ }
782
799
  this.#ipVersion = (value = asInt(data.dns?.family)) && (value === 4 || value === 6) ? value : 0;
783
800
  if ((value = fromSeconds(agent?.timeout)) >= 0) {
784
801
  this.#agentTimeout = value;
@@ -787,16 +804,13 @@ class Request extends Module {
787
804
  this.#agentTimeout = AGENT_TIMEOUT;
788
805
  value = undefined;
789
806
  }
790
- if ((value = asInt(use?.http_version)) === 1 || value === 2) {
791
- this.#httpVersion = value;
792
- }
793
807
  const proxy = getProxySettings(data, value);
794
808
  if (proxy) {
795
809
  this.proxy = proxy;
796
810
  }
797
811
  this.parseHeaders(headers);
798
812
  if (isObject(certs)) {
799
- this.#certs = validateCerts(certs);
813
+ this.#certs = validateCerts(certs, false);
800
814
  }
801
815
  if (isObject(connect)) {
802
816
  this.apply({
@@ -1662,7 +1676,7 @@ class Request extends Module {
1662
1676
  return { ...options, host, url };
1663
1677
  }
1664
1678
  open(uri, options) {
1665
- let { host, url, httpVersion, search, encoding, format, socketPath, expectContinue = false, timeout = this._config.connectTimeout, outStream } = options, headers = copyHeaders(options.headers), getting = false, posting = false;
1679
+ let { host, url, httpVersion, search, proxyHost, encoding, format, socketPath, expectContinue = false, timeout = this._config.connectTimeout, outStream } = options, headers = copyHeaders(options.headers), getting = false, posting = false;
1666
1680
  const method = (options.method?.toUpperCase() || 'GET');
1667
1681
  switch (method) {
1668
1682
  case 'GET':
@@ -1671,6 +1685,7 @@ class Request extends Module {
1671
1685
  break;
1672
1686
  case 'POST':
1673
1687
  case 'PUT':
1688
+ case 'PATCH':
1674
1689
  posting = true;
1675
1690
  break;
1676
1691
  }
@@ -1680,7 +1695,7 @@ class Request extends Module {
1680
1695
  ({ out: format = 'json', parser } = format);
1681
1696
  }
1682
1697
  if (format.includes('/')) {
1683
- format = format.split('/').pop().split('-').pop();
1698
+ format = format.split('/').at(-1).split('-').at(-1);
1684
1699
  }
1685
1700
  headers ||= {};
1686
1701
  switch (format = format.toLowerCase()) {
@@ -1729,6 +1744,9 @@ class Request extends Module {
1729
1744
  }
1730
1745
  options.url = url;
1731
1746
  }
1747
+ else if (search) {
1748
+ options.url = new URL(url);
1749
+ }
1732
1750
  if (options.base) {
1733
1751
  this.#baseUrl = [url, httpVersion, headers];
1734
1752
  }
@@ -1742,14 +1760,24 @@ class Request extends Module {
1742
1760
  for (const param in search) {
1743
1761
  url.searchParams.append(param, search[param]);
1744
1762
  }
1763
+ url.searchParams.sort();
1745
1764
  uri = url.href;
1746
1765
  }
1747
1766
  const { hostname, origin, secure } = host;
1748
1767
  const pathname = url.pathname + (socketPath ? '' : url.search);
1749
- const proxy = this.proxyOf(uri, host.localhost);
1750
1768
  const version = this.httpVersion;
1751
1769
  const baseHeaders = this.headersOf(uri);
1752
- let request, ca, cert, key, ciphers, minVersion;
1770
+ let request, ca, cert, key, ciphers, proxy, minVersion;
1771
+ if (proxyHost) {
1772
+ let pass = new URL(proxyHost.uri);
1773
+ if (proxyHost.username) {
1774
+ pass = new URL(pass.protocol + '//' + getBasicAuth(proxyHost.username, proxyHost.password) + pass.host);
1775
+ }
1776
+ proxy = { host: pass, keepAlive: proxyHost.keepAlive ?? this.keepAlive ?? true, agentOptions: proxyHost.agentOptions };
1777
+ }
1778
+ else if (proxyHost !== null) {
1779
+ proxy = this.proxyOf(uri, host.localhost);
1780
+ }
1753
1781
  if (getting && this.acceptEncoding && !host.localhost && !baseHeaders?.['accept-encoding']) {
1754
1782
  (headers ||= {})['accept-encoding'] ||= 'gzip, deflate, br' + (SUPPORTED_ZSTD ? ', zstd' : '');
1755
1783
  }
@@ -1783,9 +1811,12 @@ class Request extends Module {
1783
1811
  }
1784
1812
  if (headers) {
1785
1813
  const cacheControl = headers['cache-control']?.toLowerCase();
1786
- if (headers.authorization && !cacheControl?.includes('max-age') || cacheControl?.includes('no-cache')) {
1814
+ if (headers.authorization && !cacheControl?.includes('max-age')) {
1787
1815
  options.noCache ??= true;
1788
1816
  }
1817
+ else if (cacheControl?.includes('no-cache')) {
1818
+ options.noCache ??= 0;
1819
+ }
1789
1820
  if (cacheControl?.includes('no-store')) {
1790
1821
  options.noStore ??= true;
1791
1822
  }
@@ -1919,7 +1950,7 @@ class Request extends Module {
1919
1950
  }
1920
1951
  if (!agent) {
1921
1952
  let proxyEnv;
1922
- if (SUPPORTED_PROXY && !(proxyEnv = agentOptions?.proxyEnv) && proxy) {
1953
+ if (SUPPORTED_PROXY && proxyHost !== null && !(proxyEnv = agentOptions?.proxyEnv) && proxy) {
1923
1954
  const http_proxy = proxy.host.toString();
1924
1955
  proxyEnv = { http_proxy, https_proxy: http_proxy };
1925
1956
  }
@@ -2086,17 +2117,10 @@ class Request extends Module {
2086
2117
  return this.open(out.url, out);
2087
2118
  }
2088
2119
  async put(uri, data, contentType, options) {
2089
- if (isPlainObject(contentType)) {
2090
- options = contentType;
2091
- }
2092
- else {
2093
- (options ||= {}).contentType = contentType;
2094
- }
2095
- options.method = 'PUT';
2096
- if (options.contentType === "multipart/form-data") {
2097
- options.contentType = "application/x-www-form-urlencoded";
2098
- }
2099
- return this.post(uri, data, options);
2120
+ return this.post(uri, data, setOutgoingMethod('PUT', contentType, options));
2121
+ }
2122
+ async patch(uri, data, contentType, options) {
2123
+ return this.post(uri, data, setOutgoingMethod('PATCH', contentType, options));
2100
2124
  }
2101
2125
  async post(uri, data, contentType, options) {
2102
2126
  let putting = false, parts;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/request",
3
- "version": "0.14.1",
3
+ "version": "0.14.3",
4
4
  "description": "Request constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -16,10 +16,10 @@
16
16
  "license": "BSD-3-Clause",
17
17
  "homepage": "https://github.com/anpham6/e-mc#readme",
18
18
  "dependencies": {
19
- "@e-mc/module": "0.14.1",
20
- "@e-mc/types": "0.14.1",
19
+ "@e-mc/module": "0.14.3",
20
+ "@e-mc/types": "0.14.3",
21
21
  "combined-stream": "^1.0.8",
22
- "js-yaml": "^4.1.1",
22
+ "js-yaml": "^4.2.0",
23
23
  "picomatch": "^4.0.4",
24
24
  "which": "^4.0.0"
25
25
  }