]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - lib/webtorrent.js
Require on the top of the files
[github/Chocobozzz/PeerTube.git] / lib / webtorrent.js
1 'use strict'
2
3 var config = require('config')
4 var ipc = require('node-ipc')
5 var pathUtils = require('path')
6 var spawn = require('electron-spawn')
7
8 var logger = require('../helpers/logger')
9
10 var host = config.get('webserver.host')
11 var port = config.get('webserver.port')
12 var nodeKey = 'webtorrentnode' + port
13 var processKey = 'webtorrentprocess' + port
14 ipc.config.silent = true
15 ipc.config.id = nodeKey
16
17 var 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 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)
49
50 ipc.server.on(processKey + '.ready', function () {
51 if (!webtorrent.silent) logger.info('Webtorrent process ready.')
52 clearTimeout(timeout_webtorrent_process)
53 callback()
54 })
55
56 ipc.server.on(processKey + '.exception', function (data) {
57 logger.error('Received exception error from webtorrent process.', { exception: data.exception })
58 process.exit()
59 })
60
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 })
65
66 webtorrent_process.stdout.on('data', function (data) {
67 // logger.debug('Webtorrent process:', data.toString())
68 })
69
70 webtorrent.app = webtorrent_process
71 })
72
73 ipc.server.start()
74 }
75
76 function 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
83 }
84 }
85
86 if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id)
87
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)
92
93 // This is a fake object, we just use the magnetUri in this project
94 var torrent = {
95 magnetURI: received.magnetUri
96 }
97
98 ipc.server.off(event_key)
99 callback(torrent)
100 })
101
102 ipc.server.broadcast(processKey + '.seed', data)
103 }
104
105 function add (magnetUri, callback) {
106 var data = {
107 _id: magnetUri,
108 args: {
109 magnetUri: magnetUri
110 }
111 }
112
113 if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id)
114
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.')
119
120 // This is a fake object, we just use the magnetUri in this project
121 var torrent = {
122 files: received.files
123 }
124
125 ipc.server.off(event_key)
126 callback(torrent)
127 })
128
129 ipc.server.broadcast(processKey + '.add', data)
130 }
131
132 function remove (magnetUri, callback) {
133 var data = {
134 _id: magnetUri,
135 args: {
136 magnetUri: magnetUri
137 }
138 }
139
140 if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id)
141
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)
146
147 var err = null
148 if (received.err) err = received.err
149
150 ipc.server.off(event_key)
151 callback(err)
152 })
153
154 ipc.server.broadcast(processKey + '.remove', data)
155 }
156
157 // ---------------------------------------------------------------------------
158
159 module.exports = webtorrent