until.js 735 B

1234567891011121314151617181920212223242526
  1. var MatchStream = require('../');
  2. var streamBuffers = require("stream-buffers");
  3. var ms = new MatchStream({ pattern: 'World'}, function (buf, matched, extra) {
  4. if (!matched) {
  5. return this.push(buf);
  6. }
  7. this.push(buf);
  8. return this.push(null); //signal end of data
  9. });
  10. var sourceStream = new streamBuffers.ReadableStreamBuffer();
  11. sourceStream.put("Hello World");
  12. var writableStream = new streamBuffers.WritableStreamBuffer();
  13. sourceStream
  14. .pipe(ms)
  15. .pipe(writableStream)
  16. .once('close', function () {
  17. var str = writableStream.getContentsAsString('utf8');
  18. console.log('Piped data before pattern occurs:', "'" + str + "'");
  19. sourceStream.destroy();
  20. });
  21. //Output
  22. //Piped data before pattern occurs: 'Hello '