Comparing version 6.2.2 to 7.0.0
@@ -23,9 +23,10 @@ import { Url } from 'url'; | ||
toHeaders(): Headers; | ||
set(name: string, value: string | string[]): Base; | ||
append(name: string, value: string | string[]): this; | ||
set(name: string, value?: string | string[]): this; | ||
append(name: string, value?: string | string[]): this; | ||
name(name: string): string; | ||
get(name: string): string; | ||
getAll(name: string): string[]; | ||
remove(name: string): this; | ||
type(): string; | ||
type(value: string): Base; | ||
type(value: string): this; | ||
} |
@@ -13,3 +13,3 @@ "use strict"; | ||
function type(str) { | ||
return str == null ? null : str.split(/ *; */)[0]; | ||
return str == null ? null : str.split(/ *; */, 1)[0]; | ||
} | ||
@@ -103,8 +103,12 @@ function concat(a, b) { | ||
for (var _i = 0, value_1 = value; _i < value_1.length; _i++) { | ||
var v = value_1[_i]; | ||
this.rawHeaders.push(name, v); | ||
var val = value_1[_i]; | ||
if (val != null) { | ||
this.rawHeaders.push(name, val); | ||
} | ||
} | ||
} | ||
else { | ||
this.rawHeaders.push(name, value); | ||
if (value != null) { | ||
this.rawHeaders.push(name, value); | ||
} | ||
} | ||
@@ -125,10 +129,18 @@ return this; | ||
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 this.rawHeaders[i + 1]; | ||
} | ||
} | ||
return value; | ||
}; | ||
Base.prototype.getAll = function (name) { | ||
var lowered = lowerHeader(name); | ||
var result = []; | ||
for (var i = 0; i < this.rawHeaders.length; i += 2) { | ||
if (lowerHeader(this.rawHeaders[i]) === lowered) { | ||
result.push(this.rawHeaders[i + 1]); | ||
} | ||
} | ||
return result; | ||
}; | ||
Base.prototype.remove = function (name) { | ||
@@ -135,0 +147,0 @@ var lowered = lowerHeader(name); |
import Promise = require('any-promise'); | ||
import Request from './request'; | ||
import { defaults as use } from './plugins/index'; | ||
export { open, abort, use }; | ||
declare function open(request: Request): Promise<{}>; | ||
declare function abort(request: Request): void; | ||
import Response from './response'; | ||
import { TextTypes } from './utils'; | ||
export declare type Types = 'document' | 'blob' | 'arraybuffer' | TextTypes | string; | ||
export interface Options { | ||
type?: Types; | ||
withCredentials?: boolean; | ||
overrideMimeType?: string; | ||
} | ||
export declare function createTransport(options: Options): { | ||
use: ((request: Request, next: () => Promise<Response>) => Promise<Response>)[]; | ||
abort: (request: Request) => void; | ||
open(request: Request): Promise<Response>; | ||
}; |
"use strict"; | ||
var Promise = require('any-promise'); | ||
var response_1 = require('./response'); | ||
var index_1 = require('./plugins/index'); | ||
exports.use = index_1.defaults; | ||
function open(request) { | ||
var utils_1 = require('./utils'); | ||
function createTransport(options) { | ||
return { | ||
use: use, | ||
abort: abort, | ||
open: function (request) { | ||
return handle(request, options); | ||
} | ||
}; | ||
} | ||
exports.createTransport = createTransport; | ||
var use = [index_1.stringify(), index_1.headers()]; | ||
function handle(request, options) { | ||
return new Promise(function (resolve, reject) { | ||
var type = options.type || 'text'; | ||
var url = request.url, method = request.method; | ||
var responseType = request.options.responseType; | ||
var isText = utils_1.textTypes.indexOf(type) > -1; | ||
if (window.location.protocol === 'https:' && /^http\:/.test(url)) { | ||
@@ -13,14 +26,15 @@ return reject(request.error("The request to \"" + url + "\" was blocked", 'EBLOCKED')); | ||
var xhr = request._raw = new XMLHttpRequest(); | ||
xhr.onload = function () { | ||
return resolve({ | ||
status: xhr.status === 1223 ? 204 : xhr.status, | ||
statusText: xhr.statusText, | ||
rawHeaders: parseToRawHeaders(xhr.getAllResponseHeaders()), | ||
body: responseType ? xhr.response : xhr.responseText, | ||
url: xhr.responseURL | ||
function done() { | ||
return new Promise(function (resolve) { | ||
return resolve(new response_1.default({ | ||
status: xhr.status === 1223 ? 204 : xhr.status, | ||
statusText: xhr.statusText, | ||
rawHeaders: parseToRawHeaders(xhr.getAllResponseHeaders()), | ||
body: isText ? utils_1.parse(request, xhr.responseText, type) : xhr.response, | ||
url: xhr.responseURL | ||
})); | ||
}); | ||
}; | ||
xhr.onabort = function () { | ||
return reject(request.error('Request aborted', 'EABORT')); | ||
}; | ||
} | ||
xhr.onload = function () { return resolve(done()); }; | ||
xhr.onabort = function () { return resolve(done()); }; | ||
xhr.onerror = function () { | ||
@@ -33,7 +47,8 @@ return reject(request.error("Unable to connect to \"" + request.url + "\"", 'EUNAVAILABLE')); | ||
} | ||
request.downloadedBytes = e.loaded; | ||
request._setDownloadedBytes(e.loaded); | ||
}; | ||
xhr.upload.onloadend = function () { return request.downloaded = 1; }; | ||
if (method === 'GET' || method === 'HEAD' || !xhr.upload) { | ||
request.uploadLength = 0; | ||
request.uploadedBytes = 0; | ||
request._setUploadedBytes(0, 1); | ||
} | ||
@@ -45,4 +60,5 @@ else { | ||
} | ||
request.uploadedBytes = e.loaded; | ||
request._setUploadedBytes(e.loaded); | ||
}; | ||
xhr.upload.onloadend = function () { return request.uploaded = 1; }; | ||
} | ||
@@ -55,12 +71,15 @@ try { | ||
} | ||
if (request.options.withCredentials) { | ||
if (options.withCredentials) { | ||
xhr.withCredentials = true; | ||
} | ||
if (responseType) { | ||
if (options.overrideMimeType) { | ||
xhr.overrideMimeType(options.overrideMimeType); | ||
} | ||
if (!isText) { | ||
try { | ||
xhr.responseType = responseType; | ||
xhr.responseType = type; | ||
} | ||
finally { | ||
if (xhr.responseType !== responseType) { | ||
throw request.error("Unsupported response type: " + responseType, 'ERESPONSETYPE'); | ||
if (xhr.responseType !== type) { | ||
return reject(request.error("Unsupported type: " + type, 'ETYPE')); | ||
} | ||
@@ -75,16 +94,16 @@ } | ||
} | ||
exports.open = open; | ||
function abort(request) { | ||
request._raw.abort(); | ||
} | ||
exports.abort = abort; | ||
function parseToRawHeaders(headers) { | ||
var rawHeaders = []; | ||
var lines = headers.replace(/\r?\n$/, '').split(/\r?\n/); | ||
var lines = headers.split(/\r?\n/); | ||
for (var _i = 0, lines_1 = lines; _i < lines_1.length; _i++) { | ||
var header = lines_1[_i]; | ||
var indexOf = header.indexOf(':'); | ||
var name_1 = header.substr(0, indexOf).trim(); | ||
var value = header.substr(indexOf + 1).trim(); | ||
rawHeaders.push(name_1, value); | ||
var line = lines_1[_i]; | ||
if (line) { | ||
var indexOf = line.indexOf(':'); | ||
var name_1 = line.substr(0, indexOf).trim(); | ||
var value = line.substr(indexOf + 1).trim(); | ||
rawHeaders.push(name_1, value); | ||
} | ||
} | ||
@@ -91,0 +110,0 @@ return rawHeaders; |
@@ -7,5 +7,4 @@ import Request, { RequestOptions, DefaultsOptions } from './request'; | ||
import PopsicleError from './error'; | ||
import * as transport from './index'; | ||
import { createTransport } from './index'; | ||
export declare function defaults(defaultsOptions: DefaultsOptions): (options: RequestOptions | string) => Request; | ||
export declare const browser: boolean; | ||
export declare const request: (options: RequestOptions | string) => Request; | ||
@@ -18,3 +17,3 @@ export declare const get: (options: RequestOptions | string) => Request; | ||
export declare const head: (options: RequestOptions | string) => Request; | ||
export { Request, Response, PopsicleError, plugins, form, jar, transport }; | ||
export { Request, Response, PopsicleError, plugins, form, jar, createTransport }; | ||
export default request; |
@@ -15,5 +15,6 @@ "use strict"; | ||
exports.PopsicleError = error_1.default; | ||
var transport = require('./index'); | ||
exports.transport = transport; | ||
var index_1 = require('./index'); | ||
exports.createTransport = index_1.createTransport; | ||
function defaults(defaultsOptions) { | ||
var transport = index_1.createTransport({ type: 'text' }); | ||
var defaults = extend({ transport: transport }, defaultsOptions); | ||
@@ -35,3 +36,2 @@ return function popsicle(options) { | ||
exports.defaults = defaults; | ||
exports.browser = !!process.browser; | ||
exports.request = defaults({}); | ||
@@ -38,0 +38,0 @@ exports.get = defaults({ method: 'get' }); |
@@ -0,6 +1,25 @@ | ||
import { IncomingMessage, ClientRequest } from 'http'; | ||
import Promise = require('any-promise'); | ||
import Request from './request'; | ||
import { defaults as use } from './plugins/index'; | ||
export { open, abort, use }; | ||
declare function open(request: Request): Promise<{}>; | ||
declare function abort(request: Request): void; | ||
import Response from './response'; | ||
import { TextTypes } from './utils'; | ||
export declare type Types = 'buffer' | 'array' | 'uint8array' | 'stream' | TextTypes | string; | ||
export interface Options { | ||
type?: Types; | ||
unzip?: boolean; | ||
jar?: any; | ||
agent?: any; | ||
maxRedirects?: number; | ||
rejectUnauthorized?: boolean; | ||
followRedirects?: boolean; | ||
confirmRedirect?: (request: ClientRequest, response: IncomingMessage) => boolean; | ||
ca?: string | Buffer | Array<string | Buffer>; | ||
cert?: string | Buffer; | ||
key?: string | Buffer; | ||
maxBufferSize?: number; | ||
} | ||
export declare function createTransport(options: Options): { | ||
use: ((request: Request, next: () => Promise<Response>) => Promise<Response>)[]; | ||
abort: (request: Request) => void; | ||
open(request: Request): Promise<{}>; | ||
}; |
@@ -7,5 +7,20 @@ "use strict"; | ||
var arrify = require('arrify'); | ||
var concat = require('concat-stream'); | ||
var Promise = require('any-promise'); | ||
var zlib_1 = require('zlib'); | ||
var response_1 = require('./response'); | ||
var index_1 = require('./plugins/index'); | ||
exports.use = index_1.defaults; | ||
var utils_1 = require('./utils'); | ||
var validTypes = ['buffer', 'array', 'uint8array', 'stream'].concat(utils_1.textTypes); | ||
function createTransport(options) { | ||
return { | ||
use: use, | ||
abort: abort, | ||
open: function (request) { | ||
return handle(request, options); | ||
} | ||
}; | ||
} | ||
exports.createTransport = createTransport; | ||
var use = [index_1.stringify(), index_1.headers()]; | ||
var REDIRECT_TYPE; | ||
@@ -25,10 +40,17 @@ (function (REDIRECT_TYPE) { | ||
}; | ||
function open(request) { | ||
var url = request.url, method = request.method, body = request.body, options = request.options; | ||
function handle(request, options) { | ||
var followRedirects = options.followRedirects, type = options.type, unzip = options.unzip, rejectUnauthorized = options.rejectUnauthorized, ca = options.ca, key = options.key, cert = options.cert, agent = options.agent; | ||
var url = request.url, method = request.method, body = request.body; | ||
var maxRedirects = num(options.maxRedirects, 5); | ||
var followRedirects = options.followRedirects !== false; | ||
var maxBufferSize = num(options.maxBufferSize, type === 'stream' ? Infinity : 2 * 1000 * 1000); | ||
var storeCookies = getStoreCookies(request, options); | ||
var attachCookies = getAttachCookies(request, options); | ||
var confirmRedirect = options.confirmRedirect || falsey; | ||
var requestCount = 0; | ||
var isStreaming = false; | ||
var confirmRedirect = typeof options.followRedirects === 'function' ? | ||
options.followRedirects : falsey; | ||
if (type && validTypes.indexOf(type) === -1) { | ||
return Promise.reject(request.error("Unsupported type: " + type, 'ETYPE')); | ||
} | ||
if (unzip !== false && request.get('Accept-Encoding') == null) { | ||
request.set('Accept-Encoding', 'gzip,deflate'); | ||
} | ||
function get(url, method, body) { | ||
@@ -38,3 +60,3 @@ if (requestCount++ > maxRedirects) { | ||
} | ||
return appendCookies(request) | ||
return attachCookies(url) | ||
.then(function () { | ||
@@ -47,29 +69,36 @@ return new Promise(function (resolve, reject) { | ||
arg.headers = request.toHeaders(); | ||
arg.agent = options.agent; | ||
arg.rejectUnauthorized = options.rejectUnauthorized !== false; | ||
arg.ca = options.ca; | ||
arg.cert = options.cert; | ||
arg.key = options.key; | ||
arg.agent = agent; | ||
arg.rejectUnauthorized = rejectUnauthorized !== false; | ||
arg.ca = ca; | ||
arg.cert = cert; | ||
arg.key = key; | ||
var rawRequest = engine(arg); | ||
var requestStream = new stream_1.PassThrough(); | ||
var responseStream = new stream_1.PassThrough(); | ||
var uploadedBytes = 0; | ||
var downloadedBytes = 0; | ||
requestStream.on('data', function (chunk) { | ||
request.uploadedBytes += chunk.length; | ||
uploadedBytes += chunk.length; | ||
request._setUploadedBytes(uploadedBytes); | ||
}); | ||
requestStream.on('end', function () { | ||
request.uploadedBytes = request.uploadLength; | ||
request._setUploadedBytes(uploadedBytes, 1); | ||
}); | ||
responseStream.on('data', function (chunk) { | ||
request.downloadedBytes += chunk.length; | ||
downloadedBytes += chunk.length; | ||
request._setDownloadedBytes(downloadedBytes); | ||
if (downloadedBytes > maxBufferSize) { | ||
rawRequest.abort(); | ||
responseStream.emit('error', request.error('Response too large', 'ETOOLARGE')); | ||
} | ||
}); | ||
responseStream.on('end', function () { | ||
request.downloadedBytes = request.downloadLength; | ||
request._setDownloadedBytes(downloadedBytes, 1); | ||
}); | ||
function response(rawResponse) { | ||
var status = rawResponse.statusCode; | ||
function response(incomingMessage) { | ||
var headers = incomingMessage.headers, rawHeaders = incomingMessage.rawHeaders, status = incomingMessage.statusCode, statusText = incomingMessage.statusMessage; | ||
var redirect = REDIRECT_STATUS[status]; | ||
if (followRedirects && redirect != null && rawResponse.headers.location) { | ||
var newUrl = urlLib.resolve(url, rawResponse.headers.location); | ||
rawResponse.resume(); | ||
request.remove('Cookie'); | ||
if (followRedirects !== false && redirect != null && headers.location) { | ||
var newUrl = urlLib.resolve(url, headers.location); | ||
incomingMessage.resume(); | ||
if (redirect === REDIRECT_TYPE.FOLLOW_WITH_GET) { | ||
@@ -83,3 +112,3 @@ request.set('Content-Length', '0'); | ||
} | ||
if (confirmRedirect(rawRequest, rawResponse)) { | ||
if (confirmRedirect(rawRequest, incomingMessage)) { | ||
return get(newUrl, method, body); | ||
@@ -89,12 +118,14 @@ } | ||
} | ||
request.downloadLength = num(rawResponse.headers['content-length'], 0); | ||
isStreaming = true; | ||
rawResponse.pipe(responseStream); | ||
return Promise.resolve({ | ||
body: responseStream, | ||
status: status, | ||
statusText: rawResponse.statusMessage, | ||
headers: rawResponse.headers, | ||
rawHeaders: rawResponse.rawHeaders, | ||
url: url | ||
request.downloadLength = num(headers['content-length'], null); | ||
incomingMessage.pipe(responseStream); | ||
return handleResponse(request, responseStream, headers, options) | ||
.then(function (body) { | ||
return new response_1.default({ | ||
status: status, | ||
headers: headers, | ||
statusText: statusText, | ||
rawHeaders: rawHeaders, | ||
body: body, | ||
url: url | ||
}); | ||
}); | ||
@@ -104,26 +135,18 @@ } | ||
rawRequest.abort(); | ||
if (isStreaming) { | ||
responseStream.emit('error', error); | ||
} | ||
else { | ||
reject(error); | ||
} | ||
reject(error); | ||
} | ||
rawRequest.once('response', function (message) { | ||
resolve(setCookies(request, url, message).then(function () { return response(message); })); | ||
rawRequest.on('response', function (message) { | ||
resolve(storeCookies(url, message.headers).then(function () { return response(message); })); | ||
}); | ||
rawRequest.once('error', function (error) { | ||
rawRequest.on('error', function (error) { | ||
emitError(request.error("Unable to connect to \"" + url + "\"", 'EUNAVAILABLE', error)); | ||
}); | ||
rawRequest.once('clientAborted', function () { | ||
emitError(request.error('Request aborted', 'EABORT')); | ||
}); | ||
request._raw = rawRequest; | ||
request.uploadLength = num(rawRequest.getHeader('content-length'), 0); | ||
request.uploadLength = num(rawRequest.getHeader('content-length'), null); | ||
requestStream.pipe(rawRequest); | ||
requestStream.once('error', emitError); | ||
requestStream.on('error', emitError); | ||
if (body) { | ||
if (typeof body.pipe === 'function') { | ||
body.pipe(requestStream); | ||
body.once('error', emitError); | ||
body.on('error', emitError); | ||
} | ||
@@ -142,8 +165,5 @@ else { | ||
} | ||
exports.open = open; | ||
function abort(request) { | ||
request._raw.emit('clientAborted'); | ||
request._raw.abort(); | ||
} | ||
exports.abort = abort; | ||
function num(value, fallback) { | ||
@@ -158,30 +178,36 @@ if (value == null) { | ||
} | ||
function appendCookies(request) { | ||
return new Promise(function (resolve, reject) { | ||
if (!request.options.jar) { | ||
return resolve(); | ||
} | ||
request.options.jar.getCookies(request.url, function (err, cookies) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
if (cookies.length) { | ||
request.append('Cookie', cookies.join('; ')); | ||
} | ||
return resolve(); | ||
function getAttachCookies(request, options) { | ||
var jar = options.jar; | ||
var cookie = request.get('Cookie'); | ||
if (!jar) { | ||
return function () { return Promise.resolve(); }; | ||
} | ||
return function (url) { | ||
return new Promise(function (resolve, reject) { | ||
request.set('Cookie', cookie); | ||
options.jar.getCookies(url, function (err, cookies) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
if (cookies.length) { | ||
request.append('Cookie', cookies.join('; ')); | ||
} | ||
return resolve(); | ||
}); | ||
}); | ||
}); | ||
}; | ||
} | ||
function setCookies(request, url, message) { | ||
return new Promise(function (resolve, reject) { | ||
if (!request.options.jar) { | ||
return resolve(); | ||
} | ||
var cookies = arrify(message.headers['set-cookie']); | ||
function getStoreCookies(request, options) { | ||
var jar = options.jar; | ||
if (!jar) { | ||
return function () { return Promise.resolve(); }; | ||
} | ||
return function (url, headers) { | ||
var cookies = arrify(headers['set-cookie']); | ||
if (!cookies.length) { | ||
return resolve(); | ||
return Promise.resolve(); | ||
} | ||
var setCookies = cookies.map(function (cookie) { | ||
var storeCookies = cookies.map(function (cookie) { | ||
return new Promise(function (resolve, reject) { | ||
request.options.jar.setCookie(cookie, url, { ignoreError: true }, function (err) { | ||
jar.setCookie(cookie, url, { ignoreError: true }, function (err) { | ||
return err ? reject(err) : resolve(); | ||
@@ -191,5 +217,32 @@ }); | ||
}); | ||
return resolve(Promise.all(setCookies)); | ||
return Promise.all(storeCookies); | ||
}; | ||
} | ||
function handleResponse(request, stream, headers, options) { | ||
var type = options.type || 'text'; | ||
var unzip = options.unzip !== false; | ||
var isText = utils_1.textTypes.indexOf(type) > -1; | ||
var result = new Promise(function (resolve, reject) { | ||
if (unzip) { | ||
var enc = headers['content-encoding']; | ||
if (enc === 'deflate' || enc === 'gzip') { | ||
var unzip_1 = zlib_1.createUnzip(); | ||
stream.pipe(unzip_1); | ||
stream.on('error', function (err) { return unzip_1.emit('error', err); }); | ||
stream = unzip_1; | ||
} | ||
} | ||
if (type === 'stream') { | ||
return resolve(stream); | ||
} | ||
var encoding = isText ? 'string' : type; | ||
var concatStream = concat({ encoding: encoding }, resolve); | ||
stream.on('error', reject); | ||
stream.pipe(concatStream); | ||
}); | ||
if (isText) { | ||
return result.then(function (str) { return utils_1.parse(request, str, type); }); | ||
} | ||
return result; | ||
} | ||
//# sourceMappingURL=index.js.map |
export * from './common'; | ||
import { Middleware } from '../request'; | ||
export declare const defaults: Middleware[]; |
@@ -6,4 +6,2 @@ "use strict"; | ||
__export(require('./common')); | ||
var common_2 = require('./common'); | ||
exports.defaults = [common_2.stringify(), common_2.headers(), common_2.parse()]; | ||
//# sourceMappingURL=browser.js.map |
@@ -7,2 +7,1 @@ import Promise = require('any-promise'); | ||
export declare const stringify: () => (request: Request, next: () => Promise<Response>) => Promise<Response>; | ||
export declare const parse: () => (request: Request, next: () => Promise<Response>) => Promise<{}>; |
@@ -54,28 +54,2 @@ "use strict"; | ||
}); | ||
exports.parse = wrap(function (request, next) { | ||
return next() | ||
.then(function (response) { | ||
var body = response.body; | ||
if (typeof body !== 'string') { | ||
return response; | ||
} | ||
if (body === '') { | ||
response.body = null; | ||
return response; | ||
} | ||
var type = response.type(); | ||
try { | ||
if (JSON_MIME_REGEXP.test(type)) { | ||
response.body = body === '' ? null : JSON.parse(body); | ||
} | ||
else if (QUERY_MIME_REGEXP.test(type)) { | ||
response.body = querystring_1.parse(body); | ||
} | ||
} | ||
catch (err) { | ||
return Promise.reject(request.error('Unable to parse response body: ' + err.message, 'EPARSE', err)); | ||
} | ||
return response; | ||
}); | ||
}); | ||
//# sourceMappingURL=common.js.map |
import Promise = require('any-promise'); | ||
import Request from '../request'; | ||
import Response from '../response'; | ||
export * from './common'; | ||
import Request, { Middleware } from '../request'; | ||
import Response from '../response'; | ||
export declare const unzip: () => (request: Request, next: () => Promise<Response>) => Promise<Response>; | ||
export declare function concatStream(encoding: string): (request: Request, next: () => Promise<Response>) => Promise<{}>; | ||
export declare function headers(): (request: Request, next: () => Promise<Response>) => Promise<Response>; | ||
export declare const defaults: Middleware[]; |
@@ -5,41 +5,8 @@ "use strict"; | ||
} | ||
var concat = require('concat-stream'); | ||
var FormData = require('form-data'); | ||
var zlib_1 = require('zlib'); | ||
var Promise = require('any-promise'); | ||
var common_1 = require('./common'); | ||
__export(require('./common')); | ||
var common_2 = require('./common'); | ||
exports.unzip = common_2.wrap(function (request, next) { | ||
if (!request.get('Accept-Encoding')) { | ||
request.set('Accept-Encoding', 'gzip,deflate'); | ||
} | ||
return next() | ||
.then(function (response) { | ||
var enc = response.get('Content-Encoding'); | ||
if (enc === 'gzip' || enc === 'deflate') { | ||
var unzip_1 = zlib_1.createUnzip(); | ||
response.body.pipe(unzip_1); | ||
response.body = unzip_1; | ||
} | ||
return response; | ||
}); | ||
}); | ||
function concatStream(encoding) { | ||
return function (request, next) { | ||
return next() | ||
.then(function (response) { | ||
return new Promise(function (resolve, reject) { | ||
var stream = concat({ encoding: encoding }, function (data) { | ||
response.body = data; | ||
return resolve(response); | ||
}); | ||
response.body.on('error', reject); | ||
response.body.pipe(stream); | ||
}); | ||
}); | ||
}; | ||
} | ||
exports.concatStream = concatStream; | ||
function headers() { | ||
var common = common_2.headers(); | ||
var common = common_1.headers(); | ||
return function (request, next) { | ||
@@ -93,3 +60,2 @@ return common(request, function () { | ||
exports.headers = headers; | ||
exports.defaults = [common_2.stringify(), headers(), common_2.parse(), concatStream('string'), exports.unzip()]; | ||
//# sourceMappingURL=index.js.map |
import Promise = require('any-promise'); | ||
import Base, { BaseOptions, Headers } from './base'; | ||
import Response, { ResponseOptions } from './response'; | ||
import Response from './response'; | ||
import PopsicleError from './error'; | ||
@@ -10,3 +10,2 @@ export interface DefaultsOptions extends BaseOptions { | ||
body?: any; | ||
options?: any; | ||
use?: Middleware[]; | ||
@@ -24,8 +23,7 @@ progress?: ProgressFunction[]; | ||
timeout: number; | ||
options: any; | ||
method: string; | ||
} | ||
export interface TransportOptions { | ||
open: OpenHandler; | ||
abort?: AbortHandler; | ||
open: (request: Request) => Promise<Response>; | ||
abort?: (request: Request) => any; | ||
use?: Middleware[]; | ||
@@ -35,4 +33,2 @@ } | ||
export declare type ProgressFunction = (request: Request) => any; | ||
export declare type OpenHandler = (request: Request) => Promise<ResponseOptions>; | ||
export declare type AbortHandler = (request: Request) => any; | ||
export default class Request extends Base implements Promise<Response> { | ||
@@ -42,3 +38,2 @@ method: string; | ||
body: any; | ||
options: any; | ||
transport: TransportOptions; | ||
@@ -48,6 +43,8 @@ middleware: Middleware[]; | ||
aborted: boolean; | ||
uploaded: number; | ||
downloaded: number; | ||
uploadedBytes: number; | ||
downloadedBytes: number; | ||
uploadLength: number; | ||
downloadLength: number; | ||
private _uploadedBytes; | ||
private _downloadedBytes; | ||
_raw: any; | ||
@@ -71,9 +68,7 @@ _progress: ProgressFunction[]; | ||
private _handle(); | ||
uploaded: number; | ||
downloaded: number; | ||
completed: number; | ||
completedBytes: number; | ||
totalBytes: number; | ||
uploadedBytes: number; | ||
downloadedBytes: number; | ||
_setUploadedBytes(bytes: number, uploaded?: number): void; | ||
_setDownloadedBytes(bytes: number, downloaded?: number): void; | ||
} |
@@ -12,3 +12,2 @@ "use strict"; | ||
var base_1 = require('./base'); | ||
var response_1 = require('./response'); | ||
var error_1 = require('./error'); | ||
@@ -23,6 +22,8 @@ var Request = (function (_super) { | ||
this.aborted = false; | ||
this.uploaded = 0; | ||
this.downloaded = 0; | ||
this.uploadedBytes = null; | ||
this.downloadedBytes = null; | ||
this.uploadLength = null; | ||
this.downloadLength = null; | ||
this._uploadedBytes = null; | ||
this._downloadedBytes = null; | ||
this._progress = []; | ||
@@ -32,3 +33,2 @@ this.timeout = (options.timeout | 0); | ||
this.body = options.body; | ||
this.options = extend(options.options); | ||
var promised = new Promise(function (resolve, reject) { | ||
@@ -38,11 +38,10 @@ _this._resolve = resolve; | ||
}); | ||
this._promise = new Promise(function (resolve) { | ||
process.nextTick(function () { | ||
var handle = throwback_1.compose(_this.middleware); | ||
var cb = function () { | ||
_this._handle(); | ||
return promised; | ||
}; | ||
return resolve(handle(_this, cb)); | ||
}); | ||
this._promise = Promise.resolve() | ||
.then(function () { | ||
var run = throwback_1.compose(_this.middleware); | ||
var cb = function () { | ||
_this._handle(); | ||
return promised; | ||
}; | ||
return run(_this, cb); | ||
}); | ||
@@ -71,3 +70,2 @@ this.transport = extend(options.transport); | ||
method: this.method, | ||
options: this.options, | ||
body: this.body, | ||
@@ -86,3 +84,2 @@ transport: this.transport, | ||
body: this.body, | ||
options: this.options, | ||
timeout: this.timeout, | ||
@@ -114,3 +111,2 @@ method: this.method | ||
this.aborted = true; | ||
this._reject(this.error('Request aborted', 'EABORT')); | ||
if (this.opened) { | ||
@@ -122,2 +118,3 @@ this._emit(); | ||
} | ||
this._reject(this.error('Request aborted', 'EABORT')); | ||
return this; | ||
@@ -157,18 +154,4 @@ }; | ||
return this.transport.open(this) | ||
.then(function (res) { return _this._resolve(new response_1.default(res)); }, function (err) { return _this._reject(err); }); | ||
.then(function (res) { return _this._resolve(res); }, function (err) { return _this._reject(err); }); | ||
}; | ||
Object.defineProperty(Request.prototype, "uploaded", { | ||
get: function () { | ||
return this.uploadLength ? this.uploadedBytes / this.uploadLength : 0; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Request.prototype, "downloaded", { | ||
get: function () { | ||
return this.downloadLength ? this.downloadedBytes / this.downloadLength : 0; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Request.prototype, "completed", { | ||
@@ -195,28 +178,16 @@ get: function () { | ||
}); | ||
Object.defineProperty(Request.prototype, "uploadedBytes", { | ||
get: function () { | ||
return this._uploadedBytes; | ||
}, | ||
set: function (bytes) { | ||
if (bytes !== this._uploadedBytes) { | ||
this._uploadedBytes = bytes; | ||
this._emit(); | ||
} | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Request.prototype, "downloadedBytes", { | ||
get: function () { | ||
return this._downloadedBytes; | ||
}, | ||
set: function (bytes) { | ||
if (bytes !== this._downloadedBytes) { | ||
this._downloadedBytes = bytes; | ||
this._emit(); | ||
} | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Request.prototype._setUploadedBytes = function (bytes, uploaded) { | ||
if (bytes !== this.uploadedBytes) { | ||
this.uploaded = uploaded || bytes / this.uploadLength; | ||
this.uploadedBytes = bytes; | ||
this._emit(); | ||
} | ||
}; | ||
Request.prototype._setDownloadedBytes = function (bytes, downloaded) { | ||
if (bytes !== this.downloadedBytes) { | ||
this.downloaded = downloaded || bytes / this.downloadLength; | ||
this.downloadedBytes = bytes; | ||
this._emit(); | ||
} | ||
}; | ||
return Request; | ||
@@ -223,0 +194,0 @@ }(base_1.default)); |
@@ -67,3 +67,3 @@ "use strict"; | ||
t.equal(typeof res.toJSON, 'function'); | ||
t.deepEqual(Object.keys(req.toJSON()), ['url', 'headers', 'body', 'options', 'timeout', 'method']); | ||
t.deepEqual(Object.keys(req.toJSON()), ['url', 'headers', 'body', 'timeout', 'method']); | ||
t.deepEqual(Object.keys(res.toJSON()), ['url', 'headers', 'rawHeaders', 'body', 'status', 'statusText']); | ||
@@ -106,3 +106,3 @@ return req | ||
t.equal(res.status, 200); | ||
t.equal(res.body, METHODS_WITHOUT_BODY.indexOf(method) === -1 ? method.toUpperCase() : null); | ||
t.equal(res.body, METHODS_WITHOUT_BODY.indexOf(method) === -1 ? method.toUpperCase() : ''); | ||
}); | ||
@@ -163,3 +163,3 @@ })); | ||
}); | ||
if (!popsicle.browser) { | ||
if (!process.browser) { | ||
t.test('send a custom user agent header', function (t) { | ||
@@ -223,3 +223,4 @@ return popsicle.request({ | ||
method: 'POST', | ||
body: EXAMPLE_BODY | ||
body: EXAMPLE_BODY, | ||
transport: popsicle.createTransport({ type: 'json' }) | ||
}) | ||
@@ -238,3 +239,4 @@ .then(function (res) { | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
} | ||
}, | ||
transport: popsicle.createTransport({ type: 'urlencoded' }) | ||
}) | ||
@@ -292,3 +294,4 @@ .then(function (res) { | ||
url: REMOTE_URL + '/echo/query', | ||
query: EXAMPLE_BODY | ||
query: EXAMPLE_BODY, | ||
transport: popsicle.createTransport({ type: 'json' }) | ||
}) | ||
@@ -302,3 +305,4 @@ .then(function (res) { | ||
url: REMOTE_URL + '/echo/query?query=true', | ||
query: EXAMPLE_BODY | ||
query: EXAMPLE_BODY, | ||
transport: popsicle.createTransport({ type: 'json' }) | ||
}); | ||
@@ -324,3 +328,4 @@ var query = { | ||
url: REMOTE_URL + '/echo/query', | ||
query: 'query=true' | ||
query: 'query=true', | ||
transport: popsicle.createTransport({ type: 'json' }) | ||
}); | ||
@@ -363,3 +368,3 @@ t.equal(req.url, REMOTE_URL + '/echo/query?query=true'); | ||
var req = popsicle.request(REMOTE_URL + '/download'); | ||
t.plan(3); | ||
t.plan(1); | ||
setTimeout(function () { | ||
@@ -370,5 +375,3 @@ req.abort(); | ||
.catch(function (err) { | ||
t.equal(err.message, 'Request aborted'); | ||
t.equal(err.code, 'EABORT'); | ||
t.ok(err.popsicle instanceof popsicle.Request); | ||
}); | ||
@@ -411,3 +414,4 @@ }); | ||
body: EXAMPLE_BODY, | ||
method: 'POST' | ||
method: 'POST', | ||
transport: popsicle.createTransport({ type: 'json' }) | ||
}); | ||
@@ -441,4 +445,7 @@ t.plan(3); | ||
test('response body', function (t) { | ||
t.test('automatically parse json responses', function (t) { | ||
return popsicle.request(REMOTE_URL + '/json') | ||
t.test('parse json responses', function (t) { | ||
return popsicle.request({ | ||
url: REMOTE_URL + '/json', | ||
transport: popsicle.createTransport({ type: 'json' }) | ||
}) | ||
.then(function (res) { | ||
@@ -449,4 +456,7 @@ t.equal(res.type(), 'application/json'); | ||
}); | ||
t.test('automatically parse form encoded responses', function (t) { | ||
return popsicle.request(REMOTE_URL + '/foo') | ||
t.test('parse form encoded responses', function (t) { | ||
return popsicle.request({ | ||
url: REMOTE_URL + '/foo', | ||
transport: popsicle.createTransport({ type: 'urlencoded' }) | ||
}) | ||
.then(function (res) { | ||
@@ -457,6 +467,5 @@ t.equal(res.type(), 'application/x-www-form-urlencoded'); | ||
}); | ||
t.test('disable automatic parsing', function (t) { | ||
t.test('string response by default', function (t) { | ||
return popsicle.request({ | ||
url: REMOTE_URL + '/json', | ||
use: popsicle.browser ? [] : [popsicle.plugins.concatStream('string')] | ||
url: REMOTE_URL + '/json' | ||
}) | ||
@@ -468,3 +477,3 @@ .then(function (res) { | ||
}); | ||
t.test('set non-parsable responses as null', function (t) { | ||
t.test('empty response bodies', function (t) { | ||
return popsicle.request({ | ||
@@ -475,6 +484,6 @@ url: REMOTE_URL + '/echo', | ||
.then(function (res) { | ||
t.equal(res.body, null); | ||
t.equal(res.body, ''); | ||
}); | ||
}); | ||
t.test('set body to null when json is empty', function (t) { | ||
t.test('empty response body with json', function (t) { | ||
return popsicle.request({ | ||
@@ -485,3 +494,4 @@ url: REMOTE_URL + '/echo', | ||
'Content-Type': 'application/json' | ||
} | ||
}, | ||
transport: popsicle.createTransport({ type: 'json' }) | ||
}) | ||
@@ -493,2 +503,13 @@ .then(function (res) { | ||
}); | ||
t.test('throw on unsupported type', function (t) { | ||
t.plan(2); | ||
return popsicle.request({ | ||
url: REMOTE_URL + '/text', | ||
transport: popsicle.createTransport({ type: 'foobar' }) | ||
}) | ||
.catch(function (err) { | ||
t.equal(err.message, 'Unsupported type: foobar'); | ||
t.equal(err.code, 'ETYPE'); | ||
}); | ||
}); | ||
if (!process.browser) { | ||
@@ -502,3 +523,3 @@ var fs_2 = require('fs'); | ||
url: REMOTE_URL + '/json', | ||
use: [] | ||
transport: popsicle.createTransport({ type: 'stream' }) | ||
}) | ||
@@ -515,2 +536,9 @@ .then(function (res) { | ||
}); | ||
t.test('should break when response body is bigger than buffer size', function (t) { | ||
t.plan(1); | ||
return popsicle.request(REMOTE_URL + '/urandom') | ||
.catch(function (err) { | ||
t.equal(err.code, 'ETOOLARGE'); | ||
}); | ||
}); | ||
t.test('pipe streams', function (t) { | ||
@@ -572,5 +600,3 @@ return popsicle.request({ | ||
url: REMOTE_URL + '/text', | ||
options: { | ||
responseType: 'arraybuffer' | ||
} | ||
transport: popsicle.createTransport({ type: 'arraybuffer' }) | ||
}) | ||
@@ -581,15 +607,2 @@ .then(function (res) { | ||
}); | ||
t.test('throw on unsupported response type', function (t) { | ||
t.plan(2); | ||
return popsicle.request({ | ||
url: REMOTE_URL + '/text', | ||
options: { | ||
responseType: 'foobar' | ||
} | ||
}) | ||
.catch(function (err) { | ||
t.equal(err.message, 'Unsupported response type: foobar'); | ||
t.equal(err.code, 'ERESPONSETYPE'); | ||
}); | ||
}); | ||
} | ||
@@ -607,3 +620,3 @@ }); | ||
}); | ||
t.test('give a parse error on invalid response body', function (t) { | ||
t.test('give a parse error on invalid json response', function (t) { | ||
t.plan(3); | ||
@@ -616,3 +629,6 @@ return popsicle.request({ | ||
'Content-Type': 'application/json' | ||
} | ||
}, | ||
transport: popsicle.createTransport({ | ||
type: 'json' | ||
}) | ||
}) | ||
@@ -716,5 +732,5 @@ .catch(function (err) { | ||
var instance = popsicle.defaults({ | ||
options: { | ||
transport: popsicle.createTransport({ | ||
jar: popsicle.jar() | ||
} | ||
}) | ||
}); | ||
@@ -735,5 +751,5 @@ return instance(REMOTE_URL + '/cookie') | ||
var instance = popsicle.defaults({ | ||
options: { | ||
transport: popsicle.createTransport({ | ||
jar: popsicle.jar() | ||
} | ||
}) | ||
}); | ||
@@ -766,3 +782,3 @@ return instance(REMOTE_URL + '/cookie/redirect') | ||
}); | ||
if (!popsicle.browser) { | ||
if (!process.browser) { | ||
test('redirect', function (t) { | ||
@@ -799,5 +815,5 @@ t.test('should follow 302 redirect with get', function (t) { | ||
url: REMOTE_URL + '/redirect', | ||
options: { | ||
transport: popsicle.createTransport({ | ||
followRedirects: false | ||
} | ||
}) | ||
}) | ||
@@ -820,5 +836,5 @@ .then(function (res) { | ||
url: REMOTE_URL + '/redirect/6', | ||
options: { | ||
transport: popsicle.createTransport({ | ||
maxRedirects: 10 | ||
} | ||
}) | ||
}) | ||
@@ -834,3 +850,3 @@ .then(function (res) { | ||
.then(function (res) { | ||
t.equal(res.body, null); | ||
t.equal(res.body, ''); | ||
t.equal(res.status, 200); | ||
@@ -850,7 +866,7 @@ t.ok(/\/destination$/.test(res.url)); | ||
url: REMOTE_URL + '/redirect/code/308', | ||
options: { | ||
followRedirects: function () { | ||
transport: popsicle.createTransport({ | ||
confirmRedirect: function () { | ||
return true; | ||
} | ||
} | ||
}) | ||
}) | ||
@@ -865,3 +881,3 @@ .then(function (res) { | ||
} | ||
if (!popsicle.browser) { | ||
if (!process.browser) { | ||
test('https reject unauthorized', function (t) { | ||
@@ -879,5 +895,5 @@ t.plan(1); | ||
url: "" + REMOTE_HTTPS_URL, | ||
options: { | ||
transport: popsicle.createTransport({ | ||
ca: fs_1.readFileSync(path_1.join(__dirname, '../../scripts/support/ca-crt.pem')) | ||
} | ||
}) | ||
}) | ||
@@ -891,5 +907,5 @@ .then(function (res) { | ||
url: "" + REMOTE_HTTPS_URL, | ||
options: { | ||
transport: popsicle.createTransport({ | ||
rejectUnauthorized: false | ||
} | ||
}) | ||
}) | ||
@@ -896,0 +912,0 @@ .then(function (res) { |
{ | ||
"name": "popsicle", | ||
"version": "6.2.2", | ||
"version": "7.0.0", | ||
"description": "Simple HTTP requests for node and the browser", | ||
@@ -22,5 +22,5 @@ "main": "dist/common.js", | ||
"lint": "# TODO", | ||
"check-size": "browserify . -s popsicle --external bluebird > popsicle.js && du -h popsicle.js", | ||
"check-size": "browserify-size .", | ||
"build": "rm -rf dist/ && tsc && npm run check-size", | ||
"test-spec": "npm run test-server-open && HTTPS_PORT=7358 PORT=7357 node dist/test/index.js | tap-spec; EXIT=$?; npm run test-server-close; exit $EXIT", | ||
"test-spec": "npm run test-server-open && HTTPS_PORT=7358 PORT=7357 node dist/test/index.js; EXIT=$?; npm run test-server-close; exit $EXIT", | ||
"test-cov": "HTTPS_PORT=7358 PORT=7357 istanbul cover --print none dist/test/index.js | tap-spec", | ||
@@ -61,2 +61,3 @@ "test-browser": "HTTPS_PORT=7358 PORT=7357 browserify -d -t envify dist/test/index.js | tape-run --render tap-spec", | ||
"browserify": "^13.0.0", | ||
"browserify-size": "^1.0.6", | ||
"envify": "^3.4.0", | ||
@@ -63,0 +64,0 @@ "express": "^4.10.2", |
{ | ||
"resolution": { | ||
"main": "lib/typings" | ||
}, | ||
"devDependencies": { | ||
"blue-tape": "registry:npm/blue-tape#0.1.0+20160322235613", | ||
"blue-tape": "registry:npm/blue-tape#0.2.0+20160623043621", | ||
"methods": "registry:npm/methods#1.0.0+20160211003958" | ||
}, | ||
"globalDependencies": { | ||
"node": "registry:dt/node#4.0.0+20160330064709" | ||
"node": "registry:env/node#6.0.0+20160622202520" | ||
}, | ||
"dependencies": { | ||
"arrify": "registry:npm/arrify#1.0.0+20160211003958", | ||
"arrify": "registry:npm/arrify#1.0.0+20160508201941", | ||
"concat-stream": "registry:npm/concat-stream#1.0.0+20160211003958", | ||
@@ -15,5 +18,5 @@ "form-data": "registry:npm/form-data#1.0.0+20160211003958", | ||
"throwback": "npm:throwback", | ||
"tough-cookie": "registry:npm/tough-cookie#2.2.0+20160211003958", | ||
"tough-cookie": "registry:npm/tough-cookie#2.2.0+20160428222547", | ||
"xtend": "registry:npm/xtend#4.0.0+20160211003958" | ||
} | ||
} |
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
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
215011
58
2092
13