49 lines
1.5 KiB
JavaScript
49 lines
1.5 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.toReadableStream = exports.startsWith = exports.appendLine = exports.createLineIterator = exports.CRLF = void 0;
|
|
const stream_1 = require("stream");
|
|
const LF = 0x0A;
|
|
const CR = 0x0D;
|
|
const CRLF_LENGTH = 2;
|
|
exports.CRLF = Buffer.from([CR, LF]);
|
|
function createLineIterator(buffer) {
|
|
return {
|
|
[Symbol.iterator]: function* () {
|
|
const lastIdx = buffer.length - 1;
|
|
let start = 0;
|
|
for (let i = 0; i < buffer.length; i++) {
|
|
if (i === lastIdx)
|
|
yield buffer.slice(start);
|
|
else if (buffer[i] === CR && buffer[i + 1] === LF) {
|
|
yield buffer.slice(start, i);
|
|
start = i + CRLF_LENGTH;
|
|
}
|
|
}
|
|
},
|
|
};
|
|
}
|
|
exports.createLineIterator = createLineIterator;
|
|
function appendLine(lines, line) {
|
|
if (lines.length)
|
|
lines.push(exports.CRLF);
|
|
lines.push(line);
|
|
}
|
|
exports.appendLine = appendLine;
|
|
function startsWith(buffer, searchBuffer) {
|
|
if (buffer.length < searchBuffer.length)
|
|
return false;
|
|
for (let i = 0; i < searchBuffer.length; i++) {
|
|
if (buffer[i] !== searchBuffer[i])
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
exports.startsWith = startsWith;
|
|
function toReadableStream(buffer) {
|
|
const stream = new stream_1.Readable();
|
|
stream.push(buffer);
|
|
stream.push(null);
|
|
return stream;
|
|
}
|
|
exports.toReadableStream = toReadableStream;
|