UNPKG

875 BJavaScriptView Raw
1'use strict'
2const Base = require('./base.js')
3const ownOr = require('own-or')
4
5class Stdin extends Base {
6 constructor (options) {
7 options = options || {}
8 options.name = ownOr(options, 'name', '/dev/stdin')
9 super(options)
10
11 // This has to be here for node 0.10's wonky streams
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
37module.exports = Stdin