Comparing version 1.0.0 to 1.1.0
@@ -1,49 +0,4 @@ | ||
var request_1 = require('./request'); | ||
var common_1 = require('./common'); | ||
var index_1 = require('./plugins/index'); | ||
request_1.default.prototype._open = open; | ||
request_1.default.prototype._abort = abort; | ||
request_1.default.prototype._use = index_1.defaults; | ||
function getXHR() { | ||
if (XMLHttpRequest) { | ||
return new XMLHttpRequest(); | ||
} | ||
try { | ||
return new ActiveXObject('Microsoft.XMLHTTP'); | ||
} | ||
catch (e) { } | ||
try { | ||
return new ActiveXObject('Msxml2.XMLHTTP.6.0'); | ||
} | ||
catch (e) { } | ||
try { | ||
return new ActiveXObject('Msxml2.XMLHTTP.3.0'); | ||
} | ||
catch (e) { } | ||
try { | ||
return new ActiveXObject('Msxml2.XMLHTTP'); | ||
} | ||
catch (e) { } | ||
throw new TypeError('XMLHttpRequest is not available'); | ||
} | ||
function parseRawHeaders(xhr) { | ||
var headers = {}; | ||
var lines = xhr.getAllResponseHeaders().split(/\r?\n/); | ||
lines.pop(); | ||
lines.forEach(function (header) { | ||
var indexOf = header.indexOf(':'); | ||
var name = header.substr(0, indexOf); | ||
var value = header.substr(indexOf + 1).trim(); | ||
if (!headers.hasOwnProperty(name)) { | ||
headers[name] = value; | ||
} | ||
else if (typeof headers[name] === 'string') { | ||
headers[name] = [headers[name], value]; | ||
} | ||
else { | ||
headers[name].push(value); | ||
} | ||
}); | ||
return headers; | ||
} | ||
var get_headers_1 = require('get-headers'); | ||
function open(request) { | ||
@@ -53,11 +8,12 @@ return new Promise(function (resolve, reject) { | ||
var method = request.method; | ||
var responseType = request.options.responseType; | ||
if (window.location.protocol === 'https:' && /^http\:/.test(url)) { | ||
return reject(request.error("The request to \"" + url + "\" was blocked", 'EBLOCKED')); | ||
} | ||
var xhr = request.raw = getXHR(); | ||
var xhr = request.raw = new XMLHttpRequest(); | ||
xhr.onload = function () { | ||
return resolve({ | ||
status: xhr.status === 1223 ? 204 : xhr.status, | ||
headers: parseRawHeaders(xhr), | ||
body: xhr.responseText, | ||
headers: get_headers_1.parse(xhr.getAllResponseHeaders()), | ||
body: responseType ? xhr.response : xhr.responseText, | ||
url: xhr.responseURL | ||
@@ -99,2 +55,12 @@ }); | ||
} | ||
if (responseType) { | ||
try { | ||
xhr.responseType = responseType; | ||
} | ||
finally { | ||
if (xhr.responseType !== responseType) { | ||
throw request.error("Unsupported response type: " + responseType, 'ERESPONSETYPE'); | ||
} | ||
} | ||
} | ||
Object.keys(request.headers).forEach(function (header) { | ||
@@ -109,3 +75,5 @@ xhr.setRequestHeader(request.name(header), request.get(header)); | ||
} | ||
module.exports = common_1.defaults({}); | ||
module.exports = common_1.defaults({ | ||
transport: { open: open, abort: abort, use: index_1.defaults } | ||
}); | ||
//# sourceMappingURL=browser.js.map |
@@ -1,3 +0,3 @@ | ||
var http = require('http'); | ||
var https = require('https'); | ||
var http_1 = require('http'); | ||
var https_1 = require('https'); | ||
var agent = require('infinity-agent'); | ||
@@ -7,3 +7,3 @@ var through2 = require('through2'); | ||
var extend = require('xtend'); | ||
var request_1 = require('./request'); | ||
var get_headers_1 = require('get-headers'); | ||
var common_1 = require('./common'); | ||
@@ -25,33 +25,2 @@ var index_1 = require('./plugins/index'); | ||
}; | ||
request_1.default.prototype._open = open; | ||
request_1.default.prototype._abort = abort; | ||
request_1.default.prototype._use = index_1.defaults; | ||
function parseRawHeaders(response) { | ||
var headers = {}; | ||
if (!response.rawHeaders) { | ||
Object.keys(response.headers).forEach(function (key) { | ||
var value = response.headers[key]; | ||
if (Array.isArray(value) && value.length === 1) { | ||
value = value[0]; | ||
} | ||
headers[key] = value; | ||
}); | ||
} | ||
else { | ||
for (var i = 0; i < response.rawHeaders.length; i = i + 2) { | ||
var name = response.rawHeaders[i]; | ||
var value = response.rawHeaders[i + 1]; | ||
if (!headers.hasOwnProperty(name)) { | ||
headers[name] = value; | ||
} | ||
else if (typeof headers[name] === 'string') { | ||
headers[name] = [headers[name], value]; | ||
} | ||
else { | ||
headers[name].push(value); | ||
} | ||
} | ||
} | ||
return headers; | ||
} | ||
function open(request) { | ||
@@ -87,6 +56,6 @@ return new Promise(function (resolve, reject) { | ||
var isHttp = arg.protocol !== 'https:'; | ||
var engine = isHttp ? http : https; | ||
var engine = isHttp ? http_1.request : https_1.request; | ||
arg.agent = request.options.agent || (isHttp ? agent.http.globalAgent : agent.https.globalAgent); | ||
arg.rejectUnauthorized = request.options.rejectUnauthorized !== false; | ||
var req = engine.request(arg); | ||
var req = engine(arg); | ||
req.once('response', function (res) { | ||
@@ -118,3 +87,3 @@ var status = res.statusCode; | ||
status: status, | ||
headers: parseRawHeaders(res), | ||
headers: get_headers_1.http(res), | ||
url: url | ||
@@ -163,3 +132,5 @@ }); | ||
} | ||
module.exports = common_1.defaults({}); | ||
module.exports = common_1.defaults({ | ||
transport: { open: open, abort: abort, use: index_1.defaults } | ||
}); | ||
//# sourceMappingURL=index.js.map |
@@ -67,2 +67,5 @@ var FormData = require('form-data'); | ||
var body = response.body; | ||
if (typeof body !== 'string') { | ||
return; | ||
} | ||
if (body === '') { | ||
@@ -69,0 +72,0 @@ response.body = null; |
@@ -23,5 +23,5 @@ var __extends = (this && this.__extends) || function (d, b) { | ||
return { | ||
url: this.fullUrl(), | ||
headers: this.get(), | ||
body: this.body, | ||
url: this.fullUrl(), | ||
status: this.status | ||
@@ -28,0 +28,0 @@ }; |
{ | ||
"name": "popsicle", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Simple HTTP requests for node and the browser", | ||
@@ -77,2 +77,3 @@ "main": "dist/lib/index.js", | ||
"form-data": "^0.2.0", | ||
"get-headers": "^1.0.0", | ||
"infinity-agent": "^2.0.3", | ||
@@ -79,0 +80,0 @@ "methods": "^1.1.1", |
@@ -92,2 +92,3 @@ # ![Popsicle](https://cdn.rawgit.com/blakeembrey/popsicle/master/logo.svg) | ||
* **withCredentials** Send cookies with CORS requests (default: `false`) | ||
* **responseType** Set the XHR `responseType` (default: `undefined`) | ||
@@ -349,3 +350,3 @@ #### Short-hand Methods | ||
Creating a custom transportation layer is just a matter creating an object with `open`, `close` and `use` options set. The open method should set any request information required between called as `request.raw`. Close must abort the request, and `open` must **always** resolve. Use can be set to an empty array if no plugins should be used by default. It's recommended to keep `use` set to the defaults, or as close as possible. | ||
Creating a custom transportation layer is just a matter creating an object with `open`, `abort` and `use` options set. The open method should set any request information required between called as `request.raw`. Abort must abort the current request instance, while `open` must **always** resolve the promise. You can set `use` to an empty array if no plugins should be used by default. However, it's recommended you keep `use` set to the defaults, or as close as possible using your transport layer. | ||
@@ -352,0 +353,0 @@ ## TypeScript |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
381
2
93991
9
1098
+ Addedget-headers@^1.0.0
+ Addedget-headers@1.0.5(transitive)