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 = actor
12+ this . stats = {
13+ start : Date . now ( ) ,
14+ bytes_in : ! req ? null : JSON . stringify ( req . body ) . length
15+ }
16+ this . oauth_cloud = oauth_cloud
17+ this . session = session
18+ this . identity = identity
19+ this . input = input
20+ this . input_session_id = input_session_id
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+ applyMiddleware
44+
45+ debug ( msg , ...args ) {
46+ let line = ( ( new Error ( ) . stack ) . split ( 'at ' ) [ 2 ] ) . trim ( )
47+
48+ const openParen = line . indexOf ( '(' ) + 1
49+ const closeParen = line . indexOf ( ')' )
50+
51+ const filePath = line . substring ( openParen , closeParen ) . replace ( __dirname , '' )
52+ line = filePath
53+
54+ const newMsg = line + ' ' + msg
55+
56+ this . _debugContent . push ( {
57+ file : filePath ,
58+ time : Date . now ( ) ,
59+ msg : msg + ' ' + args . map ( v => { return JSON . stringify ( v ) } ) . join ( ' ' )
60+ } )
61+
62+ this . _debug ( newMsg , ...args )
63+ }
64+ }
65+
66+ module . exports = EndpointContext
0 commit comments