You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pipeline.js 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict'
  2. const MinipassPipeline = require('minipass-pipeline')
  3. class CachingMinipassPipeline extends MinipassPipeline {
  4. #events = []
  5. #data = new Map()
  6. constructor (opts, ...streams) {
  7. // CRITICAL: do NOT pass the streams to the call to super(), this will start
  8. // the flow of data and potentially cause the events we need to catch to emit
  9. // before we've finished our own setup. instead we call super() with no args,
  10. // finish our setup, and then push the streams into ourselves to start the
  11. // data flow
  12. super()
  13. this.#events = opts.events
  14. /* istanbul ignore next - coverage disabled because this is pointless to test here */
  15. if (streams.length) {
  16. this.push(...streams)
  17. }
  18. }
  19. on (event, handler) {
  20. if (this.#events.includes(event) && this.#data.has(event)) {
  21. return handler(...this.#data.get(event))
  22. }
  23. return super.on(event, handler)
  24. }
  25. emit (event, ...data) {
  26. if (this.#events.includes(event)) {
  27. this.#data.set(event, data)
  28. }
  29. return super.emit(event, ...data)
  30. }
  31. }
  32. module.exports = CachingMinipassPipeline