-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[all] Change parsers to be es6 classes
- Reduces code a bit - Provides a better code example - clean up options passing into transform streams
- Loading branch information
Showing
12 changed files
with
218 additions
and
285 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,45 +1,38 @@ | ||
'use strict'; | ||
const Buffer = require('safe-buffer').Buffer; | ||
const inherits = require('util').inherits; | ||
const Transform = require('stream').Transform; | ||
|
||
function ByteLengthParser(options) { | ||
if (!(this instanceof ByteLengthParser)) { | ||
return new ByteLengthParser(options); | ||
} | ||
Transform.call(this, options); | ||
module.exports = class ByteLengthParser extends Transform { | ||
constructor(options) { | ||
super(options); | ||
options = options || {}; | ||
|
||
options = options || {}; | ||
if (typeof options.length !== 'number') { | ||
throw new TypeError('"length" is not a number'); | ||
} | ||
|
||
if (typeof options.length !== 'number') { | ||
throw new TypeError('"length" is not a number'); | ||
} | ||
if (options.length < 1) { | ||
throw new TypeError('"length" is not greater than 0'); | ||
} | ||
|
||
if (options.length < 1) { | ||
throw new TypeError('"length" is not greater than 0'); | ||
this.length = options.length; | ||
this.buffer = Buffer.alloc(0); | ||
} | ||
|
||
this.length = options.length; | ||
this.buffer = Buffer.alloc(0); | ||
} | ||
|
||
inherits(ByteLengthParser, Transform); | ||
|
||
ByteLengthParser.prototype._transform = function(chunk, encoding, cb) { | ||
let data = Buffer.concat([this.buffer, chunk]); | ||
while (data.length >= this.length) { | ||
const out = data.slice(0, this.length); | ||
this.push(out); | ||
data = data.slice(this.length); | ||
_transform(chunk, encoding, cb) { | ||
let data = Buffer.concat([this.buffer, chunk]); | ||
while (data.length >= this.length) { | ||
const out = data.slice(0, this.length); | ||
this.push(out); | ||
data = data.slice(this.length); | ||
} | ||
this.buffer = data; | ||
cb(); | ||
} | ||
this.buffer = data; | ||
cb(); | ||
}; | ||
|
||
ByteLengthParser.prototype._flush = function(cb) { | ||
this.push(this.buffer); | ||
this.buffer = Buffer.alloc(0); | ||
cb(); | ||
_flush(cb) { | ||
this.push(this.buffer); | ||
this.buffer = Buffer.alloc(0); | ||
cb(); | ||
} | ||
}; | ||
|
||
module.exports = ByteLengthParser; |
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,45 +1,38 @@ | ||
'use strict'; | ||
const Buffer = require('safe-buffer').Buffer; | ||
const inherits = require('util').inherits; | ||
const Transform = require('stream').Transform; | ||
|
||
function DelimiterParser(options) { | ||
if (!(this instanceof DelimiterParser)) { | ||
return new DelimiterParser(options); | ||
} | ||
Transform.call(this, options); | ||
module.exports = class DelimiterParser extends Transform { | ||
constructor(options) { | ||
options = options || {}; | ||
super(options); | ||
|
||
options = options || {}; | ||
if (options.delimiter === undefined) { | ||
throw new TypeError('"delimiter" is not a bufferable object'); | ||
} | ||
|
||
if (options.delimiter === undefined) { | ||
throw new TypeError('"delimiter" is not a bufferable object'); | ||
} | ||
if (options.delimiter.length === 0) { | ||
throw new TypeError('"delimiter" has a 0 or undefined length'); | ||
} | ||
|
||
if (options.delimiter.length === 0) { | ||
throw new TypeError('"delimiter" has a 0 or undefined length'); | ||
this.delimiter = Buffer.from(options.delimiter); | ||
this.buffer = Buffer.alloc(0); | ||
} | ||
|
||
this.delimiter = Buffer.from(options.delimiter); | ||
this.buffer = Buffer.alloc(0); | ||
} | ||
|
||
inherits(DelimiterParser, Transform); | ||
|
||
DelimiterParser.prototype._transform = function(chunk, encoding, cb) { | ||
let data = Buffer.concat([this.buffer, chunk]); | ||
let position; | ||
while ((position = data.indexOf(this.delimiter)) !== -1) { | ||
this.push(data.slice(0, position)); | ||
data = data.slice(position + this.delimiter.length); | ||
_transform(chunk, encoding, cb) { | ||
let data = Buffer.concat([this.buffer, chunk]); | ||
let position; | ||
while ((position = data.indexOf(this.delimiter)) !== -1) { | ||
this.push(data.slice(0, position)); | ||
data = data.slice(position + this.delimiter.length); | ||
} | ||
this.buffer = data; | ||
cb(); | ||
} | ||
this.buffer = data; | ||
cb(); | ||
}; | ||
|
||
DelimiterParser.prototype._flush = function(cb) { | ||
this.push(this.buffer); | ||
this.buffer = Buffer.alloc(0); | ||
cb(); | ||
_flush(cb) { | ||
this.push(this.buffer); | ||
this.buffer = Buffer.alloc(0); | ||
cb(); | ||
} | ||
}; | ||
|
||
module.exports = DelimiterParser; |
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,26 +1,18 @@ | ||
'use strict'; | ||
|
||
const inherits = require('util').inherits; | ||
const Buffer = require('safe-buffer').Buffer; | ||
const DelimiterParser = require('./delimiter'); | ||
|
||
function ReadlineParser(options) { | ||
if (!(this instanceof ReadlineParser)) { | ||
return new ReadlineParser(options); | ||
} | ||
module.exports = class ReadLineParser extends DelimiterParser { | ||
constructor(options) { | ||
const opts = Object.assign({ | ||
delimiter: Buffer.from('\n', 'utf8'), | ||
encoding: 'utf8' | ||
}, options); | ||
|
||
options = options || {}; | ||
if (typeof opts.delimiter === 'string') { | ||
opts.delimiter = Buffer.from(opts.delimiter, opts.encoding); | ||
} | ||
|
||
if (options.delimiter === undefined) { | ||
options.delimiter = Buffer.from('\n', 'utf8'); | ||
super(opts); | ||
} | ||
|
||
DelimiterParser.call(this, options); | ||
|
||
const encoding = options.encoding || 'utf8'; | ||
this.delimiter = Buffer.from(options.delimiter, encoding); | ||
this.setEncoding(encoding); | ||
} | ||
|
||
inherits(ReadlineParser, DelimiterParser); | ||
module.exports = ReadlineParser; | ||
}; |
Oops, something went wrong.