From 05677a5c5a9a16ee2633957bc29eec015dd6d891 Mon Sep 17 00:00:00 2001 From: Udit Date: Wed, 16 Aug 2023 15:42:02 +0530 Subject: [PATCH] Adapt gateway request to handle freeform stdin --- packages/core/src/helpers/functions.ts | 32 ++++++++++++++++++++++---- packages/core/src/services/headNode.ts | 4 ++-- packages/core/src/utils/strings.ts | 14 +++++++++++ packages/core/test/functions.test.ts | 3 ++- packages/server/src/routes/invoke.ts | 1 - 5 files changed, 45 insertions(+), 9 deletions(-) diff --git a/packages/core/src/helpers/functions.ts b/packages/core/src/helpers/functions.ts index f0bc4a2..bd15e13 100644 --- a/packages/core/src/helpers/functions.ts +++ b/packages/core/src/helpers/functions.ts @@ -3,7 +3,7 @@ import { IHeadNodeResponse } from '../interfaces/headNode' import { IFunctionEnvVarRecord, IFunctionRequestData } from '../models' import { generateCRC32Checksum } from '../utils/checksum' import { decryptValue } from '../utils/encryption' -import { normalize } from '../utils/strings' +import { convertRequestBodyToString, normalize } from '../utils/strings' /** * A utility function to parse env vars key-value array from a function's env var records @@ -39,6 +39,31 @@ export function parseFunctionEnvVars( export function parseFunctionRequestVars(requestData: IFunctionRequestData): INameValueArray { const requestVars = [] as INameValueArray + // Look for stdin over request vars + let stdin = requestData.path || '/' + if (requestData.method === 'GET') { + // Look in query + if (requestData.query?.stdin) { + stdin = requestData.query.stdin + } + } else if (requestData.body) { + // Look in body + if (typeof requestData.body === 'object' && 'stdin' in requestData.body) { + stdin = String(requestData.body.stdin) + } else if (typeof requestData.body === 'string') { + stdin = requestData.body + } else if (requestData.query?.stdin) { + stdin = requestData.query.stdin + } + } + + if (stdin) { + requestVars.push({ + name: 'BLS_REQUEST_STDIN', + value: stdin + }) + } + requestVars.push({ name: 'BLS_REQUEST_METHOD', value: requestData.method || 'GET' @@ -73,10 +98,7 @@ export function parseFunctionRequestVars(requestData: IFunctionRequestData): INa if (requestData.body) { requestVars.push({ name: 'BLS_REQUEST_BODY', - value: - typeof requestData.body !== 'string' - ? JSON.stringify(requestData.body) - : (requestData.body as string) + value: convertRequestBodyToString(requestData.body) }) } diff --git a/packages/core/src/services/headNode.ts b/packages/core/src/services/headNode.ts index 1655427..1eeac3b 100644 --- a/packages/core/src/services/headNode.ts +++ b/packages/core/src/services/headNode.ts @@ -124,7 +124,7 @@ function parsePayload( manifest: IFunctionManifestRecord, envVars: INameValueArray ): IHeadNodePayload { - const pathObj = envVars.find((e) => e.name === 'BLS_REQUEST_PATH') + const stdin = envVars.find((e) => e.name === 'BLS_REQUEST_STDIN') return { function_id: functionId, method: manifest.entry, @@ -132,7 +132,7 @@ function parsePayload( config: { permissions: [...manifest.permissions], env_vars: [...envVars], - stdin: pathObj ? pathObj.value : '/', + stdin: stdin ? stdin.value : '', number_of_nodes: 1 } } diff --git a/packages/core/src/utils/strings.ts b/packages/core/src/utils/strings.ts index 4a23114..821055c 100644 --- a/packages/core/src/utils/strings.ts +++ b/packages/core/src/utils/strings.ts @@ -14,3 +14,17 @@ export const normalize = (v: string) => { return formatted } + +export function convertRequestBodyToString(input: unknown): string { + if (typeof input === 'string') { + return input // Already a string, no conversion needed + } else if (typeof input === 'number' || typeof input === 'boolean') { + return String(input) // Convert numbers and booleans to string + } else if (input instanceof Date) { + return input.toISOString() // Convert Date objects to ISO string + } else if (input instanceof Object) { + return JSON.stringify(input) // Convert objects to JSON string + } else { + return String(input) // Fallback for other types + } +} diff --git a/packages/core/test/functions.test.ts b/packages/core/test/functions.test.ts index 05521d3..250d5a1 100644 --- a/packages/core/test/functions.test.ts +++ b/packages/core/test/functions.test.ts @@ -143,8 +143,9 @@ describe('Functions Request Parser', () => { } const result = parseFunctionRequestVars(requestData) - expect(result.length).toBe(6) + expect(result.length).toBe(7) expect(result).toStrictEqual([ + { name: 'BLS_REQUEST_STDIN', value: '/test' }, { name: 'BLS_REQUEST_METHOD', value: 'GET' }, { name: 'BLS_REQUEST_PATH', value: '/test' }, { name: 'BLS_REQUEST_PARAMS', value: 'param1=value1¶m2=value2' }, diff --git a/packages/server/src/routes/invoke.ts b/packages/server/src/routes/invoke.ts index c4e51ba..e952e6b 100644 --- a/packages/server/src/routes/invoke.ts +++ b/packages/server/src/routes/invoke.ts @@ -19,7 +19,6 @@ async function invokeHostnameAPI(request: FastifyRequest, reply: FastifyReply) { host: request.hostname, path: decodeURIComponent(request.url.split('?')[0]), method: request.method, - params: request.params as IFunctionRequestData['params'], query: request.query as IFunctionRequestData['query'], headers: request.headers as IFunctionRequestData['headers'], body: request.body as unknown