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 const electronDebug = config.get('electron.debug')
11 let host = config.get('webserver.host')
12 let port = config.get('webserver.port')
13 let nodeKey = 'webtorrentnode' + port
14 let processKey = 'webtorrentprocess' + port
15 ipc.config.silent = true
16 ipc.config.id = nodeKey
20 app: null, // Pid of the app
24 silent: false // Useful for beautiful tests
27 function create (options, callback) {
28 if (typeof options === 'function') {
34 if (options.host) host = options.host
37 nodeKey = 'webtorrentnode' + port
38 processKey = 'webtorrentprocess' + port
39 ipc.config.id = nodeKey
42 ipc.serve(function () {
43 if (!webtorrent.silent) logger.info('IPC server ready.')
45 // Run a timeout of 30s after which we exit the process
46 const timeoutWebtorrentProcess = setTimeout(function () {
47 throw new Error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.')
50 ipc.server.on(processKey + '.ready', function () {
51 if (!webtorrent.silent) logger.info('Webtorrent process ready.')
52 clearTimeout(timeoutWebtorrentProcess)
56 ipc.server.on(processKey + '.exception', function (data) {
57 throw new Error('Received exception error from webtorrent process : ' + data.exception)
60 const webtorrentProcess = spawn(pathUtils.join(__dirname, 'webtorrentProcess.js'), host, port, { detached: true })
62 if (electronDebug === true) {
63 webtorrentProcess.stderr.on('data', function (data) {
64 logger.debug('Webtorrent process stderr: ', data.toString())
67 webtorrentProcess.stdout.on('data', function (data) {
68 logger.debug('Webtorrent process:', data.toString())
72 webtorrent.app = webtorrentProcess
78 function seed (path, callback) {
79 const extension = pathUtils.extname(path)
80 const basename = pathUtils.basename(path, extension)
88 if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id)
91 const eventKey = nodeKey + '.seedDone.' + data._id
92 ipc.server.on(eventKey, function listener (received) {
93 if (!webtorrent.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(eventKey)
104 ipc.server.broadcast(processKey + '.seed', data)
107 function add (magnetUri, callback) {
115 if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id)
118 const eventKey = nodeKey + '.addDone.' + data._id
119 ipc.server.on(eventKey, function (received) {
120 if (!webtorrent.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(eventKey)
131 ipc.server.broadcast(processKey + '.add', data)
134 function remove (magnetUri, callback) {
142 if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id)
145 const eventKey = nodeKey + '.removeDone.' + data._id
146 ipc.server.on(eventKey, function (received) {
147 if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id)
150 if (received.err) err = received.err
152 ipc.server.off(eventKey)
156 ipc.server.broadcast(processKey + '.remove', data)
159 // ---------------------------------------------------------------------------
161 module.exports = webtorrent