import url from 'url' ; import { HMR_SOCK_PATH } from '@modern-js/utils/constants' ; type ParsedSearch = { host : string ; port : string ; path : string ; protocol ? : string ; } ; export function createSocketUrl ( resourceQuery ) { const searchParams = resourceQuery . substr ( 1 ) . split ( '&' ) ; const options = { } ; for ( const pair of searchParams ) { const ary = pair . split ( '=' ) ; options [ ary [ 0 ] ] = decodeURIComponent ( ary [ 1 ] ) ; } const currentLocation = self . location ; return getSocketUrl ( options as ParsedSearch , currentLocation ) ; } function getSocketUrl ( urlParts , location ) { const { host , port , path , protocol } = urlParts ; return url . format ( { protocol : protocol || location . protocol === 'https:' ? 'wss' : 'ws' , hostname : host || location . hostname , port : port || location . port , pathname : path || HMR_SOCK_PATH , slashes : true , } ) ; }