Comparing version 6.14.0 to 6.15.0
@@ -28,3 +28,3 @@ /*global unescape, btoa, atob, JSON*/ | ||
// Descending priority: | ||
var bodyPropertyNames = ['parts', 'body', 'unchunkedBody', 'rawBody']; | ||
var bodyPropertyNames = ['parts', 'body', 'unchunkedBody', 'decodedBody', 'rawBody']; | ||
@@ -64,2 +64,4 @@ Message.propertyNames = ['headers', 'fileName', 'isJson', 'isMultipart', 'boundary', 'charset'].concat(bodyPropertyNames); | ||
} | ||
} else if (typeof obj.decodedBody !== 'undefined') { | ||
this.decodedBody = obj.decodedBody; | ||
} else if (typeof obj.unchunkedBody !== 'undefined') { | ||
@@ -166,2 +168,80 @@ this.unchunkedBody = obj.unchunkedBody; | ||
Object.defineProperty(Message.prototype, 'decodedBody', { | ||
enumerable: true, | ||
get: function () { | ||
if (!isDefined(this._decodedBody)) { | ||
if (isDefined(this._rawBody) || isDefined(this._unchunkedBody)) { | ||
this._decodedBody = this.unchunkedBody; | ||
if (zlib && zlib.gunzipSync) { | ||
var contentEncoding = this.headers.get('Content-Encoding'); | ||
if (contentEncoding) { | ||
contentEncoding = contentEncoding.trim().toLowerCase(); | ||
if (contentEncoding === 'gzip' || contentEncoding === 'deflate') { | ||
if (typeof Buffer !== 'undefined' && !Buffer.isBuffer(this._body)) { | ||
this._decodedBody = new Buffer(this._decodedBody, 'utf-8'); | ||
} | ||
try { | ||
this._decodedBody = zlib[contentEncoding === 'gzip' ? 'gunzipSync' : 'inflateSync'](this._decodedBody); | ||
} catch (e) {} | ||
} | ||
} | ||
} | ||
var contentTransferEncoding = this.headers.get('Content-Transfer-Encoding'), | ||
contentTransferEncodingIsHonored = !contentTransferEncoding; | ||
if (contentTransferEncoding) { | ||
contentTransferEncoding = contentTransferEncoding.trim().toLowerCase(); | ||
if (contentTransferEncoding === 'quoted-printable') { | ||
if (typeof Buffer === 'function' && Buffer.isBuffer(this._decodedBody)) { | ||
this._decodedBody = this._decodedBody.toString('ascii'); | ||
} | ||
var qpDecodedBodyAsByteString = quotedPrintable.decode(this._decodedBody); | ||
this._decodedBody = new Buffer(qpDecodedBodyAsByteString.length); | ||
for (var i = 0 ; i < qpDecodedBodyAsByteString.length ; i += 1) { | ||
this._decodedBody[i] = qpDecodedBodyAsByteString.charCodeAt(i); | ||
} | ||
contentTransferEncodingIsHonored = true; | ||
} else if (contentTransferEncoding === 'base64') { | ||
if (typeof Buffer === 'function' && Buffer.isBuffer(this._decodedBody)) { | ||
this._decodedBody = this._decodedBody.toString('ascii'); | ||
} | ||
if (typeof Buffer !== 'undefined') { | ||
this._decodedBody = new Buffer(this._decodedBody, 'base64'); | ||
} else { | ||
this._decodedBody = atob(this._decodedBody); | ||
} | ||
contentTransferEncodingIsHonored = true; | ||
} else if (contentTransferEncoding === '8bit' || contentTransferEncoding === '7bit') { | ||
contentTransferEncodingIsHonored = true; | ||
} | ||
} | ||
if (this.hasTextualContentType && contentTransferEncodingIsHonored && this._decodedBody && typeof this._decodedBody !== 'string') { | ||
var charset = this.charset; | ||
if (iconvLite.encodingExists(charset)) { | ||
this._decodedBody = iconvLite.decode(this._decodedBody, charset); | ||
} | ||
} | ||
} else if (isDefined(this._body) || isDefined(this._parts)) { | ||
this._decodedBody = this.body; | ||
if ( | ||
((this.isJson && typeof this._decodedBody !== 'undefined') || this._decodedBody && typeof this._decodedBody === 'object') && | ||
(typeof Buffer === 'undefined' || !Buffer.isBuffer(this._decodedBody)) | ||
) { | ||
try { | ||
this._decodedBody = JSON.stringify(this._decodedBody); | ||
} catch (e) {} | ||
} | ||
} | ||
} | ||
return this._decodedBody; | ||
}, | ||
set: function (decodedBody) { | ||
this._unchunkedBody = null; | ||
this._decodedBody = decodedBody; | ||
this._body = null; | ||
this._rawBody = null; | ||
this._parts = null; | ||
} | ||
}); | ||
Object.defineProperty(Message.prototype, 'unchunkedBody', { | ||
@@ -179,12 +259,4 @@ enumerable: true, | ||
} | ||
} else if (isDefined(this._body) || isDefined(this._parts)) { | ||
this._unchunkedBody = this.body; | ||
if ( | ||
((this.isJson && typeof this._unchunkedBody !== 'undefined') || this._unchunkedBody && typeof this._unchunkedBody === 'object') && | ||
(typeof Buffer === 'undefined' || !Buffer.isBuffer(this._unchunkedBody)) | ||
) { | ||
try { | ||
this._unchunkedBody = JSON.stringify(this._unchunkedBody); | ||
} catch (e) {} | ||
} | ||
} else if (isDefined(this._body) || isDefined(this._parts) || isDefined(this._decodedBody)) { | ||
this._unchunkedBody = this.decodedBody; | ||
var charset = this.charset; | ||
@@ -226,2 +298,3 @@ if (/^utf-?8$/i.test(charset) && typeof Buffer !== 'undefined') { | ||
} | ||
} | ||
@@ -233,2 +306,3 @@ } | ||
this._unchunkedBody = unchunkedBody; | ||
this._decodedBody = null; | ||
this._body = null; | ||
@@ -243,3 +317,3 @@ this._rawBody = null; | ||
get: function () { | ||
if (!isDefined(this._rawBody) && (isDefined(this._body) || isDefined(this._parts) || isDefined(this._unchunkedBody))) { | ||
if (!isDefined(this._rawBody) && (isDefined(this._body) || isDefined(this._parts) || isDefined(this._unchunkedBody) || isDefined(this._decodedBody))) { | ||
this._rawBody = this.unchunkedBody; | ||
@@ -268,2 +342,3 @@ var transferEncoding = this.headers.get('Transfer-Encoding'); | ||
this._unchunkedBody = null; | ||
this._decodedBody = null; | ||
this._body = null; | ||
@@ -303,53 +378,4 @@ this._parts = null; | ||
} | ||
} else if (!isDefined(this._body) && (isDefined(this._rawBody) || isDefined(this._unchunkedBody))) { | ||
this._body = this.unchunkedBody; | ||
if (zlib && zlib.gunzipSync) { | ||
var contentEncoding = this.headers.get('Content-Encoding'); | ||
if (contentEncoding) { | ||
contentEncoding = contentEncoding.trim().toLowerCase(); | ||
if (contentEncoding === 'gzip' || contentEncoding === 'deflate') { | ||
if (typeof Buffer !== 'undefined' && !Buffer.isBuffer(this._body)) { | ||
this._body = new Buffer(this._body, 'utf-8'); | ||
} | ||
try { | ||
this._body = zlib[contentEncoding === 'gzip' ? 'gunzipSync' : 'inflateSync'](this._body); | ||
} catch (e) {} | ||
} | ||
} | ||
} | ||
var contentTransferEncoding = this.headers.get('Content-Transfer-Encoding'), | ||
contentTransferEncodingIsHonored = !contentTransferEncoding; | ||
if (contentTransferEncoding) { | ||
contentTransferEncoding = contentTransferEncoding.trim().toLowerCase(); | ||
if (contentTransferEncoding === 'quoted-printable') { | ||
if (typeof Buffer === 'function' && Buffer.isBuffer(this._body)) { | ||
this._body = this._body.toString('ascii'); | ||
} | ||
var qpDecodedBodyAsByteString = quotedPrintable.decode(this._body); | ||
this._body = new Buffer(qpDecodedBodyAsByteString.length); | ||
for (var i = 0 ; i < qpDecodedBodyAsByteString.length ; i += 1) { | ||
this._body[i] = qpDecodedBodyAsByteString.charCodeAt(i); | ||
} | ||
contentTransferEncodingIsHonored = true; | ||
} else if (contentTransferEncoding === 'base64') { | ||
if (typeof Buffer === 'function' && Buffer.isBuffer(this._body)) { | ||
this._body = this._body.toString('ascii'); | ||
} | ||
if (typeof Buffer !== 'undefined') { | ||
this._body = new Buffer(this._body, 'base64'); | ||
} else { | ||
this._body = atob(this._body); | ||
} | ||
contentTransferEncodingIsHonored = true; | ||
} else if (contentTransferEncoding === '8bit' || contentTransferEncoding === '7bit') { | ||
contentTransferEncodingIsHonored = true; | ||
} | ||
} | ||
if (this.hasTextualContentType && contentTransferEncodingIsHonored && this._body && typeof this._body !== 'string') { | ||
var charset = this.charset; | ||
if (iconvLite.encodingExists(charset)) { | ||
this._body = iconvLite.decode(this._body, charset); | ||
} | ||
} | ||
} else if (!isDefined(this._body) && (isDefined(this._rawBody) || isDefined(this._unchunkedBody) || isDefined(this._decodedBody))) { | ||
this._body = this.decodedBody; | ||
if (this.isJson && typeof this._body === 'string') { | ||
@@ -372,2 +398,3 @@ try { | ||
this._unchunkedBody = null; | ||
this._decodedBody = null; | ||
this._parts = null; | ||
@@ -384,2 +411,3 @@ } | ||
this._unchunkedBody = null; | ||
this._decodedBody = null; | ||
}, | ||
@@ -386,0 +414,0 @@ get: function () { |
{ | ||
"name": "messy", | ||
"version": "6.14.0", | ||
"version": "6.15.0", | ||
"description": "Object model for HTTP and RFC822 messages", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -242,6 +242,7 @@ /*global describe, it*/ | ||
it('should interpret the string as already encoded JSON', function () { | ||
expect(new Message({ | ||
var msg = new Message({ | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: '{"foo": 123}' | ||
}).unchunkedBody.toString(), 'to equal', '{"foo": 123}'); | ||
}); | ||
expect(msg.unchunkedBody.toString(), 'to equal', '{"foo": 123}'); | ||
}); | ||
@@ -564,2 +565,131 @@ }); | ||
describe('#decodedBody', function () { | ||
it('should decode Transfer-Encoding:chunked when the body is provided as a string', function () { | ||
expect(new Message( | ||
'Content-Type: text/plain; charset=UTF-8\r\n' + | ||
'Transfer-Encoding: chunked\r\n' + | ||
'\r\n' + | ||
'4\r\n' + | ||
'Wiki\r\n' + | ||
'5\r\n' + | ||
'pedia\r\n' + | ||
'e\r\n' + | ||
' in\r\n\r\nchunks.\r\n' + | ||
'0\r\n' + | ||
'\r\n' | ||
).decodedBody, 'to equal', 'Wikipedia in\r\n\r\nchunks.'); | ||
}); | ||
it('should decode Transfer-Encoding:chunked when the body is provided as a Buffer', function () { | ||
expect(new Message( | ||
new Buffer( | ||
'Content-Type: text/plain; charset=UTF-8\r\n' + | ||
'Transfer-Encoding: chunked\r\n' + | ||
'\r\n' + | ||
'4\r\n' + | ||
'Wiki\r\n' + | ||
'5\r\n' + | ||
'pedia\r\n' + | ||
'e\r\n' + | ||
' in\r\n\r\nchunks.\r\n' + | ||
'0\r\n' + | ||
'\r\n', | ||
'utf-8' | ||
) | ||
).decodedBody, 'to equal', 'Wikipedia in\r\n\r\nchunks.'); | ||
}); | ||
it('should decode Transfer-Encoding:chunked when a partial body is provided as a string', function () { | ||
expect(new Message( | ||
'Content-Type: text/plain; charset=UTF-8\r\n' + | ||
'Transfer-Encoding: chunked\r\n' + | ||
'\r\n' + | ||
'4\r\n' + | ||
'Wiki\r\n' + | ||
'5\r\n' + | ||
'pedia\r\n' + | ||
'e\r\n' + | ||
' in\r\n\r\nchunks.' | ||
).decodedBody, 'to equal', 'Wikipedia in\r\n\r\nchunks.'); | ||
expect(new Message( | ||
'Content-Type: text/plain; charset=UTF-8\r\n' + | ||
'Transfer-Encoding: chunked\r\n' + | ||
'\r\n' + | ||
'4\r\n' + | ||
'Wiki\r\n' + | ||
'5\r\n' + | ||
'pedia\r\n' + | ||
'e\r\n' + | ||
' in\r\n\r\nchunks.\r\n' | ||
).decodedBody, 'to equal', 'Wikipedia in\r\n\r\nchunks.'); | ||
}); | ||
it('should decode Transfer-Encoding:chunked when a partial body is provided as a Buffer', function () { | ||
expect(new Message( | ||
new Buffer( | ||
'Content-Type: text/plain; charset=UTF-8\r\n' + | ||
'Transfer-Encoding: chunked\r\n' + | ||
'\r\n' + | ||
'4\r\n' + | ||
'Wiki\r\n' + | ||
'5\r\n' + | ||
'pedia\r\n' + | ||
'e\r\n' + | ||
' in\r\n\r\nchunks.', | ||
'utf-8' | ||
) | ||
).decodedBody, 'to equal', 'Wikipedia in\r\n\r\nchunks.'); | ||
expect(new Message( | ||
new Buffer( | ||
'Content-Type: text/plain; charset=UTF-8\r\n' + | ||
'Transfer-Encoding: chunked\r\n' + | ||
'\r\n' + | ||
'4\r\n' + | ||
'Wiki\r\n' + | ||
'5\r\n' + | ||
'pedia\r\n' + | ||
'e\r\n' + | ||
' in\r\n\r\nchunks.\r\n', | ||
'utf-8' | ||
) | ||
).decodedBody, 'to equal', 'Wikipedia in\r\n\r\nchunks.'); | ||
}); | ||
describe('when accessed as a setter', function () { | ||
it('should update rawBody and body', function () { | ||
var message = new Message( | ||
'Content-Type: text/plain; charset=UTF-8\r\n' + | ||
'Transfer-Encoding: chunked\r\n' + | ||
'\r\n' + | ||
'4\r\n' + | ||
'Wiki\r\n' + | ||
'5\r\n' + | ||
'pedia\r\n' + | ||
'e\r\n' + | ||
' in\r\n\r\nchunks.\r\n' + | ||
'0\r\n' + | ||
'\r\n' | ||
); | ||
message.decodedBody = 'foobar'; | ||
expect(message.rawBody, 'to equal', new Buffer('6\r\nfoobar\r\n0\r\n\r\n')); | ||
expect(message.body, 'to equal', 'foobar'); | ||
}); | ||
}); | ||
describe('when passed to the constructor', function () { | ||
it('should populate rawBody and body', function () { | ||
var message = new Message({ | ||
headers: | ||
'Content-Type: text/plain; charset=UTF-8\r\n' + | ||
'Transfer-Encoding: chunked\r\n', | ||
decodedBody: 'foobar' | ||
}); | ||
expect(message.rawBody, 'to equal', new Buffer('6\r\nfoobar\r\n0\r\n\r\n', 'utf-8')); | ||
expect(message.body, 'to equal', 'foobar'); | ||
}); | ||
}); | ||
}); | ||
describe('#body', function () { | ||
@@ -566,0 +696,0 @@ it('should decode the body as text when the Content-Type contains +xml', function () { |
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
184315
3885