follow-redirects 1.5.4 → 1.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.

Potentially problematic release.


This version of follow-redirects might be problematic. Click here for more details.

Files changed (2) hide show
  1. package/index.js +27 -10
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -58,9 +58,15 @@ RedirectableRequest.prototype = Object.create(Writable.prototype);
58
58
 
59
59
  // Writes buffered data to the current native request
60
60
  RedirectableRequest.prototype.write = function (data, encoding, callback) {
61
+ // Validate input and shift parameters if necessary
61
62
  if (!(typeof data === "string" || typeof data === "object" && ("length" in data))) {
62
63
  throw new Error("data should be a string, Buffer or Uint8Array");
63
64
  }
65
+ if (typeof encoding === "function") {
66
+ callback = encoding;
67
+ encoding = null;
68
+ }
69
+
64
70
  // Ignore empty buffers, since writing them doesn't invoke the callback
65
71
  // https://github.com/nodejs/node/issues/22066
66
72
  if (data.length === 0) {
@@ -84,15 +90,21 @@ RedirectableRequest.prototype.write = function (data, encoding, callback) {
84
90
 
85
91
  // Ends the current native request
86
92
  RedirectableRequest.prototype.end = function (data, encoding, callback) {
87
- var currentRequest = this._currentRequest;
88
- if (!data) {
89
- currentRequest.end(null, null, callback);
93
+ // Shift parameters if necessary
94
+ if (typeof data === "function") {
95
+ callback = data;
96
+ data = encoding = null;
90
97
  }
91
- else {
92
- this.write(data, encoding, function () {
93
- currentRequest.end(null, null, callback);
94
- });
98
+ else if (typeof encoding === "function") {
99
+ callback = encoding;
100
+ encoding = null;
95
101
  }
102
+
103
+ // Write data and end
104
+ var currentRequest = this._currentRequest;
105
+ this.write(data || "", encoding, function () {
106
+ currentRequest.end(null, null, callback);
107
+ });
96
108
  };
97
109
 
98
110
  // Sets a header value on the current native request
@@ -129,6 +141,10 @@ RedirectableRequest.prototype._performRequest = function () {
129
141
  // Load the native protocol
130
142
  var protocol = this._options.protocol;
131
143
  var nativeProtocol = this._options.nativeProtocols[protocol];
144
+ if (!nativeProtocol) {
145
+ this.emit("error", new Error("Unsupported protocol " + protocol));
146
+ return;
147
+ }
132
148
 
133
149
  // If specified, use the agent corresponding to the protocol
134
150
  // (HTTP and HTTPS use different types of agents)
@@ -155,10 +171,11 @@ RedirectableRequest.prototype._performRequest = function () {
155
171
  // (The first request must be ended explicitly with RedirectableRequest#end)
156
172
  if (this._isRedirect) {
157
173
  // Write the request entity and end.
158
- var requestBodyBuffers = this._requestBodyBuffers;
174
+ var i = 0;
175
+ var buffers = this._requestBodyBuffers;
159
176
  (function writeNext() {
160
- if (requestBodyBuffers.length !== 0) {
161
- var buffer = requestBodyBuffers.shift();
177
+ if (i < buffers.length) {
178
+ var buffer = buffers[i++];
162
179
  request.write(buffer.data, buffer.encoding, writeNext);
163
180
  }
164
181
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "follow-redirects",
3
- "version": "1.5.4",
3
+ "version": "1.5.8",
4
4
  "description": "HTTP and HTTPS modules that follow redirects.",
5
5
  "main": "index.js",
6
6
  "engines": {
@@ -44,7 +44,7 @@
44
44
  "https.js"
45
45
  ],
46
46
  "dependencies": {
47
- "debug": "^3.1.0"
47
+ "debug": "=3.1.0"
48
48
  },
49
49
  "devDependencies": {
50
50
  "bluebird": "^3.5.1",