@blaasvaer/frmwrk 0.1.4 → 0.1.8

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.
@@ -0,0 +1,25 @@
1
+ {
2
+ "workbench.colorCustomizations": {
3
+ "activityBar.activeBackground": "#2f7c47",
4
+ "activityBar.activeBorder": "#422c74",
5
+ "activityBar.background": "#2f7c47",
6
+ "activityBar.foreground": "#e7e7e7",
7
+ "activityBar.inactiveForeground": "#e7e7e799",
8
+ "activityBarBadge.background": "#422c74",
9
+ "activityBarBadge.foreground": "#e7e7e7",
10
+ "statusBar.background": "#215732",
11
+ "statusBar.foreground": "#e7e7e7",
12
+ "statusBarItem.hoverBackground": "#2f7c47",
13
+ "titleBar.activeBackground": "#215732",
14
+ "titleBar.activeForeground": "#e7e7e7",
15
+ "titleBar.inactiveBackground": "#21573299",
16
+ "titleBar.inactiveForeground": "#e7e7e799",
17
+ "editorGroup.border": "#2f7c47",
18
+ "panel.border": "#2f7c47",
19
+ "sash.hoverBorder": "#2f7c47",
20
+ "sideBar.border": "#2f7c47",
21
+ "statusBarItem.remoteBackground": "#215732",
22
+ "statusBarItem.remoteForeground": "#e7e7e7"
23
+ },
24
+ "peacock.color": "#215732"
25
+ }
package/connections.js CHANGED
@@ -1,4 +1,3 @@
1
- const path = require('path');
2
1
  const utils = require('./utils');
3
2
 
4
3
  /**
@@ -7,7 +6,6 @@ const utils = require('./utils');
7
6
  function installconnections ( config ) {
8
7
  FW.connections = FW.connections || {};
9
8
 
10
- console.log("Installing connections …", FW.connections);
11
9
  let connections_dir = config.root + '/connections/';
12
10
 
13
11
  return utils.readdirAsync( connections_dir )
@@ -24,18 +22,23 @@ console.log("Installing connections …", FW.connections);
24
22
 
25
23
  let connection = require( connections_dir + file );
26
24
 
27
- FW.connections[ path.parse(file).name ] = connection;
28
-
29
- // Check for config connection type
30
- if ( config.connection && FW.connections[ config.connection ] ) {
31
- FW.database = FW.connections[ config.connection ];
32
- console.log("FW.database:", FW.database);
25
+ if ( connection ) {
26
+ // FW.connections[ path.parse(file).name ] = connection;
27
+ FW.connections[ file.split('.')[0] ] = connection;
28
+
29
+ // Check for config connection type
30
+ if ( config.connection && FW.connections[ config.connection ] ) {
31
+ FW.database = FW.connections[ config.connection ];
32
+ }
33
+
34
+ resolve( file );
35
+ } else {
36
+ reject( "Loading of connection: " + file + " failed!" );
33
37
  }
34
38
  });
35
39
 
36
40
  connection_files.push( promise );
37
41
  });
38
- // console.log("connection file require()", FW.connections);
39
42
 
40
43
  return Promise.all( connection_files );
41
44
  });
package/controllers.js CHANGED
@@ -1,10 +1,9 @@
1
- const utils = require('./utils');
1
+ const utils = require('@blaasvaer/frmwrk/utils');
2
2
 
3
3
  /**
4
4
  * Install Controllers
5
5
  */
6
6
  function installControllers ( config ) {
7
- // console.log("Installing controllers …", config);
8
7
  let controllers_dir = config.root + '/controllers/';
9
8
 
10
9
  return utils.readdirAsync( controllers_dir )
@@ -38,7 +37,7 @@ function installControllers ( config ) {
38
37
 
39
38
  controller_files.push( promise );
40
39
  });
41
- // console.log("This controllers: ", this.controllers);
40
+
42
41
  return Promise.all( controller_files );
43
42
  });
44
43
  }
package/handle-request.js CHANGED
@@ -3,8 +3,8 @@
3
3
  const fs = require('fs');
4
4
  const path = require('path');
5
5
  const statics = require('serve-handler');
6
- // const Utils = require('./utils');
7
- // const Controller = require('./Controller');
6
+ const Url = require('url');
7
+ const { findRoute, Router } = require('./router');
8
8
 
9
9
  const content_types = {
10
10
  "text" : 'text/plain; charset=utf-8',
@@ -18,15 +18,9 @@ const content_types = {
18
18
  * @param {Object} res The response object
19
19
  * @return {Object} Return the response
20
20
  */
21
- function handleRequest( req, res ) {
22
- /**
23
- * TEMP
24
- * @type {[type]}
25
- */
26
- const SOME_CONTROLLER_EXISTS = true;
27
- const method = req.method;
28
- const url = FW.utils.removeTrailingSlashes( req.url );
29
- const query = url.split('/').splice(3); // Get url parts as parameters
21
+ async function handleRequest( req, res ) {
22
+ // console.log("Cookies",req.headers);
23
+ const request_url = new URL( req.url, `http://${req.headers.host}`);
30
24
 
31
25
  /**
32
26
  * Set default status and header
@@ -36,14 +30,11 @@ function handleRequest( req, res ) {
36
30
  let content_type = 'html';
37
31
 
38
32
  // Prevent errors from missing favicon
39
- if (url === '/favicon.ico') {
40
- /**
41
- * TODO: Set theme dynamically
42
- */
33
+ if (request_url.pathname === '/favicon.ico') {
34
+ // TODO: Set theme dynamically
43
35
  var favicon = path.join('themes/default', 'favicon.ico');
44
36
  res.statusCode = 200;
45
37
  res.setHeader('Content-Type', 'image/x-icon');
46
- // const icon = fs.createReadStream( favicon );
47
38
 
48
39
  if ( fs.existsSync( favicon ) ) {
49
40
  fs.createReadStream( favicon ).pipe( res );
@@ -53,44 +44,22 @@ function handleRequest( req, res ) {
53
44
  }
54
45
  }
55
46
 
56
- statics( req, res );
57
-
58
47
  /**
59
48
  * Find route in Router based on url
60
49
  */
61
- const route = global.routes.find(( obj ) => obj.route === url);
62
- const controller = url.split('/')[1];
63
- console.log("Controller", controller);
50
+ // const route = global.routes.find( ( obj ) => obj.route === FW.utils.removeTrailingSlashes( request_url.pathname ) );
51
+ const route = findRoute( request_url.pathname );
52
+ // console.log("route", route);
64
53
  /**
65
54
  * Check for complete route
66
55
  * @param {[type]} route [description]
67
56
  * @return {[type]} [description]
68
57
  */
69
- console.log("route",url);
70
58
  if ( route ) {
71
59
  /**
72
60
  * Create new controller from route
73
61
  */
74
- const controller = new Controller( route, req, res, query );
75
-
76
- /**
77
- * TODO
78
- * Figure out how to receive data and handle it properly (in a general and robust way) in routing
79
- */
80
-
81
- /**
82
- * Prepare body
83
- * @type {String}
84
- */
85
- let body = '';
86
-
87
- req.on('data', ( chunk ) => {
88
- body += chunk;
89
- });
90
-
91
- req.on('end', () => {
92
- controller.addBody( body );
93
- })
62
+ const controller = new Controller( route, req, res );
94
63
 
95
64
  let response_output = 'Nothing here …';
96
65
 
@@ -98,13 +67,13 @@ console.log("route",url);
98
67
  /**
99
68
  * If the route has no parameters, then just run the route method and serve whatever it may …
100
69
  */
101
- response_output = controller.route.action.call();
70
+ response_output = await controller.route.action( route );
102
71
  } else {
103
72
  /**
104
73
  * Authorization
105
74
  */
106
75
  if ( Authorize( controller.route ) ) {
107
- response_output = controller.route.action.call();
76
+ response_output = await controller.route.action( route );
108
77
  } else {
109
78
  /**
110
79
  * Redirect the user to login if auth fails
@@ -113,29 +82,33 @@ console.log("route",url);
113
82
  status_code = 302;
114
83
  res.setHeader( 'Location', controller.route.params.access.redirect );
115
84
  }
116
- console.log("NO USER", controller.route.params.redirect);
85
+ // console.log("NO USER", controller.route.params.redirect);
117
86
  // response_output = redirect.action.call();
118
87
  // route.params.redirect.call();
119
88
  // response_output = 'Access denied';
120
89
  }
121
-
90
+
122
91
  if ( controller.route.params.type ) {
123
92
  content_type = controller.route.params.type;
124
93
  }
125
- console.log("content_type:", content_type);
126
94
  }
127
- console.log("response_output:", typeof response_output);
128
95
 
129
96
  res.statusCode = status_code;
130
97
  res.setHeader( 'Content-Type', content_types[content_type] );
98
+
99
+ // Cookies
100
+ res.setHeader('Set-Cookie','uuid=1234-1324-1234-1234-1234; Max-Age=3000; SameSite=lax; Secure');
101
+ // Set a same-site cookie for first-party contexts
102
+ // res.cookie('cookie1', 'value1', { sameSite: 'lax' });
103
+ // Set a cross-site cookie for third-party contexts
104
+ // res.cookie('cookie2', 'value2', { sameSite: 'none', secure: true });
105
+
131
106
  res.end( response_output );
132
- } else if ( url.split('/')[1] == 'themes' ) {
107
+ } else if ( request_url.pathname.split('/')[1] == 'themes' ) {
133
108
  /**
134
109
  * Serve static files from public directory
135
110
  */
136
- // req.addListener('end', function () {
137
- // statics( req, res );
138
- // }).resume();
111
+ statics( req, res );
139
112
 
140
113
  return;
141
114
  // } else if ( SOME_CONTROLLER_EXISTS ) {
@@ -145,7 +118,7 @@ console.log("response_output:", typeof response_output);
145
118
  } else {
146
119
  res.statusCode = 404;
147
120
  res.setHeader('Content-Type', 'text/plain; charset=utf-8' );
148
- res.end("404 – Page not found: " + url + ".\n\nDo you have a controller with an action for that route?");
121
+ res.end("404 – Page not found: " + request_url + ".\n\nDo you have a controller with an action for that route?");
149
122
  }
150
123
  }
151
124
 
package/index.js CHANGED
@@ -10,19 +10,17 @@
10
10
  * Native modules
11
11
  */
12
12
  const http = require('http');
13
-
14
13
  /**
15
14
  * Custom modules
16
15
  */
17
16
  const handleRequest = require('./handle-request');
18
- const router = require('./router');
19
17
  const Utils = require('./utils');
20
18
 
21
19
  /**
22
20
  * Define variables
23
21
  */
24
22
  let hostname,
25
- server;
23
+ server;
26
24
 
27
25
  /**
28
26
  * If port number is passed in as first (custom) parameter, set port
@@ -40,20 +38,21 @@ if ( process.env.NODE_ENV !== 'production') {
40
38
  * @param {object} config The configuration object
41
39
  * @return {Node} Node
42
40
  */
43
- FW = function ( config ) {
41
+ FW = function ( config ) {
42
+ let _this = this;
43
+
44
44
  hostname = config.hostname ? config.hostname : '127.0.0.1';
45
45
 
46
- this.controllers = [];
47
- this.models = [];
46
+ controllers = {};
47
+ models = {};
48
48
 
49
49
  /**
50
50
  * Create and start the server
51
51
  * @return {function} Server instance
52
52
  */
53
- http.createServer( handleRequest ).listen(port, hostname, () => {
53
+ http.createServer( handleRequest ).listen(port, hostname, () => {
54
54
  console.log(`Server running at http://${hostname}:${port}`);
55
55
  });
56
- // console.log("This.server:", this.server);
57
56
 
58
57
  /**
59
58
  * Set the Controller
@@ -61,11 +60,11 @@ if ( process.env.NODE_ENV !== 'production') {
61
60
  * @param {object} req The incomming request object
62
61
  * @param {object} res The response object
63
62
  */
64
- Controller = function Controller ( route, req, res, query ) {
63
+ Controller = function Controller ( route, req, res ) {
64
+ // console.log("Controller in index:\n", _this);
65
65
  this.route = route;
66
66
  this.req = req;
67
67
  this.res = res;
68
- this.query = query;
69
68
 
70
69
  /**
71
70
  * Check for authorization parameters.
@@ -75,32 +74,31 @@ if ( process.env.NODE_ENV !== 'production') {
75
74
  console.log("This route should be authorized!");
76
75
  }
77
76
  }
78
-
77
+
79
78
  return this;
80
79
  }
81
80
 
82
- Controller.prototype.addBody = function addBody ( body ) {
83
- this.body = body;
84
- console.log("Controller.addBody:", this.body);
85
- }
86
-
87
81
  /**
88
- * Authorize user
82
+ * TODO: Authorize user
89
83
  */
90
84
  Authorize = function Authorize ( credentials ) {
91
- // console.log("Authorize - credentials:", credentials, this);
85
+ // console.log("Authorize - check credentials:", credentials);
92
86
  return true;
93
87
  }
94
88
 
95
89
  /**
96
90
  * Install framework components and start the server
97
91
  */
98
- Install( config ).then(() => {
92
+ Install( config )
93
+ .then(( result ) => {
99
94
  /**
100
95
  * Run post install processes here …
101
96
  */
102
- });
103
- // console.log("This:", this);
97
+ // console.log("Install result:", routes);
98
+ })
99
+ .catch(function( err ) {
100
+ console.log("Install Error:", err);
101
+ });;
104
102
  };
105
103
 
106
104
  /**
@@ -125,15 +123,7 @@ global.Install = function Install ( config ) {
125
123
  // require('./users')( config )
126
124
  ];
127
125
 
128
- return Promise.all( install_processes )
129
- .then(function( result ) {
130
- console.log("Install result", result);
131
- // console.log("Routes", global.routes );
132
- // console.log("User:", global.users );
133
- })
134
- .catch(function( err ) {
135
- console.log("Install Error:", err);
136
- });
126
+ return Promise.all( install_processes );
137
127
  }
138
128
 
139
129
  FW.utils = Utils;
package/models.js CHANGED
@@ -43,7 +43,7 @@ function installModels ( config ) {
43
43
 
44
44
  model_files.push( promise );
45
45
  });
46
- // console.log("This models: ", this.models);
46
+
47
47
  return Promise.all( model_files );
48
48
  });
49
49
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blaasvaer/frmwrk",
3
- "version": "0.1.4",
3
+ "version": "0.1.8",
4
4
  "description": "My personal Node framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/router.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * This is the router for the F framework.
2
+ * This is the router for the fw framework.
3
3
  *
4
4
  * @author Sam Blåsvær <sam@blaasvaer.dk>
5
5
  * Version 1.0.0
@@ -10,16 +10,108 @@ global.routes = [];
10
10
  global.ROUTE = function ( route, action, params = null ) {
11
11
  let r = {
12
12
  "controller": route.split('/')[1] == '' ? 'index' : route.split('/')[1],
13
+ "resource": route.split('/')[2] == '' ? 'index' : route.split('/')[2],
14
+ "resource_id": route.split('/')[3] == '' ? 'index' : route.split('/')[3],
13
15
  "route": route,
14
16
  "action": action,
15
17
  "params": params,
16
18
  }
17
- // console.log("Router params:", params);
19
+ // console.log("Router params:", params?.type);
20
+ // console.log("----------------------");
21
+ // console.log("Router route:", route);
22
+ // console.log("Router controller:", r.controller);
23
+ // console.log("Router resource:", r.resource);
24
+ // console.log("Router resource_id:", r.resource_id);
25
+ // console.log("Router route:", r.route);
26
+ // console.log("Router action:", r.action);
27
+ // console.log("Router params:", r.params);
18
28
  global.routes.push( r );
19
29
  };
20
30
 
21
- let Router = function () {
22
- // console.log("Router");
31
+ function Router ( req, url ) {
32
+ const method = req.method;
33
+ console.log("Router url:", url);
34
+ // Split url into array
35
+ let url_array = url.pathname.split('/');
36
+
37
+ // Remove first empty element
38
+ url_array.shift();
39
+
40
+ // Set controller, resource, resource_id, search, searchParams
41
+ this.controller = url_array[0];
42
+ this.resource = url_array[1];
43
+ this.resource_id = url_array[2];
44
+ this.search = url_array[3];
45
+ this.searchParams = url_array[4];
46
+
47
+ // console.log("referer", referer);
48
+ // console.log("url", url);
49
+ // console.log("this.controller", this.controller);
50
+ // console.log("this.resource", this.resource);
51
+ // console.log("this.resource_id", this.resource_id);
52
+ // console.log("this.search", this.search);
53
+ // console.log("this.searchParams", this.searchParams);
54
+
55
+ return this;
23
56
  };
24
57
 
25
- module.exports = new Router();
58
+ function findRoute ( url ) {
59
+ // Check if route exist
60
+ let route = global.routes.find( ( obj ) => obj.route === FW.utils.removeTrailingSlashes( url ) );
61
+
62
+ if ( route ) {
63
+ // console.log("Route defined", url);
64
+ // Execute action for route
65
+ return route;
66
+ } else {
67
+ const url_array = url.split('/').filter( e => e );
68
+
69
+ // Check if route is an API call
70
+ let isAPICall = url.split('/')[1] === 'api' ? true : false;
71
+
72
+ if ( isAPICall ) {
73
+ // console.log("Route NOT defined (API)", url, url.split('/')[1]);
74
+ /**
75
+ * If checking for UUID use:
76
+ * UUID v1:
77
+ * /^[0-9A-F]{8}-[0-9A-F]{4}-[1][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
78
+ * UUID v2:
79
+ * /^[0-9A-F]{8}-[0-9A-F]{4}-[2][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
80
+ * UUID v3:
81
+ * /^[0-9A-F]{8}-[0-9A-F]{4}-[3][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
82
+ * UUID v4:
83
+ * /^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
84
+ * UUID v5:
85
+ * /^[0-9A-F]{8}-[0-9A-F]{4}-[5][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
86
+ */
87
+
88
+ let regex = /()/;
89
+
90
+ switch ( url_array.length ) {
91
+ case 2:
92
+ regex = /api\/(?<resource>[a-z_]+)/; // match /api/<resource>
93
+ route = global.routes.find( ( obj ) => obj.route === '/api/:resources' );
94
+ break;
95
+ case 3:
96
+ regex = /api\/(?<resource>[a-z_]+)\/(?<id>[0-9]+)/; // match /api/<resource>/<id>
97
+ route = global.routes.find( ( obj ) => obj.route === '/api/:resources/:id' );
98
+ break;
99
+ }
100
+
101
+ let regex_result = url.match(regex);
102
+ let regex_groups = regex_result.groups;
103
+
104
+ route.resource = regex_groups.resource;
105
+ route.resource_id = regex_groups.id;
106
+
107
+ return route;
108
+ } else {
109
+ return global.routes.find( ( obj ) => obj.route === FW.utils.removeTrailingSlashes( url ) );
110
+ }
111
+ }
112
+ }
113
+
114
+ module.exports = {
115
+ Router,
116
+ findRoute
117
+ }
package/views.js CHANGED
@@ -13,6 +13,7 @@
13
13
  */
14
14
  const { readdir, readFile, stat } = require("fs").promises;
15
15
  const { basename, join } = require("path");
16
+
16
17
  /**
17
18
  * 3rd party modules
18
19
  */
@@ -22,7 +23,7 @@ const htmlclean = require('htmlclean');
22
23
  /**
23
24
  * Custom modules
24
25
  */
25
- const utils = require('./utils');
26
+ const utils = require('@blaasvaer/frmwrk/utils');
26
27
 
27
28
  /**
28
29
  * Compile views on application initialization.
@@ -35,7 +36,12 @@ function Views ( config ) {
35
36
  .isFile()
36
37
  ? readFile( path )
37
38
  .then(function ( template ) {
38
- let tpl = 'Handlebars.template(' + Handlebars.precompile( htmlclean( template.toString() ) ) + ')';
39
+ let tpl = 'Handlebars.template(' + Handlebars.precompile( template.toString() ) + ')';
40
+
41
+ if ( config.compress_html ) {
42
+ tpl = 'Handlebars.template(' + Handlebars.precompile( htmlclean( template.toString() ) ) + ')';
43
+ }
44
+
39
45
  return eval( tpl );
40
46
  })
41
47
  .catch(function ( err ) {
@@ -56,7 +62,6 @@ function Views ( config ) {
56
62
  dir2obj ( views_dir )
57
63
  .then( function ( output ) {
58
64
  global.views = output;
59
- // console.log('global.views:', global.views);
60
65
  })
61
66
  }
62
67