thedesk/app/node_modules/match-stream/examples/until.js
2018-02-18 16:29:06 +09:00

26 lines
735 B
JavaScript

var MatchStream = require('../');
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 '