]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/webtorrent.js
3 const ipc
= require('node-ipc')
4 const pathUtils
= require('path')
5 const spawn
= require('electron-spawn')
7 const constants
= require('../initializers/constants')
8 const logger
= require('../helpers/logger')
10 let host
= constants
.CONFIG
.WEBSERVER
.HOST
11 let port
= constants
.CONFIG
.WEBSERVER
.PORT
12 let nodeKey
= 'webtorrentnode' + port
13 let processKey
= 'webtorrentprocess' + port
14 ipc
.config
.silent
= true
15 ipc
.config
.id
= nodeKey
19 app: null, // Pid of the app
23 silent: false // Useful for beautiful tests
26 function create (options
, callback
) {
27 if (typeof options
=== 'function') {
33 if (options
.host
) host
= options
.host
36 nodeKey
= 'webtorrentnode' + port
37 processKey
= 'webtorrentprocess' + port
38 ipc
.config
.id
= nodeKey
41 ipc
.serve(function () {
42 if (!webtorrent
.silent
) logger
.info('IPC server ready.')
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-prebuilt npm package installed with xvfb-run.')
49 ipc
.server
.on(processKey
+ '.ready', function () {
50 if (!webtorrent
.silent
) logger
.info('Webtorrent process ready.')
51 clearTimeout(timeoutWebtorrentProcess
)
55 ipc
.server
.on(processKey
+ '.exception', function (data
) {
56 throw new Error('Received exception error from webtorrent process : ' + data
.exception
)
59 const webtorrentProcess
= spawn(pathUtils
.join(__dirname
, 'webtorrent-process.js'), host
, port
, { detached: true })
61 if (constants
.CONFIG
.ELECTRON
.DEBUG
=== true) {
62 webtorrentProcess
.stderr
.on('data', function (data
) {
63 logger
.debug('Webtorrent process stderr: ', data
.toString())
66 webtorrentProcess
.stdout
.on('data', function (data
) {
67 logger
.debug('Webtorrent process:', data
.toString())
71 webtorrent
.app
= webtorrentProcess
77 function seed (path
, callback
) {
78 const extension
= pathUtils
.extname(path
)
79 const basename
= pathUtils
.basename(path
, extension
)
87 if (!webtorrent
.silent
) logger
.debug('Node wants to seed %s.', data
._id
)
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
)
94 // This is a fake object, we just use the magnetUri in this project
96 magnetURI: received
.magnetUri
99 ipc
.server
.off(eventKey
, '*')
103 ipc
.server
.broadcast(processKey
+ '.seed', data
)
106 function add (magnetUri
, callback
) {
114 if (!webtorrent
.silent
) logger
.debug('Node wants to add ' + data
._id
)
117 const eventKey
= nodeKey
+ '.addDone.' + data
._id
118 ipc
.server
.on(eventKey
, function (received
) {
119 if (!webtorrent
.silent
) logger
.debug('Process added torrent.')
121 // This is a fake object, we just use the magnetUri in this project
123 files: received
.files
126 ipc
.server
.off(eventKey
, '*')
130 ipc
.server
.broadcast(processKey
+ '.add', data
)
133 function remove (magnetUri
, callback
) {
141 if (!webtorrent
.silent
) logger
.debug('Node wants to stop seeding %s.', data
._id
)
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
)
149 if (received
.err
) err
= received
.err
151 ipc
.server
.off(eventKey
, '*')
155 ipc
.server
.broadcast(processKey
+ '.remove', data
)
158 // ---------------------------------------------------------------------------
160 module
.exports
= webtorrent