@e-mc/request 0.5.6 → 0.5.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,6 +1,6 @@
1
1
  # @e-mc/request
2
2
 
3
- PEP 402 - Forever
3
+ PEP 402 - Forever Any Ketchup
4
4
 
5
5
  ## LICENSE
6
6
 
@@ -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(5000)
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
@@ -248,6 +248,56 @@ function validateCerts(certs) {
248
248
  }
249
249
  return [text, file];
250
250
  }
251
+ function checkEncoding(request, response, statusCode, outStream, contentEncoding = '') {
252
+ switch (statusCode) {
253
+ case 206:
254
+ request.emit('error', (0, types_1.errorValue)("Aborted", 'Partial content'));
255
+ case 204:
256
+ return;
257
+ default:
258
+ contentEncoding = contentEncoding.trim();
259
+ if (!contentEncoding) {
260
+ return;
261
+ }
262
+ contentEncoding = contentEncoding.toLowerCase();
263
+ break;
264
+ }
265
+ const chunkSize = outStream === null || outStream === void 0 ? void 0 : outStream.writableHighWaterMark;
266
+ let pipeTo;
267
+ if (contentEncoding.indexOf(',') === -1) {
268
+ pipeTo = decompressEncoding(contentEncoding, chunkSize);
269
+ }
270
+ else {
271
+ for (const value of contentEncoding.split(/\s*,\s*/).reverse()) {
272
+ const next = decompressEncoding(value, chunkSize);
273
+ if (!next) {
274
+ return;
275
+ }
276
+ pipeTo = pipeTo ? pipeTo.pipe(next) : next;
277
+ }
278
+ }
279
+ if (pipeTo) {
280
+ if (outStream) {
281
+ stream.pipeline(response, pipeTo, outStream, err => err && response.emit('error', err));
282
+ }
283
+ else {
284
+ stream.pipeline(response, pipeTo, err => err && response.emit('error', err));
285
+ }
286
+ return pipeTo;
287
+ }
288
+ }
289
+ function decompressEncoding(value, chunkSize) {
290
+ switch (value) {
291
+ case 'gzip':
292
+ return zlib.createGunzip({ chunkSize });
293
+ case 'br':
294
+ return zlib.createBrotliDecompress({ chunkSize });
295
+ case 'deflate':
296
+ return zlib.createInflate({ chunkSize });
297
+ case 'deflate-raw':
298
+ return zlib.createInflateRaw({ chunkSize });
299
+ }
300
+ }
251
301
  class Request extends module_1.default {
252
302
  static purgeMemory(percent = 1, limit = 0, parent) {
253
303
  if (percent >= 1) {
@@ -1335,39 +1385,6 @@ class Request extends module_1.default {
1335
1385
  url = new URL(uri);
1336
1386
  options.url = url;
1337
1387
  }
1338
- const checkEncoding = (response, statusCode, contentEncoding = '') => {
1339
- switch (statusCode) {
1340
- case 206:
1341
- request.emit('error', new Error('[ABORT] Partial content'));
1342
- case 204:
1343
- return;
1344
- }
1345
- const chunkSize = outStream === null || outStream === void 0 ? void 0 : outStream.writableHighWaterMark;
1346
- let pipeTo;
1347
- switch (contentEncoding.trim().toLowerCase()) {
1348
- case 'gzip':
1349
- pipeTo = zlib.createGunzip({ chunkSize });
1350
- break;
1351
- case 'br':
1352
- pipeTo = zlib.createBrotliDecompress({ chunkSize });
1353
- break;
1354
- case 'deflate':
1355
- pipeTo = zlib.createInflate({ chunkSize });
1356
- break;
1357
- case 'deflate-raw':
1358
- pipeTo = zlib.createInflateRaw({ chunkSize });
1359
- break;
1360
- }
1361
- if (pipeTo) {
1362
- if (outStream) {
1363
- stream.pipeline(response, pipeTo, outStream, err => err && response.emit('error', err));
1364
- }
1365
- else {
1366
- stream.pipeline(response, pipeTo, err => err && response.emit('error', err));
1367
- }
1368
- return pipeTo;
1369
- }
1370
- };
1371
1388
  const { hostname, origin } = host;
1372
1389
  const pathname = url.pathname + (socketPath ? '' : url.search);
1373
1390
  const proxy = this.proxyOf(uri, host.localhost);
@@ -1392,7 +1409,8 @@ class Request extends module_1.default {
1392
1409
  connected = true;
1393
1410
  const statusCode = response[':status'];
1394
1411
  if (statusCode >= 200 && statusCode < 300) {
1395
- if (emitter = checkEncoding(request, statusCode, response['content-encoding'])) {
1412
+ emitter = checkEncoding(request, request, statusCode, outStream, response['content-encoding']);
1413
+ if (emitter) {
1396
1414
  for (const event in listenerMap) {
1397
1415
  listenerMap[event].forEach(listener => {
1398
1416
  const [name, type] = event.split('-');
@@ -1511,7 +1529,7 @@ class Request extends module_1.default {
1511
1529
  }, response => {
1512
1530
  const statusCode = response.statusCode;
1513
1531
  if ((getting || posting) && statusCode >= 200 && statusCode < 300) {
1514
- let source = checkEncoding(response, statusCode, response.headers['content-encoding']);
1532
+ let source = checkEncoding(request, response, statusCode, outStream, response.headers['content-encoding']);
1515
1533
  if (source) {
1516
1534
  source.once('finish', () => request.emit('end'));
1517
1535
  }
@@ -1747,6 +1765,9 @@ class Request extends module_1.default {
1747
1765
  const startTime = log ? process.hrtime() : 0;
1748
1766
  let retries = 0, redirects = 0, closed, timeout, outStream;
1749
1767
  const throwError = (err, outAbort) => {
1768
+ if (timeout) {
1769
+ clearTimeout(timeout);
1770
+ }
1750
1771
  if (!closed) {
1751
1772
  closed = true;
1752
1773
  if (outStream && (0, types_1.isString)(pipeTo)) {
@@ -1754,9 +1775,6 @@ class Request extends module_1.default {
1754
1775
  }
1755
1776
  reject(typeof err === 'string' ? new Error(err) : err);
1756
1777
  }
1757
- if (timeout) {
1758
- clearTimeout(timeout);
1759
- }
1760
1778
  if (outAbort) {
1761
1779
  this[kDownloading].delete(outAbort);
1762
1780
  }
@@ -1950,10 +1968,10 @@ class Request extends module_1.default {
1950
1968
  result = encoding && !pipeline ? '' : null;
1951
1969
  titleBgColor = 'bgBlue';
1952
1970
  }
1953
- resolve(result);
1954
1971
  if (log) {
1955
1972
  this.writeTimeProcess('HTTP' + httpVersion, request.statusMessage || url.toString(), startTime, { type: 1024, queue: !!this.host, titleBgColor, messageUnit, messageUnitMinWidth: 9, delayTime, bypassLog: true });
1956
1973
  }
1974
+ resolve(result);
1957
1975
  });
1958
1976
  host.success(httpVersion);
1959
1977
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/request",
3
- "version": "0.5.6",
3
+ "version": "0.5.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.5.6",
24
- "@e-mc/types": "0.5.6",
23
+ "@e-mc/module": "0.5.8",
24
+ "@e-mc/types": "0.5.8",
25
25
  "combined-stream": "^1.0.8",
26
26
  "js-yaml": "^4.1.0",
27
27
  "which": "^2.0.2"