@@ -16,14 +16,17 @@ const fs = require('fs');
1616const http = require ( 'http' ) ;
1717const url = require ( 'url' ) ;
1818
19+ const SERVER_PORT = 3000 ;
20+
1921// Process CLI options.
2022const program = new Command ( ) ;
2123
2224program
2325 . usage ( '[options]' )
2426
2527 . option ( '--handler <path>' , 'Lambda@Edge handler script.' )
26- . option ( '--port <number>' , 'HTTP server port number.' , 3000 )
28+ . option ( '--port <number>' , 'HTTP server port number.' , SERVER_PORT )
29+ . option ( '--debug <boolean>' , 'Log event to STDOUT.' , false )
2730
2831 . action ( function ( opts ) {
2932 const errors = [ ] ;
@@ -67,7 +70,7 @@ program
6770if ( process . env . NODE_ENV === 'test' ) {
6871 const { handler} = require ( process . env . TEST_HANDLER ) ;
6972
70- module . exports = initServer ( handler , 3000 ) ;
73+ module . exports = initServer ( handler , SERVER_PORT ) ;
7174} else {
7275 program . parse ( ) ;
7376}
@@ -87,13 +90,16 @@ if (process.env.NODE_ENV === 'test') {
8790 */
8891function initServer ( handler , port ) {
8992 return http . createServer ( function ( req , res ) {
90- let body ;
93+ let body = '' ;
9194
9295 req . on ( 'data' , function ( data ) {
9396 body += data ;
9497 } ) ;
9598
9699 req . on ( 'end' , function ( ) {
100+ const path = url . parse ( req . url ) . pathname ;
101+ const query = url . parse ( req . url ) . query ;
102+
97103 if ( body ) {
98104 body = encodeBody ( body ) ;
99105 }
@@ -107,8 +113,8 @@ function initServer(handler, port) {
107113 clientIp : res . socket . remoteAddress ,
108114 headers : formatHeaders ( req . headers ) . toEdge ( ) ,
109115 method : req . method ,
110- querystring : url . parse ( req . url ) . query ,
111- uri : url . parse ( req . url ) . pathname ,
116+ querystring : query ,
117+ uri : path ,
112118 body : {
113119 data : body
114120 }
@@ -154,6 +160,9 @@ function initServer(handler, port) {
154160 // Synchronous handling.
155161 handler ( event , null , callback ) ;
156162 }
163+
164+ console . log ( req . method , path , JSON . stringify ( event ) ) ;
165+
157166 } catch ( err ) {
158167 this . emit ( 'error' , Error ( 'Malformed handler method. Exiting..' ) ) ;
159168 }
0 commit comments