@@ -8,8 +8,8 @@ import {Router} from '../../../util/router';
88import { Printable } from '../../../util/print/types' ;
99import { printTree } from '../../../util/print/printTree' ;
1010import { PayloadTooLarge } from './errors' ;
11- import { findTokenInText } from './util' ;
12- import { Http1ConnectionContext } from './context' ;
11+ import { findTokenInText , setCodecs } from './util' ;
12+ import { Http1ConnectionContext , WsConnectionContext } from './context' ;
1313import { RpcCodecs } from '../../common/codec/RpcCodecs' ;
1414import { Codecs } from '../../../json-pack/codecs/Codecs' ;
1515import { RpcMessageCodecs } from '../../common/codec/RpcMessageCodecs' ;
@@ -43,9 +43,10 @@ export interface Http1EndpointDefinition {
4343
4444export interface WsEndpointDefinition {
4545 path : string ;
46- maxPayload ?: number ;
46+ maxIncomingMessage ?: number ;
47+ maxOutgoingBackpressure ?: number ;
4748 onUpgrade ?( req : http . IncomingMessage , connection : WsServerConnection ) : void ;
48- onConnect ( connection : WsServerConnection , req : http . IncomingMessage ) : void ;
49+ onConnect ( ctx : WsConnectionContext , req : http . IncomingMessage ) : void ;
4950}
5051
5152export interface Http1ServerOpts {
@@ -150,7 +151,7 @@ export class Http1Server implements Printable {
150151 ) ;
151152 const headers = req . headers ;
152153 const contentType = headers [ 'content-type' ] ;
153- if ( typeof contentType === 'string' ) ctx . setCodecs ( contentType , codecs ) ;
154+ if ( typeof contentType === 'string' ) setCodecs ( ctx , contentType , codecs ) ;
154155 const handler = match . data . handler ;
155156 await handler ( ctx ) ;
156157 } catch ( error ) {
@@ -165,7 +166,15 @@ export class Http1Server implements Printable {
165166 protected wsMatcher : RouteMatcher < WsEndpointDefinition > = ( ) => undefined ;
166167
167168 private readonly onWsUpgrade = ( req : http . IncomingMessage , socket : net . Socket , head : Buffer ) => {
168- const route = req . url || '' ;
169+ const url = req . url ?? '' ;
170+ const queryStartIndex = url . indexOf ( '?' ) ;
171+ let path = url ;
172+ let query = '' ;
173+ if ( queryStartIndex >= 0 ) {
174+ path = url . slice ( 0 , queryStartIndex ) ;
175+ query = url . slice ( queryStartIndex + 1 ) ;
176+ }
177+ const route = ( req . method || '' ) + path ;
169178 const match = this . wsMatcher ( route ) ;
170179 if ( ! match ) {
171180 socket . end ( ) ;
@@ -174,14 +183,37 @@ export class Http1Server implements Printable {
174183 const def = match . data ;
175184 const headers = req . headers ;
176185 const connection = new WsServerConnection ( this . wsEncoder , socket as net . Socket , head ) ;
186+ connection . maxIncomingMessage = def . maxIncomingMessage ?? 2 * 1024 * 1024 ;
187+ connection . maxBackpressure = def . maxOutgoingBackpressure ?? 2 * 1024 * 1024 ;
177188 if ( def . onUpgrade ) def . onUpgrade ( req , connection ) ;
178189 else {
179190 const secWebSocketKey = headers [ 'sec-websocket-key' ] ?? '' ;
180191 const secWebSocketProtocol = headers [ 'sec-websocket-protocol' ] ?? '' ;
181192 const secWebSocketExtensions = headers [ 'sec-websocket-extensions' ] ?? '' ;
182193 connection . upgrade ( secWebSocketKey , secWebSocketProtocol , secWebSocketExtensions ) ;
183194 }
184- def . onConnect ( connection , req ) ;
195+ const codecs = this . codecs ;
196+ const ip = this . findIp ( req ) ;
197+ const token = this . findToken ( req ) ;
198+ const ctx = new WsConnectionContext (
199+ connection ,
200+ path ,
201+ query ,
202+ ip ,
203+ token ,
204+ match . params ,
205+ new NullObject ( ) ,
206+ codecs . value . json ,
207+ codecs . value . json ,
208+ codecs . messages . compact ,
209+ ) ;
210+ const contentType = headers [ 'content-type' ] ;
211+ if ( typeof contentType === 'string' ) setCodecs ( ctx , contentType , codecs ) ;
212+ else {
213+ const secWebSocketProtocol = headers [ 'sec-websocket-protocol' ] ?? '' ;
214+ if ( typeof secWebSocketProtocol === 'string' ) setCodecs ( ctx , secWebSocketProtocol , codecs ) ;
215+ }
216+ def . onConnect ( ctx , req ) ;
185217 } ;
186218
187219 public ws ( def : WsEndpointDefinition ) : void {
@@ -209,23 +241,19 @@ export class Http1Server implements Printable {
209241 */
210242 public findToken ( req : http . IncomingMessage ) : string {
211243 let token : string = '' ;
212- let text : string = '' ;
213244 const headers = req . headers ;
214245 let header : string | string [ ] | undefined ;
215246 header = headers [ 'authorization' ] ;
216- text = typeof header === 'string' ? header : header ?. [ 0 ] ?? '' ;
217- if ( text ) token = findTokenInText ( text ) ;
247+ if ( typeof header === 'string' ) token = findTokenInText ( header ) ;
218248 if ( token ) return token ;
219- text = req . url || '' ;
220- if ( text ) token = findTokenInText ( text ) ;
249+ const url = req . url ;
250+ if ( typeof url === 'string' ) token = findTokenInText ( url ) ;
221251 if ( token ) return token ;
222252 header = headers [ 'cookie' ] ;
223- text = typeof header === 'string' ? header : header ?. [ 0 ] ?? '' ;
224- if ( text ) token = findTokenInText ( text ) ;
253+ if ( typeof header === 'string' ) token = findTokenInText ( header ) ;
225254 if ( token ) return token ;
226255 header = headers [ 'sec-websocket-protocol' ] ;
227- text = typeof header === 'string' ? header : header ?. [ 0 ] ?? '' ;
228- if ( text ) token = findTokenInText ( text ) ;
256+ if ( typeof header === 'string' ) token = findTokenInText ( header ) ;
229257 return token ;
230258 }
231259
0 commit comments