1 | 'use strict'
|
2 |
|
3 | var stream = require('stream')
|
4 | var writeToStream = require('mqtt-packet').writeToStream
|
5 |
|
6 | function 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 |
|
27 | module.exports = StreamGenerator
|