Skip to content

Commit

Permalink
Merge pull request #14 from blocklessnetwork/feature/eng-972-adapt-ga…
Browse files Browse the repository at this point in the history
…teway-request-to-handle-freeform-stdin

Adapt gateway request to handle freeform stdin
  • Loading branch information
uditdc authored Aug 16, 2023
2 parents 867ead6 + 05677a5 commit 0e872c7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
32 changes: 27 additions & 5 deletions packages/core/src/helpers/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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)
})
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/services/headNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ 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,
parameters: null,
config: {
permissions: [...manifest.permissions],
env_vars: [...envVars],
stdin: pathObj ? pathObj.value : '/',
stdin: stdin ? stdin.value : '',
number_of_nodes: 1
}
}
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/utils/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
3 changes: 2 additions & 1 deletion packages/core/test/functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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&param2=value2' },
Expand Down
1 change: 0 additions & 1 deletion packages/server/src/routes/invoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0e872c7

Please sign in to comment.