Comparing version 1.2.6 to 1.2.8
@@ -0,1 +1,5 @@ | ||
## 1.2.8 (December 5, 2020) | ||
* __Patch:__ Fixed broken error construction code (#5, thanks to @ThePendulum) | ||
## 1.2.6 (February 18, 2020) | ||
@@ -2,0 +6,0 @@ |
129
lib/bhttp.js
@@ -7,10 +7,18 @@ "use strict"; // FIXME: Force-lowercase user-supplied headers before merging them into the request? | ||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } | ||
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } | ||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | ||
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } | ||
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); } | ||
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
var urlUtil = require("url"); | ||
@@ -131,8 +139,8 @@ | ||
var match = false; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
var _iterator = _createForOfIteratorHelper(types), | ||
_step; | ||
try { | ||
for (var _iterator = types[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
for (_iterator.s(); !(_step = _iterator.n()).done;) { | ||
var type = _step.value; | ||
@@ -142,14 +150,5 @@ match = match || obj instanceof type; | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
_iterator.e(err); | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
_iterator.f(); | ||
} | ||
@@ -160,9 +159,2 @@ | ||
var addErrorData = function addErrorData(err, request, response, requestState) { | ||
err.request = request; | ||
err.response = response; | ||
err.requestState = requestState; | ||
return err; | ||
}; | ||
var isStream = function isStream(obj) { | ||
@@ -306,7 +298,18 @@ return obj != null && (ofTypes(obj, [stream.Readable, stream.Duplex, stream.Transform]) || obj.hasOwnProperty("_bhttpStreamWrapper")); | ||
if ((request.options.formFields != null || request.options.files != null) && (request.options.inputStream != null || request.options.inputBuffer != null)) { | ||
return Promise.reject(addErrorData(new bhttpErrors.ConflictingOptionsError("You cannot define both formFields/files and a raw inputStream or inputBuffer."), request, response, requestState)); | ||
return Promise.reject(new bhttpErrors.ConflictingOptionsError({ | ||
message: "You cannot define both formFields/files and a raw inputStream or inputBuffer.", | ||
request: request, | ||
response: response, | ||
requestState: requestState | ||
})); | ||
} | ||
if (request.options.encodeJSON && (request.options.inputStream != null || request.options.inputBuffer != null)) { | ||
return Promise.reject(addErrorData(new bhttpErrors.ConflictingOptionsError("You cannot use both encodeJSON and a raw inputStream or inputBuffer.", undefined, "If you meant to JSON-encode the stream, you will currently have to do so manually."), request, response, requestState)); | ||
var _bhttpErrors$Conflict; | ||
return Promise.reject(new bhttpErrors.ConflictingOptionsError((_bhttpErrors$Conflict = { | ||
message: "You cannot use both encodeJSON and a raw inputStream or inputBuffer.", | ||
response: "If you meant to JSON-encode the stream, you will currently have to do so manually.", | ||
request: request | ||
}, _defineProperty(_bhttpErrors$Conflict, "response", response), _defineProperty(_bhttpErrors$Conflict, "requestState", requestState), _bhttpErrors$Conflict))); | ||
} // If the user plans on streaming the response, we need to disable the agent entirely - otherwise the streams will block the pool. | ||
@@ -380,8 +383,7 @@ | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
var _iterator2 = _createForOfIteratorHelper(fieldValue), | ||
_step2; | ||
try { | ||
for (var _iterator2 = fieldValue[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { | ||
var valueElement = _step2.value; | ||
@@ -400,14 +402,5 @@ var streamOptions; | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
_iterator2.e(err); | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return != null) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
_iterator2.f(); | ||
} | ||
@@ -421,3 +414,8 @@ } | ||
if (headers["content-transfer-encoding"] === "chunked" && !request.options.allowChunkedMultipart) { | ||
return Promise.reject(addErrorData(new bhttpErrors.MultipartError("Most servers do not support chunked transfer encoding for multipart/form-data payloads, and we could not determine the length of all the input streams. See the documentation for more information."), request, response, requestState)); | ||
return Promise.reject(new bhttpErrors.MultipartError({ | ||
message: "Most servers do not support chunked transfer encoding for multipart/form-data payloads, and we could not determine the length of all the input streams. See the documentation for more information.", | ||
request: request, | ||
response: response, | ||
requestState: requestState | ||
})); | ||
} else { | ||
@@ -528,3 +526,3 @@ assign(request.options.headers, headers); | ||
req.abort(); | ||
return reject(addErrorData(new bhttpErrors.ResponseTimeoutError("The response timed out."))); | ||
return reject(new bhttpErrors.ResponseTimeoutError("The response timed out.")); | ||
}; | ||
@@ -576,3 +574,3 @@ | ||
debugRequest("a connection timeout occurred!"); | ||
return reject(addErrorData(new bhttpErrors.ConnectionTimeoutError("The connection timed out."))); | ||
return reject(new bhttpErrors.ConnectionTimeoutError("The connection timed out.")); | ||
} else { | ||
@@ -604,8 +602,8 @@ return reject(err); | ||
var result = []; | ||
var _iteratorNormalCompletion3 = true; | ||
var _didIteratorError3 = false; | ||
var _iteratorError3 = undefined; | ||
var _iterator3 = _createForOfIteratorHelper(response.headers["set-cookie"]), | ||
_step3; | ||
try { | ||
for (var _iterator3 = response.headers["set-cookie"][Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { | ||
var cookieHeader = _step3.value; | ||
@@ -616,14 +614,5 @@ debugResponse("storing cookie: %s", cookieHeader); | ||
} catch (err) { | ||
_didIteratorError3 = true; | ||
_iteratorError3 = err; | ||
_iterator3.e(err); | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return != null) { | ||
_iterator3.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError3) { | ||
throw _iteratorError3; | ||
} | ||
} | ||
_iterator3.f(); | ||
} | ||
@@ -646,3 +635,3 @@ | ||
if (requestState.redirectHistory.length >= request.responseOptions.redirectLimit - 1) { | ||
return Promise.reject(addErrorData(new bhttpErrors.RedirectError("The maximum amount of redirects ({request.responseOptions.redirectLimit}) was reached."))); | ||
return Promise.reject(new bhttpErrors.RedirectError("The maximum amount of redirects ({request.responseOptions.redirectLimit}) was reached.")); | ||
} // 301: For GET and HEAD, redirect unchanged. For POST, PUT, PATCH, DELETE, "ask user" (in our case: throw an error.) | ||
@@ -665,6 +654,11 @@ // 302: Redirect, change method to GET. | ||
case "delete": | ||
return Promise.reject(addErrorData(new bhttpErrors.RedirectError("Encountered a 301 redirect for POST, PUT, PATCH or DELETE. RFC says we can't automatically continue."), request, response, requestState)); | ||
return Promise.reject(new bhttpErrors.RedirectError({ | ||
message: "Encountered a 301 redirect for POST, PUT, PATCH or DELETE. RFC says we can't automatically continue.", | ||
request: request, | ||
response: response, | ||
requestState: requestState | ||
})); | ||
default: | ||
return Promise.reject(addErrorData(new bhttpErrors.RedirectError("Encountered a 301 redirect, but not sure how to proceed for the ".concat(request.options.method.toUpperCase(), " method.")))); | ||
return Promise.reject(new bhttpErrors.RedirectError("Encountered a 301 redirect, but not sure how to proceed for the ".concat(request.options.method.toUpperCase(), " method."))); | ||
} | ||
@@ -678,3 +672,8 @@ | ||
if (request.containsStreams && !["get", "head"].includes(request.options.method)) { | ||
return Promise.reject(addErrorData(new bhttpErrors.RedirectError("Encountered a 307 redirect for POST, PUT or DELETE, but your payload contained (single-use) streams. We therefore can't automatically follow the redirect."), request, response, requestState)); | ||
return Promise.reject(new bhttpErrors.RedirectError({ | ||
message: "Encountered a 307 redirect for POST, PUT or DELETE, but your payload contained (single-use) streams. We therefore can't automatically follow the redirect.", | ||
request: request, | ||
response: response, | ||
requestState: requestState | ||
})); | ||
} else { | ||
@@ -681,0 +680,0 @@ return redirectUnchanged(request, response, requestState); |
{ | ||
"name": "bhttp", | ||
"version": "1.2.6", | ||
"version": "1.2.8", | ||
"description": "A sane HTTP client library for Node.js with Streams2 support.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -46,5 +46,5 @@ # bhttp | ||
Pull requests welcome. Please make sure your modifications are in line with the overall code style, and ensure that you're editing the `.coffee` files, not the `.js` files. | ||
Pull requests welcome. Please make sure your modifications are in line with the overall code style, and ensure that you're editing the `src/` files, not the `lib/` files. | ||
Build tool of choice is `gulp`; simply run `gulp` while developing, and it will watch for changes. | ||
Build tool of choice is `babel`; simply run `npm run build` while developing, and it will watch for changes. | ||
@@ -51,0 +51,0 @@ Be aware that by making a pull request, you agree to release your modifications under the licenses stated above. |
@@ -117,9 +117,2 @@ "use strict"; | ||
const addErrorData = function(err, request, response, requestState) { | ||
err.request = request; | ||
err.response = response; | ||
err.requestState = requestState; | ||
return err; | ||
}; | ||
const isStream = obj => (obj != null) && (ofTypes(obj, [stream.Readable, stream.Duplex, stream.Transform]) || obj.hasOwnProperty("_bhttpStreamWrapper")); | ||
@@ -233,7 +226,13 @@ | ||
if (((request.options.formFields != null) || (request.options.files != null)) && ((request.options.inputStream != null) || (request.options.inputBuffer != null))) { | ||
return Promise.reject(addErrorData(new bhttpErrors.ConflictingOptionsError("You cannot define both formFields/files and a raw inputStream or inputBuffer."), request, response, requestState)); | ||
return Promise.reject(new bhttpErrors.ConflictingOptionsError({ message: "You cannot define both formFields/files and a raw inputStream or inputBuffer.", request, response, requestState })); | ||
} | ||
if (request.options.encodeJSON && ((request.options.inputStream != null) || (request.options.inputBuffer != null))) { | ||
return Promise.reject(addErrorData(new bhttpErrors.ConflictingOptionsError("You cannot use both encodeJSON and a raw inputStream or inputBuffer.", undefined, "If you meant to JSON-encode the stream, you will currently have to do so manually."), request, response, requestState)); | ||
return Promise.reject(new bhttpErrors.ConflictingOptionsError({ | ||
message: "You cannot use both encodeJSON and a raw inputStream or inputBuffer.", | ||
response: "If you meant to JSON-encode the stream, you will currently have to do so manually.", | ||
request, | ||
response, | ||
requestState, | ||
})); | ||
} | ||
@@ -321,3 +320,3 @@ | ||
if ((headers["content-transfer-encoding"] === "chunked") && !request.options.allowChunkedMultipart) { | ||
return Promise.reject(addErrorData(new bhttpErrors.MultipartError("Most servers do not support chunked transfer encoding for multipart/form-data payloads, and we could not determine the length of all the input streams. See the documentation for more information."), request, response, requestState)); | ||
return Promise.reject(new bhttpErrors.MultipartError({ message: "Most servers do not support chunked transfer encoding for multipart/form-data payloads, and we could not determine the length of all the input streams. See the documentation for more information.", request, response, requestState })); | ||
} else { | ||
@@ -431,3 +430,3 @@ assign(request.options.headers, headers); | ||
req.abort(); | ||
return reject(addErrorData(new bhttpErrors.ResponseTimeoutError("The response timed out."))); | ||
return reject(new bhttpErrors.ResponseTimeoutError("The response timed out.")); | ||
}; | ||
@@ -481,3 +480,3 @@ | ||
debugRequest("a connection timeout occurred!"); | ||
return reject(addErrorData(new bhttpErrors.ConnectionTimeoutError("The connection timed out."))); | ||
return reject(new bhttpErrors.ConnectionTimeoutError("The connection timed out.")); | ||
} else { | ||
@@ -525,3 +524,3 @@ return reject(err); | ||
if (requestState.redirectHistory.length >= (request.responseOptions.redirectLimit - 1)) { | ||
return Promise.reject(addErrorData(new bhttpErrors.RedirectError("The maximum amount of redirects ({request.responseOptions.redirectLimit}) was reached."))); | ||
return Promise.reject(new bhttpErrors.RedirectError("The maximum amount of redirects ({request.responseOptions.redirectLimit}) was reached.")); | ||
} | ||
@@ -539,5 +538,5 @@ | ||
case "post": case "put": case "patch": case "delete": | ||
return Promise.reject(addErrorData(new bhttpErrors.RedirectError("Encountered a 301 redirect for POST, PUT, PATCH or DELETE. RFC says we can't automatically continue."), request, response, requestState)); | ||
return Promise.reject(new bhttpErrors.RedirectError({ message: "Encountered a 301 redirect for POST, PUT, PATCH or DELETE. RFC says we can't automatically continue.", request, response, requestState })); | ||
default: | ||
return Promise.reject(addErrorData(new bhttpErrors.RedirectError(`Encountered a 301 redirect, but not sure how to proceed for the ${request.options.method.toUpperCase()} method.`))); | ||
return Promise.reject(new bhttpErrors.RedirectError(`Encountered a 301 redirect, but not sure how to proceed for the ${request.options.method.toUpperCase()} method.`)); | ||
} | ||
@@ -548,3 +547,3 @@ case 302: case 303: | ||
if (request.containsStreams && !["get", "head"].includes(request.options.method)) { | ||
return Promise.reject(addErrorData(new bhttpErrors.RedirectError("Encountered a 307 redirect for POST, PUT or DELETE, but your payload contained (single-use) streams. We therefore can't automatically follow the redirect."), request, response, requestState)); | ||
return Promise.reject(new bhttpErrors.RedirectError({ message: "Encountered a 307 redirect for POST, PUT or DELETE, but your payload contained (single-use) streams. We therefore can't automatically follow the redirect.", request, response, requestState })); | ||
} else { | ||
@@ -551,0 +550,0 @@ return redirectUnchanged(request, response, requestState); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3168089
1538