-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
1,395 additions
and
1,273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.