]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/webtorrent.js
Electron-prebuilt -> electron
[github/Chocobozzz/PeerTube.git] / server / lib / webtorrent.js
1 'use strict'
2
3 const ipc = require('node-ipc')
4 const pathUtils = require('path')
5 const spawn = require('electron-spawn')
6
7 const constants = require('../initializers/constants')
8 const logger = require('../helpers/logger')
9
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
16
17 const 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
26 function create (options, callback) {
27 if (typeof options === 'function') {
28 callback = options
29 options = {}
30 }
31
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 }
40
41 ipc.serve(function () {
42 if (!webtorrent.silent) logger.info('IPC server ready.')
43
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 npm package installed with xvfb-run.')
47 }, 30000)
48
49 ipc.server.on(processKey + '.ready', function () {
50 if (!webtorrent.silent) logger.info('Webtorrent process ready.')
51 clearTimeout(timeoutWebtorrentProcess)
52 callback()
53 })
54
55 ipc.server.on(processKey + '.exception', function (data) {
56 throw new Error('Received exception error from webtorrent process : ' + data.exception)
57 })
58
59 const webtorrentProcess = spawn(pathUtils.join(__dirname, 'webtorrent-process.js'), host, port, { detached: true })
60
61 if (constants.CONFIG.ELECTRON.DEBUG === true) {
62 webtorrentProcess.stderr.on('data', function (data) {
63 logger.debug('Webtorrent process stderr: ', data.toString())
64 })
65
66 webtorrentProcess.stdout.on('data', function (data) {
67 logger.debug('Webtorrent process:', data.toString())
68 })
69 }
70
71 webtorrent.app = webtorrentProcess
72 })
73
74 ipc.server.start()
75 }
76
77 function seed (path, callback) {
78 const extension = pathUtils.extname(path)
79 const basename = pathUtils.basename(path, extension)
80 const data = {
81 _id: basename,
82 args: {
83 path: path
84 }
85 }
86
87 if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id)
88
89 // Finish signal
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)
93
94 // This is a fake object, we just use the magnetUri in this project
95 const torrent = {
96 magnetURI: received.magnetUri
97 }
98
99 ipc.server.off(eventKey, '*')
100 callback(torrent)
101 })
102
103 ipc.server.broadcast(processKey + '.seed', data)
104 }
105
106 function add (magnetUri, callback) {
107 const data = {
108 _id: magnetUri,
109 args: {
110 magnetUri: magnetUri
111 }
112 }
113
114 if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id)
115
116 // Finish signal
117 const eventKey = nodeKey + '.addDone.' + data._id
118 ipc.server.on(eventKey, function (received) {
119 if (!webtorrent.silent) logger.debug('Process added torrent.')
120
121 // This is a fake object, we just use the magnetUri in this project
122 const torrent = {
123 files: received.files
124 }
125
126 ipc.server.off(eventKey, '*')
127 callback(torrent)
128 })
129
130 ipc.server.broadcast(processKey + '.add', data)
131 }
132
133 function remove (magnetUri, callback) {
134 const data = {
135 _id: magnetUri,
136 args: {
137 magnetUri: magnetUri
138 }
139 }
140
141 if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id)
142
143 // Finish signal
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)
147
148 let err = null
149 if (received.err) err = received.err
150
151 ipc.server.off(eventKey, '*')
152 callback(err)
153 })
154
155 ipc.server.broadcast(processKey + '.remove', data)
156 }
157
158 // ---------------------------------------------------------------------------
159
160 module.exports = webtorrent