@e-mc/request 0.11.11 → 0.11.13

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.11.11/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.11.13/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { IModule, ModuleConstructor } from "./index";
@@ -209,9 +209,9 @@ instance.get("http://hostname/path/config.yml", options).then(data => {
209
209
 
210
210
  ## References
211
211
 
212
- - https://www.unpkg.com/@e-mc/types@0.11.11/lib/http.d.ts
213
- - https://www.unpkg.com/@e-mc/types@0.11.11/lib/request.d.ts
214
- - https://www.unpkg.com/@e-mc/types@0.11.11/lib/settings.d.ts
212
+ - https://www.unpkg.com/@e-mc/types@0.11.13/lib/http.d.ts
213
+ - https://www.unpkg.com/@e-mc/types@0.11.13/lib/request.d.ts
214
+ - https://www.unpkg.com/@e-mc/types@0.11.13/lib/settings.d.ts
215
215
 
216
216
  * https://www.npmjs.com/package/@types/node
217
217
 
@@ -73,13 +73,21 @@ class HttpAdapter {
73
73
  return;
74
74
  }
75
75
  const statusCode = headers[':status'];
76
- if (statusCode < 300) {
77
- this.acceptResponse(headers);
76
+ if (statusCode === 204 || statusCode === 304) {
77
+ this.acceptResponse(headers, true);
78
78
  }
79
- else if (statusCode < 400) {
79
+ else if (statusCode < 300) {
80
+ this.acceptResponse(headers, false);
81
+ }
82
+ else if (statusCode === 301 ||
83
+ statusCode === 302 ||
84
+ statusCode === 303 ||
85
+ statusCode === 307 ||
86
+ statusCode === 308) {
80
87
  this.redirectResponse(statusCode, headers.location);
81
88
  }
82
- else if (statusCode === 401 ||
89
+ else if (statusCode === 305 ||
90
+ statusCode === 401 ||
83
91
  statusCode === 402 ||
84
92
  statusCode === 403 ||
85
93
  statusCode === 404 ||
@@ -145,10 +153,17 @@ class HttpAdapter {
145
153
  return;
146
154
  }
147
155
  const statusCode = res.statusCode;
148
- if (statusCode < 300) {
149
- this.acceptResponse(res.headers);
156
+ if (statusCode === 204 || statusCode === 304) {
157
+ this.acceptResponse(res.headers, true);
158
+ }
159
+ else if (statusCode < 300) {
160
+ this.acceptResponse(res.headers, false);
150
161
  }
151
- else if (statusCode < 400) {
162
+ else if (statusCode === 301 ||
163
+ statusCode === 302 ||
164
+ statusCode === 303 ||
165
+ statusCode === 307 ||
166
+ statusCode === 308) {
152
167
  this.redirectResponse(statusCode, res.headers.location);
153
168
  }
154
169
  else if (this.isRetry(statusCode)) {
@@ -220,7 +235,7 @@ class HttpAdapter {
220
235
  this.opts.httpVersion = 1;
221
236
  this.init();
222
237
  }
223
- acceptResponse(headers) {
238
+ acceptResponse(headers, empty = false) {
224
239
  const opts = this.opts;
225
240
  if ('outHeaders' in opts) {
226
241
  opts.outHeaders = headers;
@@ -232,7 +247,7 @@ class HttpAdapter {
232
247
  const enabled = opts.connected?.call(this.client, headers) !== false && !pipeline;
233
248
  const maxBufferSize = opts.maxBufferSize ? (0, types_1.alignSize)(opts.maxBufferSize) : 0;
234
249
  this.contentLength = parseInt(headers['content-length'] || '0');
235
- const updating = opts.progressId !== undefined && !!this.instance.host && this.contentLength > 0;
250
+ const updating = opts.progressId !== undefined && !!this.instance.host && this.contentLength > 0 && !empty;
236
251
  const readTimeout = this.instance.readTimeout;
237
252
  let log = this.state.log, buffer = null, dataLength = 0;
238
253
  this.client.once('readable', () => {
@@ -261,7 +276,7 @@ class HttpAdapter {
261
276
  this.updateProgress(0, 0);
262
277
  }
263
278
  });
264
- if (enabled) {
279
+ if (enabled && !empty) {
265
280
  const encoding = opts.encoding;
266
281
  this.client.on('data', (chunk) => {
267
282
  if (buffer) {
@@ -360,7 +375,7 @@ class HttpAdapter {
360
375
  if (updating) {
361
376
  this.updateProgress(0, this.contentLength);
362
377
  }
363
- if (enabled && this.instance.readExpect === 'always') {
378
+ if (enabled && this.instance.readExpect === 'always' && !empty) {
364
379
  this.terminate("No data received");
365
380
  return;
366
381
  }
@@ -396,6 +411,9 @@ class HttpAdapter {
396
411
  }
397
412
  else if (++this.redirects <= this.redirectLimit) {
398
413
  this.abortResponse();
414
+ if (this.opts.method?.toUpperCase() === 'PUT' && statusCode !== 307 && statusCode !== 308) {
415
+ this[kOptions].method = 'GET';
416
+ }
399
417
  this.setOpts((0, util_1.fromURL)(this.opts.url, location));
400
418
  this.init();
401
419
  }
package/index.js CHANGED
@@ -277,6 +277,8 @@ function checkEncoding(request, response, statusCode, outStream, contentEncoding
277
277
  case 206:
278
278
  request.emit('error', (0, types_1.errorValue)("Aborted", 'Partial content'));
279
279
  case 204:
280
+ case 205:
281
+ case 304:
280
282
  return;
281
283
  }
282
284
  if (!contentEncoding) {
@@ -379,6 +381,7 @@ function successDns(instance, value, pending, connected) {
379
381
  }
380
382
  pending.length = 0;
381
383
  }
384
+ const hasResponse = (value) => value >= 200 && (value < 300 || value === 304);
382
385
  const isFunction = (value) => typeof value === 'function';
383
386
  const configureDns = (family, options) => family === 0 ? options : { family, hints: family === 6 ? dns.V4MAPPED : 0 };
384
387
  const ignoreOpt = (opts, ...values) => !opts?.some(item => values.includes(item));
@@ -1481,7 +1484,7 @@ class Request extends module_1 {
1481
1484
  return;
1482
1485
  }
1483
1486
  connected = true;
1484
- if (this.matchStatus(statusCode, url, response, request, options) && statusCode >= 200 && statusCode < 300) {
1487
+ if (this.matchStatus(statusCode, url, response, request, options) && hasResponse(statusCode)) {
1485
1488
  if (emitter = checkEncoding(request, request, statusCode, outStream, response['content-encoding'])) {
1486
1489
  for (const event in listenerMap) {
1487
1490
  const [name, type] = event.split('-');
@@ -1601,7 +1604,7 @@ class Request extends module_1 {
1601
1604
  }, response => {
1602
1605
  const statusCode = response.statusCode;
1603
1606
  const incoming = response.headers;
1604
- if (!expectContinue && this.matchStatus(statusCode, url, incoming, request, options) && (getting || posting) && statusCode >= 200 && statusCode < 300) {
1607
+ if (!expectContinue && this.matchStatus(statusCode, url, incoming, request, options) && (getting || posting) && hasResponse(statusCode)) {
1605
1608
  let source = checkEncoding(request, response, statusCode, outStream, incoming['content-encoding']);
1606
1609
  if (source) {
1607
1610
  source.once('finish', () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/request",
3
- "version": "0.11.11",
3
+ "version": "0.11.13",
4
4
  "description": "Request constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -20,11 +20,11 @@
20
20
  "license": "BSD-3-Clause",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/module": "0.11.11",
24
- "@e-mc/types": "0.11.11",
23
+ "@e-mc/module": "0.11.13",
24
+ "@e-mc/types": "0.11.13",
25
25
  "combined-stream": "^1.0.8",
26
- "js-yaml": "^4.1.0",
27
- "picomatch": "^4.0.2",
26
+ "js-yaml": "^4.1.1",
27
+ "picomatch": "^4.0.3",
28
28
  "which": "^4.0.0"
29
29
  }
30
30
  }