1
0
Fork 0
peertube/server/lib/webtorrentProcess.js

93 lines
2.3 KiB
JavaScript
Raw Normal View History

'use strict'
2016-02-06 15:22:39 +00:00
2016-03-16 21:29:27 +00:00
const WebTorrent = require('webtorrent')
const ipc = require('node-ipc')
2016-02-06 15:22:39 +00:00
2016-02-07 12:12:32 +00:00
function webtorrent (args) {
if (args.length !== 3) {
throw new Error('Wrong arguments number: ' + args.length + '/3')
}
2016-02-06 15:22:39 +00:00
2016-03-16 21:29:27 +00:00
const host = args[1]
const port = args[2]
const nodeKey = 'webtorrentnode' + port
const processKey = 'webtorrentprocess' + port
2016-02-06 15:22:39 +00:00
ipc.config.silent = true
ipc.config.id = processKey
2016-02-06 15:22:39 +00:00
if (host === 'client' && port === '1') global.WEBTORRENT_ANNOUNCE = []
else global.WEBTORRENT_ANNOUNCE = 'ws://' + host + ':' + port + '/tracker/socket'
2016-03-16 21:29:27 +00:00
const wt = new WebTorrent({ dht: false })
2016-02-06 15:22:39 +00:00
function seed (data) {
2016-03-16 21:29:27 +00:00
const args = data.args
const path = args.path
const _id = data._id
2016-02-06 15:22:39 +00:00
wt.seed(path, { announceList: '' }, function (torrent) {
2016-03-16 21:29:27 +00:00
const to_send = {
magnetUri: torrent.magnetURI
}
2016-02-06 15:22:39 +00:00
ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, to_send)
})
}
2016-02-06 15:22:39 +00:00
function add (data) {
2016-03-16 21:29:27 +00:00
const args = data.args
const magnetUri = args.magnetUri
const _id = data._id
2016-02-06 15:22:39 +00:00
wt.add(magnetUri, function (torrent) {
2016-03-16 21:29:27 +00:00
const to_send = {
files: []
}
2016-02-06 15:22:39 +00:00
torrent.files.forEach(function (file) {
to_send.files.push({ path: file.path })
2016-02-06 15:22:39 +00:00
})
ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, to_send)
})
}
2016-02-06 15:22:39 +00:00
function remove (data) {
2016-03-16 21:29:27 +00:00
const args = data.args
const magnetUri = args.magnetUri
const _id = data._id
2016-02-06 15:22:39 +00:00
try {
wt.remove(magnetUri, callback)
} catch (err) {
console.log('Cannot remove the torrent from WebTorrent.')
return callback(null)
}
function callback () {
2016-03-16 21:29:27 +00:00
const to_send = {}
ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, to_send)
2016-02-06 15:22:39 +00:00
}
}
2016-02-06 15:22:39 +00:00
console.log('Configuration: ' + host + ':' + port)
console.log('Connecting to IPC...')
2016-02-06 15:22:39 +00:00
ipc.connectTo(nodeKey, function () {
ipc.of[nodeKey].on(processKey + '.seed', seed)
ipc.of[nodeKey].on(processKey + '.add', add)
ipc.of[nodeKey].on(processKey + '.remove', remove)
2016-02-06 15:22:39 +00:00
ipc.of[nodeKey].emit(processKey + '.ready')
console.log('Ready.')
})
2016-02-06 15:22:39 +00:00
process.on('uncaughtException', function (e) {
ipc.of[nodeKey].emit(processKey + '.exception', { exception: e })
})
}
2016-02-06 15:22:39 +00:00
// ---------------------------------------------------------------------------
2016-02-06 15:22:39 +00:00
module.exports = webtorrent