vanilla-jet 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/framework/router.js +113 -137
- package/package.json +1 -1
package/framework/router.js
CHANGED
|
@@ -1,81 +1,69 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
'
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
this.server = server;
|
|
19
|
-
}
|
|
1
|
+
let Request = require('./request.js');
|
|
2
|
+
let Response = require('./response.js');
|
|
3
|
+
let _ = require('underscore');
|
|
4
|
+
|
|
5
|
+
class Router {
|
|
6
|
+
|
|
7
|
+
constructor(server) {
|
|
8
|
+
|
|
9
|
+
this.routes = {
|
|
10
|
+
'*': [],
|
|
11
|
+
'get': [],
|
|
12
|
+
'post': []
|
|
13
|
+
};
|
|
14
|
+
this.defaultRoute = '';
|
|
15
|
+
this.server = server;
|
|
16
|
+
this.cwd = process.cwd();
|
|
17
|
+
}
|
|
20
18
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
Router.prototype.addRoute = function(method, route, handler, insert) {
|
|
38
|
-
|
|
39
|
-
var obj = this,
|
|
40
|
-
insert = insert || false,
|
|
41
|
-
method = method.toLowerCase(),
|
|
42
|
-
prev = (obj.server.options.base_url && obj.server.options.base_url != '' && obj.server.options.base_url != '/') ? obj.server.options.base_url : '',
|
|
43
|
-
instance = {
|
|
19
|
+
routeToRegExp(route) {
|
|
20
|
+
|
|
21
|
+
var optionalParam = /\((.*?)\)/g, namedParam = /(\(\?)?:\w+/g, splatParam = /\*\w+/g, escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g;
|
|
22
|
+
// Convert route to regular expression, this was taken from Backbone's router
|
|
23
|
+
route = route.replace(escapeRegExp, '\\$&')
|
|
24
|
+
.replace(optionalParam, '(?:$1)?')
|
|
25
|
+
.replace(namedParam, function (match, optional) {
|
|
26
|
+
return optional ? match : '([^/?]+)';
|
|
27
|
+
})
|
|
28
|
+
.replace(splatParam, '([^?]*?)');
|
|
29
|
+
return new RegExp(`^${route}(?:\\?([\\s\\S]*))?$`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
addRoute(method, route, handler, insert) {
|
|
33
|
+
|
|
34
|
+
var obj = this, insert = insert || false, method = method.toLowerCase(), prev = (obj.server.options.base_url && obj.server.options.base_url != '' && obj.server.options.base_url != '/') ? obj.server.options.base_url : '', instance = {
|
|
44
35
|
regexp: obj.routeToRegExp(prev + route),
|
|
45
36
|
handler: handler
|
|
46
37
|
};
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
38
|
+
// Add the route, may be at the beginning or at the end
|
|
39
|
+
if (insert) { // Adding the route at the beginning of the route's array
|
|
40
|
+
obj.routes[method].unshift(instance);
|
|
41
|
+
} else { // Adding the route at the end of the route's array
|
|
42
|
+
obj.routes[method].push(instance);
|
|
43
|
+
}
|
|
52
44
|
}
|
|
53
|
-
};
|
|
54
45
|
|
|
55
|
-
|
|
46
|
+
removeRoute(method, route) {}
|
|
56
47
|
|
|
57
|
-
|
|
48
|
+
onRequest(req, res) {
|
|
58
49
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
onDataReceived: function() {
|
|
50
|
+
let obj = this;
|
|
51
|
+
let isMatch = false;
|
|
52
|
+
let zlib = require('zlib');
|
|
53
|
+
let response = new Response(res);
|
|
54
|
+
let request = new Request(req, {
|
|
55
|
+
onDataReceived: function () {
|
|
65
56
|
|
|
66
57
|
//console.log(request.path);
|
|
67
58
|
if (request.path == obj.server.options.base_url) {
|
|
68
59
|
request.path = obj.server.options.base_url + obj.defaultRoute;
|
|
69
60
|
}
|
|
70
|
-
|
|
61
|
+
//console.log(request.path);
|
|
71
62
|
// Try with the routes for the current method (get or post)
|
|
72
|
-
_.each(obj.routes[request.type], function(route) {
|
|
73
|
-
if (
|
|
63
|
+
_.each(obj.routes[request.type], function (route) {
|
|
64
|
+
if (request.path.match(route.regexp)) {
|
|
74
65
|
|
|
75
|
-
var parts = route.handler.split('.'),
|
|
76
|
-
clazz = parts[0],
|
|
77
|
-
method = parts[1],
|
|
78
|
-
callback = obj.validateCallback(clazz, method);
|
|
66
|
+
var parts = route.handler.split('.'), clazz = parts[0], method = parts[1], callback = obj.validateCallback(clazz, method);
|
|
79
67
|
|
|
80
68
|
if (callback && callback != undefined && callback != '') {
|
|
81
69
|
|
|
@@ -88,14 +76,11 @@ Router.prototype.onRequest = function(req, res) {
|
|
|
88
76
|
|
|
89
77
|
// If not handled yet, try with the wildcard ones
|
|
90
78
|
if (!handled) {
|
|
91
|
-
_.each(obj.routes['*'], function(route) {
|
|
79
|
+
_.each(obj.routes['*'], function (route) {
|
|
92
80
|
|
|
93
|
-
if (
|
|
81
|
+
if (request.path.match(route.regexp)) {
|
|
94
82
|
|
|
95
|
-
var parts = route.handler.split('.'),
|
|
96
|
-
clazz = parts[0],
|
|
97
|
-
method = parts[1],
|
|
98
|
-
callback = obj.validateCallback(clazz, method);
|
|
83
|
+
var parts = route.handler.split('.'), clazz = parts[0], method = parts[1], callback = obj.validateCallback(clazz, method);
|
|
99
84
|
|
|
100
85
|
if (callback && callback != undefined && callback != '') {
|
|
101
86
|
|
|
@@ -109,58 +94,50 @@ Router.prototype.onRequest = function(req, res) {
|
|
|
109
94
|
|
|
110
95
|
// No route catched, maybe it's a static content
|
|
111
96
|
// or not handled? Well, at this point we call 404
|
|
112
|
-
if (handled == false && isMatch == false
|
|
97
|
+
if (handled == false && isMatch == false) {
|
|
113
98
|
|
|
114
|
-
var path = require('path'),
|
|
115
|
-
ext = path.extname(req.url).replace('.', ''),
|
|
116
|
-
extHandled = false,
|
|
117
|
-
extHeader = {};
|
|
99
|
+
var path = require('path'), ext = path.extname(req.url).replace('.', ''), extHandled = false, extHeader = {};
|
|
118
100
|
var mimes = {
|
|
119
|
-
|
|
120
|
-
'png' : 'image/png',
|
|
101
|
+
'png': 'image/png',
|
|
121
102
|
'webp': 'image/webp',
|
|
122
|
-
'jpg'
|
|
123
|
-
'css'
|
|
124
|
-
'gz'
|
|
125
|
-
'gif'
|
|
126
|
-
'js'
|
|
127
|
-
'svg'
|
|
128
|
-
'ttf'
|
|
129
|
-
'otf'
|
|
130
|
-
'pdf'
|
|
131
|
-
'json'
|
|
103
|
+
'jpg': 'image/jpg',
|
|
104
|
+
'css': 'text/css',
|
|
105
|
+
'gz': 'application/x-gzip',
|
|
106
|
+
'gif': 'image/gif',
|
|
107
|
+
'js': 'text/javascript',
|
|
108
|
+
'svg': 'image/svg+xml',
|
|
109
|
+
'ttf': 'application/x-font-ttf',
|
|
110
|
+
'otf': 'application/x-font-opentype',
|
|
111
|
+
'pdf': 'application/pdf',
|
|
112
|
+
'json': 'application/json'
|
|
132
113
|
};
|
|
133
114
|
|
|
134
115
|
var compressionMimes = {
|
|
135
|
-
'css'
|
|
136
|
-
'js'
|
|
137
|
-
'gz'
|
|
116
|
+
'css': 'text/css',
|
|
117
|
+
'js': 'text/javascript',
|
|
118
|
+
'gz': 'application/x-gzip'
|
|
138
119
|
};
|
|
139
120
|
|
|
140
121
|
if (mimes[ext] != undefined && mimes[ext] != 'undefined') {
|
|
141
122
|
|
|
142
123
|
extHandled = true;
|
|
143
|
-
extHeader = {'Content-Type': mimes[ext]};
|
|
124
|
+
extHeader = { 'Content-Type': mimes[ext] };
|
|
144
125
|
}
|
|
145
126
|
|
|
146
127
|
if (extHandled) {
|
|
147
128
|
|
|
148
|
-
|
|
149
|
-
rep = __dirname.replace('core/framework', ''),
|
|
150
|
-
route = request.path.replace(obj.server.options.base_url, ''),
|
|
151
|
-
filename = path.join(rep, route),
|
|
152
|
-
filePrivate = obj.isProtectedFile(route);
|
|
129
|
+
let fs = require('fs'), rep = obj.cwd.replace('core/framework', ''), route = request.path.replace(obj.server.options.base_url, ''), filename = path.join(rep, route), filePrivate = obj.isProtectedFile(route);
|
|
153
130
|
|
|
154
|
-
fs.exists(filename, function(exists) {
|
|
131
|
+
fs.exists(filename, function (exists) {
|
|
155
132
|
|
|
156
133
|
if (exists && !filePrivate) {
|
|
157
|
-
|
|
134
|
+
|
|
158
135
|
var acceptEncoding = (req.headers['accept-encoding'] != undefined) ? req.headers['accept-encoding'] : '';
|
|
159
136
|
var fileStream = fs.createReadStream(filename);
|
|
160
137
|
if (acceptEncoding.match(/\bgzip\b/) && compressionMimes[ext] != undefined) {
|
|
161
138
|
extHeader['Content-Encoding'] = 'gzip';
|
|
162
|
-
|
|
163
|
-
|
|
139
|
+
res.writeHead(200, extHeader);
|
|
140
|
+
fileStream.pipe(zlib.createGzip()).pipe(res);
|
|
164
141
|
} else {
|
|
165
142
|
res.writeHead(200, extHeader);
|
|
166
143
|
fileStream.pipe(res);
|
|
@@ -176,60 +153,59 @@ Router.prototype.onRequest = function(req, res) {
|
|
|
176
153
|
}
|
|
177
154
|
}
|
|
178
155
|
}
|
|
179
|
-
}),
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
};
|
|
156
|
+
}), handled = false;
|
|
157
|
+
isMatch = false;
|
|
158
|
+
}
|
|
183
159
|
|
|
184
|
-
|
|
160
|
+
isProtectedFile(route) {
|
|
185
161
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
162
|
+
let protectedDirs = ['framework', 'external', 'node_mudules'];
|
|
163
|
+
var routeParts = route.split('/');
|
|
164
|
+
if (routeParts[1] != undefined && routeParts.length > 2) {
|
|
165
|
+
return protectedDirs.includes(routeParts[1]);
|
|
166
|
+
}
|
|
167
|
+
return true;
|
|
190
168
|
}
|
|
191
|
-
return true;
|
|
192
|
-
};
|
|
193
169
|
|
|
194
|
-
|
|
170
|
+
validateExtension(route) {}
|
|
195
171
|
|
|
196
|
-
|
|
172
|
+
validateCallback(clazz, method) {
|
|
197
173
|
|
|
198
|
-
|
|
199
|
-
endpoints = obj.server.functions.endpoints;
|
|
174
|
+
var obj = this, endpoints = obj.server.functions.endpoints;
|
|
200
175
|
|
|
201
|
-
|
|
176
|
+
if (endpoints[clazz] != undefined) {
|
|
202
177
|
|
|
203
|
-
|
|
178
|
+
clazz = endpoints[clazz];
|
|
204
179
|
|
|
205
|
-
|
|
206
|
-
|
|
180
|
+
if (typeof clazz[method] === 'function') {
|
|
181
|
+
return clazz[method];
|
|
182
|
+
}
|
|
207
183
|
}
|
|
184
|
+
return '';
|
|
208
185
|
}
|
|
209
|
-
return '';
|
|
210
|
-
};
|
|
211
|
-
/**
|
|
212
|
-
* This method allows to set the default route for the api
|
|
213
|
-
* @param route: String name for the route
|
|
214
|
-
**/
|
|
215
|
-
Router.prototype.setDefaultRoute = function(route) {
|
|
216
186
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
187
|
+
/**
|
|
188
|
+
* This method allows to set the default route for the api
|
|
189
|
+
* @param route: String name for the route
|
|
190
|
+
**/
|
|
191
|
+
setDefaultRoute(route) {
|
|
220
192
|
|
|
221
|
-
|
|
193
|
+
var obj = this;
|
|
194
|
+
obj.defaultRoute = route;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
getDefaultRoute() {
|
|
222
198
|
|
|
223
|
-
|
|
224
|
-
prev = (obj.server.options.base_url && obj.server.options.base_url != '' && obj.server.options.base_url != '/') ? obj.server.options.base_url : '';
|
|
199
|
+
var obj = this, prev = (obj.server.options.base_url && obj.server.options.base_url != '' && obj.server.options.base_url != '/') ? obj.server.options.base_url : '';
|
|
225
200
|
|
|
226
|
-
|
|
227
|
-
}
|
|
201
|
+
return (prev + obj.defaultRoute);
|
|
202
|
+
}
|
|
228
203
|
|
|
229
|
-
|
|
204
|
+
onNotFound(response) {
|
|
230
205
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
}
|
|
206
|
+
response.setStatus(404);
|
|
207
|
+
response.respond();
|
|
208
|
+
}
|
|
209
|
+
}
|
|
234
210
|
|
|
235
211
|
module.exports = Router;
|