]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - lib/webtorrent.js
Remove useless anonymous functions of files
[github/Chocobozzz/PeerTube.git] / lib / webtorrent.js
CommitLineData
9f10b292
C
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 = {}
c5a8be2b 30 }
8c308c2b 31
9f10b292
C
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 }
8c308c2b 40
9f10b292
C
41 ipc.serve(function () {
42 if (!webtorrent.silent) logger.info('IPC server ready.')
8c308c2b 43
9f10b292
C
44 // Run a timeout of 30s after which we exit the process
45 var timeout_webtorrent_process = setTimeout(function () {
46 logger.error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.')
47 process.exit()
48 }, 30000)
8c308c2b 49
9f10b292
C
50 ipc.server.on(processKey + '.ready', function () {
51 if (!webtorrent.silent) logger.info('Webtorrent process ready.')
52 clearTimeout(timeout_webtorrent_process)
53 callback()
54 })
8c308c2b 55
9f10b292
C
56 ipc.server.on(processKey + '.exception', function (data) {
57 logger.error('Received exception error from webtorrent process.', { exception: data.exception })
58 process.exit()
59 })
8c308c2b 60
9f10b292
C
61 var webtorrent_process = spawn(pathUtils.join(__dirname, 'webtorrentProcess.js'), host, port, { detached: true })
62 webtorrent_process.stderr.on('data', function (data) {
63 // logger.debug('Webtorrent process stderr: ', data.toString())
64 })
8c308c2b 65
9f10b292
C
66 webtorrent_process.stdout.on('data', function (data) {
67 // logger.debug('Webtorrent process:', data.toString())
c5a8be2b 68 })
8c308c2b 69
9f10b292
C
70 webtorrent.app = webtorrent_process
71 })
8c308c2b 72
9f10b292
C
73 ipc.server.start()
74}
75
76function seed (path, callback) {
77 var extension = pathUtils.extname(path)
78 var basename = pathUtils.basename(path, extension)
79 var data = {
80 _id: basename,
81 args: {
82 path: path
8c308c2b 83 }
9f10b292 84 }
8c308c2b 85
9f10b292 86 if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id)
8c308c2b 87
9f10b292
C
88 // Finish signal
89 var event_key = nodeKey + '.seedDone.' + data._id
90 ipc.server.on(event_key, function listener (received) {
91 if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', received.magnetUri)
c5a8be2b 92
9f10b292
C
93 // This is a fake object, we just use the magnetUri in this project
94 var torrent = {
95 magnetURI: received.magnetUri
96 }
8c308c2b 97
9f10b292
C
98 ipc.server.off(event_key)
99 callback(torrent)
100 })
c5a8be2b 101
9f10b292
C
102 ipc.server.broadcast(processKey + '.seed', data)
103}
c5a8be2b 104
9f10b292
C
105function add (magnetUri, callback) {
106 var data = {
107 _id: magnetUri,
108 args: {
109 magnetUri: magnetUri
8c308c2b 110 }
9f10b292 111 }
8c308c2b 112
9f10b292 113 if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id)
8c308c2b 114
9f10b292
C
115 // Finish signal
116 var event_key = nodeKey + '.addDone.' + data._id
117 ipc.server.on(event_key, function (received) {
118 if (!webtorrent.silent) logger.debug('Process added torrent.')
c5a8be2b 119
9f10b292
C
120 // This is a fake object, we just use the magnetUri in this project
121 var torrent = {
122 files: received.files
123 }
8c308c2b 124
9f10b292
C
125 ipc.server.off(event_key)
126 callback(torrent)
127 })
0ae2e7f7 128
9f10b292
C
129 ipc.server.broadcast(processKey + '.add', data)
130}
c5a8be2b 131
9f10b292
C
132function remove (magnetUri, callback) {
133 var data = {
134 _id: magnetUri,
135 args: {
136 magnetUri: magnetUri
c5a8be2b 137 }
9f10b292 138 }
c5a8be2b 139
9f10b292 140 if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id)
c5a8be2b 141
9f10b292
C
142 // Finish signal
143 var event_key = nodeKey + '.removeDone.' + data._id
144 ipc.server.on(event_key, function (received) {
145 if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id)
c5a8be2b 146
9f10b292
C
147 var err = null
148 if (received.err) err = received.err
c5a8be2b 149
9f10b292
C
150 ipc.server.off(event_key)
151 callback(err)
152 })
c5a8be2b 153
9f10b292
C
154 ipc.server.broadcast(processKey + '.remove', data)
155}
c45f7f84 156
9f10b292 157// ---------------------------------------------------------------------------
c45f7f84 158
9f10b292 159module.exports = webtorrent