@@ -11,60 +11,60 @@ const utils = require('./utils');
11
11
const handlerCache = { } ;
12
12
const messageCallbacks = { } ;
13
13
14
- function runPythonHandler ( funOptions , options ) {
15
- var spawn = require ( "child_process" ) . spawn ;
16
- return function ( event , context ) {
17
- var args = [ "invoke" , "local" , "-f" , funOptions . funName ]
18
- var stage = options . s || options . stage
19
- if ( stage )
20
- args = args . concat ( [ "-s" , stage ] )
21
-
22
- var process = spawn ( 'sls' , args ,
23
- { stdio : [ 'pipe' , 'pipe' , 'pipe' ] , shell : true , cwd : funOptions . servicePath } ) ;
24
- process . stdin . write ( JSON . stringify ( event ) + "\n" ) ;
25
- process . stdin . end ( ) ;
26
- let results = ''
27
- let hasDetectedJson = false ;
28
- process . stdout . on ( 'data' , ( data ) => {
29
- let str = data . toString ( 'utf8' ) ;
30
- if ( hasDetectedJson ) {
31
- // Assumes that all data after matching the start of the
32
- // JSON result is the rest of the context result.
33
- results = results + trimNewlines ( str ) ;
34
- } else {
35
- // Search for the start of the JSON result
36
- // https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format
37
- const match = / { [ \r \n ] \s + " i s B a s e 6 4 E n c o d e d " | { [ \r \n ] \s + " s t a t u s C o d e " | { [ \r \n ] \s + " h e a d e r s " | { [ \r \n ] \s + " b o d y " | { [ \r \n ] \s + " p r i n c i p a l I d " / . exec ( str ) ;
38
- if ( match && match . index > - 1 ) {
39
- // The JSON result was in this chunk so slice it out
40
- hasDetectedJson = true ;
41
- results = results + trimNewlines ( str . slice ( match . index ) ) ;
42
- str = str . slice ( 0 , match . index ) ;
43
- }
44
-
45
- if ( str . length > 0 ) {
46
- // The data does not look like JSON and we have not
47
- // detected the start of JSON, so write the
48
- // output to the console instead.
49
- console . log ( 'Python :' , '\x1b[34m' + str + '\x1b[0m' ) ;
50
- }
51
- }
52
- } ) ;
53
- process . stderr . on ( 'data' , ( data ) => {
54
- context . fail ( data ) ;
55
- } ) ;
56
- process . on ( 'close' , ( code ) => {
57
- if ( code == 0 ) {
58
- try {
59
- context . succeed ( JSON . parse ( results ) ) ;
60
- } catch ( ex ) {
61
- context . fail ( results ) ;
62
- }
63
- } else {
64
- context . succeed ( code , results ) ;
65
- }
66
- } ) ;
67
- }
14
+ function runProxyHandler ( funOptions , options ) {
15
+ var spawn = require ( "child_process" ) . spawn ;
16
+ return function ( event , context ) {
17
+ var args = [ "invoke" , "local" , "-f" , funOptions . funName ]
18
+ var stage = options . s || options . stage
19
+ if ( stage )
20
+ args = args . concat ( [ "-s" , stage ] )
21
+
22
+ var process = spawn ( 'sls' , args ,
23
+ { stdio : [ 'pipe' , 'pipe' , 'pipe' ] , shell : true , cwd : funOptions . servicePath } ) ;
24
+ process . stdin . write ( JSON . stringify ( event ) + "\n" ) ;
25
+ process . stdin . end ( ) ;
26
+ let results = ''
27
+ let hasDetectedJson = false ;
28
+ process . stdout . on ( 'data' , ( data ) => {
29
+ let str = data . toString ( 'utf8' ) ;
30
+ if ( hasDetectedJson ) {
31
+ // Assumes that all data after matching the start of the
32
+ // JSON result is the rest of the context result.
33
+ results = results + trimNewlines ( str ) ;
34
+ } else {
35
+ // Search for the start of the JSON result
36
+ // https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format
37
+ const match = / { [ \r \n ] ? \s * " i s B a s e 6 4 E n c o d e d " | { [ \r \n ] ? \s * " s t a t u s C o d e " | { [ \r \n ] ? \s * " h e a d e r s " | { [ \r \n ] ? \s * " b o d y " | { [ \r \n ] ? \s * " p r i n c i p a l I d " / . exec ( str ) ;
38
+ if ( match && match . index > - 1 ) {
39
+ // The JSON result was in this chunk so slice it out
40
+ hasDetectedJson = true ;
41
+ results = results + trimNewlines ( str . slice ( match . index ) ) ;
42
+ str = str . slice ( 0 , match . index ) ;
43
+ }
44
+
45
+ if ( str . length > 0 ) {
46
+ // The data does not look like JSON and we have not
47
+ // detected the start of JSON, so write the
48
+ // output to the console instead.
49
+ console . log ( 'Proxy Handler could not detect JSON :' , '\x1b[34m' + str + '\x1b[0m' ) ;
50
+ }
51
+ }
52
+ } ) ;
53
+ process . stderr . on ( 'data' , ( data ) => {
54
+ context . fail ( data ) ;
55
+ } ) ;
56
+ process . on ( 'close' , ( code ) => {
57
+ if ( code == 0 ) {
58
+ try {
59
+ context . succeed ( JSON . parse ( results ) ) ;
60
+ } catch ( ex ) {
61
+ context . fail ( results ) ;
62
+ }
63
+ } else {
64
+ context . succeed ( code , results ) ;
65
+ }
66
+ } ) ;
67
+ }
68
68
}
69
69
70
70
@@ -159,10 +159,10 @@ module.exports = {
159
159
if ( ! key . match ( 'node_modules' ) ) delete require . cache [ key ] ;
160
160
}
161
161
}
162
- let user_python = true
162
+
163
163
let handler = null ;
164
- if ( utils . isPythonRuntime ( funOptions [ 'serviceRuntime' ] ) ) {
165
- handler = runPythonHandler ( funOptions , options )
164
+ if ( utils . isProxyRuntime ( funOptions [ 'serviceRuntime' ] ) ) {
165
+ handler = runProxyHandler ( funOptions , options )
166
166
} else {
167
167
debugLog ( `Loading handler... (${ funOptions . handlerPath } )` ) ;
168
168
handler = require ( funOptions . handlerPath ) [ funOptions . handlerName ] ;
0 commit comments