@@ -3,6 +3,7 @@ import { DeferredPromise } from '@open-draft/deferred-promise'
33import type { WebSocketData } from './WebSocketTransport'
44import { bindEvent } from './utils/bindEvent'
55import { CloseEvent } from './utils/events'
6+ import { resolveWebSocketUrl } from '../../utils/resolveWebSocketUrl'
67
78export 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- }
0 commit comments