2022-12-05 22:06:54 +00:00
|
|
|
import { Server } from '../index.js'
|
2014-07-22 05:58:13 +00:00
|
|
|
|
2022-12-05 22:06:54 +00:00
|
|
|
export const createServer = (t, opts, cb) => {
|
2016-03-01 01:39:03 +00:00
|
|
|
if (typeof opts === 'string') opts = { serverType: opts }
|
|
|
|
|
|
|
|
opts.http = (opts.serverType === 'http')
|
|
|
|
opts.udp = (opts.serverType === 'udp')
|
|
|
|
opts.ws = (opts.serverType === 'ws')
|
|
|
|
|
2020-10-29 04:57:47 +00:00
|
|
|
const server = new Server(opts)
|
2014-07-22 05:58:13 +00:00
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
server.on('error', err => { t.error(err) })
|
|
|
|
server.on('warning', err => { t.error(err) })
|
2014-07-22 05:58:13 +00:00
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
server.listen(0, () => {
|
2020-10-29 04:57:47 +00:00
|
|
|
const port = server[opts.serverType].address().port
|
|
|
|
let announceUrl
|
2016-03-01 01:39:03 +00:00
|
|
|
if (opts.serverType === 'http') {
|
2021-06-15 01:54:41 +00:00
|
|
|
announceUrl = `http://127.0.0.1:${port}/announce`
|
2016-03-01 01:39:03 +00:00
|
|
|
} else if (opts.serverType === 'udp') {
|
2021-06-15 01:54:41 +00:00
|
|
|
announceUrl = `udp://127.0.0.1:${port}`
|
2016-03-01 01:39:03 +00:00
|
|
|
} else if (opts.serverType === 'ws') {
|
2021-06-15 01:54:41 +00:00
|
|
|
announceUrl = `ws://127.0.0.1:${port}`
|
2016-02-29 22:48:23 +00:00
|
|
|
}
|
2014-07-22 05:58:13 +00:00
|
|
|
|
|
|
|
cb(server, announceUrl)
|
|
|
|
})
|
|
|
|
}
|
2016-03-01 01:11:02 +00:00
|
|
|
|
2022-12-05 22:06:54 +00:00
|
|
|
export const mockWebsocketTracker = client => {
|
2021-06-15 01:54:41 +00:00
|
|
|
client._trackers[0]._generateOffers = (numwant, cb) => {
|
2020-10-29 04:57:47 +00:00
|
|
|
const offers = []
|
|
|
|
for (let i = 0; i < numwant; i++) {
|
2021-06-15 01:54:41 +00:00
|
|
|
offers.push({ fake_offer: `fake_offer_${i}` })
|
2016-03-01 01:11:02 +00:00
|
|
|
}
|
2021-06-15 01:54:41 +00:00
|
|
|
process.nextTick(() => {
|
2016-03-01 01:11:02 +00:00
|
|
|
cb(offers)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-12-05 22:06:54 +00:00
|
|
|
|
|
|
|
export default { mockWebsocketTracker, createServer }
|