Skip to content

Commit

Permalink
feat: added WebSocket support
Browse files Browse the repository at this point in the history
  • Loading branch information
bergos committed Mar 18, 2024
1 parent df78923 commit 5296c97
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
19 changes: 12 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { createServer } from 'node:http'
import { promisify } from 'node:util'
import express from 'express'
import fetch from 'node-fetch'
import { WebSocketServer } from 'ws'

class ExpressAsPromise {
constructor () {
this.app = express()
this.server = null
this.server = createServer(this.app)
this.wss = new WebSocketServer({ server: this.server })
}

async listen (port, host) {
await (new Promise((resolve, reject) => {
this.server = this.app.listen(port, host, err => {
this.server.listen(port, host, err => {
if (err) {
return reject(err)
}
Expand All @@ -23,13 +26,15 @@ class ExpressAsPromise {
}

async stop () {
await promisify(this.server.close.bind(this.server))()
if (!this.server.listening) {
return
}

this.server = null
await promisify(this.server.close.bind(this.server))()
}

async fetch (pathname, options) {
if (!this.server) {
if (!this.server.listening) {
await this.listen()
}

Expand All @@ -46,7 +51,7 @@ class ExpressAsPromise {
}

if (address.family === 'IPv6' && address.address !== '::') {
return address.address
return `[${address.address}]`
}

return 'localhost'
Expand All @@ -57,7 +62,7 @@ class ExpressAsPromise {
}

get url () {
return `http://${this.host}${this.port !== 80 ? `:${this.port}` : ''}/`
return new URL(`http://${this.host}${this.port !== 80 ? `:${this.port}` : ''}/`)
}
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"homepage": "https://github.com/bergos/express-as-promise",
"dependencies": {
"express": "^4.18.3",
"node-fetch": "^3.3.2"
"node-fetch": "^3.3.2",
"ws": "^8.16.0"
},
"devDependencies": {
"c8": "^9.1.0",
Expand Down
56 changes: 49 additions & 7 deletions test/ExpressAsPromise.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { notStrictEqual, strictEqual } from 'node:assert'
import { match, notStrictEqual, strictEqual } from 'node:assert'
import { describe, it } from 'mocha'
import fetch from 'node-fetch'
import { WebSocket, WebSocketServer } from 'ws'
import ExpressAsPromise from '../index.js'

describe('ExpressAsPromise', () => {
Expand All @@ -27,6 +28,45 @@ describe('ExpressAsPromise', () => {
})
})

describe('.wss', () => {
it('should be a WebSocketServer object', async () => {
const server = new ExpressAsPromise()

server.app.get('/', (req, res) => {
res.send('Hello World!')
})

await server.listen()

strictEqual(server.wss instanceof WebSocketServer, true)

await server.stop()
})

it('should be bound to the server socket', async () => {
const server = new ExpressAsPromise()

server.wss.on('connection', ws => {
ws.send('test')
})

const url = await server.listen()
const socket = new WebSocket(`ws://${url.host}/`)

await new Promise(resolve => {
socket.on('message', message => {
strictEqual(message.toString(), 'test')

socket.close()

resolve()
})
})

await server.stop()
})
})

describe('.fetch', () => {
it('should be a method', () => {
const server = new ExpressAsPromise()
Expand Down Expand Up @@ -96,12 +136,13 @@ describe('ExpressAsPromise', () => {
await server.stop()
})

it('should return the base URL', async () => {
it('should return the base URL as URL object', async () => {
const server = new ExpressAsPromise()

const baseUrl = await server.listen(12345, 'localhost')

strictEqual(baseUrl, 'http://127.0.0.1:12345/')
strictEqual(baseUrl instanceof URL, true)
match(baseUrl.toString(), /http:\/\/(127.0.0.1|\[::1\]):12345\//)

await server.stop()
})
Expand All @@ -120,17 +161,18 @@ describe('ExpressAsPromise', () => {
await server.listen()
await server.stop()

strictEqual(server.server, null)
strictEqual(server.server.listening, false)
})
})

describe('.url', () => {
it('should be a readable property to get the base URL', async () => {
it('should be a readable URL property to get the base URL', async () => {
const server = new ExpressAsPromise()

await server.listen(12346, 'localhost')

strictEqual(server.url, 'http://127.0.0.1:12346/')
strictEqual(server.url instanceof URL, true)
match(server.url.toString(), /http:\/\/(127.0.0.1|\[::1\]):12346\//)

await server.stop()
})
Expand All @@ -146,7 +188,7 @@ describe('ExpressAsPromise', () => {

await server.listen(12347, 'localhost')

strictEqual(server.url, 'http://127.0.0.1/')
match(server.url.toString(), /http:\/\/(127.0.0.1|\[::1\])\//)

await server.stop()
})
Expand Down
2 changes: 1 addition & 1 deletion test/withServer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ describe('withServer', () => {
await instance.listen()
})

strictEqual(instance.server, null)
strictEqual(instance.server.listening, false)
})
})

0 comments on commit 5296c97

Please sign in to comment.