@@ -19,7 +19,13 @@ const send = (ws, msg) => {
19
19
}
20
20
} ;
21
21
22
- export function makeHTTPListener ( basedir , port , host , inboundCommand ) {
22
+ export function makeHTTPListener ( basedir , port , host , rawInboundCommand ) {
23
+ // Enrich the inbound command with some metadata.
24
+ const inboundCommand = ( body , { headers : { origin } = { } } = { } ) => {
25
+ const requestContext = { origin, date : Date . now ( ) } ;
26
+ return rawInboundCommand ( { ...body , requestContext } ) ;
27
+ } ;
28
+
23
29
const app = express ( ) ;
24
30
// HTTP logging.
25
31
app . use (
@@ -36,7 +42,9 @@ export function makeHTTPListener(basedir, port, host, inboundCommand) {
36
42
fs . statSync ( dapphtmldir ) ;
37
43
console . log ( `Serving Dapp files from ${ dapphtmldir } ` ) ;
38
44
app . use ( express . static ( dapphtmldir ) ) ;
39
- } catch ( e ) { }
45
+ } catch ( e ) {
46
+ // Do nothing.
47
+ }
40
48
41
49
// serve the static HTML for the UI
42
50
const htmldir = path . join ( basedir , 'html' ) ;
@@ -80,7 +88,7 @@ export function makeHTTPListener(basedir, port, host, inboundCommand) {
80
88
}
81
89
82
90
// console.log(`POST /vat got`, req.body); // should be jsonable
83
- inboundCommand ( req . body ) . then (
91
+ inboundCommand ( req . body , req ) . then (
84
92
r => res . json ( { ok : true , res : r } ) ,
85
93
rej => res . json ( { ok : false , rej } ) ,
86
94
) ;
@@ -124,23 +132,23 @@ export function makeHTTPListener(basedir, port, host, inboundCommand) {
124
132
console . log ( id , 'client closed' ) ;
125
133
broadcasts . delete ( ws ) ;
126
134
if ( req . url === '/captp' ) {
127
- inboundCommand ( { type : 'CTP_CLOSE' , connectionID } ) . catch ( _ => { } ) ;
135
+ inboundCommand ( { type : 'CTP_CLOSE' , connectionID } , req ) . catch ( _ => { } ) ;
128
136
points . delete ( connectionID ) ;
129
137
}
130
138
} ) ;
131
139
132
140
if ( req . url === '/captp' ) {
133
141
// This is a point-to-point connection, not broadcast.
134
142
points . set ( connectionID , ws ) ;
135
- inboundCommand ( { type : 'CTP_OPEN' , connectionID } ) . catch ( err => {
143
+ inboundCommand ( { type : 'CTP_OPEN' , connectionID } , req ) . catch ( err => {
136
144
console . log ( id , `error establishing connection` , err ) ;
137
145
} ) ;
138
146
ws . on ( 'message' , async message => {
139
147
try {
140
148
// some things use inbound messages
141
149
const obj = JSON . parse ( message ) ;
142
150
obj . connectionID = connectionID ;
143
- await inboundCommand ( obj ) ;
151
+ await inboundCommand ( obj , req ) ;
144
152
} catch ( e ) {
145
153
console . log ( id , 'client error' , e ) ;
146
154
const error = e . message || JSON . stringify ( e ) ;
0 commit comments