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