export function createStreamRequest()

in src/http/HttpRequest.ts [117:165]


export function createStreamRequest(
    proxyReq: IncomingMessage,
    triggerMetadata: Record<string, RpcTypedData>
): HttpRequest {
    const hostHeaderName = 'x-forwarded-host';
    const protoHeaderName = 'x-forwarded-proto';
    const host = proxyReq.headers[hostHeaderName];
    const proto = proxyReq.headers[protoHeaderName];
    if (typeof host !== 'string' || typeof proto !== 'string') {
        throw new AzFuncSystemError(`Expected headers "${hostHeaderName}" and "${protoHeaderName}" to be set.`);
    }
    const url = `${proto}://${host}${nonNullProp(proxyReq, 'url')}`;

    let body: stream.Readable | undefined;
    const lowerMethod = proxyReq.method?.toLowerCase();
    if (lowerMethod !== 'get' && lowerMethod !== 'head') {
        body = proxyReq;
    }

    // Get headers and params from trigger metadata
    // See here for more info: https://github.com/Azure/azure-functions-host/issues/9840
    // NOTE: We ignore query info because it has this bug: https://github.com/Azure/azure-functions-nodejs-library/issues/168
    const { Query: rpcQueryIgnored, Headers: rpcHeaders, ...rpcParams } = triggerMetadata;

    let headers: HeadersInit | undefined;
    const headersData = fromRpcTypedData(rpcHeaders);
    if (typeof headersData === 'object' && isDefined(headersData)) {
        headers = <HeadersInit>headersData;
    }

    const uReq = new uRequest(url, {
        body,
        duplex: 'half',
        method: nonNullProp(proxyReq, 'method'),
        headers,
    });

    const params: Record<string, string> = {};
    for (const [key, rpcValue] of Object.entries(rpcParams)) {
        if (isDefined(rpcValue.string)) {
            params[key] = rpcValue.string;
        }
    }

    return new HttpRequest({
        undiciRequest: uReq,
        params,
    });
}