@e-mc/request 0.7.6 → 0.7.8

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
@@ -1,7 +1,5 @@
1
1
  # @e-mc/request
2
2
 
3
- PEP 402 - Forever 2056
4
-
5
3
  ## LICENSE
6
4
 
7
5
  MIT
@@ -102,28 +102,28 @@ class HttpHost {
102
102
  return this._tlsConnect || (this._tlsConnect = new Promise(resolve => {
103
103
  const alpn = 'h' + version;
104
104
  const socket = tls.connect(+this.port, this.hostname, { ALPNProtocols: [alpn], requestCert: true, rejectUnauthorized: false }, () => {
105
- resolve(data[3] = alpn === socket.alpnProtocol ? 1 : 0);
106
105
  this._tlsConnect = null;
106
+ resolve(data[3] = alpn === socket.alpnProtocol ? 1 : 0);
107
107
  });
108
108
  socket
109
109
  .setNoDelay(false)
110
110
  .setTimeout(10000)
111
111
  .on('timeout', () => {
112
+ socket.destroy();
112
113
  if (this._tlsConnect) {
114
+ this._tlsConnect = null;
113
115
  if (this.error(version) >= 10) {
114
116
  resolve(data[3] = 0);
115
117
  }
116
118
  else {
117
119
  resolve(2);
118
120
  }
119
- this._tlsConnect = null;
120
121
  }
121
- socket.destroy();
122
122
  })
123
123
  .on('error', () => {
124
124
  this.failed(version);
125
- resolve(data[3] = 0);
126
125
  this._tlsConnect = null;
126
+ resolve(data[3] = 0);
127
127
  })
128
128
  .end();
129
129
  }));
package/index.js CHANGED
@@ -257,6 +257,61 @@ function validateCerts(certs) {
257
257
  }
258
258
  return [text, file];
259
259
  }
260
+ function checkEncoding(request, response, statusCode, outStream, contentEncoding = '') {
261
+ switch (statusCode) {
262
+ case 206:
263
+ request.emit('error', (0, types_1.errorValue)("Aborted", 'Partial content'));
264
+ case 204:
265
+ return;
266
+ default:
267
+ contentEncoding = contentEncoding.trim();
268
+ if (!contentEncoding) {
269
+ return;
270
+ }
271
+ contentEncoding = contentEncoding.toLowerCase();
272
+ break;
273
+ }
274
+ const chunkSize = outStream === null || outStream === void 0 ? void 0 : outStream.writableHighWaterMark;
275
+ let pipeTo;
276
+ if (contentEncoding.indexOf(',') === -1) {
277
+ pipeTo = decompressEncoding(contentEncoding, chunkSize);
278
+ }
279
+ else {
280
+ for (const value of contentEncoding.split(/\s*,\s*/).reverse()) {
281
+ const next = decompressEncoding(value, chunkSize);
282
+ if (!next) {
283
+ return;
284
+ }
285
+ pipeTo = pipeTo ? pipeTo.pipe(next) : next;
286
+ }
287
+ }
288
+ if (pipeTo) {
289
+ if (outStream) {
290
+ stream.pipeline(response, pipeTo, outStream, err => err && response.emit('error', err));
291
+ }
292
+ else {
293
+ stream.pipeline(response, pipeTo, err => err && response.emit('error', err));
294
+ }
295
+ return pipeTo;
296
+ }
297
+ }
298
+ function decompressEncoding(value, chunkSize) {
299
+ switch (value) {
300
+ case 'gzip':
301
+ return zlib.createGunzip({ chunkSize });
302
+ case 'br':
303
+ return zlib.createBrotliDecompress({ chunkSize });
304
+ case 'deflate':
305
+ return zlib.createInflate({ chunkSize });
306
+ case 'deflate-raw':
307
+ return zlib.createInflateRaw({ chunkSize });
308
+ case 'zstd':
309
+ if (LIB_ZSTD) {
310
+ return new LIB_ZSTD.ZstdDecompressTransform({ writableHighWaterMark: chunkSize });
311
+ }
312
+ break;
313
+ }
314
+ }
260
315
  class Request extends module_1.default {
261
316
  static purgeMemory(percent = 1, limit = 0, parent) {
262
317
  if (percent >= 1) {
@@ -1366,44 +1421,6 @@ class Request extends module_1.default {
1366
1421
  url = new URL(uri);
1367
1422
  options.url = url;
1368
1423
  }
1369
- const checkEncoding = (response, statusCode, contentEncoding = '') => {
1370
- switch (statusCode) {
1371
- case 206:
1372
- request.emit('error', new Error('[ABORT] Partial content'));
1373
- case 204:
1374
- return;
1375
- }
1376
- const chunkSize = outStream === null || outStream === void 0 ? void 0 : outStream.writableHighWaterMark;
1377
- let pipeTo;
1378
- switch (contentEncoding.trim().toLowerCase()) {
1379
- case 'gzip':
1380
- pipeTo = zlib.createGunzip({ chunkSize });
1381
- break;
1382
- case 'br':
1383
- pipeTo = zlib.createBrotliDecompress({ chunkSize });
1384
- break;
1385
- case 'deflate':
1386
- pipeTo = zlib.createInflate({ chunkSize });
1387
- break;
1388
- case 'deflate-raw':
1389
- pipeTo = zlib.createInflateRaw({ chunkSize });
1390
- break;
1391
- case 'zstd':
1392
- if (LIB_ZSTD) {
1393
- pipeTo = new LIB_ZSTD.ZstdDecompressTransform({ writableHighWaterMark: chunkSize });
1394
- }
1395
- break;
1396
- }
1397
- if (pipeTo) {
1398
- if (outStream) {
1399
- stream.pipeline(response, pipeTo, outStream, err => err && response.emit('error', err));
1400
- }
1401
- else {
1402
- stream.pipeline(response, pipeTo, err => err && response.emit('error', err));
1403
- }
1404
- return pipeTo;
1405
- }
1406
- };
1407
1424
  const { hostname, origin, secure, localhost } = host;
1408
1425
  const pathname = url.pathname + (socketPath ? '' : url.search);
1409
1426
  const proxy = this.proxyOf(uri, localhost);
@@ -1429,7 +1446,8 @@ class Request extends module_1.default {
1429
1446
  connected = true;
1430
1447
  const statusCode = response[':status'];
1431
1448
  if (statusCode >= 200 && statusCode < 300) {
1432
- if (emitter = checkEncoding(request, statusCode, response['content-encoding'])) {
1449
+ emitter = checkEncoding(request, request, statusCode, outStream, response['content-encoding']);
1450
+ if (emitter) {
1433
1451
  for (const event in listenerMap) {
1434
1452
  listenerMap[event].forEach(listener => {
1435
1453
  const [name, type] = event.split('-');
@@ -1550,7 +1568,7 @@ class Request extends module_1.default {
1550
1568
  const statusCode = response.statusCode;
1551
1569
  if ((getting || posting) && statusCode >= 200 && statusCode < 300) {
1552
1570
  const incoming = response.headers;
1553
- let source = checkEncoding(response, statusCode, incoming['content-encoding']);
1571
+ let source = checkEncoding(request, response, statusCode, outStream, incoming['content-encoding']);
1554
1572
  if (source) {
1555
1573
  source.once('finish', () => request.emit('end'));
1556
1574
  }
@@ -1788,6 +1806,9 @@ class Request extends module_1.default {
1788
1806
  const startTime = log ? process.hrtime() : 0;
1789
1807
  let retries = 0, redirects = 0, closed, timeout, outStream;
1790
1808
  const throwError = (err, outAbort) => {
1809
+ if (timeout) {
1810
+ clearTimeout(timeout);
1811
+ }
1791
1812
  if (!closed) {
1792
1813
  closed = true;
1793
1814
  if (outStream && (0, types_1.isString)(pipeTo)) {
@@ -1795,9 +1816,6 @@ class Request extends module_1.default {
1795
1816
  }
1796
1817
  reject(typeof err === 'string' ? new Error(err) : err);
1797
1818
  }
1798
- if (timeout) {
1799
- clearTimeout(timeout);
1800
- }
1801
1819
  if (outAbort) {
1802
1820
  this[kDownloading].delete(outAbort);
1803
1821
  }
@@ -1992,10 +2010,10 @@ class Request extends module_1.default {
1992
2010
  result = encoding && !pipeline ? '' : null;
1993
2011
  titleBgColor = 'bgBlue';
1994
2012
  }
1995
- resolve(result);
1996
2013
  if (log) {
1997
2014
  this.writeTimeProcess('HTTP' + httpVersion, request.statusMessage || url.toString(), startTime, { type: 1024, queue: !!this.host, titleBgColor, messageUnit, messageUnitMinWidth: 9, delayTime, bypassLog: LOG_STDOUT });
1998
2015
  }
2016
+ resolve(result);
1999
2017
  });
2000
2018
  host.success(httpVersion);
2001
2019
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/request",
3
- "version": "0.7.6",
3
+ "version": "0.7.8",
4
4
  "description": "Request constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -20,8 +20,8 @@
20
20
  "license": "MIT",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/module": "0.7.6",
24
- "@e-mc/types": "0.7.6",
23
+ "@e-mc/module": "0.7.8",
24
+ "@e-mc/types": "0.7.8",
25
25
  "combined-stream": "^1.0.8",
26
26
  "js-yaml": "^4.1.0",
27
27
  "which": "^2.0.2"