aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/webtorrent.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-03-07 11:33:59 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-03-07 11:33:59 +0100
commitb9a3e09ad5a7673f64556d1dba122ed4c4fac980 (patch)
tree66d4928b82af19a2372a2505822233884f3fd471 /lib/webtorrent.js
parentb2ff5e3e686eb552c5ccd64ce67b0455972ceef0 (diff)
downloadPeerTube-b9a3e09ad5a7673f64556d1dba122ed4c4fac980.tar.gz
PeerTube-b9a3e09ad5a7673f64556d1dba122ed4c4fac980.tar.zst
PeerTube-b9a3e09ad5a7673f64556d1dba122ed4c4fac980.zip
Prepare folders structure for angular app
Diffstat (limited to 'lib/webtorrent.js')
-rw-r--r--lib/webtorrent.js157
1 files changed, 0 insertions, 157 deletions
diff --git a/lib/webtorrent.js b/lib/webtorrent.js
deleted file mode 100644
index cb641fead..000000000
--- a/lib/webtorrent.js
+++ /dev/null
@@ -1,157 +0,0 @@
1'use strict'
2
3var config = require('config')
4var ipc = require('node-ipc')
5var pathUtils = require('path')
6var spawn = require('electron-spawn')
7
8var logger = require('../helpers/logger')
9
10var host = config.get('webserver.host')
11var port = config.get('webserver.port')
12var nodeKey = 'webtorrentnode' + port
13var processKey = 'webtorrentprocess' + port
14ipc.config.silent = true
15ipc.config.id = nodeKey
16
17var 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
26function 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 throw new Error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt 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(timeout_webtorrent_process)
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 var webtorrent_process = spawn(pathUtils.join(__dirname, 'webtorrentProcess.js'), host, port, { detached: true })
60 webtorrent_process.stderr.on('data', function (data) {
61 // logger.debug('Webtorrent process stderr: ', data.toString())
62 })
63
64 webtorrent_process.stdout.on('data', function (data) {
65 // logger.debug('Webtorrent process:', data.toString())
66 })
67
68 webtorrent.app = webtorrent_process
69 })
70
71 ipc.server.start()
72}
73
74function seed (path, callback) {
75 var extension = pathUtils.extname(path)
76 var basename = pathUtils.basename(path, extension)
77 var data = {
78 _id: basename,
79 args: {
80 path: path
81 }
82 }
83
84 if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id)
85
86 // Finish signal
87 var event_key = nodeKey + '.seedDone.' + data._id
88 ipc.server.on(event_key, function listener (received) {
89 if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', 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 ipc.server.off(event_key)
97 callback(torrent)
98 })
99
100 ipc.server.broadcast(processKey + '.seed', data)
101}
102
103function add (magnetUri, callback) {
104 var data = {
105 _id: magnetUri,
106 args: {
107 magnetUri: magnetUri
108 }
109 }
110
111 if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id)
112
113 // Finish signal
114 var event_key = nodeKey + '.addDone.' + data._id
115 ipc.server.on(event_key, function (received) {
116 if (!webtorrent.silent) logger.debug('Process added torrent.')
117
118 // This is a fake object, we just use the magnetUri in this project
119 var torrent = {
120 files: received.files
121 }
122
123 ipc.server.off(event_key)
124 callback(torrent)
125 })
126
127 ipc.server.broadcast(processKey + '.add', data)
128}
129
130function remove (magnetUri, callback) {
131 var data = {
132 _id: magnetUri,
133 args: {
134 magnetUri: magnetUri
135 }
136 }
137
138 if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id)
139
140 // Finish signal
141 var event_key = nodeKey + '.removeDone.' + data._id
142 ipc.server.on(event_key, function (received) {
143 if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id)
144
145 var err = null
146 if (received.err) err = received.err
147
148 ipc.server.off(event_key)
149 callback(err)
150 })
151
152 ipc.server.broadcast(processKey + '.remove', data)
153}
154
155// ---------------------------------------------------------------------------
156
157module.exports = webtorrent