@@ -2,11 +2,10 @@ import express, { NextFunction, Request, Response } from 'express';
22// eslint-disable-next-line @typescript-eslint/ban-ts-comment
33// @ts -ignore
44import queue from 'express-queue' ;
5- import { promisify } from 'util' ;
6- import { exec } from 'child_process' ;
75import { APIGatewayProxyStructuredResultV2 } from 'aws-lambda' ;
86import { InvocationType , InvokeCommand , InvokeCommandOutput , LambdaClient } from '@aws-sdk/client-lambda' ;
97import { httpRequestToEvent } from './apiGateway.js' ;
8+ import { runDockerCommand } from "./docker" ;
109import bodyParser from 'body-parser' ;
1110
1211const app = express ( ) ;
@@ -30,7 +29,7 @@ const doDebugLogging = process.env.LOG_LEVEL === 'debug';
3029
3130// Determine whether to use Docker CLI or AWS Lambda RIE for execution
3231const dockerHost = process . env . TARGET_CONTAINER ?? target . split ( ":" ) [ 0 ] ;
33- const dockerHandler = process . env . TARGET_HANDLER ;
32+ const dockerHandler = process . env . TARGET_HANDLER ?? '' ;
3433const mode = ( dockerHost && dockerHandler ) ? "docker" : "rie" ;
3534if ( doInfoLogging ) {
3635 if ( mode === "docker" ) {
@@ -39,7 +38,6 @@ if (doInfoLogging) {
3938 console . log ( "Using AWS Lambda RIE environment - set TARGET_CONTAINER and TARGET_HANDLER environment variables to enable docker CLI mode" ) ;
4039 }
4140}
42- const isBrefLocalHandler = dockerHandler ?. includes ( 'bref-local' ) ?? false ;
4341const maxParallelRequests = process . env . DEV_MAX_REQUESTS_IN_PARALLEL ?? 10 ;
4442const maxQueuedRequests = process . env . DEV_MAX_REQUESTS_IN_QUEUE ?? - 1 ;
4543
@@ -62,9 +60,6 @@ const requestQueue = queue({
6260} ) ;
6361app . use ( requestQueue ) ;
6462
65- // Create an async version of exec() for calling Docker
66- const asyncExec = promisify ( exec ) ;
67-
6863const client = new LambdaClient ( {
6964 region : 'us-east-1' ,
7065 endpoint : `http://${ target } ` ,
@@ -92,47 +87,23 @@ app.all('*', async (req: Request, res: Response, next) => {
9287 try {
9388 const payload = Buffer . from ( JSON . stringify ( event ) ) . toString ( ) ;
9489 if ( doInfoLogging ) {
95- console . log ( `START [${ mode . toUpperCase ( ) } ] ${ requestContext ?. method } ${ requestContext ?. path } ` , doDebugLogging ? payload : null ) ;
90+ console . log ( `START [${ mode . toUpperCase ( ) } ] ${ requestContext ?. method } ${ requestContext ?. path } ` , doDebugLogging ? payload : '' ) ;
9691 }
97-
9892 if ( mode === "docker" ) {
99- const payloadAsEscapedJson = payload . replace ( "'" , "\\'" ) ;
100- const dockerCommand = `/usr/bin/docker exec ${ dockerHost } ${ dockerHandler } '${ payloadAsEscapedJson } '` ;
101- const { stdout, stderr} = await asyncExec ( dockerCommand ) ;
102- result = Buffer . from ( stdout ) . toString ( ) ;
103-
104- if ( isBrefLocalHandler ) {
105- // The 'bref-local' handler returns the following, which needs to be stripped:
106- // START
107- // END Duration XXXXX
108- // (blank line)
109- // ...real output...
110- result = result
111- . split ( '\n' ) // Split the output into lines
112- . slice ( 3 ) // Skip the first three lines
113- . join ( '\n' ) ; // Join the remaining lines back together
114- }
115- if ( doInfoLogging ) {
116- console . log ( `END [DOCKER] ${ requestContext ?. method } ${ requestContext ?. path } ` , doDebugLogging ? result : null ) ;
117- if ( doDebugLogging ) {
118- console . log ( `END [DOCKER] CMD ` , dockerCommand ) ;
119- console . log ( `END [DOCKER] STDOUT` , stdout ) ;
120- if ( stderr ) {
121- console . error ( `END [DOCKER] STDERR: ${ stderr } ` ) ;
122- }
123- }
124- }
93+ // Run via Docker
94+ result = await runDockerCommand ( dockerHost , dockerHandler , payload ) ;
12595 } else {
96+ // Run via Lambda RIE SDK
12697 const invokeCommand = new InvokeCommand ( {
12798 FunctionName : 'function' ,
12899 Payload : payload ,
129100 InvocationType : InvocationType . RequestResponse ,
130101 } ) ;
131102 const invokeResponse : InvokeCommandOutput = await client . send ( invokeCommand ) ;
132103 result = String ( invokeResponse . Payload ) ;
133- if ( doDebugLogging ) {
134- console . log ( `END [RIE] ${ requestContext ?. method } ${ requestContext ?. path } ` , doDebugLogging ? result : null ) ;
135- }
104+ }
105+ if ( doInfoLogging ) {
106+ console . log ( `END [ ${ mode . toUpperCase ( ) } ] ${ requestContext ?. method } ${ requestContext ?. path } ` , doDebugLogging ? result : ` ${ result . length } bytes` ) ;
136107 }
137108
138109 } catch ( e ) {
0 commit comments