Comparing version 4.0.3 to 4.0.4
174
lib/http.js
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -114,6 +106,4 @@ const util = require("util"); | ||
*/ | ||
static patch(url, options = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return this.request(url, Object.assign({}, options, { method: 'PATCH' })); | ||
}); | ||
static async patch(url, options = {}) { | ||
return this.request(url, Object.assign({}, options, { method: 'PATCH' })); | ||
} | ||
@@ -131,6 +121,4 @@ /** | ||
*/ | ||
static delete(url, options = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return this.request(url, Object.assign({}, options, { method: 'DELETE' })); | ||
}); | ||
static async delete(url, options = {}) { | ||
return this.request(url, Object.assign({}, options, { method: 'DELETE' })); | ||
} | ||
@@ -152,8 +140,6 @@ /** | ||
} | ||
static request(url, options = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let http = new this(url, options); | ||
yield http._request(); | ||
return http; | ||
}); | ||
static async request(url, options = {}) { | ||
let http = new this(url, options); | ||
await http._request(); | ||
return http; | ||
} | ||
@@ -220,65 +206,59 @@ static defaults(options = {}) { | ||
} | ||
_request() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this._debugRequest(); | ||
try { | ||
this.response = yield this._performRequest(); | ||
} | ||
catch (err) { | ||
debug(err); | ||
return this._maybeRetry(err); | ||
} | ||
if (this._shouldParseResponseBody) | ||
yield this._parse(); | ||
this._debugResponse(); | ||
if (this._responseRedirect) | ||
return this._redirect(); | ||
if (!this._responseOK) { | ||
throw new HTTPError(this); | ||
} | ||
if (!this.partial) | ||
yield this._getNextRange(); | ||
}); | ||
async _request() { | ||
this._debugRequest(); | ||
try { | ||
this.response = await this._performRequest(); | ||
} | ||
catch (err) { | ||
debug(err); | ||
return this._maybeRetry(err); | ||
} | ||
if (this._shouldParseResponseBody) | ||
await this._parse(); | ||
this._debugResponse(); | ||
if (this._responseRedirect) | ||
return this._redirect(); | ||
if (!this._responseOK) { | ||
throw new HTTPError(this); | ||
} | ||
if (!this.partial) | ||
await this._getNextRange(); | ||
} | ||
_redirect() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this._redirectRetries) | ||
this._redirectRetries = 0; | ||
this._redirectRetries++; | ||
if (this._redirectRetries > 10) | ||
throw new Error(`Redirect loop at ${this.url}`); | ||
if (!this.headers.location) | ||
throw new Error(`Redirect from ${this.url} has no location header`); | ||
const location = this.headers.location; | ||
if (Array.isArray(location)) { | ||
this.url = location[0]; | ||
} | ||
else { | ||
this.url = location; | ||
} | ||
yield this._request(); | ||
}); | ||
async _redirect() { | ||
if (!this._redirectRetries) | ||
this._redirectRetries = 0; | ||
this._redirectRetries++; | ||
if (this._redirectRetries > 10) | ||
throw new Error(`Redirect loop at ${this.url}`); | ||
if (!this.headers.location) | ||
throw new Error(`Redirect from ${this.url} has no location header`); | ||
const location = this.headers.location; | ||
if (Array.isArray(location)) { | ||
this.url = location[0]; | ||
} | ||
else { | ||
this.url = location; | ||
} | ||
await this._request(); | ||
} | ||
_maybeRetry(err) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this._errorRetries) | ||
this._errorRetries = 0; | ||
this._errorRetries++; | ||
const allowed = (err) => { | ||
if (this._errorRetries > 5) | ||
return false; | ||
if (!err.code) | ||
return false; | ||
if (err.code === 'ENOTFOUND') | ||
return true; | ||
return require('is-retry-allowed')(err); | ||
}; | ||
if (allowed(err)) { | ||
let noise = Math.random() * 100; | ||
yield this._wait((1 << this._errorRetries) * 100 + noise); | ||
yield this._request(); | ||
return; | ||
} | ||
throw err; | ||
}); | ||
async _maybeRetry(err) { | ||
if (!this._errorRetries) | ||
this._errorRetries = 0; | ||
this._errorRetries++; | ||
const allowed = (err) => { | ||
if (this._errorRetries > 5) | ||
return false; | ||
if (!err.code) | ||
return false; | ||
if (err.code === 'ENOTFOUND') | ||
return true; | ||
return require('is-retry-allowed')(err); | ||
}; | ||
if (allowed(err)) { | ||
let noise = Math.random() * 100; | ||
await this._wait((1 << this._errorRetries) * 100 + noise); | ||
await this._request(); | ||
return; | ||
} | ||
throw err; | ||
} | ||
@@ -316,9 +296,7 @@ _debugRequest() { | ||
} | ||
_parse() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.body = yield concat(this.response); | ||
let json = this.response.headers['content-type'] && deps_1.deps.contentType.parse(this.response).type.startsWith('application/json'); | ||
if (json) | ||
this.body = JSON.parse(this.body); | ||
}); | ||
async _parse() { | ||
this.body = await concat(this.response); | ||
let json = this.response.headers['content-type'] && deps_1.deps.contentType.parse(this.response).type.startsWith('application/json'); | ||
if (json) | ||
this.body = JSON.parse(this.body); | ||
} | ||
@@ -341,10 +319,8 @@ _parseBody(body) { | ||
} | ||
_getNextRange() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const next = this.headers['next-range']; | ||
this.options.headers['range'] = Array.isArray(next) ? next[0] : next; | ||
let prev = this.body; | ||
yield this._request(); | ||
this.body = prev.concat(this.body); | ||
}); | ||
async _getNextRange() { | ||
const next = this.headers['next-range']; | ||
this.options.headers['range'] = Array.isArray(next) ? next[0] : next; | ||
let prev = this.body; | ||
await this._request(); | ||
this.body = prev.concat(this.body); | ||
} | ||
@@ -365,3 +341,3 @@ _redactedHeaders(headers) { | ||
return false; | ||
return this.statusCode >= 300 && this.statusCode < 400; | ||
return [301, 302, 303, 307, 308].includes(this.statusCode); | ||
} | ||
@@ -368,0 +344,0 @@ get _shouldParseResponseBody() { |
{ | ||
"name": "http-call", | ||
"description": "make http requests", | ||
"version": "4.0.3", | ||
"version": "4.0.4", | ||
"author": "Jeff Dickey @jdxcode", | ||
@@ -6,0 +6,0 @@ "bugs": "https://github.com/heroku/http-call/issues", |
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
21188
604