stream-utils.js 677 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. const {Transform} = require('stream');
  3. class ObjectTransform extends Transform {
  4. constructor() {
  5. super({
  6. objectMode: true
  7. });
  8. }
  9. }
  10. class FilterStream extends ObjectTransform {
  11. constructor(filter) {
  12. super();
  13. this._filter = filter;
  14. }
  15. _transform(data, encoding, callback) {
  16. if (this._filter(data)) {
  17. this.push(data);
  18. }
  19. callback();
  20. }
  21. }
  22. class UniqueStream extends ObjectTransform {
  23. constructor() {
  24. super();
  25. this._pushed = new Set();
  26. }
  27. _transform(data, encoding, callback) {
  28. if (!this._pushed.has(data)) {
  29. this.push(data);
  30. this._pushed.add(data);
  31. }
  32. callback();
  33. }
  34. }
  35. module.exports = {
  36. FilterStream,
  37. UniqueStream
  38. };