From e8714f15c3b5f5560a404237edaca08dd60996f9 Mon Sep 17 00:00:00 2001 From: Ayon Lee Date: Tue, 13 Oct 2020 21:15:44 +0800 Subject: [PATCH 1/5] add windows named pipe support for client --- lib/websocket.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/websocket.js b/lib/websocket.js index ce6f34caf..1f5434dea 100644 --- a/lib/websocket.js +++ b/lib/websocket.js @@ -466,10 +466,32 @@ 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); + let 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: "", + pathname, + search, + hash: "", + username: "", + password: "" + }; } else { parsedUrl = new URL(address); websocket.url = address; @@ -528,7 +550,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]; From 3d17a5310fb83a2e92ba3c0829d1499230916bc2 Mon Sep 17 00:00:00 2001 From: Ayon Lee Date: Tue, 13 Oct 2020 21:28:56 +0800 Subject: [PATCH 2/5] increase version number --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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", From 5b06cfc0d893d67a18e617fc1c712cb5ad11fb53 Mon Sep 17 00:00:00 2001 From: Ayon Lee Date: Tue, 13 Oct 2020 21:41:02 +0800 Subject: [PATCH 3/5] fix prettier errors --- lib/websocket.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/websocket.js b/lib/websocket.js index 1f5434dea..3454d3a15 100644 --- a/lib/websocket.js +++ b/lib/websocket.js @@ -471,10 +471,10 @@ function initAsClient(websocket, address, protocols, options) { if (address instanceof URL) { parsedUrl = address; websocket.url = address.href; - } else if (isWinPipe = /^ws\+unix\:\/\/\\\\[\.\?]\\pipe\\/.test(address)) { + } else if ((isWinPipe = /^ws\+unix:\/\/\\\\[.?]\\pipe\\/.test(address))) { let pathname = address.slice(10); - let index = pathname.indexOf("?", 9); - let search = ""; + const index = pathname.indexOf('?', 9); + let search = ''; if (index !== -1) { search = pathname.slice(index); @@ -482,15 +482,16 @@ function initAsClient(websocket, address, protocols, options) { } parsedUrl = { - protocol: "ws+unix:", - hostname: "", - port: "", - host: "", + protocol: 'ws+unix:', + hostname: '', + port: '', + host: '', + origin: '', pathname, search, - hash: "", - username: "", - password: "" + hash: '', + username: '', + password: '' }; } else { parsedUrl = new URL(address); @@ -551,10 +552,10 @@ function initAsClient(websocket, address, protocols, options) { } if (isWinPipe) { - const index = opts.path.indexOf(":", 11); + 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); + opts.path = index === -1 ? '' : opts.path.slice(index + 1); } else if (isUnixSocket) { const parts = opts.path.split(':'); From 6a030c8cadef0183e73c7fc9c89675196cdf6a00 Mon Sep 17 00:00:00 2001 From: Ayon Lee Date: Wed, 14 Oct 2020 01:32:02 +0800 Subject: [PATCH 4/5] refine test for windows named pipe --- test/websocket-server.test.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/test/websocket-server.test.js b/test/websocket-server.test.js index f41993f3f..55c769341 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( 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 }); From 520035eb02ca26c7d9a62d40a5d14f03fc4ff39b Mon Sep 17 00:00:00 2001 From: Ayon Lee Date: Wed, 14 Oct 2020 01:37:05 +0800 Subject: [PATCH 5/5] refine test for windows named pipe --- test/websocket-server.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/websocket-server.test.js b/test/websocket-server.test.js index 55c769341..99b91935d 100644 --- a/test/websocket-server.test.js +++ b/test/websocket-server.test.js @@ -116,7 +116,7 @@ describe('WebSocketServer', () => { it('uses a precreated http server listening on unix socket', function (done) { const server = http.createServer(); - const sockPath = path.join( + let sockPath = path.join( os.tmpdir(), `ws.${crypto.randomBytes(16).toString('hex')}.sock` );