"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const SESSION_PING_TIMEOUT = 2000;
function default_1(s, contentLength) {
    return new Promise((resolve, reject) => {
        let currentLength = 0;
        const chunks = [];
        const finalLength = typeof contentLength === 'string' ? parseInt(contentLength, 10) : null;
        const http2session = finalLength === null && 'session' in s &&
            s.session || null;
        let isResolved = false;
        let timeout;
        s.on('data', (chunk) => {
            chunks.push(chunk);
            currentLength += chunk.length;
            if (currentLength === finalLength) {
                isResolved = true;
                resolve(Buffer.concat(chunks));
            }
            if (http2session) {
                clearTimeout(timeout);
                timeout = setTimeout(() => http2session.ping(lodash_1.noop), SESSION_PING_TIMEOUT);
            }
        });
        s.once('end', () => {
            clearTimeout(timeout);
            if (!isResolved)
                resolve(Buffer.concat(chunks));
        });
        s.once('error', error => {
            clearTimeout(timeout);
            reject(error);
        });
    });
}
exports.default = default_1;module.exports = exports.default;