]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/webtorrent.js
Server: put config in constants
[github/Chocobozzz/PeerTube.git] / server / lib / webtorrent.js
CommitLineData
9f10b292
C
1'use strict'
2
f0f5567b
C
3const ipc = require('node-ipc')
4const pathUtils = require('path')
5const spawn = require('electron-spawn')
9f10b292 6
e861452f 7const constants = require('../initializers/constants')
f0f5567b 8const logger = require('../helpers/logger')
9f10b292 9
e861452f
C
10let host = constants.CONFIG.WEBSERVER.HOST
11let port = constants.CONFIG.WEBSERVER.PORT
f0f5567b
C
12let nodeKey = 'webtorrentnode' + port
13let processKey = 'webtorrentprocess' + port
9f10b292
C
14ipc.config.silent = true
15ipc.config.id = nodeKey
16
f0f5567b 17const webtorrent = {
9f10b292
C
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 44 // Run a timeout of 30s after which we exit the process
bc503c2a 45 const timeoutWebtorrentProcess = setTimeout(function () {
ac2f99eb 46 throw new Error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.')
9f10b292 47 }, 30000)
8c308c2b 48
9f10b292
C
49 ipc.server.on(processKey + '.ready', function () {
50 if (!webtorrent.silent) logger.info('Webtorrent process ready.')
bc503c2a 51 clearTimeout(timeoutWebtorrentProcess)
9f10b292
C
52 callback()
53 })
8c308c2b 54
9f10b292 55 ipc.server.on(processKey + '.exception', function (data) {
233d12d8 56 throw new Error('Received exception error from webtorrent process : ' + data.exception)
9f10b292 57 })
8c308c2b 58
5f698b82 59 const webtorrentProcess = spawn(pathUtils.join(__dirname, 'webtorrent-process.js'), host, port, { detached: true })
8c308c2b 60
e861452f 61 if (constants.CONFIG.ELECTRON.DEBUG === true) {
bc503c2a 62 webtorrentProcess.stderr.on('data', function (data) {
09bc69df
C
63 logger.debug('Webtorrent process stderr: ', data.toString())
64 })
65
bc503c2a 66 webtorrentProcess.stdout.on('data', function (data) {
09bc69df
C
67 logger.debug('Webtorrent process:', data.toString())
68 })
69 }
8c308c2b 70
bc503c2a 71 webtorrent.app = webtorrentProcess
9f10b292 72 })
8c308c2b 73
9f10b292
C
74 ipc.server.start()
75}
76
77function seed (path, callback) {
f0f5567b
C
78 const extension = pathUtils.extname(path)
79 const basename = pathUtils.basename(path, extension)
80 const data = {
9f10b292
C
81 _id: basename,
82 args: {
83 path: path
8c308c2b 84 }
9f10b292 85 }
8c308c2b 86
9f10b292 87 if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id)
8c308c2b 88
9f10b292 89 // Finish signal
bc503c2a
C
90 const eventKey = nodeKey + '.seedDone.' + data._id
91 ipc.server.on(eventKey, function listener (received) {
9f10b292 92 if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', received.magnetUri)
c5a8be2b 93
9f10b292 94 // This is a fake object, we just use the magnetUri in this project
f0f5567b 95 const torrent = {
9f10b292
C
96 magnetURI: received.magnetUri
97 }
8c308c2b 98
5495c83a 99 ipc.server.off(eventKey, '*')
9f10b292
C
100 callback(torrent)
101 })
c5a8be2b 102
9f10b292
C
103 ipc.server.broadcast(processKey + '.seed', data)
104}
c5a8be2b 105
9f10b292 106function add (magnetUri, callback) {
f0f5567b 107 const data = {
9f10b292
C
108 _id: magnetUri,
109 args: {
110 magnetUri: magnetUri
8c308c2b 111 }
9f10b292 112 }
8c308c2b 113
9f10b292 114 if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id)
8c308c2b 115
9f10b292 116 // Finish signal
bc503c2a
C
117 const eventKey = nodeKey + '.addDone.' + data._id
118 ipc.server.on(eventKey, function (received) {
9f10b292 119 if (!webtorrent.silent) logger.debug('Process added torrent.')
c5a8be2b 120
9f10b292 121 // This is a fake object, we just use the magnetUri in this project
f0f5567b 122 const torrent = {
9f10b292
C
123 files: received.files
124 }
8c308c2b 125
5495c83a 126 ipc.server.off(eventKey, '*')
9f10b292
C
127 callback(torrent)
128 })
0ae2e7f7 129
9f10b292
C
130 ipc.server.broadcast(processKey + '.add', data)
131}
c5a8be2b 132
9f10b292 133function remove (magnetUri, callback) {
f0f5567b 134 const data = {
9f10b292
C
135 _id: magnetUri,
136 args: {
137 magnetUri: magnetUri
c5a8be2b 138 }
9f10b292 139 }
c5a8be2b 140
9f10b292 141 if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id)
c5a8be2b 142
9f10b292 143 // Finish signal
bc503c2a
C
144 const eventKey = nodeKey + '.removeDone.' + data._id
145 ipc.server.on(eventKey, function (received) {
9f10b292 146 if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id)
c5a8be2b 147
f0f5567b 148 let err = null
9f10b292 149 if (received.err) err = received.err
c5a8be2b 150
5495c83a 151 ipc.server.off(eventKey, '*')
9f10b292
C
152 callback(err)
153 })
c5a8be2b 154
9f10b292
C
155 ipc.server.broadcast(processKey + '.remove', data)
156}
c45f7f84 157
9f10b292 158// ---------------------------------------------------------------------------
c45f7f84 159
9f10b292 160module.exports = webtorrent