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 3.1.1 to 3.2.0

8

dist/base.d.ts

@@ -8,5 +8,2 @@ import { Url } from 'url';

}
export interface HeaderMap {
[name: string]: string;
}
export declare type RawHeaders = string[];

@@ -21,4 +18,3 @@ export interface BaseOptions {

Url: Url;
headerNames: HeaderMap;
headerValues: HeaderMap;
rawHeaders: RawHeaders;
constructor({url, headers, rawHeaders, query}: BaseOptions);

@@ -29,3 +25,3 @@ url: string;

set(name: string, value: string | string[]): Base;
append(name: string, value: string | string[]): Base;
append(name: string, value: string | string[]): this;
name(name: string): string;

@@ -32,0 +28,0 @@ get(name: string): string;

@@ -14,4 +14,7 @@ var url_1 = require('url');

}
function stringifyHeader(value) {
return Array.isArray(value) ? value.join(', ') : String(value);
function concat(a, b) {
if (a == null) {
return b;
}
return Array.isArray(a) ? a.concat(b) : [a, b];
}

@@ -22,4 +25,3 @@ var Base = (function () {

this.Url = {};
this.headerNames = {};
this.headerValues = {};
this.rawHeaders = [];
if (url != null) {

@@ -32,7 +34,6 @@ this.url = url;

if (rawHeaders) {
for (var i = 0; i < rawHeaders.length; i += 2) {
var name_1 = rawHeaders[i];
var value = rawHeaders[i + 1];
this.append(name_1, value);
if (rawHeaders.length % 2 === 1) {
throw new TypeError("Expected raw headers length to be even, was " + rawHeaders.length);
}
this.rawHeaders = rawHeaders.slice(0);
}

@@ -67,5 +68,6 @@ else {

var headers = {};
for (var _i = 0, _a = Object.keys(this.headerNames); _i < _a.length; _i++) {
var key = _a[_i];
headers[key] = this.headerValues[key];
for (var i = 0; i < this.rawHeaders.length; i += 2) {
var key = lowerHeader(this.rawHeaders[i]);
var value = concat(headers[key], this.rawHeaders[i + 1]);
headers[key] = value;
}

@@ -75,8 +77,7 @@ return headers;

set: function (headers) {
this.headerNames = {};
this.headerValues = {};
this.rawHeaders = [];
if (headers) {
for (var _i = 0, _a = Object.keys(headers); _i < _a.length; _i++) {
var key = _a[_i];
this.set(key, headers[key]);
this.append(key, headers[key]);
}

@@ -89,30 +90,46 @@ }

Base.prototype.set = function (name, value) {
var lower = lowerHeader(name);
if (value == null) {
delete this.headerNames[lower];
delete this.headerValues[lower];
this.remove(name);
this.append(name, value);
return this;
};
Base.prototype.append = function (name, value) {
if (Array.isArray(value)) {
for (var _i = 0; _i < value.length; _i++) {
var v = value[_i];
this.rawHeaders.push(name, v);
}
}
else {
this.headerNames[lower] = name;
this.headerValues[lower] = stringifyHeader(value);
this.rawHeaders.push(name, value);
}
return this;
};
Base.prototype.append = function (name, value) {
var previous = this.get(name);
if (previous != null) {
value = previous + ", " + stringifyHeader(value);
Base.prototype.name = function (name) {
var lowered = lowerHeader(name);
var headerName;
for (var i = 0; i < this.rawHeaders.length; i += 2) {
if (lowerHeader(this.rawHeaders[i]) === lowered) {
headerName = this.rawHeaders[i];
}
}
return this.set(name, value);
return headerName;
};
Base.prototype.name = function (name) {
return this.headerNames[lowerHeader(name)];
};
Base.prototype.get = function (name) {
return this.headerValues[lowerHeader(name)];
var lowered = lowerHeader(name);
var value;
for (var i = 0; i < this.rawHeaders.length; i += 2) {
if (lowerHeader(this.rawHeaders[i]) === lowered) {
value = value == null ? this.rawHeaders[i + 1] : value + ", " + this.rawHeaders[i + 1];
}
}
return value;
};
Base.prototype.remove = function (name) {
var lower = lowerHeader(name);
delete this.headerNames[lower];
delete this.headerValues[lower];
var lowered = lowerHeader(name);
var len = this.rawHeaders.length;
while ((len -= 2) >= 0) {
if (lowerHeader(this.rawHeaders[len]) === lowered) {
this.rawHeaders.splice(len, 2);
}
}
return this;

@@ -119,0 +136,0 @@ };

@@ -64,5 +64,5 @@ var Promise = require('any-promise');

}
Object.keys(request.headers).forEach(function (header) {
xhr.setRequestHeader(request.name(header), request.get(header));
});
for (var i = 0; i < request.rawHeaders.length; i += 2) {
xhr.setRequestHeader(request.rawHeaders[i], request.rawHeaders[i + 1]);
}
xhr.send(request.body);

@@ -69,0 +69,0 @@ });

@@ -12,2 +12,6 @@ import Promise = require('any-promise');

use?: Middleware[];
before?: RequestPluginFunction[];
after?: ResponsePluginFunction[];
always?: RequestPluginFunction[];
progress?: RequestPluginFunction[];
transport?: TransportOptions;

@@ -64,7 +68,9 @@ }

exec(cb: (err: PopsicleError, response?: Response) => any): void;
toOptions(): RequestOptions;
toJSON(): RequestJSON;
progress(fn: RequestPluginFunction): Request;
before(fn: RequestPluginFunction): Request;
after(fn: ResponsePluginFunction): Request;
always(fn: RequestPluginFunction): Request;
clone(): Request;
progress(fn: RequestPluginFunction | RequestPluginFunction[]): Request;
before(fn: RequestPluginFunction | RequestPluginFunction[]): Request;
after(fn: ResponsePluginFunction | ResponsePluginFunction[]): Request;
always(fn: RequestPluginFunction | RequestPluginFunction[]): Request;
abort(): this;

@@ -71,0 +77,0 @@ uploaded: number;

@@ -38,3 +38,6 @@ var __extends = (this && this.__extends) || function (d, b) {

this.use(options.use || this.transport.use);
this.always(removeListeners);
this.before(options.before);
this.after(options.after);
this.always(options.always);
this.progress(options.progress);
}

@@ -58,4 +61,19 @@ Request.prototype.use = function (fn) {

cb(null, response);
}).catch(cb);
}, cb);
};
Request.prototype.toOptions = function () {
return {
url: this.url,
method: this.method,
options: this.options,
use: [],
transport: this.transport,
timeout: this.timeout,
rawHeaders: this.rawHeaders,
before: this._before,
after: this._after,
progress: this._progress,
always: this._always
};
};
Request.prototype.toJSON = function () {

@@ -71,2 +89,5 @@ return {

};
Request.prototype.clone = function () {
return new Request(this.toOptions());
};
Request.prototype.progress = function (fn) {

@@ -164,10 +185,14 @@ return pluginFunction(this, '_progress', fn);

exports.default = Request;
function pluginFunction(request, property, fn) {
function pluginFunction(request, property, fns) {
if (request.started) {
throw new TypeError('Plugins can not be used after the request has started');
}
if (typeof fn !== 'function') {
throw new TypeError("Expected a function, but got " + fn + " instead");
for (var _i = 0, _a = arrify(fns); _i < _a.length; _i++) {
var fn = _a[_i];
if (typeof fn !== 'function') {
throw new TypeError("Expected a function, but got " + fn + " instead");
}
;
request[property].push(fn);
}
request[property].push(fn);
return request;

@@ -215,5 +240,7 @@ }

.then(function () {
return chain(req._always, request).then(function () { return request.response; });
return chain(req._always, request)
.then(function () { return request.response; });
}, function (error) {
return chain(req._always, request).then(function () { return Promise.reject(request.errored || error); });
return chain(req._always, request)
.then(function () { return Promise.reject(request.errored || error); });
})

@@ -232,9 +259,2 @@ .then(function (response) {

}
function removeListeners(request) {
;
request._before = null;
request._after = null;
request._always = null;
request._progress = null;
}
function emitProgress(request) {

@@ -241,0 +261,0 @@ var fns = request._progress;

@@ -22,3 +22,2 @@ import Base, { BaseOptions, Headers, RawHeaders } from './base';

request: Request;
rawHeaders: RawHeaders;
constructor(options: ResponseOptions);

@@ -25,0 +24,0 @@ statusType(): number;

@@ -14,3 +14,2 @@ var __extends = (this && this.__extends) || function (d, b) {

this.statusText = options.statusText;
this.rawHeaders = options.rawHeaders;
}

@@ -17,0 +16,0 @@ Response.prototype.statusType = function () {

@@ -40,7 +40,7 @@ var test = require('blue-tape');

test('throw an error when no options are provided', function (t) {
t.throws(function () { return popsicle.default({}); }, /url must be a string/i);
t.throws(function () { return popsicle.request({}); }, /url must be a string/i);
t.end();
});
test('create a popsicle#Request instance', function (t) {
var req = popsicle.default('/');
var req = popsicle.request('/');
t.ok(req instanceof popsicle.Request);

@@ -56,4 +56,4 @@ return req.then(null, function () { });

t.ok(typeof res.url === 'string' || res.url == null);
t.ok(Array.isArray(res.rawHeaders));
t.equal(typeof res.headers, 'object');
t.equal(typeof res.headerNames, 'object');
t.equal(typeof res.status, 'number');

@@ -75,6 +75,20 @@ t.equal(typeof res.get, 'function');

});
test('clone a request instance', function (t) {
var req = popsicle.get(REMOTE_URL + '/echo/header/x-example');
req.use(function (self) {
self.before(function (req) {
req.set('X-Example', 'foobar');
});
});
return Promise.all([req, req.clone()])
.then(function (res) {
t.notEqual(res[0], res[1]);
t.equal(res[0].body, 'foobar');
t.equal(res[0].body, res[1].body);
});
});
test('methods', function (t) {
t.test('use node-style callbacks', function (t) {
t.plan(1);
return popsicle.default(REMOTE_URL + '/echo')
return popsicle.request(REMOTE_URL + '/echo')
.exec(function (err, res) {

@@ -87,3 +101,3 @@ t.ok(res instanceof popsicle.Response);

return Promise.all(SUPPORTED_METHODS.map(function (method) {
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/echo/method',

@@ -110,3 +124,3 @@ method: method

t.test('5xx', function (t) {
return popsicle.default(REMOTE_URL + '/error')
return popsicle.request(REMOTE_URL + '/error')
.then(function (res) {

@@ -121,3 +135,3 @@ t.equal(res.status, 500);

t.test('4xx', function (t) {
return popsicle.default(REMOTE_URL + '/not-found')
return popsicle.request(REMOTE_URL + '/not-found')
.then(function (res) {

@@ -132,3 +146,3 @@ t.equal(res.status, 404);

t.test('2xx', function (t) {
return popsicle.default(REMOTE_URL + '/no-content')
return popsicle.request(REMOTE_URL + '/no-content')
.then(function (res) {

@@ -145,3 +159,3 @@ t.equal(res.status, 204);

t.test('always send user agent', function (t) {
return popsicle.default(REMOTE_URL + '/echo/header/user-agent')
return popsicle.request(REMOTE_URL + '/echo/header/user-agent')
.then(function (res) {

@@ -156,3 +170,3 @@ var regexp = process.browser ?

t.test('send a custom user agent header', function (t) {
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/echo/header/user-agent',

@@ -171,3 +185,3 @@ headers: {

t.test('parse', function (t) {
return popsicle.default(REMOTE_URL + '/notfound')
return popsicle.request(REMOTE_URL + '/notfound')
.then(function (res) {

@@ -181,3 +195,3 @@ t.equal(res.type(), 'text/html');

t.test('send post data', function (t) {
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/echo',

@@ -200,3 +214,3 @@ method: 'POST',

t.test('should automatically send objects as json', function (t) {
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/echo',

@@ -212,3 +226,3 @@ method: 'POST',

t.test('should send as form encoded when header is set', function (t) {
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/echo',

@@ -249,3 +263,3 @@ method: 'POST',

t.ok(form instanceof FormData);
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/echo',

@@ -257,3 +271,3 @@ method: 'POST',

t.test('should stringify to form data when set as multipart', function (t) {
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/echo',

@@ -272,3 +286,3 @@ method: 'POST',

t.test('should stringify and send query parameters', function (t) {
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/echo/query',

@@ -282,3 +296,3 @@ query: EXAMPLE_BODY

t.test('should stringify and append to query object', function (t) {
var req = popsicle.default({
var req = popsicle.request({
url: REMOTE_URL + '/echo/query?query=true',

@@ -304,3 +318,3 @@ query: EXAMPLE_BODY

t.test('should accept query as a string', function (t) {
var req = popsicle.default({
var req = popsicle.request({
url: REMOTE_URL + '/echo/query',

@@ -320,3 +334,3 @@ query: 'query=true'

t.plan(3);
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/delay/1500',

@@ -334,3 +348,3 @@ timeout: 500

t.test('abort before it starts', function (t) {
var req = popsicle.default(REMOTE_URL + '/echo');
var req = popsicle.request(REMOTE_URL + '/echo');
req.abort();

@@ -346,3 +360,3 @@ t.plan(3);

t.test('abort mid-request', function (t) {
var req = popsicle.default(REMOTE_URL + '/download');
var req = popsicle.request(REMOTE_URL + '/download');
t.plan(3);

@@ -360,3 +374,3 @@ setTimeout(function () {

t.test('no side effects of aborting twice', function (t) {
var req = popsicle.default(REMOTE_URL + '/download');
var req = popsicle.request(REMOTE_URL + '/download');
t.plan(3);

@@ -376,3 +390,3 @@ req.abort();

t.test('download progress', function (t) {
var req = popsicle.default(REMOTE_URL + '/download');
var req = popsicle.request(REMOTE_URL + '/download');
t.plan(typeof window === 'undefined' ? 3 : 2);

@@ -393,3 +407,3 @@ t.equal(req.downloaded, 0);

t.test('emit progress events', function (t) {
var req = popsicle.default({
var req = popsicle.request({
url: REMOTE_URL + '/echo',

@@ -412,3 +426,3 @@ body: EXAMPLE_BODY,

t.test('error when the progress callback errors', function (t) {
var req = popsicle.default(REMOTE_URL + '/echo');
var req = popsicle.request(REMOTE_URL + '/echo');
t.plan(2);

@@ -428,3 +442,3 @@ req.progress(function () {

t.test('automatically parse json responses', function (t) {
return popsicle.default(REMOTE_URL + '/json')
return popsicle.request(REMOTE_URL + '/json')
.then(function (res) {

@@ -436,3 +450,3 @@ t.equal(res.type(), 'application/json');

t.test('automatically parse form encoded responses', function (t) {
return popsicle.default(REMOTE_URL + '/foo')
return popsicle.request(REMOTE_URL + '/foo')
.then(function (res) {

@@ -444,3 +458,3 @@ t.equal(res.type(), 'application/x-www-form-urlencoded');

t.test('disable automatic parsing', function (t) {
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/json',

@@ -455,3 +469,3 @@ use: popsicle.browser ? [] : [popsicle.plugins.concatStream('string')]

t.test('set non-parsable responses as null', function (t) {
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/echo',

@@ -465,3 +479,3 @@ method: 'post'

t.test('set body to null when json is empty', function (t) {
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/echo',

@@ -484,3 +498,3 @@ method: 'POST',

t.test('stream the response body', function (t) {
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/json',

@@ -500,3 +514,3 @@ use: []

t.test('pipe streams', function (t) {
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/echo',

@@ -510,3 +524,3 @@ body: fs.createReadStream(filename)

t.test('pipe streams into forms', function (t) {
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/echo',

@@ -530,3 +544,3 @@ body: popsicle.form({

t.test('unzip contents', function (t) {
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/echo/zip',

@@ -541,3 +555,3 @@ body: fs.createReadStream(filename)

t.test('unzip with gzip encoding', function (t) {
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/echo/zip',

@@ -557,3 +571,3 @@ body: fs.createReadStream(filename),

t.test('browser response type', function (t) {
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/text',

@@ -570,3 +584,3 @@ options: {

t.plan(2);
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/text',

@@ -587,3 +601,3 @@ options: {

t.plan(3);
return popsicle.default('http://fdahkfjhuehfakjbvdahjfds.fdsa')
return popsicle.request('http://fdahkfjhuehfakjbvdahjfds.fdsa')
.catch(function (err) {

@@ -597,3 +611,3 @@ t.ok(/Unable to connect/i.exec(err.message));

t.plan(3);
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/echo',

@@ -616,3 +630,3 @@ method: 'POST',

obj.obj = obj;
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/echo',

@@ -631,3 +645,3 @@ method: 'POST',

t.test('modify the request', function (t) {
var req = popsicle.default(REMOTE_URL + '/echo');
var req = popsicle.request(REMOTE_URL + '/echo');
t.plan(1);

@@ -643,3 +657,3 @@ req.use(function (self) {

t.test('run a function before opening the request', function (t) {
var req = popsicle.default(REMOTE_URL + '/echo');
var req = popsicle.request(REMOTE_URL + '/echo');
t.plan(2);

@@ -653,3 +667,3 @@ req.before(function (self) {

t.test('fail the request before starting', function (t) {
var req = popsicle.default(REMOTE_URL + '/echo');
var req = popsicle.request(REMOTE_URL + '/echo');
t.plan(1);

@@ -665,3 +679,3 @@ req.before(function () {

t.test('accept a promise to delay the request', function (t) {
var req = popsicle.default(REMOTE_URL + '/echo');
var req = popsicle.request(REMOTE_URL + '/echo');
t.plan(1);

@@ -681,3 +695,3 @@ req.before(function (self) {

t.test('run after the response', function (t) {
var req = popsicle.default(REMOTE_URL + '/echo');
var req = popsicle.request(REMOTE_URL + '/echo');
t.plan(4);

@@ -695,3 +709,3 @@ req.before(function () {

t.test('accept a promise', function (t) {
var req = popsicle.default(REMOTE_URL + '/echo');
var req = popsicle.request(REMOTE_URL + '/echo');
t.plan(1);

@@ -711,3 +725,3 @@ req.after(function (response) {

t.test('run all together in order', function (t) {
var req = popsicle.default(REMOTE_URL + '/echo');
var req = popsicle.request(REMOTE_URL + '/echo');
var before = false;

@@ -735,3 +749,3 @@ var after = false;

t.test('run on error', function (t) {
var req = popsicle.default(REMOTE_URL + '/echo');
var req = popsicle.request(REMOTE_URL + '/echo');
t.plan(2);

@@ -786,3 +800,3 @@ req.before(function () {

test('override request mechanism', function (t) {
return popsicle.default({
return popsicle.request({
url: '/foo',

@@ -808,3 +822,3 @@ transport: {

t.test('should follow 302 redirect with get', function (t) {
return popsicle.default(REMOTE_URL + '/redirect')
return popsicle.request(REMOTE_URL + '/redirect')
.then(function (res) {

@@ -836,3 +850,3 @@ t.equal(res.body, 'welcome get');

t.test('disable following redirects', function (t) {
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/redirect',

@@ -850,3 +864,3 @@ options: {

t.plan(2);
return popsicle.default(REMOTE_URL + '/redirect/6')
return popsicle.request(REMOTE_URL + '/redirect/6')
.catch(function (err) {

@@ -858,3 +872,3 @@ t.equal(err.message, 'Exceeded maximum of 5 redirects');

t.test('change maximum redirects', function (t) {
return popsicle.default({
return popsicle.request({
url: REMOTE_URL + '/redirect/6',

@@ -861,0 +875,0 @@ options: {

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

@@ -55,3 +55,3 @@ "main": "dist/common.js",

"devDependencies": {
"blue-tape": "^0.1.10",
"blue-tape": "^0.2.0",
"bluebird": "^3.0.5",

@@ -67,3 +67,3 @@ "body-parser": "^1.9.2",

"typescript": "^1.7.3",
"typings": "^0.5.2"
"typings": "^0.6.7"
},

@@ -70,0 +70,0 @@ "dependencies": {

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

* [Group](https://github.com/blakeembrey/popsicle-group) - Group requests and perform operations on them all at once
* [Proxy Agent](https://github.com/blakeembrey/popsicle-proxy-agent) - Enable HTTP(s) proxying under node (with environment variable support)

@@ -335,4 +336,6 @@ #### Creating Plugins

function prefix (url) {
return function (request) {
request.url = url + req.url
return function (self) {
self.before(function (req) {
request.url = url + req.url
})
}

@@ -354,2 +357,4 @@ }

**Tip:** Always use the lifecycle hooks, actions directly in `use` can not be re-used (E.g. with a request clone).
#### Checking The Environment

@@ -356,0 +361,0 @@

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

Sorry, the diff of this file is not supported yet

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