Skip to content

Commit d354619

Browse files
committed
WIP
1 parent 11bbf7b commit d354619

File tree

4 files changed

+33
-199
lines changed

4 files changed

+33
-199
lines changed

lib/server/swarm.js

Lines changed: 0 additions & 172 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"url": "git://github.com/webtorrent/bittorrent-tracker.git"
7777
},
7878
"scripts": {
79-
"watch": "tape-watch test/*.js",
79+
"watch": "tape-watch test/server.js",
8080
"test": "tape test/*.js",
8181
"update-authors": "./tools/update-authors.sh"
8282
},

server.js

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const attachUdpService = require('./services/attachUdp')
88
const attachWSService = require('./services/attachWS')
99
const setupStatsRoute = require('./services/statsRoute')
1010
const common = require('./lib/common')
11-
const Swarm = require('./lib/server/swarm')
11+
const Swarm = require('./swarm')
1212

1313
/**
1414
* BitTorrent tracker server.
@@ -163,6 +163,22 @@ class Server extends EventEmitter {
163163
})
164164
}
165165

166+
// Get existing swarm, or create one if one does not exist
167+
getOrCreateSwarm(params) {
168+
const gotOrCreatedSwarm = resolve => {
169+
this.getSwarm(params.info_hash, (err, swarm) => {
170+
if (err) return resolve(err)
171+
if (swarm) return resolve(swarm)
172+
this.createSwarm(params.info_hash, (err, swarm) => {
173+
if (err) return resolve(err)
174+
resolve(swarm)
175+
})
176+
})
177+
}
178+
179+
return new Promise(gotOrCreatedSwarm)
180+
}
181+
166182
_onRequest (params, cb) {
167183
if (params && params.action === common.ACTIONS.CONNECT) {
168184
cb(null, { action: common.ACTIONS.CONNECT })
@@ -179,37 +195,24 @@ class Server extends EventEmitter {
179195
const self = this
180196

181197
if (this._filter) {
182-
this._filter(params.info_hash, params, err => {
198+
const onFiltered = err => {
183199
// Presence of `err` means that this announce request is disallowed
184200
if (err) return cb(err)
185201

186-
getOrCreateSwarm((err, swarm) => {
187-
if (err) return cb(err)
188-
announce(swarm)
189-
})
190-
})
191-
} else {
192-
getOrCreateSwarm((err, swarm) => {
193-
if (err) return cb(err)
194-
announce(swarm)
195-
})
196-
}
202+
this.getOrCreateSwarm(params)
203+
.then(announce)
204+
}
197205

198-
// Get existing swarm, or create one if one does not exist
199-
function getOrCreateSwarm (cb) {
200-
self.getSwarm(params.info_hash, (err, swarm) => {
201-
if (err) return cb(err)
202-
if (swarm) return cb(null, swarm)
203-
self.createSwarm(params.info_hash, (err, swarm) => {
204-
if (err) return cb(err)
205-
cb(null, swarm)
206-
})
207-
})
206+
this._filter(params.info_hash, params, onFiltered)
207+
} else {
208+
this.getOrCreateSwarm(params)
209+
.then(announce)
208210
}
209211

210212
function announce (swarm) {
211213
if (!params.event || params.event === 'empty') params.event = 'update'
212-
swarm.announce(params, (err, response) => {
214+
215+
const _onAnnounce = (err, response) => {
213216
if (err) return cb(err)
214217

215218
if (!response.action) response.action = common.ACTIONS.ANNOUNCE
@@ -242,7 +245,9 @@ class Server extends EventEmitter {
242245
} // else, return full peer objects (used for websocket responses)
243246

244247
cb(null, response)
245-
})
248+
}
249+
250+
swarm.announce(params, _onAnnounce )
246251
}
247252
}
248253

services/attachWS/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const WebSocketServer = require("ws").Server
22
const debug = require('debug')('bittorrent-tracker:server')
33

4+
const common = require('../../lib/common')
5+
46
const parseWebSocketRequest = require("./parseWebsocketRequest")
57
const attachHttpServer = require("../attachHttp")
68

79
const SDP_TRICKLE_REGEX = /a=ice-options:trickle\s\n/
810

9-
const common = require('../../lib/common')
1011
function setupHttpService(server, onListening) {
1112
if (server.http) return server.http
1213
else {

0 commit comments

Comments
 (0)