3 const config
= require('config')
4 const ipc
= require('node-ipc')
5 const pathUtils
= require('path')
6 const spawn
= require('electron-spawn')
8 const logger
= require('../helpers/logger')
10 let host
= config
.get('webserver.host')
11 let port
= config
.get('webserver.port')
12 let nodeKey
= 'webtorrentnode' + port
13 let processKey
= 'webtorrentprocess' + port
14 ipc
.config
.silent
= true
15 ipc
.config
.id
= nodeKey
19 app: null, // Pid of the app
23 silent: false // Useful for beautiful tests
26 function create (options
, callback
) {
27 if (typeof options
=== 'function') {
33 if (options
.host
) host
= options
.host
36 nodeKey
= 'webtorrentnode' + port
37 processKey
= 'webtorrentprocess' + port
38 ipc
.config
.id
= nodeKey
41 ipc
.serve(function () {
42 if (!webtorrent
.silent
) logger
.info('IPC server ready.')
44 // Run a timeout of 30s after which we exit the process
45 const timeout_webtorrent_process
= setTimeout(function () {
46 throw new Error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.')
49 ipc
.server
.on(processKey
+ '.ready', function () {
50 if (!webtorrent
.silent
) logger
.info('Webtorrent process ready.')
51 clearTimeout(timeout_webtorrent_process
)
55 ipc
.server
.on(processKey
+ '.exception', function (data
) {
56 throw new Error('Received exception error from webtorrent process : ' + data
.exception
)
59 const webtorrent_process
= spawn(pathUtils
.join(__dirname
, 'webtorrentProcess.js'), host
, port
, { detached: true })
60 webtorrent_process
.stderr
.on('data', function (data
) {
61 // logger.debug('Webtorrent process stderr: ', data.toString())
64 webtorrent_process
.stdout
.on('data', function (data
) {
65 // logger.debug('Webtorrent process:', data.toString())
68 webtorrent
.app
= webtorrent_process
74 function seed (path
, callback
) {
75 const extension
= pathUtils
.extname(path
)
76 const basename
= pathUtils
.basename(path
, extension
)
84 if (!webtorrent
.silent
) logger
.debug('Node wants to seed %s.', data
._id
)
87 const event_key
= nodeKey
+ '.seedDone.' + data
._id
88 ipc
.server
.on(event_key
, function listener (received
) {
89 if (!webtorrent
.silent
) logger
.debug('Process seeded torrent %s.', received
.magnetUri
)
91 // This is a fake object, we just use the magnetUri in this project
93 magnetURI: received
.magnetUri
96 ipc
.server
.off(event_key
)
100 ipc
.server
.broadcast(processKey
+ '.seed', data
)
103 function add (magnetUri
, callback
) {
111 if (!webtorrent
.silent
) logger
.debug('Node wants to add ' + data
._id
)
114 const event_key
= nodeKey
+ '.addDone.' + data
._id
115 ipc
.server
.on(event_key
, function (received
) {
116 if (!webtorrent
.silent
) logger
.debug('Process added torrent.')
118 // This is a fake object, we just use the magnetUri in this project
120 files: received
.files
123 ipc
.server
.off(event_key
)
127 ipc
.server
.broadcast(processKey
+ '.add', data
)
130 function remove (magnetUri
, callback
) {
138 if (!webtorrent
.silent
) logger
.debug('Node wants to stop seeding %s.', data
._id
)
141 const event_key
= nodeKey
+ '.removeDone.' + data
._id
142 ipc
.server
.on(event_key
, function (received
) {
143 if (!webtorrent
.silent
) logger
.debug('Process removed torrent %s.', data
._id
)
146 if (received
.err
) err
= received
.err
148 ipc
.server
.off(event_key
)
152 ipc
.server
.broadcast(processKey
+ '.remove', data
)
155 // ---------------------------------------------------------------------------
157 module
.exports
= webtorrent