Skip to content
Browse files

release 0.0.5 with zlib compression capabilities

  • Loading branch information...
1 parent c8d068c commit 5b2847d8265be9d0ddfbaf94e4f856635e1f8a0b @niftylettuce committed Sep 21, 2012
Showing with 79 additions and 13 deletions.
  1. +15 −1 Readme.md
  2. +63 −11 lib/main.js
  3. +1 −1 package.json
View
16 Readme.md
@@ -1,8 +1,21 @@
-# node-email-templates <sup>0.0.4</sup>
+# node-email-templates <sup>0.0.5</sup>
Node.js module for rendering beautiful emails with [ejs][1] templates and email-friendly inline CSS using [juice][2].
+**v0.0.5**:
+* Added support for an optional [zlib][8] compression type (e.g. you can return compressed html/text buffer for db storage)
+
+```bash
+...
+template('newsletter', locals, 'deflateRaw', function(err, html, text) {
+ // The `html` and `text` are buffers compressed using zlib.deflateRaw
+ // <http://nodejs.org/docs/latest/api/zlib.html#zlib_zlib_deflateraw_buf_callback>
+ // **NOTE**: You could also pass 'deflate' or 'gzip' if necessary, and it works with batch rendering as well
+})
+...
+```
+
**v0.0.4 (with bug fix for 0.0.3)**:
* Removed requirement for `style.css` and `text.ejs` files with compatibility in `node` v0.6.x to v0.8.x (utilizes `path.exists` vs. `fs.exists` respectively).
@@ -365,3 +378,4 @@ MIT Licensed
[5]: https://github.com/voodootikigod/postmark.js
[6]: https://github.com/andris9/Nodemailer#well-known-services-for-smtp
[7]: http://developer.postmarkapp.com/developer-build.html#message-format
+[8]: http://nodejs.org/docs/latest/api/zlib.html
View
74 lib/main.js
@@ -15,7 +15,10 @@ var path = require('path')
, async = require('async')
, ejs = require('ejs')
, juice = require('juice')
- , _ = require('underscore');
+ , zlib = require('zlib')
+ , _ = require('underscore')
+
+var validBufferTypes = [ 'deflate', 'deflateRaw', 'gzip' ]
// # Email Template
var EmailTemplate = function(templateDirectory, defaults, done) {
@@ -27,19 +30,47 @@ var EmailTemplate = function(templateDirectory, defaults, done) {
this.html = '';
this.text = '';
this.stylesheet = '';
- this.render = function(locals, html, text, stylesheet, callback) {
- if (typeof html === 'function') {
- callback = html;
- html = that.html;
- text = that.text;
- stylesheet = that.stylesheet;
+ this.bufferType = ''
+ this.render = function(locals, html, text, stylesheet, bufferType, callback) {
+ // Check if `bufferType` is a function (callback), or string
+ var isBuffer = false
+ if (typeof bufferType === 'function') {
+ callback = bufferType
+ } else if (typeof bufferType === 'string') {
+ // Ensure that `bufferType` is a valid `zlib` compression type
+ if (_.indexOf(validBufferTypes, bufferType) === -1) {
+ throw new Error('`zlib.' + bufferType + '` does not exist or is not a valid buffer type')
+ } else {
+ isBuffer = true
+ }
+ }
+
+ // Check if `that.bufferType` is not an empty string, so we'll use the global bufferType instead
+ if (that.bufferType !== '') {
+ isBuffer = true
+ bufferType = that.bufferType
}
+
+ if (!html) html = that.html
+ if (!text) text = that.text
+ if (!stylesheet) stylesheet = that.stylesheet
locals = _.defaults(locals, (typeof defaults === 'object') ? defaults : {});
html = ejs.render(html, locals);
text = (text) ? ejs.render(text, locals) : '';
if (stylesheet) html = juice(html, stylesheet);
if (typeof html === 'function') callback = html;
- return callback(null, html, text);
+
+ // return a compressed buffer
+ if (isBuffer) {
+ async.map([ new Buffer(html), new Buffer(text) ], zlib[bufferType], function(err, buffers) {
+ if (err) return callback(err)
+ html = buffers[0]
+ text = buffers[1]
+ callback(null, html, text)
+ })
+ } else {
+ callback(null, html, text)
+ }
};
// Ensure the `templateDirectory` is undefined
@@ -54,7 +85,20 @@ var EmailTemplate = function(templateDirectory, defaults, done) {
if (!stats.isDirectory())
return done(new Error('templateDirectory is not a valid directory path'));
- done(null, function(templateName, locals, callback) {
+ done(null, function(templateName, locals, bufferType, callback) {
+
+ // Check if `bufferType` is a function (callback), or string
+ var isBuffer = false
+ if (typeof bufferType === 'function') {
+ callback = bufferType
+ } else if (typeof bufferType === 'string') {
+ // Ensure that `bufferType` is a valid `zlib` compression type
+ if (_.indexOf(validBufferTypes, bufferType) === -1) {
+ throw new Error('`zlib.' + bufferType + '` does not exist or is not a valid buffer type')
+ } else {
+ isBuffer = true
+ }
+ }
// Fallback if user doesn't pass all the args
var batchEmail = false;
@@ -75,8 +119,16 @@ var EmailTemplate = function(templateDirectory, defaults, done) {
// Batch load function
var batchCheck = function() {
- if (batchEmail) return callback(null, that.render);
- else that.render(locals, callback);
+ if (batchEmail) {
+ if (isBuffer) that.bufferType = bufferType
+ return callback(null, that.render);
+ }
+ if (isBuffer) {
+ that.render(locals, null, null, null, bufferType, callback)
+ } else {
+ that.render(locals, null, null, null, callback)
+ }
+ //that.render(locals, callback);
};
// Ensure the `templateName` is valid
View
2 package.json
@@ -1,7 +1,7 @@
{
"name": "email-templates"
, "description": "Node.js module for rendering beautiful emails with ejs templates and email-friendly inline CSS using juice."
- , "version": "0.0.4"
+ , "version": "0.0.5"
, "author": "Nick Baugh <niftylettuce@gmail.com>"
, "contributors": [
{ "name": "Nick Baugh", "email": "niftylettuce@gmail.com" }

0 comments on commit 5b2847d

Please sign in to comment.
Something went wrong with that request. Please try again.