bit-bin
Version:
<a href="https://opensource.org/licenses/Apache-2.0"><img alt="apache" src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"></a> <a href="https://github.com/teambit/bit/blob/master/CONTRIBUTING.md"><img alt="prs" src="https://img.shields.io/b
33 lines (29 loc) • 1.28 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = replaceBuffer;
/**
* replace string with another string inside Buffer without converting it to string.
* other npm packages, such as replace-buffer and buffer-replace are recursive, and as such,
* when using huge files (more than 8.5K lines of code), it crushes with an error "Maximum call
* stack size exceeded".
*/
function replaceBuffer(buffer, oldStr, newStr) {
if (!(buffer instanceof Buffer)) throw new Error(`replaceBuffer expect to get Buffer, got ${typeof buffer} instead`);
if (!buffer.includes(oldStr)) return buffer;
const bufferOldStr = Buffer.from(oldStr);
const bufferOldStrLength = bufferOldStr.length;
const bufferNewStr = Buffer.from(newStr);
let idx = buffer.indexOf(oldStr);
let bufferInProgress = Buffer.from('');
let startingPoint = 0;
while (idx !== -1) {
const bufferStart = buffer.slice(startingPoint, idx);
bufferInProgress = Buffer.concat([bufferInProgress, bufferStart, bufferNewStr]);
startingPoint = idx + bufferOldStrLength;
idx = buffer.indexOf(oldStr, idx + bufferOldStrLength);
}
const bufferEnd = buffer.slice(startingPoint, buffer.length);
return Buffer.concat([bufferInProgress, bufferEnd]);
}
;