-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassetLoader.js
More file actions
104 lines (94 loc) · 3.7 KB
/
assetLoader.js
File metadata and controls
104 lines (94 loc) · 3.7 KB
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
var url = require('url'),
http = require('http'),
AverageRing = require('./averageRing.js'),
assetDownload = require('./assetDownload.js'),
mime = require('./mimeTypeHelper.js');
var mod = function(options) {
var cacheHandler = options.cacheHandler;
var that = this;
var activeDownloads = {};
var callbacks = {};
var downloadCount = 0;
var downloadTimeRing = new AverageRing(500, false);
that.download = function(host, url, callback) {
cacheHandler.getFileBufferAndType(url, function(err, buffer, mimeType) {
if (err) {
_download(host, url, callback);
} else {
options.stats.assetsServed++;
callback(buffer, mimeType, 200);
}
});
};
var _download = function(host, url, callback) {
if (!activeDownloads[url]) {
callbacks[url] = [callback];
var start = new Date();
activeDownloads[url] = new assetDownload(
host, url, options,
function() {
downloadTimeRing.add(new Date() - start);
onDownloadComplete.apply({}, arguments);
}
);
} else {
callbacks[url].push(callback);
activeDownloads[url].addMirror(host);
}
};
var onDownloadComplete = function(url, buffer, httpStatusCode, mimeType) {
var onMimeRequestResponse = function(err, mimeType) {
for (var i in callbacks[url]) {
callbacks[url][i](buffer, err ? 'text/html' : mimeType, httpStatusCode);
options.stats.assetsServed++;
}
delete callbacks[url];
delete activeDownloads[url];
if (err) {
options.logger.error("requesting mime type", err);
}
if (err ||
mimeType.search(/application/) != -1 ||
mimeType.search(/text/) != -1 ||
httpStatusCode < 200 ||
httpStatusCode >= 300 ||
buffer.length === 0) {
options.stats.soupErrors++;
} else {
process.nextTick(function() {
cacheHandler.insertFileBuffer(url, buffer, mimeType, function(err) {
if (!err) {
options.eventBus.emit('newAsset', url, buffer, mimeType);
downloadCount++;
}
});
});
}
}
if (mimeType) {
onMimeRequestResponse(null, mimeType)
} else {
mime.getBufferMimeType(buffer, onMimeRequestResponse);
}
};
that.getStatus = function() {
var servedCount = options.stats.assetsServed;
var status = "";
status += "assets served: " + servedCount + "\n";
status += "downloaded: " + downloadCount + "\n";
status += "cache efficiency: " + Math.round((servedCount / downloadCount) * 1000) / 1000 + "\n";
status += "average soup download time: " + downloadTimeRing.getAverage(2) + "ms\n";
if (Object.keys(activeDownloads).length > 0) {
status += "\n";
status += "active downloads (" + Object.keys(activeDownloads).length + "):" + "\n";
var processed = 0;
for (var i in activeDownloads) {
var lineend = Object.keys(activeDownloads).length - 1 == processed ? "" : "\n";
status += callbacks[i].length + " clients: " + activeDownloads[i].getStatus() + lineend;
processed++;
}
}
return status;
};
};
module.exports = mod;