Comparing version 4.4.0 to 5.0.0
@@ -1,2 +0,2 @@ | ||
/*global unescape*/ | ||
/*global unescape, btoa, atob*/ | ||
var Headers = require('./Headers'), | ||
@@ -6,4 +6,13 @@ isRegExp = require('./isRegExp'), | ||
quotedPrintable = require('quoted-printable'), | ||
decodeChunkedTransferEncoding = require('./decodeChunkedTransferEncoding'); | ||
decodeChunkedTransferEncoding = require('./decodeChunkedTransferEncoding'), | ||
zlib; | ||
try { | ||
zlib = require('' + 'zlib'); | ||
} catch (e) {} | ||
function isDefined(obj) { | ||
return obj !== null && typeof obj !== 'undefined'; | ||
} | ||
function quoteRegExp(str) { | ||
@@ -37,3 +46,5 @@ return str.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&'); | ||
} | ||
if (typeof obj.body !== 'undefined') { | ||
if (typeof obj.rawBody !== 'undefined') { | ||
this.rawBody = obj.rawBody; | ||
} else if (typeof obj.body !== 'undefined') { | ||
this.body = obj.body; | ||
@@ -45,3 +56,2 @@ } | ||
Message.prototype.populateFromBuffer = function (buffer) { | ||
this.rawSrc = buffer; | ||
// Hack: Interpret non-ASCII in headers as iso-8859-1: | ||
@@ -59,3 +69,3 @@ var str = ''; | ||
if (i < buffer.length) { | ||
this.body = buffer.slice(i); | ||
this.rawBody = buffer.slice(i); | ||
} | ||
@@ -70,6 +80,5 @@ break; | ||
Message.prototype.populateFromString = function (str) { | ||
this.rawSrc = str; | ||
var bodyStartIndex = this.headers.populateFromStringAndReturnBodyStartIndex(str); | ||
if (bodyStartIndex < str.length) { | ||
this.body = str.substr(bodyStartIndex); | ||
this.rawBody = str.substr(bodyStartIndex); | ||
} | ||
@@ -122,2 +131,67 @@ return this; | ||
Object.defineProperty(Message.prototype, 'rawBody', { | ||
enumerable: true, | ||
get: function () { | ||
if (!isDefined(this._rawBody) && isDefined(this._body)) { | ||
this._rawBody = this._body; | ||
var charset = this.headers.parameter('Content-Type', 'charset') || 'iso-8859-1'; | ||
if (iconvLite.encodingExists(charset) && !/^utf-?8$/i.test(charset)) { | ||
this._rawBody = iconvLite.encode(this._rawBody, charset); | ||
} | ||
var contentTransferEncoding = this.headers.get('Content-Transfer-Encoding'); | ||
if (contentTransferEncoding) { | ||
contentTransferEncoding = contentTransferEncoding.trim().toLowerCase(); | ||
if (contentTransferEncoding === 'base64') { | ||
if (typeof Buffer !== 'undefined') { | ||
if (!Buffer.isBuffer(this._rawBody)) { | ||
this._rawBody = new Buffer(this._rawBody, 'utf-8'); | ||
} | ||
this._rawBody = this.rawBody.toString('base64'); | ||
} else { | ||
this._rawBody = btoa(this._rawBody); | ||
} | ||
} else if (contentTransferEncoding === 'quoted-printable') { | ||
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(this._rawBody)) { | ||
this._rawBody = this._rawBody.toString('binary'); | ||
} | ||
this._rawBody = quotedPrintable.encode(this._rawBody); | ||
} | ||
} | ||
if (zlib && zlib.gzipSync) { | ||
var contentEncoding = this.headers.get('Content-Encoding'); | ||
if (contentEncoding) { | ||
contentEncoding = contentEncoding.trim().toLowerCase(); | ||
if (contentEncoding === 'gzip' || contentEncoding === 'deflate') { | ||
try { | ||
this._rawBody = zlib[contentEncoding === 'gzip' ? 'gzipSync' : 'deflateSync'](this._body || ''); | ||
} catch (e) {} | ||
} | ||
} | ||
} | ||
var transferEncoding = this.headers.get('Transfer-Encoding'); | ||
if (transferEncoding && transferEncoding === 'chunked') { | ||
if (typeof Buffer !== 'undefined' && !Buffer.isBuffer(this._rawBody)) { | ||
this._rawBody = new Buffer(this._rawBody, 'utf-8'); | ||
} | ||
var chunks = []; | ||
if (this._rawBody.length > 0) { | ||
chunks.push( | ||
new Buffer(this._rawBody.length.toString(16) + '\r\n', 'ascii'), | ||
this._rawBody, | ||
new Buffer('\r\n', 'ascii') | ||
); | ||
} | ||
chunks.push(new Buffer('0\r\n\r\n', 'ascii')); | ||
this._rawBody = Buffer.concat(chunks); | ||
} | ||
} | ||
return this._rawBody; | ||
}, | ||
set: function (rawBody) { | ||
this._rawBody = rawBody; | ||
this._body = null; | ||
this._parts = null; | ||
} | ||
}); | ||
Object.defineProperty(Message.prototype, 'body', { | ||
@@ -151,23 +225,25 @@ enumerable: true, | ||
} | ||
} else { | ||
return this._body; | ||
} | ||
}, | ||
set: function (body) { | ||
this._body = body; | ||
this._parts = null; | ||
} | ||
}); | ||
Object.defineProperty(Message.prototype, 'decodedBody', { | ||
enumerable: true, | ||
get: function () { | ||
var decodedBody = this.body; | ||
if (decodedBody) { | ||
} else if (!isDefined(this._body) && isDefined(this._rawBody)) { | ||
this._body = this._rawBody; | ||
var transferEncoding = this.headers.get('Transfer-Encoding'); | ||
if (transferEncoding && transferEncoding === 'chunked') { | ||
try { | ||
decodedBody = decodeChunkedTransferEncoding(decodedBody); | ||
this._body = decodeChunkedTransferEncoding(this._body); | ||
} catch (e) {} | ||
} | ||
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._rawBody)) { | ||
this._rawBody = new Buffer(this._rawBody, 'utf-8'); | ||
} | ||
try { | ||
this._body = zlib[contentEncoding === 'gzip' ? 'gunzipSync' : 'inflateSync'](this._body); | ||
} catch (e) {} | ||
} | ||
} | ||
} | ||
var contentTransferEncoding = this.headers.get('Content-Transfer-Encoding'), | ||
@@ -178,16 +254,20 @@ contentTransferEncodingIsHonored = !contentTransferEncoding; | ||
if (contentTransferEncoding === 'quoted-printable') { | ||
if (typeof Buffer === 'function' && Buffer.isBuffer(decodedBody)) { | ||
decodedBody = decodedBody.toString('ascii'); | ||
if (typeof Buffer === 'function' && Buffer.isBuffer(this._body)) { | ||
this._body = this._body.toString('ascii'); | ||
} | ||
var qpDecodedBodyAsByteString = quotedPrintable.decode(decodedBody); | ||
decodedBody = new Buffer(qpDecodedBodyAsByteString.length); | ||
var qpDecodedBodyAsByteString = quotedPrintable.decode(this._body); | ||
this._body = new Buffer(qpDecodedBodyAsByteString.length); | ||
for (var i = 0 ; i < qpDecodedBodyAsByteString.length ; i += 1) { | ||
decodedBody[i] = qpDecodedBodyAsByteString.charCodeAt(i); | ||
this._body[i] = qpDecodedBodyAsByteString.charCodeAt(i); | ||
} | ||
contentTransferEncodingIsHonored = true; | ||
} else if (contentTransferEncoding === 'base64') { | ||
if (typeof Buffer === 'function' && Buffer.isBuffer(decodedBody)) { | ||
decodedBody = decodedBody.toString('ascii'); | ||
if (typeof Buffer === 'function' && Buffer.isBuffer(this._body)) { | ||
this._body = this._body.toString('ascii'); | ||
} | ||
decodedBody = new Buffer(decodedBody, 'base64'); | ||
if (typeof Buffer !== 'undefined') { | ||
this._body = new Buffer(this._body, 'base64'); | ||
} else { | ||
this._body = atob(this._body); | ||
} | ||
contentTransferEncodingIsHonored = true; | ||
@@ -198,10 +278,15 @@ } else if (contentTransferEncoding === '8bit' || contentTransferEncoding === '7bit') { | ||
} | ||
if (this.hasTextualContentType && contentTransferEncodingIsHonored && typeof decodedBody !== 'string') { | ||
if (this.hasTextualContentType && contentTransferEncodingIsHonored && typeof this._body !== 'string') { | ||
var charset = this.headers.parameter('Content-Type', 'charset') || 'iso-8859-1'; | ||
if (iconvLite.encodingExists(charset)) { | ||
decodedBody = iconvLite.decode(decodedBody, charset); | ||
this._body = iconvLite.decode(this._body, charset); | ||
} | ||
} | ||
} | ||
return decodedBody; | ||
return this._body; | ||
}, | ||
set: function (body) { | ||
this._body = body; | ||
this._rawBody = null; | ||
this._parts = null; | ||
} | ||
@@ -214,2 +299,4 @@ }); | ||
this._parts = parts; | ||
this._body = null; | ||
this._rawBody = null; | ||
}, | ||
@@ -216,0 +303,0 @@ get: function () { |
{ | ||
"name": "messy", | ||
"version": "4.4.0", | ||
"version": "5.0.0", | ||
"description": "Object model for HTTP and RFC822 messages", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -5,2 +5,6 @@ /*global describe, it*/ | ||
it.skipIf = function (condition) { | ||
(condition ? it.skip : it).apply(it, Array.prototype.slice.call(arguments, 1)); | ||
}; | ||
describe('Message', function () { | ||
@@ -307,3 +311,3 @@ it('should accept an options object with headers and body', function () { | ||
describe('#decodedBody', function () { | ||
describe('#body', function () { | ||
it('should decode a base64 body to a string when the Content-Transfer-Encoding is base64 and the Content-Type is textual and the body is stored as a string', function () { | ||
@@ -315,3 +319,3 @@ expect(new Message( | ||
'Zm9v\r\n' | ||
).decodedBody, 'to equal', 'foo'); | ||
).body, 'to equal', 'foo'); | ||
}); | ||
@@ -325,3 +329,3 @@ | ||
'Zm9v\r\n') | ||
).decodedBody, 'to equal', 'foo'); | ||
).body, 'to equal', 'foo'); | ||
}); | ||
@@ -335,3 +339,3 @@ | ||
'Zm9v\r\n' | ||
).decodedBody, 'to equal', new Buffer('foo')); | ||
).body, 'to equal', new Buffer('foo')); | ||
}); | ||
@@ -345,3 +349,3 @@ | ||
'Abc =C3=A6=C3=B8=C3=A5\r\n' | ||
).decodedBody, 'to equal', 'Abc æøå\r\n'); | ||
).body, 'to equal', 'Abc æøå\r\n'); | ||
}); | ||
@@ -355,3 +359,3 @@ | ||
'Zm9v\r\n' | ||
).decodedBody, 'to equal', 'Zm9v\r\n'); | ||
).body, 'to equal', 'Zm9v\r\n'); | ||
}); | ||
@@ -365,3 +369,3 @@ | ||
'Abc =F8\r\n' | ||
).decodedBody, 'to equal', 'Abc ø\r\n'); | ||
).body, 'to equal', 'Abc ø\r\n'); | ||
}); | ||
@@ -374,3 +378,3 @@ | ||
'Abcdef\r\n' | ||
).decodedBody, 'to equal', 'Abcdef\r\n'); | ||
).body, 'to equal', 'Abcdef\r\n'); | ||
}); | ||
@@ -388,3 +392,3 @@ | ||
]) | ||
).decodedBody, 'to equal', 'Abc ø\r\n'); | ||
).body, 'to equal', 'Abc ø\r\n'); | ||
}); | ||
@@ -405,3 +409,3 @@ | ||
'\r\n' | ||
).decodedBody, 'to equal', 'Wikipedia in\r\n\r\nchunks.'); | ||
).body, 'to equal', 'Wikipedia in\r\n\r\nchunks.'); | ||
}); | ||
@@ -425,16 +429,78 @@ | ||
) | ||
).decodedBody, 'to equal', 'Wikipedia in\r\n\r\nchunks.'); | ||
).body, 'to equal', 'Wikipedia in\r\n\r\nchunks.'); | ||
}); | ||
it.skipIf(!require('zlib').gunzipSync, 'should decode Content-Encoding:gzip', function () { | ||
expect(new Message(Buffer.concat([ | ||
new Buffer( | ||
'Content-Type: text/plain; charset=UTF-8\r\n' + | ||
'Content-Encoding: gzip\r\n' + | ||
'\r\n', 'ascii' | ||
), | ||
new Buffer([0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x4b, 0xcb, 0xcf, 0x4f, 0x4a, 0x2c, 0x02, 0x00, 0x95, 0x1f, 0xf6, 0x9e, 0x06, 0x00, 0x00, 0x00]) | ||
])).body, 'to equal', 'foobar'); | ||
}); | ||
}); | ||
describe('#rawSrc', function () { | ||
it('should be populated when instiating a Message from a string', function () { | ||
var rawSrc = 'Foo: bar\r\n\r\nquux'; | ||
expect(new Message(rawSrc).rawSrc, 'to equal', rawSrc); | ||
describe('#rawBody', function () { | ||
it('should be populated when instantiating a Message from a string', function () { | ||
var rawBody = 'Foo: bar\r\n\r\nquux'; | ||
expect(new Message(rawBody).rawBody, 'to equal', 'quux'); | ||
}); | ||
it('should be populated when instiating a Message from a Buffer', function () { | ||
var rawSrc = new Buffer('Foo: bar\r\n\r\nquux', 'utf-8'); | ||
expect(new Message(rawSrc).rawSrc, 'to equal', rawSrc); | ||
it('should be populated when instantiating a Message from a Buffer', function () { | ||
var rawBody = new Buffer('Foo: bar\r\n\r\nquux', 'utf-8'); | ||
expect(new Message(rawBody).rawBody, 'to equal', new Buffer('quux', 'utf-8')); | ||
}); | ||
it('should be recomputed from the body if updated, with Content-Transfer-Encoding', function () { | ||
var message = new Message( | ||
'Content-Type: text/plain; charset=UTF-8\r\nContent-Transfer-Encoding: base64\r\n\r\nZm9vYmFy' | ||
); | ||
expect(message.rawBody, 'to equal', 'Zm9vYmFy'); | ||
expect(message.body, 'to equal', 'foobar'); | ||
message.body = 'quux'; | ||
expect(message.rawBody, 'to equal', 'cXV1eA=='); | ||
}); | ||
it('should be recomputed from the body if updated, with quoted-printable and a charset', function () { | ||
var message = new Message( | ||
'Content-Type: text/plain; charset=iso-8859-1\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\nF=E6' | ||
); | ||
expect(message.rawBody, 'to equal', 'F=E6'); | ||
expect(message.body, 'to equal', 'Fæ'); | ||
message.body = 'øh'; | ||
expect(message.rawBody, 'to equal', '=F8h'); | ||
}); | ||
it('should be recomputed from the body if updated, with transfer-encoding: chunked', 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' + | ||
'0\r\n' + | ||
'\r\n' | ||
); | ||
expect(message.rawBody, 'to equal', '4\r\nWiki\r\n0\r\n\r\n'); | ||
expect(message.body, 'to equal', 'Wiki'); | ||
message.body = 'sarbarbarbab'; | ||
expect(message.rawBody, 'to equal', new Buffer('c\r\nsarbarbarbab\r\n0\r\n\r\n')); | ||
expect(message.body, 'to equal', 'sarbarbarbab'); | ||
}); | ||
it.skipIf(!require('zlib').gzipSync, 'should be recomputed from the body if updated, with Content-Encoding:gzip', function () { | ||
var message = new Message(Buffer.concat([ | ||
new Buffer( | ||
'Content-Type: text/plain; charset=UTF-8\r\n' + | ||
'Content-Encoding: gzip\r\n' + | ||
'\r\n', 'ascii' | ||
), | ||
new Buffer([0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x4b, 0xcb, 0xcf, 0x4f, 0x4a, 0x2c, 0x02, 0x00, 0x95, 0x1f, 0xf6, 0x9e, 0x06, 0x00, 0x00, 0x00]) | ||
])); | ||
expect(message.body, 'to equal', 'foobar'); | ||
message.body = 'barfoo'; | ||
expect(message.rawBody, 'to equal', new Buffer([0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x4b, 0x4a, 0x2c, 0x4a, 0xcb, 0xcf, 0x07, 0x00, 0x2b, 0x85, 0xa8, 0xe2, 0x06, 0x00, 0x00, 0x00])); | ||
expect(message.body, 'to equal', 'barfoo'); | ||
}); | ||
}); | ||
@@ -441,0 +507,0 @@ |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
125937
2595
2