4 var config
= require('config')
5 var ipc
= require('node-ipc')
6 var pathUtils
= require('path')
7 var spawn
= require('electron-spawn')
9 var logger
= require('./logger')
11 var host
= config
.get('webserver.host')
12 var port
= config
.get('webserver.port')
14 var nodeKey
= 'webtorrentnode' + port
15 var processKey
= 'webtorrent' + port
17 ipc
.config
.silent
= true
18 ipc
.config
.id
= nodeKey
20 var webtorrentnode
= {}
22 // Useful for beautiful tests
23 webtorrentnode
.silent
= false
26 webtorrentnode
.app
= null
28 webtorrentnode
.create = function (options
, callback
) {
29 if (typeof options
=== 'function') {
35 if (options
.host
) host
= options
.host
38 nodeKey
= 'webtorrentnode' + port
39 processKey
= 'webtorrent' + port
40 ipc
.config
.id
= nodeKey
43 ipc
.serve(function () {
44 if (!webtorrentnode
.silent
) logger
.info('IPC server ready.')
46 // Run a timeout of 30s after which we exit the process
47 var timeout_webtorrent_process
= setTimeout(function () {
48 logger
.error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.')
52 ipc
.server
.on(processKey
+ '.ready', function () {
53 if (!webtorrentnode
.silent
) logger
.info('Webtorrent process ready.')
54 clearTimeout(timeout_webtorrent_process
)
58 ipc
.server
.on(processKey
+ '.exception', function (data
) {
59 logger
.error('Received exception error from webtorrent process.', { exception: data
.exception
})
63 var webtorrent_process
= spawn(__dirname
+ '/webtorrent.js', host
, port
, { detached: true })
64 webtorrent_process
.stderr
.on('data', function (data
) {
65 // logger.debug('Webtorrent process stderr: ', data.toString())
68 webtorrent_process
.stdout
.on('data', function (data
) {
69 // logger.debug('Webtorrent process:', data.toString())
72 webtorrentnode
.app
= webtorrent_process
78 webtorrentnode
.seed = function (path
, callback
) {
79 var extension
= pathUtils
.extname(path
)
80 var basename
= pathUtils
.basename(path
, extension
)
88 if (!webtorrentnode
.silent
) logger
.debug('Node wants to seed %s.', data
._id
)
91 var event_key
= nodeKey
+ '.seedDone.' + data
._id
92 ipc
.server
.on(event_key
, function listener (received
) {
93 if (!webtorrentnode
.silent
) logger
.debug('Process seeded torrent %s.', received
.magnetUri
)
95 // This is a fake object, we just use the magnetUri in this project
97 magnetURI: received
.magnetUri
100 ipc
.server
.off(event_key
)
104 ipc
.server
.broadcast(processKey
+ '.seed', data
)
107 webtorrentnode
.add = function (magnetUri
, callback
) {
115 if (!webtorrentnode
.silent
) logger
.debug('Node wants to add ' + data
._id
)
118 var event_key
= nodeKey
+ '.addDone.' + data
._id
119 ipc
.server
.on(event_key
, function (received
) {
120 if (!webtorrentnode
.silent
) logger
.debug('Process added torrent.')
122 // This is a fake object, we just use the magnetUri in this project
124 files: received
.files
127 ipc
.server
.off(event_key
)
131 ipc
.server
.broadcast(processKey
+ '.add', data
)
134 webtorrentnode
.remove = function (magnetUri
, callback
) {
142 if (!webtorrentnode
.silent
) logger
.debug('Node wants to stop seeding %s.', data
._id
)
145 var event_key
= nodeKey
+ '.removeDone.' + data
._id
146 ipc
.server
.on(event_key
, function (received
) {
147 if (!webtorrentnode
.silent
) logger
.debug('Process removed torrent %s.', data
._id
)
150 if (received
.err
) err
= received
.err
152 ipc
.server
.off(event_key
)
156 ipc
.server
.broadcast(processKey
+ '.remove', data
)
159 module
.exports
= webtorrentnode