Skip to content

Commit

Permalink
move date functions to separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
zackschuster committed Jun 24, 2018
1 parent 0a9bfd6 commit 557c480
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 61 deletions.
1 change: 1 addition & 0 deletions email.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
exports.server = require('./smtp/client');
exports.message = require('./smtp/message');
exports.date = require('./smtp/date');
exports.SMTP = require('./smtp/smtp');
exports.error = require('./smtp/error');
29 changes: 29 additions & 0 deletions smtp/date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function getRFC2822Date(date = new Date(), useUtc = false) {
if (useUtc) {
return getRFC2822DateUTC(date);
}

const dates = date
.toString()
.replace('GMT', '')
.replace(/\s\(.*\)$/, '')
.split(' ');

dates[0] = dates[0] + ',';

const day = dates[1];
dates[1] = dates[2];
dates[2] = day;

return dates.join(' ');
}

function getRFC2822DateUTC(date = new Date()) {
const dates = date.toUTCString().split(' ');
dates.pop(); // remove timezone
dates.push('+0000');
return dates.join(' ');
}

exports.getRFC2822Date = getRFC2822Date;
exports.getRFC2822DateUTC = getRFC2822DateUTC;
31 changes: 2 additions & 29 deletions smtp/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,15 @@ const os = require('os');
const path = require('path');
const mimeWordEncode = require('emailjs-mime-codec').mimeWordEncode;
const addressparser = require('addressparser');
const { getRFC2822Date } = require('./date');

const CRLF = '\r\n';
const MIMECHUNK = 76; // MIME standard wants 76 char chunks when sending out.
const MIME64CHUNK = MIMECHUNK * 6; // meets both base64 and mime divisibility
const BUFFERSIZE = MIMECHUNK * 24 * 7; // size of the message stream buffer

let counter = 0;

function getRFC2822Date(date = new Date(), useUtc = false) {
if (useUtc) {
return getRFC2822DateUTC(date);
}

const dates = date
.toString()
.replace('GMT', '')
.replace(/\s\(.*\)$/, '')
.split(' ');

dates[0] = dates[0] + ',';

const day = dates[1];
dates[1] = dates[2];
dates[2] = day;

return dates.join(' ');
}

function getRFC2822DateUTC(date = new Date()) {
const dates = date.toUTCString().split(' ');
dates.pop(); // remove timezone
dates.push('+0000');
return dates.join(' ');
}

function generate_boundary() {
let text = '';
const possible =
Expand Down Expand Up @@ -566,5 +541,3 @@ class MessageStream extends Stream {
exports.Message = Message;
exports.BUFFERSIZE = BUFFERSIZE;
exports.create = headers => new Message(headers);
exports.getRFC2822Date = getRFC2822Date;
exports.getRFC2822DateUTC = getRFC2822DateUTC;
39 changes: 39 additions & 0 deletions test/date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
describe('rfc2822 dates', function() {
const { expect } = require('chai');
const { date } = require('../email');

var d_utc = dt => date.getRFC2822DateUTC(new Date(dt));
var d = (dt, utc = false) => date.getRFC2822Date(new Date(dt), utc);

it('should match standard regex', function(done) {
// RFC 2822 regex: For details see https://tools.ietf.org/html/rfc2822#section-3.3
// thanks to moment.js for the listing: https://github.com/moment/moment/blob/a831fc7e2694281ce31e4f090bbcf90a690f0277/src/lib/create/from-string.js#L101
var rfc2822re = /^(?:(Mon|Tue|Wed|Thu|Fri|Sat|Sun),?\s)?(\d{1,2})\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s(\d{2,4})\s(\d\d):(\d\d)(?::(\d\d))?\s(?:(UT|GMT|[ECMP][SD]T)|([Zz])|([+-]\d{4}))$/;
expect(d(0)).to.match(rfc2822re);
expect(d(329629726785)).to.match(rfc2822re);
expect(d(729629726785)).to.match(rfc2822re);
expect(d(1129629726785)).to.match(rfc2822re);
expect(d(1529629726785)).to.match(rfc2822re);

done();
});

it('should produce proper UTC dates', function(done) {
expect(d_utc(0)).to.equal('Thu, 01 Jan 1970 00:00:00 +0000');
expect(d_utc(0)).to.equal(d(0, true));

expect(d_utc(329629726785)).to.equal('Thu, 12 Jun 1980 03:48:46 +0000');
expect(d_utc(329629726785)).to.equal(d(329629726785, true));

expect(d_utc(729629726785)).to.equal('Sat, 13 Feb 1993 18:55:26 +0000');
expect(d_utc(729629726785)).to.equal(d(729629726785, true));

expect(d_utc(1129629726785)).to.equal('Tue, 18 Oct 2005 10:02:06 +0000');
expect(d_utc(1129629726785)).to.equal(d(1129629726785, true));

expect(d_utc(1529629726785)).to.equal('Fri, 22 Jun 2018 01:08:46 +0000');
expect(d_utc(1529629726785)).to.equal(d(1529629726785, true));

done();
});
});
32 changes: 0 additions & 32 deletions test/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,38 +54,6 @@ describe('messages', function() {
smtp.close(done);
});

it('rfc2822 date', function(done) {
var d_utc = dt => email.message.getRFC2822DateUTC(new Date(dt));
var d = (dt, utc = false) =>
email.message.getRFC2822Date(new Date(dt), utc);

expect(d_utc(0)).to.equal('Thu, 01 Jan 1970 00:00:00 +0000');
expect(d_utc(0)).to.equal(d(0, true));

expect(d_utc(329629726785)).to.equal('Thu, 12 Jun 1980 03:48:46 +0000');
expect(d_utc(329629726785)).to.equal(d(329629726785, true));

expect(d_utc(729629726785)).to.equal('Sat, 13 Feb 1993 18:55:26 +0000');
expect(d_utc(729629726785)).to.equal(d(729629726785, true));

expect(d_utc(1129629726785)).to.equal('Tue, 18 Oct 2005 10:02:06 +0000');
expect(d_utc(1129629726785)).to.equal(d(1129629726785, true));

expect(d_utc(1529629726785)).to.equal('Fri, 22 Jun 2018 01:08:46 +0000');
expect(d_utc(1529629726785)).to.equal(d(1529629726785, true));

// RFC 2822 regex: For details see https://tools.ietf.org/html/rfc2822#section-3.3
// thanks to moment.js for the listing: https://github.com/moment/moment/blob/a831fc7e2694281ce31e4f090bbcf90a690f0277/src/lib/create/from-string.js#L101
var rfc2822re = /^(?:(Mon|Tue|Wed|Thu|Fri|Sat|Sun),?\s)?(\d{1,2})\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s(\d{2,4})\s(\d\d):(\d\d)(?::(\d\d))?\s(?:(UT|GMT|[ECMP][SD]T)|([Zz])|([+-]\d{4}))$/;
expect(d(0)).to.match(rfc2822re);
expect(d(329629726785)).to.match(rfc2822re);
expect(d(729629726785)).to.match(rfc2822re);
expect(d(1129629726785)).to.match(rfc2822re);
expect(d(1529629726785)).to.match(rfc2822re);

done();
});

it('simple text message', function(done) {
var message = {
subject: 'this is a test TEXT message from emailjs',
Expand Down

0 comments on commit 557c480

Please sign in to comment.