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

Commit d7e23f9

Browse files
committed
feat: pass through middleware config, noDefaultRoute option
1 parent a686e42 commit d7e23f9

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

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

Lines changed: 12 additions & 6 deletions
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'
@@ -19,6 +20,8 @@ interface Options {
1920
host?: string
2021
metrics?: boolean
2122
refreshPeerListIntervalMS?: number
23+
noDefaultRoute?: boolean
24+
hapi?: ServerOptions
2225
}
2326

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

3437
const http: SigServer = Object.assign(new Server({
3538
...config.hapi.options,
39+
...options.hapi ?? {},
3640
port,
3741
host
3842
}), {
@@ -57,13 +61,15 @@ export async function sigServer (options: Options = {}) {
5761

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

60-
http.route({
61-
method: 'GET',
62-
path: '/',
63-
handler: (request, reply) => reply.file(path.join(currentDir, 'index.html'), {
64-
confine: false
64+
if (options.noDefaultRoute == null) {
65+
http.route({
66+
method: 'GET',
67+
path: '/',
68+
handler: (_, reply) => reply.file(path.join(currentDir, 'index.html'), {
69+
confine: false
70+
})
6571
})
66-
})
72+
}
6773

6874
if (options.metrics === true) {
6975
log('enabling metrics')

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

Lines changed: 33 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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)