-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathindex.js
56 lines (45 loc) · 1.23 KB
/
index.js
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
var duplexify = require('duplexify')
var ndjson = require('ndjson')
var run = require('docker-run')
var xtend = require('xtend')
module.exports = function(image, opts) {
var input = ndjson.parse()
var output = ndjson.stringify()
var result = duplexify()
input.once('data', function(handshake) {
if (handshake.type !== 'run') return result.destroy(new Error('Invalid handshake'))
var child = run(image, xtend(opts, {
tty: true,
width: handshake.width,
height: handshake.height
}))
input.on('data', function(data) {
if (data.type === 'resize') child.resize(data.width, data.height)
if (data.type === 'stdin') child.stdin.write(data.data)
})
child.stdout.on('data', function(data) {
output.write({
type: 'stdout',
data: data.toString()
})
})
child.stderr.on('data', function(data) {
output.write({
type: 'stderr',
data: data.toString()
})
})
child.on('exit', function() {
result.destroy()
})
child.on('error', function(err) {
result.destroy(err)
})
result.on('close', function() {
child.kill()
})
})
result.setReadable(output)
result.setWritable(input)
return result
}