🎨 Improve streamToString performance

This commit is contained in:
C-3PO 2018-07-05 13:13:05 +02:00
parent eab9a27fa6
commit 66d14a1d5b
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
3 changed files with 12 additions and 6 deletions

View file

@ -27,11 +27,15 @@ export default function getDecryptor([key0, key1, key2]: [number, number, number
const dvOut = new DataView(decryptedChunk.buffer); const dvOut = new DataView(decryptedChunk.buffer);
for (let i = 0; i < encryptedChunk.length; i += 1) { for (let i = 0; i < encryptedChunk.length; i += 1) {
//read and decrypt byte //read byte
let curChar = encryptedChunk.readUInt8(i); let curChar = encryptedChunk.readUInt8(i);
//decrypt byte
const keyPart = (key2 | 2) & 0xFFFF; const keyPart = (key2 | 2) & 0xFFFF;
const decryptedByte = (keyPart * (keyPart ^ 1)) >>> 8; const decryptedByte = (keyPart * (keyPart ^ 1)) >>> 8;
curChar ^= decryptedByte & 0xFF; curChar ^= decryptedByte & 0xFF;
//write byte
if (position + i < 12) { if (position + i < 12) {
//do nothing //do nothing
} else if (position + i >= 12 && position < 12) { } else if (position + i >= 12 && position < 12) {

View file

@ -25,7 +25,9 @@ export default function extractFileStream(file: ISsnFileEntry, inputStream: stre
const localExtraSize = localFileHeader.readUInt16LE(28); const localExtraSize = localFileHeader.readUInt16LE(28);
//skip local file name and extra field //skip local file name and extra field
inputStream.read(localFilenameSize + localExtraSize); if (localFilenameSize + localExtraSize > 0) {
inputStream.read(localFilenameSize + localExtraSize);
}
//------------------------------------------------- //-------------------------------------------------

View file

@ -5,17 +5,17 @@ const decoder = new TextDecoder('utf-8');
export default function streamToString(inputStream: stream.Readable): Promise<string> { export default function streamToString(inputStream: stream.Readable): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let outputString = ''; const stringParts: string[] = [];
//Convert chunks to string //Convert chunks to string
inputStream.on('data', (chunk: Buffer) => { inputStream.on('data', (chunk: Buffer) => {
outputString += decoder.decode(chunk, { stream: true }); stringParts.push(decoder.decode(chunk, { stream: true }));
}); });
//Output final string //Output final string
inputStream.on('end', () => { inputStream.on('end', () => {
outputString += decoder.decode(); stringParts.push(decoder.decode());
resolve(outputString); resolve(stringParts.join(''));
}); });
//Exit on error //Exit on error