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.js160
1 files changed, 0 insertions, 160 deletions
diff --git a/server/lib/webtorrent.js b/server/lib/webtorrent.js
deleted file mode 100644
index dde046828..000000000
--- a/server/lib/webtorrent.js
+++ /dev/null
@@ -1,160 +0,0 @@
1'use strict'
2
3const ipc = require('node-ipc')
4const pathUtils = require('path')
5const spawn = require('electron-spawn')
6
7const constants = require('../initializers/constants')
8const logger = require('../helpers/logger')
9
10let host = constants.CONFIG.WEBSERVER.HOST
11let port = constants.CONFIG.WEBSERVER.PORT
12let nodeKey = 'webtorrentnode' + port
13let processKey = 'webtorrentprocess' + port
14ipc.config.silent = true
15ipc.config.id = nodeKey
16
17const 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 const timeoutWebtorrentProcess = setTimeout(function () {
46 throw new Error('Timeout : cannot run the webtorrent process. Please ensure you have electron 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(timeoutWebtorrentProcess)
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 const webtorrentProcess = spawn(pathUtils.join(__dirname, 'webtorrent-process.js'), host, port, { detached: true })
60
61 if (constants.CONFIG.ELECTRON.DEBUG === true) {
62 webtorrentProcess.stderr.on('data', function (data) {
63 logger.debug('Webtorrent process stderr: ', data.toString())
64 })
65
66 webtorrentProcess.stdout.on('data', function (data) {
67 logger.debug('Webtorrent process:', data.toString())
68 })
69 }
70
71 webtorrent.app = webtorrentProcess
72 })
73
74 ipc.server.start()
75}
76
77function seed (path, callback) {
78 const extension = pathUtils.extname(path)
79 const basename = pathUtils.basename(path, extension)
80 const data = {
81 _id: basename,
82 args: {
83 path: path
84 }
85 }
86
87 if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id)
88
89 // Finish signal
90 const eventKey = nodeKey + '.seedDone.' + data._id
91 ipc.server.on(eventKey, function listener (received) {
92 if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', received.magnetUri)
93
94 // This is a fake object, we just use the magnetUri in this project
95 const torrent = {
96 magnetURI: received.magnetUri
97 }
98
99 ipc.server.off(eventKey, '*')
100 callback(torrent)
101 })
102
103 ipc.server.broadcast(processKey + '.seed', data)
104}
105
106function add (magnetUri, callback) {
107 const data = {
108 _id: magnetUri,
109 args: {
110 magnetUri: magnetUri
111 }
112 }
113
114 if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id)
115
116 // Finish signal
117 const eventKey = nodeKey + '.addDone.' + data._id
118 ipc.server.on(eventKey, function (received) {
119 if (!webtorrent.silent) logger.debug('Process added torrent.')
120
121 // This is a fake object, we just use the magnetUri in this project
122 const torrent = {
123 files: received.files
124 }
125
126 ipc.server.off(eventKey, '*')
127 callback(torrent)
128 })
129
130 ipc.server.broadcast(processKey + '.add', data)
131}
132
133function remove (magnetUri, callback) {
134 const data = {
135 _id: magnetUri,
136 args: {
137 magnetUri: magnetUri
138 }
139 }
140
141 if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id)
142
143 // Finish signal
144 const eventKey = nodeKey + '.removeDone.' + data._id
145 ipc.server.on(eventKey, function (received) {
146 if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id)
147
148 let err = null
149 if (received.err) err = received.err
150
151 ipc.server.off(eventKey, '*')
152 callback(err)
153 })
154
155 ipc.server.broadcast(processKey + '.remove', data)
156}
157
158// ---------------------------------------------------------------------------
159
160module.exports = webtorrent