"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.__tsfTypeAliases = exports.HttpRequestStream = exports.HttpRequest = exports.defaultHttpRequestBodyLimits = void 0;
const tslib_1 = require("tslib");
const _a = tslib_1.__importStar(require("@zyno-io/ts-server-foundation/type-metadata-runtime"));
const node_stream_1 = require("node:stream");
const node_zlib_1 = require("node:zlib");
const errors_1 = require("./errors");
const __tsf_metadata_type = _a.createCompactMetadataRegistryV1("[1,[{\"kind\":18,\"typeName\":\"Record\",\"utilityType\":\"Record\",\"typeArguments\":[{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":2,\"typeName\":\"symbol\"}]},{\"kind\":2}],\"index\":{\"kind\":2},\"types\":[]},{\"kind\":18,\"typeName\":\"Record\",\"utilityType\":\"Record\",\"typeArguments\":[{\"kind\":6},{\"kind\":6}],\"index\":{\"kind\":6},\"types\":[]},{\"kind\":18,\"typeName\":\"Record\",\"utilityType\":\"Record\",\"typeArguments\":[{\"kind\":6},{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":14,\"type\":{\"kind\":6}}]}],\"index\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":14,\"type\":{\"kind\":6}}]},\"types\":[]},{\"kind\":6},{\"kind\":13,\"types\":[{\"kind\":18,\"typeName\":\"Record\",\"utilityType\":\"Record\",\"typeArguments\":[{\"kind\":6},{\"kind\":6}],\"index\":{\"kind\":6},\"types\":[]},{\"kind\":2,\"typeName\":\"(headers: HttpHeaderInput) =\\u003e HttpRequest\"}],\"typeName\":\"HttpRequestHeaders\"},{\"kind\":2},{\"kind\":18,\"typeName\":\"HttpRequestBodyLimits\",\"types\":[{\"kind\":20,\"name\":\"maxBodyBytes\",\"type\":{\"kind\":7},\"optional\":false},{\"kind\":20,\"name\":\"maxCompressedBodyBytes\",\"type\":{\"kind\":7},\"optional\":false}]},{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":7},{\"kind\":8}]},{\"kind\":17},{\"kind\":22,\"type\":{\"kind\":6}},{\"kind\":18,\"typeName\":\"Partial\",\"typeArguments\":[{\"kind\":18,\"typeName\":\"HttpRequestBodyLimits\",\"types\":[{\"kind\":20,\"name\":\"maxBodyBytes\",\"type\":{\"kind\":7},\"optional\":false},{\"kind\":20,\"name\":\"maxCompressedBodyBytes\",\"type\":{\"kind\":7},\"optional\":false}]}],\"types\":[{\"kind\":20,\"name\":\"maxBodyBytes\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"maxCompressedBodyBytes\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true}]},{\"kind\":3},{\"kind\":12,\"types\":[{\"kind\":12,\"types\":[{\"kind\":10,\"literal\":\"GET\"},{\"kind\":10,\"literal\":\"POST\"},{\"kind\":10,\"literal\":\"PUT\"},{\"kind\":10,\"literal\":\"PATCH\"},{\"kind\":10,\"literal\":\"DELETE\"},{\"kind\":10,\"literal\":\"OPTIONS\"},{\"kind\":10,\"literal\":\"HEAD\"}],\"typeName\":\"KnownHttpMethod\"},{\"kind\":13,\"types\":[{\"kind\":6},{\"kind\":18,\"types\":[]}]}],\"typeName\":\"HttpMethod\"},{\"kind\":18,\"typeName\":\"Record\",\"utilityType\":\"Record\",\"typeArguments\":[{\"kind\":6},{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":14,\"type\":{\"kind\":6}},{\"kind\":7},{\"kind\":8},{\"kind\":4}]}],\"index\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":14,\"type\":{\"kind\":6}},{\"kind\":7},{\"kind\":8},{\"kind\":4}]},\"types\":[],\"typeName\":\"HttpHeaderInput\"},{\"kind\":2,\"typeName\":\"(error?: Error | null, data?: Buffer) =\\u003e void\"},{\"kind\":7}]]", []);
exports.defaultHttpRequestBodyLimits = {
    maxBodyBytes: 100 * 1024 * 1024,
    maxCompressedBodyBytes: 25 * 1024 * 1024
};
let _HttpRequestMetadata_1;
class HttpRequest extends node_stream_1.Readable {
    method;
    url;
    bodyStream;
    store = {};
    context = {};
    query = {};
    path;
    headers;
    body;
    parsedBody;
    uploadedFiles = {};
    pathParams = {};
    remoteAddress = '127.0.0.1';
    socket = { remoteAddress: this.remoteAddress };
    trustProxyHeaders = false;
    bodyLimits = { ...exports.defaultHttpRequestBodyLimits };
    bodyRead;
    streamAttached = false;
    streamEnded = false;
    cachedBodyPushed = false;
    bodyGuardBypass = false;
    attachedStream;
    cachedBodyDecoded = false;
    constructor(method, url, headers = {}, body, bodyStream) {
        super();
        this.method = method;
        this.url = url;
        this.bodyStream = bodyStream;
        this.headers = createHeaderStore(headers, this);
        const parsed = new URL(url, 'http://localhost');
        this.path = parsed.pathname;
        for (const [key, value] of parsed.searchParams.entries()) {
            const current = this.query[key];
            if (current === undefined) {
                this.query[key] = value;
            }
            else if (Array.isArray(current)) {
                current.push(value);
            }
            else {
                this.query[key] = [current, value];
            }
        }
        if (Buffer.isBuffer(body))
            this.body = body;
        else if (typeof body === 'string')
            this.body = Buffer.from(body);
        else if (body !== undefined) {
            this.parsedBody = body;
            this.body = Buffer.from(JSON.stringify(body));
            this.headers['content-type'] ??= 'application/json';
        }
    }
    static GET(url, headers) {
        return new HttpRequest('GET', url, headers);
    }
    static POST(url, body, headers) {
        return new HttpRequest('POST', url, headers, body);
    }
    static PUT(url, body, headers) {
        return new HttpRequest('PUT', url, headers, body);
    }
    static PATCH(url, body, headers) {
        return new HttpRequest('PATCH', url, headers, body);
    }
    static DELETE(url, headers) {
        return new HttpRequest('DELETE', url, headers);
    }
    static OPTIONS(url, headers) {
        return new HttpRequest('OPTIONS', url, headers);
    }
    static HEAD(url, headers) {
        return new HttpRequest('HEAD', url, headers);
    }
    build() {
        return this;
    }
    header(name, value) {
        this.headers[name.toLowerCase()] = String(value);
        return this;
    }
    json(body) {
        this.parsedBody = body;
        this.body = Buffer.from(JSON.stringify(body));
        this.headers['content-type'] = 'application/json';
        return this;
    }
    multiPart(parts) {
        const boundary = `----tsf-${Math.random().toString(16).slice(2)}`;
        const chunks = [];
        for (const part of parts) {
            chunks.push(Buffer.from(`--${boundary}\r\n`));
            if (part.file !== undefined) {
                chunks.push(Buffer.from(`Content-Disposition: form-data; name="${escapeMultipartName(part.name)}"; filename="${escapeMultipartName(part.fileName ?? 'upload')}"\r\n` +
                    `Content-Type: ${part.contentType ?? 'application/octet-stream'}\r\n\r\n`));
                chunks.push(Buffer.isBuffer(part.file) ? part.file : Buffer.from(part.file));
                chunks.push(Buffer.from('\r\n'));
                continue;
            }
            chunks.push(Buffer.from(`Content-Disposition: form-data; name="${escapeMultipartName(part.name)}"\r\n\r\n${part.value ?? ''}\r\n`));
        }
        chunks.push(Buffer.from(`--${boundary}--\r\n`));
        this.body = Buffer.concat(chunks);
        this.parsedBody = undefined;
        this.headers['content-type'] = `multipart/form-data; boundary=${boundary}`;
        return this;
    }
    getRemoteAddress() {
        if (!this.trustProxyHeaders)
            return this.remoteAddress;
        const realIp = getHeader(this.headers, 'x-real-ip');
        if (typeof realIp === 'string')
            return realIp;
        const forwarded = getHeader(this.headers, 'x-forwarded-for');
        if (typeof forwarded === 'string')
            return forwarded.split(',')[0]?.trim() || this.remoteAddress;
        return this.remoteAddress;
    }
    async readBodyText() {
        return (await this.readBodyBuffer()).toString();
    }
    async readBody() {
        return this.readBodyBuffer();
    }
    async readBodyBuffer() {
        if (this.body) {
            if (this.bodyGuardBypass || this.cachedBodyDecoded)
                return this.body;
            if (this.bodyRead)
                return this.bodyRead;
            this.bodyRead = readStreamBody(createGuardedRequestBodyStream(node_stream_1.Readable.from([this.body]), this.headers, this.bodyLimits)).then(body => {
                this.body = body;
                this.cachedBodyDecoded = true;
                return body;
            });
            return this.bodyRead;
        }
        if (!this.bodyStream)
            return Buffer.alloc(0);
        if (this.bodyRead)
            return this.bodyRead;
        if (this.streamAttached)
            throw new Error('Request body stream is already being consumed');
        this.bodyRead = readStreamBody(this.createBodyReadStream()).then(body => {
            this.body = body;
            this.cachedBodyDecoded = true;
            return body;
        });
        return this.bodyRead;
    }
    get stream() {
        if (this.body && !this.bodyGuardBypass && !this.cachedBodyDecoded)
            return createGuardedRequestBodyStream(node_stream_1.Readable.from([this.body]), this.headers, this.bodyLimits);
        if (this.bodyStream && !this.body)
            return this.createBodyReadStream();
        return node_stream_1.Readable.from(this.body ? [this.body] : []);
    }
    setBodyLimits(limits) {
        this.bodyLimits = { ...this.bodyLimits, ...limits };
    }
    enableBodyGuardBypass() {
        this.bodyGuardBypass = true;
    }
    _read() {
        if (this.streamEnded)
            return;
        if (this.body !== undefined && !this.bodyGuardBypass && !this.cachedBodyDecoded) {
            this.attachReadable(createGuardedRequestBodyStream(node_stream_1.Readable.from([this.body]), this.headers, this.bodyLimits));
            return;
        }
        if (this.body !== undefined) {
            if (!this.cachedBodyPushed) {
                this.cachedBodyPushed = true;
                if (this.body.length)
                    this.push(this.body);
                this.streamEnded = true;
                this.push(null);
            }
            return;
        }
        if (!this.bodyStream) {
            this.streamEnded = true;
            this.push(null);
            return;
        }
        if (this.attachedStream) {
            this.attachedStream.resume();
            return;
        }
        this.attachReadable(this.createBodyReadStream());
    }
    attachReadable(stream) {
        if (this.attachedStream) {
            this.attachedStream.resume();
            return;
        }
        this.attachedStream = stream;
        stream.on('data', chunk => {
            if (!this.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk))) {
                stream.pause();
            }
        });
        stream.once('end', () => {
            this.streamEnded = true;
            this.push(null);
        });
        stream.once('error', error => this.destroy(error));
        stream.resume();
    }
    createBodyReadStream() {
        if (this.streamAttached)
            throw new Error('Request body stream is already being consumed');
        this.streamAttached = true;
        if (!this.bodyStream)
            return node_stream_1.Readable.from([]);
        if (this.bodyGuardBypass)
            return this.bodyStream;
        return createGuardedRequestBodyStream(this.bodyStream, this.headers, this.bodyLimits);
    }
    static __tsfType = _HttpRequestMetadata_1 = _a.decodeCompactMetadataV1("[1,{\"kind\":16,\"name\":\"HttpRequest\",\"typeName\":\"HttpRequest\",\"classType\":{\"$tsf\":0},\"properties\":[{\"name\":\"store\",\"type\":{\"$tsfType\":0},\"optional\":false},{\"name\":\"context\",\"type\":{\"$tsfType\":1},\"optional\":false},{\"name\":\"query\",\"type\":{\"$tsfType\":2},\"optional\":false},{\"name\":\"path\",\"type\":{\"$tsfType\":3},\"optional\":false},{\"name\":\"headers\",\"type\":{\"$tsfType\":4},\"optional\":false},{\"name\":\"body\",\"type\":{\"kind\":16,\"typeName\":\"Buffer\",\"classType\":{\"$tsf\":1}},\"optional\":true},{\"name\":\"parsedBody\",\"type\":{\"$tsfType\":5},\"optional\":true},{\"name\":\"uploadedFiles\",\"type\":{\"kind\":18,\"typeName\":\"Record\",\"utilityType\":\"Record\",\"typeArguments\":[{\"kind\":6},{\"kind\":12,\"types\":[{\"kind\":16,\"typeName\":\"FileUpload\",\"classType\":{\"$tsf\":2}},{\"kind\":14,\"type\":{\"kind\":16,\"typeName\":\"FileUpload\",\"classType\":{\"$tsf\":2}}}]}],\"index\":{\"kind\":12,\"types\":[{\"kind\":16,\"typeName\":\"FileUpload\",\"classType\":{\"$tsf\":2}},{\"kind\":14,\"type\":{\"kind\":16,\"typeName\":\"FileUpload\",\"classType\":{\"$tsf\":2}}}]},\"types\":[],\"typeName\":\"UploadedFiles\"},\"optional\":false},{\"name\":\"pathParams\",\"type\":{\"$tsfType\":1},\"optional\":false},{\"name\":\"remoteAddress\",\"type\":{\"$tsfType\":5},\"optional\":false},{\"name\":\"socket\",\"type\":{\"kind\":18,\"typeName\":\"Pick\",\"utilityType\":\"Pick\",\"typeArguments\":[{\"$tsfAlias\":[3,\"node:net\",\"Socket\",\"Socket\"]}],\"utilityKeys\":[\"remoteAddress\"],\"types\":[]},\"optional\":false},{\"name\":\"trustProxyHeaders\",\"type\":{\"$tsfType\":5},\"optional\":false},{\"name\":\"bodyLimits\",\"type\":{\"$tsfType\":6},\"optional\":false}],\"methods\":[{\"name\":\"build\",\"parameters\":[],\"returnType\":{\"kind\":16,\"typeName\":\"this\",\"classType\":{\"$tsf\":4}}},{\"name\":\"header\",\"parameters\":[{\"name\":\"name\",\"type\":{\"$tsfType\":3},\"optional\":false,\"default\":false},{\"name\":\"value\",\"type\":{\"$tsfType\":7},\"optional\":false,\"default\":false}],\"returnType\":{\"kind\":16,\"typeName\":\"this\",\"classType\":{\"$tsf\":4}}},{\"name\":\"json\",\"parameters\":[{\"name\":\"body\",\"type\":{\"$tsfType\":8},\"optional\":false,\"default\":false}],\"returnType\":{\"kind\":16,\"typeName\":\"this\",\"classType\":{\"$tsf\":4}}},{\"name\":\"multiPart\",\"parameters\":[{\"name\":\"parts\",\"type\":{\"kind\":14,\"type\":{\"kind\":18,\"typeName\":\"MultipartRequestPart\",\"types\":[{\"kind\":20,\"name\":\"name\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"value\",\"type\":{\"kind\":12,\"types\":[{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":7},{\"kind\":8}]},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"file\",\"type\":{\"kind\":12,\"types\":[{\"kind\":12,\"types\":[{\"kind\":16,\"typeName\":\"Buffer\",\"classType\":{\"$tsf\":1}},{\"kind\":6}]},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"fileName\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"contentType\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true}]}},\"optional\":false,\"default\":false}],\"returnType\":{\"kind\":16,\"typeName\":\"this\",\"classType\":{\"$tsf\":4}}},{\"name\":\"getRemoteAddress\",\"parameters\":[],\"returnType\":{\"$tsfType\":3}},{\"name\":\"readBodyText\",\"parameters\":[],\"returnType\":{\"$tsfType\":9}},{\"name\":\"readBody\",\"parameters\":[],\"returnType\":{\"kind\":22,\"type\":{\"kind\":16,\"typeName\":\"Buffer\",\"classType\":{\"$tsf\":1}}}},{\"name\":\"readBodyBuffer\",\"parameters\":[],\"returnType\":{\"kind\":22,\"type\":{\"kind\":16,\"typeName\":\"Buffer\",\"classType\":{\"$tsf\":1}}}},{\"name\":\"setBodyLimits\",\"parameters\":[{\"name\":\"limits\",\"type\":{\"$tsfType\":10},\"optional\":false,\"default\":false}],\"returnType\":{\"$tsfType\":11}},{\"name\":\"enableBodyGuardBypass\",\"parameters\":[],\"returnType\":{\"$tsfType\":11}},{\"name\":\"_read\",\"parameters\":[],\"returnType\":{\"$tsfType\":11}}],\"hasConstructor\":true,\"constructorParameters\":[{\"name\":\"method\",\"type\":{\"$tsfType\":12},\"optional\":false,\"default\":false},{\"name\":\"url\",\"type\":{\"$tsfType\":3},\"optional\":false,\"default\":false},{\"name\":\"headers\",\"type\":{\"$tsfType\":13},\"optional\":true,\"default\":true},{\"name\":\"body\",\"type\":{\"kind\":12,\"types\":[{\"kind\":16,\"typeName\":\"Buffer\",\"classType\":{\"$tsf\":1}},{\"kind\":6},{\"kind\":17}]},\"optional\":true,\"default\":false},{\"name\":\"bodyStream\",\"type\":{\"$tsfAlias\":[3,\"node:stream\",\"Readable\",\"Readable\"]},\"optional\":true,\"default\":false}]}]", [() => HttpRequest, () => (typeof Buffer !== "undefined" ? Buffer : (typeof exports !== "undefined" ? exports.Buffer : undefined)), () => (typeof FileUpload !== "undefined" ? FileUpload : (typeof exports !== "undefined" ? exports.FileUpload : undefined)), require, () => (typeof this !== "undefined" ? this : (typeof exports !== "undefined" ? exports.this : undefined))], __tsf_metadata_type);
}
exports.HttpRequest = HttpRequest;
_HttpRequestMetadata_1.classType = HttpRequest;
HttpRequest.__tsfType = _HttpRequestMetadata_1;
let _HttpRequestStreamMetadata_1;
class HttpRequestStream extends HttpRequest {
    static __tsfType = _HttpRequestStreamMetadata_1 = _a.decodeCompactMetadataV1("[1,{\"kind\":16,\"name\":\"HttpRequestStream\",\"typeName\":\"HttpRequestStream\",\"classType\":{\"$tsf\":0},\"properties\":[],\"methods\":[],\"hasConstructor\":false,\"constructorParameters\":[]}]", [() => HttpRequestStream], __tsf_metadata_type);
}
exports.HttpRequestStream = HttpRequestStream;
_HttpRequestStreamMetadata_1.classType = HttpRequestStream;
HttpRequestStream.__tsfType = _HttpRequestStreamMetadata_1;
function getHeader(headers, name) {
    return headers[name] ?? headers[name.toLowerCase()];
}
function createHeaderStore(headers, request) {
    const store = ((nextHeaders) => {
        Object.assign(store, normalizeHeaders(nextHeaders));
        return request;
    });
    Object.assign(store, normalizeHeaders(headers));
    return store;
}
function normalizeHeaders(headers) {
    const normalized = {};
    for (const [name, value] of Object.entries(headers)) {
        if (value === undefined)
            continue;
        normalized[name.toLowerCase()] = Array.isArray(value) ? (value[0] ?? '') : String(value);
    }
    return normalized;
}
function createGuardedRequestBodyStream(stream, headers, limits) {
    const encoding = getContentEncoding(headers);
    if (encoding === 'identity') {
        throwIfContentLengthExceeds(headers, limits.maxBodyBytes, 'Request body is too large');
        return pipelineToReadable([stream, new ByteLimitTransform(limits.maxBodyBytes, 'Request body is too large')], encoding);
    }
    if (encoding === 'gzip' || encoding === 'x-gzip') {
        throwIfContentLengthExceeds(headers, limits.maxCompressedBodyBytes, 'Compressed request body is too large');
        return pipelineToReadable([
            stream,
            new ByteLimitTransform(limits.maxCompressedBodyBytes, 'Compressed request body is too large'),
            (0, node_zlib_1.createGunzip)(),
            new ByteLimitTransform(limits.maxBodyBytes, 'Request body is too large')
        ], encoding);
    }
    throw new errors_1.HttpUnsupportedMediaTypeError(`Unsupported request content encoding: ${encoding}`);
}
function pipelineToReadable(streams, encoding) {
    const output = new node_stream_1.PassThrough();
    const fail = (error) => {
        if (!output.destroyed)
            output.destroy(normalizeBodyStreamError(error, encoding));
    };
    // Register before pipeline() so the normalized HTTP error reaches readers
    // before pipeline propagates the lower-level zlib/stream error to output.
    for (const stream of streams)
        stream.once('error', fail);
    node_stream_1.pipeline(...streams, output, (error) => {
        if (error)
            fail(error);
    });
    return output;
}
function normalizeBodyStreamError(error, encoding) {
    if (error instanceof errors_1.HttpPayloadTooLargeError || error instanceof errors_1.HttpUnsupportedMediaTypeError || error instanceof errors_1.HttpBadRequestError)
        return error;
    if (encoding !== 'identity')
        return new errors_1.HttpBadRequestError('Failed to decode request body');
    return error;
}
function getContentEncoding(headers) {
    const value = headers['content-encoding'] ?? headers['Content-Encoding'];
    const encoding = (Array.isArray(value) ? value[0] : value)?.trim().toLowerCase();
    return encoding || 'identity';
}
function throwIfContentLengthExceeds(headers, maxBytes, message) {
    const value = headers['content-length'] ?? headers['Content-Length'];
    if (value === undefined)
        return;
    const size = Number.parseInt(Array.isArray(value) ? (value[0] ?? '') : value, 10);
    if (Number.isFinite(size) && size > maxBytes)
        throw new errors_1.HttpPayloadTooLargeError(message);
}
let _ByteLimitTransformMetadata_1;
class ByteLimitTransform extends node_stream_1.Transform {
    maxBytes;
    message;
    size = 0;
    constructor(maxBytes, message) {
        super();
        this.maxBytes = maxBytes;
        this.message = message;
    }
    _transform(chunk, encoding, callback) {
        const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, encoding);
        this.size += buffer.length;
        if (this.size > this.maxBytes) {
            callback(new errors_1.HttpPayloadTooLargeError(this.message));
            return;
        }
        callback(null, buffer);
    }
    static __tsfType = _ByteLimitTransformMetadata_1 = _a.decodeCompactMetadataV1("[1,{\"kind\":16,\"name\":\"ByteLimitTransform\",\"typeName\":\"ByteLimitTransform\",\"classType\":{\"$tsf\":0},\"properties\":[],\"methods\":[{\"name\":\"_transform\",\"parameters\":[{\"name\":\"chunk\",\"type\":{\"kind\":12,\"types\":[{\"kind\":16,\"typeName\":\"Buffer\",\"classType\":{\"$tsf\":1}},{\"kind\":6}]},\"optional\":false,\"default\":false},{\"name\":\"encoding\",\"type\":{\"kind\":16,\"typeName\":\"BufferEncoding\",\"classType\":{\"$tsf\":2}},\"optional\":false,\"default\":false},{\"name\":\"callback\",\"type\":{\"$tsfType\":14},\"optional\":false,\"default\":false}],\"returnType\":{\"$tsfType\":11}}],\"hasConstructor\":true,\"constructorParameters\":[{\"name\":\"maxBytes\",\"type\":{\"$tsfType\":15},\"optional\":false,\"default\":false},{\"name\":\"message\",\"type\":{\"$tsfType\":3},\"optional\":false,\"default\":false}]}]", [() => ByteLimitTransform, () => (typeof Buffer !== "undefined" ? Buffer : (typeof exports !== "undefined" ? exports.Buffer : undefined)), () => (typeof BufferEncoding !== "undefined" ? BufferEncoding : (typeof exports !== "undefined" ? exports.BufferEncoding : undefined))], __tsf_metadata_type);
}
_ByteLimitTransformMetadata_1.classType = ByteLimitTransform;
ByteLimitTransform.__tsfType = _ByteLimitTransformMetadata_1;
function escapeMultipartName(value) {
    return value.replace(/["\r\n]/g, '_');
}
function readStreamBody(stream) {
    return new Promise((resolve, reject) => {
        const chunks = [];
        stream.on('data', chunk => chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)));
        stream.on('end', () => resolve(Buffer.concat(chunks)));
        stream.on('error', reject);
    });
}
exports.__tsfTypeAliases = _a.decodeCompactMetadataV1("[1,{\"HttpHeaderInput\":{\"kind\":18,\"typeName\":\"Record\",\"utilityType\":\"Record\",\"typeArguments\":[{\"kind\":6},{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":14,\"type\":{\"kind\":6}},{\"kind\":7},{\"kind\":8},{\"kind\":4}]}],\"index\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":14,\"type\":{\"kind\":6}},{\"kind\":7},{\"kind\":8},{\"kind\":4}]},\"types\":[],\"typeName\":\"HttpHeaderInput\"},\"HttpMethod\":{\"kind\":12,\"types\":[{\"kind\":12,\"types\":[{\"kind\":10,\"literal\":\"GET\"},{\"kind\":10,\"literal\":\"POST\"},{\"kind\":10,\"literal\":\"PUT\"},{\"kind\":10,\"literal\":\"PATCH\"},{\"kind\":10,\"literal\":\"DELETE\"},{\"kind\":10,\"literal\":\"OPTIONS\"},{\"kind\":10,\"literal\":\"HEAD\"}],\"typeName\":\"KnownHttpMethod\"},{\"kind\":13,\"types\":[{\"kind\":6},{\"kind\":18,\"types\":[]}]}],\"typeName\":\"HttpMethod\"},\"HttpRequestBodyLimits\":{\"kind\":18,\"typeName\":\"HttpRequestBodyLimits\",\"types\":[{\"kind\":20,\"name\":\"maxBodyBytes\",\"type\":{\"kind\":7},\"optional\":false},{\"kind\":20,\"name\":\"maxCompressedBodyBytes\",\"type\":{\"kind\":7},\"optional\":false}]},\"HttpRequestHeaders\":{\"kind\":13,\"types\":[{\"kind\":18,\"typeName\":\"Record\",\"utilityType\":\"Record\",\"typeArguments\":[{\"kind\":6},{\"kind\":6}],\"index\":{\"kind\":6},\"types\":[]},{\"kind\":2,\"typeName\":\"(headers: HttpHeaderInput) =\\u003e HttpRequest\"}],\"typeName\":\"HttpRequestHeaders\"},\"KnownHttpMethod\":{\"kind\":12,\"types\":[{\"kind\":10,\"literal\":\"GET\"},{\"kind\":10,\"literal\":\"POST\"},{\"kind\":10,\"literal\":\"PUT\"},{\"kind\":10,\"literal\":\"PATCH\"},{\"kind\":10,\"literal\":\"DELETE\"},{\"kind\":10,\"literal\":\"OPTIONS\"},{\"kind\":10,\"literal\":\"HEAD\"}],\"typeName\":\"KnownHttpMethod\"},\"MultipartRequestPart\":{\"kind\":18,\"typeName\":\"MultipartRequestPart\",\"types\":[{\"kind\":20,\"name\":\"name\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"value\",\"type\":{\"kind\":12,\"types\":[{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":7},{\"kind\":8}]},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"file\",\"type\":{\"kind\":12,\"types\":[{\"kind\":12,\"types\":[{\"kind\":16,\"typeName\":\"Buffer\",\"classType\":{\"$tsf\":0}},{\"kind\":6}]},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"fileName\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"contentType\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true}]}}]", [() => (typeof Buffer !== "undefined" ? Buffer : (typeof exports !== "undefined" ? exports.Buffer : undefined))], __tsf_metadata_type);
//# sourceMappingURL=request.js.map