-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.js
35 lines (28 loc) · 873 Bytes
/
play.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
const fs = require('fs');
const Throttle = require('throttle'); //TODO implement own throttle
const DEFAULT_OPT = {limit: 1200, clearBefore: true};
/**
*
* @param {String} filePath -
* @param {Object} [options] -
* @returns {ReadableStream}
*/
function stream(filePath = '', options) {
options = {...DEFAULT_OPT, ...options};
let inputStream;
if (!process.stdin.isTTY) { //process was piped
inputStream = process.stdin;
}
else if (filePath && typeof filePath === 'string') {
inputStream = fs.createReadStream(filePath);
}
else {
throw new Error('No input was given!');
}
if (options.clearBefore) {
process.stdout.write('\x1B[2J'); //clearing console before starting
}
return inputStream
.pipe(new Throttle(options.limit)); //limiting bytes pro second
}
module.exports = stream;