1+ const Debug = require ( 'debug' )
2+
3+ class EndpointContext {
4+ constructor ( { party, endpoint, req, res} ) {
5+ this . party = party
6+ this . endpoint = endpoint
7+ this . MiddlewareConfig = endpoint . info . MiddlewareConfig
8+
9+ this . req = req
10+ this . res = res
11+ this . actor = null
12+ this . stats = {
13+ start : Date . now ( ) ,
14+ bytes_in : ! req ? null : JSON . stringify ( req . body ) . length
15+ }
16+ this . oauth_cloud = null
17+ this . session = null
18+ this . identity = null
19+ this . input = null
20+ this . input_session_id = null
21+ this . _debug = Debug ( 'dataparty.context.undefined' )
22+ this . _debugContent = [ ]
23+ }
24+
25+ setReq ( req ) { this . req = req }
26+ setRes ( res ) { this . res = res }
27+ setCloud ( cloud ) { this . cloud = cloud }
28+
29+ setSession ( session ) {
30+ this . session = session
31+ this . _debug = Debug ( 'dataparty.context.' + session . id )
32+ }
33+
34+ setOauthCloud ( oauth_cloud ) {
35+ this . oauth_cloud = oauth_cloud
36+ this . _debug = Debug ( 'oauth cloud' , oauth_cloud . id )
37+ }
38+
39+ setIdentity ( identity ) { this . identity = identity }
40+ setActor ( actor ) { this . actor = actor }
41+ setInputSession ( input_session_id ) { this . input_session_id = input_session_id }
42+
43+ /*async applyMiddleware(){
44+
45+ }*/
46+
47+ debug ( msg , ...args ) {
48+ let line = ( ( new Error ( ) . stack ) . split ( 'at ' ) [ 2 ] ) . trim ( )
49+
50+ const openParen = line . indexOf ( '(' ) + 1
51+ const closeParen = line . indexOf ( ')' )
52+
53+ const filePath = line . substring ( openParen , closeParen ) . replace ( __dirname , '' )
54+ line = filePath
55+
56+ const newMsg = line + ' ' + msg
57+
58+ this . _debugContent . push ( {
59+ file : filePath ,
60+ time : Date . now ( ) ,
61+ msg : msg + ' ' + args . map ( v => { return JSON . stringify ( v ) } ) . join ( ' ' )
62+ } )
63+
64+ this . _debug ( newMsg , ...args )
65+ }
66+ }
67+
68+ module . exports = EndpointContext
0 commit comments