forked from alexeisavca/webtorrentapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
241 lines (217 loc) · 7.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
var WebTorrent = require('webtorrent');
var promisedRequest = require('./lib/promised-request.js');
var Buffer = require('buffer/').Buffer;
var localforage = require('localforage');
var Q = require('q');
var debug = require('debug');
var log = debug('webtorrentapp');
var FileStream = require('webtorrent/lib/file-stream');
var ReadableStream = require('stream').Readable;
function extractModuleExports(script){
return eval.call(window, '(function(module){' + script + ';return module.exports})({})');
}
module.exports = function(config){
var appFiles = ['index.js'].concat(config.files || []);
var cacheFiles = config.cache;
var path = config.path || '';
var seedTimeoutMs = config.seedTimeout || 5000;
var appName = config.name || 'Just another WebTorrent app';
var restoreFromCache = "boolean" == typeof config.restoreFromCache ? config.restoreFromCache : true;
var promisedFiles = {};
appFiles.forEach(function(file){
promisedFiles[file] = Q.defer();
});
var torrentPromise = Q.defer();
function findFileInTorrent(filename, torrent){
var index;
for(index in torrent.files){
var file = torrent.files[index];
if(file.name == filename){
return file;
}
}
}
function requestFile(filename){
if(torrentPromise){
torrentPromise.promise.then(function(torrent){
findFileInTorrent(filename, torrent).getBuffer(function(err, buffer){
promisedFiles[filename].resolve(buffer);
});
});
}
return promisedFiles[filename].promise;
}
function requestBlobUrl(filename){
var promise = Q.defer();
requestFile(filename).then(function(buffer){
promise.resolve(URL.createObjectURL(new Blob([buffer])));
});
return promise.promise;
}
function requestStream(filename){
var promise = Q.defer();
if(torrentPromise){
torrentPromise.promise.then(function(torrent){
promise.resolve(findFileInTorrent(filename, torrent).createReadStream())
})
}
requestFile(filename).then(function(file){
var stream = ReadableStream();
stream.push(file);
stream.push(null);
stream.pipe = FileStream.prototype.pipe.bind(stream);
promise.resolve(stream);
});
return promise.promise;
}
function requestExternalStyle(url){
var ss = document.createElement("link");
ss.type = "text/css";
ss.rel = "stylesheet";
ss.href = url;
document.head.appendChild(ss);
}
function requestExternalScript(url){
var ss = document.createElement("script");
ss.src = url;
document.head.appendChild(ss);
}
function requestStyle(filename){
return requestBlobUrl(filename).then(requestExternalStyle);
}
function requestScript(filename){
return requestBlobUrl(filename).then(requestExternalScript);
}
function requestModule(filename){
return requestFile(filename).then(extractModuleExports);
}
function launchApp(script){
var f = extractModuleExports(script);
f({
requestFile: requestFile,
requestBlobUrl: requestBlobUrl,
requestStream: requestStream,
requestExternalStyle: requestExternalStyle,
requestExternalScript: requestExternalScript,
requestStyle: requestStyle,
requestScript: requestScript,
requestModule: requestModule
})
}
if(restoreFromCache){
log("Checking cache");
localforage.getItem(appName).then(function(cache){
Object.keys(cache.files).forEach(function(filename){
promisedFiles[filename].resolve(new Buffer(cache.files[filename]))
});
log("Successfully restored from cache")
}).catch(log.bind(log, "Restoring from cache failed"));
} else {
log("Restoring from cache disabled by developer. Skipping");
}
function isCacheOutdated(torrentId){
var deferred = Q.defer();
localforage.getItem(appName).then(function(cache){
deferred.resolve(cache.torrentId != torrentId);
}).catch(function(){
deferred.resolve(true)
});
return deferred.promise;
}
function updateCache(filePromises, torrentId){
log("Updating cache");
var cache = {};
cacheFiles.forEach(function(filename){
filePromises[filename].then(function(content){
cache[filename] = content;
});
});
Q.all(cacheFiles.map(function(name){
return filePromises[name];
})).then(function(){
localforage.setItem(appName, {
torrentId: torrentId,
files: cache
}, function(){
log('cache updated');
});
});
}
var client = new WebTorrent();
function seed(){
if(config.torrent){
client.remove(config.torrent);
}
torrentPromise = null;
var fileNames = Object.keys(promisedFiles);
var totalFiles = 0;
var downloadedFiles = {};
fileNames.forEach( function(key){
downloadedFiles[key] = promisedRequest(path + key)
downloadedFiles[key].then(function(){
totalFiles++;
log('Downloaded file ' + totalFiles + ' out of ' + appFiles.length, key);
});
downloadedFiles[key].then(promisedFiles[key].resolve);
});
log('Preparing to seed.');
Q.all(fileNames.map(function(name){
return downloadedFiles[name];
})).then(function(files){
log('All files have been downloaded. Attempting to seed.');
try{
client.seed( files.map(function(body, index){
var buffer = body;
buffer.name = fileNames[index];
return buffer;
}), {
name: appName
}, function(torrent){
log('Successfully started seeding. Infohash: %c%s %cMagnet: %c%s', 'color: blue', torrent.infoHash, 'color:black', 'color:blue', torrent.magnetURI);
isCacheOutdated(torrent.infoHash).then(function(outdated){
if(outdated){
updateCache(downloadedFiles, torrent.infoHash);
}
});
});
}catch(e){
log('Oops!', e);
}
});
}
var seedTimeout = setTimeout(function() {
log('Download timeout expired.');
seed();
}, seedTimeoutMs);
if(config.torrent){
log('Connecting to the torrent %c%s', 'color:blue', config.torrent);
client.add(config.torrent, {}, function(torrent){
clearTimeout(seedTimeout);
if(torrentPromise){
torrentPromise.resolve(torrent);
} else {
return;
}
log('Connected!');
isCacheOutdated(torrent.infoHash).then(function(outdated){
if(outdated){
var downloadedFiles = {};
var totalFiles = 0;
cacheFiles.forEach( function(filename){
downloadedFiles[filename] = downloadTextFile(filename);
downloadedFiles[filename].then(function(){
totalFiles++;
log('Downloaded file ' + totalFiles + ' out of ' + cacheFiles.length, filename);
});
});
updateCache(downloadedFiles, torrent.infoHash);
}
});
});
} else {
clearTimeout(seedTimeout);
log('No torrent provided. Skipping download.');
seed();
}
requestFile('index.js').then(launchApp);
};