]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
f0f5567b C |
3 | const config = require('config') |
4 | const ipc = require('node-ipc') | |
5 | const pathUtils = require('path') | |
6 | const spawn = require('electron-spawn') | |
9f10b292 | 7 | |
f0f5567b | 8 | const logger = require('../helpers/logger') |
9f10b292 | 9 | |
bc503c2a | 10 | const electronDebug = config.get('electron.debug') |
f0f5567b C |
11 | let host = config.get('webserver.host') |
12 | let port = config.get('webserver.port') | |
13 | let nodeKey = 'webtorrentnode' + port | |
14 | let processKey = 'webtorrentprocess' + port | |
9f10b292 C |
15 | ipc.config.silent = true |
16 | ipc.config.id = nodeKey | |
17 | ||
f0f5567b | 18 | const webtorrent = { |
9f10b292 C |
19 | add: add, |
20 | app: null, // Pid of the app | |
21 | create: create, | |
22 | remove: remove, | |
23 | seed: seed, | |
24 | silent: false // Useful for beautiful tests | |
25 | } | |
26 | ||
27 | function create (options, callback) { | |
28 | if (typeof options === 'function') { | |
29 | callback = options | |
30 | options = {} | |
c5a8be2b | 31 | } |
8c308c2b | 32 | |
9f10b292 C |
33 | // Override options |
34 | if (options.host) host = options.host | |
35 | if (options.port) { | |
36 | port = options.port | |
37 | nodeKey = 'webtorrentnode' + port | |
38 | processKey = 'webtorrentprocess' + port | |
39 | ipc.config.id = nodeKey | |
40 | } | |
8c308c2b | 41 | |
9f10b292 C |
42 | ipc.serve(function () { |
43 | if (!webtorrent.silent) logger.info('IPC server ready.') | |
8c308c2b | 44 | |
9f10b292 | 45 | // Run a timeout of 30s after which we exit the process |
bc503c2a | 46 | const timeoutWebtorrentProcess = setTimeout(function () { |
ac2f99eb | 47 | throw new Error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.') |
9f10b292 | 48 | }, 30000) |
8c308c2b | 49 | |
9f10b292 C |
50 | ipc.server.on(processKey + '.ready', function () { |
51 | if (!webtorrent.silent) logger.info('Webtorrent process ready.') | |
bc503c2a | 52 | clearTimeout(timeoutWebtorrentProcess) |
9f10b292 C |
53 | callback() |
54 | }) | |
8c308c2b | 55 | |
9f10b292 | 56 | ipc.server.on(processKey + '.exception', function (data) { |
233d12d8 | 57 | throw new Error('Received exception error from webtorrent process : ' + data.exception) |
9f10b292 | 58 | }) |
8c308c2b | 59 | |
5f698b82 | 60 | const webtorrentProcess = spawn(pathUtils.join(__dirname, 'webtorrent-process.js'), host, port, { detached: true }) |
8c308c2b | 61 | |
bc503c2a C |
62 | if (electronDebug === true) { |
63 | webtorrentProcess.stderr.on('data', function (data) { | |
09bc69df C |
64 | logger.debug('Webtorrent process stderr: ', data.toString()) |
65 | }) | |
66 | ||
bc503c2a | 67 | webtorrentProcess.stdout.on('data', function (data) { |
09bc69df C |
68 | logger.debug('Webtorrent process:', data.toString()) |
69 | }) | |
70 | } | |
8c308c2b | 71 | |
bc503c2a | 72 | webtorrent.app = webtorrentProcess |
9f10b292 | 73 | }) |
8c308c2b | 74 | |
9f10b292 C |
75 | ipc.server.start() |
76 | } | |
77 | ||
78 | function seed (path, callback) { | |
f0f5567b C |
79 | const extension = pathUtils.extname(path) |
80 | const basename = pathUtils.basename(path, extension) | |
81 | const data = { | |
9f10b292 C |
82 | _id: basename, |
83 | args: { | |
84 | path: path | |
8c308c2b | 85 | } |
9f10b292 | 86 | } |
8c308c2b | 87 | |
9f10b292 | 88 | if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id) |
8c308c2b | 89 | |
9f10b292 | 90 | // Finish signal |
bc503c2a C |
91 | const eventKey = nodeKey + '.seedDone.' + data._id |
92 | ipc.server.on(eventKey, function listener (received) { | |
9f10b292 | 93 | if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', received.magnetUri) |
c5a8be2b | 94 | |
9f10b292 | 95 | // This is a fake object, we just use the magnetUri in this project |
f0f5567b | 96 | const torrent = { |
9f10b292 C |
97 | magnetURI: received.magnetUri |
98 | } | |
8c308c2b | 99 | |
5495c83a | 100 | ipc.server.off(eventKey, '*') |
9f10b292 C |
101 | callback(torrent) |
102 | }) | |
c5a8be2b | 103 | |
9f10b292 C |
104 | ipc.server.broadcast(processKey + '.seed', data) |
105 | } | |
c5a8be2b | 106 | |
9f10b292 | 107 | function add (magnetUri, callback) { |
f0f5567b | 108 | const data = { |
9f10b292 C |
109 | _id: magnetUri, |
110 | args: { | |
111 | magnetUri: magnetUri | |
8c308c2b | 112 | } |
9f10b292 | 113 | } |
8c308c2b | 114 | |
9f10b292 | 115 | if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id) |
8c308c2b | 116 | |
9f10b292 | 117 | // Finish signal |
bc503c2a C |
118 | const eventKey = nodeKey + '.addDone.' + data._id |
119 | ipc.server.on(eventKey, function (received) { | |
9f10b292 | 120 | if (!webtorrent.silent) logger.debug('Process added torrent.') |
c5a8be2b | 121 | |
9f10b292 | 122 | // This is a fake object, we just use the magnetUri in this project |
f0f5567b | 123 | const torrent = { |
9f10b292 C |
124 | files: received.files |
125 | } | |
8c308c2b | 126 | |
5495c83a | 127 | ipc.server.off(eventKey, '*') |
9f10b292 C |
128 | callback(torrent) |
129 | }) | |
0ae2e7f7 | 130 | |
9f10b292 C |
131 | ipc.server.broadcast(processKey + '.add', data) |
132 | } | |
c5a8be2b | 133 | |
9f10b292 | 134 | function remove (magnetUri, callback) { |
f0f5567b | 135 | const data = { |
9f10b292 C |
136 | _id: magnetUri, |
137 | args: { | |
138 | magnetUri: magnetUri | |
c5a8be2b | 139 | } |
9f10b292 | 140 | } |
c5a8be2b | 141 | |
9f10b292 | 142 | if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id) |
c5a8be2b | 143 | |
9f10b292 | 144 | // Finish signal |
bc503c2a C |
145 | const eventKey = nodeKey + '.removeDone.' + data._id |
146 | ipc.server.on(eventKey, function (received) { | |
9f10b292 | 147 | if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id) |
c5a8be2b | 148 | |
f0f5567b | 149 | let err = null |
9f10b292 | 150 | if (received.err) err = received.err |
c5a8be2b | 151 | |
5495c83a | 152 | ipc.server.off(eventKey, '*') |
9f10b292 C |
153 | callback(err) |
154 | }) | |
c5a8be2b | 155 | |
9f10b292 C |
156 | ipc.server.broadcast(processKey + '.remove', data) |
157 | } | |
c45f7f84 | 158 | |
9f10b292 | 159 | // --------------------------------------------------------------------------- |
c45f7f84 | 160 | |
9f10b292 | 161 | module.exports = webtorrent |