-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
74 lines (66 loc) · 1.89 KB
/
index.js
File metadata and controls
74 lines (66 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* @module streamss-fork
* @copyright 2019 commenthol
* @licence MIT
*/
const { Transform } = require('stream')
const writeToStream = (chunk, enc) => (stream) =>
new Promise(resolve => {
if (stream.writable) {
stream.write(chunk, enc, (err) => {
resolve(err)
})
} else {
resolve(new Error('stream not writable'))
}
})
/**
* @class Fork
*/
class Fork extends Transform {
/**
* @constructor
* @param {Writable[]} streams - Array of Writable streams
* @param {Object} [options] - see [https://nodejs.org/...](https://nodejs.org/dist/latest/docs/api/stream.html#stream_constructor_new_stream_writable_options)
*/
constructor (streams, options = {}) {
super(options)
this.streams = streams
this.results = new Array(streams.length)
const emit = (event, arg) => {
this.streams.forEach(stream => stream.emit(event, arg))
}
const finish = () => {
this.streams.forEach(stream => stream.end())
}
this.on('error', err => emit('error', err))
this.once('close', () => emit('close'))
this.once('end', () => emit('end'))
this.once('finish', () => finish())
this.on('pipe', (src) => {
if (options.passError !== false) {
src.on('error', (err) => {
this.emit('error', err)
})
}
})
}
_transform (chunk, enc, done) {
Promise.all(
this.streams.map(writeToStream(chunk, enc))
).then(results => {
this.results = results.map((result, i) => result || this.results[i])
this.push(chunk, enc)
done()
})
}
/**
* @param {Stream[]} streams - Array of streams
* @param {Object} [options] - see [https://nodejs.org/...](https://nodejs.org/dist/latest/docs/api/stream.html#stream_constructor_new_stream_writable_options)
* @return {Fork}
*/
static fork (streams, options) {
return new Fork(streams, options)
}
}
module.exports = Fork