@e-mc/request 0.9.2 → 0.9.4

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.9.2/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.9.4/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { IModule, ModuleConstructor } from "./index";
@@ -201,9 +201,9 @@ instance.get("http://hostname/path/config.yml", options).then(data => {
201
201
 
202
202
  ## References
203
203
 
204
- - https://www.unpkg.com/@e-mc/types@0.9.2/lib/http.d.ts
205
- - https://www.unpkg.com/@e-mc/types@0.9.2/lib/request.d.ts
206
- - https://www.unpkg.com/@e-mc/types@0.9.2/lib/settings.d.ts
204
+ - https://www.unpkg.com/@e-mc/types@0.9.4/lib/http.d.ts
205
+ - https://www.unpkg.com/@e-mc/types@0.9.4/lib/request.d.ts
206
+ - https://www.unpkg.com/@e-mc/types@0.9.4/lib/settings.d.ts
207
207
 
208
208
  * https://www.npmjs.com/package/@types/node
209
209
 
@@ -100,28 +100,28 @@ class HttpHost {
100
100
  return this._tlsConnect || (this._tlsConnect = new Promise(resolve => {
101
101
  const alpn = 'h' + version;
102
102
  const socket = tls.connect(+this.port, this.hostname, { ALPNProtocols: [alpn], requestCert: true, rejectUnauthorized: false }, () => {
103
- resolve(data[3] = alpn === socket.alpnProtocol ? 1 : 0);
104
103
  this._tlsConnect = null;
104
+ resolve(data[3] = alpn === socket.alpnProtocol ? 1 : 0);
105
105
  });
106
106
  socket
107
107
  .setNoDelay(false)
108
108
  .setTimeout(10000)
109
109
  .on('timeout', () => {
110
+ socket.destroy();
110
111
  if (this._tlsConnect) {
112
+ this._tlsConnect = null;
111
113
  if (this.error(version) >= 10) {
112
114
  resolve(data[3] = 0);
113
115
  }
114
116
  else {
115
117
  resolve(2);
116
118
  }
117
- this._tlsConnect = null;
118
119
  }
119
- socket.destroy();
120
120
  })
121
121
  .on('error', () => {
122
122
  this.failed(version);
123
- resolve(data[3] = 0);
124
123
  this._tlsConnect = null;
124
+ resolve(data[3] = 0);
125
125
  })
126
126
  .end();
127
127
  }));
package/index.js CHANGED
@@ -267,6 +267,61 @@ function abortHeaders(href, request, options) {
267
267
  }
268
268
  request.destroy(reason);
269
269
  }
270
+ function checkEncoding(request, response, statusCode, outStream, contentEncoding = '') {
271
+ switch (statusCode) {
272
+ case 206:
273
+ request.emit('error', (0, types_1.errorValue)("Aborted", 'Partial content'));
274
+ case 204:
275
+ return;
276
+ default:
277
+ contentEncoding = contentEncoding.trim();
278
+ if (!contentEncoding) {
279
+ return;
280
+ }
281
+ contentEncoding = contentEncoding.toLowerCase();
282
+ break;
283
+ }
284
+ const chunkSize = outStream?.writableHighWaterMark;
285
+ let pipeTo;
286
+ if (contentEncoding.indexOf(',') === -1) {
287
+ pipeTo = decompressEncoding(contentEncoding, chunkSize);
288
+ }
289
+ else {
290
+ for (const value of contentEncoding.split(/\s*,\s*/).reverse()) {
291
+ const next = decompressEncoding(value, chunkSize);
292
+ if (!next) {
293
+ return;
294
+ }
295
+ pipeTo = pipeTo ? pipeTo.pipe(next) : next;
296
+ }
297
+ }
298
+ if (pipeTo) {
299
+ if (outStream) {
300
+ stream.pipeline(response, pipeTo, outStream, err => err && response.emit('error', err));
301
+ }
302
+ else {
303
+ stream.pipeline(response, pipeTo, err => err && response.emit('error', err));
304
+ }
305
+ return pipeTo;
306
+ }
307
+ }
308
+ function decompressEncoding(value, chunkSize) {
309
+ switch (value) {
310
+ case 'gzip':
311
+ return zlib.createGunzip({ chunkSize });
312
+ case 'br':
313
+ return zlib.createBrotliDecompress({ chunkSize });
314
+ case 'deflate':
315
+ return zlib.createInflate({ chunkSize });
316
+ case 'deflate-raw':
317
+ return zlib.createInflateRaw({ chunkSize });
318
+ case 'zstd':
319
+ if (LIB_ZSTD) {
320
+ return new LIB_ZSTD.ZstdDecompressTransform({ writableHighWaterMark: chunkSize });
321
+ }
322
+ break;
323
+ }
324
+ }
270
325
  class Request extends module_1 {
271
326
  static async purgeMemory(percent = 1, limit = 0, parent) {
272
327
  if (percent >= 1) {
@@ -1419,44 +1474,6 @@ class Request extends module_1 {
1419
1474
  }
1420
1475
  uri = url.href;
1421
1476
  }
1422
- const checkEncoding = (response, statusCode, contentEncoding = '') => {
1423
- switch (statusCode) {
1424
- case 206:
1425
- request.emit('error', (0, types_1.errorValue)("Aborted", 'Partial content'));
1426
- case 204:
1427
- return;
1428
- }
1429
- const chunkSize = outStream?.writableHighWaterMark;
1430
- let pipeTo;
1431
- switch (contentEncoding.trim().toLowerCase()) {
1432
- case 'gzip':
1433
- pipeTo = zlib.createGunzip({ chunkSize });
1434
- break;
1435
- case 'br':
1436
- pipeTo = zlib.createBrotliDecompress({ chunkSize });
1437
- break;
1438
- case 'deflate':
1439
- pipeTo = zlib.createInflate({ chunkSize });
1440
- break;
1441
- case 'deflate-raw':
1442
- pipeTo = zlib.createInflateRaw({ chunkSize });
1443
- break;
1444
- case 'zstd':
1445
- if (LIB_ZSTD) {
1446
- pipeTo = new LIB_ZSTD.ZstdDecompressTransform({ writableHighWaterMark: chunkSize });
1447
- }
1448
- break;
1449
- }
1450
- if (pipeTo) {
1451
- if (outStream) {
1452
- stream.pipeline(response, pipeTo, outStream, err => err && response.emit('error', err));
1453
- }
1454
- else {
1455
- stream.pipeline(response, pipeTo, err => err && response.emit('error', err));
1456
- }
1457
- return pipeTo;
1458
- }
1459
- };
1460
1477
  const { hostname, origin, secure, localhost } = host;
1461
1478
  const pathname = url.pathname + (socketPath ? '' : url.search);
1462
1479
  const proxy = this.proxyOf(uri, localhost);
@@ -1482,7 +1499,8 @@ class Request extends module_1 {
1482
1499
  connected = true;
1483
1500
  const statusCode = response[':status'];
1484
1501
  if (this.matchStatus(statusCode, url, response, request, options) && statusCode >= 200 && statusCode < 300) {
1485
- if (emitter = checkEncoding(request, statusCode, response['content-encoding'])) {
1502
+ emitter = checkEncoding(request, request, statusCode, outStream, response['content-encoding']);
1503
+ if (emitter) {
1486
1504
  for (const event in listenerMap) {
1487
1505
  listenerMap[event].forEach(listener => {
1488
1506
  const [name, type] = event.split('-');
@@ -1603,7 +1621,7 @@ class Request extends module_1 {
1603
1621
  const statusCode = response.statusCode;
1604
1622
  const incoming = response.headers;
1605
1623
  if (this.matchStatus(statusCode, url, incoming, request, options) && (getting || posting) && statusCode >= 200 && statusCode < 300) {
1606
- let source = checkEncoding(response, statusCode, incoming['content-encoding']);
1624
+ let source = checkEncoding(request, response, statusCode, outStream, incoming['content-encoding']);
1607
1625
  if (source) {
1608
1626
  source.once('finish', () => request.emit('end'));
1609
1627
  }
@@ -2060,10 +2078,10 @@ class Request extends module_1 {
2060
2078
  result = encoding && !pipeline ? '' : null;
2061
2079
  titleBgColor = 'bgBlue';
2062
2080
  }
2063
- resolve(result);
2064
2081
  if (log) {
2065
2082
  this.writeTimeProcess('HTTP' + httpVersion, request.statusMessage || url.toString(), startTime, { type: 1024, queue: !!this.host, titleBgColor, messageUnit, messageUnitMinWidth: 9, delayTime, bypassLog: LOG_STDOUT });
2066
2083
  }
2084
+ resolve(result);
2067
2085
  });
2068
2086
  host.success(httpVersion);
2069
2087
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/request",
3
- "version": "0.9.2",
3
+ "version": "0.9.4",
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": "BSD 3-Clause",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/module": "0.9.2",
24
- "@e-mc/types": "0.9.2",
23
+ "@e-mc/module": "0.9.4",
24
+ "@e-mc/types": "0.9.4",
25
25
  "combined-stream": "^1.0.8",
26
26
  "js-yaml": "^4.1.0",
27
27
  "picomatch": "^4.0.2",