Comparing version 1.6.0 to 2.0.0
@@ -81,29 +81,2 @@ /*global unescape*/ | ||
function canonicalizeObject(obj, stack) { | ||
stack = stack || []; | ||
if (stack.indexOf(obj) !== -1) return '[Circular]'; | ||
var canonicalizedObject; | ||
if ({}.toString.call(obj) === '[object Array]') { | ||
stack.push(obj); | ||
canonicalizedObject = obj.map(function (item) { | ||
return canonicalizeObject(item, stack); | ||
}); | ||
stack.pop(); | ||
} else if (typeof obj === 'object' && obj !== null) { | ||
stack.push(obj); | ||
canonicalizedObject = {}; | ||
Object.keys(obj).sort().forEach(function (key) { | ||
canonicalizedObject[key] = canonicalizeObject(obj[key], stack); | ||
}); | ||
stack.pop(); | ||
} else { | ||
canonicalizedObject = obj; | ||
} | ||
return canonicalizedObject; | ||
} | ||
Message.prototype.clone = function () { | ||
@@ -152,85 +125,2 @@ return new Message({ | ||
function isTextualContentType(contentType) { | ||
if (typeof contentType === 'string') { | ||
contentType = contentType.toLowerCase().trim().replace(/\s*;.*$/, ''); | ||
return ( | ||
/^text\//.test(contentType) || | ||
/^application\/(json|javascript)$/.test(contentType) || | ||
/^application\/xml/.test(contentType) || | ||
/^application\/x-www-form-urlencoded\b/.test(contentType) || | ||
/\+xml$/.test(contentType) | ||
); | ||
} | ||
return false; | ||
} | ||
function bufferCanBeInterpretedAsUtf8(buffer) { | ||
// Hack: Since Buffer.prototype.toString('utf-8') is very forgiving, convert the buffer to a string | ||
// with percent-encoded octets, then see if decodeURIComponent accepts it. | ||
try { | ||
decodeURIComponent(Array.prototype.map.call(buffer, function (octet) { | ||
return '%' + (octet < 16 ? '0' : '') + octet.toString(16); | ||
}).join('')); | ||
} catch (e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
Message.prototype.upgradeBody = function () { | ||
if (typeof this.body !== 'undefined') { | ||
var isJson = /^application\/json\b/i.test(this.headers.get('Content-Type')); | ||
if ((typeof Buffer === 'function' && Buffer.isBuffer(this.body)) && isTextualContentType(this.headers.get('Content-Type')) && bufferCanBeInterpretedAsUtf8(this.body)) { | ||
this.body = this.body.toString('utf-8'); | ||
} | ||
if (isJson && typeof this.body === 'string') { | ||
try { | ||
this.body = JSON.parse(this.body); | ||
} catch (e) { | ||
// The body cannot be parsed as JSON, keep as a string instance | ||
} | ||
} | ||
} | ||
}; | ||
Message.prototype.upgradeOrDowngradeBodyToMatchSpec = function (spec) { | ||
if (typeof this.body !== 'undefined') { | ||
var isJson = /^application\/json\b/i.test(this.headers.get('Content-Type')); | ||
if (isNonBufferNonRegExpObject(this.body) && isJson) { | ||
if (typeof spec.body === 'string' || (typeof Buffer === 'function' && Buffer.isBuffer(spec.body))) { | ||
var parsedSpecBody; | ||
try { | ||
parsedSpecBody = JSON.parse(spec.body); | ||
} catch (e) {} | ||
if (typeof parsedSpecBody !== 'undefined') { | ||
spec.body = JSON.stringify(canonicalizeObject(parsedSpecBody), undefined, ' '); | ||
this.body = JSON.stringify(this.body, undefined, ' '); | ||
} | ||
} else if (isRegExp(spec.body)) { | ||
this.body = JSON.stringify(this.body, undefined, ' '); | ||
} | ||
} | ||
if (typeof Buffer === 'function' && Buffer.isBuffer(this.body) && ((typeof spec.body === 'string' || isRegExp(spec.body) || isNonBufferNonRegExpObject(spec.body)) || (bufferCanBeInterpretedAsUtf8(this.body) && isTextualContentType(this.headers.get('Content-Type'))))) { | ||
try { | ||
this.body = this.body.toString('utf-8'); | ||
} catch (e) { | ||
// The body cannot be intepreted as utf-8, keep it as a Buffer instance | ||
} | ||
} | ||
if (isJson && typeof this.body === 'string' && (typeof spec.body === 'undefined' || typeof spec.body === 'function' || isNonBufferNonRegExpObject(spec.body))) { | ||
try { | ||
this.body = JSON.parse(this.body); | ||
} catch (e) { | ||
// The body cannot be parsed as JSON, keep as a string instance | ||
} | ||
} else if (typeof Buffer === 'function' && Buffer.isBuffer(spec.body) && (!this.body || typeof this.body === 'string')) { | ||
this.body = new Buffer(this.body, 'utf-8'); | ||
} | ||
} else if (typeof Buffer === 'function' && Buffer.isBuffer(spec.body) && spec.body.length === 0) { | ||
this.body = new Buffer([]); | ||
} else if (spec.body === '') { | ||
this.body = ''; | ||
} | ||
}; | ||
module.exports = Message; |
{ | ||
"name": "messy", | ||
"version": "1.6.0", | ||
"version": "2.0.0", | ||
"description": "Object model for HTTP and RFC822 messages", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
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
75517
1636