vanilla-jet 1.0.1 → 1.0.3
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/server.js +82 -83
- 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/server.js
CHANGED
|
@@ -6,111 +6,110 @@
|
|
|
6
6
|
const _ = require('underscore');
|
|
7
7
|
const http = require('http');
|
|
8
8
|
const http2 = require('http2');
|
|
9
|
-
const url = require('url') ;
|
|
10
9
|
const fs = require('fs');
|
|
11
10
|
const nunjucks = require('nunjucks');
|
|
12
11
|
const path = require('path');
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
var Functions = require('../external/functions.js');
|
|
13
|
+
let Router = require('./router.js');
|
|
14
|
+
let Dipper = require('./dipper.js');
|
|
17
15
|
|
|
18
|
-
|
|
16
|
+
class Server {
|
|
19
17
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
constructor(options, functionsModule) {
|
|
19
|
+
|
|
20
|
+
console.log(functionsModule);
|
|
21
|
+
|
|
22
|
+
this.httpx = null;
|
|
23
|
+
this.verbose = true;
|
|
24
|
+
this.router = null;
|
|
25
|
+
this.functions = null;
|
|
26
|
+
this.dipper = null;
|
|
27
|
+
this.init(options, functionsModule);
|
|
28
|
+
}
|
|
27
29
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
opts
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
base_url: '',
|
|
38
|
-
site_url: '',
|
|
39
|
-
wsServer: false,
|
|
40
|
-
onNotFound: obj.onNotFound
|
|
41
|
-
});
|
|
42
|
-
obj.options = opts;
|
|
43
|
-
|
|
44
|
-
_.defaults(security, {
|
|
45
|
-
pass_salt: '1234567890',
|
|
46
|
-
token_salt: '0987654321',
|
|
47
|
-
version: '1.0'
|
|
48
|
-
});
|
|
49
|
-
obj.security = security;
|
|
50
|
-
|
|
51
|
-
// Render
|
|
52
|
-
global.render = nunjucks.configure(path.join(process.cwd(), 'assets'), {
|
|
53
|
-
autoescape: false,
|
|
54
|
-
throwOnUndefined: true,
|
|
55
|
-
noCache: true,
|
|
56
|
-
});
|
|
57
|
-
global.dipper = new Dipper(opts, shared);
|
|
58
|
-
|
|
59
|
-
// Cheking for http or https
|
|
60
|
-
if (/^((http):\/\/)/.test(obj.options.site_url) || /^((localhost))/.test(obj.options.site_url)) {
|
|
61
|
-
|
|
62
|
-
obj.httpx = http.createServer((req, res) => {
|
|
63
|
-
//console.log(req.url);
|
|
64
|
-
obj.router.onRequest.call(obj.router, req, res);
|
|
30
|
+
init(options, functionsModule) {
|
|
31
|
+
|
|
32
|
+
let obj = this, settings = options.settings, opts = settings[options.profile] || {}, shared = settings['shared'] || {}, security = settings['security'] || {};
|
|
33
|
+
|
|
34
|
+
_.defaults(opts, {
|
|
35
|
+
base_url: '',
|
|
36
|
+
site_url: '',
|
|
37
|
+
wsServer: false,
|
|
38
|
+
onNotFound: obj.onNotFound
|
|
65
39
|
});
|
|
40
|
+
obj.options = opts;
|
|
66
41
|
|
|
67
|
-
|
|
42
|
+
_.defaults(security, {
|
|
43
|
+
pass_salt: '1234567890',
|
|
44
|
+
token_salt: '0987654321',
|
|
45
|
+
version: '1.0'
|
|
46
|
+
});
|
|
47
|
+
obj.security = security;
|
|
68
48
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
obj.httpx = http2.createSecureServer(certs, (req, res) => {
|
|
75
|
-
obj.router.onRequest.call(obj.router, req, res);
|
|
49
|
+
// Render
|
|
50
|
+
global.render = nunjucks.configure(path.join(process.cwd(), 'assets'), {
|
|
51
|
+
autoescape: false,
|
|
52
|
+
throwOnUndefined: true,
|
|
53
|
+
noCache: true,
|
|
76
54
|
});
|
|
77
|
-
|
|
55
|
+
global.dipper = new Dipper(opts, shared);
|
|
78
56
|
|
|
79
|
-
|
|
57
|
+
// Cheking for http or https
|
|
58
|
+
if (/^((http):\/\/)/.test(obj.options.site_url) || /^((localhost))/.test(obj.options.site_url)) {
|
|
80
59
|
|
|
81
|
-
|
|
82
|
-
|
|
60
|
+
obj.httpx = http.createServer((req, res) => {
|
|
61
|
+
//console.log(req.url);
|
|
62
|
+
obj.router.onRequest.call(obj.router, req, res);
|
|
63
|
+
});
|
|
83
64
|
|
|
84
|
-
|
|
85
|
-
obj.functions = new Functions(obj);
|
|
86
|
-
};
|
|
65
|
+
} else if (/^((https):\/\/)/.test(obj.options.site_url)) {
|
|
87
66
|
|
|
88
|
-
|
|
67
|
+
certs = {
|
|
68
|
+
key: fs.readFileSync(obj.security.key, 'utf8'),
|
|
69
|
+
cert: fs.readFileSync(obj.security.cert, 'utf8'),
|
|
70
|
+
allowHTTP1: true
|
|
71
|
+
};
|
|
72
|
+
obj.httpx = http2.createSecureServer(certs, (req, res) => {
|
|
73
|
+
obj.router.onRequest.call(obj.router, req, res);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
89
76
|
|
|
90
|
-
|
|
91
|
-
var port = (obj.options.port && obj.options.port != '') ? obj.options.port : 8080;
|
|
77
|
+
obj.onNotFound = opts.onNotFound;
|
|
92
78
|
|
|
93
|
-
|
|
94
|
-
|
|
79
|
+
// Initialize router
|
|
80
|
+
obj.router = new Router(obj);
|
|
95
81
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
82
|
+
// Setting endpoints
|
|
83
|
+
obj.functions = new functionsModule(obj);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
start() {
|
|
101
87
|
|
|
102
|
-
|
|
88
|
+
let obj = this;
|
|
89
|
+
let port = (obj.options.port && obj.options.port != '') ? obj.options.port : 8080;
|
|
103
90
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
91
|
+
// Listen on the specified port
|
|
92
|
+
obj.httpx.listen(port);
|
|
93
|
+
|
|
94
|
+
// Logging
|
|
95
|
+
obj.log('__________________ Dragonfly server started ___________________');
|
|
96
|
+
obj.log(` > Listening on ${obj.options.site_url}:${obj.options.port}${obj.options.base_url} < `);
|
|
97
|
+
obj.log('-----------------------------------------------------------------');
|
|
107
98
|
}
|
|
108
|
-
};
|
|
109
99
|
|
|
110
|
-
|
|
100
|
+
log(value) {
|
|
111
101
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
102
|
+
let obj = this;
|
|
103
|
+
if (obj.verbose) {
|
|
104
|
+
console.log(value);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
onNotFound(request, response) {
|
|
109
|
+
|
|
110
|
+
response.setStatus(404);
|
|
111
|
+
response.respond();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
115
114
|
|
|
116
115
|
module.exports = Server;
|