-
Notifications
You must be signed in to change notification settings - Fork 27
/
service.js
269 lines (252 loc) · 7.7 KB
/
service.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*!
* Cube services, start a http server, or return a middleware
*/
'use strict';
const xfs = require('xfs');
const debug = require('debug')('cube:service');
const url = require('url');
const path = require('path');
const connect = require('connect');
const connectStatic = require('serve-static');
const async = require('async');
function createMiddleware(cube, serveStatic, checkSkip) {
let config = cube.config;
let cubeLoaderPath = config.loaderPath || '/cube.js';
let charset = config.charset || 'utf-8';
return function (req, res, next) {
debug('query path', req.url);
let q = url.parse(req.url, true);
let qpath = q.pathname;
let flagWrap;
let flagCompress;
if (qpath === '/') {
req.url = '/index.html';
}
// parse query object
if (!req.query) {
req.query = q.query;
}
/**
* for cube loader
*/
if (qpath === cubeLoaderPath) {
res.setHeader('content-type', 'text/javascript');
xfs.readFile(path.join(__dirname, './runtime/cube.js'), (err, data) => {
res.write(data);
res.write('\n');
xfs.readFile(path.join(__dirname, './runtime/cube_clean_cache.js'), (err, data) => {
res.write(data);
res.write('\n');
if (cube.config.optimize === false) {
res.end('');
} else {
cube.caches.getNodeModules(function (code) {
res.write(code);
res.write('\n');
}, function () {
res.end('');
});
}
});
});
return;
} else if (qpath === '/__clean_cache__') {
return cube.caches.clean((err) => {
res.setHeader('content-type', 'image/png');
if (err) {
res.statusCode = 500;
res.end(err.message);
} else {
res.statusCode = 200;
res.end('');
}
});
}
if (checkSkip(req.url)) {
res.setHeader('x-cube-skip', 'true');
return serveStatic(req, res, next);
}
flagWrap = req.query.m === undefined ? false : true;
flagCompress = req.query.c === undefined ? false : true;
let useCache = flagWrap && !flagCompress;
let cachePath = qpath + ':' + (config.remote || '-');
async.waterfall([
function prepareData(done) {
/**
* 贯穿转换过程的Data数据结构
*/
let data = {
queryPath: qpath, // 查询path
absPath: null, // 绝对路径
realPath: null, // 真实path
type: null, // 文件类型
source: null, // 源码
code: null, // 文件代码
codeWraped: null, // wrap后的文件代码
sourceMap: null, // 源码的sourceMap
modifyTime: null, // 修改时间,检测缓存用
mime: null, // mime 类型
wrap: flagWrap, // 是否wrap代码
skip: !flagWrap, // 是否跳过解析
compress: flagCompress
};
done(null, data);
},
/** 检查cache,以便下次快速返回 */
function checkCache(data, callback) {
if (!config.devCache) {
return callback(null, data);
}
let cache;
if (useCache) {
cache = cube.caches.get(cachePath);
}
if (!cache) {
return callback(null, data);
}
xfs.lstat(cache.absPath, function (err, stats) {
if (err) {
return callback({
code: 'CHECK_CACHE_FILE_ERROR',
message: 'read file stats error:' + err.message
});
}
var mtime = new Date(stats.mtime).getTime();
if (cache.modifyTime === mtime) { // target the cache, just return
debug('hint cache', cachePath, data.queryPath);
return done(null, cache);
}
data.modifyTime = mtime;
callback(null, data);
});
},
cube.seekFile.bind(cube),
cube.readFile.bind(cube),
cube.transferCode.bind(cube),
/** cache结果,以便下次快速返回 */
function cacheData(data, callback) {
data.genCode(function (err, data) {
if (!err && config.devCache) {
debug('cache processed file: %s, %s', data.queryPath, data.modifyTime);
delete data.ast;
if (useCache) {
cube.caches.set(cachePath, data);
}
}
callback(err, data);
});
}
], done);
function done(err, result) {
if (err) {
switch(err.code) {
case 'PASS_UNKNOW_EXT':
case 'STATIC_FILE':
return serveStatic(req, res, next);
case 'FILE_NOT_FOUND':
err.statusCode = 404;
return errorMsg(err, result);
default:
return errorMsg(err, result);
}
}
var code = flagWrap ? result.codeWraped : result.code;
output();
function output() {
if (result.ext === '.html' && !flagWrap) {
code = result.source;
}
res.statusCode = 200;
res.setHeader('content-type', result.mime + ';charset=' + charset);
res.end(code);
}
}
function errorMsg(e, result) {
let mime = result ? result.mime : 'text/html';
res.statusCode = e.statusCode || 200;
res.setHeader('content-type', flagWrap ? 'text/javascript' : mime);
var msg = flagWrap ?
'console.error("[CUBE]",' +
JSON.stringify(
(e.code || e.name || 'UNKNOW_ERROR') + ':' + e.message +
' [File]:' + e.file +
' [Line]:' + (e.loc ? e.loc.line : e.line) +
' [Column]: ' + (e.loc ? e.loc.column : e.column)
) +
');' :
'[CUBE]\n' +
(e.code || e.name || 'UNKNOW_ERROR') + e.message +
' [File]:' + e.file +
' [Line]:' + (e.loc ? e.loc.line : e.line) +
' [Column]: ' + (e.loc ? e.loc.column : e.column);
res.end(msg);
}
};
}
/**
* init service
*
* @param {Cube} cube instance
*/
exports.init = function (cube) {
let config = cube.config;
let root = config.root;
let serveStatic;
let cubeMiddleware;
let app;
if (!config.cached) {
config.cached = config.built ? config.root : root + '.release';
}
if (!xfs.existsSync(config.cached)) {
config.cached = false;
}
let skip = cube.ignoreRules.skip;
function checkSkip(url) {
if (!skip) {
return false;
}
for (let i =0, len = skip.length; i < len; i++) {
if (skip[i].test(url)) {
return true;
}
}
return false;
}
config.maxAge = config.maxAge && config.cached ? config.maxAge : 0;
/**
* fallback the 404 request
*/
serveStatic = connectStatic(config.cached ? config.cached : config.root, {
maxAge: config.maxAge
});
/**
* cube middleware
*/
cubeMiddleware = createMiddleware(cube, serveStatic, checkSkip);
if (config.middleware) {
cube.middleware = config.static || config.cached ? serveStatic : cubeMiddleware;
cube.middleware.getCube = function () {
return cube;
};
} else {
app = connect();
app.use(config.router, config.static || config.cached ? serveStatic : cubeMiddleware);
app.use(function(err, req, res, next) {
if (/favicon\.ico$/.test(req.url)) {
res.statusCode = 200;
res.end('ico not found');
return;
}
next();
});
cube.connect = app;
config.port && app.listen(config.port, function (err) {
if (err) {
cube.log.error('server fail to start,', err.message);
} else {
cube.log.info('server started, listen:', config.port);
cube.log.info('[Cube] now you can visit: http://localhost:' + config.port + config.router);
}
});
}
};