Skip to content

Commit 0245685

Browse files
authored
fix(ws): support resolutionContext on parse and run (#2544)
1 parent ce3ab1f commit 0245685

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/core/handlers/WebSocketHandler.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,20 @@ describe('parse', () => {
108108
},
109109
})
110110
})
111+
112+
it('supports a custom resolution context (base url)', () => {
113+
expect(
114+
new WebSocketHandler('/api/ws').parse({
115+
url: new URL('ws://localhost:3000/api/ws'),
116+
resolutionContext: {
117+
baseUrl: 'ws://localhost:3000/',
118+
},
119+
}),
120+
).toEqual({
121+
match: {
122+
matches: true,
123+
params: {},
124+
},
125+
})
126+
})
111127
})

src/core/handlers/WebSocketHandler.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export interface WebSocketHandlerConnection {
2929
params: PathParams
3030
}
3131

32+
export interface WebSocketResolutionContext {
33+
baseUrl?: string
34+
}
35+
3236
export const kEmitter = Symbol('kEmitter')
3337
export const kSender = Symbol('kSender')
3438
const kStopPropagationPatched = Symbol('kStopPropagationPatched')
@@ -50,7 +54,10 @@ export class WebSocketHandler {
5054
this.__kind = 'EventHandler'
5155
}
5256

53-
public parse(args: { url: URL }): WebSocketHandlerParsedResult {
57+
public parse(args: {
58+
url: URL
59+
resolutionContext?: WebSocketResolutionContext
60+
}): WebSocketHandlerParsedResult {
5461
const clientUrl = new URL(args.url)
5562

5663
/**
@@ -60,7 +67,11 @@ export class WebSocketHandler {
6067
*/
6168
clientUrl.pathname = clientUrl.pathname.replace(/^\/socket.io\//, '/')
6269

63-
const match = matchRequestUrl(clientUrl, this.url)
70+
const match = matchRequestUrl(
71+
clientUrl,
72+
this.url,
73+
args.resolutionContext?.baseUrl,
74+
)
6475

6576
return {
6677
match,
@@ -76,9 +87,11 @@ export class WebSocketHandler {
7687

7788
public async run(
7889
connection: Omit<WebSocketHandlerConnection, 'params'>,
90+
resolutionContext?: WebSocketResolutionContext,
7991
): Promise<boolean> {
8092
const parsedResult = this.parse({
8193
url: connection.client.url,
94+
resolutionContext,
8295
})
8396

8497
if (!this.predicate({ url: connection.client.url, parsedResult })) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy