Repository navigation
Expand file tree
/
Copy pathstreams.js
More file actions
205 lines (184 loc) · 4.78 KB
/
Copy pathstreams.js
File metadata and controls
205 lines (184 loc) · 4.78 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const stream = require('stream')
module.exports = {
readLines,
readDelimited,
readJsonObjects,
simpleLengthPrefixReader,
simpleLengthPrefixWriter,
readLengthPrefixedBuffers,
writeLengthPrefixedBuffers,
writeDelimited,
sendMessagesToParent,
readMessagesFromParent,
readMessagesFromForked,
}
function readLines() {
let buffer = ''
return new stream.Transform({
transform(chunk, encoding, done) {
if (chunk !== null) {
buffer += chunk
const lines = buffer.split(/[\r\n]/)
buffer = lines.pop() // will be empty string if end char was newline
lines.filter(l => l!=='').forEach(l => this.push(l))
}
done()
},
flush(done) {
const lines = buffer.split(/[\r\n]/)
lines.filter(l => l!=='').forEach(l => this.push(l))
done()
}
})
}
function readDelimited(delimiterBuffer) {
let buffer = ''
return new stream.Transform({
transform(chunk, encoding, done) {
if (chunk !== null) {
buffer += chunk
const lines = buffer.split(delimiterBuffer)
buffer = lines.pop() // will be empty string if end char was delimiter
lines
.map(l => l.trim())
.filter(l => l!=='')
.forEach(l => this.push(l))
}
done()
},
flush(done) {
const lines = buffer.split(delimiterBuffer)
lines
.map(l => l.trim())
.filter(l => l!=='')
.forEach(l => this.push(l))
done()
}
})
}
function readJsonObjects() {
return new stream.Transform({
readableObjectMode: true,
transform(chunk, encoding, done) {
this.push(JSON.parse(chunk))
done()
}
})
}
const prefixSizeByFormat = {
'Int32LE': 4,
'Int32BE': 4,
'Int16LE': 2,
'Int16BE': 2,
'Int8': 1,
'UInt32LE': 4,
'UInt32BE': 4,
'UInt16LE': 2,
'UInt16BE': 2,
'UInt8': 1,
}
function simpleLengthPrefixReader(prefixFormat) {
const prefixSize = prefixSizeByFormat[prefixFormat]
const prefixReadFn = `read${prefixFormat}`
return {
tryReadPrefix(buffer, lengthCallback) {
if(buffer.length > prefixSize) {
lengthCallback(buffer[prefixReadFn](), prefixSize)
return true
} else {
return false
}
},
}
}
function simpleLengthPrefixWriter(prefixFormat) {
const prefixSize = prefixSizeByFormat[prefixFormat]
const prefixWriteFn = `write${prefixFormat}`
return {
prefixSize(length) {
return prefixSize
},
writePrefix(buffer, length) {
buffer[prefixWriteFn](length)
},
}
}
function readLengthPrefixedBuffers({ tryReadPrefix }) {
let chunkBuffer = Buffer.from([])
return new stream.Transform({
transform(chunk, encoding, done) {
chunkBuffer = Buffer.concat([chunkBuffer, chunk])
let chunkBufferLength = 0
let prefixSize = 0
while(tryReadPrefix(chunkBuffer, (length, bytesRead) => {
chunkBufferLength = length
prefixSize = bytesRead
})) {
const totalBytesToRead = prefixSize + chunkBufferLength
if(chunkBuffer.length >= totalBytesToRead) {
this.push(chunkBuffer.slice(prefixSize,totalBytesToRead))
chunkBuffer = chunkBuffer.slice(totalBytesToRead)
} else {
break
}
}
done()
}
})
}
function writeLengthPrefixedBuffers({ prefixSize, writePrefix }) {
return new stream.Transform({
transform(chunk, encoding, done) {
const chunkAsBuffer = ('string' === typeof chunk) ?
Buffer.from(chunk) :
chunk
const prefixBytes = prefixSize(chunkAsBuffer.length)
const outputBuffer = Buffer.allocUnsafe(prefixBytes + chunkAsBuffer.length)
writePrefix(outputBuffer, chunkAsBuffer.length)
chunkAsBuffer.copy(outputBuffer, prefixBytes)
this.push(outputBuffer)
done()
}
})
}
function writeDelimited(delimiterBuffer) {
return new stream.Transform({
transform(chunk, encoding, done) {
try {
this.push(chunk)
this.push(delimiterBuffer)
done()
} catch(ex) {
console.error('WTFFFFF', ex)
}
}
})
}
function sendMessagesToParent() {
const childMessagesStream = new stream.Transform({
writableObjectMode: true,
write(jsonObject, encoding, done) {
process.send(jsonObject)
done()
}
})
return childMessagesStream
}
function readMessagesFromParent() {
const parentMessagesStream = new stream.Transform({
readableObjectMode: true
})
process.on('message', (message) => {
parentMessagesStream.push(message)
})
return parentMessagesStream
}
function readMessagesFromForked(forkedProcess) {
const forkedMessagesStream = new stream.Transform({
readableObjectMode: true
})
forkedProcess.on('message', (message) => {
forkedMessagesStream.push(message)
})
return forkedMessagesStream
}