1
0
mirror of https://github.com/mgerb/mywebsite synced 2026-01-12 02:42:48 +00:00

updated bunch of file paths and changed the way posts are loaded

This commit is contained in:
2016-01-05 12:28:04 -06:00
parent 4bb8cae81e
commit 6ab45fe935
13249 changed files with 317868 additions and 2101398 deletions

View File

@@ -1,25 +1,29 @@
'use strict';
var Compiler = require('./compiler');
var mailcomposer = require('mailcomposer');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var directTransport = require('nodemailer-direct-transport');
var smtpTransport = require('nodemailer-smtp-transport');
var packageData = require('../package.json');
var fs = require('fs');
var hyperquest = require('hyperquest');
var needle = require('needle');
var PassThrough = require('stream').PassThrough;
// Export createTransport method
module.exports.createTransport = function(transporter) {
module.exports.createTransport = function (transporter, defaults) {
transporter = transporter || directTransport({
debug: true
});
if (typeof transporter === 'object' && typeof transporter.send !== 'function') {
if (
(typeof transporter === 'object' && typeof transporter.send !== 'function') ||
(typeof transporter === 'string' && /^smtps?:/i.test(transporter))
) {
transporter = smtpTransport(transporter);
}
return new Nodemailer(transporter);
return new Nodemailer(transporter, defaults);
};
/**
@@ -28,9 +32,11 @@ module.exports.createTransport = function(transporter) {
* @constructor
* @param {Object} transporter Transport object instance to pass the mails to
*/
function Nodemailer(transporter) {
function Nodemailer(transporter, defaults) {
EventEmitter.call(this);
this._defaults = defaults || {};
this._plugins = {
compile: [],
stream: []
@@ -45,7 +51,7 @@ function Nodemailer(transporter) {
}
util.inherits(Nodemailer, EventEmitter);
Nodemailer.prototype.use = function(step, plugin) {
Nodemailer.prototype.use = function (step, plugin) {
step = (step || '').toString();
if (!this._plugins.hasOwnProperty(step)) {
this._plugins[step] = [plugin];
@@ -57,7 +63,7 @@ Nodemailer.prototype.use = function(step, plugin) {
/**
* Optional close method, passed to the underlying transport object
*/
Nodemailer.prototype.close = function( /* possible arguments */ ) {
Nodemailer.prototype.close = function ( /* possible arguments */ ) {
var args = Array.prototype.slice.call(arguments);
if (typeof this.transporter.close === 'function') {
this.transporter.close.apply(this.transporter, args);
@@ -70,10 +76,32 @@ Nodemailer.prototype.close = function( /* possible arguments */ ) {
* @param {Object} data E-data description
* @param {Function} callback Callback to run once the sending succeeded or failed
*/
Nodemailer.prototype.sendMail = function(data, callback) {
Nodemailer.prototype.sendMail = function (data, callback) {
var promise;
if (!callback && typeof Promise === 'function') {
promise = new Promise(function (resolve, reject) {
callback = callbackPromise(resolve, reject);
});
}
data = data || {};
data.headers = data.headers || {};
callback = callback || function() {};
callback = callback || function () {};
// apply defaults
Object.keys(this._defaults).forEach(function (key) {
if (!(key in data)) {
data[key] = this._defaults[key];
} else if (key === 'headers') {
// headers is a special case. Allow setting individual default headers
Object.keys(this._defaults.headers || {}).forEach(function (key) {
if (!(key in data.headers)) {
data.headers[key] = this._defaults.headers[key];
}
}.bind(this));
}
}.bind(this));
var mail = {
data: data,
@@ -82,27 +110,46 @@ Nodemailer.prototype.sendMail = function(data, callback) {
};
if (typeof this.transporter === 'string') {
return callback(new Error('Unsupported configuration, downgrade Nodemailer to v0.7.1 or see the migration guide https://github.com/andris9/Nodemailer#migration-guide'));
return callback(new Error('Unsupported configuration, downgrade Nodemailer to v0.7.1 to use it'));
}
this._processPlugins('compile', mail, function(err) {
this._processPlugins('compile', mail, function (err) {
if (err) {
return callback(err);
}
mail.message = new Compiler(mail.data).compile();
mail.message = mailcomposer(mail.data);
if (mail.data.xMailer !== false) {
mail.message.setHeader('X-Mailer', mail.data.xMailer || this._getVersionString());
}
this._processPlugins('stream', mail, function(err) {
if (mail.data.priority) {
switch ((mail.data.priority || '').toString().toLowerCase()) {
case 'high':
mail.message.setHeader('X-Priority', '1 (Highest)');
mail.message.setHeader('X-MSMail-Priority', 'High');
mail.message.setHeader('Importance', 'High');
break;
case 'low':
mail.message.setHeader('X-Priority', '5 (Lowest)');
mail.message.setHeader('X-MSMail-Priority', 'Low');
mail.message.setHeader('Importance', 'Low');
break;
default:
// do not add anything, since all messages are 'Normal' by default
}
}
this._processPlugins('stream', mail, function (err) {
if (err) {
return callback(err);
}
this.transporter.send(mail, callback);
}.bind(this));
}.bind(this));
return promise;
};
/**
@@ -118,8 +165,17 @@ Nodemailer.prototype.sendMail = function(data, callback) {
* @param {String|Number} key Property name or an Array index
* @param {Function} callback Callback function with (err, value)
*/
Nodemailer.prototype.resolveContent = function(data, key, callback) {
Nodemailer.prototype.resolveContent = function (data, key, callback) {
var promise;
if (!callback && typeof Promise === 'function') {
promise = new Promise(function (resolve, reject) {
callback = callbackPromise(resolve, reject);
});
}
var content = data && data[key] && data[key].content || data[key];
var contentStream;
var encoding = (typeof data[key] === 'object' && data[key].encoding || 'utf8')
.toString()
.toLowerCase()
@@ -131,7 +187,7 @@ Nodemailer.prototype.resolveContent = function(data, key, callback) {
if (typeof content === 'object') {
if (typeof content.pipe === 'function') {
return this._resolveStream(content, function(err, value) {
return this._resolveStream(content, function (err, value) {
if (err) {
return callback(err);
}
@@ -141,7 +197,21 @@ Nodemailer.prototype.resolveContent = function(data, key, callback) {
callback(null, value);
}.bind(this));
} else if (/^https?:\/\//i.test(content.path || content.href)) {
return this._resolveStream(hyperquest(content.path || content.href), callback);
contentStream = new PassThrough();
needle.get(content.path || content.href, {
decode_response: false,
parse_response: false,
compressed: true,
follow_max: 5
}).on('end', function (err) {
if (err) {
contentStream.emit('error', err);
}
contentStream.emit('end');
}).pipe(contentStream, {
end: false
});
return this._resolveStream(contentStream, callback);
} else if (/^data:/i.test(content.path || content.href)) {
var parts = (content.path || content.href).match(/^data:((?:[^;]*;)*(?:[^,]*)),(.*)$/i);
if (!parts) {
@@ -158,7 +228,9 @@ Nodemailer.prototype.resolveContent = function(data, key, callback) {
}
// default action, return as is
callback(null, content);
setImmediate(callback.bind(null, null, content));
return promise;
};
/**
@@ -167,12 +239,12 @@ Nodemailer.prototype.resolveContent = function(data, key, callback) {
* @param {Object} stream Readable stream
* @param {Function} callback Callback function with (err, value)
*/
Nodemailer.prototype._resolveStream = function(stream, callback) {
Nodemailer.prototype._resolveStream = function (stream, callback) {
var responded = false;
var chunks = [];
var chunklen = 0;
stream.on('error', function(err) {
stream.on('error', function (err) {
if (responded) {
return;
}
@@ -181,14 +253,14 @@ Nodemailer.prototype._resolveStream = function(stream, callback) {
callback(err);
});
stream.on('data', function(chunk) {
stream.on('data', function (chunk) {
if (chunk && chunk.length) {
chunks.push(chunk);
chunklen += chunk.length;
}
});
stream.on('end', function() {
stream.on('end', function () {
if (responded) {
return;
}
@@ -205,7 +277,7 @@ Nodemailer.prototype._resolveStream = function(stream, callback) {
});
};
Nodemailer.prototype._getVersionString = function() {
Nodemailer.prototype._getVersionString = function () {
return util.format(
'%s (%s; +%s; %s/%s)',
packageData.name,
@@ -216,7 +288,7 @@ Nodemailer.prototype._getVersionString = function() {
);
};
Nodemailer.prototype._processPlugins = function(step, mail, callback) {
Nodemailer.prototype._processPlugins = function (step, mail, callback) {
step = (step || '').toString();
if (!this._plugins.hasOwnProperty(step) || !this._plugins[step].length) {
@@ -225,12 +297,12 @@ Nodemailer.prototype._processPlugins = function(step, mail, callback) {
var plugins = Array.prototype.slice.call(this._plugins[step]);
var processPlugins = function() {
var processPlugins = function () {
if (!plugins.length) {
return callback(null);
}
var plugin = plugins.shift();
plugin(mail, function(err) {
plugin(mail, function (err) {
if (err) {
return callback(err);
}
@@ -239,4 +311,23 @@ Nodemailer.prototype._processPlugins = function(step, mail, callback) {
}.bind(this);
processPlugins();
};
};
/**
* Wrapper for creating a callback than either resolves or rejects a promise
* based on input
*
* @param {Function} resolve Function to run if callback is called
* @param {Function} reject Function to run if callback ends with an error
*/
function callbackPromise(resolve, reject) {
return function () {
var args = Array.prototype.slice.call(arguments);
var err = args.shift();
if (err) {
reject(err);
} else {
resolve.apply(null, args);
}
};
}