I create a HTTP server that streams a video file.
http.createServer((req, res) => {
const file = 'example.mp4';
const size = fs.statSync(file).size;
res.writeHead(200, { 'Content-Length': size, 'Content-Type': 'video/mp4' });
fs.createReadStream(file).pipe(res);
}).listen(1911, '127.0.0.1');
I connect to it in my browser or video player to verify that it works. It does.
I encrypt a file:
fs.createReadStream('example.mp4')
.pipe(crypto.createCipher('aes-256-ctr', 'x'))
.pipe(fs.createWriteStream('encrypted_file'));
I decrypt it and play it back to verify that it works. It does.
Yet combining that decryption and streaming in the manner below fails silently.
const decrypt = crypto.createDecipher('aes-256-ctr', 'x');
http.createServer((req, res) => {
const file = 'encrypted_file';
const size = fs.statSync(file).size;
res.writeHead(200, { 'Content-Length': size, 'Content-Type': 'video/mp4' });
fs.createReadStream(file).pipe(decrypt).pipe(res);
}).listen(1911, '127.0.0.1');
The original and the encrypted file are the same size in bytes, and the original and the encrypted-then-decrypted file both have the same SHA-256 hash. Given that, I'd expect fs.createReadStream(original)
and fs.createReadStream(encrypted).pipe(decrypt)
to behave identically -- yet they don't. No video data is sent to the user, but no error is displayed to them either, and the error event never fires on the http.Server instance.
What am I missing?