-
Notifications
You must be signed in to change notification settings - Fork 1
/
fifo.js
176 lines (146 loc) · 3.96 KB
/
fifo.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const cp = require('child_process'),
fs = require('fs'),
FIFOError = require('./fifoError')
class FIFO {
constructor(path, encoding) {
this.path = path || this._generateFifoPath()
this.encoding = encoding || "utf8"
this._children = []
this._initFifo()
this._open = true
}
get open() {
return this._open && this._pathExists()
}
read(callback) {
this._throwIfClosed()
this._throwIfReader()
let cmd = this._generateReadCommand()
let child = cp.exec(cmd, {
encoding: this.encoding
}, (err, stdout, stderr) => {
if (this.open && callback) {
callback(stdout)
}
})
this._children.push(child)
}
readSync() {
this._throwIfClosed()
this._throwIfReader()
let cmd = this._generateReadCommand()
return cp.execSync(cmd, {
encoding: this.encoding
})
}
setReader(callback) {
this._throwIfClosed()
this._throwIfReader()
this.reader = true
this._reader(callback)
}
removeReader() {
this.reader = false
this._readerChild.kill()
delete this._readerChild
}
write(string, trim, callback) {
if (typeof trim === 'function') {
callback = trim
trim = false
}
this._throwIfClosed()
let cmd = this._generateWriteCommand(string, trim)
let child = cp.exec(cmd, {
encoding: this.encoding
}, () => {
if (this.open && callback) {
callback()
}
})
this._children.push(child)
}
writeSync(string, trim) {
this._throwIfClosed()
let cmd = this._generateWriteCommand(string, trim)
cp.execSync(cmd)
}
close() {
if (this._open) {
this._open = false
this._killAllChildren()
if (!this.preserve) {
this._unlink()
}
}
}
_initFifo() {
let stat = this._pathExists()
if (!stat) {
this._createFifo()
} else if (!stat.isFIFO()) {
throw new FIFOError(this.path,
"Non fifo file with this name already exists.")
} else {
this.preserve = true
}
}
_createFifo() {
cp.execSync('mkfifo ' + this.path)
}
_generateFifoPath() {
let path
do {
let random = Math.floor(Math.random() * 8999 + 1000)
path = '/tmp/fifo-js-' + random + '.fifo'
} while (this._pathExists(path))
return path
}
_generateWriteCommand(string, trim) {
return 'echo' + (trim ? ' -n' : '') + ' "' + string + '" > ' + this.path
}
_generateReadCommand() {
return 'cat ' + this.path
}
_reader(callback) {
let cmd = this._generateReadCommand()
let child = cp.exec(cmd, {
encoding: this.encoding
}, (err, stdout, stderr) => {
if (this.reader && this.open && callback) {
this._reader(callback)
callback(stdout)
}
})
this._readerChild = child
}
_killAllChildren() {
this._readerChild && this._readerChild.kill()
this._children.forEach(child => child.kill())
}
_pathExists(path) {
path = path || this.path
try {
return fs.statSync(path)
} catch(e) {
return false
}
}
_unlink() {
if (this._pathExists()) {
fs.unlinkSync(this.path)
}
}
_throwIfClosed() {
if (!this.open) {
throw new FIFOError(this.path, "Fifo isn't open.")
}
}
_throwIfReader() {
if (this.reader) {
throw new FIFOError(this.path,
'Cannot read or set another reader if a reader is set.')
}
}
}
module.exports = FIFO