Fix memory usage when non-streaming-en/decrypting large files
Broken in #1071.
This commit is contained in:
parent
e39216424f
commit
c4a7455cb5
60
src/util.js
60
src/util.js
|
@ -698,26 +698,41 @@ export default {
|
||||||
let carryOverCR = false;
|
let carryOverCR = false;
|
||||||
|
|
||||||
return stream.transform(data, bytes => {
|
return stream.transform(data, bytes => {
|
||||||
bytes = carryOverCR ? [CR].concat(Array.from(bytes)) : Array.from(bytes);
|
if (carryOverCR) {
|
||||||
|
bytes = util.concatUint8Array([new Uint8Array([CR]), bytes]);
|
||||||
|
}
|
||||||
|
|
||||||
if (bytes[bytes.length - 1] === CR) {
|
if (bytes[bytes.length - 1] === CR) {
|
||||||
carryOverCR = true;
|
carryOverCR = true;
|
||||||
bytes.pop();
|
bytes = bytes.subarray(0, -1);
|
||||||
} else {
|
} else {
|
||||||
carryOverCR = false;
|
carryOverCR = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const normalized = [];
|
let index;
|
||||||
for (let i = 0; i < bytes.length; i++){
|
const indices = [];
|
||||||
const x = bytes[i];
|
for (let i = 0; ; i = index) {
|
||||||
if (x === LF && i > 0 && bytes[i - 1] !== CR) {
|
index = bytes.indexOf(LF, i) + 1;
|
||||||
normalized.push(CR, LF);
|
if (index && bytes[index - 2] !== CR) indices.push(index);
|
||||||
} else {
|
else break;
|
||||||
normalized.push(x);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return new Uint8Array(normalized);
|
if (!indices.length) {
|
||||||
}, () => new Uint8Array(carryOverCR ? [CR] : []));
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalized = new Uint8Array(bytes.length + indices.length);
|
||||||
|
let j = 0;
|
||||||
|
for (let i = 0; i < indices.length; i++) {
|
||||||
|
const sub = bytes.subarray(indices[i - 1] || 0, indices[i]);
|
||||||
|
normalized.set(sub, j);
|
||||||
|
j += sub.length;
|
||||||
|
normalized[j - 1] = CR;
|
||||||
|
normalized[j] = LF;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
normalized.set(bytes.subarray(indices[indices.length - 1] || 0), j);
|
||||||
|
return normalized;
|
||||||
|
}, () => (carryOverCR ? new Uint8Array([CR]) : undefined));
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -730,17 +745,30 @@ export default {
|
||||||
let carryOverCR = false;
|
let carryOverCR = false;
|
||||||
|
|
||||||
return stream.transform(data, bytes => {
|
return stream.transform(data, bytes => {
|
||||||
bytes = carryOverCR ? [CR].concat(Array.from(bytes)) : Array.from(bytes);
|
if (carryOverCR && bytes[0] !== LF) {
|
||||||
|
bytes = util.concatUint8Array([new Uint8Array([CR]), bytes]);
|
||||||
|
} else {
|
||||||
|
bytes = new Uint8Array(bytes); // Don't mutate passed bytes
|
||||||
|
}
|
||||||
|
|
||||||
if (bytes[bytes.length - 1] === CR) {
|
if (bytes[bytes.length - 1] === CR) {
|
||||||
carryOverCR = true;
|
carryOverCR = true;
|
||||||
bytes.pop();
|
bytes = bytes.subarray(0, -1);
|
||||||
} else {
|
} else {
|
||||||
carryOverCR = false;
|
carryOverCR = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Uint8Array(bytes.filter((x, i, xs) => (x !== CR || (i < xs.length - 1 && xs[i + 1] !== LF))));
|
let index;
|
||||||
}, () => new Uint8Array(carryOverCR ? [CR] : []));
|
let j = 0;
|
||||||
|
for (let i = 0; i !== bytes.length; i = index) {
|
||||||
|
index = bytes.indexOf(CR, i) + 1;
|
||||||
|
if (!index) index = bytes.length;
|
||||||
|
const last = index - (bytes[index] === LF ? 1 : 0);
|
||||||
|
if (i) bytes.copyWithin(j, i, last);
|
||||||
|
j += last - i;
|
||||||
|
}
|
||||||
|
return bytes.subarray(0, j);
|
||||||
|
}, () => (carryOverCR ? new Uint8Array([CR]) : undefined));
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue
Block a user