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