binhend 2.1.29 → 2.1.31

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/demo.js CHANGED
@@ -1,5 +1,6 @@
1
1
 
2
2
  const { HttpCodes, ConfigLoader, WebBuilder, HttpError, validation } = require('./index');
3
+ const path = require('path');
3
4
 
4
5
  // var jsonPath = __dirname + '/jsconfig.json';
5
6
  // var cc = ConfigLoader.json(jsonPath);
@@ -11,4 +12,6 @@ const loader = new ConfigLoader(config);
11
12
 
12
13
  loader.object({ foo: '1', koo: '2', hoo: '3' }, { filter: ['koo', 'hoo'] });
13
14
 
14
- console.log('config', config);
15
+ console.log('config', config);
16
+
17
+ console.log(path.resolve(__dirname, './app.secret'));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "2.1.29",
3
+ "version": "2.1.31",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -1,6 +1,6 @@
1
1
  const fs = require('fs');
2
2
  const path = require('path');
3
- const { isEmptyArray, isArray, isObject, mustString, isNullish, isEmptyString } = require('./utils/types');
3
+ const { isEmptyArray, isArray, isObject, mustString, isNullish, isString } = require('./utils/types');
4
4
 
5
5
  // @ts-ignore
6
6
  function ConfigLoader(configObject, { rootPath } = {}) {
@@ -27,12 +27,6 @@ function ConfigLoader(configObject, { rootPath } = {}) {
27
27
  return this.object(cli(), options);
28
28
  };
29
29
 
30
- this.json = (filepath, options = {}) => {
31
- let filePath = path.resolve(rootPath, filepath);
32
- let jsonObject = json(filePath);
33
- return this.object(jsonObject, options);
34
- };
35
-
36
30
  this.file = (filepath, options) => {
37
31
  let filePath = path.resolve(rootPath, filepath);
38
32
  return this.object(file(filePath, options?.encoding), options);
@@ -40,7 +34,6 @@ function ConfigLoader(configObject, { rootPath } = {}) {
40
34
  }
41
35
 
42
36
  ConfigLoader.cli = cli;
43
- ConfigLoader.json = json;
44
37
  ConfigLoader.file = file;
45
38
  ConfigLoader.require = require.main.require;
46
39
 
@@ -71,7 +64,7 @@ function assign(config, filtered, overwrite) {
71
64
  };
72
65
 
73
66
  function remap(object, use) {
74
- if (isEmptyString(use)) return object;
67
+ if (!isString(use)) return object;
75
68
 
76
69
  for (var key in object) {
77
70
  const value = object[key];
@@ -83,33 +76,35 @@ function remap(object, use) {
83
76
  return object;
84
77
  };
85
78
 
86
- function json(path) {
87
- try {
88
- // return require.main.require(path);
89
- return ConfigLoader.require(path);
90
- }
91
- catch (e) {
92
- return failed(e, path);
93
- }
94
- }
95
-
96
79
  function cli() {
97
80
  return processKeyValueStrings(process.argv);
98
81
  }
99
82
 
100
- function file(path, encoding) {
83
+ function file(filepath, encoding) {
101
84
  encoding = encoding || 'utf8';
85
+
102
86
  try {
103
- var source = fs.readFileSync(path, { encoding });
104
- // @ts-ignore
105
- source = source.replace(/\r\n?/mg, '\n');
106
- // @ts-ignore
107
- var lines = source.split('\n');
87
+ var fileExtension = path.parse(filepath).ext;
88
+
89
+ switch (fileExtension) {
90
+ case '.js':
91
+ case '.json':
92
+ return ConfigLoader.require(filepath);
93
+
94
+ default:
95
+ var source = fs.readFileSync(filepath, { encoding });
96
+ // @ts-ignore
97
+ source = source.replace(/\r\n?/mg, '\n');
98
+ // @ts-ignore
99
+ var lines = source.split('\n');
108
100
 
109
- return processKeyValueStrings(lines);
101
+ return processKeyValueStrings(lines);
102
+ }
110
103
  }
111
- catch (e) {
112
- return failed(e, path);
104
+ catch (error) {
105
+ console.log(`[BINHEND] Failed loading config from: ${filepath}`);
106
+ console.log('[BINHEND] Details:', error);
107
+ return {};
113
108
  }
114
109
  }
115
110
 
@@ -146,10 +141,4 @@ function parseKeyValue(string) {
146
141
  return { key, value, success: true };
147
142
  }
148
143
 
149
- function failed(error, source) {
150
- console.log(`[BINHEND] Failed loading config from: ${source}`);
151
- console.log('[BINHEND] Details:', error);
152
- return {};
153
- };
154
-
155
144
  module.exports = { ConfigLoader, config: {} };
@@ -34,7 +34,10 @@ module.exports = (options) => {
34
34
  if (origin === undefined) return next();
35
35
 
36
36
  // Verify allowed origins
37
- const allowedOrigin = allowedOrigins.length === 0 || allowedOrigins.includes(origin) ? origin : '';
37
+ const acceptLocalhost = allowedOrigins.includes('localhost') && /^https{0,1}:\/\/localhost:\d+$/.test(origin);
38
+ const acceptOrigin = acceptLocalhost || allowedOrigins.length === 0 || allowedOrigins.includes(origin);
39
+ const allowedOrigin = acceptOrigin ? origin : '';
40
+
38
41
  res.header('Access-Control-Allow-Origin', allowedOrigin);
39
42
 
40
43
  if (!allowedOrigin) {