♻ 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
|
//pipe into decryption if file is encrypted
|
||||||
if (file.decryptionKeys !== undefined) {
|
if (file.decryptionKeys !== undefined) {
|
||||||
const decryptTransform = decryptStream(curStream, [...file.decryptionKeys] as [number, number, number]);
|
const decryptTransform = decryptStream([...file.decryptionKeys] as [number, number, number]);
|
||||||
curStream = decryptTransform;
|
curStream = curStream.pipe(decryptTransform);
|
||||||
}
|
}
|
||||||
|
|
||||||
//pipe into decompression
|
//pipe into decompression
|
||||||
|
@ -46,8 +46,7 @@ export default function extractFileStream(file: ISsnFileEntry, inputStream: stre
|
||||||
decompressTransform.on('error', (error) => {
|
decompressTransform.on('error', (error) => {
|
||||||
throw new Error(`Error during decompression: ${error.message}`);
|
throw new Error(`Error during decompression: ${error.message}`);
|
||||||
});
|
});
|
||||||
curStream.pipe(decompressTransform);
|
curStream = curStream.pipe(decompressTransform);
|
||||||
curStream = decompressTransform;
|
|
||||||
|
|
||||||
//set max length
|
//set max length
|
||||||
const maxLength2 = streamSetMaxLength(curStream, file.size);
|
const maxLength2 = streamSetMaxLength(curStream, file.size);
|
||||||
|
|
|
@ -1,26 +1,22 @@
|
||||||
import * as stream from 'stream';
|
import * as stream from 'stream';
|
||||||
import getDecryptor from '../decryption/decryptChunk';
|
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 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 (encryptedChunk === null) {
|
||||||
//If end has been reached, stop
|
transform.end();
|
||||||
this.push(null);
|
return;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
const decryptedChunk = decryptor(encryptedChunk);
|
const decryptedChunk = decryptor(encryptedChunk);
|
||||||
this.push(decryptedChunk);
|
|
||||||
}
|
transform.write(decryptedChunk);
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
inputStream.on('end', () => {
|
return transform;
|
||||||
outStream.emit('end');
|
|
||||||
});
|
|
||||||
|
|
||||||
return outStream;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue