-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
188 lines (168 loc) · 5.14 KB
/
Copy pathserver.js
File metadata and controls
188 lines (168 loc) · 5.14 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
const fs = require('fs');
const path = require('path');
const port = process.env.PORT || 8080;
let filePath = process.argv[2]
// cosmetic vars
const YELLOW = '\x1b[33m%s\x1b[0m'
const GREEN = '\x1b[32m%s\x1b[0m'
const BLUE = '\x1b[34m%s\x1b[0m'
const RED = '\x1b[31m%s\x1b[0m'
// Mocha constructor
const Mocha = require('mocha')
const mocha = new Mocha({
reporter: 'spec',
useColors: true,
fullTrace: true,
});
let data
let chunksKB = []
// argv option bools
const options = process.argv.slice(3)
let log = false
let test = false
let verbose = false
// start timer for program start
console.time('Program operation finished in');
// argv options logic
if (process.argv.length < 3) {
console.log('File path parameter is required.');
console.log('Try running "node server sample/small.txt [options]" \n');
process.exit(1);
} else if (process.argv.length > 3) {
options.forEach((flag) => {
switch (flag) {
case '-l':
log = true
break;
case '-t':
test = true
break;
case '-v':
verbose = true
break;
default:
console.log(RED,'\nERROR: Ommitted unrecognized option from user argument.');
}
})
}
// summary logfile class
class Summary {
constructor (date, file, totalTime, size, transfer, chunks, lines) {
this.date = date;
this.file = file;
this.totalTime = totalTime;
this.size = size;
this.transfer = transfer;
this.chunks = chunks;
this.lines = lines;
}
logMsg() {
return (`
=======================================
============= LOG OUTPUT ==============
${this.date}
FILENAME: ${this.file}
=======================================
Data stream completed in: ${this.totalTime} ms
${this.size} bytes received
Transfer rate of ${this.transfer} KB/ms
Number of chunks received: ${this.chunks}
Total number of lines: ${this.lines}
`)
}
print() {
console.log(GREEN,this.logMsg());
if (!log) console.log('To save this log, add the -l flag\n');
}
}
const toKB = (bytes) => {
return parseFloat(bytes / 1000)
}
// if no filename is passed
if (!fs.existsSync(filePath)) {
console.log(RED,`\n FILEPATH ERROR`);
console.log('==================');
console.log(RED,`"${filePath}" is not a valid file`);
console.log(RED,'Check the file path and extension \n');
console.log('eg: "sample/small.txt"');
console.log('eg: "../../somewhere/else.txt"\n');
console.log('Program will now exit... \n\n');
process.exit(1);
}
const time = process.hrtime();
const outputdir = '/output'
const logsdir = '/logs'
!fs.existsSync(__dirname + outputdir) && fs.mkdirSync(__dirname + outputdir)
!fs.existsSync(__dirname + logsdir) && fs.mkdirSync(__dirname + logsdir)
const stream = fs.createReadStream(filePath)
const filename = path.parse(filePath).base;
const output = fs.createWriteStream(__dirname + `${outputdir}/${filename}`)
stream.pipe(output)
console.log('\n=============================');
console.log(GREEN,'Sending data from ' + filename);
console.log('=============================');
stream
.on('data', (chunk, err) => {
if (err) {
console.log(err)
} else {
console.time('Received in');
data += chunk
console.log(YELLOW,'\n>>>> incoming chunk');
console.log(toKB(chunk.length) + ' KB chunk')
chunksKB.push(toKB(chunk.length))
console.timeEnd('Received in')
}
})
.on('error', err => console.error(err.stack))
.on('end', (chunk) => {
console.log('\n=============================');
console.log(GREEN,' Operation completed');
console.log('=============================\n');
chunksKBTotal = chunksKB.reduce((a, b) => a + b )
endTime = process.hrtime(time)
endTime = parseFloat((endTime[0] * 1000) + endTime[1]/1000000).toFixed(3);
rate = (endTime / chunksKBTotal)
rate = parseFloat(rate).toFixed(3)
summaryLog = new Summary(
new Date(),
filename,
endTime,
data.length,
rate,
chunksKB.length,
data.toString().split("\n").length
);
console.timeEnd('Program operation finished in');
summaryLog.print()
// argument actions
if (options != 0) {
if (verbose) {
console.log('Argument flags: {')
if (log) { console.log(' log: ' + YELLOW, log)}
if (test) { console.log(' test: ' + YELLOW, test)}
if (verbose) { console.log(' verbose: ' + YELLOW, verbose)}
console.log('}');
console.log('\nOutput file can be found at: ');
console.log(BLUE,__dirname + `/output/${filename}`);
if (log) {
console.log('\nLog file can be found at ')
console.log(BLUE, __dirname + '/logs/logfile.txt')
} else {
console.log('\nNo log argument present, logfile was not created\n');
}
}
if (log) {
fs.appendFileSync(__dirname + '/logs/logfile.txt', summaryLog)
}
if (test) {
console.log('\nTest argument invoked, running default test file');
console.log('Results from tests: ');
mocha.addFile(__dirname + '/test/test-server.js')
mocha.run();
}
}
})
module.exports = {
toKB, filePath, output, filename
}