@@ -34,15 +34,19 @@ class Client {
3434 }
3535
3636 static ( ) {
37- const { req : { url } , res } = this ;
37+ const { req : { url, method } , res, ip } = this ;
3838 const filePath = url === '/' ? '/index.html' : url ;
3939 const fileExt = path . extname ( filePath ) . substring ( 1 ) ;
4040 const mimeType = MIME_TYPES [ fileExt ] || MIME_TYPES . html ;
4141 res . writeHead ( 200 , { ...HEADERS , 'Content-Type' : mimeType } ) ;
4242 if ( res . writableEnded ) return ;
4343 const data = application . static . get ( filePath ) ;
44- if ( data ) res . end ( data ) ;
45- else this . error ( 404 ) ;
44+ if ( data ) {
45+ res . end ( data ) ;
46+ application . logger . log ( `${ ip } \t${ method } \t${ url } ` ) ;
47+ return ;
48+ }
49+ this . error ( 404 ) ;
4650 }
4751
4852 redirect ( location ) {
@@ -52,38 +56,43 @@ class Client {
5256 res . end ( ) ;
5357 }
5458
55- error ( status , err , callId = err ) {
56- const { req : { url } , res, connection, ip } = this ;
57- const reason = http . STATUS_CODES [ status ] ;
59+ error ( code , err , callId = err ) {
60+ const { req : { url, method } , res, connection, ip } = this ;
61+ const status = http . STATUS_CODES [ code ] ;
5862 if ( typeof err === 'number' ) err = undefined ;
59- const error = err ? err . stack : reason ;
60- const msg = status === 403 ? err . message : `${ url } \t${ error } \t${ status } ` ;
61- application . logger . error ( `${ ip } \t${ msg } ` ) ;
62- const packet = { callback : callId , error : { message : reason } } ;
63- const result = JSON . stringify ( packet ) ;
63+ const reason = err ? err . stack : status ;
64+ application . logger . error ( `${ ip } \t${ method } \t${ url } \t${ code } \t${ reason } ` ) ;
6465 if ( connection ) {
65- connection . send ( result ) ;
66+ const packet = { callback : callId , error : { code, message : status } } ;
67+ connection . send ( JSON . stringify ( packet ) ) ;
6668 return ;
6769 }
6870 if ( res . writableEnded ) return ;
69- res . writeHead ( status , { 'Content-Type' : MIME_TYPES . json } ) ;
70- res . end ( result ) ;
71+ res . writeHead ( code , { 'Content-Type' : MIME_TYPES . json } ) ;
72+ const packet = { code, error : status } ;
73+ res . end ( JSON . stringify ( packet ) ) ;
7174 }
7275
7376 message ( data ) {
77+ let packet ;
7478 try {
75- const packet = JSON . parse ( data ) ;
76- const [ callType , methodName ] = Object . keys ( packet ) ;
77- const callId = packet [ callType ] ;
78- const args = packet [ methodName ] ;
79- this . rpc ( callId , methodName , args ) ;
79+ packet = JSON . parse ( data ) ;
8080 } catch ( err ) {
81- application . logger . error ( err . message ) ;
81+ this . error ( 500 , new Error ( 'JSON parsing error' ) ) ;
82+ return ;
83+ }
84+ const [ callType , methodName ] = Object . keys ( packet ) ;
85+ const callId = packet [ callType ] ;
86+ const args = packet [ methodName ] ;
87+ if ( callId && args ) {
88+ this . rpc ( callId , methodName , args ) ;
89+ return ;
8290 }
91+ this . error ( 500 , new Error ( 'Packet structure error' ) ) ;
8392 }
8493
8594 async rpc ( callId , method , args ) {
86- const { res, connection } = this ;
95+ const { res, connection, ip } = this ;
8796 const { semaphore } = application . server ;
8897 try {
8998 await semaphore . enter ( ) ;
@@ -92,25 +101,27 @@ class Client {
92101 return ;
93102 }
94103 try {
95- const session = await application . auth . restore ( this ) ;
104+ let session = await application . auth . restore ( this ) ;
96105 const proc = application . runMethod ( method , session ) ;
97106 if ( ! proc ) {
98107 this . error ( 404 , callId ) ;
99108 return ;
100109 }
101110 if ( ! session && proc . access !== 'public' ) {
102- this . error ( 403 , new Error ( `Forbidden: /api/ ${ method } ` ) , callId ) ;
111+ this . error ( 403 , callId ) ;
103112 return ;
104113 }
105114 const result = await proc . method ( args ) ;
106115 if ( ! session && proc . access === 'public' ) {
107- const session = application . auth . start ( this , result . userId ) ;
116+ session = application . auth . start ( this , result . userId ) ;
108117 result . token = session . token ;
109118 }
110119 const packet = { callback : callId , result } ;
111120 const data = JSON . stringify ( packet ) ;
112121 if ( connection ) connection . send ( data ) ;
113122 else res . end ( data ) ;
123+ const token = session ? session . token : 'anonymous' ;
124+ application . logger . log ( `${ ip } \t${ token } \t${ method } ` ) ;
114125 } catch ( err ) {
115126 this . error ( 500 , err , callId ) ;
116127 } finally {
0 commit comments