Skip to content

Commit

Permalink
v3.0.0. Switch over to ES6 syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Dec 9, 2016
1 parent e14d43c commit 524aec5
Show file tree
Hide file tree
Showing 8 changed files with 1,395 additions and 1,273 deletions.
20 changes: 17 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {
allowShortCircuit: true
}],
'no-unused-vars': 2,
'no-undef': 2,
'no-undefined': 2,
'handle-callback-err': 2,
'no-new': 2,
'new-cap': 2,
Expand All @@ -39,18 +39,32 @@ module.exports = {
'block-scoped-var': 2,
'no-sequences': 2,
'no-throw-literal': 2,
'no-useless-call': 2,
'no-useless-concat': 2,
'no-void': 2,
yoda: 2,
'no-undef': 2,
'global-require': 2,
'no-var': 2,
'no-bitwise': 2,
'no-lonely-if': 2,
'no-mixed-spaces-and-tabs': 2,
'no-console': 2
'arrow-body-style': [2, 'as-needed'],
'arrow-parens': [2, 'as-needed'],
'prefer-arrow-callback': 2,
'object-shorthand': 2,
'prefer-spread': 2
},
env: {
es6: false,
es6: true,
node: true
},
extends: 'eslint:recommended',
globals: {
it: true,
describe: true,
beforeEach: true,
afterEach: true
},
fix: true
};
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
language: node_js
sudo: false
node_js:
- 0.12
- iojs
- 4
- 5
- 6
notifications:
email:
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v3.0.0 2016-12-09

* Use ES6 syntax
* Updated logging to support structured output for real Bunyan logger instances

## v2.12.1 2016-10-10

* Fixed invalid SIZE detection
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ This module is the successor for the client part of the (now deprecated) SMTP mo

## Other similar packages you might be interested in

* **[nodemailer](https://github.com/nodemailer/nodemailer)** – all in one package to send email from Node.js
* **[smtp-server](https://github.com/andris9/smtp-server)** – add SMTP server interface to your application
* **[zone-mta](https://github.com/zone-eu/zone-mta)** – full featured outbound MTA built using smtp-connection and smtp-server modules
- **[nodemailer](https://github.com/nodemailer/nodemailer)** – all in one package to send email from Node.js
- **[smtp-server](https://github.com/andris9/smtp-server)** – add SMTP server interface to your application
- **[zone-mta](https://github.com/zone-eu/zone-mta)** – full featured outbound MTA built using smtp-connection and smtp-server modules

## Usage

Expand All @@ -22,14 +22,14 @@ npm install smtp-connection

Require in your script

```
var SMTPConnection = require('smtp-connection');
```javascript
const SMTPConnection = require('smtp-connection');
```

### Create SMTPConnection instance

```javascript
var connection = new SMTPConnection(options);
let connection = new SMTPConnection(options);
```

Where
Expand All @@ -48,8 +48,8 @@ Where
- **options.greetingTimeout** how many milliseconds to wait for the greeting after connection is established
- **options.socketTimeout** how many milliseconds of inactivity to allow
- **options.logger** optional [bunyan](https://github.com/trentm/node-bunyan) compatible logger instance. If set to `true` then logs to console. If value is not set or is `false` then nothing is logged
- **options.debug** if set to true, then logs SMTP traffic, otherwise logs only transaction events
- **options.transactionLog** if set to true, then logs SMTP traffic without message content
- **options.debug** if set to true, then logs SMTP traffic and message content, otherwise logs only transaction events
- **options.authMethod** defines preferred authentication method, e.g. 'PLAIN'
- **options.tls** defines additional options to be passed to the socket constructor, e.g. _{rejectUnauthorized: true}_
- **options.socket** - initialized socket to use instead of creating a new one
Expand Down Expand Up @@ -104,7 +104,7 @@ If a [XOAuth2](https://github.com/andris9/xoauth2) token generator is used as th
**XOAuth2 Example**

```javascript
var generator = require('xoauth2').createXOAuth2Generator({
let generator = require('xoauth2').createXOAuth2Generator({
user: '{username}',
clientId: '{Client ID}',
clientSecret: '{Client Secret}',
Expand Down
172 changes: 87 additions & 85 deletions lib/data-stream.js
Original file line number Diff line number Diff line change
@@ -1,111 +1,113 @@
'use strict';

var stream = require('stream');
var Transform = stream.Transform;
var util = require('util');

module.exports = DataStream;
const stream = require('stream');
const Transform = stream.Transform;

/**
* Escapes dots in the beginning of lines. Ends the stream with <CR><LF>.<CR><LF>
* Also makes sure that only <CR><LF> sequences are used for linebreaks
*
* @param {Object} options Stream options
*/
function DataStream(options) {
// init Transform
this.options = options || {};
this._curLine = '';
class DataStream extends Transform {

this.inByteCount = 0;
this.outByteCount = 0;
this.lastByte = false;
constructor(options) {
super(options);
// init Transform
this.options = options || {};
this._curLine = '';

Transform.call(this, this.options);
}
util.inherits(DataStream, Transform);
this.inByteCount = 0;
this.outByteCount = 0;
this.lastByte = false;

/**
* Escapes dots
*/
DataStream.prototype._transform = function (chunk, encoding, done) {
var chunks = [];
var chunklen = 0;
var i, len, lastPos = 0;
var buf;

if (!chunk || !chunk.length) {
return done();
}

if (typeof chunk === 'string') {
chunk = new Buffer(chunk);
}
/**
* Escapes dots
*/
_transform(chunk, encoding, done) {
let chunks = [];
let chunklen = 0;
let i, len, lastPos = 0;
let buf;

this.inByteCount += chunk.length;
if (!chunk || !chunk.length) {
return done();
}

for (i = 0, len = chunk.length; i < len; i++) {
if (chunk[i] === 0x2E) { // .
if (
(i && chunk[i - 1] === 0x0A) ||
(!i && (!this.lastByte || this.lastByte === 0x0A))
) {
buf = chunk.slice(lastPos, i + 1);
chunks.push(buf);
chunks.push(new Buffer('.'));
chunklen += buf.length + 1;
lastPos = i + 1;
}
} else if (chunk[i] === 0x0A) { // .
if (
(i && chunk[i - 1] !== 0x0D) ||
(!i && this.lastByte !== 0x0D)
) {
if (i > lastPos) {
buf = chunk.slice(lastPos, i);
if (typeof chunk === 'string') {
chunk = new Buffer(chunk);
}

this.inByteCount += chunk.length;

for (i = 0, len = chunk.length; i < len; i++) {
if (chunk[i] === 0x2E) { // .
if (
(i && chunk[i - 1] === 0x0A) ||
(!i && (!this.lastByte || this.lastByte === 0x0A))
) {
buf = chunk.slice(lastPos, i + 1);
chunks.push(buf);
chunklen += buf.length + 2;
} else {
chunklen += 2;
chunks.push(new Buffer('.'));
chunklen += buf.length + 1;
lastPos = i + 1;
}
} else if (chunk[i] === 0x0A) { // .
if (
(i && chunk[i - 1] !== 0x0D) ||
(!i && this.lastByte !== 0x0D)
) {
if (i > lastPos) {
buf = chunk.slice(lastPos, i);
chunks.push(buf);
chunklen += buf.length + 2;
} else {
chunklen += 2;
}
chunks.push(new Buffer('\r\n'));
lastPos = i + 1;
}
chunks.push(new Buffer('\r\n'));
lastPos = i + 1;
}
}
}

if (chunklen) {
// add last piece
if (lastPos < chunk.length) {
buf = chunk.slice(lastPos);
chunks.push(buf);
chunklen += buf.length;
if (chunklen) {
// add last piece
if (lastPos < chunk.length) {
buf = chunk.slice(lastPos);
chunks.push(buf);
chunklen += buf.length;
}

this.outByteCount += chunklen;
this.push(Buffer.concat(chunks, chunklen));
} else {
this.outByteCount += chunk.length;
this.push(chunk);
}

this.outByteCount += chunklen;
this.push(Buffer.concat(chunks, chunklen));
} else {
this.outByteCount += chunk.length;
this.push(chunk);
this.lastByte = chunk[chunk.length - 1];
done();
}

this.lastByte = chunk[chunk.length - 1];
done();
};

/**
* Finalizes the stream with a dot on a single line
*/
DataStream.prototype._flush = function (done) {
var buf;
if (this.lastByte === 0x0A) {
buf = new Buffer('.\r\n');
} else if (this.lastByte === 0x0D) {
buf = new Buffer('\n.\r\n');
} else {
buf = new Buffer('\r\n.\r\n');
/**
* Finalizes the stream with a dot on a single line
*/
_flush(done) {
let buf;
if (this.lastByte === 0x0A) {
buf = new Buffer('.\r\n');
} else if (this.lastByte === 0x0D) {
buf = new Buffer('\n.\r\n');
} else {
buf = new Buffer('\r\n.\r\n');
}
this.outByteCount += buf.length;
this.push(buf);
done();
}
this.outByteCount += buf.length;
this.push(buf);
done();
};

}

module.exports = DataStream;
Loading

0 comments on commit 524aec5

Please sign in to comment.