-
Notifications
You must be signed in to change notification settings - Fork 1
/
CommandParseStream.js
32 lines (27 loc) · 1.07 KB
/
CommandParseStream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
var util = require('util');
var Transform = require('stream').Transform;
var StringDecoder = require('string_decoder').StringDecoder;
util.inherits(CommandParseStream, Transform);
var DEFAULT_COMMAND = 'say';
function CommandParseStream() {
if (!(this instanceof CommandParseStream))
return new CommandParseStream();
//decodeStrings has no effect here; bug in node.js #5580
Transform.call(this, {decodeStrings: false});
this._writableState.objectMode = false;
this._readableState.objectMode = true;
this._commandPattern = new RegExp(/^\/(\w+)\s?(.*)?$/);
this._decoder = new StringDecoder('utf8');
}
CommandParseStream.prototype._transform = function(line, encoding, done) {
var command = DEFAULT_COMMAND;
var payload = this._decoder.write(line); //bug in node.js #5580
var commandPatternMatches = payload.match(this._commandPattern);
if (commandPatternMatches) {
command = commandPatternMatches[1];
payload = commandPatternMatches[2];
}
this.push({command: command, payload: payload});
done();
};
module.exports = CommandParseStream;