-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadWriteStream.js
111 lines (89 loc) · 2.63 KB
/
readWriteStream.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
#!/usr/bin/env node
"use strict";
//Imports
import * as path from 'path';
import * as fs from 'fs';
import minimist from 'minimist';
import * as stepIn from 'stream';
import zlib from 'zlib';
// Mensagem de suporte a variáveis
// Linux: basta usar o comando export para preparar as variáveis de ambiente
var BASE_PATH = path.resolve(process.env.BASE_PATH || __dirname);
var OUTFILE = path.join(BASE_PATH, "out.txt");
var args = minimist(process.argv.slice(2), {
boolean: ["help", "in", "out", "uncompress"],
string: [ "file" ]
});
function printHelp(){
console.log('This script usage:');
console.log("");
console.log("command --help");
console.log('command --file={FILENAME}');
console.log("");
console.log("--help print this help");
console.log('--file process the file');
console.log("");
console.log("--in, - process stdin");
console.log("");
console.log("--out process to a local file 'out.txt'");
console.log("");
console.log("--compress gzip the output");
console.log("");
console.log("--uncompress un-gzip the input");
console.log("");
}
// Main: controle
if(args.help){
console.log("");
printHelp();
} else if(args.file) {
let stream = fs.createReadStream(path.join(BASE_PATH, args.file));
processFile(stream).then(() => console.log('\nComplete!')).catch(error);
} else if (args.in || args._.includes("-")){
processFile(process.stdin).then(() => console.log('\n complete!')).catch(error);
}else {
error('Incorrect usage!',true);
}
// Functions and helpers
function streamComplete(stream){
return new Promise(function (res){
stream.on("end", res);
});
}
function error(msg, includeHelp = false){
console.error(msg);
if(includeHelp){
console.log("");
printHelp();
}
}
async function processFile(inStream){
var outStream = inStream;
if(args.uncompress){
let gunzipStream = zlib.createGunzip();
outStream = outStream.pipe(gunzipStream);
}
// Acessa a stream e captura o buffer
var upperStream = new stepIn.Transform({
transform(chunk,enc,cb){
this.push(chunk.toString().toUpperCase());
//Se ativado adiciona um delay para 65Kb processados do buffer
// setTimeout(cb,500);
cb();
}
});
outStream = outStream.pipe(upperStream);
let targetStream;
if(args.compress){
console.log('here')
let gzipStream = zlib.createGzip();
outStream = outStream.pipe(gzipStream);
OUTFILE = `${OUTFILE}.gz`;
}
if(args.out)
targetStream = fs.createWriteStream(OUTFILE);
else
targetStream = process.stdout;
outStream.pipe(targetStream);
await streamComplete(outStream);
}