Comparing version 0.5.4 to 0.5.5
{ | ||
"name": "popsicle", | ||
"version": "0.5.4", | ||
"version": "0.5.5", | ||
"description": "Simple HTTP requests for node and the browser", | ||
@@ -5,0 +5,0 @@ "main": "popsicle.js", |
@@ -165,2 +165,16 @@ /* global define */ | ||
/** | ||
* Create a stringify error instance. | ||
* | ||
* @param {Request} req | ||
* @param {Error} e | ||
* @return {Error} | ||
*/ | ||
function stringifyError (req, e) { | ||
var err = req.error('Unable to stringify the request body') | ||
err.stringify = true | ||
err.original = e | ||
return err | ||
} | ||
/** | ||
* Create a CSP error instance (Cross-. | ||
@@ -185,5 +199,6 @@ * | ||
*/ | ||
function unavailableError (req) { | ||
function unavailableError (req, e) { | ||
var err = req.error('Unable to connect to "' + req.fullUrl() + '"') | ||
err.unavailable = true | ||
err.original = e | ||
return err | ||
@@ -375,8 +390,12 @@ } | ||
if (JSON_MIME_REGEXP.test(type)) { | ||
req.body = JSON.stringify(body) | ||
} else if (FORM_MIME_REGEXP.test(type)) { | ||
req.body = form(body) | ||
} else if (QUERY_MIME_REGEXP.test(type)) { | ||
req.body = stringifyQuery(body) | ||
try { | ||
if (JSON_MIME_REGEXP.test(type)) { | ||
req.body = JSON.stringify(body) | ||
} else if (FORM_MIME_REGEXP.test(type)) { | ||
req.body = form(body) | ||
} else if (QUERY_MIME_REGEXP.test(type)) { | ||
req.body = stringifyQuery(body) | ||
} | ||
} catch (e) { | ||
return Promise.reject(stringifyError(req, e)) | ||
} | ||
@@ -871,4 +890,2 @@ } | ||
var query = options.query | ||
var stream = options.stream === true | ||
var parse = options.parse !== false | ||
@@ -887,2 +904,6 @@ // Request options. | ||
this.agent = options.agent | ||
this.stream = options.stream === true | ||
this.raw = options.raw === true | ||
this.encoding = options.encoding || 'string' | ||
this.parse = !this.raw && this.encoding === 'string' && options.parse !== false | ||
@@ -923,9 +944,20 @@ // Default redirect count. | ||
if (this.jar && isNode) { | ||
this.before(getCookieJar) | ||
this.after(setCookieJar) | ||
if (this.jar) { | ||
if (isNode) { | ||
this.before(getCookieJar) | ||
this.after(setCookieJar) | ||
} else { | ||
throw new TypeError('Option `jar` is not available in browsers') | ||
} | ||
} | ||
// Support streaming responses under node. | ||
if (!stream) { | ||
if (this.raw) { | ||
if (!isNode) { | ||
throw new TypeError('Option `raw` is not available in browsers') | ||
} | ||
} else if (isNode) { | ||
this.after(unzipResponse) | ||
} | ||
if (!this.stream) { | ||
if (isNode) { | ||
@@ -935,7 +967,7 @@ this.after(streamResponse) | ||
if (parse) { | ||
if (this.parse) { | ||
this.after(parseResponse) | ||
} | ||
} else if (!isNode) { | ||
throw new Error('Streaming is only available in node') | ||
throw new TypeError('Option `stream` is not available in browsers') | ||
} | ||
@@ -1081,3 +1113,3 @@ | ||
var concatStream = concat({ | ||
encoding: 'string' | ||
encoding: res.request.encoding | ||
}, function (data) { | ||
@@ -1096,2 +1128,15 @@ // Update the response `body`. | ||
/** | ||
* Automatically unzip response bodies. | ||
* | ||
* @param {Response} res | ||
*/ | ||
var unzipResponse = function (res) { | ||
if (['gzip', 'deflate'].indexOf(res.get('Content-Encoding')) !== -1) { | ||
var unzip = zlib.createUnzip() | ||
res.body.pipe(unzip) | ||
res.body = unzip | ||
} | ||
} | ||
/** | ||
* Read cookies from the cookie jar. | ||
@@ -1236,3 +1281,2 @@ * | ||
var statusCode = response.statusCode | ||
var stream = response | ||
@@ -1256,15 +1300,7 @@ // Handle HTTP redirections. | ||
// Track download progress. | ||
stream.pipe(responseProxy) | ||
stream = responseProxy | ||
response.pipe(responseProxy) | ||
// Decode zipped responses. | ||
if (['gzip', 'deflate'].indexOf(response.headers['content-encoding']) !== -1) { | ||
var unzip = zlib.createUnzip() | ||
stream.pipe(unzip) | ||
stream = unzip | ||
} | ||
var res = new Response(req) | ||
res.body = stream | ||
res.body = responseProxy | ||
res.status = response.statusCode | ||
@@ -1276,4 +1312,4 @@ res.set(parseRawHeaders(response)) | ||
request.once('error', function () { | ||
return reject(req.aborted ? abortError(req) : unavailableError(req)) | ||
request.once('error', function (err) { | ||
return reject(req.aborted ? abortError(req) : unavailableError(req, err)) | ||
}) | ||
@@ -1280,0 +1316,0 @@ |
@@ -72,3 +72,3 @@ # ![Popsicle](https://cdn.rawgit.com/blakeembrey/popsicle/master/logo.svg) | ||
* **timeout** The number of milliseconds before cancelling the request (default: `Infinity`) | ||
* **parse** Optionally skip response parsing (default: `true`) | ||
* **parse** Skip automatic response parsing (default: `true`) | ||
@@ -81,3 +81,5 @@ **Node only** | ||
* **rejectUnauthorized** Reject invalid SSL certificates (default: `true`) | ||
* **stream** Stream the HTTP response body (default: `false`) | ||
* **stream** Stream the HTTP response body (default: `false`, disables `parse`) | ||
* **raw** Return the raw stream without unzipping (default: `false`, disables `parse`) | ||
* **encoding** Specify the response body format when not streaming (default: `string`, allowed: `string`, `buffer`, `array`, `uint8`, disables `parse`) | ||
@@ -230,2 +232,3 @@ **Browser only** | ||
* **parse error** Response body failed to parse - invalid body or incorrect type (`err.parse`) | ||
* **stringify error** Request body failed to stringify - invalid body or incorrect type (`err.stringify`) | ||
* **abort error** The request was aborted by user intervention (`err.abort`) | ||
@@ -232,0 +235,0 @@ * **timeout error** The request timed out (`err.timeout`) |
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
52119
1314
300