]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - lib/webtorrentProcess.js
3 function webtorrent (args
) {
4 var WebTorrent
= require('webtorrent')
5 var ipc
= require('node-ipc')
7 if (args
.length
!== 3) {
8 console
.log('Wrong arguments number: ' + args
.length
+ '/3')
14 var nodeKey
= 'webtorrentnode' + port
15 var processKey
= 'webtorrentprocess' + port
17 ipc
.config
.silent
= true
18 ipc
.config
.id
= processKey
20 if (host
=== 'client' && port
=== '1') global
.WEBTORRENT_ANNOUNCE
= []
21 else global
.WEBTORRENT_ANNOUNCE
= 'ws://' + host
+ ':' + port
+ '/tracker/socket'
22 var wt
= new WebTorrent({ dht: false })
24 function seed (data
) {
29 wt
.seed(path
, { announceList: '' }, function (torrent
) {
31 magnetUri: torrent
.magnetURI
34 ipc
.of[nodeKey
].emit(nodeKey
+ '.seedDone.' + _id
, to_send
)
40 var magnetUri
= args
.magnetUri
43 wt
.add(magnetUri
, function (torrent
) {
48 torrent
.files
.forEach(function (file
) {
49 to_send
.files
.push({ path: file
.path
})
52 ipc
.of[nodeKey
].emit(nodeKey
+ '.addDone.' + _id
, to_send
)
56 function remove (data
) {
58 var magnetUri
= args
.magnetUri
62 wt
.remove(magnetUri
, callback
)
64 console
.log('Cannot remove the torrent from WebTorrent.')
68 function callback () {
70 ipc
.of[nodeKey
].emit(nodeKey
+ '.removeDone.' + _id
, to_send
)
74 console
.log('Configuration: ' + host
+ ':' + port
)
75 console
.log('Connecting to IPC...')
77 ipc
.connectTo(nodeKey
, function () {
78 ipc
.of[nodeKey
].on(processKey
+ '.seed', seed
)
79 ipc
.of[nodeKey
].on(processKey
+ '.add', add
)
80 ipc
.of[nodeKey
].on(processKey
+ '.remove', remove
)
82 ipc
.of[nodeKey
].emit(processKey
+ '.ready')
86 process
.on('uncaughtException', function (e
) {
87 ipc
.of[nodeKey
].emit(processKey
+ '.exception', { exception: e
})
91 // ---------------------------------------------------------------------------
93 module
.exports
= webtorrent