-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcdn.js
137 lines (114 loc) · 3.69 KB
/
cdn.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
var _ = require('lodash');
var async = require('async');
var fs = require('fs');
var http = require('http');
var watch = require('watch');
var sass = require("node-sass");
var path = require('path');
var utils;
module.exports = {
name:'cdn',
description:'Aggregates all the static assets across all microservices and serves them via a pseudo local CDN url',
example:'bosco cdn <minify>',
cmd:cmd
}
function cmd(bosco, args) {
var minify = _.contains(args,'minify');
var port = bosco.config.get('cdn:port') || 7334;
var serverUrl = "http://localhost:" + port + "/";
bosco.log("Starting pseudo CDN on port: " + (port+"").blue);
var repos = bosco.config.get('github:repos');
if(!repos) return bosco.error("You are repo-less :( You need to initialise bosco first, try 'bosco fly'.");
var startServer = function(staticAssets, serverPort) {
var server = http.createServer(function(request, response) {
var url = request.url.replace("/","");
if(staticAssets[url]) {
response.writeHead(200, {
"Content-Type": "text/" + staticAssets[url].type,
"Cache-Control": "no-cache, must-revalidate",
"Pragma": "no-cache",
"Expires": "Sat, 21 May 1952 00:00:00 GMT"
});
getContent(staticAssets[url], function(err, content) {
if(err) {
response.writeHead(500, {"Content-Type": "text/html"});
response.end("<h2>There was an error: " + err.message + "</h2>");
} else {
response.end(content);
}
})
} else {
response.writeHead(404, {"Content-Type": "text/html"});
response.end(staticAssets.formattedAssets);
}
});
server.listen(serverPort);
bosco.log("Server is listening on " + serverPort);
}
var startMonitor = function(staticAssets) {
var watchSet = {};
_.forOwn(staticAssets, function(asset, key) {
if(!minify) return watchSet[asset.path] = key;
if(asset.extname == ".manifest") {
var manifestKey = key,
manifestFile,
manifestFiles = asset.files;
manifestFiles.forEach(function(file) {
if(file) watchSet[file.path] = asset.tag;
});
}
});
watch.createMonitor(bosco.getOrgPath(), {ignoreDirectoryPattern: /node_modules/, interval: 50}, function (monitor) {
monitor.on("changed", function (f, curr, prev) {
var fileKey = watchSet[f];
if(!minify) {
if(fileKey) {
staticAssets[fileKey].content = fs.readFileSync(staticAssets[fileKey].path);
bosco.log("Reloaded " + fileKey);
}
} else {
if(fileKey) {
bosco.log('Recompiling tag ' + fileKey.blue + ' due to change in ' + f.blue);
var options = {
repos: repos,
minify: minify,
tagFilter: fileKey,
watchBuilds: false,
reloadOnly: true
}
bosco.staticUtils.getStaticAssets(options, function(err, updatedAssets) {
// Clear old for tag
_.forOwn(staticAssets, function(value, key) {
if(value.tag == fileKey) delete staticAssets[key];
});
// Add new
_.forOwn(updatedAssets, function(value, key) {
staticAssets[key] = value;
});
bosco.log("Reloaded minified assets for tag " + fileKey.blue);
});
}
}
})
});
}
var getContent = function(asset, next) {
if(asset.extname == '.scss') {
sass.render(asset.content, next);
} else {
next(null, asset.content);
}
}
if(minify) bosco.log("Minifying front end assets, this can take some time ...");
var options = {
repos: repos,
minify: minify,
tagFilter: null,
watchBuilds: true,
reloadOnly: false
}
bosco.staticUtils.getStaticAssets(options, function(err, staticAssets) {
startServer(staticAssets, port);
startMonitor(staticAssets);
});
}