UNPKG

611 BJavaScriptView Raw
1'use strict'
2
3var stream = require('stream')
4var writeToStream = require('mqtt-packet').writeToStream
5
6function StreamGenerator (output, opts) {
7 if (!(this instanceof StreamGenerator)) return new StreamGenerator(output, opts)
8 var that = this
9 this.opts = opts || {}
10 var input = new stream.Writable({ objectMode: true, write: write })
11
12 function write (chunk, enc, cb) {
13 if (writeToStream(chunk, output, that.opts)) {
14 cb()
15 } else {
16 output.once('drain', cb)
17 }
18 }
19
20 input.setOptions = function (opts) {
21 that.opts = opts
22 }
23
24 return input
25}
26
27module.exports = StreamGenerator