vanilla-jet 1.1.4 → 1.1.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.
@@ -46,13 +46,13 @@ function Dipper(options, shared) {
46
46
  Dipper.prototype.getPageTitle = function() {
47
47
 
48
48
  var obj = this;
49
- return obj.site_title;
49
+ return obj.page_title;
50
50
  }
51
51
 
52
52
  Dipper.prototype.getSiteTitle = function() {
53
53
 
54
54
  var obj = this;
55
- return obj.page_title;
55
+ return obj.site_title;
56
56
  }
57
57
 
58
58
  Dipper.prototype.getDescription = function() {
@@ -2,98 +2,111 @@ class Functions {
2
2
 
3
3
  constructor() {
4
4
 
5
- // Dipper
6
- dipper = global.dipper;
5
+ // Dipper
6
+ let dipper = global.dipper;
7
7
 
8
- // -- Hydrate
9
- Functions.hydrate(dipper);
8
+ // -- Hydrate
9
+ Functions.hydrate(dipper);
10
10
  }
11
11
 
12
12
  static hydrate(dipper) {
13
13
 
14
- // -- Get vanillaJet.package.json
15
- let json = dipper.openJsonFile('vanillaJet.package.json');
16
-
17
- // -- Google Fonts
18
- if (json.fonts && json.fonts.length > 0) {
19
- dipper.registerStyle('google-fonts', dipper.get_google_fonts(json.fonts));
20
- }
21
-
22
- // - Styles
23
- let stylesKeys = [];
24
- if (json.styles) {
25
-
26
- // -- Gettign the keys
27
- stylesKeys = Object.keys(json.styles);
28
-
29
- // -- Add google-fonts to keys
30
- stylesKeys.push('google-fonts');
31
-
32
- // -- Adding styles
33
- for (let key in json.styles) {
34
- // -- Check if init with http or https
35
- let url = json.styles[key];
36
- if (!/^(https?:\/\/|\/\/)/.test(url)) {
37
- dipper.registerStyle(key, dipper.style(url));
38
- } else {
39
- dipper.registerStyle(key, url);
40
- }
41
- }
42
- }
43
- dipper.registerStyle('app', dipper.style('app.min.css'), stylesKeys);
44
- dipper.enqueueStyle('app');
45
-
46
- // -- Scripts
47
- let scriptsKeys = [];
48
- if (json.dependencies) {
49
-
50
- // -- Adding scripts
51
- for (let key in json.dependencies) {
52
-
53
- // -- Key name and requires
54
- let keyParts = key.split(':');
55
- let keyName = keyParts[0];
56
- let keyRequires = keyParts[1] || '';
57
-
58
- // -- Add key to scriptsKeys
59
- scriptsKeys.push(keyName);
60
-
61
- // -- Check if init with http or https
62
- let url = json.dependencies[key];
63
- if (!/^(https?:\/\/|\/\/)/.test(url)) {
64
- dipper.registerScript(
65
- keyName,
66
- dipper.script(url),
67
- (keyRequires !== '') ? [keyRequires] : undefined
68
- );
69
- } else {
70
- dipper.registerScript(
71
- keyName,
72
- url,
73
- (keyRequires !== '') ? [keyRequires] : undefined
74
- );
75
- }
14
+ // -- Get vanillaJet.package.json
15
+ let json = dipper.openJsonFile('vanillaJet.package.json');
16
+
17
+ // -- Google Fonts
18
+ if (json.fonts && json.fonts.length > 0) {
19
+ dipper.registerStyle('google-fonts', dipper.get_google_fonts(json.fonts));
20
+ }
21
+
22
+ // - Styles
23
+ let stylesKeys = [];
24
+ if (json.styles) {
25
+
26
+ // -- Gettign the keys
27
+ stylesKeys = Object.keys(json.styles);
28
+
29
+ // -- Add google-fonts to keys
30
+ stylesKeys.push('google-fonts');
31
+
32
+ // -- Adding styles
33
+ for (let key in json.styles) {
34
+ // -- Check if init with http or https
35
+ let url = json.styles[key];
36
+ if (!/^(https?:\/\/|\/\/)/.test(url)) {
37
+ dipper.registerStyle(key, dipper.style(url));
38
+ } else {
39
+ dipper.registerStyle(key, url);
40
+ }
41
+ }
42
+ }
43
+ dipper.registerStyle('app', dipper.style('app.min.css'), stylesKeys);
44
+ dipper.enqueueStyle('app');
45
+
46
+ // -- Scripts
47
+ let scriptsKeys = [];
48
+ if (json.dependencies) {
49
+ // -- Adding scripts
50
+ for (let key in json.dependencies) {
51
+
52
+ // -- Key name and requires
53
+ let keyParts = key.split(':');
54
+ let keyName = keyParts[0];
55
+ let keyRequires = keyParts[1] || '';
56
+
57
+ // -- Add key to scriptsKeys
58
+ scriptsKeys.push(keyName);
59
+
60
+ // -- Check if init with http or https
61
+ let url = json.dependencies[key];
62
+ if (!/^(https?:\/\/|\/\/)/.test(url)) {
63
+ dipper.registerScript(
64
+ keyName,
65
+ dipper.script(url),
66
+ (keyRequires !== '') ? [keyRequires] : undefined
67
+ );
68
+ } else {
69
+ dipper.registerScript(
70
+ keyName,
71
+ url,
72
+ (keyRequires !== '') ? [keyRequires] : undefined
73
+ );
76
74
  }
77
75
  }
78
- scriptsKeys.push('jquery');
79
- scriptsKeys.push('underscore');
80
- dipper.registerScript('vanillaJet', dipper.script('core/vanillaJet.js'), scriptsKeys);
81
- dipper.enqueueScript('vanillaJet');
82
-
83
- // -- Main app
84
- dipper.registerScript('vanilla', dipper.script('vanilla.js'));
85
- dipper.enqueueScript('vanilla');
86
-
87
- // Adding meta tags
88
- dipper.addMeta('UTF-8', '', 'charset');
89
- dipper.addMeta('viewport', 'width=device-width, minimum-scale=1');
90
- dipper.addMeta('og:title', dipper.getPageTitle(), 'property');
91
- dipper.addMeta('og:site_name', dipper.getSiteTitle(), 'property');
92
- dipper.addMeta('og:description', dipper.getDescription(), 'property');
93
- dipper.addMeta('og:image', dipper.img('logo.png'), 'property');
94
- dipper.addMeta('og:type', 'website', 'property');
95
- dipper.addMeta('og:url', dipper.urlTo(''), 'property');
96
- dipper.addMeta('theme-color', '#ffffff', '');
76
+ }
77
+ scriptsKeys.push('jquery');
78
+ scriptsKeys.push('underscore');
79
+ dipper.registerScript('vanillaJet', dipper.script('core/vanillaJet.js'), scriptsKeys);
80
+ dipper.enqueueScript('vanillaJet');
81
+
82
+ // -- Main app
83
+ dipper.registerScript('vanilla', dipper.script('vanilla.js'));
84
+ dipper.enqueueScript('vanilla');
85
+
86
+ const basicMeta = [
87
+ { name: 'charset', content: 'UTF-8' },
88
+ { name: 'viewport', content: 'width=device-width, minimum-scale=1' },
89
+ { name: 'theme-color', content: '#ffffff' }
90
+ ];
91
+
92
+ const openGraphMeta = {
93
+ 'title': dipper.getPageTitle(),
94
+ 'site_name': dipper.getSiteTitle(),
95
+ 'description': dipper.getDescription(),
96
+ 'image': dipper.img('logo.png'),
97
+ 'type': 'website',
98
+ 'url': dipper.urlTo('')
99
+ };
100
+
101
+ // Add basic meta tags
102
+ basicMeta.forEach(meta => {
103
+ dipper.addMeta(meta.name, meta.content);
104
+ });
105
+
106
+ // Add Open Graph meta tags
107
+ Object.entries(openGraphMeta).forEach(([key, value]) => {
108
+ dipper.addMeta(`og:${key}`, value, 'property');
109
+ });
97
110
  }
98
111
  }
99
112
 
@@ -84,7 +84,10 @@ class Response {
84
84
 
85
85
  render(template) {
86
86
 
87
- var obj = this, path = require("path"), fs = require("fs"), template = 'pages/' + template;
87
+ let obj = this,
88
+ path = require("path"),
89
+ fs = require("fs"),
90
+ template = 'pages/' + template;
88
91
 
89
92
  const filename = path.join(process.cwd(), 'public/' + template);
90
93
  const fileStream = fs.createReadStream(filename);
@@ -43,30 +43,24 @@ class Router {
43
43
  }
44
44
  }
45
45
 
46
- removeRoute(method, route) {}
47
-
48
46
  onRequest(req, res) {
49
47
 
50
48
  let obj = this;
51
49
  let isMatch = false;
52
- let zlib = require('zlib');
53
50
  let response = new Response(res);
54
51
  let request = new Request(req, {
55
52
  onDataReceived: function () {
56
-
57
- //console.log(request.path);
58
53
  if (request.path == obj.server.options.base_url) {
59
54
  request.path = obj.server.options.base_url + obj.defaultRoute;
60
55
  }
61
- //console.log(request.path);
62
- // Try with the routes for the current method (get or post)
56
+ // -- Check GET or POST routes
63
57
  _.each(obj.routes[request.type], function (route) {
64
58
  if (request.path.match(route.regexp)) {
65
-
66
- var parts = route.handler.split('.'), clazz = parts[0], method = parts[1], callback = obj.validateCallback(clazz, method);
67
-
59
+ let parts = route.handler.split('.'),
60
+ clazz = parts[0],
61
+ method = parts[1],
62
+ callback = obj.validateCallback(clazz, method);
68
63
  if (callback && callback != undefined && callback != '') {
69
-
70
64
  isMatch = true;
71
65
  handled = callback(request, response, obj.server);
72
66
  return;
@@ -74,92 +68,47 @@ class Router {
74
68
  }
75
69
  });
76
70
 
77
- // If not handled yet, try with the wildcard ones
78
- if (!handled) {
79
- _.each(obj.routes['*'], function (route) {
80
-
81
- if (request.path.match(route.regexp)) {
82
-
83
- var parts = route.handler.split('.'), clazz = parts[0], method = parts[1], callback = obj.validateCallback(clazz, method);
84
-
85
- if (callback && callback != undefined && callback != '') {
86
-
87
- isMatch = true;
88
- handled = callback(request, response, obj.server);
89
- return;
90
- }
91
- }
92
- });
93
- }
94
-
95
- // No route catched, maybe it's a static content
96
- // or not handled? Well, at this point we call 404
97
- if (handled == false && isMatch == false) {
98
-
99
- var path = require('path'), ext = path.extname(req.url).replace('.', ''), extHandled = false, extHeader = {};
100
- var mimes = {
101
- 'png': 'image/png',
102
- 'webp': 'image/webp',
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'
113
- };
114
-
115
- var compressionMimes = {
116
- 'css': 'text/css',
117
- 'js': 'text/javascript',
118
- 'gz': 'application/x-gzip'
119
- };
120
-
121
- if (mimes[ext] != undefined && mimes[ext] != 'undefined') {
71
+ // -- Check static files
72
+ if (!handled && !isMatch) {
122
73
 
74
+ let ext = path.extname(req.url).replace('.', ''),
75
+ extHandled = false,
76
+ extHeader = {};
77
+
78
+ if (obj.mimes[ext] != undefined && obj.mimes[ext] != 'undefined') {
123
79
  extHandled = true;
124
- extHeader = { 'Content-Type': mimes[ext] };
80
+ extHeader = { 'Content-Type': obj.mimes[ext] };
125
81
  }
126
82
 
127
83
  if (extHandled) {
128
84
 
129
- let fs = require('fs'),
130
- rep = obj.cwd.replace('core/framework', ''),
85
+ let rep = obj.cwd.replace('core/framework', ''),
131
86
  route = request.path.replace(obj.server.options.base_url, ''),
132
87
  filename = path.join(rep, route),
133
88
  filePrivate = obj.isProtectedFile(route);
134
89
 
135
- fs.exists(filename, function (exists) {
136
-
137
- if (exists && !filePrivate) {
138
-
139
- var acceptEncoding = (req.headers['accept-encoding'] != undefined) ? req.headers['accept-encoding'] : '';
140
- var fileStream = fs.createReadStream(filename);
90
+ fs.stat(filename, (err, stats) => {
141
91
 
142
- if (ext === 'js' && !route.match(/(vanilla\.min\.js|vanillaJet\.min\.js)$/)) {
143
- extHeader['Cache-Control'] = 'public, max-age=15552000';
144
- extHeader['Expires'] = new Date(Date.now() + 15552000000).toUTCString();
145
- }
92
+ if (err || !stats.isFile() || filePrivate) {
93
+ return obj.onNotFound(response);
94
+ }
146
95
 
147
- if (acceptEncoding.match(/\bgzip\b/) && compressionMimes[ext] != undefined) {
148
- extHeader['Content-Encoding'] = 'gzip';
149
- res.writeHead(200, extHeader);
150
- fileStream.pipe(zlib.createGzip()).pipe(res);
151
- } else {
152
- res.writeHead(200, extHeader);
153
- fileStream.pipe(res);
154
- }
155
- return;
96
+ const acceptEncoding = req.headers['accept-encoding'] || '';
97
+ const fileStream = fs.createReadStream(filename);
98
+ fileStream.on('error', (streamErr) => {
99
+ //console.error("❌ Error leyendo archivo:", streamErr);
100
+ res.writeHead(500);
101
+ res.end('Error interno');
102
+ });
156
103
 
157
- } else {
104
+ extHeader['Content-Length'] = stats.size;
105
+ res.writeHead(200, extHeader);
106
+ fileStream.pipe(res);
158
107
 
159
- // Return 404
160
- obj.onNotFound(response);
161
- }
162
- });
108
+ res.on('close', () => {
109
+ //console.log("✅ Archivo servido y cerrado:", filename);
110
+ });
111
+ });
163
112
  }
164
113
  }
165
114
  }
@@ -177,8 +126,6 @@ class Router {
177
126
  return true;
178
127
  }
179
128
 
180
- validateExtension(route) {}
181
-
182
129
  validateCallback(clazz, method) {
183
130
 
184
131
  var obj = this, endpoints = obj.server.endpoints;
@@ -200,19 +147,18 @@ class Router {
200
147
  **/
201
148
  setDefaultRoute(route) {
202
149
 
203
- var obj = this;
150
+ let obj = this;
204
151
  obj.defaultRoute = route;
205
152
  }
206
153
 
207
154
  getDefaultRoute() {
208
155
 
209
- var obj = this, prev = (obj.server.options.base_url && obj.server.options.base_url != '' && obj.server.options.base_url != '/') ? obj.server.options.base_url : '';
210
-
156
+ let obj = this,
157
+ prev = (obj.server.options.base_url && obj.server.options.base_url != '' && obj.server.options.base_url != '/') ? obj.server.options.base_url : '';
211
158
  return (prev + obj.defaultRoute);
212
159
  }
213
160
 
214
161
  onNotFound(response) {
215
-
216
162
  response.setStatus(404);
217
163
  response.respond();
218
164
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vanilla-jet",
3
- "version": "1.1.4",
3
+ "version": "1.1.5",
4
4
  "description": "VannilaJet framework",
5
5
  "main": "index.js",
6
6
  "bin": {