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 electron_debug = 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 timeout_webtorrent_process = 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(timeout_webtorrent_process)
56 ipc.server.on(processKey + '.exception', function (data) {
57 throw new Error('Received exception error from webtorrent process : ' + data.exception)
60 const webtorrent_process = spawn(pathUtils.join(__dirname, 'webtorrentProcess.js'), host, port, { detached: true })
62 if (electron_debug === true) {
63 webtorrent_process.stderr.on('data', function (data) {
64 logger.debug('Webtorrent process stderr: ', data.toString())
67 webtorrent_process.stdout.on('data', function (data) {
68 logger.debug('Webtorrent process:', data.toString())
72 webtorrent.app = webtorrent_process
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 event_key = nodeKey + '.seedDone.' + data._id
92 ipc.server.on(event_key, 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(event_key)
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 event_key = nodeKey + '.addDone.' + data._id
119 ipc.server.on(event_key, 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(event_key)
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 event_key = nodeKey + '.removeDone.' + data._id
146 ipc.server.on(event_key, 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(event_key)
156 ipc.server.broadcast(processKey + '.remove', data)
159 // ---------------------------------------------------------------------------
161 module.exports = webtorrent