1 | 'use strict'
|
2 | const Base = require('./base.js')
|
3 | const ownOr = require('own-or')
|
4 |
|
5 | class Stdin extends Base {
|
6 | constructor (options) {
|
7 | options = options || {}
|
8 | options.name = ownOr(options, 'name', '/dev/stdin')
|
9 | super(options)
|
10 |
|
11 |
|
12 | this.stream = ownOr(options, 'tapStream', process.stdin)
|
13 | this.stream.pause()
|
14 | }
|
15 |
|
16 | main (cb) {
|
17 | this.stream.on('error', er => {
|
18 | er.tapCaught = 'stdinError'
|
19 | this.threw(er)
|
20 | })
|
21 | this.setTimeout(this.options.timeout)
|
22 | this.stream.pipe(this.parser)
|
23 | if (this.parent)
|
24 | this.parent.emit('stdin', this)
|
25 | this.stream.resume()
|
26 | this.once('end', cb)
|
27 | }
|
28 |
|
29 | threw (er, extra, proxy) {
|
30 | extra = super.threw(er, extra, proxy)
|
31 | this.options = extra
|
32 | this.parser.abort(er.message, extra)
|
33 | this.parser.end()
|
34 | }
|
35 | }
|
36 |
|
37 | module.exports = Stdin
|