schangxiang@126.com
2025-09-19 0821aa23eabe557c0d9ef5dbe6989c68be35d1fe
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
69
70
71
72
73
74
match-stream [![Build Status](https://travis-ci.org/EvanOxfeld/match-stream.png)](https://travis-ci.org/EvanOxfeld/match-stream)
============
 
Supply a function to handle pattern matches within a NodeJS stream.
 
## Installation
 
```bash
$ npm install match-stream
```
 
## Quick Examples
 
### End stream on match
 
```javascript
var MatchStream = require('match-stream');
var streamBuffers = require("stream-buffers");
 
var ms = new MatchStream({ pattern: 'World'}, function (buf, matched, extra) {
  if (!matched) {
    return this.push(buf);
  }
  this.push(buf);
  return this.push(null); //signal end of data
});
 
var sourceStream = new streamBuffers.ReadableStreamBuffer();
sourceStream.put("Hello World");
var writableStream = new streamBuffers.WritableStreamBuffer();
 
sourceStream
  .pipe(ms)
  .pipe(writableStream)
  .once('close', function () {
    var str = writableStream.getContentsAsString('utf8');
    console.log('Piped data before pattern occurs:', "'" + str + "'");
    sourceStream.destroy();
  });
 
//Output
//Piped data before pattern occurs: 'Hello '
```
 
### Split stream
 
```javascript
var MatchStream = require('match-stream');
var fs = require('fs');
 
var line = "";
var loremLines = [];
var ms = new MatchStream({ pattern: '.', consume: true}, function (buf, matched, extra) {
  line += buf.toString();
  if (matched) {
    loremLines.push(line.trim());
    line = "";
  }
});
 
fs.createReadStream('lorem.txt')
  .pipe(ms)
  .once('finish', function() {
    console.log(loremLines);
  });
```
 
## License
 
MIT
 
## Acknowledgements
 
Special thanks to @wanderview for assisting with the API.