UNPKG

@fanoutio/eventstream

Version:

Connect-compatible middleware that enables the easy creation of EventStream endpoints

32 lines (31 loc) 989 B
import { Writable } from 'stream'; import Debug from 'debug'; const debug = Debug('eventstream'); export default class ChannelWritable extends Writable { channelPublisher; constructor(channelPublisher) { super({ objectMode: true, }); this.channelPublisher = channelPublisher; } async _write(event, _encoding, callback) { let err; try { debug('ChannelWritable.write'); await this.channelPublisher.publishEvent(event); } catch (ex) { err = ex instanceof Error ? ex : new Error(String(ex)); } // The above is an async method that doesn't return until the publish has finished // (if GRIP is enabled then it won't return until GRIP publish has finished) // For this reason this should apply backpressure to the writing. if (err != null) { callback(err); } else { callback(); } } }