vanilla-jet 1.0.2 → 1.0.4
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/request.js +89 -125
- package/framework/router.js +104 -97
- package/package.json +1 -1
package/framework/request.js
CHANGED
|
@@ -1,147 +1,111 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
this.path = '';
|
|
22
|
-
this.parts = [];
|
|
23
|
-
// Call initialization callback
|
|
24
|
-
this.init(req, options);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
Request.prototype.init = function(req, options) {
|
|
28
|
-
|
|
29
|
-
var obj = this,
|
|
30
|
-
body = '',
|
|
31
|
-
options = options || {},
|
|
32
|
-
parsed = url.parse(req.url, true),
|
|
33
|
-
server = require('../../index').server;
|
|
34
|
-
|
|
35
|
-
//console.log(obj);
|
|
36
|
-
|
|
37
|
-
// Save callbacks
|
|
38
|
-
obj.onDataReceived = options.onDataReceived || obj.onDataReceived;
|
|
39
|
-
// Save request method (get, post, etc)
|
|
40
|
-
obj.type = req.method.toLowerCase();
|
|
41
|
-
// Parse path parts
|
|
42
|
-
obj.path = parsed.pathname.replace(/\/$/, ''); // This is important
|
|
43
|
-
obj.parts = parsed.pathname.replace(/(^\/|\/$)/, '').split('/');
|
|
44
|
-
|
|
45
|
-
// // Try to guess MVC parameters
|
|
46
|
-
// obj.controller = obj.parts[0] || '';
|
|
47
|
-
// obj.action = obj.parts[1] || '';
|
|
48
|
-
// obj.id = obj.parts[2] || '';
|
|
49
|
-
obj.controller = 'app';
|
|
50
|
-
obj.action = obj.parts[0];
|
|
51
|
-
obj.id = obj.parts[1] || null;
|
|
52
|
-
|
|
53
|
-
// Patch for hanlde default route
|
|
54
|
-
if (obj.action == '' && obj.path == '') {
|
|
55
|
-
if ( server != null && server.getDefaultRoute() != '') {
|
|
56
|
-
obj.path = server.getDefaultRoute();
|
|
57
|
-
}
|
|
1
|
+
let url = require('url');
|
|
2
|
+
let querystring = require('querystring');
|
|
3
|
+
|
|
4
|
+
class Request {
|
|
5
|
+
|
|
6
|
+
constructor(req, options) {
|
|
7
|
+
|
|
8
|
+
this.params = {
|
|
9
|
+
get: {},
|
|
10
|
+
post: {},
|
|
11
|
+
body: ''
|
|
12
|
+
};
|
|
13
|
+
this.format = 'html';
|
|
14
|
+
this.type = 'get';
|
|
15
|
+
this.controller = 'index';
|
|
16
|
+
this.action = 'index';
|
|
17
|
+
this.id = '';
|
|
18
|
+
this.path = '';
|
|
19
|
+
this.parts = [];
|
|
20
|
+
this.init(req, options);
|
|
58
21
|
}
|
|
59
22
|
|
|
60
|
-
|
|
61
|
-
console.log(obj.action);
|
|
62
|
-
console.log(obj.path);
|
|
63
|
-
console.log("======= End ========");*/
|
|
23
|
+
init(req, options) {
|
|
64
24
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
25
|
+
var obj = this, body = '', options = options || {}, parsed = url.parse(req.url, true);
|
|
26
|
+
|
|
27
|
+
// Save callbacks
|
|
28
|
+
obj.onDataReceived = options.onDataReceived || obj.onDataReceived;
|
|
29
|
+
// Save request method (get, post, etc)
|
|
30
|
+
obj.type = req.method.toLowerCase();
|
|
31
|
+
// Parse path parts
|
|
32
|
+
obj.path = parsed.pathname.replace(/\/$/, '');
|
|
33
|
+
obj.parts = parsed.pathname.replace(/(^\/|\/$)/, '').split('/');
|
|
69
34
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
35
|
+
// -- Defaults
|
|
36
|
+
obj.controller = 'app';
|
|
37
|
+
obj.action = obj.parts[0];
|
|
38
|
+
obj.id = obj.parts[1] || null;
|
|
73
39
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
40
|
+
// Patch for catch if not pass second parameter
|
|
41
|
+
if (obj.id == undefined || obj.id === 'undefined' || obj.id == null) {
|
|
42
|
+
obj.id = null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Matches
|
|
46
|
+
var matches = (obj.id == null) ? null : obj.id.match(/\.([a-z0-9]+)$/);
|
|
47
|
+
if (matches) {
|
|
78
48
|
obj.format = matches[1] || 'html';
|
|
79
49
|
obj.id = obj.id.replace(/\.([a-z0-9]+)$/, '');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Stream request body
|
|
53
|
+
req.on('data', function (chunk) {
|
|
54
|
+
body += chunk.toString();
|
|
55
|
+
});
|
|
56
|
+
// Parse GET and POST params
|
|
57
|
+
obj.params.get = parsed.query;
|
|
58
|
+
req.on('end', function (chunk) {
|
|
59
|
+
obj.params.body = body;
|
|
60
|
+
obj.params.post = querystring.parse(body);
|
|
61
|
+
obj.onDataReceived.call(obj);
|
|
62
|
+
});
|
|
80
63
|
}
|
|
81
|
-
//}
|
|
82
|
-
|
|
83
|
-
// Stream request body
|
|
84
|
-
req.on('data', function(chunk) {
|
|
85
|
-
body += chunk.toString();
|
|
86
|
-
});
|
|
87
|
-
// Parse GET and POST params
|
|
88
|
-
obj.params.get = parsed.query;
|
|
89
|
-
req.on('end', function(chunk) {
|
|
90
|
-
obj.params.body = body;
|
|
91
|
-
obj.params.post = querystring.parse(body);
|
|
92
|
-
obj.onDataReceived.call(obj);
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
64
|
|
|
96
|
-
/**
|
|
97
|
-
* This method try to get a parameter from GET OR POST global parameters
|
|
98
|
-
* @param name: Name of the parameter
|
|
99
|
-
* @param value: Default value if the parameter is missing
|
|
100
|
-
**/
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
65
|
+
/**
|
|
66
|
+
* This method try to get a parameter from GET OR POST global parameters
|
|
67
|
+
* @param name: Name of the parameter
|
|
68
|
+
* @param value: Default value if the parameter is missing
|
|
69
|
+
**/
|
|
70
|
+
param(name, value) {
|
|
71
|
+
|
|
72
|
+
var obj = this, ret = value || '';
|
|
73
|
+
// Try to retrieve parameter from both, POST and GET objects
|
|
74
|
+
if (typeof obj.params.post[name] !== 'undefined') {
|
|
75
|
+
ret = obj.params.post[name];
|
|
76
|
+
} else if (typeof obj.params.get[name] !== 'undefined') {
|
|
77
|
+
ret = obj.params.get[name];
|
|
78
|
+
}
|
|
79
|
+
return ret;
|
|
110
80
|
}
|
|
111
|
-
return ret;
|
|
112
|
-
}
|
|
113
81
|
|
|
114
|
-
|
|
82
|
+
post(name, value) {
|
|
115
83
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
84
|
+
var obj = this, ret = value || '';
|
|
85
|
+
// Try to retrieve parameter from POST object
|
|
86
|
+
if (typeof obj.params.post[name] !== 'undefined') {
|
|
87
|
+
ret = obj.params.post[name];
|
|
88
|
+
}
|
|
89
|
+
return ret;
|
|
121
90
|
}
|
|
122
|
-
return ret;
|
|
123
|
-
}
|
|
124
91
|
|
|
125
|
-
|
|
92
|
+
get(name, value) {
|
|
126
93
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
94
|
+
var obj = this, ret = value || '';
|
|
95
|
+
// Try to retrieve parameter from GET object
|
|
96
|
+
if (typeof obj.params.get[name] !== 'undefined') {
|
|
97
|
+
ret = obj.params.get[name];
|
|
98
|
+
}
|
|
99
|
+
return ret;
|
|
132
100
|
}
|
|
133
|
-
return ret;
|
|
134
|
-
}
|
|
135
101
|
|
|
136
|
-
|
|
102
|
+
body() {
|
|
137
103
|
|
|
138
|
-
|
|
139
|
-
|
|
104
|
+
var obj = this;
|
|
105
|
+
return obj.params.body;
|
|
106
|
+
}
|
|
140
107
|
}
|
|
141
108
|
|
|
142
|
-
Request.prototype.onDataReceived = function() {
|
|
143
|
-
|
|
144
|
-
// Interface
|
|
145
|
-
}
|
|
109
|
+
Request.prototype.onDataReceived = function() {}
|
|
146
110
|
|
|
147
111
|
module.exports = Request;
|
package/framework/router.js
CHANGED
|
@@ -16,6 +16,7 @@ function Router(server) {
|
|
|
16
16
|
};
|
|
17
17
|
this.defaultRoute = '';
|
|
18
18
|
this.server = server;
|
|
19
|
+
this.cwd = process.cwd();
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
Router.prototype.routeToRegExp = function(route) {
|
|
@@ -56,20 +57,41 @@ Router.prototype.removeRoute = function(method, route) {};
|
|
|
56
57
|
|
|
57
58
|
Router.prototype.onRequest = function(req, res) {
|
|
58
59
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
60
|
+
let obj = this;
|
|
61
|
+
let isMatch = false;
|
|
62
|
+
let zlib = require('zlib');
|
|
63
|
+
let response = new Response(res);
|
|
64
|
+
let request = new Request(req, {
|
|
65
|
+
onDataReceived: function() {
|
|
66
|
+
|
|
67
|
+
console.log(request.path);
|
|
68
|
+
if (request.path == obj.server.options.base_url) {
|
|
69
|
+
request.path = obj.server.options.base_url + obj.defaultRoute;
|
|
70
|
+
}
|
|
71
|
+
console.log(request.path);
|
|
72
|
+
|
|
73
|
+
// Try with the routes for the current method (get or post)
|
|
74
|
+
_.each(obj.routes[request.type], function(route) {
|
|
75
|
+
if ( request.path.match(route.regexp) ) {
|
|
76
|
+
|
|
77
|
+
var parts = route.handler.split('.'),
|
|
78
|
+
clazz = parts[0],
|
|
79
|
+
method = parts[1],
|
|
80
|
+
callback = obj.validateCallback(clazz, method);
|
|
81
|
+
|
|
82
|
+
if (callback && callback != undefined && callback != '') {
|
|
83
|
+
|
|
84
|
+
isMatch = true;
|
|
85
|
+
handled = callback(request, response, obj.server);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
69
88
|
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// If not handled yet, try with the wildcard ones
|
|
92
|
+
if (!handled) {
|
|
93
|
+
_.each(obj.routes['*'], function(route) {
|
|
70
94
|
|
|
71
|
-
// Try with the routes for the current method (get or post)
|
|
72
|
-
_.each(obj.routes[request.type], function(route) {
|
|
73
95
|
if ( request.path.match(route.regexp) ) {
|
|
74
96
|
|
|
75
97
|
var parts = route.handler.split('.'),
|
|
@@ -85,99 +107,84 @@ Router.prototype.onRequest = function(req, res) {
|
|
|
85
107
|
}
|
|
86
108
|
}
|
|
87
109
|
});
|
|
110
|
+
}
|
|
88
111
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
112
|
+
// No route catched, maybe it's a static content
|
|
113
|
+
// or not handled? Well, at this point we call 404
|
|
114
|
+
if (handled == false && isMatch == false ) {
|
|
115
|
+
|
|
116
|
+
var path = require('path'),
|
|
117
|
+
ext = path.extname(req.url).replace('.', ''),
|
|
118
|
+
extHandled = false,
|
|
119
|
+
extHeader = {};
|
|
120
|
+
var mimes = {
|
|
121
|
+
|
|
122
|
+
'png' : 'image/png',
|
|
123
|
+
'webp': 'image/webp',
|
|
124
|
+
'jpg' : 'image/jpg',
|
|
125
|
+
'css' : 'text/css',
|
|
126
|
+
'gz' : 'application/x-gzip',
|
|
127
|
+
'gif' : 'image/gif',
|
|
128
|
+
'js' : 'text/javascript',
|
|
129
|
+
'svg' : 'image/svg+xml',
|
|
130
|
+
'ttf' : 'application/x-font-ttf',
|
|
131
|
+
'otf' : 'application/x-font-opentype',
|
|
132
|
+
'pdf' : 'application/pdf',
|
|
133
|
+
'json' : 'application/json'
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
var compressionMimes = {
|
|
137
|
+
'css' : 'text/css',
|
|
138
|
+
'js' : 'text/javascript',
|
|
139
|
+
'gz' : 'application/x-gzip'
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
if (mimes[ext] != undefined && mimes[ext] != 'undefined') {
|
|
143
|
+
|
|
144
|
+
extHandled = true;
|
|
145
|
+
extHeader = {'Content-Type': mimes[ext]};
|
|
146
|
+
}
|
|
94
147
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
148
|
+
if (extHandled) {
|
|
149
|
+
|
|
150
|
+
let fs = require('fs'),
|
|
151
|
+
rep = obj.cwd.replace('core/framework', ''),
|
|
152
|
+
route = request.path.replace(obj.server.options.base_url, ''),
|
|
153
|
+
filename = path.join(rep, route),
|
|
154
|
+
filePrivate = obj.isProtectedFile(route);
|
|
155
|
+
|
|
156
|
+
console.log(rep);
|
|
157
|
+
console.log(route);
|
|
158
|
+
console.log(filename);
|
|
159
|
+
console.log("-------------");
|
|
160
|
+
|
|
161
|
+
fs.exists(filename, function(exists) {
|
|
162
|
+
|
|
163
|
+
if (exists && !filePrivate) {
|
|
164
|
+
|
|
165
|
+
var acceptEncoding = (req.headers['accept-encoding'] != undefined) ? req.headers['accept-encoding'] : '';
|
|
166
|
+
var fileStream = fs.createReadStream(filename);
|
|
167
|
+
if (acceptEncoding.match(/\bgzip\b/) && compressionMimes[ext] != undefined) {
|
|
168
|
+
extHeader['Content-Encoding'] = 'gzip';
|
|
169
|
+
res.writeHead(200, extHeader);
|
|
170
|
+
fileStream.pipe(zlib.createGzip()).pipe(res);
|
|
171
|
+
} else {
|
|
172
|
+
res.writeHead(200, extHeader);
|
|
173
|
+
fileStream.pipe(res);
|
|
174
|
+
}
|
|
175
|
+
return;
|
|
99
176
|
|
|
100
|
-
|
|
177
|
+
} else {
|
|
101
178
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
179
|
+
// Return 404
|
|
180
|
+
obj.onNotFound(response);
|
|
106
181
|
}
|
|
107
182
|
});
|
|
108
183
|
}
|
|
109
|
-
|
|
110
|
-
// No route catched, maybe it's a static content
|
|
111
|
-
// or not handled? Well, at this point we call 404
|
|
112
|
-
if (handled == false && isMatch == false ) {
|
|
113
|
-
|
|
114
|
-
var path = require('path'),
|
|
115
|
-
ext = path.extname(req.url).replace('.', ''),
|
|
116
|
-
extHandled = false,
|
|
117
|
-
extHeader = {};
|
|
118
|
-
var mimes = {
|
|
119
|
-
|
|
120
|
-
'png' : 'image/png',
|
|
121
|
-
'webp': 'image/webp',
|
|
122
|
-
'jpg' : 'image/jpg',
|
|
123
|
-
'css' : 'text/css',
|
|
124
|
-
'gz' : 'application/x-gzip',
|
|
125
|
-
'gif' : 'image/gif',
|
|
126
|
-
'js' : 'text/javascript',
|
|
127
|
-
'svg' : 'image/svg+xml',
|
|
128
|
-
'ttf' : 'application/x-font-ttf',
|
|
129
|
-
'otf' : 'application/x-font-opentype',
|
|
130
|
-
'pdf' : 'application/pdf',
|
|
131
|
-
'json' : 'application/json'
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
var compressionMimes = {
|
|
135
|
-
'css' : 'text/css',
|
|
136
|
-
'js' : 'text/javascript',
|
|
137
|
-
'gz' : 'application/x-gzip'
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
if (mimes[ext] != undefined && mimes[ext] != 'undefined') {
|
|
141
|
-
|
|
142
|
-
extHandled = true;
|
|
143
|
-
extHeader = {'Content-Type': mimes[ext]};
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (extHandled) {
|
|
147
|
-
|
|
148
|
-
var fs = require('fs'),
|
|
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);
|
|
153
|
-
|
|
154
|
-
fs.exists(filename, function(exists) {
|
|
155
|
-
|
|
156
|
-
if (exists && !filePrivate) {
|
|
157
|
-
|
|
158
|
-
var acceptEncoding = (req.headers['accept-encoding'] != undefined) ? req.headers['accept-encoding'] : '';
|
|
159
|
-
var fileStream = fs.createReadStream(filename);
|
|
160
|
-
if (acceptEncoding.match(/\bgzip\b/) && compressionMimes[ext] != undefined) {
|
|
161
|
-
extHeader['Content-Encoding'] = 'gzip';
|
|
162
|
-
res.writeHead(200, extHeader);
|
|
163
|
-
fileStream.pipe(zlib.createGzip()).pipe(res);
|
|
164
|
-
} else {
|
|
165
|
-
res.writeHead(200, extHeader);
|
|
166
|
-
fileStream.pipe(res);
|
|
167
|
-
}
|
|
168
|
-
return;
|
|
169
|
-
|
|
170
|
-
} else {
|
|
171
|
-
|
|
172
|
-
// Return 404
|
|
173
|
-
obj.onNotFound(response);
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
184
|
}
|
|
179
|
-
}
|
|
180
|
-
|
|
185
|
+
}
|
|
186
|
+
}),
|
|
187
|
+
handled = false;
|
|
181
188
|
isMatch = false;
|
|
182
189
|
};
|
|
183
190
|
|