Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Commit

Permalink
feat: pass through middleware config, noDefaultRoute option
Browse files Browse the repository at this point in the history
  • Loading branch information
abetaev committed Dec 13, 2022
1 parent a686e42 commit d7e23f9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
18 changes: 12 additions & 6 deletions packages/webrtc-star-signalling-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Server } from '@hapi/hapi'
import type { ServerOptions } from '@hapi/hapi'
import Inert from '@hapi/inert'

import { config } from './config.js'
Expand All @@ -19,6 +20,8 @@ interface Options {
host?: string
metrics?: boolean
refreshPeerListIntervalMS?: number
noDefaultRoute?: boolean
hapi?: ServerOptions
}

export interface SigServer extends Server {
Expand All @@ -33,6 +36,7 @@ export async function sigServer (options: Options = {}) {

const http: SigServer = Object.assign(new Server({
...config.hapi.options,
...options.hapi ?? {},
port,
host
}), {
Expand All @@ -57,13 +61,15 @@ export async function sigServer (options: Options = {}) {

log('signaling server has started on: ' + http.info.uri)

http.route({
method: 'GET',
path: '/',
handler: (request, reply) => reply.file(path.join(currentDir, 'index.html'), {
confine: false
if (options.noDefaultRoute == null) {
http.route({
method: 'GET',
path: '/',
handler: (_, reply) => reply.file(path.join(currentDir, 'index.html'), {
confine: false
})
})
})
}

if (options.metrics === true) {
log('enabling metrics')
Expand Down
33 changes: 33 additions & 0 deletions packages/webrtc-star-signalling-server/test/sig-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { SigServer, sigServer } from '../src/index.js'
import pWaitFor from 'p-wait-for'
import { pEvent } from 'p-event'
import type { WebRTCStarSocket } from '@libp2p/webrtc-star-protocol'
import { get as httpGet } from 'http'

export default (clientName: string, io: (url: string, opts: any) => WebRTCStarSocket, sioOptions: any) => {
describe(`signalling ${clientName}`, () => {
Expand Down Expand Up @@ -68,11 +69,34 @@ export default (clientName: string, io: (url: string, opts: any) => WebRTCStarSo
}

const server = await sigServer(options)
const response = await fetch(`${server.info.uri}`)

expect(server.info.port).to.equal(12345)
expect(server.info.protocol).to.equal('http')
expect(server.info.address).to.equal('0.0.0.0')

expect(response.status).to.equal(404)

await server.stop()
})

it('start and stop signalling server (alternate default route)', async () => {
const options = {
noDefaultRoute: true
}

const server = await sigServer(options)
server.route({
method: 'GET',
path: '/',
handler: (_, reply) => reply.file('./src/index.html', {
confine: false
})
})
const response = await fetch(`${server.info.uri}/`)

expect(response.status).to.equal(200)

await server.stop()
})

Expand Down Expand Up @@ -224,3 +248,12 @@ export default (clientName: string, io: (url: string, opts: any) => WebRTCStarSo
})
})
}

async function fetch (url: string): Promise<{ status: number }> {
return await new Promise((resolve, reject) => {
httpGet(url, (res) => {
const { statusCode } = res
resolve({ status: statusCode ?? -1 })
}).on('error', reject)
})
}
3 changes: 2 additions & 1 deletion packages/webrtc-star-signalling-server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"compilerOptions": {
"outDir": "dist",
"emitDeclarationOnly": false,
"module": "ES2020"
"module": "ES2020",
"moduleResolution": "node"
},
"include": [
"src",
Expand Down
1 change: 1 addition & 0 deletions packages/webrtc-star-transport/.aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ let firstRun = true
/** @type {import('aegir').PartialOptions} */
export default {
test: {
target: ["node", "browser", "electron-main"],
async before () {
const { sigServer } = await import('@libp2p/webrtc-star-signalling-server')

Expand Down

0 comments on commit d7e23f9

Please sign in to comment.