Skip to content

Bump ws from 7.5.3 to 8.0.0 #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}