Browse Source

Fix assigning properties to non-writable error object. Updated PR section in README.md.

pull/4/head
Niels Simenon 1 year ago
parent
commit
066c1f2213
  1. 4
      README.md
  2. 86
      lib/bhttp.js
  3. 10
      src/bhttp.js

4
README.md

@ -43,9 +43,9 @@ You can donate using Bitcoin, PayPal, Gratipay, Flattr, cash-in-mail, SEPA trans
## Contributing
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.
Simply run `npm run build` while developing, and it will watch for changes.
Be aware that by making a pull request, you agree to release your modifications under the licenses stated above.

86
lib/bhttp.js

@ -4,13 +4,19 @@
// FIXME: Are arrays of streams in `data` correctly recognized as being streams?
// Core modules
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
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 _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
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 _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 _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");
@ -127,38 +133,31 @@ function assign() {
var ofTypes = function ofTypes(obj, types) {
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;
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();
}
return match;
};
var addErrorData = function addErrorData(err, request, response, requestState) {
err.request = request;
err.response = response;
err.requestState = requestState;
return err;
// 'errors' lib uses Object.defineProperty, non-writable by default
return Object.assign({}, err, {
request: request,
response: response,
requestState: requestState
});
};
var isStream = function isStream(obj) {
@ -372,12 +371,11 @@ var preparePayload = function preparePayload(request, response, requestState) {
fieldValue = [fieldValue];
}
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;
var streamOptions;
@ -391,18 +389,9 @@ var preparePayload = function preparePayload(request, response, requestState) {
formDataObject.append(fieldName, valueElement, 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();
}
}
@ -591,29 +580,20 @@ var processResponse = function processResponse(request, response, requestState)
if (request.cookieJar != null && response.headers["set-cookie"] != null) {
var promises = function () {
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;
debugResponse("storing cookie: %s", cookieHeader);
result.push(request.cookieJar.set(cookieHeader, request.url));
}
} 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();
}
return result;

10
src/bhttp.js

@ -115,10 +115,12 @@ const ofTypes = function(obj, types) {
};
const addErrorData = function(err, request, response, requestState) {
err.request = request;
err.response = response;
err.requestState = requestState;
return err;
// 'errors' lib uses Object.defineProperty, non-writable by default
return Object.assign({}, err, {
request,
response,
requestState,
});
};
const isStream = obj => (obj != null) && (ofTypes(obj, [stream.Readable, stream.Duplex, stream.Transform]) || obj.hasOwnProperty("_bhttpStreamWrapper"));

Loading…
Cancel
Save