1
1
import { enableCors } from './util' ;
2
2
import { Match , Router } from '../../../util/router' ;
3
- import { listToUint8 } from '../../../util/buffers/concat' ;
4
3
import { IncomingBatchMessage , RpcMessageBatchProcessor } from '../../common/rpc/RpcMessageBatchProcessor' ;
5
4
import { RpcError , RpcErrorCodes , RpcErrorType } from '../../common/rpc/caller/error' ;
6
5
import { ConnectionContext } from '../context' ;
@@ -11,15 +10,7 @@ import {RpcMessageFormat} from '../../common/codec/constants';
11
10
import { RpcCodecs } from '../../common/codec/RpcCodecs' ;
12
11
import { type ReactiveRpcMessage , RpcMessageStreamProcessor , ReactiveRpcClientMessage } from '../../common' ;
13
12
import type { Codecs } from '../../../json-pack/codecs/Codecs' ;
14
- import type {
15
- TemplatedApp ,
16
- HttpRequest ,
17
- HttpResponse ,
18
- HttpMethodPermissive ,
19
- JsonRouteHandler ,
20
- WebSocket ,
21
- RpcWebSocket ,
22
- } from './types' ;
13
+ import type * as types from './types' ;
23
14
import type { RouteHandler } from './types' ;
24
15
import type { RpcCaller } from '../../common/rpc/caller/RpcCaller' ;
25
16
import type { JsonValueCodec } from '../../../json-pack/codecs/types' ;
@@ -31,15 +22,16 @@ const ERR_NOT_FOUND = RpcError.fromCode(RpcErrorCodes.NOT_FOUND, 'Not Found');
31
22
const ERR_INTERNAL = RpcError . internal ( ) ;
32
23
33
24
export interface RpcAppOptions {
34
- uws : TemplatedApp ;
25
+ uws : types . TemplatedApp ;
35
26
maxRequestBodySize : number ;
36
27
codecs : Codecs ;
37
- caller : RpcCaller ;
28
+ caller : RpcCaller < any > ;
29
+ augmentContext : ( ctx : ConnectionContext ) => void ;
38
30
}
39
31
40
32
export class RpcApp < Ctx extends ConnectionContext > {
41
33
public readonly codecs : RpcCodecs ;
42
- protected readonly app : TemplatedApp ;
34
+ protected readonly app : types . TemplatedApp ;
43
35
protected readonly maxRequestBodySize : number ;
44
36
protected readonly router = new Router ( ) ;
45
37
protected readonly batchProcessor : RpcMessageBatchProcessor < Ctx > ;
@@ -55,12 +47,12 @@ export class RpcApp<Ctx extends ConnectionContext> {
55
47
enableCors ( this . options . uws ) ;
56
48
}
57
49
58
- public routeRaw ( method : HttpMethodPermissive , path : string , handler : RouteHandler < Ctx > ) : void {
59
- method = method . toLowerCase ( ) as HttpMethodPermissive ;
50
+ public routeRaw ( method : types . HttpMethodPermissive , path : string , handler : RouteHandler < Ctx > ) : void {
51
+ method = method . toLowerCase ( ) as types . HttpMethodPermissive ;
60
52
this . router . add ( method + path , handler ) ;
61
53
}
62
54
63
- public route ( method : HttpMethodPermissive , path : string , handler : JsonRouteHandler < Ctx > ) : void {
55
+ public route ( method : types . HttpMethodPermissive , path : string , handler : types . JsonRouteHandler < Ctx > ) : void {
64
56
this . routeRaw ( method , path , async ( ctx : Ctx ) => {
65
57
const result = await handler ( ctx ) ;
66
58
const res = ctx . res ! ;
@@ -112,6 +104,7 @@ export class RpcApp<Ctx extends ConnectionContext> {
112
104
113
105
public enableWsRpc ( path : string = '/rpc' ) : this {
114
106
const maxBackpressure = 4 * 1024 * 1024 ;
107
+ const augmentContext = this . options . augmentContext ;
115
108
this . app . ws ( path , {
116
109
idleTimeout : 0 ,
117
110
maxPayloadLength : 4 * 1024 * 1024 ,
@@ -120,11 +113,12 @@ export class RpcApp<Ctx extends ConnectionContext> {
120
113
const secWebSocketProtocol = req . getHeader ( 'sec-websocket-protocol' ) ;
121
114
const secWebSocketExtensions = req . getHeader ( 'sec-websocket-extensions' ) ;
122
115
const ctx = ConnectionContext . fromReqRes ( req , res , null , this ) ;
116
+ augmentContext ( ctx ) ;
123
117
/* This immediately calls open handler, you must not use res after this call */
124
118
res . upgrade ( { ctx} , secWebSocketKey , secWebSocketProtocol , secWebSocketExtensions , context ) ;
125
119
} ,
126
- open : ( ws_ : WebSocket ) => {
127
- const ws = ws_ as RpcWebSocket < Ctx > ;
120
+ open : ( ws_ : types . WebSocket ) => {
121
+ const ws = ws_ as types . RpcWebSocket < Ctx > ;
128
122
const ctx = ws . ctx ;
129
123
const resCodec = ctx . resCodec ;
130
124
const msgCodec = ctx . msgCodec ;
@@ -144,8 +138,8 @@ export class RpcApp<Ctx extends ConnectionContext> {
144
138
bufferTime : 0 ,
145
139
} ) ;
146
140
} ,
147
- message : ( ws_ : WebSocket , buf : ArrayBuffer , isBinary : boolean ) => {
148
- const ws = ws_ as RpcWebSocket < Ctx > ;
141
+ message : ( ws_ : types . WebSocket , buf : ArrayBuffer , isBinary : boolean ) => {
142
+ const ws = ws_ as types . RpcWebSocket < Ctx > ;
149
143
const ctx = ws . ctx ;
150
144
const reqCodec = ctx . reqCodec ;
151
145
const msgCodec = ctx . msgCodec ;
@@ -158,8 +152,8 @@ export class RpcApp<Ctx extends ConnectionContext> {
158
152
rpc . sendNotification ( '.err' , RpcError . value ( RpcError . invalidRequest ( ) ) ) ;
159
153
}
160
154
} ,
161
- close : ( ws_ : WebSocket , code : number , message : ArrayBuffer ) => {
162
- const ws = ws_ as RpcWebSocket < Ctx > ;
155
+ close : ( ws_ : types . WebSocket , code : number , message : ArrayBuffer ) => {
156
+ const ws = ws_ as types . RpcWebSocket < Ctx > ;
163
157
ws . rpc ! . stop ( ) ;
164
158
} ,
165
159
} ) ;
@@ -170,7 +164,8 @@ export class RpcApp<Ctx extends ConnectionContext> {
170
164
const matcher = this . router . compile ( ) ;
171
165
const codecs = this . codecs ;
172
166
let responseCodec : JsonValueCodec = codecs . value . json ;
173
- this . app . any ( '/*' , async ( res : HttpResponse , req : HttpRequest ) => {
167
+ const augmentContext = this . options . augmentContext ;
168
+ this . app . any ( '/*' , async ( res : types . HttpResponse , req : types . HttpRequest ) => {
174
169
res . onAborted ( ( ) => {
175
170
res . aborted = true ;
176
171
} ) ;
@@ -189,6 +184,7 @@ export class RpcApp<Ctx extends ConnectionContext> {
189
184
const params = match . params ;
190
185
const ctx = ConnectionContext . fromReqRes ( req , res , params , this ) as Ctx ;
191
186
responseCodec = ctx . resCodec ;
187
+ augmentContext ( ctx ) ;
192
188
await handler ( ctx ) ;
193
189
} catch ( err ) {
194
190
if ( err instanceof RpcError ) {
0 commit comments