Combine Request and RequestProperties (#485)

This commit is contained in:
Eric Jizba 2021-12-02 11:28:51 -08:00 коммит произвёл GitHub
Родитель 62d0ab5998
Коммит f6a7f575ca
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 30 добавлений и 45 удалений

Просмотреть файл

@ -14,14 +14,13 @@ import { v4 as uuid } from 'uuid';
import { AzureFunctionsRpcMessages as rpc } from '../azure-functions-language-worker-protobuf/src/rpc';
import {
convertKeysToCamelCase,
fromRpcHttp,
fromRpcTraceContext,
fromTypedData,
getBindingDefinitions,
getNormalizedBindingData,
} from './converters';
import { FunctionInfo } from './FunctionInfo';
import { Request, RequestProperties } from './http/Request';
import { Request } from './http/Request';
import { Response } from './http/Response';
import LogLevel = rpc.RpcLog.Level;
import LogCategory = rpc.RpcLog.RpcLogCategory;
@ -36,12 +35,12 @@ export function CreateContextAndInputs(
const bindings: ContextBindings = {};
const inputs: any[] = [];
let httpInput: RequestProperties | undefined;
let httpInput: Request | undefined;
for (const binding of <rpc.IParameterBinding[]>request.inputData) {
if (binding.data && binding.name) {
let input;
if (binding.data && binding.data.http) {
input = httpInput = fromRpcHttp(binding.data.http);
input = httpInput = new Request(binding.data.http);
} else {
// TODO: Don't hard code fix for camelCase https://github.com/Azure/azure-functions-nodejs-worker/issues/188
if (info.getTimerTriggerName() === binding.name) {
@ -58,7 +57,7 @@ export function CreateContextAndInputs(
context.bindings = bindings;
if (httpInput) {
context.req = new Request(httpInput);
context.req = httpInput;
context.res = new Response(context.done);
// This is added for backwards compatability with what the host used to send to the worker
context.bindingData.sys = {

Просмотреть файл

@ -1,13 +1,12 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License.
import { Cookie, HttpMethod } from '@azure/functions';
import { Cookie } from '@azure/functions';
import {
AzureFunctionsRpcMessages as rpc,
INullableString,
} from '../../azure-functions-language-worker-protobuf/src/rpc';
import { Dict } from '../Context';
import { RequestProperties } from '../http/Request';
import {
fromTypedData,
toNullableBool,
@ -18,32 +17,13 @@ import {
toTypedData,
} from './RpcConverters';
/**
* Converts 'IRpcHttp' input from the RPC layer to a JavaScript object.
* @param rpcHttp RPC layer representation of an HTTP request
*/
export function fromRpcHttp(rpcHttp: rpc.IRpcHttp): RequestProperties {
const httpContext: RequestProperties = {
method: <HttpMethod>rpcHttp.method,
url: <string>rpcHttp.url,
originalUrl: <string>rpcHttp.url,
headers: fromNullableMapping(rpcHttp.nullableHeaders, rpcHttp.headers),
query: fromNullableMapping(rpcHttp.nullableQuery, rpcHttp.query),
params: fromNullableMapping(rpcHttp.nullableParams, rpcHttp.params),
body: fromTypedData(<rpc.ITypedData>rpcHttp.body),
rawBody: fromRpcHttpBody(<rpc.ITypedData>rpcHttp.body),
};
return httpContext;
}
/**
* Converts the provided body from the RPC layer to the appropriate javascript object.
* Body of type 'byte' is a special case and it's converted to it's utf-8 string representation.
* This is to avoid breaking changes in v2.
* @param body The body from the RPC layer.
*/
function fromRpcHttpBody(body: rpc.ITypedData) {
export function fromRpcHttpBody(body: rpc.ITypedData) {
if (body && body.bytes) {
return (<Buffer>body.bytes).toString();
} else {
@ -51,7 +31,7 @@ function fromRpcHttpBody(body: rpc.ITypedData) {
}
}
function fromNullableMapping(
export function fromNullableMapping(
nullableMapping: { [k: string]: INullableString } | null | undefined,
originalMapping?: { [k: string]: string } | null
): Dict<string> {

Просмотреть файл

@ -1,27 +1,33 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License.
import { HttpMethod, HttpRequest } from '@azure/functions';
import { HttpMethod, HttpRequest, HttpRequestHeaders, HttpRequestParams, HttpRequestQuery } from '@azure/functions';
import { AzureFunctionsRpcMessages as rpc } from '../../azure-functions-language-worker-protobuf/src/rpc';
import { fromTypedData } from '../converters/RpcConverters';
import { fromNullableMapping, fromRpcHttpBody } from '../converters/RpcHttpConverters';
export class RequestProperties implements HttpRequest {
method: HttpMethod | null = null;
url = '';
originalUrl = '';
headers: { [key: string]: string } = {};
query: { [key: string]: string } = {};
params: { [key: string]: string } = {};
body?: any;
rawBody?: any;
[key: string]: any;
}
export class Request implements HttpRequest {
public method: HttpMethod | null;
public url: string;
public originalUrl: string;
public headers: HttpRequestHeaders;
public query: HttpRequestQuery;
public params: HttpRequestParams;
public body?: any;
public rawBody?: any;
export class Request extends RequestProperties {
constructor(httpInput: RequestProperties) {
super();
Object.assign(this, httpInput);
public constructor(rpcHttp: rpc.IRpcHttp) {
this.method = <HttpMethod>rpcHttp.method;
this.url = <string>rpcHttp.url;
this.originalUrl = <string>rpcHttp.url;
this.headers = fromNullableMapping(rpcHttp.nullableHeaders, rpcHttp.headers);
this.query = fromNullableMapping(rpcHttp.nullableQuery, rpcHttp.query);
this.params = fromNullableMapping(rpcHttp.nullableParams, rpcHttp.params);
this.body = fromTypedData(<rpc.ITypedData>rpcHttp.body);
this.rawBody = fromRpcHttpBody(<rpc.ITypedData>rpcHttp.body);
}
get(field: string): string | undefined {
public get(field: string): string | undefined {
return this.headers && this.headers[field.toLowerCase()];
}
}