On every request, I am trying to decrypt an encrypted image, and provide a response based on that. I started my server, on first request, its decrypting the file, and sending a proper response, while on next request, it trying to decrypt, but not storing any data into new file, and at the end throwing an error "ERR_STREAM_WRITE_AFTER_END", and my server crushed down.
My decrypt function is
let decrypt = Crypto.createDecipher('aes-256-ctr', 'random_string');
async function decryptFile(encryptedFileDetails) {
try {
// write you code here..
let decryptPromise = new Promise(async (decryptResolve) => {
let r = FS.createReadStream(source);
let w = FS.createWriteStream(destination);
// start pipe
r.pipe(decrypt).pipe(w);
w.on('finish', function () {
console.log('Decrypted file written to disk!');
decryptResolve();
});
});
await decryptPromise;
} catch (error) {
return Promise.reject(error);
}
}
The error message I am receiving is
NodeError [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_stream_writable.js:248:12)
at Decipher.Writable.write (_stream_writable.js:296:5)
at ReadStream.ondata (_stream_readable.js:708:20)
at ReadStream.emit (events.js:198:13)
at addChunk (_stream_readable.js:287:12)
at readableAddChunk (_stream_readable.js:268:11)
at ReadStream.Readable.push (_stream_readable.js:223:10)
at lazyFs.read (internal/fs/streams.js:181:12)
at FSReqWrap.wrapper [as oncomplete] (fs.js:467:17) Emitted 'error' event at:
at errorOrDestroy (internal/streams/destroy.js:107:12)
at Decipher.onerror (_stream_readable.js:732:7)
at Decipher.emit (events.js:198:13)
at errorOrDestroy (internal/streams/destroy.js:107:12)
at writeAfterEnd (_stream_writable.js:250:3)
at Decipher.Writable.write (_stream_writable.js:296:5)
[... lines matching original stack trace ...]
at FSReqWrap.wrapper [as oncomplete] (fs.js:467:17)
</pre>