const nlBuffer = Buffer.from([13, 10]);
/**
 * Parser for HTTP status lines
 */
export class StatusParser {
    logger;
    constructor(logger) {
        this.logger = logger;
    }
    /**
     * Parse HTTP status line from buffer
     * @param data Buffer containing status line data
     * @returns Parsed status result or undefined if incomplete
     */
    parse(data) {
        this.logger.info('Processing status');
        const index = data.indexOf(nlBuffer);
        if (index === -1) {
            // Incomplete status line, need more data
            return undefined;
        }
        let statusLine = data.slice(0, index).toString();
        const remaining = data.slice(index + 2);
        // Remove HTTP version prefix
        statusLine = statusLine.replace(/HTTP\/\d(\.\d)?\s/, '');
        const delimiterPos = statusLine.indexOf(' ');
        let status;
        let msg = '';
        if (delimiterPos === -1) {
            status = statusLine;
        }
        else {
            status = statusLine.substr(0, delimiterPos);
            msg = statusLine.substr(delimiterPos + 1);
        }
        let typedStatus = Number(status);
        if (Number.isNaN(typedStatus)) {
            typedStatus = 0;
        }
        if (msg && msg.indexOf('\n') !== -1) {
            msg = msg.split('\n')[0];
        }
        this.logger.info('Received status', typedStatus, msg);
        return {
            status: typedStatus,
            statusText: msg,
            remaining,
        };
    }
}
//# sourceMappingURL=StatusParser.js.map