Skip to content

Commit 7c9c4f9

Browse files
Bump ws from 7.5.3 to 8.0.0 (#171)
* Bump ws from 7.5.3 to 8.0.0 Bumps [ws](https://github.com/websockets/ws) from 7.5.3 to 8.0.0. - [Release notes](https://github.com/websockets/ws/releases) - [Commits](websockets/ws@7.5.3...8.0.0) --- updated-dependencies: - dependency-name: ws dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <[email protected]> * force `ws://` protocol when instantiating a new `WebSocket` (#172) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Leonardo Rossi <[email protected]>
1 parent 76ff8ab commit 7c9c4f9

File tree

7 files changed

+41
-8
lines changed

7 files changed

+41
-8
lines changed

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22
const From = require('fastify-reply-from')
33
const WebSocket = require('ws')
4+
const { convertUrlToWebSocket } = require('./utils')
45

56
const httpMethods = ['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']
67
const urlPattern = /^https?:\/\//
@@ -98,11 +99,11 @@ function setupWebSocketProxy (fastify, options, rewritePrefix) {
9899
})
99100

100101
function createWebSocketUrl (request) {
101-
const source = new URL(request.url, 'http://127.0.0.1')
102+
const source = new URL(request.url, 'ws://127.0.0.1')
102103

103104
const target = new URL(
104105
source.pathname.replace(fastify.prefix, rewritePrefix),
105-
options.upstream
106+
convertUrlToWebSocket(options.upstream)
106107
)
107108

108109
target.search = source.search

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
},
5050
"dependencies": {
5151
"fastify-reply-from": "^6.0.0",
52-
"ws": "^7.4.1"
52+
"ws": "^8.0.0"
5353
},
5454
"tsd": {
5555
"directory": "test"

test/utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
3+
const t = require('tap')
4+
const { convertUrlToWebSocket } = require('../utils')
5+
6+
t.test('convertUrlToWebSocket', function (t) {
7+
const testData = [
8+
{ input: 'http://localhost', expected: 'ws://localhost' },
9+
{ input: 'https://localhost', expected: 'wss://localhost' },
10+
{ input: 'ws://localhost', expected: 'ws://localhost' },
11+
{ input: 'wss://localhost', expected: 'wss://localhost' },
12+
{ input: 'wronghttp://localhost', expected: 'wronghttp://localhost' },
13+
{ input: 'NOT_AN_URL', expected: 'NOT_AN_URL' }
14+
15+
]
16+
t.plan(testData.length)
17+
for (const { input, expected } of testData) {
18+
t.equal(convertUrlToWebSocket(input), expected)
19+
}
20+
})

test/websocket.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ test('basic websocket proxy', async (t) => {
2828

2929
const server = Fastify()
3030
server.register(proxy, {
31-
upstream: `http://localhost:${origin.address().port}`,
31+
upstream: `ws://localhost:${origin.address().port}`,
3232
websocket: true
3333
})
3434

3535
await server.listen(0)
3636
t.teardown(server.close.bind(server))
3737

38-
const ws = new WebSocket(`http://localhost:${server.server.address().port}`)
38+
const ws = new WebSocket(`ws://localhost:${server.server.address().port}`)
3939

4040
await once(ws, 'open')
4141

@@ -58,7 +58,7 @@ test('captures errors on start', async (t) => {
5858
await app.listen(0)
5959

6060
const app2 = Fastify()
61-
app2.register(proxy, { upstream: 'http://localhost', websocket: true })
61+
app2.register(proxy, { upstream: 'ws://localhost', websocket: true })
6262

6363
const appPort = app.server.address().port
6464

test/ws-prefix-rewrite-core.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const fastifyWebSocket = require('fastify-websocket')
88
const proxy = require('..')
99
const WebSocket = require('ws')
1010
const got = require('got')
11+
const { convertUrlToWebSocket } = require('../utils')
1112

1213
const level = 'warn'
1314

@@ -35,7 +36,8 @@ async function proxyServer (t, backendURL, backendPath, proxyOptions, wrapperOpt
3536
async function processRequest (t, frontendURL, path, expected) {
3637
const url = new URL(path, frontendURL)
3738
t.comment('ws connecting to ' + url.toString())
38-
const ws = new WebSocket(url)
39+
const wsUrl = convertUrlToWebSocket(url.href)
40+
const ws = new WebSocket(wsUrl)
3941
let wsResult, gotResult
4042

4143
try {

test/ws-prefix-rewrite.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ async function proxyServer (t, backendURL, backendPath, proxyOptions, wrapperOpt
3434
async function processRequest (t, frontendURL, path, expected) {
3535
const url = new URL(path, frontendURL)
3636
t.comment('ws connecting to ' + url.toString())
37-
const ws = new WebSocket(url)
37+
const wsUrl = url.href.replace('http:', 'ws:')
38+
const ws = new WebSocket(wsUrl)
3839
let wsResult, gotResult
3940

4041
try {

utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict'
2+
3+
function convertUrlToWebSocket (urlString) {
4+
return urlString.replace(/^(http)(s)?:\/\//, 'ws$2://')
5+
}
6+
7+
module.exports = {
8+
convertUrlToWebSocket
9+
}

0 commit comments

Comments
 (0)