forked from mafintosh/peerflix
-
Notifications
You must be signed in to change notification settings - Fork 130
/
server.js
130 lines (105 loc) · 3.61 KB
/
server.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
var http = require('http');
var rangeParser = require('range-parser');
var mime = require('mime');
var Readable = require('readable-stream');
var util = require('util');
var MIN_BUFFER = 1.5 * 1000 * 1000;
var pipeline = function(inp, out) {
inp.pipe(out);
out.on('close', function() {
inp.destroy();
});
};
module.exports = function(storage, file, options) {
var dest = storage.dest;
var missing = storage.missing;
var torrent = storage.torrent;
var piecesToBuffer = Math.ceil((options.buffer || MIN_BUFFER) / torrent.pieceLength);
var start = (file.offset / torrent.pieceLength) | 0;
var end = ((file.offset + file.length + 1) / torrent.pieceLength) | 0;
var PieceStream = function(range) {
Readable.call(this);
range = range || {start:0, end:file.length-1};
this.position = ((range.start + file.offset) / torrent.pieceLength) | 0;
this.remaining = range.end - range.start + 1;
this.skip = (range.start + file.offset) % torrent.pieceLength;
this.destroyed = false;
this._buffer = this.position + Math.min(piecesToBuffer, (this.remaining / torrent.pieceLength) | 0);
this._onreadable = null;
server.position = this.position;
};
util.inherits(PieceStream, Readable);
PieceStream.prototype._read = function() {
if (!this.remaining) return this.push(null);
var self = this;
var onread = function(err, data) {
if (err) return self.emit('error', err);
if (self.skip) data = data.slice(self.skip);
if (data.length > self.remaining) data = data.slice(0, self.remaining);
self.skip = 0;
self.remaining -= data.length;
if (self.destroyed) return;
self.push(data);
server.position = self.position;
};
if (!this.buffering()) return dest.read(this.position++ - start, onread);
this._onreadable = function(index) {
if (self.buffering()) return;
dest.removeListener('readable', self._onreadable);
dest.read(self.position++ - start, onread);
server.position = self.position;
};
dest.on('readable', this._onreadable);
};
PieceStream.prototype.buffering = function() {
for (var i = this.position; i < this._buffer; i++) {
if (!dest.readable(i - start)) return true;
}
return !dest.readable(this.position - start);
};
PieceStream.prototype.destroy = function() {
this.destroyed = true;
if (this._onreadable) dest.removeListener('readable', this._onreadable);
this.emit('close');
};
var isAvi = /\.avi$/i.test(file.name);
var prioritize = function(i) {
missing.sort(function(a, b) {
if (a === end && !isAvi) return -1;
if (b === end && !isAvi) return 1;
if (a >= i && b < i) return -1;
if (b >= i && a < i) return 1;
return a - b;
});
};
var stream = function(range) {
var s = new PieceStream(range);
prioritize(s.position);
return s;
};
var server = http.createServer(function(request, response) {
var range = request.headers.range;
request.connection.setTimeout(0);
response.setHeader('Accept-Ranges', 'bytes');
response.setHeader('Content-Type', mime.lookup(file.name));
range = range && rangeParser(file.length, range)[0];
if (!range) {
response.setHeader('Content-Length', file.length);
if (request.method === 'HEAD') return response.end();
pipeline(stream(), response);
return;
}
response.statusCode = 206;
response.setHeader('Content-Length', range.end - range.start + 1);
response.setHeader('Content-Range', 'bytes '+range.start+'-'+range.end+'/'+file.length);
if (request.method === 'HEAD') return response.end();
pipeline(stream(range), response);
});
server.position = 0;
prioritize(0);
server.listen(options.port || 8888);
server.on('error', function() {
server.listen(0);
});
return server;
};