]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - src/webTorrentNode.js
update readme dependencies
[github/Chocobozzz/PeerTube.git] / src / webTorrentNode.js
CommitLineData
8c308c2b
C
1;(function () {
2 'use strict'
3
4 var spawn = require('electron-spawn')
5 var config = require('config')
6 var ipc = require('node-ipc')
7 var pathUtils = require('path')
8
9 var logger = require('./logger')
10
77c2df95
C
11 var host = config.get('webserver.host')
12 var port = config.get('webserver.port')
8c308c2b
C
13
14 var nodeKey = 'webtorrentnode' + port
15 var processKey = 'webtorrent' + port
16
17 ipc.config.silent = true
18 ipc.config.id = nodeKey
19
20 var webtorrentnode = {}
21
22 // Useful for beautiful tests
23 webtorrentnode.silent = false
24
25 // Useful to kill it
26 webtorrentnode.app = null
27
77c2df95
C
28 webtorrentnode.create = function (options, callback) {
29 if (typeof options === 'function') {
30 callback = options
31 options = {}
32 }
33
34 // Override options
35 if (options.host) host = options.host
36 if (options.port) {
37 port = options.port
38 nodeKey = 'webtorrentnode' + port
39 processKey = 'webtorrent' + port
40 ipc.config.id = nodeKey
41 }
42
8c308c2b
C
43 ipc.serve(function () {
44 if (!webtorrentnode.silent) logger.info('IPC server ready.')
45
46 ipc.server.on(processKey + '.ready', function () {
47 if (!webtorrentnode.silent) logger.info('Webtorrent process ready.')
48 callback()
49 })
50
51 var webtorrent_process = spawn(__dirname + '/webtorrent.js', host, port, { detached: true })
52 webtorrent_process.stderr.on('data', function (data) {
53 // logger.debug('Webtorrent process stderr: ', data.toString())
54 })
55
56 webtorrent_process.stdout.on('data', function (data) {
57 // logger.debug('Webtorrent process:', data.toString())
58 })
59
60 function exitChildProcess () {
61 if (!webtorrentnode.silent) logger.info('Gracefully exit child')
62 process.kill(-webtorrent_process.pid)
63 process.exit(0)
64 }
65
66 process.on('SIGINT', exitChildProcess)
67 process.on('SIGTERM', exitChildProcess)
68
69 webtorrentnode.app = webtorrent_process
70 })
71
72 ipc.server.start()
73 }
74
75 webtorrentnode.seed = function (path, callback) {
76 var extension = pathUtils.extname(path)
77 var basename = pathUtils.basename(path, extension)
78 var data = {
79 _id: basename,
80 args: {
81 path: path
82 }
83 }
84
85 if (!webtorrentnode.silent) logger.debug('Node wants to seed ' + data._id)
86
87 // Finish signal
88 ipc.server.on(nodeKey + '.seedDone.' + data._id, function (received) {
89 if (!webtorrentnode.silent) logger.debug('Process seeded torrent ' + received.magnetUri)
90
91 // This is a fake object, we just use the magnetUri in this project
92 var torrent = {
93 magnetURI: received.magnetUri
94 }
95
96 callback(torrent)
97 })
98
99 ipc.server.broadcast(processKey + '.seed', data)
100 }
101
102 webtorrentnode.add = function (magnetUri, callback) {
103 var data = {
104 _id: magnetUri,
105 args: {
106 magnetUri: magnetUri
107 }
108 }
109
110 if (!webtorrentnode.silent) logger.debug('Node wants to add ' + data._id)
111
112 // Finish signal
113 ipc.server.on(nodeKey + '.addDone.' + data._id, function (received) {
114 if (!webtorrentnode.silent) logger.debug('Process added torrent')
115
116 // This is a fake object, we just use the magnetUri in this project
117 var torrent = {
118 files: received.files
119 }
120
121 callback(torrent)
122 })
123
124 ipc.server.broadcast(processKey + '.add', data)
125 }
126
127 webtorrentnode.remove = function (magnetUri, callback) {
128 var data = {
129 _id: magnetUri,
130 args: {
131 magnetUri: magnetUri
132 }
133 }
134
135 if (!webtorrentnode.silent) logger.debug('Node wants to stop seeding ' + data._id)
136
137 // Finish signal
138 ipc.server.on(nodeKey + '.removeDone.' + data._id, function (received) {
139 if (!webtorrentnode.silent) logger.debug('Process removed torrent ' + data._id)
140
141 var err = null
142 if (received.err) err = received.err
143
144 callback(err)
145 })
146
147 ipc.server.broadcast(processKey + '.remove', data)
148 }
149
150 module.exports = webtorrentnode
151})()