Skip to content

Commit 24c4034

Browse files
author
Hussein
committed
stream work
1 parent 910bf22 commit 24c4034

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

20-streams/203-pipe.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@
55
const fs = require('fs');
66
const crypto = require('crypto');
77
const gzip = require('node:zlib').createGzip();
8-
8+
console.time("start");
99
// Encryption setup
1010
const algorithm = 'aes-256-cbc'; // Encryption algorithm
1111
//hard code keys/ivs
1212
const key = Buffer.from("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", "hex") // 32 bytes key for AES-256
1313
const iv = Buffer.from("abcdef9876543210abcdef9876543210", "hex") // Initialization vector (16 bytes)
1414

15+
const hwm = 65000; //updating the value changes the speed
1516
//read stream from a file
16-
const inp = fs.createReadStream('/Users/HusseinNasser/Desktop/Video.mp4');
17+
const inp = fs.createReadStream('/home/hussein/large.file', {"highWaterMark": hwm})// /Users/HusseinNasser/Desktop/Video.mp4');
1718
//write stream to a file
18-
const out = fs.createWriteStream('/Users/HusseinNasser/Desktop/Video.mp4.enc.gz');
19+
const out = fs.createWriteStream('/home/hussein/large.file.enc.gz', {"highWaterMark": hwm} ) // '/Users/HusseinNasser/Desktop/Video.mp4.enc.gz');
1920
// Create a transform stream to handle encryption
2021
const encryptStream = crypto.createCipheriv(algorithm, key, iv);
2122

23+
out.on("close", ()=> console.timeEnd("start"));
24+
2225
//write stream to encryption
2326
//which then becomes readable
2427
//we write it the zip stream
2528
//pipe it finally out to a file
26-
inp.pipe(encryptStream).pipe(gzip).pipe(out)
27-
29+
inp.pipe(encryptStream).pipe(gzip).pipe(out);
2830

0 commit comments

Comments
 (0)