Skip to content

Commit

Permalink
Bump ws from 7.5.3 to 8.0.0 (#171)
Browse files Browse the repository at this point in the history
* 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] <support@github.com>

* 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 <leonardo.rossi@gmail.com>
  • Loading branch information
dependabot[bot] and leorossi committed Aug 3, 2021
1 parent 76ff8ab commit 7c9c4f9
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 8 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'
const From = require('fastify-reply-from')
const WebSocket = require('ws')
const { convertUrlToWebSocket } = require('./utils')

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

function createWebSocketUrl (request) {
const source = new URL(request.url, 'http://127.0.0.1')
const source = new URL(request.url, 'ws://127.0.0.1')

const target = new URL(
source.pathname.replace(fastify.prefix, rewritePrefix),
options.upstream
convertUrlToWebSocket(options.upstream)
)

target.search = source.search
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
},
"dependencies": {
"fastify-reply-from": "^6.0.0",
"ws": "^7.4.1"
"ws": "^8.0.0"
},
"tsd": {
"directory": "test"
Expand Down
20 changes: 20 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

const t = require('tap')
const { convertUrlToWebSocket } = require('../utils')

t.test('convertUrlToWebSocket', function (t) {
const testData = [
{ input: 'http://localhost', expected: 'ws://localhost' },
{ input: 'https://localhost', expected: 'wss://localhost' },
{ input: 'ws://localhost', expected: 'ws://localhost' },
{ input: 'wss://localhost', expected: 'wss://localhost' },
{ input: 'wronghttp://localhost', expected: 'wronghttp://localhost' },
{ input: 'NOT_AN_URL', expected: 'NOT_AN_URL' }

]
t.plan(testData.length)
for (const { input, expected } of testData) {
t.equal(convertUrlToWebSocket(input), expected)
}
})
6 changes: 3 additions & 3 deletions test/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ test('basic websocket proxy', async (t) => {

const server = Fastify()
server.register(proxy, {
upstream: `http://localhost:${origin.address().port}`,
upstream: `ws://localhost:${origin.address().port}`,
websocket: true
})

await server.listen(0)
t.teardown(server.close.bind(server))

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

await once(ws, 'open')

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

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

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

Expand Down
4 changes: 3 additions & 1 deletion test/ws-prefix-rewrite-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const fastifyWebSocket = require('fastify-websocket')
const proxy = require('..')
const WebSocket = require('ws')
const got = require('got')
const { convertUrlToWebSocket } = require('../utils')

const level = 'warn'

Expand Down Expand Up @@ -35,7 +36,8 @@ async function proxyServer (t, backendURL, backendPath, proxyOptions, wrapperOpt
async function processRequest (t, frontendURL, path, expected) {
const url = new URL(path, frontendURL)
t.comment('ws connecting to ' + url.toString())
const ws = new WebSocket(url)
const wsUrl = convertUrlToWebSocket(url.href)
const ws = new WebSocket(wsUrl)
let wsResult, gotResult

try {
Expand Down
3 changes: 2 additions & 1 deletion test/ws-prefix-rewrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ async function proxyServer (t, backendURL, backendPath, proxyOptions, wrapperOpt
async function processRequest (t, frontendURL, path, expected) {
const url = new URL(path, frontendURL)
t.comment('ws connecting to ' + url.toString())
const ws = new WebSocket(url)
const wsUrl = url.href.replace('http:', 'ws:')
const ws = new WebSocket(wsUrl)
let wsResult, gotResult

try {
Expand Down
9 changes: 9 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'

function convertUrlToWebSocket (urlString) {
return urlString.replace(/^(http)(s)?:\/\//, 'ws$2://')
}

module.exports = {
convertUrlToWebSocket
}

0 comments on commit 7c9c4f9

Please sign in to comment.