-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhow-streams-transfer-data.js
More file actions
37 lines (32 loc) · 1.12 KB
/
Copy pathhow-streams-transfer-data.js
File metadata and controls
37 lines (32 loc) · 1.12 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
const { createWriteStream, createReadStream } = require('fs');
const { Readable } = require('stream');
const { join } = require('path');
// push alphabet characters to readable stream
const rs = new Readable({
read: (function () {
let alphabetChar = 97;
return function () {
// terminate after last character
if (alphabetChar > 'z'.charCodeAt(0)) rs.push(null);
else rs.push(String.fromCharCode(alphabetChar));
alphabetChar += 1;
};
})(),
});
function getPath(fileName) {
if (typeof fileName !== 'string') {
throw new Error(fileName + 'is not a string');
}
return join(__dirname, 'out', fileName);
}
rs.pipe(createWriteStream(getPath('output.txt')));
// Create CPP file and build executable.
// Transfer executable using streams.
// Execute the transferred executable.
//
// If the copied executable is successfully executed, then
// the stream has successfully transferred the executable.
const source = createReadStream('./main.exe');
source.pipe(createWriteStream(getPath('main-copy.exe')));
// make executable content readable
source.pipe(createWriteStream(getPath('main-copy.txt')));