♻ Convert decrypt stream into transform
This commit is contained in:
parent
9688c12797
commit
933134eb35
2 changed files with 17 additions and 22 deletions
|
@ -37,8 +37,8 @@ export default function extractFileStream(file: ISsnFileEntry, inputStream: stre
|
|||
|
||||
//pipe into decryption if file is encrypted
|
||||
if (file.decryptionKeys !== undefined) {
|
||||
const decryptTransform = decryptStream(curStream, [...file.decryptionKeys] as [number, number, number]);
|
||||
curStream = decryptTransform;
|
||||
const decryptTransform = decryptStream([...file.decryptionKeys] as [number, number, number]);
|
||||
curStream = curStream.pipe(decryptTransform);
|
||||
}
|
||||
|
||||
//pipe into decompression
|
||||
|
@ -46,8 +46,7 @@ export default function extractFileStream(file: ISsnFileEntry, inputStream: stre
|
|||
decompressTransform.on('error', (error) => {
|
||||
throw new Error(`Error during decompression: ${error.message}`);
|
||||
});
|
||||
curStream.pipe(decompressTransform);
|
||||
curStream = decompressTransform;
|
||||
curStream = curStream.pipe(decompressTransform);
|
||||
|
||||
//set max length
|
||||
const maxLength2 = streamSetMaxLength(curStream, file.size);
|
||||
|
|
|
@ -1,26 +1,22 @@
|
|||
import * as stream from 'stream';
|
||||
import getDecryptor from '../decryption/decryptChunk';
|
||||
|
||||
export default function decryptStream(inputStream: stream.Readable, decryptionKeys: [number, number, number]): stream.Readable {
|
||||
export default function decryptTransform(decryptionKeys: [number, number, number]): stream.Transform {
|
||||
const decryptor = getDecryptor(decryptionKeys);
|
||||
const transform = new stream.Transform();
|
||||
|
||||
transform.on('readable', () => {
|
||||
const encryptedChunk = transform.read();
|
||||
|
||||
const outStream = new stream.Readable({
|
||||
read(size) {
|
||||
//Decrypt chunk
|
||||
const encryptedChunk: Buffer = inputStream.read(size);
|
||||
if (encryptedChunk === null) {
|
||||
//If end has been reached, stop
|
||||
this.push(null);
|
||||
} else {
|
||||
const decryptedChunk = decryptor(encryptedChunk);
|
||||
this.push(decryptedChunk);
|
||||
transform.end();
|
||||
return;
|
||||
}
|
||||
},
|
||||
|
||||
const decryptedChunk = decryptor(encryptedChunk);
|
||||
|
||||
transform.write(decryptedChunk);
|
||||
});
|
||||
|
||||
inputStream.on('end', () => {
|
||||
outStream.emit('end');
|
||||
});
|
||||
|
||||
return outStream;
|
||||
return transform;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue