diff --git a/lib/websocket.js b/lib/websocket.js index ce6f34caf..3454d3a15 100644 --- a/lib/websocket.js +++ b/lib/websocket.js @@ -466,10 +466,33 @@ function initAsClient(websocket, address, protocols, options) { } let parsedUrl; + let isWinPipe; if (address instanceof URL) { parsedUrl = address; websocket.url = address.href; + } else if ((isWinPipe = /^ws\+unix:\/\/\\\\[.?]\\pipe\\/.test(address))) { + let pathname = address.slice(10); + const index = pathname.indexOf('?', 9); + let search = ''; + + if (index !== -1) { + search = pathname.slice(index); + pathname = pathname.slice(0, index); + } + + parsedUrl = { + protocol: 'ws+unix:', + hostname: '', + port: '', + host: '', + origin: '', + pathname, + search, + hash: '', + username: '', + password: '' + }; } else { parsedUrl = new URL(address); websocket.url = address; @@ -528,7 +551,12 @@ function initAsClient(websocket, address, protocols, options) { opts.auth = `${parsedUrl.username}:${parsedUrl.password}`; } - if (isUnixSocket) { + if (isWinPipe) { + const index = opts.path.indexOf(':', 11); + + opts.socketPath = index === -1 ? opts.path : opts.path.slice(0, index); + opts.path = index === -1 ? '' : opts.path.slice(index + 1); + } else if (isUnixSocket) { const parts = opts.path.split(':'); opts.socketPath = parts[0]; diff --git a/package.json b/package.json index f45e9d347..4ba329d31 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ws", - "version": "7.3.1", + "version": "7.3.2", "description": "Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js", "keywords": [ "HyBi", diff --git a/test/websocket-server.test.js b/test/websocket-server.test.js index f41993f3f..99b91935d 100644 --- a/test/websocket-server.test.js +++ b/test/websocket-server.test.js @@ -115,20 +115,16 @@ describe('WebSocketServer', () => { }); it('uses a precreated http server listening on unix socket', function (done) { - // - // Skip this test on Windows. The URL parser: - // - // - Throws an error if the named pipe uses backward slashes. - // - Incorrectly parses the path if the named pipe uses forward slashes. - // - if (process.platform === 'win32') return this.skip(); - const server = http.createServer(); - const sockPath = path.join( + let sockPath = path.join( os.tmpdir(), `ws.${crypto.randomBytes(16).toString('hex')}.sock` ); + if (process.platform === 'win32') { + sockPath = '\\\\?\\pipe\\' + sockPath; + } + server.listen(sockPath, () => { const wss = new WebSocket.Server({ server });