@@ -2,7 +2,17 @@ import crypto from 'crypto';
22import { ConnectorService } from '@sre/Core/ConnectorsService' ;
33import { AccessCandidate } from '@sre/Security/AccessControl/AccessCandidate.class' ;
44import zl from 'zip-lib' ;
5- import { InvokeCommand , Runtime , LambdaClient , UpdateFunctionCodeCommand , CreateFunctionCommand , GetFunctionCommand , GetFunctionCommandOutput , InvokeCommandOutput , UpdateFunctionConfigurationCommand } from '@aws-sdk/client-lambda' ;
5+ import {
6+ InvokeCommand ,
7+ Runtime ,
8+ LambdaClient ,
9+ UpdateFunctionCodeCommand ,
10+ CreateFunctionCommand ,
11+ GetFunctionCommand ,
12+ GetFunctionCommandOutput ,
13+ InvokeCommandOutput ,
14+ UpdateFunctionConfigurationCommand ,
15+ } from '@aws-sdk/client-lambda' ;
616import { GetRoleCommand , CreateRoleCommand , IAMClient , GetRoleCommandOutput , CreateRoleCommandOutput } from '@aws-sdk/client-iam' ;
717import fs from 'fs' ;
818import { AWSConfig , AWSCredentials , AWSRegionConfig } from '@sre/types/AWS.types' ;
@@ -19,7 +29,6 @@ export function getLambdaFunctionName(agentId: string, componentId: string) {
1929 return `${ agentId } -${ componentId } ` ;
2030}
2131
22-
2332export function generateCodeHash ( code_body : string , codeInputs : string [ ] , envVariables : string [ ] ) {
2433 const bodyHash = getSanitizeCodeHash ( code_body ) ;
2534 const inputsHash = getSanitizeCodeHash ( JSON . stringify ( codeInputs ) ) ;
@@ -79,9 +88,7 @@ export async function getDeployedCodeHash(agentId: string, componentId: string)
7988
8089export async function setDeployedCodeHash ( agentId : string , componentId : string , codeHash : string ) {
8190 const redisCache = ConnectorService . getCacheConnector ( ) ;
82- await redisCache
83- . user ( AccessCandidate . agent ( agentId ) )
84- . set ( `${ cachePrefix } _${ agentId } -${ componentId } ` , codeHash , null , null , cacheTTL ) ;
91+ await redisCache . user ( AccessCandidate . agent ( agentId ) ) . set ( `${ cachePrefix } _${ agentId } -${ componentId } ` , codeHash , null , null , cacheTTL ) ;
8592}
8693
8794function replaceVaultKeysTemplateVars ( code : string , envVariables : Record < string , string > ) {
@@ -121,7 +128,7 @@ export async function zipCode(directory: string) {
121128 } ,
122129 function ( err ) {
123130 reject ( err ) ;
124- } ,
131+ }
125132 ) ;
126133 } ) ;
127134}
@@ -276,7 +283,6 @@ export function getLambdaRolePolicy() {
276283 } ) ;
277284}
278285
279-
280286export async function updateDeployedCodeTTL ( agentId : string , componentId : string , ttl : number ) {
281287 const redisCache = ConnectorService . getCacheConnector ( ) ;
282288 await redisCache . user ( AccessCandidate . agent ( agentId ) ) . updateTTL ( `${ cachePrefix } _${ agentId } -${ componentId } ` , ttl ) ;
@@ -285,7 +291,7 @@ export async function updateDeployedCodeTTL(agentId: string, componentId: string
285291export async function invokeLambdaFunction (
286292 functionName : string ,
287293 inputs : { [ key : string ] : any } ,
288- awsCredentials : AWSCredentials & AWSRegionConfig ,
294+ awsCredentials : AWSCredentials & AWSRegionConfig
289295) : Promise < any > {
290296 try {
291297 const client = new LambdaClient ( {
@@ -384,11 +390,11 @@ export function reportUsage({ cost, agentId, teamId }: { cost: number; agentId:
384390
385391export function validateAsyncMainFunction ( rawCode : string ) : { isValid : boolean ; error ?: string ; parameters ?: string [ ] ; dependencies ?: string [ ] } {
386392 try {
387- const code = replaceVaultKeysTemplateVars ( rawCode , { } ) ;
393+ const code = replaceVaultKeysTemplateVars ( rawCode . trim ( ) , { } ) ;
388394 // Parse the code using acorn
389395 const ast = acorn . parse ( code , {
390396 ecmaVersion : 'latest' ,
391- sourceType : 'module'
397+ sourceType : 'module' ,
392398 } ) ;
393399
394400 // Extract library imports
@@ -414,22 +420,26 @@ export function validateAsyncMainFunction(rawCode: string): { isValid: boolean;
414420 }
415421
416422 // Handle CallExpression (require() calls)
417- if ( node . type === 'CallExpression' &&
423+ if (
424+ node . type === 'CallExpression' &&
418425 node . callee . type === 'Identifier' &&
419426 node . callee . name === 'require' &&
420427 node . arguments . length > 0 &&
421- node . arguments [ 0 ] . type === 'Literal' ) {
428+ node . arguments [ 0 ] . type === 'Literal'
429+ ) {
422430 const modulePath = node . arguments [ 0 ] . value ;
423431 if ( modulePath && ! modulePath . startsWith ( '.' ) && ! modulePath . startsWith ( '/' ) ) {
424432 libraries . add ( extractPackageName ( modulePath ) ) ;
425433 }
426434 }
427435
428436 // Handle dynamic import() calls
429- if ( node . type === 'CallExpression' &&
437+ if (
438+ node . type === 'CallExpression' &&
430439 node . callee . type === 'Import' &&
431440 node . arguments . length > 0 &&
432- node . arguments [ 0 ] . type === 'Literal' ) {
441+ node . arguments [ 0 ] . type === 'Literal'
442+ ) {
433443 const modulePath = node . arguments [ 0 ] . value ;
434444 if ( modulePath && ! modulePath . startsWith ( '.' ) && ! modulePath . startsWith ( '/' ) ) {
435445 libraries . add ( extractPackageName ( modulePath ) ) ;
@@ -503,23 +513,23 @@ export function validateAsyncMainFunction(rawCode: string): { isValid: boolean;
503513 return {
504514 isValid : false ,
505515 error : 'No main function found at root level' ,
506- dependencies
516+ dependencies,
507517 } ;
508518 }
509519
510520 if ( ! hasAsyncMain ) {
511521 return {
512522 isValid : false ,
513523 error : 'Main function exists but is not async' ,
514- dependencies
524+ dependencies,
515525 } ;
516526 }
517527
518528 return { isValid : true , parameters : mainParameters , dependencies } ;
519529 } catch ( error ) {
520530 return {
521531 isValid : false ,
522- error : `Failed to parse code: ${ error . message } `
532+ error : `Failed to parse code: ${ error . message } ` ,
523533 } ;
524534 }
525535}
@@ -549,7 +559,7 @@ export function generateCodeFromLegacyComponent(code_body: string, code_imports:
549559 async function main(${ codeInputs . join ( ', ' ) } ) {
550560 ${ code_body }
551561 }
552- `
562+ ` ;
553563 return code ;
554564}
555565
@@ -565,14 +575,12 @@ export function extractAllKeyNamesFromTemplateVars(input: string): string[] {
565575 return matches ;
566576}
567577
568-
569- async function fetchVaultSecret ( keyName : string , agentTeamId : string ) : Promise < { value : string , key : string } > {
578+ async function fetchVaultSecret ( keyName : string , agentTeamId : string ) : Promise < { value : string ; key : string } > {
570579 const vaultSecret = await VaultHelper . getAgentKey ( keyName , agentTeamId ) ;
571580 return {
572581 value : vaultSecret ,
573582 key : keyName ,
574583 } ;
575-
576584}
577585
578586export async function getCurrentEnvironmentVariables ( agentTeamId : string , code : string ) : Promise < Record < string , string > > {
@@ -588,4 +596,4 @@ export async function getCurrentEnvironmentVariables(agentTeamId: string, code:
588596export function getSortedObjectValues ( obj : Record < string , string > ) : string [ ] {
589597 const sortedKeys = Object . keys ( obj ) . sort ( ) ;
590598 return sortedKeys . map ( ( key ) => obj [ key ] ) ;
591- }
599+ }
0 commit comments