Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

popsicle

Package Overview
Dependencies
Maintainers
1
Versions
99
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

popsicle - npm Package Compare versions

Comparing version 0.3.2 to 0.3.3

2

bower.json
{
"name": "popsicle",
"main": "popsicle.js",
"version": "0.3.2",
"version": "0.3.3",
"homepage": "https://github.com/blakeembrey/popsicle",

@@ -6,0 +6,0 @@ "authors": [

{
"name": "popsicle",
"version": "0.3.2",
"version": "0.3.3",
"description": "Simple HTTP requests for node and the browser",

@@ -5,0 +5,0 @@ "main": "popsicle.js",

@@ -1,3 +0,4 @@

(function (root) {
(function () {
var isNode = typeof window === 'undefined';
var root = isNode ? global : window;
var Buffer = isNode ? require('buffer').Buffer : null;

@@ -99,20 +100,2 @@ var FormData = isNode ? require('form-data') : window.FormData;

/**
* Return the byte length of an input.
*
* @param {(String|Buffer)} data
* @return {Number}
*/
function byteLength (data) {
if (Buffer.isBuffer(data)) {
return data.length;
}
if (typeof data === 'string') {
return Buffer.byteLength(data);
}
return 0;
}
/**
* Turn a value into a number (avoid `null` becoming `0`).

@@ -213,20 +196,2 @@ *

/**
* Determine XHR method.
*
* @return {Function}
*/
function getXHR () {
if (window.XMLHttpRequest) {
return new window.XMLHttpRequest();
}
try { return new window.ActiveXObject('Microsoft.XMLHTTP'); } catch (e) {}
try { return new window.ActiveXObject('Msxml2.XMLHTTP.6.0'); } catch (e) {}
try { return new window.ActiveXObject('Msxml2.XMLHTTP.3.0'); } catch (e) {}
try { return new window.ActiveXObject('Msxml2.XMLHTTP'); } catch (e) {}
throw new Error('XMLHttpRequest is not available');
}
/**
* Return the content type from a header string.

@@ -254,3 +219,3 @@ *

return encodeURIComponent(str)
.replace(/[!'()]/g, escape)
.replace(/[!'()]/g, root.escape)
.replace(/\*/g, '%2A');

@@ -474,143 +439,5 @@ } catch (e) {

/**
* Parse headers from a string.
* Set headers on an instance.
*
* @param {String} str
* @return {Object}
*/
function parseHeaders (str) {
var headers = {};
var lines = str.split(/\r?\n/);
lines.pop();
lines.forEach(function (header) {
var index = header.indexOf(':');
var name = header.substr(0, index);
var value = header.substr(index + 1).trim();
headers[name] = value;
});
return headers;
}
/**
* Get all XHR response headers as an object.
*
* @param {XMLHttpRequest} xhr
* @return {Object}
*/
function getAllResponseHeaders (xhr) {
var headers = parseHeaders(xhr.getAllResponseHeaders());
return headers;
}
/**
* Turn raw headers into a header object.
*
* @param {Object} response
* @return {Object}
*/
function parseRawHeaders (response) {
if (!response.rawHeaders) {
return response.headers;
}
var headers = {};
var rawHeaders = response.rawHeaders;
for (var i = 0; i < rawHeaders.length; i = i + 2) {
headers[rawHeaders[i]] = rawHeaders[i + 1];
}
return headers;
}
/**
* Return options sanitized for the request module.
*
* @param {Request} self
* @return {Object}
*/
function requestOptions (self) {
var request = {};
request.url = self.fullUrl();
request.method = self.method;
request.headers = self.headers;
// The `request` module supports form data under a private property.
if (self.body instanceof FormData) {
request._form = self.body;
} else {
request.body = self.body;
}
if (self.rejectUnauthorized) {
request.rejectUnauthorized = true;
}
return request;
}
/**
* Track the current download size.
*
* @param {Request} self
* @param {request} request
*/
function trackRequestProgress (self, request) {
var write = request.write;
self._request = request;
function onRequest () {
var write = request.req.write;
self.uploadTotal = num(request.headers['content-length']);
// Override `Request.prototype.write` to track amount of sent data.
request.req.write = function (data) {
self._setUploadSize(self.uploadSize + byteLength(data));
return write.apply(this, arguments);
};
}
function onResponse (response) {
self.downloadTotal = num(response.headers['content-length']);
self._uploadFinished();
}
function onResponseData (data) {
// Data should always be a `Buffer` instance.
self._setDownloadSize(self.downloadSize + data.length);
}
function onResponseEnd () {
removeListeners();
self._downloadFinished();
}
function removeListeners () {
request.removeListener('request', onRequest);
request.removeListener('response', onResponse);
request.removeListener('data', onResponseData);
request.removeListener('end', onResponseEnd);
request.removeListener('error', removeListeners);
}
request.on('request', onRequest);
request.on('response', onResponse);
request.on('data', onResponseData);
request.on('end', onResponseEnd);
request.on('error', removeListeners);
}
/**
* Set multiple headers on an instance.
*
* @param {Request} self
* @param {Object} headers

@@ -647,3 +474,3 @@ */

this.headers = {};
this._headerNames = {};
this.headerNames = {};
}

@@ -668,3 +495,3 @@

this.headers[lower] = value;
this._headerNames[lower] = key;
this.headerNames[lower] = key;

@@ -681,3 +508,3 @@ return this;

Headers.prototype.name = function (key) {
return this._headerNames[lowerHeader(key)];
return this.headerNames[lowerHeader(key)];
};

@@ -705,3 +532,3 @@

delete this.headers[lower];
delete this._headerNames[lower];
delete this.headerNames[lower];

@@ -1055,3 +882,3 @@ return this;

// Set everything to be completed.
// Set everything to completed.
this.downloaded = this.uploaded = this.completed = 1;

@@ -1128,4 +955,127 @@

var request = require('request');
var pkg = require('./package.json');
/**
* Return options sanitized for the request module.
*
* @param {Request} self
* @return {Object}
*/
var requestOptions = function (self) {
var request = {};
request.url = self.fullUrl();
request.method = self.method;
// Set a default user-agent.
request.headers = assign(self.headers, {
'User-Agent': 'node-popsicle/' + pkg.version
});
// The `request` module supports form data under a private property.
if (self.body instanceof FormData) {
request._form = self.body;
} else {
request.body = self.body;
}
if (self.rejectUnauthorized) {
request.rejectUnauthorized = true;
}
return request;
};
/**
* Return the byte length of an input.
*
* @param {(String|Buffer)} data
* @return {Number}
*/
var byteLength = function (data) {
if (Buffer.isBuffer(data)) {
return data.length;
}
if (typeof data === 'string') {
return Buffer.byteLength(data);
}
return 0;
};
/**
* Track the current download size.
*
* @param {Request} self
* @param {request} request
*/
var trackRequestProgress = function (self, request) {
self._request = request;
function onRequest () {
var write = request.req.write;
self.uploadTotal = num(request.headers['content-length']);
// Override `Request.prototype.write` to track amount of sent data.
request.req.write = function (data) {
self._setUploadSize(self.uploadSize + byteLength(data));
return write.apply(this, arguments);
};
}
function onResponse (response) {
self.downloadTotal = num(response.headers['content-length']);
self._uploadFinished();
}
function onResponseData (data) {
// Data should always be a `Buffer` instance.
self._setDownloadSize(self.downloadSize + data.length);
}
function onResponseEnd () {
removeListeners();
self._downloadFinished();
}
function removeListeners () {
request.removeListener('request', onRequest);
request.removeListener('response', onResponse);
request.removeListener('data', onResponseData);
request.removeListener('end', onResponseEnd);
request.removeListener('error', removeListeners);
}
request.on('request', onRequest);
request.on('response', onResponse);
request.on('data', onResponseData);
request.on('end', onResponseEnd);
request.on('error', removeListeners);
};
/**
* Turn raw headers into a header object.
*
* @param {Object} response
* @return {Object}
*/
var parseRawHeaders = function (response) {
if (!response.rawHeaders) {
return response.headers;
}
var headers = {};
var rawHeaders = response.rawHeaders;
for (var i = 0; i < rawHeaders.length; i = i + 2) {
headers[rawHeaders[i]] = rawHeaders[i + 1];
}
return headers;
};
/**
* Trigger the request in node.

@@ -1220,2 +1170,56 @@ *

/**
* Determine XHR method.
*
* @return {Function}
*/
var getXHR = function () {
if (root.XMLHttpRequest) {
return new root.XMLHttpRequest();
}
try { return new root.ActiveXObject('Microsoft.XMLHTTP'); } catch (e) {}
try { return new root.ActiveXObject('Msxml2.XMLHTTP.6.0'); } catch (e) {}
try { return new root.ActiveXObject('Msxml2.XMLHTTP.3.0'); } catch (e) {}
try { return new root.ActiveXObject('Msxml2.XMLHTTP'); } catch (e) {}
throw new Error('XMLHttpRequest is not available');
};
/**
* Parse headers from a string.
*
* @param {String} str
* @return {Object}
*/
var parseHeaders = function (str) {
var headers = {};
var lines = str.split(/\r?\n/);
lines.pop();
lines.forEach(function (header) {
var index = header.indexOf(':');
var name = header.substr(0, index);
var value = header.substr(index + 1).trim();
headers[name] = value;
});
return headers;
};
/**
* Get all XHR response headers as an object.
*
* @param {XMLHttpRequest} xhr
* @return {Object}
*/
var getAllResponseHeaders = function (xhr) {
var headers = parseHeaders(xhr.getAllResponseHeaders());
return headers;
};
/**
* Trigger the request in a browser.

@@ -1383,2 +1387,2 @@ *

}
})(this);
})();

@@ -231,2 +231,3 @@ # ![Popsicle](https://cdn.rawgit.com/blakeembrey/popsicle/master/logo.svg)

* [Prefix](https://github.com/blakeembrey/popsicle-prefix) - Automatically prefix all HTTP requests
* [Constants](https://github.com/blakeembrey/popsicle-constants) - Replace constants in the URL string

@@ -233,0 +234,0 @@ #### Using Plugins

@@ -0,1 +1,3 @@

var isNode = typeof window === 'undefined';
var REMOTE_URL = 'http://localhost:4567';

@@ -116,4 +118,15 @@

describe('headers', function () {
it('should parse response headers', function () {
describe('request headers', function () {
it('should always send a user agent', function () {
return popsicle(REMOTE_URL + '/echo/header/user-agent')
.then(function (res) {
var regexp = isNode ? /^node-popsicle\/\d\.\d\.\d$/ : /^Mozilla\/.+$/;
expect(res.body).to.match(regexp);
});
});
});
describe('response headers', function () {
it('should parse', function () {
return popsicle(REMOTE_URL + '/notfound')

@@ -120,0 +133,0 @@ .then(function (res) {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc