@e-mc/request 0.9.23 → 0.9.25

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.23/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.9.24/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { IModule, ModuleConstructor } from "./index";
@@ -202,9 +202,9 @@ instance.get("http://hostname/path/config.yml", options).then(data => {
202
202
 
203
203
  ## References
204
204
 
205
- - https://www.unpkg.com/@e-mc/types@0.9.23/lib/http.d.ts
206
- - https://www.unpkg.com/@e-mc/types@0.9.23/lib/request.d.ts
207
- - https://www.unpkg.com/@e-mc/types@0.9.23/lib/settings.d.ts
205
+ - https://www.unpkg.com/@e-mc/types@0.9.24/lib/http.d.ts
206
+ - https://www.unpkg.com/@e-mc/types@0.9.24/lib/request.d.ts
207
+ - https://www.unpkg.com/@e-mc/types@0.9.24/lib/settings.d.ts
208
208
 
209
209
  * https://www.npmjs.com/package/@types/node
210
210
 
@@ -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
  }
@@ -402,6 +417,9 @@ class HttpAdapter {
402
417
  }
403
418
  else if (++this.redirects <= this.redirectLimit) {
404
419
  this.abortResponse();
420
+ if (this.opts.method?.toUpperCase() === 'PUT' && statusCode !== 307 && statusCode !== 308) {
421
+ this.#options.method = 'GET';
422
+ }
405
423
  this.setOpts((0, util_1.fromURL)(this.opts.url, location));
406
424
  this.init();
407
425
  }
package/index.js CHANGED
@@ -272,6 +272,8 @@ function checkEncoding(request, response, statusCode, outStream, contentEncoding
272
272
  case 206:
273
273
  request.emit('error', (0, types_1.errorValue)("Aborted", 'Partial content'));
274
274
  case 204:
275
+ case 205:
276
+ case 304:
275
277
  return;
276
278
  default:
277
279
  contentEncoding = contentEncoding.trim();
@@ -322,6 +324,7 @@ function decompressEncoding(value, chunkSize) {
322
324
  break;
323
325
  }
324
326
  }
327
+ const hasResponse = (value) => value >= 200 && (value < 300 || value === 304);
325
328
  class Request extends module_1 {
326
329
  static async purgeMemory(percent = 1, limit = 0, parent) {
327
330
  if (percent >= 1) {
@@ -1518,7 +1521,7 @@ class Request extends module_1 {
1518
1521
  request.on('response', response => {
1519
1522
  connected = true;
1520
1523
  const statusCode = response[':status'];
1521
- if (this.matchStatus(statusCode, url, response, request, options) && statusCode >= 200 && statusCode < 300) {
1524
+ if (this.matchStatus(statusCode, url, response, request, options) && hasResponse(statusCode)) {
1522
1525
  emitter = checkEncoding(request, request, statusCode, outStream, response['content-encoding']);
1523
1526
  if (emitter) {
1524
1527
  for (const event in listenerMap) {
@@ -1637,7 +1640,7 @@ class Request extends module_1 {
1637
1640
  }, response => {
1638
1641
  const statusCode = response.statusCode;
1639
1642
  const incoming = response.headers;
1640
- if (this.matchStatus(statusCode, url, incoming, request, options) && (getting || posting) && statusCode >= 200 && statusCode < 300) {
1643
+ if (this.matchStatus(statusCode, url, incoming, request, options) && (getting || posting) && hasResponse(statusCode)) {
1641
1644
  let source = checkEncoding(request, response, statusCode, outStream, incoming['content-encoding']);
1642
1645
  if (source) {
1643
1646
  source.once('finish', () => request.emit('end'));
@@ -1961,7 +1964,7 @@ class Request extends module_1 {
1961
1964
  sendWarning(formatRetry('Connection timeout'));
1962
1965
  downloadUri.call(this, href);
1963
1966
  };
1964
- const acceptResponse = (headers) => {
1967
+ const acceptResponse = (headers, empty) => {
1965
1968
  if ('outHeaders' in opts) {
1966
1969
  opts.outHeaders = headers;
1967
1970
  }
@@ -2002,7 +2005,7 @@ class Request extends module_1 {
2002
2005
  }
2003
2006
  });
2004
2007
  }
2005
- if (enabled) {
2008
+ if (enabled && !empty) {
2006
2009
  client.on('data', (chunk) => {
2007
2010
  if (buffer) {
2008
2011
  if (typeof buffer === 'string') {
@@ -2103,7 +2106,7 @@ class Request extends module_1 {
2103
2106
  if (contentLength > 0) {
2104
2107
  parent.updateProgress("request", progressId, 0, contentLength);
2105
2108
  }
2106
- if (enabled && this.readExpect === 'always') {
2109
+ if (enabled && this.readExpect === 'always' && !empty) {
2107
2110
  throwError('No data received');
2108
2111
  return;
2109
2112
  }
@@ -2124,6 +2127,9 @@ class Request extends module_1 {
2124
2127
  }
2125
2128
  else if (++redirects <= this._config.redirectLimit) {
2126
2129
  abortResponse();
2130
+ if (request.method?.toUpperCase() === 'PUT' && statusCode !== 307 && statusCode !== 308) {
2131
+ opts.method = 'GET';
2132
+ }
2127
2133
  downloadUri.call(this, Request.fromURL(url, location));
2128
2134
  }
2129
2135
  else {
@@ -2204,13 +2210,21 @@ class Request extends module_1 {
2204
2210
  return;
2205
2211
  }
2206
2212
  const statusCode = headers[':status'];
2207
- if (statusCode < 300) {
2208
- acceptResponse(headers);
2213
+ if (statusCode === 204 || statusCode === 304) {
2214
+ acceptResponse(headers, true);
2215
+ }
2216
+ else if (statusCode < 300) {
2217
+ acceptResponse(headers, false);
2209
2218
  }
2210
- else if (statusCode < 400) {
2219
+ else if (statusCode === 301 ||
2220
+ statusCode === 302 ||
2221
+ statusCode === 303 ||
2222
+ statusCode === 307 ||
2223
+ statusCode === 308) {
2211
2224
  redirectResponse(statusCode, headers.location);
2212
2225
  }
2213
- else if (statusCode === 401 ||
2226
+ else if (statusCode === 305 ||
2227
+ statusCode === 401 ||
2214
2228
  statusCode === 402 ||
2215
2229
  statusCode === 403 ||
2216
2230
  statusCode === 404 ||
@@ -2275,10 +2289,17 @@ class Request extends module_1 {
2275
2289
  return;
2276
2290
  }
2277
2291
  const statusCode = res.statusCode;
2278
- if (statusCode < 300) {
2279
- acceptResponse(res.headers);
2292
+ if (statusCode === 204 || statusCode === 304) {
2293
+ acceptResponse(res.headers, true);
2294
+ }
2295
+ else if (statusCode < 300) {
2296
+ acceptResponse(res.headers, false);
2280
2297
  }
2281
- else if (statusCode < 400) {
2298
+ else if (statusCode === 301 ||
2299
+ statusCode === 302 ||
2300
+ statusCode === 303 ||
2301
+ statusCode === 307 ||
2302
+ statusCode === 308) {
2282
2303
  redirectResponse(statusCode, res.headers.location);
2283
2304
  }
2284
2305
  else if (isRetry(statusCode)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/request",
3
- "version": "0.9.23",
3
+ "version": "0.9.25",
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": "MIT",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/module": "0.9.23",
24
- "@e-mc/types": "0.9.23",
23
+ "@e-mc/module": "0.9.25",
24
+ "@e-mc/types": "0.9.25",
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": "^2.0.2"
29
29
  }
30
30
  }