Skip to content
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

WIP: Replace websocket-stream with ws #413

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"standard": "^14.3.1",
"tap": "^14.10.6",
"typescript": "^3.7.5",
"websocket-stream": "^5.5.0"
"ws": "^7.2.1"
},
"dependencies": {
"aedes-packet": "^2.1.0",
Expand Down
19 changes: 5 additions & 14 deletions test/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

const { test } = require('tap')
const http = require('http')
const ws = require('websocket-stream')
const mqtt = require('mqtt')
const mqttPacket = require('mqtt-packet')
const net = require('net')
const proxyProtocol = require('proxy-protocol-js')
const { protocolDecoder } = require('aedes-protocol-decoder')
const { setup, connect, delay } = require('./helper')
const { setup, connect, delay, createWebsocketServer } = require('./helper')
const aedes = require('../')

;[{ ver: 3, id: 'MQIsdp' }, { ver: 4, id: 'MQTT' }].forEach(function (ele) {
Expand Down Expand Up @@ -537,9 +536,7 @@ test('websocket clients have access to the request object', function (t) {
})

const server = http.createServer()
ws.createServer({
server: server
}, broker.handle)
createWebsocketServer(server, broker)

server.listen(port, function (err) {
t.error(err, 'no error')
Expand Down Expand Up @@ -796,9 +793,7 @@ test('websocket clients have access to the ipAddress from the socket (if no ip h
})

const server = http.createServer()
ws.createServer({
server: server
}, broker.handle)
createWebsocketServer(server, broker)

server.listen(port, function (err) {
t.error(err, 'no error')
Expand Down Expand Up @@ -833,9 +828,7 @@ test('websocket proxied clients have access to the ipAddress from x-real-ip head
})

const server = http.createServer()
ws.createServer({
server: server
}, broker.handle)
createWebsocketServer(server, broker)

server.listen(port, function (err) {
t.error(err, 'no error')
Expand Down Expand Up @@ -876,9 +869,7 @@ test('websocket proxied clients have access to the ipAddress from x-forwarded-fo
})

const server = http.createServer()
ws.createServer({
server: server
}, broker.handle)
createWebsocketServer(server, broker)

server.listen(port, function (err) {
t.error(err, 'no error')
Expand Down
15 changes: 14 additions & 1 deletion test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
const duplexify = require('duplexify')
const mqtt = require('mqtt-connection')
const through = require('through2')
const { createWebSocketStream, Server } = require('ws')
const util = require('util')

const aedes = require('../')

const parseStream = mqtt.parseStream
Expand Down Expand Up @@ -110,11 +112,22 @@ function subscribeMultiple (t, subscriber, subs, expectedGranted, done) {
})
}

function createWebsocketServer (server, broker) {
const ws = new Server({ server })

ws.on('connection', function (conn, req) {
var stream = createWebSocketStream(conn)
stream._socket = conn._socket
broker.handle(stream, req)
})
}

module.exports = {
setup: setup,
connect: connect,
noError: noError,
subscribe: subscribe,
subscribeMultiple: subscribeMultiple,
delay: util.promisify(setTimeout)
delay: util.promisify(setTimeout),
createWebsocketServer: createWebsocketServer
}