Socket
Socket
Sign inDemoInstall

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 11.0.5 to 12.0.0

dist/index.d.ts

23

dist/browser.d.ts

@@ -1,16 +0,15 @@

import { Request, CreateHeaders } from 'servie';
import { CreateBody } from 'servie/dist/body/browser';
export * from './transports/xhr';
import { Request } from "servie/dist/browser";
import { transport, XhrResponse } from "popsicle-transport-xhr";
import { toFetch } from "./common";
/**
* Universal request options.
* Expose browser components.
*/
export interface RequestOptions {
method?: string;
headers?: CreateHeaders;
trailer?: CreateHeaders | Promise<CreateHeaders>;
body?: CreateBody;
}
export { transport, Request, XhrResponse, toFetch };
/**
* Simple universal request creator.
* Browser standard middleware stack.
*/
export declare function request(url: string, options?: RequestOptions): Request;
export declare const middleware: (req: Request) => Promise<XhrResponse>;
/**
* Standard browser fetch interface.
*/
export declare const fetch: (input: string | Request, init?: import("servie/dist/common").CommonRequestOptions<import("servie/dist/browser").CreateBody> | undefined) => Promise<XhrResponse>;
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
const servie_1 = require("servie");
const browser_1 = require("servie/dist/body/browser");
// Use `xhr` transport in browsers.
__export(require("./transports/xhr"));
const browser_1 = require("servie/dist/browser");
exports.Request = browser_1.Request;
const popsicle_transport_xhr_1 = require("popsicle-transport-xhr");
exports.transport = popsicle_transport_xhr_1.transport;
exports.XhrResponse = popsicle_transport_xhr_1.XhrResponse;
const common_1 = require("./common");
exports.toFetch = common_1.toFetch;
/**
* Simple universal request creator.
* Browser standard middleware stack.
*/
function request(url, options = {}) {
const { method } = options;
const headers = servie_1.createHeaders(options.headers);
const body = browser_1.createBody(options.body);
const trailer = Promise.resolve(options.trailer).then(servie_1.createHeaders);
return new servie_1.Request({ url, method, headers, body, trailer });
}
exports.request = request;
exports.middleware = popsicle_transport_xhr_1.transport();
/**
* Standard browser fetch interface.
*/
exports.fetch = common_1.toFetch(exports.middleware, browser_1.Request);
//# sourceMappingURL=browser.js.map

@@ -1,12 +0,6 @@

import { Request, Response } from 'servie';
import { Middleware } from 'throwback';
import { Composed } from "throwback";
import { CommonRequest, CommonResponse } from "servie/dist/common";
/**
* Request normalization.
* Create a `fetch` like interface from middleware stack.
*/
export interface NormalizeRequestOptions {
upgradeInsecureRequests?: boolean;
}
/**
* Default header handling.
*/
export declare function normalizeRequest<T extends Request, U extends Response>(options?: NormalizeRequestOptions): Middleware<T, U>;
export declare function toFetch<T extends CommonRequest, U extends CommonResponse, A extends any[]>(middleware: Composed<T, U>, Request: new (...args: A) => T): (...args: A) => Promise<U>;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Default header handling.
* Create a `fetch` like interface from middleware stack.
*/
function normalizeRequest(options = {}) {
return function (req, next) {
// Remove headers that should not be created by the user.
req.headers.delete('Host');
// If we have no accept header set already, default to accepting
// everything. This is needed because otherwise Firefox defaults to
// an accept header of `html/xml`.
if (!req.headers.get('Accept')) {
req.headers.set('Accept', '*/*');
}
// Request a preference to upgrade to HTTPS.
if (options.upgradeInsecureRequests !== false && req.Url.protocol === 'http:') {
req.headers.set('Upgrade-Insecure-Requests', '1');
}
return next();
function toFetch(middleware, Request) {
function done() {
throw new TypeError("Invalid middleware stack, missing transport function");
}
return function fetch(...args) {
const req = args[0] instanceof Request ? args[0] : new Request(...args);
return middleware(req, done);
};
}
exports.normalizeRequest = normalizeRequest;
exports.toFetch = toFetch;
//# sourceMappingURL=common.js.map

@@ -1,21 +0,19 @@

import { Request, CreateHeaders } from 'servie';
import { CookieJar, Store } from 'tough-cookie';
import { CreateBody } from 'servie/dist/body/node';
export * from './transports/http';
import { Request } from "servie/dist/node";
import { transport, HttpResponse } from "popsicle-transport-http";
import { cookies } from "popsicle-cookie-jar";
import { contentEncoding } from "popsicle-content-encoding";
import { redirects } from "popsicle-redirects";
import { userAgent } from "popsicle-user-agent";
import { toFetch } from "./common";
/**
* Create a cookie jar instance.
* Expose node.js components.
*/
export declare function cookieJar(store?: Store): CookieJar;
export { contentEncoding, cookies, HttpResponse, redirects, Request, toFetch, transport, userAgent };
/**
* Universal request options.
* Node.js standard middleware stack.
*/
export interface RequestOptions {
method?: string;
headers?: CreateHeaders;
trailer?: CreateHeaders | Promise<CreateHeaders>;
body?: CreateBody;
}
export declare const middleware: import("throwback").Composed<Request, HttpResponse>;
/**
* Simple universal request creator.
* Standard node.js fetch interface.
*/
export declare function request(url: string, options?: RequestOptions): Request;
export declare const fetch: (input: string | Request, init?: import("servie/dist/common").CommonRequestOptions<import("servie/dist/node").CreateBody> | undefined) => Promise<HttpResponse>;
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
const servie_1 = require("servie");
const tough_cookie_1 = require("tough-cookie");
const node_1 = require("servie/dist/body/node");
// Use `http` transport by default.
__export(require("./transports/http"));
const node_1 = require("servie/dist/node");
exports.Request = node_1.Request;
const throwback_1 = require("throwback");
const popsicle_transport_http_1 = require("popsicle-transport-http");
exports.transport = popsicle_transport_http_1.transport;
exports.HttpResponse = popsicle_transport_http_1.HttpResponse;
const popsicle_cookie_jar_1 = require("popsicle-cookie-jar");
exports.cookies = popsicle_cookie_jar_1.cookies;
const popsicle_content_encoding_1 = require("popsicle-content-encoding");
exports.contentEncoding = popsicle_content_encoding_1.contentEncoding;
const popsicle_redirects_1 = require("popsicle-redirects");
exports.redirects = popsicle_redirects_1.redirects;
const popsicle_user_agent_1 = require("popsicle-user-agent");
exports.userAgent = popsicle_user_agent_1.userAgent;
const common_1 = require("./common");
exports.toFetch = common_1.toFetch;
/**
* Create a cookie jar instance.
* Node.js standard middleware stack.
*/
function cookieJar(store) {
return new tough_cookie_1.CookieJar(store);
}
exports.cookieJar = cookieJar;
exports.middleware = throwback_1.compose([
popsicle_user_agent_1.userAgent(),
popsicle_content_encoding_1.contentEncoding(),
// Redirects must happen around cookie support.
popsicle_redirects_1.redirects(throwback_1.compose([popsicle_cookie_jar_1.cookies(), popsicle_transport_http_1.transport()]))
]);
/**
* Simple universal request creator.
* Standard node.js fetch interface.
*/
function request(url, options = {}) {
const { method } = options;
const headers = servie_1.createHeaders(options.headers);
const body = node_1.createBody(options.body);
const trailer = Promise.resolve(options.trailer).then(servie_1.createHeaders);
return new servie_1.Request({ url, method, headers, body, trailer });
}
exports.request = request;
exports.fetch = common_1.toFetch(exports.middleware, node_1.Request);
//# sourceMappingURL=node.js.map
{
"name": "popsicle",
"version": "11.0.5",
"description": "Advanced HTTP requests in node.js and browsers, using Servie",
"version": "12.0.0",
"description": "Advanced HTTP requests in node.js and browsers",
"main": "dist/universal.js",

@@ -15,11 +15,10 @@ "types": "dist/universal.d.ts",

"scripts": {
"prettier": "prettier --write",
"lint": "tslint \"src/**/*.ts\" --project tsconfig.json",
"bundle": "browserify -g [ envify --NODE_ENV production ] -s popsicle -o popsicle.js .",
"bundle:size": "du -h popsicle.js",
"build": "rimraf dist/ && tsc && npm run bundle && npm run bundle:size",
"specs": "HTTPS_PORT=7358 PORT=7357 jest --coverage",
"server:open": "PORT=7357 node scripts/http-server.js & echo $! > server.pid; HTTPS_PORT=7358 node scripts/https-server.js & echo $! > https-server.pid",
"server:close": "if [ -f server.pid ]; then kill -9 $(cat server.pid); rm server.pid; fi; if [ -f https-server.pid ]; then kill -9 $(cat https-server.pid); rm https-server.pid; fi",
"test": "npm run lint && npm run build && npm run server:open && npm run specs; EXIT=$?; npm run server:close; exit $EXIT",
"prepare": "npm run build"
"format": "npm run prettier -- README.md \"src/**/*.{js,ts}\"",
"build": "rimraf dist && tsc",
"specs": "jest --coverage",
"test": "npm run -s lint && npm run -s build && npm run -s specs && npm run -s size",
"prepare": "npm run build",
"size": "size-limit"
},

@@ -66,26 +65,49 @@ "repository": {

},
"size-limit": [
{
"path": "./dist/index.js",
"limit": "2.7 kB"
}
],
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,json,css,md}": [
"npm run prettier",
"git add"
]
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@types/jest": "^23.3.5",
"@types/node": "^10.12.10",
"body-parser": "^1.18.3",
"@types/jest": "^24.0.11",
"@types/node": "^12.0.0",
"browserify": "^16.2.3",
"envify": "^4.1.0",
"express": "^4.16.3",
"jest": "^23.6.0",
"methods": "^1.1.2",
"husky": "^2.2.0",
"jest": "^24.6.0",
"lint-staged": "^8.1.6",
"prettier": "^1.17.0",
"rimraf": "^2.6.2",
"ts-jest": "^23.10.4",
"tslint": "^5.10.0",
"size-limit": "^1.3.1",
"ts-jest": "^24.0.1",
"tslint": "^5.16.0",
"tslint-config-prettier": "^1.18.0",
"tslint-config-standard": "^8.0.1",
"typescript": "^3.1.3"
"typescript": "^3.4.1"
},
"dependencies": {
"@types/pump": "^1.0.1",
"@types/tough-cookie": "^2.3.2",
"make-error-cause": "^2.1.0",
"pump": "^3.0.0",
"servie": "^3.2.3",
"throwback": "^4.0.0",
"tough-cookie": "^2.0.0"
"popsicle-content-encoding": "^1.0.0",
"popsicle-cookie-jar": "^1.0.0",
"popsicle-redirects": "^1.0.0",
"popsicle-transport-http": "^1.0.0",
"popsicle-transport-xhr": "^1.0.0",
"popsicle-user-agent": "^1.0.0",
"servie": "^4.0.6",
"throwback": "^4.1.0",
"tough-cookie": "^3.0.1"
}
}

@@ -20,63 +20,42 @@ # ![Popsicle](https://cdn.rawgit.com/serviejs/popsicle/master/logo.svg)

```js
import { transport, request } from 'popsicle'
import { fetch } from "popsicle";
const req = request('http://example.com') // Creates a `Request` instance.
const res = await transport()(req) // Transports `Request` and returns `Response` instance.
const req = fetch("http://example.com");
```
Thin wrapper to transport [Servie](https://github.com/serviejs/servie) HTTP request interfaces.
> Popsicle is a universal package, meaning node.js and browsers are supported without any configuration. This means the primary endpoint requires some `dom` types in TypeScript. When in a node.js or browser only environments prefer importing `popsicle/dist/{node,browser}` instead.
**P.S.** The default export from `popsicle` is `universal.js`. In TypeScript, this can cause trouble with types. Use specific imports such as `popsicle/dist/{browser,node,universal}` instead, depending on preference.
### [Browser](./src/browser.ts)
### Node.js Normalization
The middleware stack for browsers contains _only_ the transport layer. This makes the package tiny and quick on browsers.
Normalizes some behavior that happens automatically in browsers (each normalization can be disabled).
### [Node.js](./src/node.ts)
* Default `User-Agent` insertion
* Default unzip behaviour of `gzip` and `deflate` encoding
* Follows HTTP redirects
The middleware stack for node.js includes a lot more normalization to act similar to browsers:
### Built-in Transports
- Default `User-Agent` initialization
- Default decoding of compressed responses
- Follows valid HTTP redirects
- Caches cookies in-memory
#### HTTP (node.js)
### Errors
* **unzip: boolean** Automatically unzip response bodies (default: `true`)
* **follow: boolean** Automatically follow HTTP redirects (default: `true`)
* **jar: CookieJar** An instance of a cookie jar (`jar()` from `node.js` import)
* **maxRedirects: number** Override the number of redirects allowed (default: `5`)
* **confirmRedirect: Function** Validate `307` and `308` redirects (default: `() => false`)
* **rejectUnauthorized: boolean** Reject invalid SSL certificates (default: `true`)
* **agent: http.Agent** Custom `http.request` agent
* **ca: string | Buffer** A string, `Buffer` or array of strings or `Buffers` of trusted certificates in PEM format
* **key: string | Buffer** Private key to use for SSL
* **cert: string | Buffer** Public x509 certificate to use
* **secureProtocol: string** Optional SSL method to use
Transports can return an error. The built-in codes are documented below:
#### XHR (browsers)
- **EUNAVAILABLE** Unable to connect to the remote URL
- **EINVALID** Request URL is invalid (browsers)
- **EMAXREDIRECTS** Maximum number of redirects exceeded (node.js)
- **EBLOCKED** The request was blocked (HTTPS -> HTTP) (browsers)
- **ECSP** Request violates the documents Content Security Policy (browsers)
- **ETYPE** Invalid transport type (browsers)
* **type: XMLHttpRequestResponseType** Handle the XHR response (default: `text`)
* **withCredentials: boolean** Send cookies with CORS requests (default: `false`)
* **overrideMimeType: string** Override the XHR response MIME type
### Customization
#### Errors
Build the functionality you require by composing middleware functions and using `toFetch`. See [`src/node.ts`](./src/node.ts) for an example.
Transports can return an error. Errors have a `request` property set to the request object and a `code` string. The built-in codes are documented below:
## Plugins
* **EUNAVAILABLE** Unable to connect to the remote URL
* **EINVALID** Request URL is invalid (browsers)
* **EMAXREDIRECTS** Maximum number of redirects exceeded (node.js)
* **EBLOCKED** The request was blocked (HTTPS -> HTTP) (browsers)
* **ECSP** Request violates the documents Content Security Policy (browsers)
* **ETYPE** Invalid transport type (browsers)
- [Popsicle Status](https://github.com/serviejs/popsicle-status) - Reject on invalid HTTP status codes
- [Popsicle Retry](https://github.com/serviejs/popsicle-retry) - Retry HTTP requests on bad server responses
### Plugins
_Coming back soon._
### Helpful Utilities
* [`throat`](https://github.com/ForbesLindesay/throat) - Throttle promise-based functions with concurrency support
* [`is-browser`](https://github.com/ForbesLindesay/is-browser) - Check if your in a browser environment (E.g. Browserify, Webpack)
* [`parse-link-header`](https://github.com/thlorenz/parse-link-header) - Handy for parsing HTTP link headers
### Creating Plugins

@@ -87,13 +66,8 @@

```ts
type Plugin = (req: Request, next: () => Promise<Response>) => Promise<Response>
type Plugin = (
req: Request,
next: () => Promise<Response>
) => Promise<Response>;
```
### Transportation Layers
See [Servie](https://github.com/serviejs/servie#implementers) for more information:
```ts
type Transport = (req: Request) => Promise<Response>
```
## TypeScript

@@ -105,5 +79,5 @@

* [Superagent](https://github.com/visionmedia/superagent) - HTTP requests for node and browsers
* [Fetch](https://github.com/github/fetch) - Browser polyfill for promise-based HTTP requests
* [Axios](https://github.com/mzabriskie/axios) - HTTP request API based on Angular's `$http` service
- [Superagent](https://github.com/visionmedia/superagent) - HTTP requests for node and browsers
- [Fetch](https://github.com/github/fetch) - Browser polyfill for promise-based HTTP requests
- [Axios](https://github.com/mzabriskie/axios) - HTTP request API based on Angular's `$http` service

@@ -110,0 +84,0 @@ ## License

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