-
-
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.
Parsers are now transform streams! I'm not 100% convinced this is easier to use but it is more clear about the mechanics. I think that might make it worthwhile. It also opens up a clear example to how to make your own parser.
- Loading branch information
Showing
11 changed files
with
527 additions
and
240 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
var Transform = require('readable-stream').Transform; | ||
var inherits = require('inherits'); | ||
|
||
function ByteLengthParser(options) { | ||
if (!(this instanceof ByteLengthParser)) { | ||
return new ByteLengthParser(options); | ||
} | ||
Transform.call(this, options); | ||
|
||
options = options || {}; | ||
|
||
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'); | ||
} | ||
|
||
this.length = options.length; | ||
this.buffer = new Buffer(0); | ||
} | ||
|
||
inherits(ByteLengthParser, Transform); | ||
|
||
ByteLengthParser.prototype._transform = function(chunk, encoding, cb) { | ||
var data = Buffer.concat([this.buffer, chunk]); | ||
while (data.length >= this.length) { | ||
var out = data.slice(0, this.length); | ||
this.push(out); | ||
data = data.slice(this.length); | ||
} | ||
this.buffer = data; | ||
cb(); | ||
}; | ||
|
||
ByteLengthParser.prototype._flush = function(cb) { | ||
this.push(this.buffer); | ||
this.buffer = new Buffer(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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
var Transform = require('readable-stream').Transform; | ||
var inherits = require('inherits'); | ||
var bindexOf; | ||
|
||
if (typeof Buffer.prototype.indexOf === 'function') { | ||
bindexOf = function(buff, search, offset, encoding) { | ||
return buff.indexOf(search, offset, encoding); | ||
}; | ||
} else { | ||
bindexOf = require('buffer-indexof'); | ||
} | ||
|
||
function DelimiterParser(options) { | ||
if (!(this instanceof DelimiterParser)) { | ||
return new DelimiterParser(options); | ||
} | ||
Transform.call(this, options); | ||
|
||
options = options || {}; | ||
|
||
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'); | ||
} | ||
|
||
this.delimiter = new Buffer(options.delimiter); | ||
this.buffer = new Buffer(0); | ||
} | ||
|
||
inherits(DelimiterParser, Transform); | ||
|
||
DelimiterParser.prototype._transform = function(chunk, encoding, cb) { | ||
var data = Buffer.concat([this.buffer, chunk]); | ||
var position; | ||
while ((position = bindexOf(data, this.delimiter)) !== -1) { | ||
this.push(data.slice(0, position)); | ||
data = data.slice(position + this.delimiter.length); | ||
} | ||
this.buffer = data; | ||
cb(); | ||
}; | ||
|
||
DelimiterParser.prototype._flush = function(cb) { | ||
this.push(this.buffer); | ||
this.buffer = new Buffer(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
var DelimiterParser = require('./parser-delimiter'); | ||
var inherits = require('inherits'); | ||
|
||
function ReadLineParser(options) { | ||
if (!(this instanceof ReadLineParser)) { | ||
return new ReadLineParser(options); | ||
} | ||
|
||
options = options || {}; | ||
|
||
if (options.delimiter === undefined) { | ||
options.delimiter = new Buffer('\n', 'utf8'); | ||
} | ||
|
||
DelimiterParser.call(this, options); | ||
|
||
var encoding = options.encoding || 'utf8'; | ||
this.delimiter = new Buffer(options.delimiter, encoding); | ||
this.setEncoding(encoding); | ||
} | ||
|
||
inherits(ReadLineParser, DelimiterParser); | ||
module.exports = ReadLineParser; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.