postman-collection
Advanced tools
Comparing version 0.4.4 to 0.4.5
@@ -1,5 +0,5 @@ | ||
var _ = require('../util').lodash, | ||
var util = require('../util'), | ||
_ = util.lodash, | ||
PropertyBase = require('./property-base').PropertyBase, | ||
PropertyList = require('./property-list').PropertyList, | ||
Header; | ||
@@ -47,10 +47,5 @@ /** | ||
if (_.isString(options)) { | ||
options = Header.parseSingle(options); | ||
options = _.isString(name) ? { key: name, value: options } : Header.parseSingle(options); | ||
} | ||
if (options && _.isString(name)) { | ||
options.value = options.key; | ||
options.key = name; | ||
} | ||
// this constructor is intended to inherit and as such the super constructor is required to be excuted | ||
@@ -141,11 +136,20 @@ Header.super_.apply(this, arguments); | ||
* Parses a single Header. | ||
* @param header | ||
* @param {String} header | ||
* @returns {{key: string, value: string}} | ||
*/ | ||
parseSingle: function (header) { | ||
var parsed; | ||
parsed = header.split(':'); | ||
if (!_.isString(header)) { return { key: '', value: '' }; } | ||
var index = header.indexOf(':'), | ||
key, | ||
value; | ||
(index < 0) && (index = header.length); | ||
key = header.substr(0, index); | ||
value = header.substr(index + 1); | ||
return { | ||
key: _.trim(parsed[0]), | ||
value: _.trim(parsed[1]) | ||
key: _.trim(key), | ||
value: _.trim(value) | ||
}; | ||
@@ -158,7 +162,9 @@ }, | ||
* @param {Array|PropertyList<Header>} headers | ||
* @param {String=} separator - Specify a string for separating each header, by default, '\n', but sometimes, | ||
* it might be more useful to use a carriage return ('\r\n') | ||
* @returns {string} | ||
*/ | ||
unparse: function (headers) { | ||
unparse: function (headers, separator) { | ||
if (!_.isArray(headers) && !PropertyList.isPropertyList(headers)) { | ||
throw new Error('It is only possible to unparse PropertyList or Array of headers.'); | ||
return ''; | ||
} | ||
@@ -168,3 +174,3 @@ | ||
return header.key + ': ' + header.value; | ||
}).join('\n'); | ||
}).join(separator ? separator : '\n'); | ||
}, | ||
@@ -197,2 +203,37 @@ | ||
return new (Header.bind.apply(Header, args))(); | ||
}, | ||
/** | ||
* Gets value from an Array or {@link PropertyList} of Headers by filtering through key. | ||
* | ||
* @param {Array|PropertyList<Header>} headers | ||
* @param {string} key | ||
* @returns {string} | ||
*/ | ||
headerValue: function (headers, key) { | ||
if (!_.isArray(headers) && !PropertyList.isPropertyList(headers)) { | ||
return undefined; | ||
} | ||
var header = headers.find(function (header) { | ||
return header.key === key; | ||
}); | ||
return header ? header.value : undefined; | ||
}, | ||
/** | ||
* Gets size of an Array or {@link PropertyList} of Headers. | ||
* | ||
* @param {Array|PropertyList<Header>} headers | ||
* @returns {Number} | ||
*/ | ||
size: function (headers, statusCode, reason) { | ||
if (!_.isArray(headers) && !PropertyList.isPropertyList(headers)) { | ||
return 0; | ||
} | ||
// https://tools.ietf.org/html/rfc7230#section-3.1.2 | ||
// status-line = HTTP-version SP status-code SP reason-phrase CRLF | ||
var raw = 'HTTP/X.X' + ' ' + statusCode + ' ' + reason + '\r\n'; | ||
raw += this.unparse(headers, '\r\n'); | ||
raw += '\r\n\r\n'; | ||
return raw.length; | ||
} | ||
@@ -199,0 +240,0 @@ }); |
@@ -214,2 +214,26 @@ var util = require('../util'), | ||
util.bufferOrArrayBufferToBase64(this.stream)) || ((this.body != null) && util.btoa(this.body)) || E); | ||
}, | ||
/** | ||
* Get the response size | ||
* @return {Number} | ||
* @todo write unit tests | ||
*/ | ||
size: function () { | ||
var sizeInfo = {}, | ||
contentEncoding = Header.headerValue(this.headers, 'Content-Encoding'), | ||
contentLength = Header.headerValue(this.headers, 'Content-Length'); | ||
// if 'Content-Length' header is present and encoding is of type gzip/deflate | ||
if (contentLength && /^(gzip|deflate)$/.test(contentEncoding) && util.isNumeric(contentLength)) { | ||
sizeInfo.body = _.parseInt(contentLength, 10); | ||
} | ||
// else size of body is added | ||
else { | ||
sizeInfo.body = this.stream ? this.stream.byteLength : (this.body && this.body.length || 0); | ||
} | ||
// size of header is added | ||
sizeInfo.header = Header.size(this.headers, this.code, this.reason()); | ||
return sizeInfo; | ||
} | ||
@@ -216,0 +240,0 @@ }); |
@@ -236,2 +236,11 @@ /* global btoa */ | ||
return base64; | ||
}, | ||
/** | ||
* Check whether a value is number-like | ||
* https://github.com/lodash/lodash/issues/1148#issuecomment-141139153 | ||
* @return {Boolean} | ||
*/ | ||
isNumeric: function (n) { | ||
return !isNaN(parseFloat(n)) && isFinite(n); | ||
} | ||
@@ -238,0 +247,0 @@ }; |
@@ -5,3 +5,3 @@ { | ||
"author": "Postman Labs <help@getpostman.com>", | ||
"version": "0.4.4", | ||
"version": "0.4.5", | ||
"keywords": [ | ||
@@ -35,3 +35,3 @@ "postman" | ||
"atob": "^2.0.0", | ||
"aws4": "^1.2.1", | ||
"aws4": "^1.4.1", | ||
"btoa": "^1.1.2", | ||
@@ -38,0 +38,0 @@ "crypto-js": "^3.1.6", |
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
277098
6291
Updatedaws4@^1.4.1