]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/webtorrent.js
Add debug electron setting
[github/Chocobozzz/PeerTube.git] / server / lib / webtorrent.js
CommitLineData
9f10b292
C
1'use strict'
2
f0f5567b
C
3const config = require('config')
4const ipc = require('node-ipc')
5const pathUtils = require('path')
6const spawn = require('electron-spawn')
9f10b292 7
f0f5567b 8const logger = require('../helpers/logger')
9f10b292 9
09bc69df 10const electron_debug = config.get('electron.debug')
f0f5567b
C
11let host = config.get('webserver.host')
12let port = config.get('webserver.port')
13let nodeKey = 'webtorrentnode' + port
14let processKey = 'webtorrentprocess' + port
9f10b292
C
15ipc.config.silent = true
16ipc.config.id = nodeKey
17
f0f5567b 18const webtorrent = {
9f10b292
C
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
27function create (options, callback) {
28 if (typeof options === 'function') {
29 callback = options
30 options = {}
c5a8be2b 31 }
8c308c2b 32
9f10b292
C
33 // Override options
34 if (options.host) host = options.host
35 if (options.port) {
36 port = options.port
37 nodeKey = 'webtorrentnode' + port
38 processKey = 'webtorrentprocess' + port
39 ipc.config.id = nodeKey
40 }
8c308c2b 41
9f10b292
C
42 ipc.serve(function () {
43 if (!webtorrent.silent) logger.info('IPC server ready.')
8c308c2b 44
9f10b292 45 // Run a timeout of 30s after which we exit the process
f0f5567b 46 const timeout_webtorrent_process = setTimeout(function () {
ac2f99eb 47 throw new Error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.')
9f10b292 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 56 ipc.server.on(processKey + '.exception', function (data) {
233d12d8 57 throw new Error('Received exception error from webtorrent process : ' + data.exception)
9f10b292 58 })
8c308c2b 59
f0f5567b 60 const webtorrent_process = spawn(pathUtils.join(__dirname, 'webtorrentProcess.js'), host, port, { detached: true })
8c308c2b 61
09bc69df
C
62 if (electron_debug === 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 }
8c308c2b 71
9f10b292
C
72 webtorrent.app = webtorrent_process
73 })
8c308c2b 74
9f10b292
C
75 ipc.server.start()
76}
77
78function seed (path, callback) {
f0f5567b
C
79 const extension = pathUtils.extname(path)
80 const basename = pathUtils.basename(path, extension)
81 const data = {
9f10b292
C
82 _id: basename,
83 args: {
84 path: path
8c308c2b 85 }
9f10b292 86 }
8c308c2b 87
9f10b292 88 if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id)
8c308c2b 89
9f10b292 90 // Finish signal
f0f5567b 91 const event_key = nodeKey + '.seedDone.' + data._id
9f10b292
C
92 ipc.server.on(event_key, function listener (received) {
93 if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', received.magnetUri)
c5a8be2b 94
9f10b292 95 // This is a fake object, we just use the magnetUri in this project
f0f5567b 96 const torrent = {
9f10b292
C
97 magnetURI: received.magnetUri
98 }
8c308c2b 99
9f10b292
C
100 ipc.server.off(event_key)
101 callback(torrent)
102 })
c5a8be2b 103
9f10b292
C
104 ipc.server.broadcast(processKey + '.seed', data)
105}
c5a8be2b 106
9f10b292 107function add (magnetUri, callback) {
f0f5567b 108 const data = {
9f10b292
C
109 _id: magnetUri,
110 args: {
111 magnetUri: magnetUri
8c308c2b 112 }
9f10b292 113 }
8c308c2b 114
9f10b292 115 if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id)
8c308c2b 116
9f10b292 117 // Finish signal
f0f5567b 118 const event_key = nodeKey + '.addDone.' + data._id
9f10b292
C
119 ipc.server.on(event_key, function (received) {
120 if (!webtorrent.silent) logger.debug('Process added torrent.')
c5a8be2b 121
9f10b292 122 // This is a fake object, we just use the magnetUri in this project
f0f5567b 123 const torrent = {
9f10b292
C
124 files: received.files
125 }
8c308c2b 126
9f10b292
C
127 ipc.server.off(event_key)
128 callback(torrent)
129 })
0ae2e7f7 130
9f10b292
C
131 ipc.server.broadcast(processKey + '.add', data)
132}
c5a8be2b 133
9f10b292 134function remove (magnetUri, callback) {
f0f5567b 135 const data = {
9f10b292
C
136 _id: magnetUri,
137 args: {
138 magnetUri: magnetUri
c5a8be2b 139 }
9f10b292 140 }
c5a8be2b 141
9f10b292 142 if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id)
c5a8be2b 143
9f10b292 144 // Finish signal
f0f5567b 145 const event_key = nodeKey + '.removeDone.' + data._id
9f10b292
C
146 ipc.server.on(event_key, function (received) {
147 if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id)
c5a8be2b 148
f0f5567b 149 let err = null
9f10b292 150 if (received.err) err = received.err
c5a8be2b 151
9f10b292
C
152 ipc.server.off(event_key)
153 callback(err)
154 })
c5a8be2b 155
9f10b292
C
156 ipc.server.broadcast(processKey + '.remove', data)
157}
c45f7f84 158
9f10b292 159// ---------------------------------------------------------------------------
c45f7f84 160
9f10b292 161module.exports = webtorrent