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