import { JetroutineFunctionReturnTypes } from '../config/_symbols'; import { remote } from '../proto/runtime/v1alpha1/remote'; import { SystemError } from './_errors'; export const getPostResultRequest = ( execId: string, value?: JetroutineFunctionReturnTypes, error?: remote.Error, ) => { if (!execId) { throw new SystemError('Could not postResult: taskId must be present.'); } let returnValue; if (value) { const encodedValue = Buffer.from(JSON.stringify(value), 'utf8'); returnValue = new remote.Value({ encoded_value: encodedValue }); } const result = new remote.Result({ value: returnValue, error }); return new remote.PostResultRequest({ exec_id: execId, result }); }; // For external calls, we parse apiKey in v3 format: // v3:::::: export const getRuntimeHost = (apiKey: string) => { const decodedApiKey = Buffer.from(apiKey, 'base64').toString('utf-8'); if (!decodedApiKey) { throw new SystemError(`Could not decode api key ${apiKey}`); } const parts = decodedApiKey.split('::'); if (parts.length !== 4) { throw new SystemError(`Expected key to have 4 parts, but instead got ${parts.length} parts`); } if (parts[0] !== 'v3') { throw new SystemError(`Expected version v3, but got ${parts[0]}`); } return parts[3]; };