superagent 4.0.0-alpha.1 → 4.0.0-beta.5

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/lib/node/index.js CHANGED
@@ -15,7 +15,6 @@ let methods = require('methods');
15
15
  const Stream = require('stream');
16
16
  const utils = require('../utils');
17
17
  const unzip = require('./unzip').unzip;
18
- const extend = require('extend');
19
18
  const mime = require('mime');
20
19
  const https = require('https');
21
20
  const http = require('http');
@@ -26,6 +25,12 @@ const util = require('util');
26
25
  const pkg = require('../../package.json');
27
26
  const RequestBase = require('../request-base');
28
27
  const CookieJar = require('cookiejar');
28
+ let http2;
29
+ try {
30
+ http2 = require('./http2wrapper');
31
+ console.warn('superagent: Enable experimental feature http2');
32
+ } catch (_) {
33
+ }
29
34
 
30
35
  function request(method, url) {
31
36
  // callback
@@ -81,6 +86,7 @@ mime.define({
81
86
  exports.protocols = {
82
87
  'http:': http,
83
88
  'https:': https,
89
+ 'http2:': http2,
84
90
  };
85
91
 
86
92
  /**
@@ -144,6 +150,7 @@ function _initHeaders(req) {
144
150
  function Request(method, url) {
145
151
  Stream.call(this);
146
152
  if ('string' != typeof url) url = format(url);
153
+ this._enableHttp2 = !!process.env.HTTP2_TEST; // internal only
147
154
  this._agent = false;
148
155
  this._formData = null;
149
156
  this.method = method;
@@ -168,6 +175,43 @@ function Request(method, url) {
168
175
  util.inherits(Request, Stream);
169
176
  RequestBase(Request.prototype);
170
177
 
178
+ /**
179
+ * Enable or Disable http2.
180
+ *
181
+ * Enable http2.
182
+ *
183
+ * ``` js
184
+ * request.get('http://localhost/')
185
+ * .http2()
186
+ * .end(callback);
187
+ *
188
+ * request.get('http://localhost/')
189
+ * .http2(true)
190
+ * .end(callback);
191
+ * ```
192
+ *
193
+ * Disable http2.
194
+ *
195
+ * ``` js
196
+ * request = request.http2();
197
+ * request.get('http://localhost/')
198
+ * .http2(false)
199
+ * .end(callback);
200
+ * ```
201
+ *
202
+ * @param {Boolean} enable
203
+ * @return {Request} for chaining
204
+ * @api public
205
+ */
206
+
207
+ Request.prototype.http2 = function(bool){
208
+ if (exports.protocols['http2:'] === undefined){
209
+ throw new Error('superagent: does not support http2');
210
+ }
211
+ this._enableHttp2 = bool === undefined ? true : bool;
212
+ return this;
213
+ }
214
+
171
215
  /**
172
216
  * Queue the given `file` as an attachment to the specified `field`,
173
217
  * with optional `options` (or filename).
@@ -319,7 +363,7 @@ Request.prototype.query = function(val){
319
363
  if ('string' == typeof val) {
320
364
  this._query.push(val);
321
365
  } else {
322
- extend(this.qs, val);
366
+ Object.assign(this.qs, val);
323
367
  }
324
368
  return this;
325
369
  };
@@ -504,7 +548,7 @@ Request.prototype.auth = function(user, pass, options){
504
548
  options = { type: 'basic' };
505
549
  }
506
550
 
507
- const encoder = string => new Buffer(string).toString('base64');
551
+ const encoder = string => new Buffer.from(string).toString('base64');
508
552
 
509
553
  return this._auth(user, pass, options, encoder);
510
554
  };
@@ -649,7 +693,7 @@ Request.prototype.request = function(){
649
693
  }
650
694
 
651
695
  // initiate request
652
- const mod = exports.protocols[url.protocol];
696
+ const mod = this._enableHttp2 ? exports.protocols['http2:'].setProtocol(url.protocol) : exports.protocols[url.protocol];
653
697
 
654
698
  // request
655
699
  const req = (this.req = mod.request(options));
@@ -695,10 +739,10 @@ Request.prototype.request = function(){
695
739
 
696
740
  // add cookies
697
741
  if (this.cookies) {
698
- if(this.header.hasOwnProperty('cookie')) {
742
+ if(this._header.hasOwnProperty('cookie')) {
699
743
  // merge
700
744
  const tmpJar = new CookieJar.CookieJar();
701
- tmpJar.setCookies(this.header.cookie.split(';'));
745
+ tmpJar.setCookies(this._header.cookie.split(';'));
702
746
  tmpJar.setCookies(this.cookies.split(';'));
703
747
  req.setHeader('Cookie',tmpJar.getCookies(CookieJar.CookieAccessInfo.All).toValueString());
704
748
  } else {
@@ -789,6 +833,11 @@ Request.prototype._emitResponse = function(body, files) {
789
833
  response.body = body;
790
834
  }
791
835
  response.files = files;
836
+ if (this._endCalled) {
837
+ response.pipe = function() {
838
+ throw Error("end() has already been called, so it's too late to start piping");
839
+ }
840
+ }
792
841
  this.emit('response', response);
793
842
  return response;
794
843
  };
@@ -798,8 +847,8 @@ Request.prototype.end = function(fn) {
798
847
  debug('%s %s', this.method, this.url);
799
848
 
800
849
  if (this._endCalled) {
801
- console.warn(
802
- 'Warning: .end() was called twice. This is not supported in superagent'
850
+ throw Error(
851
+ '.end() was called twice. This is not supported in superagent'
803
852
  );
804
853
  }
805
854
  this._endCalled = true;
@@ -807,7 +856,7 @@ Request.prototype.end = function(fn) {
807
856
  // store callback
808
857
  this._callback = fn || noop;
809
858
 
810
- return this._end();
859
+ this._end();
811
860
  };
812
861
 
813
862
  Request.prototype._end = function() {
@@ -815,7 +864,6 @@ Request.prototype._end = function() {
815
864
 
816
865
  let data = this._data;
817
866
  const req = this.req;
818
- let buffer = this._buffer;
819
867
  const method = this.method;
820
868
 
821
869
  this._setTimeouts();
@@ -827,7 +875,7 @@ Request.prototype._end = function() {
827
875
  let contentType = req.getHeader('Content-Type');
828
876
  // Parse out just the content type from the header (ignore the charset)
829
877
  if (contentType) contentType = contentType.split(';')[0];
830
- let serialize = exports.serialize[contentType];
878
+ let serialize = this._serializer || exports.serialize[contentType];
831
879
  if (!serialize && isJSON(contentType)) {
832
880
  serialize = exports.serialize['application/json'];
833
881
  }
@@ -857,7 +905,6 @@ Request.prototype._end = function() {
857
905
  const type = mime.split('/')[0];
858
906
  const multipart = 'multipart' == type;
859
907
  const redirect = isRedirect(res.statusCode);
860
- let parser = this._parser;
861
908
  const responseType = this._responseType;
862
909
 
863
910
  this.res = res;
@@ -878,10 +925,19 @@ Request.prototype._end = function() {
878
925
  unzip(req, res);
879
926
  }
880
927
 
928
+ let buffer = this._buffer;
881
929
  if (buffer === undefined && mime in exports.buffer){
882
930
  buffer = !!exports.buffer[mime];
883
931
  }
884
932
 
933
+ let parser = this._parser;
934
+ if (undefined === buffer) {
935
+ if (parser) {
936
+ console.warn("A custom superagent parser has been set, but buffering strategy for the parser hasn't been configured. Call `req.buffer(true or false)` or set `superagent.buffer[mime] = true or false`");
937
+ buffer = true;
938
+ }
939
+ }
940
+
885
941
  if (!parser) {
886
942
  if (responseType) {
887
943
  parser = exports.parse.image; // It's actually a generic Buffer
@@ -905,6 +961,9 @@ Request.prototype._end = function() {
905
961
  buffer = (buffer !== false);
906
962
  } else if (buffer) {
907
963
  parser = exports.parse.text;
964
+ } else if (undefined === buffer) {
965
+ parser = exports.parse.image; // It's actually a generic Buffer
966
+ buffer = true;
908
967
  }
909
968
  }
910
969
 
@@ -913,6 +972,7 @@ Request.prototype._end = function() {
913
972
  buffer = true;
914
973
  }
915
974
 
975
+ this._resBuffered = buffer;
916
976
  let parserHandlesEnd = false;
917
977
  if (buffer) {
918
978
  // Protectiona against zip bombs and other nuisance
@@ -1059,8 +1119,6 @@ Request.prototype._end = function() {
1059
1119
  } else {
1060
1120
  req.end(data);
1061
1121
  }
1062
-
1063
- return this;
1064
1122
  };
1065
1123
 
1066
1124
  /**
@@ -36,7 +36,7 @@ function Response(req) {
36
36
  this.text = res.text;
37
37
  this.body = res.body !== undefined ? res.body : {};
38
38
  this.files = res.files || {};
39
- this.buffered = 'string' == typeof this.text;
39
+ this.buffered = req._resBuffered;
40
40
  this.header = this.headers = res.headers;
41
41
  this._setStatusProperties(res.statusCode);
42
42
  this._setHeaderProperties(this.header);
package/lib/node/unzip.js CHANGED
@@ -59,9 +59,9 @@ exports.unzip = (req, res) => {
59
59
  const _on = res.on;
60
60
  res.on = function(type, fn) {
61
61
  if ('data' == type || 'end' == type) {
62
- stream.on(type, fn);
62
+ stream.on(type, fn.bind(res));
63
63
  } else if ('error' == type) {
64
- stream.on(type, fn);
64
+ stream.on(type, fn.bind(res));
65
65
  _on.call(res, type, fn);
66
66
  } else {
67
67
  _on.call(res, type, fn);
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "superagent",
3
- "version": "4.0.0-alpha.1",
3
+ "version": "4.0.0-beta.5",
4
4
  "description": "elegant & feature rich browser / node HTTP with a fluent API",
5
5
  "scripts": {
6
6
  "prepare": "make all",
7
- "test": "make test"
7
+ "test": "make test",
8
+ "test-http2": "make test-node-http2"
8
9
  },
9
10
  "keywords": [
10
11
  "http",
@@ -25,15 +26,14 @@
25
26
  },
26
27
  "dependencies": {
27
28
  "component-emitter": "^1.2.0",
28
- "cookiejar": "^2.1.0",
29
- "debug": "^3.1.0",
30
- "extend": "^3.0.0",
29
+ "cookiejar": "^2.1.2",
30
+ "debug": "^4.0.0",
31
31
  "form-data": "^2.3.2",
32
32
  "formidable": "^1.2.0",
33
33
  "methods": "^1.1.1",
34
34
  "mime": "^2.0.3",
35
35
  "qs": "^6.5.1",
36
- "readable-stream": "^2.3.5"
36
+ "readable-stream": "^3.0.3"
37
37
  },
38
38
  "devDependencies": {
39
39
  "Base64": "^1.0.1",
@@ -46,12 +46,12 @@
46
46
  "cookie-parser": "^1.4.3",
47
47
  "express": "^4.16.3",
48
48
  "express-session": "^1.15.6",
49
- "marked": "^0.3.19",
49
+ "marked": "^0.5.0",
50
50
  "mocha": "^3.5.3",
51
51
  "multer": "^1.3.0",
52
52
  "should": "^13.2.0",
53
53
  "should-http": "^0.1.1",
54
- "zuul": "^3.11.1"
54
+ "zuul": "^3.12.0"
55
55
  },
56
56
  "browser": {
57
57
  "./lib/node/index.js": "./lib/client.js",
package/superagent.js CHANGED
@@ -1799,7 +1799,7 @@ Request.prototype.end = function (fn) {
1799
1799
  // querystring
1800
1800
  this._finalizeQueryString();
1801
1801
 
1802
- return this._end();
1802
+ this._end();
1803
1803
  };
1804
1804
 
1805
1805
  Request.prototype._end = function () {
@@ -1901,7 +1901,6 @@ Request.prototype._end = function () {
1901
1901
  // IE11 xhr.send(undefined) sends 'undefined' string as POST payload (instead of nothing)
1902
1902
  // We need null here if data is undefined
1903
1903
  xhr.send(typeof data !== 'undefined' ? data : null);
1904
- return this;
1905
1904
  };
1906
1905
 
1907
1906
  request.agent = function () {