forked from acuminous/bosco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdn.js
247 lines (212 loc) · 7.62 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
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
242
243
244
245
246
247
var _ = require('lodash');
var async = require('async');
var fs = require('fs');
var path = require('path');
var http = require('http');
var watch = require('watch');
var url = require('url');
var browserSync = require('browser-sync');
var RunListHelper = require('../src/RunListHelper');
var CmdHelper = require('../src/CmdHelper');
module.exports = {
name: 'cdn',
usage: '[-r <repoPattern>] [-w <repoPattern>] [<minify>]',
description: 'Aggregates all the static assets across all microservices and serves them via a pseudo local CDN url',
requiresNvm: true,
options: [{
name: 'tag',
alias: 't',
type: 'string',
desc: 'Filter by a tag defined within bosco-service.json',
},
{
name: 'watch',
alias: 'w',
type: 'string',
desc: 'Filter by a regex of services to watch (similar to -r in run)',
}],
};
function cmd(bosco, args) {
var minify = _.contains(args, 'minify');
var port = bosco.config.get('cdn:port') || 7334;
var repoPattern = bosco.options.repo;
var repoRegex = new RegExp(repoPattern);
var watchPattern = bosco.options.watch || '$a';
var watchRegex = new RegExp(watchPattern);
var repoTag = bosco.options.tag;
var bs = browserSync.create();
var repos;
bosco.log('Starting pseudo CDN on port: ' + (port + '').blue);
if (bosco.options.list) {
repos = bosco.options.list.split(',');
} else {
if (CmdHelper.checkInService(bosco)) {
bosco.options.watch = bosco.options.watch || new RegExp(path.basename(process.cwd()));
watchRegex = new RegExp(bosco.options.watch);
}
repos = bosco.getRepos();
}
if (!repos) return bosco.error('You are repo-less :( You need to initialise bosco first, try \'bosco clone\'.');
function getRunList(next) {
RunListHelper.getRunList(bosco, repos, repoRegex, watchRegex, repoTag, next);
}
function startServer(staticAssets, staticRepos, serverPort) {
var isWatchedFile = function(assetPath) {
var watched;
try {
watched = assetPath && !fs.lstatSync(assetPath).isDirectory() && assetPath.match(watchRegex);
} catch (ex) {
watched = false;
}
return watched;
};
function getAsset(assetUrl) {
var key = assetUrl.replace('/', '');
return _.find(staticAssets, 'assetKey', key);
}
var server = http.createServer(function(request, response) {
if (request.method === 'OPTIONS') {
response.writeHead(200, {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Credentials': true,
'Access-Control-Max-Age': '86400', // 24 hours
'Access-Control-Allow-Headers': 'X-Requested-With, Access-Control-Allow-Origin, X-HTTP-Method-Override, Content-Type, Authorization, Accept',
});
return response.end();
}
var pathname = url.parse(request.url).pathname;
if (pathname === '/repos') {
response.writeHead(200, {'Content-Type': 'text/html'});
return response.end(staticRepos.formattedRepos);
}
var asset = getAsset(pathname);
if (!asset) {
response.writeHead(404, {'Content-Type': 'text/html'});
return response.end(staticAssets.formattedAssets);
}
response.writeHead(200, {
'Content-Type': asset.mimeType,
'Cache-Control': 'no-cache, must-revalidate',
'Pragma': 'no-cache',
'Expires': 'Sat, 21 May 1952 00:00:00 GMT',
'Access-Control-Allow-Origin': '*',
});
if (isWatchedFile(asset.path)) {
fs.readFile(asset.path, function(err, content) {
response.end(content);
});
} else {
response.end(asset.data || asset.content);
}
});
server.listen(serverPort);
if (bosco.options['browser-sync']) {
var assets = _.filter(_.map(staticAssets, 'path'), isWatchedFile);
var extraFiles = _.filter(_.uniq(_.flattenDeep(_.map(staticAssets, 'extraFiles'))));
var assetsToWatch = _.union(assets, extraFiles);
bs.init({
proxy: bosco.options['browser-sync-proxy'],
files: assetsToWatch,
reloadDelay: bosco.options['browser-sync-delay'],
});
}
bosco.log('Server is listening on ' + serverPort);
}
function startMonitor(staticAssets) {
var watchSet = {};
var reloading = {};
_.forEach(staticAssets, function(asset) {
if (asset.repo && !asset.repo.match(watchRegex)) return;
if (minify && asset.extname === '.manifest') {
asset.files.forEach(function(file) {
if (file) watchSet[file.path] = asset.tag;
});
return;
}
if (asset.path) watchSet[asset.path] = asset.assetKey;
});
function filterFn(f, stat) {
return f.match(watchRegex) && stat.isDirectory() || watchSet[f];
}
function getIndexForKey(assetList, fileKey) {
var find = (_.isObject(assetList)) ? _.findKey : _.findIndex;
return find(assetList, 'assetKey', fileKey);
}
function reloadFile(fileKey) {
if (!fileKey) return;
if (!minify) {
var assetIndex = getIndexForKey(staticAssets, fileKey);
if (!assetIndex) {
bosco.error('Unable to locate asset with key: ' + fileKey);
return;
}
fs.readFile(staticAssets[assetIndex].path, function(err, data) {
if (err) {
bosco.log('Error reloading ' + fileKey);
bosco.log(err.toString());
return;
}
staticAssets[assetIndex].data = data;
staticAssets[assetIndex].content = data.toString();
bosco.log('Reloaded ' + fileKey);
reloading[fileKey] = false;
});
return;
}
bosco.log('Recompiling tag ' + fileKey.blue);
var staticAssetOptions = {
repos: repos,
minify: minify,
buildNumber: 'local',
tagFilter: fileKey,
watchBuilds: false,
reloadOnly: true,
};
bosco.staticUtils.getStaticAssets(staticAssetOptions, function(err, updatedAssets) {
_.forEach(updatedAssets, function(value) {
var index = getIndexForKey(staticAssets, value.assetKey);
staticAssets[index] = value;
});
bosco.log('Reloaded minified assets for tag ' + fileKey.blue);
reloading[fileKey] = false;
});
}
watch.createMonitor(bosco.getOrgPath(), {filter: filterFn, ignoreDotFiles: true, ignoreUnreadableDir: true, ignoreDirectoryPattern: /node_modules|\.git|coverage/, interval: 1000}, function(monitor) {
bosco.log('Watching ' + _.keys(monitor.files).length + ' files ...');
function onChange(f) {
var fileKey = watchSet[f];
if (reloading[fileKey]) return;
reloading[fileKey] = true;
reloadFile(fileKey);
}
monitor.on('changed', onChange);
monitor.on('created', onChange);
});
}
if (minify) bosco.log('Minifying front end assets, this can take some time ...');
getRunList(function(err, repoList) {
var repoNames = _.map(repoList, 'name');
var options = {
repos: repoNames,
buildNumber: 'local',
minify: minify,
tagFilter: null,
watchBuilds: true,
reloadOnly: false,
ignoreFailure: true,
watchRegex: watchRegex,
repoRegex: repoRegex,
repoTag: repoTag,
};
var executeAsync = {
staticAssets: bosco.staticUtils.getStaticAssets.bind(null, options),
staticRepos: bosco.staticUtils.getStaticRepos.bind(null, options),
};
async.parallel(executeAsync, function(err, results) {
startServer(results.staticAssets, results.staticRepos, port);
startMonitor(results.staticAssets);
});
});
}
module.exports.cmd = cmd;