@@ -184,51 +184,55 @@ class Channel {
184184 }
185185
186186 async rpc ( callId , interfaceName , methodName , args ) {
187- const { res, connection, application, session, client } = this ;
188- const { semaphore } = application . server ;
187+ const { application, session, client } = this ;
188+ const [ iname , ver = '*' ] = interfaceName . split ( '.' ) ;
189+ const proc = application . getMethod ( iname , ver , methodName ) ;
190+ if ( ! proc ) {
191+ this . error ( 404 , null , callId ) ;
192+ return ;
193+ }
189194 try {
190- await semaphore . enter ( ) ;
195+ await proc . enter ( ) ;
191196 } catch {
192197 this . error ( 504 , null , callId ) ;
193198 return ;
194199 }
195- const [ iname , ver = '*' ] = interfaceName . split ( '.' ) ;
200+ const context = session ? session . context : { client } ;
201+ const publicMethod = proc . access === 'public' ;
202+ if ( ! this . session && ! publicMethod ) {
203+ this . error ( 403 , null , callId ) ;
204+ return ;
205+ }
206+ let result = null ;
196207 try {
197- const context = session ? session . context : { client } ;
198- const proc = application . getMethod ( iname , ver , methodName ) ;
199- if ( ! proc ) {
200- this . error ( 404 , null , callId ) ;
201- return ;
202- }
203- if ( ! this . session && proc . access !== 'public' ) {
204- this . error ( 403 , null , callId ) ;
205- return ;
206- }
207- const result = await application . invoke ( context , proc , args ) ;
208- if ( result instanceof Error ) {
209- this . error ( result . code , result , callId ) ;
210- return ;
211- }
212- const id = result ? result . systemUserId : 0 ;
213- if ( ! this . session && id && proc . access === 'public' ) {
214- this . session = application . auth . startSession ( this , id ) ;
215- result . token = this . token ;
216- }
217- const data = JSON . stringify ( { callback : callId , result } ) ;
218- if ( connection ) {
219- connection . send ( data ) ;
220- } else {
221- res . writeHead ( 200 , { 'Content-Type' : MIME_TYPES . json , ...HEADERS } ) ;
222- res . end ( data ) ;
223- }
224- const { ip, token } = this ;
225- const who = id > 0 ? id : token ;
226- const record = `${ ip } \t${ who } \t${ interfaceName } /${ methodName } ` ;
227- application . console . log ( record ) ;
208+ result = await proc . invoke ( context , args ) ;
228209 } catch ( err ) {
229210 this . error ( 500 , err , callId ) ;
230211 } finally {
231- semaphore . leave ( ) ;
212+ proc . leave ( ) ;
213+ }
214+ this . reply ( callId , result , publicMethod ) ;
215+ const record = `${ this . ip } \t${ interfaceName } /${ methodName } ` ;
216+ application . console . log ( record ) ;
217+ }
218+
219+ reply ( callId , result , publicMethod ) {
220+ const { res, connection, application } = this ;
221+ if ( result instanceof Error ) {
222+ this . error ( result . code , result , callId ) ;
223+ return ;
224+ }
225+ const id = result ? result . systemUserId : 0 ;
226+ if ( ! this . session && id && publicMethod ) {
227+ this . session = application . auth . startSession ( this , id ) ;
228+ result . token = this . token ;
229+ }
230+ const data = JSON . stringify ( { callback : callId , result } ) ;
231+ if ( connection ) {
232+ connection . send ( data ) ;
233+ } else {
234+ res . writeHead ( 200 , { 'Content-Type' : MIME_TYPES . json , ...HEADERS } ) ;
235+ res . end ( data ) ;
232236 }
233237 }
234238
0 commit comments