/
index.js
68 lines (49 loc) 路 1.44 KB
/
index.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*!
* stream-snitch
* Event emitter for watching text streams with regex patterns
* Dan Motzenbecker <dan@oxism.com>
* MIT Licensed
*/
'use strict';
var Writable = require('stream').Writable,
inherits = require('util').inherits,
StreamSnitch;
module.exports = StreamSnitch = function(regex, fn, options) {
if (!(this instanceof StreamSnitch)) {
return new StreamSnitch(regex, fn, options);
}
var isFn = typeof fn === 'function';
if (!isFn) options = fn;
options = options || {};
this.regex = regex;
this._buffer = '';
this.bufferCap = options.bufferCap || 1048576;
options.decodeStrings = false;
delete options.bufferCap;
Writable.call(this, options);
if (isFn) this.on('match', fn);
}
inherits(StreamSnitch, Writable);
StreamSnitch.prototype._write = function(chunk, encoding, cb) {
var match, lastMatch;
if (Buffer.byteLength(this._buffer) > this.bufferCap) this.clearBuffer();
this._buffer += chunk;
while (match = this.regex.exec(this._buffer)) {
this.emit('match', match);
lastMatch = match;
if (!this.regex.global) {
break;
}
}
if (lastMatch) {
this._buffer = this._buffer.slice(lastMatch.index + lastMatch[0].length);
}
if (this.regex.multiline) {
this._buffer = this._buffer.slice(this._buffer.lastIndexOf('\n'));
}
cb();
}
StreamSnitch.prototype.clearBuffer = function() {
this._buffer = '';
return this;
}