@e-mc/request 0.13.4 → 0.13.5

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.4/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.13.5/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { IModule, ModuleConstructor } from "./index";
@@ -252,9 +252,9 @@ instance.get("http://hostname/path/config.yml", options).then(data => {
252
252
 
253
253
  ## References
254
254
 
255
- - https://www.unpkg.com/@e-mc/types@0.13.4/lib/http.d.ts
256
- - https://www.unpkg.com/@e-mc/types@0.13.4/lib/request.d.ts
257
- - https://www.unpkg.com/@e-mc/types@0.13.4/lib/settings.d.ts
255
+ - https://www.unpkg.com/@e-mc/types@0.13.5/lib/http.d.ts
256
+ - https://www.unpkg.com/@e-mc/types@0.13.5/lib/request.d.ts
257
+ - https://www.unpkg.com/@e-mc/types@0.13.5/lib/settings.d.ts
258
258
 
259
259
  * https://www.npmjs.com/package/@types/node
260
260
 
@@ -79,13 +79,21 @@ class HttpAdapter {
79
79
  return;
80
80
  }
81
81
  const statusCode = headers[':status'];
82
- if (statusCode < 300) {
83
- this.acceptResponse(headers);
82
+ if (statusCode === 204 || statusCode === 304) {
83
+ this.acceptResponse(headers, true);
84
84
  }
85
- else if (statusCode < 400) {
85
+ else if (statusCode < 300) {
86
+ this.acceptResponse(headers, false);
87
+ }
88
+ else if (statusCode === 301 ||
89
+ statusCode === 302 ||
90
+ statusCode === 303 ||
91
+ statusCode === 307 ||
92
+ statusCode === 308) {
86
93
  this.redirectResponse(statusCode, headers.location);
87
94
  }
88
- else if (statusCode === 401 ||
95
+ else if (statusCode === 305 ||
96
+ statusCode === 401 ||
89
97
  statusCode === 402 ||
90
98
  statusCode === 403 ||
91
99
  statusCode === 404 ||
@@ -151,10 +159,17 @@ class HttpAdapter {
151
159
  return;
152
160
  }
153
161
  const statusCode = res.statusCode;
154
- if (statusCode < 300) {
155
- this.acceptResponse(res.headers);
162
+ if (statusCode === 204 || statusCode === 304) {
163
+ this.acceptResponse(res.headers, true);
164
+ }
165
+ else if (statusCode < 300) {
166
+ this.acceptResponse(res.headers, false);
156
167
  }
157
- else if (statusCode < 400) {
168
+ else if (statusCode === 301 ||
169
+ statusCode === 302 ||
170
+ statusCode === 303 ||
171
+ statusCode === 307 ||
172
+ statusCode === 308) {
158
173
  this.redirectResponse(statusCode, res.headers.location);
159
174
  }
160
175
  else if (this.isRetry(statusCode)) {
@@ -226,7 +241,7 @@ class HttpAdapter {
226
241
  this.opts.httpVersion = 1;
227
242
  this.init();
228
243
  }
229
- acceptResponse(headers) {
244
+ acceptResponse(headers, empty = false) {
230
245
  const opts = this.opts;
231
246
  if ('outHeaders' in opts) {
232
247
  opts.outHeaders = headers;
@@ -238,7 +253,7 @@ class HttpAdapter {
238
253
  const enabled = opts.connected?.call(this.client, headers) !== false && !pipeline;
239
254
  const maxBufferSize = opts.maxBufferSize ? (0, types_1.alignSize)(opts.maxBufferSize) : 0;
240
255
  this.contentLength = parseInt(headers['content-length'] || '0');
241
- const updating = opts.progressId !== undefined && !!this.instance.host && this.contentLength > 0;
256
+ const updating = opts.progressId !== undefined && !!this.instance.host && this.contentLength > 0 && !empty;
242
257
  const readTimeout = this.instance.readTimeout;
243
258
  let log = this.state.log, buffer = null, dataLength = 0;
244
259
  this.client.once('readable', () => {
@@ -267,7 +282,7 @@ class HttpAdapter {
267
282
  this.updateProgress(0, 0);
268
283
  }
269
284
  });
270
- if (enabled) {
285
+ if (enabled && !empty) {
271
286
  const encoding = opts.encoding;
272
287
  this.client.on('data', (chunk) => {
273
288
  if (buffer) {
@@ -366,7 +381,7 @@ class HttpAdapter {
366
381
  if (updating) {
367
382
  this.updateProgress(0, this.contentLength);
368
383
  }
369
- if (enabled && this.instance.readExpect === 'always') {
384
+ if (enabled && this.instance.readExpect === 'always' && !empty) {
370
385
  this.terminate("No data received");
371
386
  return;
372
387
  }
package/index.js CHANGED
@@ -166,7 +166,8 @@ function getProxySettings(request, agentTimeout) {
166
166
  if ((0, types_1.isArray)(proxy.exclude)) {
167
167
  exclude = proxy.exclude;
168
168
  }
169
- return { host, include, exclude, keepAlive: proxy.keep_alive, agentTimeout };
169
+ const keepAliveMsecs = (0, util_1.asInt)(proxy.keep_alive_interval);
170
+ return { host, include, exclude, keepAlive: proxy.keep_alive, keepAliveMsecs: keepAliveMsecs > 0 ? keepAliveMsecs : undefined, agentTimeout };
170
171
  }
171
172
  catch {
172
173
  }
@@ -300,6 +301,8 @@ function checkEncoding(request, response, statusCode, outStream, contentEncoding
300
301
  case 206:
301
302
  request.emit('error', (0, types_1.errorValue)("Aborted", 'Partial content'));
302
303
  case 204:
304
+ case 205:
305
+ case 304:
303
306
  return;
304
307
  }
305
308
  if (!contentEncoding) {
@@ -496,6 +499,16 @@ function addAria2Proxy(args, protocol, host) {
496
499
  args.push(`--${protocol}-proxy-passwd="${escapeShellQuote(decodeURIComponent(password))}"`);
497
500
  }
498
501
  }
502
+ function createAgentOptions(options) {
503
+ const { keepAlive, keepAliveMsecs, maxFreeSockets, timeout, maxSockets, maxTotalSockets, proxyEnv } = options;
504
+ if (keepAlive === true) {
505
+ return { keepAlive: true, timeout, maxSockets, maxTotalSockets, proxyEnv, keepAliveMsecs, maxFreeSockets };
506
+ }
507
+ if (keepAlive === false || typeof timeout === 'number' || typeof maxSockets === 'number' || typeof maxTotalSockets === 'number' || proxyEnv) {
508
+ return { keepAlive: false, timeout, maxSockets, maxTotalSockets, proxyEnv };
509
+ }
510
+ }
511
+ const hasResponse = (value) => value >= 200 && (value < 300 || value === 304);
499
512
  const isDirEnd = (value) => value.endsWith('/') || value.endsWith(path.sep);
500
513
  const trimCharEnd = (value) => value.substring(0, value.length - 1);
501
514
  const configureDns = (family, options) => family === 0 ? options : { family, hints: family === 6 ? dns.V4MAPPED : 0 };
@@ -674,7 +687,7 @@ class Request extends module_1 {
674
687
  READ_TIMEOUT = read_timeout;
675
688
  }
676
689
  if ((0, types_1.isObject)(agent)) {
677
- let { keep_alive, timeout } = agent;
690
+ let { keep_alive, timeout, proxy_env } = agent;
678
691
  if ((agent_timeout = (0, util_1.fromSeconds)(timeout)) > 0) {
679
692
  AGENT_TIMEOUT = agent_timeout;
680
693
  }
@@ -687,7 +700,8 @@ class Request extends module_1 {
687
700
  else {
688
701
  keep_alive = undefined;
689
702
  }
690
- this.defineHttpAgent({ keepAlive: keep_alive, timeout: agent_timeout });
703
+ const keepAliveMsecs = (0, util_1.asInt)(agent.keep_alive_interval);
704
+ this.defineHttpAgent({ keepAlive: keep_alive, timeout: agent_timeout, proxyEnv: proxy_env, keepAliveMsecs: keepAliveMsecs > 0 ? keepAliveMsecs : undefined });
691
705
  }
692
706
  if (use) {
693
707
  let { http_version, accept_encoding } = use;
@@ -730,24 +744,18 @@ class Request extends module_1 {
730
744
  return typeof value === 'string' && (value = value.trim()).length > 0 && /^-{3,}[ \t]*BEGIN[ \t].+\n-{3,}[ \t]*END[ \t][^-]+-{3,}$/s.test(value);
731
745
  }
732
746
  static defineHttpAgent(options) {
733
- const { keepAlive, timeout = 0 } = options;
747
+ const agentOptions = createAgentOptions(options);
734
748
  if (options.http instanceof http.Agent) {
735
749
  http.globalAgent = options.http;
736
750
  }
737
- else if (timeout > 0) {
738
- http.globalAgent = new http.Agent({ keepAlive, timeout });
739
- }
740
- else if (typeof keepAlive === 'boolean') {
741
- http.globalAgent = new http.Agent({ keepAlive });
751
+ else if (agentOptions) {
752
+ http.globalAgent = new http.Agent(agentOptions);
742
753
  }
743
754
  if (options.https instanceof https.Agent) {
744
755
  https.globalAgent = options.https;
745
756
  }
746
- else if (timeout > 0) {
747
- https.globalAgent = new https.Agent({ keepAlive, timeout });
748
- }
749
- else if (typeof keepAlive === 'boolean') {
750
- https.globalAgent = new https.Agent({ keepAlive });
757
+ else if (agentOptions) {
758
+ https.globalAgent = new https.Agent(agentOptions);
751
759
  }
752
760
  }
753
761
  static defineDnsLookup({ family, expires, resolve }, reset) {
@@ -1857,7 +1865,7 @@ class Request extends module_1 {
1857
1865
  return;
1858
1866
  }
1859
1867
  connected = true;
1860
- if (this.matchStatus(statusCode, url, response, request, options) && statusCode >= 200 && statusCode < 300) {
1868
+ if (this.matchStatus(statusCode, url, response, request, options) && hasResponse(statusCode)) {
1861
1869
  if (emitter = checkEncoding(request, request, statusCode, outStream, response['content-encoding'])) {
1862
1870
  for (const event in listenerMap) {
1863
1871
  const [name, type] = event.split('-');
@@ -1930,14 +1938,14 @@ class Request extends module_1 {
1930
1938
  else {
1931
1939
  let agent;
1932
1940
  if (!socketPath) {
1933
- let { keepAlive, agentTimeout } = options;
1941
+ let { keepAlive, agentOptions, agentTimeout = agentOptions?.timeout } = options;
1934
1942
  if (proxy) {
1935
1943
  keepAlive ??= proxy.keepAlive;
1936
1944
  agentTimeout ??= proxy.agentTimeout;
1937
1945
  const proxyHeaders = this.#headers && getBaseHeaders(proxy.host.href, this.#headers) || getBaseHeaders(proxy.host.href, HTTP.HEADERS);
1938
1946
  const pkg = secure ? 'https-proxy-agent' : 'http-proxy-agent';
1939
1947
  try {
1940
- agent = require(pkg)(proxy.host, (typeof keepAlive === 'boolean' || agentTimeout > 0) && agentTimeout !== 0 ? { keepAlive: keepAlive ?? true, timeout: agentTimeout, headers: proxyHeaders } : { headers: proxyHeaders });
1948
+ agent = require(pkg)(proxy.host, keepAlive === true || keepAlive === false && agentTimeout !== 0 || agentTimeout > 0 ? { ...agentOptions, keepAlive: keepAlive ?? true, timeout: agentTimeout, headers: proxyHeaders } : { ...agentOptions, headers: proxyHeaders });
1941
1949
  }
1942
1950
  catch (err) {
1943
1951
  if (!SUPPORTED_PROXY || proxyHeaders) {
@@ -1947,19 +1955,19 @@ class Request extends module_1 {
1947
1955
  }
1948
1956
  if (!agent) {
1949
1957
  let proxyEnv;
1950
- if (proxy && SUPPORTED_PROXY) {
1958
+ if (SUPPORTED_PROXY && !(proxyEnv = agentOptions?.proxyEnv) && proxy) {
1951
1959
  const http_proxy = proxy.host.toString();
1952
1960
  proxyEnv = { http_proxy, https_proxy: http_proxy };
1953
1961
  }
1954
1962
  if (keepAlive === false) {
1955
- agent = new (secure ? https.Agent : http.Agent)({ keepAlive: false, proxyEnv });
1963
+ agent = new (secure ? https.Agent : http.Agent)({ ...agentOptions, keepAlive: false, proxyEnv });
1956
1964
  }
1957
1965
  else if (keepAlive === true || agentTimeout > 0) {
1958
- agent = new (secure ? https.Agent : http.Agent)({ keepAlive: true, timeout: agentTimeout, proxyEnv });
1966
+ agent = new (secure ? https.Agent : http.Agent)({ ...agentOptions, keepAlive: true, timeout: agentTimeout, proxyEnv });
1959
1967
  }
1960
- else if (agentTimeout !== 0 || proxyEnv) {
1968
+ else if (agentOptions || proxyEnv) {
1961
1969
  agentTimeout ??= this.agentTimeout;
1962
- agent = new (secure ? https.Agent : http.Agent)({ keepAlive: this.keepAlive ?? true, timeout: agentTimeout || undefined, proxyEnv });
1970
+ agent = new (secure ? https.Agent : http.Agent)({ ...agentOptions, keepAlive: this.keepAlive ?? true, timeout: agentTimeout || undefined, proxyEnv });
1963
1971
  }
1964
1972
  }
1965
1973
  }
@@ -1984,7 +1992,7 @@ class Request extends module_1 {
1984
1992
  }, response => {
1985
1993
  const statusCode = response.statusCode;
1986
1994
  const incoming = response.headers;
1987
- if (!expectContinue && this.matchStatus(statusCode, url, incoming, request, options) && (getting || posting) && statusCode >= 200 && statusCode < 300) {
1995
+ if (!expectContinue && this.matchStatus(statusCode, url, incoming, request, options) && (getting || posting) && hasResponse(statusCode)) {
1988
1996
  let source = checkEncoding(request, response, statusCode, outStream, incoming['content-encoding']);
1989
1997
  if (source) {
1990
1998
  source.once('finish', () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/request",
3
- "version": "0.13.4",
3
+ "version": "0.13.5",
4
4
  "description": "Request constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -19,10 +19,10 @@
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.4",
23
- "@e-mc/types": "0.13.4",
22
+ "@e-mc/module": "0.13.5",
23
+ "@e-mc/types": "0.13.5",
24
24
  "combined-stream": "^1.0.8",
25
- "js-yaml": "^4.1.0",
25
+ "js-yaml": "^4.1.1",
26
26
  "picomatch": "^4.0.3",
27
27
  "which": "^4.0.0"
28
28
  }