Skip to content

Commit eecdb7c

Browse files
authored
fix: export resolveWebSocketUrl function (#764)
1 parent 9025a8c commit eecdb7c

File tree

3 files changed

+47
-46
lines changed

3 files changed

+47
-46
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export { getCleanUrl } from './utils/getCleanUrl'
1212
export { encodeBuffer, decodeBuffer } from './utils/bufferUtils'
1313
export { FetchResponse } from './utils/fetchUtils'
1414
export { getRawRequest } from './getRawRequest'
15+
export { resolveWebSocketUrl } from './utils/resolveWebSocketUrl'

src/interceptors/WebSocket/WebSocketOverride.ts

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { DeferredPromise } from '@open-draft/deferred-promise'
33
import type { WebSocketData } from './WebSocketTransport'
44
import { bindEvent } from './utils/bindEvent'
55
import { CloseEvent } from './utils/events'
6+
import { resolveWebSocketUrl } from '../../utils/resolveWebSocketUrl'
67

78
export type WebSocketEventListener<
89
EventType extends WebSocketEventMap[keyof WebSocketEventMap] = Event,
@@ -249,49 +250,3 @@ function getDataSize(data: WebSocketData): number {
249250

250251
return data.byteLength
251252
}
252-
253-
/**
254-
* Resolve potentially relative WebSocket URLs the same way
255-
* the browser does (replace the protocol, use the origin, etc).
256-
*
257-
* @see https://websockets.spec.whatwg.org//#dom-websocket-websocket
258-
*/
259-
function resolveWebSocketUrl(url: string | URL): string {
260-
if (typeof url === 'string') {
261-
/**
262-
* @note Cast the string to a URL first so the parsing errors
263-
* are thrown as a part of the WebSocket constructor, not consumers.
264-
*/
265-
const urlRecord = new URL(
266-
url,
267-
typeof location !== 'undefined' ? location.href : undefined
268-
)
269-
270-
return resolveWebSocketUrl(urlRecord)
271-
}
272-
273-
if (url.protocol === 'http:') {
274-
url.protocol = 'ws:'
275-
} else if (url.protocol === 'https:') {
276-
url.protocol = 'wss:'
277-
}
278-
279-
if (url.protocol !== 'ws:' && url.protocol !== 'wss:') {
280-
/**
281-
* @note These errors are modeled after the browser errors.
282-
* The exact error messages aren't provided in the specification.
283-
* Node.js uses more obscure error messages that I don't wish to replicate.
284-
*/
285-
throw new SyntaxError(
286-
`Failed to construct 'WebSocket': The URL's scheme must be either 'http', 'https', 'ws', or 'wss'. '${url.protocol}' is not allowed.`
287-
)
288-
}
289-
290-
if (url.hash !== '') {
291-
throw new SyntaxError(
292-
`Failed to construct 'WebSocket': The URL contains a fragment identifier ('${url.hash}'). Fragment identifiers are not allowed in WebSocket URLs.`
293-
)
294-
}
295-
296-
return url.href
297-
}

src/utils/resolveWebSocketUrl.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Resolve potentially relative WebSocket URLs the same way
3+
* the browser does (replace the protocol, use the origin, etc).
4+
*
5+
* @see https://websockets.spec.whatwg.org//#dom-websocket-websocket
6+
*/
7+
export function resolveWebSocketUrl(url: string | URL): string {
8+
if (typeof url === 'string') {
9+
/**
10+
* @note Cast the string to a URL first so the parsing errors
11+
* are thrown as a part of the WebSocket constructor, not consumers.
12+
*/
13+
const urlRecord = new URL(
14+
url,
15+
typeof location !== 'undefined' ? location.href : undefined
16+
)
17+
18+
return resolveWebSocketUrl(urlRecord)
19+
}
20+
21+
if (url.protocol === 'http:') {
22+
url.protocol = 'ws:'
23+
} else if (url.protocol === 'https:') {
24+
url.protocol = 'wss:'
25+
}
26+
27+
if (url.protocol !== 'ws:' && url.protocol !== 'wss:') {
28+
/**
29+
* @note These errors are modeled after the browser errors.
30+
* The exact error messages aren't provided in the specification.
31+
* Node.js uses more obscure error messages that I don't wish to replicate.
32+
*/
33+
throw new SyntaxError(
34+
`Failed to construct 'WebSocket': The URL's scheme must be either 'http', 'https', 'ws', or 'wss'. '${url.protocol}' is not allowed.`
35+
)
36+
}
37+
38+
if (url.hash !== '') {
39+
throw new SyntaxError(
40+
`Failed to construct 'WebSocket': The URL contains a fragment identifier ('${url.hash}'). Fragment identifiers are not allowed in WebSocket URLs.`
41+
)
42+
}
43+
44+
return url.href
45+
}

0 commit comments

Comments
 (0)