binhend 1.1.4 → 1.1.6

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.
@@ -7,6 +7,7 @@ new Binh()
7
7
  .global('config', binh.config)
8
8
  .config('config')
9
9
  .api('routes')
10
+ .cors()
10
11
  .port('PORT')
11
12
  .cluster.fork(0)
12
13
  .start(function(server, configs) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -8,15 +8,20 @@ const { APIs, Router } = require('./api');
8
8
  const maxcpus = os.cpus().length;
9
9
 
10
10
  function ServerApp(binh, Binh, configloader) {
11
- var forkcount = 0,
12
- rootpath = Binh.getRootpath();
11
+ var rootpath = Binh.getRootpath(),
12
+ forkcount = 0, enabledCORS, pathAPI, portNumber;
13
13
 
14
14
  binh.router = function(module) {
15
15
  return Router(module);
16
16
  };
17
17
 
18
- Binh.api = function(api_path) {
19
- Binh.api.value = path.join(rootpath, api_path);
18
+ Binh.api = function(apiPath) {
19
+ pathAPI = path.join(rootpath, apiPath);
20
+ return Binh;
21
+ };
22
+
23
+ Binh.cors = function(input) {
24
+ enabledCORS = input instanceof Function ? input : (input === false ? false : true);
20
25
  return Binh;
21
26
  };
22
27
 
@@ -33,10 +38,10 @@ function ServerApp(binh, Binh, configloader) {
33
38
 
34
39
  Binh.port = function(port) {
35
40
  if (typeof port === 'number') {
36
- Binh.port.value = port;
41
+ portNumber = port;
37
42
  }
38
43
  else if (typeof port === 'string') {
39
- Binh.port.value = binh.config(port);
44
+ portNumber = binh.config(port);
40
45
  }
41
46
  return Binh;
42
47
  };
@@ -55,8 +60,9 @@ function ServerApp(binh, Binh, configloader) {
55
60
  function launch(callback) {
56
61
  configloader.done(function(configs) {
57
62
  new Server({
58
- port: Binh.port.value,
59
- api: APIs(Binh.api.value),
63
+ port: portNumber,
64
+ api: APIs(pathAPI),
65
+ cors: enabledCORS,
60
66
  web: Binh.web.value,
61
67
  callback: callback,
62
68
  configs: configs
@@ -4,42 +4,42 @@ const path = require('path');
4
4
  const Component = require('./component');
5
5
 
6
6
  function WebBuilder(binh, Binh) {
7
- binh.ui = function(amodule, component, htmltags, links) {
7
+ binh.ui = function(module, component, htmltags, links) {
8
8
  component.htmltags = htmltags;
9
9
  component.links = links;
10
- binh.bundle('ui', amodule, component);
10
+ binh.bundle('ui', module, component);
11
11
  };
12
12
 
13
- binh.service = function(amodule, component, links) {
13
+ binh.service = function(module, component, links) {
14
14
  component.links = links;
15
- binh.bundle('service', amodule, component);
15
+ binh.bundle('service', module, component);
16
16
  };
17
17
 
18
- binh.style = function(amodule, component) {
19
- binh.bundle('style', amodule, component);
18
+ binh.style = function(module, component) {
19
+ binh.bundle('style', module, component);
20
20
  };
21
21
 
22
- binh.css = function(amodule, cssFilename) {
23
- var cssFilePath = cssFilename && path.join(amodule.path, cssFilename) || amodule.filename.replace(/.js$/, '');
22
+ binh.css = function(module, cssFilename) {
23
+ var cssFilePath = cssFilename && path.join(module.path, cssFilename) || module.filename.replace(/.js$/, '');
24
24
  var content = require('fs').readFileSync(cssFilePath, { encoding: 'utf8', flag: 'r' });
25
25
  content = content.replace(/\s+/g, ' ');
26
26
  var component = new Function(`return ${JSON.stringify(content)};`);
27
- binh.bundle('style', amodule, component);
27
+ binh.bundle('style', module, component);
28
28
  };
29
29
 
30
- binh.bundle = function(type, amodule, component) {
30
+ binh.bundle = function(type, module, component) {
31
31
  if (!(component instanceof Function)) return;
32
32
 
33
33
  component.type = type;
34
- component.filename = amodule.filename;
34
+ component.filename = module.filename;
35
35
  component.constructor = Component;
36
- component.module = amodule;
36
+ component.module = module;
37
37
  component.as = alias;
38
38
  component.vars = {};
39
39
 
40
- amodule.exports = component;
40
+ module.exports = component;
41
41
 
42
- amodule.children.forEach(function(child) {
42
+ module.children.forEach(function(child) {
43
43
  child = child.exports;
44
44
  if (child.constructor !== Component) return;
45
45
  if (child.alias) component.vars[child.filename] = child.alias;
package/src/code.js CHANGED
@@ -125,15 +125,29 @@ function IIF(codes) {
125
125
  }
126
126
 
127
127
  function distinctValues(component, key) {
128
- var arrays = [component[key]];
128
+ var arrays = [component[key]], ref = {};
129
+
130
+ ref[component.filename] = true;
129
131
 
130
132
  component.module.children.forEach(function(child) {
133
+ if (ref[child.filename]) return;
134
+ ref[child.filename] = true;
131
135
  arrays.push(child.exports[key]);
136
+ getArraysFromDeeperChildren(ref, arrays, child, key);
132
137
  });
133
138
 
134
139
  return uniquefy(arrays);
135
140
  }
136
141
 
142
+ function getArraysFromDeeperChildren(ref, arrays, module, key) {
143
+ module.children.forEach(function(child) {
144
+ if (ref[child.filename]) return;
145
+ ref[child.filename] = true;
146
+ arrays.push(child.exports[key]);
147
+ getArraysFromDeeperChildren(ref, arrays, child, key);
148
+ });
149
+ }
150
+
137
151
  function uniquefy(arrays) {
138
152
  var map = {};
139
153
 
package/src/cors.js ADDED
@@ -0,0 +1,17 @@
1
+
2
+ function defaultCORS(req, res, next) {
3
+ res.header('Access-Control-Allow-Origin', req.get('Origin') || '*');
4
+ res.header('Access-Control-Allow-Credentials', 'true');
5
+ res.header('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE');
6
+ res.header('Access-Control-Expose-Headers', 'Content-Length');
7
+ res.header('Access-Control-Allow-Headers', 'Accept, Authorization, Content-Type, X-Requested-With, Range');
8
+ if (req.method === 'OPTIONS') {
9
+ return res.send(200);
10
+ } else {
11
+ return next();
12
+ }
13
+ }
14
+
15
+ module.exports = {
16
+ defaultCORS
17
+ };
package/src/server.js CHANGED
@@ -1,9 +1,19 @@
1
+
1
2
  const express = require('express');
3
+ const { defaultCORS } = require('./cors');
2
4
 
3
5
  function Server(options) {
4
6
  const server = express();
5
7
  const PORT = options.port || 1300;
6
8
  const WEB_DIRECTORY = options.web;
9
+ const CORS = options.cors;
10
+
11
+ if (CORS instanceof Function) {
12
+ server.use(CORS);
13
+ }
14
+ else if (CORS === true) {
15
+ server.use(defaultCORS);
16
+ }
7
17
 
8
18
  if (WEB_DIRECTORY) {
9
19
  server.use(express.static(WEB_DIRECTORY));
@@ -16,7 +26,7 @@ function Server(options) {
16
26
  if (WEB_DIRECTORY) {
17
27
  server.get('/*', (req, res) => res.sendFile('index.html', { root: WEB_DIRECTORY }));
18
28
  }
19
-
29
+
20
30
  server.listen(PORT, function() {
21
31
  console.log(`[NODE] Server is listening on http://localhost:${PORT}/`);
22
32