vanilla-jet 1.2.0 → 1.2.2

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/Gruntfile.js CHANGED
@@ -176,7 +176,7 @@ module.exports = function(grunt) {
176
176
  grunt.option('force', true);
177
177
  grunt.task.run('clean:' + target);
178
178
  });
179
- grunt.registerTask('default', ['buildLess', 'uglify', 'cleanForce:build', 'concat', 'cleanForce:minified', 'shell:compileTemplates', 'watch']);
180
- grunt.registerTask('build', ['buildLess', 'uglify', 'cleanForce:build', 'concat', 'cleanForce:minified', 'shell:compileTemplates']);
179
+ grunt.registerTask('default', ['buildLess', 'uglify', 'cleanForce:build', 'concat', 'cleanForce:minified', 'shell:compileTemplates', 'compress', 'watch']);
180
+ grunt.registerTask('build', ['buildLess', 'uglify', 'cleanForce:build', 'concat', 'cleanForce:minified', 'shell:compileTemplates', 'compress']);
181
181
  grunt.registerTask('server', ['buildLess']);
182
182
  };
@@ -9,12 +9,7 @@ function Dipper(options, shared) {
9
9
  this.page_title = this.site_title;
10
10
  this.description = shared.description;
11
11
  this.fbAppId = shared.fbAppId;
12
-
13
- this.site_url = ((this.options.site_url == 'localhost') ?
14
- 'http://localhost' :
15
- this.options.site_url) + ':' + this.options.port;
16
12
  this.base_dir = path.join(process.cwd(), '/');
17
- this.base_url = this.site_url + this.options.base_url;
18
13
 
19
14
  // Dirs
20
15
  this.dirs = {
@@ -44,30 +39,22 @@ function Dipper(options, shared) {
44
39
  }
45
40
 
46
41
  Dipper.prototype.getPageTitle = function() {
47
-
48
- var obj = this;
49
- return obj.page_title;
42
+ return this.page_title;
50
43
  }
51
44
 
52
45
  Dipper.prototype.getSiteTitle = function() {
53
-
54
- var obj = this;
55
- return obj.site_title;
46
+ return this.site_title;
56
47
  }
57
48
 
58
49
  Dipper.prototype.getDescription = function() {
59
-
60
- var obj = this;
61
- return obj.description;
50
+ return this.description;
62
51
  }
63
52
 
64
53
  Dipper.prototype.getFbAppId = function() {
65
-
66
- var obj = this;
67
- return obj.fbAppId;
54
+ return this.fbAppId;
68
55
  }
69
56
 
70
- Dipper.prototype.addMeta = function(name, content, attribute) {
57
+ Dipper.prototype.addMeta = function({name, content, attribute}) {
71
58
 
72
59
  let obj = this;
73
60
  attribute = attribute || 'name';
@@ -100,11 +87,8 @@ Dipper.prototype.metaTags = function() {
100
87
  }
101
88
 
102
89
  Dipper.prototype.img = function(filename) {
103
-
104
- var obj = this,
105
- dir = this.getDir('images', false);
106
- ret = this.urlTo(dir + filename);
107
- return ret;
90
+ let dir = this.getDir('images', false);
91
+ return this.urlTo(dir + filename);
108
92
  }
109
93
 
110
94
  Dipper.prototype.script = function(filename) {
@@ -119,17 +103,14 @@ Dipper.prototype.style = function(filename) {
119
103
 
120
104
  Dipper.prototype.pdf = function(filename) {
121
105
 
122
- var obj = this,
123
- dir = '/assets/pdf/'
124
- ret = this.urlTo(dir + filename);
125
- //console.log(ret);
126
- return ret;
106
+ let dir = '/assets/pdf/';
107
+ return this.urlTo(dir + filename);
127
108
  }
128
109
 
129
110
  Dipper.prototype.getDir = function(dir, full) {
130
111
 
131
- var obj = this,
132
- full = (full == undefined) ? true : full;
112
+ let obj = this;
113
+ full = (full == undefined) ? true : full;
133
114
 
134
115
  if ( obj.dirs[dir] != undefined ) {
135
116
  return (full == true ? obj.baseDir(obj.dirs[dir]) : obj.dirs[dir]);
@@ -138,32 +119,14 @@ Dipper.prototype.getDir = function(dir, full) {
138
119
  }
139
120
 
140
121
  Dipper.prototype.baseDir = function(path) {
141
-
142
- var obj = this,
143
- path = path || '',
144
- ret = obj.base_dir + path;
145
- return ret;
146
- }
147
-
148
- Dipper.prototype.isSecureRequest = function() {
149
-
150
- var obj = this;
151
- // Cheking for http or https
152
- if (/^((http):\/\/)/.test(obj.options.site_url) || /^((localhost))/.test(obj.options.site_url)) {
153
- return false;
154
- } else if (/^((https):\/\/)/.test(obj.options.site_url)) {
155
- return true;
156
- }
122
+ path = path || '';
123
+ return this.base_dir + path;
157
124
  }
158
125
 
159
- Dipper.prototype.urlTo = function(route, protocol) {
160
-
161
- var obj = this,
162
- protocol = protocol || undefined,
163
- url = obj.base_url + route;
164
- //url = obj.baseUrl(route, protocol);
165
- return url;
166
- }
126
+ Dipper.prototype.urlTo = function (route) {
127
+ if (/^(?:[a-z][a-z0-9+.-]*:)?\/\//i.test(route)) return route;
128
+ return '/' + route.replace(/^\/+/, '');
129
+ };
167
130
 
168
131
  Dipper.prototype.registerStyle = function(
169
132
  name, url, requires,
@@ -90,7 +90,11 @@ class Functions {
90
90
  { name: 'theme-color', content: '#ffffff' }
91
91
  ];
92
92
  basicMeta.forEach(meta => {
93
- dipper.addMeta(meta.name, meta.content);
93
+ dipper.addMeta({
94
+ name: meta.name,
95
+ content: meta.content || null,
96
+ attribute: meta.attribute || null
97
+ });
94
98
  });
95
99
 
96
100
  // Add Open Graph meta tags
@@ -104,7 +108,11 @@ class Functions {
104
108
  'og:locale': 'es_MX',
105
109
  };
106
110
  Object.entries(openGraphMeta).forEach(([key, value]) => {
107
- dipper.addMeta(key, value, 'property');
111
+ dipper.addMeta({
112
+ name: key,
113
+ content: value,
114
+ attribute: 'property'
115
+ });
108
116
  });
109
117
  }
110
118
  }
@@ -52,10 +52,13 @@ class Router {
52
52
 
53
53
  addRoute(method, route, handler, insert) {
54
54
 
55
- 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 = {
56
- regexp: obj.routeToRegExp(prev + route),
57
- handler: handler
58
- };
55
+ var obj = this,
56
+ insert = insert || false,
57
+ method = method.toLowerCase(),
58
+ instance = {
59
+ regexp: obj.routeToRegExp(route),
60
+ handler: handler
61
+ };
59
62
  // Add the route, may be at the beginning or at the end
60
63
  if (insert) { // Adding the route at the beginning of the route's array
61
64
  obj.routes[method].unshift(instance);
@@ -71,9 +74,7 @@ class Router {
71
74
  let response = new Response(res);
72
75
  let request = new Request(req, {
73
76
  onDataReceived: function () {
74
- if (request.path == obj.server.options.base_url) {
75
- request.path = obj.server.options.base_url + obj.defaultRoute;
76
- }
77
+ if (request.path == '') { request.path = obj.defaultRoute; }
77
78
  // -- Check GET or POST routes
78
79
  _.each(obj.routes[request.type], function (route) {
79
80
  if (request.path.match(route.regexp)) {
@@ -104,7 +105,7 @@ class Router {
104
105
  if (extHandled) {
105
106
 
106
107
  let rep = obj.cwd.replace('core/framework', ''),
107
- route = request.path.replace(obj.server.options.base_url, ''),
108
+ route = request.path,
108
109
  filename = path.join(rep, route),
109
110
  filePrivate = obj.isProtectedFile(route);
110
111
 
@@ -172,10 +173,7 @@ class Router {
172
173
  }
173
174
 
174
175
  getDefaultRoute() {
175
-
176
- let obj = this,
177
- prev = (obj.server.options.base_url && obj.server.options.base_url != '' && obj.server.options.base_url != '/') ? obj.server.options.base_url : '';
178
- return (prev + obj.defaultRoute);
176
+ return this.defaultRoute;
179
177
  }
180
178
 
181
179
  onNotFound(response) {
@@ -7,7 +7,6 @@ const path = require('path');
7
7
 
8
8
  let Router = require('./router.js');
9
9
  let Dipper = require('./dipper.js');
10
- let Functions = require('./functions.js');
11
10
 
12
11
  class Server {
13
12
 
@@ -31,10 +30,8 @@ class Server {
31
30
  security = settings['security'] || {};
32
31
 
33
32
  _.defaults(opts, {
34
- base_url: '',
35
- site_url: '',
36
- wsServer: false,
37
- onNotFound: obj.onNotFound
33
+ https_server: false,
34
+ wsServer: false
38
35
  });
39
36
  obj.options = opts;
40
37
 
@@ -57,9 +54,6 @@ class Server {
57
54
  // -- Create the server
58
55
  obj.createServer();
59
56
 
60
- // -- Not found callback
61
- obj.onNotFound = opts.onNotFound;
62
-
63
57
  // Initialize router
64
58
  obj.router = new Router(obj);
65
59
 
@@ -69,56 +63,35 @@ class Server {
69
63
  endpoints_built[endpoint.name] = new endpoint(obj.router);
70
64
  }
71
65
  this.endpoints = endpoints_built;
72
-
73
- // Setting endpoints
74
- obj.functions = new Functions();
75
66
  }
76
67
 
77
68
  start() {
78
-
79
- let obj = this;
80
- let port = (obj.options.port && obj.options.port != '') ? obj.options.port : 8080;
81
-
82
- // Listen on the specified port
83
- obj.httpx.listen(port);
84
-
85
- // Logging
86
- obj.log('__________________ VanillaJet server started ___________________');
87
- obj.log(` > Listening on ${obj.options.site_url}:${obj.options.port}${obj.options.base_url} < `);
88
- obj.log('-----------------------------------------------------------------');
69
+ this.log('__________________ VanillaJet Server started ___________________');
70
+ this.log(` > Running on 0.0.0.0:${this.options.port}/ < `);
71
+ this.log('------------------------------------------------------------------');
89
72
  }
90
73
 
91
74
  createServer() {
92
75
 
93
76
  let obj = this;
94
- // -- Http server
95
- let isLocalhost = /^((http):\/\/)/.test(obj.options.site_url) || /^((localhost))/.test(obj.options.site_url);
96
- if (isLocalhost) {
97
- obj.httpx = http.createServer((req, res) => {
98
- obj.router.onRequest.call(obj.router, req, res);
99
- });
100
- return;
101
- }
102
77
 
103
78
  // -- Https server (self managed certs)
104
79
  if (obj.security.self_managed_certs) {
105
- let certs = {
106
- key: fs.readFileSync(obj.security.key, 'utf8'),
107
- cert: fs.readFileSync(obj.security.cert, 'utf8'),
108
- allowHTTP1: true
109
- };
80
+ console.log('HTTPs server created - Self managed certs');
81
+ let certs = obj.getCertificates();
110
82
  obj.httpx = http2.createSecureServer(certs, (req, res) => {
111
83
  obj.router.onRequest.call(obj.router, req, res);
112
84
  });
113
- return;
85
+ } else {
86
+ console.log('HTTP server created - Without self managed certs');
87
+ obj.httpx = http.createServer((req, res) => {
88
+ console.log('Request received: ', req.url);
89
+ obj.router.onRequest.call(obj.router, req, res);
90
+ });
114
91
  }
115
-
116
- // -- Certs managed by NGINX, Google Cloud Run, etc.
117
- console.log('HTTP server created - Without self managed certs');
118
- obj.httpx = http.createServer((req, res) => {
119
- console.log('Request received: ', req.url);
120
- obj.router.onRequest.call(obj.router, req, res);
121
- });
92
+
93
+ // -- Set the port
94
+ obj.httpx.listen(obj.options.port || 8080);
122
95
  }
123
96
 
124
97
  log(value) {
@@ -129,11 +102,15 @@ class Server {
129
102
  }
130
103
  }
131
104
 
132
- onNotFound(request, response) {
133
-
134
- response.setStatus(404);
135
- response.respond();
136
- }
105
+ getCertificates() {
106
+
107
+ let obj = this;
108
+ return {
109
+ key: fs.readFileSync(obj.security.key, 'utf8'),
110
+ cert: fs.readFileSync(obj.security.cert, 'utf8'),
111
+ allowHTTP1: true
112
+ }
113
+ }
137
114
  }
138
115
 
139
116
  module.exports = Server;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vanilla-jet",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "VannilaJet framework",
5
5
  "main": "index.js",
6
6
  "bin": {