mailcomposer
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -162,6 +162,7 @@ var Stream = require("stream").Stream, | ||
* <li><b>filePath</b> - Path to a file for streaming</li> | ||
* <li><b>streamSource</b> - Stream object for arbitrary streams</li> | ||
* </ul> | ||
* | ||
* <p>One of <code>contents</code> or <code>filePath</code> must be specified, otherwise | ||
* the attachment is not included</p> | ||
* <p>One of <code>contents</code> or <code>filePath</code> or <code>stream</code> | ||
* must be specified, otherwise the attachment is not included</p> | ||
* | ||
@@ -189,3 +190,13 @@ * @param {Object} attachment Attachment info | ||
if(attachment.filePath || attachment.contents){ | ||
if(attachment.streamSource){ | ||
// check for pause and resume support | ||
if(typeof attachment.streamSource.pause != "function" || | ||
typeof attachment.streamSource.resume != "function"){ | ||
// Unsupported Stream source, skip it | ||
return; | ||
} | ||
attachment.streamSource.pause(); | ||
} | ||
if(attachment.filePath || attachment.contents || attachment.streamSource){ | ||
this._attachments.push(attachment); | ||
@@ -641,2 +652,4 @@ } | ||
node.filePath = attachment.filePath; | ||
}else if(attachment.streamSource){ | ||
node.streamSource = attachment.streamSource; | ||
} | ||
@@ -718,9 +731,9 @@ | ||
contentObject.contents = node.contents; | ||
} | ||
if(node.filePath){ | ||
}else if(node.filePath){ | ||
contentObject.filePath = node.filePath; | ||
}else if(node.streamSource){ | ||
contentObject.streamSource = node.streamSource; | ||
} | ||
if(node.contents || node.filePath){ | ||
if(node.contents || node.filePath || node.streamSource){ | ||
flatTree.push(contentObject); | ||
@@ -846,2 +859,5 @@ } | ||
return; | ||
}else if(element.streamSource){ | ||
this._serveStream(element.streamSource, callback); | ||
return; | ||
} | ||
@@ -855,7 +871,2 @@ | ||
* | ||
* <p>This function opens a file and starts sending 76 bytes long base64 | ||
* encoded lines. To achieve this, the incoming stream is divded into | ||
* chunks of 57 bytes (57/3*4=76) to achieve exactly 76 byte long | ||
* base64</p> | ||
* | ||
* @param {String} filePath Path to the file | ||
@@ -876,38 +887,58 @@ * @param {Function} callback Callback function to run after completion | ||
stream.on("error", (function(error){ | ||
this.emit("data", new Buffer(new Buffer("<ERROR READING FILE>", | ||
"utf-8").toString("base64")+"\r\n", "utf-8")); | ||
process.nextTick(callback); | ||
}).bind(this)); | ||
this._serveStream(stream, callback); | ||
stream.on("data", (function(chunk){ | ||
var data = "", | ||
len = remainder.length + chunk.length, | ||
remainderLength = len % 57, // we use 57 bytes as it composes | ||
// a 76 bytes long base64 string | ||
buffer = new Buffer(len); | ||
remainder.copy(buffer); // copy remainder into the beginning of the new buffer | ||
chunk.copy(buffer, remainder.length); // copy data chunk after the remainder | ||
remainder = buffer.slice(len - remainderLength); // create a new remainder | ||
data = buffer.slice(0, len - remainderLength).toString("base64").replace(/.{76}/g,"$&\r\n"); | ||
if(data.length){ | ||
this.emit("data", new Buffer(data.trim()+"\r\n", "utf-8")); | ||
} | ||
}).bind(this)); | ||
}).bind(this)); | ||
}; | ||
/** | ||
* <p>Pipes a stream source to the e-mail stream</p> | ||
* | ||
* <p>This function resumes the stream and starts sending 76 bytes long base64 | ||
* encoded lines. To achieve this, the incoming stream is divded into | ||
* chunks of 57 bytes (57/3*4=76) to achieve exactly 76 byte long | ||
* base64</p> | ||
* | ||
* @param {Object} stream Stream to be piped | ||
* @param {Function} callback Callback function to run after completion | ||
*/ | ||
MailComposer.prototype._serveStream = function(stream, callback){ | ||
var remainder = new Buffer(0); | ||
stream.on("error", (function(error){ | ||
this.emit("data", new Buffer(new Buffer("<ERROR READING STREAM>", | ||
"utf-8").toString("base64")+"\r\n", "utf-8")); | ||
process.nextTick(callback); | ||
}).bind(this)); | ||
stream.on("data", (function(chunk){ | ||
var data = "", | ||
len = remainder.length + chunk.length, | ||
remainderLength = len % 57, // we use 57 bytes as it composes | ||
// a 76 bytes long base64 string | ||
buffer = new Buffer(len); | ||
stream.on("end", (function(chunk){ | ||
var data; | ||
// stream the remainder (if any) | ||
if(remainder.length){ | ||
data = remainder.toString("base64").replace(/.{76}/g,"$&\r\n"); | ||
this.emit("data", new Buffer(data.trim()+"\r\n", "utf-8")); | ||
} | ||
process.nextTick(callback); | ||
}).bind(this)); | ||
remainder.copy(buffer); // copy remainder into the beginning of the new buffer | ||
chunk.copy(buffer, remainder.length); // copy data chunk after the remainder | ||
remainder = buffer.slice(len - remainderLength); // create a new remainder | ||
data = buffer.slice(0, len - remainderLength).toString("base64").replace(/.{76}/g,"$&\r\n"); | ||
if(data.length){ | ||
this.emit("data", new Buffer(data.trim()+"\r\n", "utf-8")); | ||
} | ||
}).bind(this)); | ||
stream.on("end", (function(chunk){ | ||
var data; | ||
// stream the remainder (if any) | ||
if(remainder.length){ | ||
data = remainder.toString("base64").replace(/.{76}/g,"$&\r\n"); | ||
this.emit("data", new Buffer(data.trim()+"\r\n", "utf-8")); | ||
} | ||
process.nextTick(callback); | ||
}).bind(this)); | ||
// resume streaming if paused | ||
stream.resume(); | ||
}; | ||
@@ -914,0 +945,0 @@ |
{ | ||
"name": "mailcomposer", | ||
"description": "Compose E-Mail messages", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"author" : "Andris Reinman", | ||
@@ -6,0 +6,0 @@ "maintainers":[ |
@@ -19,3 +19,3 @@ # mailcomposer | ||
* **HTML** content as well as **plain text** alternative | ||
* **Attachments** and streaming for larger files | ||
* **Attachments** and streaming for larger files (use strings, buffers, files or binary streams as attachments) | ||
* **Embedded images** in HTML | ||
@@ -187,6 +187,7 @@ * usage of **your own** transport mechanism | ||
* **filePath** - path to a file if you want to stream the file instead of including it (better for larger attachments) | ||
* **streamSource** - Stream object for arbitrary binary streams if you want to stream the contents (needs to support *pause*/*resume*) | ||
* **contentType** - content type for the attachment, if not set will be derived from the `fileName` property | ||
One of `contents` or `filePath` must be specified, if both are missing, the attachment | ||
will be discarded. Other fields are optional. | ||
One of `contents`, `filePath` or `streamSource` must be specified, if none is | ||
present, the attachment will be discarded. Other fields are optional. | ||
@@ -193,0 +194,0 @@ Attachments can be added as many as you want. |
@@ -611,3 +611,3 @@ var testCase = require('nodeunit').testCase, | ||
}, | ||
"Attachment stream": function(test){ | ||
"Attachment file stream": function(test){ | ||
var mc = new MailComposer(); | ||
@@ -630,2 +630,23 @@ mc.setMessageOption(); | ||
}, | ||
"Attachment source stream": function(test){ | ||
var mc = new MailComposer(); | ||
var fileStream = fs.createReadStream(__dirname+"/textfile.txt"); | ||
mc.setMessageOption(); | ||
mc.addAttachment({ | ||
fileName: "file.txt", | ||
streamSource: fileStream | ||
}); | ||
mc.streamMessage(); | ||
var mp = new MailParser(); | ||
mc.pipe(mp); | ||
mp.on("end", function(mail){ | ||
test.equal(mail.attachments[0].checksum, "59fbcbcaf18cb9232f7da6663f374eb9"); | ||
test.done(); | ||
}); | ||
}, | ||
"escape SMTP": function(test){ | ||
@@ -632,0 +653,0 @@ var mc = new MailComposer({escapeSMTP: true}); |
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
566354
2775
254