@@ -5,7 +5,7 @@ const streams = require('memory-streams');
55const { parseOutputStream, getFcReqHeaders } = require ( './http' ) ;
66const debug = require ( 'debug' ) ( 'fun:local' ) ;
77const { validateSignature, getHttpRawBody, generateInitRequestOpts, requestUntilServerUp, generateInvokeRequestOpts } = require ( './http' ) ;
8- const { red } = require ( 'colors' ) ;
8+ const { red, yellow } = require ( 'colors' ) ;
99const docker = require ( '../docker' ) ;
1010const dockerOpts = require ( '../docker-opts' ) ;
1111const uuid = require ( 'uuid' ) ;
@@ -30,29 +30,28 @@ class ApiInvoke extends Invoke {
3030 this . cmd = docker . generateDockerCmd ( this . runtime , false , {
3131 functionProps : this . functionProps ,
3232 httpMode : true ,
33- invokeInitializer,
34- event
33+ invokeInitializer
3534 } ) ;
3635
3736 const outputStream = new streams . WritableStream ( ) ;
3837 const errorStream = new streams . WritableStream ( ) ;
3938
4039 // check signature
4140 if ( ! await validateSignature ( req , res , req . method ) ) { return ; }
42- const opts = await dockerOpts . generateLocalStartOpts ( this . runtime ,
43- containerName ,
44- this . mounts ,
45- this . cmd ,
46- this . envs ,
47- {
48- debugPort : this . debugPort ,
49- dockerUser : this . dockerUser ,
50- debugIde : this . debugIde ,
51- imageName : this . imageName ,
52- caPort : this . functionProps . CAPort
53- } ) ;
5441 if ( isCustomContainerRuntime ( this . runtime ) ) {
55-
42+ const opts = await dockerOpts . generateLocalStartOpts ( this . runtime ,
43+ containerName ,
44+ this . mounts ,
45+ this . cmd ,
46+ this . envs ,
47+ {
48+ debugPort : this . debugPort ,
49+ dockerUser : this . dockerUser ,
50+ debugIde : this . debugIde ,
51+ imageName : this . imageName ,
52+ caPort : this . functionProps . CAPort
53+ }
54+ ) ;
5655 const containerRunner = await docker . runContainer ( opts , outputStream , errorStream , {
5756 serviceName : this . serviceName ,
5857 functionName : this . functionName
@@ -74,12 +73,20 @@ class ApiInvoke extends Invoke {
7473 const requestOpts = generateInvokeRequestOpts ( this . functionProps . CAPort , fcReqHeaders , event ) ;
7574
7675 const respOfCustomContainer = await requestUntilServerUp ( requestOpts , this . functionProps . Timeout || 3 ) ;
77- // console.log(respOfCustomContainer.body);
76+
7877 // exit container
7978 this . responseOfCustomContainer ( res , respOfCustomContainer ) ;
8079 await docker . exitContainer ( container ) ;
8180 } else {
8281
82+ const opts = await dockerOpts . generateLocalInvokeOpts ( this . runtime ,
83+ containerName ,
84+ this . mounts ,
85+ this . cmd ,
86+ this . debugPort ,
87+ this . envs ,
88+ this . dockerUser ,
89+ this . debugIde ) ;
8390 await docker . run ( opts ,
8491 event ,
8592 outputStream ,
@@ -97,6 +104,10 @@ class ApiInvoke extends Invoke {
97104 // responseApi
98105 response ( outputStream , errorStream , res ) {
99106 const errorResponse = errorStream . toString ( ) ;
107+ // 当容器的输出为空异常时
108+ if ( outputStream . toString ( ) === '' ) {
109+ console . log ( yellow ( 'Warning: outputStream of CA container is empty' ) ) ;
110+ }
100111
101112 let { statusCode, body, requestId, billedTime, memoryUsage } = parseOutputStream ( outputStream ) ;
102113
@@ -109,7 +120,12 @@ class ApiInvoke extends Invoke {
109120 'access-control-expose-headers' : 'Date,x-fc-request-id,x-fc-error-type,x-fc-code-checksum,x-fc-invocation-duration,x-fc-max-memory-usage,x-fc-log-result,x-fc-invocation-code-version'
110121 } ;
111122
112- res . status ( statusCode ) ;
123+ if ( statusCode ) {
124+ res . status ( statusCode ) ;
125+ } else {
126+ res . status ( 500 ) ;
127+ }
128+
113129
114130 // todo: fix body 后面多个换行的 bug
115131 if ( errorResponse ) { // process HandledInvocationError and UnhandledInvocationError
0 commit comments