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

Commit c0369b7

Browse files
committed
feat: pass through middleware config, noDefaultRoute option
1 parent 0aacddf commit c0369b7

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

packages/webrtc-star-signalling-server/src/index.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Server } from '@hapi/hapi'
2+
import type { ServerOptions } from '@hapi/hapi'
23
import Inert from '@hapi/inert'
34

45
import { config } from './config.js'
@@ -18,6 +19,8 @@ interface Options {
1819
port?: number
1920
host?: string
2021
metrics?: boolean
22+
noDefaultRoute?: boolean
23+
hapi?: ServerOptions
2124
}
2225

2326
export interface SigServer extends Server {
@@ -32,6 +35,7 @@ export async function sigServer (options: Options = {}) {
3235

3336
const http: SigServer = Object.assign(new Server({
3437
...config.hapi.options,
38+
...options.hapi ?? {},
3539
port,
3640
host
3741
}), {
@@ -52,13 +56,15 @@ export async function sigServer (options: Options = {}) {
5256

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

55-
http.route({
56-
method: 'GET',
57-
path: '/',
58-
handler: (request, reply) => reply.file(path.join(currentDir, 'index.html'), {
59-
confine: false
59+
if (options.noDefaultRoute == null) {
60+
http.route({
61+
method: 'GET',
62+
path: '/',
63+
handler: (_, reply) => reply.file(path.join(currentDir, 'index.html'), {
64+
confine: false
65+
})
6066
})
61-
})
67+
}
6268

6369
if (options.metrics === true) {
6470
log('enabling metrics')

packages/webrtc-star-signalling-server/test/sig-server.ts

+33
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { SigServer, sigServer } from '../src/index.js'
66
import pWaitFor from 'p-wait-for'
77
import { pEvent } from 'p-event'
88
import type { WebRTCStarSocket } from '@libp2p/webrtc-star-protocol'
9+
import { get as httpGet } from 'http'
910

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

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

7274
expect(server.info.port).to.equal(12345)
7375
expect(server.info.protocol).to.equal('http')
7476
expect(server.info.address).to.equal('0.0.0.0')
7577

78+
expect(response.status).to.equal(404)
79+
80+
await server.stop()
81+
})
82+
83+
it('start and stop signalling server (alternate default route)', async () => {
84+
const options = {
85+
noDefaultRoute: true
86+
}
87+
88+
const server = await sigServer(options)
89+
server.route({
90+
method: 'GET',
91+
path: '/',
92+
handler: (_, reply) => reply.file('./src/index.html', {
93+
confine: false
94+
})
95+
})
96+
const response = await fetch(`${server.info.uri}/`)
97+
98+
expect(response.status).to.equal(200)
99+
76100
await server.stop()
77101
})
78102

@@ -224,3 +248,12 @@ export default (clientName: string, io: (url: string, opts: any) => WebRTCStarSo
224248
})
225249
})
226250
}
251+
252+
async function fetch (url: string): Promise<{ status: number }> {
253+
return await new Promise((resolve, reject) => {
254+
httpGet(url, (res) => {
255+
const { statusCode } = res
256+
resolve({ status: statusCode ?? -1 })
257+
}).on('error', reject)
258+
})
259+
}

packages/webrtc-star-signalling-server/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"compilerOptions": {
44
"outDir": "dist",
55
"emitDeclarationOnly": false,
6-
"module": "ES2020"
6+
"module": "ES2020",
7+
"moduleResolution": "node"
78
},
89
"include": [
910
"src",

packages/webrtc-star-transport/.aegir.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ let firstRun = true
77
/** @type {import('aegir').PartialOptions} */
88
export default {
99
test: {
10+
target: ["node", "browser", "electron-main"],
1011
async before () {
1112
const { sigServer } = await import('@libp2p/webrtc-star-signalling-server')
1213

0 commit comments

Comments
 (0)