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.
Files changed (2) hide show
  1. package/framework/router.js +113 -137
  2. package/package.json +1 -1
@@ -1,81 +1,69 @@
1
- /**
2
- Version 3.5
3
- Created by: nalancer08 <https://github.com/nalancer08>
4
- **/
5
-
6
- var Request = require('./request.js');
7
- var Response = require('./response.js');
8
- var _ = require('underscore');
9
-
10
- function Router(server) {
11
-
12
- this.routes = {
13
- '*': [],
14
- 'get': [],
15
- 'post': []
16
- };
17
- this.defaultRoute = '';
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
- Router.prototype.routeToRegExp = function(route) {
22
-
23
- var optionalParam = /\((.*?)\)/g,
24
- namedParam = /(\(\?)?:\w+/g,
25
- splatParam = /\*\w+/g,
26
- escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g;
27
- // Convert route to regular expression, this was taken from Backbone's router
28
- route = route.replace(escapeRegExp, '\\$&')
29
- .replace(optionalParam, '(?:$1)?')
30
- .replace(namedParam, function(match, optional) {
31
- return optional ? match : '([^/?]+)';
32
- })
33
- .replace(splatParam, '([^?]*?)');
34
- return new RegExp(`^${route}(?:\\?([\\s\\S]*))?$`);
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
- // Add the route, may be at the beginning or at the end
48
- if (insert) { // Adding the route at the beginning of the route's array
49
- obj.routes[method].unshift(instance);
50
- } else { // Adding the route at the end of the route's array
51
- obj.routes[method].push(instance);
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
- Router.prototype.removeRoute = function(method, route) {};
46
+ removeRoute(method, route) {}
56
47
 
57
- Router.prototype.onRequest = function(req, res) {
48
+ onRequest(req, res) {
58
49
 
59
- var obj = this;
60
- var isMatch = false;
61
- var zlib = require('zlib');
62
- var response = new Response(res);
63
- var request = new Request(req, {
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 ( request.path.match(route.regexp) ) {
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 ( request.path.match(route.regexp) ) {
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' : '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'
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' : 'text/css',
136
- 'js' : 'text/javascript',
137
- 'gz' : 'application/x-gzip'
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
- 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);
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
- res.writeHead(200, extHeader);
163
- fileStream.pipe(zlib.createGzip()).pipe(res);
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
- handled = false;
181
- isMatch = false;
182
- };
156
+ }), handled = false;
157
+ isMatch = false;
158
+ }
183
159
 
184
- Router.prototype.isProtectedFile = function(route) {
160
+ isProtectedFile(route) {
185
161
 
186
- let protectedDirs = ['framework', 'external', 'node_mudules'];
187
- var routeParts = route.split('/');
188
- if (routeParts[1] != undefined && routeParts.length > 2) {
189
- return protectedDirs.includes(routeParts[1]);
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
- Router.prototype.validateExtension = function(route) {};
170
+ validateExtension(route) {}
195
171
 
196
- Router.prototype.validateCallback = function(clazz, method) {
172
+ validateCallback(clazz, method) {
197
173
 
198
- var obj = this,
199
- endpoints = obj.server.functions.endpoints;
174
+ var obj = this, endpoints = obj.server.functions.endpoints;
200
175
 
201
- if (endpoints[clazz] != undefined) {
176
+ if (endpoints[clazz] != undefined) {
202
177
 
203
- clazz = endpoints[clazz];
178
+ clazz = endpoints[clazz];
204
179
 
205
- if (typeof clazz[method] === 'function') {
206
- return clazz[method];
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
- var obj = this;
218
- obj.defaultRoute = route;
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
- Router.prototype.getDefaultRoute = function() {
193
+ var obj = this;
194
+ obj.defaultRoute = route;
195
+ }
196
+
197
+ getDefaultRoute() {
222
198
 
223
- var obj = this,
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
- return (prev + obj.defaultRoute);
227
- };
201
+ return (prev + obj.defaultRoute);
202
+ }
228
203
 
229
- Router.prototype.onNotFound = function(response) {
204
+ onNotFound(response) {
230
205
 
231
- response.setStatus(404);
232
- response.respond();
233
- };
206
+ response.setStatus(404);
207
+ response.respond();
208
+ }
209
+ }
234
210
 
235
211
  module.exports = Router;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vanilla-jet",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "VannilaJet framework",
5
5
  "main": "index.js",
6
6
  "scripts": {