binhend 2.2.9 → 2.2.11

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/index.js CHANGED
@@ -6,8 +6,11 @@
6
6
 
7
7
  'use strict';
8
8
 
9
+ const path = require('path');
10
+
9
11
  //___ Run scripts ___//
10
- require('./packages/module-alias'); //--- alias path '@' for requiring modules: require('@/any/path')
12
+ const moduleAlias = require('./packages/module-alias'); // path alias '@' for requiring modules: require('@/any/path') - use jsconfig.json
13
+ moduleAlias.path('@binhend/*', [ path.join(__dirname, './packages') ]); // set path alias '@binhend' for all packages to call each others when 'npm install' in another project
11
14
 
12
15
  const server = require('./packages/core/src/server');
13
16
  const Router = require('./packages/core/src/Router');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "2.2.9",
3
+ "version": "2.2.11",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -7,6 +7,7 @@
7
7
  },
8
8
  "license": "MIT",
9
9
  "dependencies": {
10
- "@binhend/types": "^1.0.0"
10
+ "@binhend/types": "^1.0.0",
11
+ "@binhend/validation": "^1.0.0"
11
12
  }
12
13
  }
@@ -1,12 +1,13 @@
1
1
  const fs = require('fs');
2
2
  const path = require('path');
3
- const { isEmptyArray, isArray, isObject, mustString, isNullish, isString } = require('@binhend/types');
3
+ const { isEmptyArray, isArray, isObject, isNullish, isString } = require('@binhend/types');
4
+ const must = require('@binhend/validation');
4
5
 
5
6
  // @ts-ignore
6
7
  function ConfigLoader(configObject, { rootPath } = {}) {
7
8
  const configs = configObject || {};
8
9
 
9
- rootPath = mustString(rootPath) || require.main.path;
10
+ rootPath = must.String(rootPath, { default: require.main.path });
10
11
 
11
12
  function getConfigPosition(key) {
12
13
  return key == undefined ? configs : configs[key] || (configs[key] = {});
@@ -1,33 +1,6 @@
1
1
 
2
- const utils = require('@binhend/utils');
3
- const middlewares = require('@binhend/middlewares');
4
- const { config, ConfigLoader } = require('@binhend/config');
5
- const { WebBuild, binh } = require('@binhend/web');
6
-
7
- // Run scripts
8
- require('@binhend/module-alias'); // for format: require('@/any/path')
9
-
10
2
  module.exports = {
11
3
  server: require('./src/server'),
12
4
  Router: require('./src/Router'),
13
- routes: require('./src/routes').routes,
14
-
15
- cors: middlewares.cors,
16
- trycatch: middlewares.trycatch,
17
-
18
- config, ConfigLoader,
19
-
20
- HTTPS: require('@binhend/https').HTTPS,
21
- HttpError: utils.HttpError,
22
- HttpCodes: utils.HttpCodes,
23
- Bromise: utils.Bromise,
24
-
25
- CSD: require('@binhend/csd'),
26
-
27
- crypto: require('@binhend/crypto'),
28
-
29
- types: require('@binhend/types'),
30
- validation: require('@binhend/validation'),
31
-
32
- WebBuild, binh
5
+ routes: require('./src/routes').routes
33
6
  };
@@ -5,10 +5,7 @@ var server = express();
5
5
  var isCalledOnce = false;
6
6
 
7
7
  function getServer() {
8
- if (isCalledOnce) {
9
- server = server;
10
- }
11
- else {
8
+ if (!isCalledOnce) {
12
9
  server = express();
13
10
  isCalledOnce = true;
14
11
  }
@@ -8,6 +8,7 @@
8
8
  "license": "MIT",
9
9
  "dependencies": {
10
10
  "@binhend/types": "^1.0.0",
11
+ "@binhend/validation": "^1.0.0",
11
12
  "@binhend/utils": "^1.0.1"
12
13
  }
13
14
  }
@@ -1,5 +1,6 @@
1
1
 
2
- const { isArray, mustArray, mustNumber } = require('@binhend/types');
2
+ const { isArray } = require('@binhend/types');
3
+ const must = require('@binhend/validation');
3
4
 
4
5
  const defaultOptions = {
5
6
  origins: [],
@@ -24,7 +25,7 @@ module.exports = (options) => {
24
25
  // Parse options
25
26
  var { origins, credentials, methods, preflightContinue, optionsSuccessStatus, exposedHeaders, allowHeaders, maxAge } = Object.assign({}, defaultOptions, options);
26
27
 
27
- const allowedOrigins = mustArray(origins);
28
+ const allowedOrigins = must.Array(origins, { default: [] });
28
29
 
29
30
  // Create a new middleware
30
31
  return function (req, res, next) {
@@ -52,11 +53,11 @@ module.exports = (options) => {
52
53
  addHeader(res, 'Access-Control-Allow-Headers', arrayToString(allowHeaders));
53
54
 
54
55
  // Cache preflight response
55
- addHeader(res, 'Access-Control-Max-Age', mustNumber(maxAge));
56
+ addHeader(res, 'Access-Control-Max-Age', must.Number(maxAge, { default: 0 }));
56
57
 
57
58
  // Handle preflight OPTIONS request
58
59
  if (req.method === 'OPTIONS' && !preflightContinue) {
59
- var statusCode = mustNumber(optionsSuccessStatus) || 204; // 204 - No Content
60
+ var statusCode = must.Number(optionsSuccessStatus, { default: 204 }); // 204 - No Content
60
61
  return res.sendStatus(statusCode);
61
62
  }
62
63
 
@@ -1,4 +1,4 @@
1
1
 
2
- require('./src/alias-cjs-loader');
2
+ module.exports = require('./src/alias-cjs-loader');
3
3
 
4
4
  // require('./alias-esm-loader'); // TODO implement this alias loader for ES Modules
@@ -5,8 +5,5 @@
5
5
  "exports": {
6
6
  ".": "./index.js"
7
7
  },
8
- "license": "MIT",
9
- "dependencies": {
10
- "@binhend/web": "^1.0.0"
11
- }
8
+ "license": "MIT"
12
9
  }
@@ -2,7 +2,6 @@
2
2
 
3
3
  const path = require('path');
4
4
  const DefaultModule = require('module');
5
- const { binh } = require('../../web/src/component.method'); // TODO handle this 'binh' object again, more properly, workspaces will not recognize global 'binh'
6
5
 
7
6
  // Protect against improperly customized module constructors by other frameworks/packages
8
7
  const Module = module.constructor.length > 1 ? module.constructor : DefaultModule;
@@ -14,11 +13,6 @@ var jsconfig;
14
13
 
15
14
  try {
16
15
  jsconfig = require(jsconfigPath);
17
- jsconfig.compilerOptions = jsconfig.compilerOptions || {};
18
- jsconfig.compilerOptions.paths = jsconfig.compilerOptions.paths || {};
19
-
20
- const paths = jsconfig.compilerOptions.paths;
21
- paths['@binhend/*'] = [ path.join(__dirname, '../../') ];
22
16
  }
23
17
  catch (error) {
24
18
  return console.log(`[BINEND] Not found 'jsconfig.json' for alias paths.`);
@@ -27,16 +21,27 @@ catch (error) {
27
21
  const baseUrl = jsconfig?.compilerOptions?.baseUrl || '.';
28
22
  const paths = jsconfig?.compilerOptions?.paths || {};
29
23
 
24
+ const settings = { // for external modification in run-time
25
+ paths,
26
+ webSourcePath: null,
27
+ webModulePath: null,
28
+ path: mapAliasWithFilePaths
29
+ };
30
+
30
31
  /** Step 2: Create an alias map with all possible paths */
31
- const aliasMap = {};
32
+ const aliasMap = {}, ENDING_WILDCARD = /\/\*$/;
32
33
 
33
34
  for (const alias in paths) {
34
- const aliasPath = alias.replace('/*', ''); // Strip the '/*' for matching // TODO write regex for ending with /*
35
- const originalPaths = paths[alias]; // Get the array of paths
35
+ const filePaths = paths[alias]; // Get the array of file paths
36
+ mapAliasWithFilePaths(alias, filePaths);
37
+ }
36
38
 
37
- aliasMap[aliasPath] = originalPaths.map((originalPath) => {
39
+ function mapAliasWithFilePaths(alias, filePaths) {
40
+ const aliasPath = alias.replace(ENDING_WILDCARD, ''); // Strip the '/*' for matching
41
+
42
+ aliasMap[aliasPath] = filePaths.map((filePath) => {
38
43
  // Resolve each path to an absolute path
39
- return path.resolve(baseUrl, originalPath.replace('/*', ''));
44
+ return path.resolve(baseUrl, filePath.replace(ENDING_WILDCARD, ''));
40
45
  });
41
46
  }
42
47
 
@@ -49,18 +54,22 @@ Module._resolveFilename = function (request, parent, isMain, options) {
49
54
  // Loop through the array of resolved paths for this alias
50
55
  for (const resolvedPath of aliasMap[alias]) {
51
56
  // Construct the new request path
52
- var newRequest = request.replace(alias, resolvedPath);
57
+ var newRequest;
53
58
 
54
- // BINHEND web components load each others stil requiring src paths (src file paths), not module paths (formatted file paths)
55
- if (parent.filename.startsWith(binh.webModulePath)) {
56
- // TODO test if this branch is acutally called.
57
- newRequest = newRequest.replace(binh.webSourcePath, binh.webModulePath);
59
+ if (parent.filename.startsWith(settings.webModulePath)) {
60
+ // BINHEND web components (formatted code) load each others in build-time still requiring src paths (src file paths), not module paths (formatted file paths)
61
+ // parent.filename is the file location of module which makes this request by calling require()
62
+ newRequest = newRequest.replace(settings.webSourcePath, settings.webModulePath);
63
+ }
64
+ else {
65
+ newRequest = request.replace(alias, resolvedPath);
58
66
  }
59
67
 
60
68
  // Attempt to resolve the new request
61
69
  try {
62
70
  return originalResolveFilename.call(this, newRequest, parent, isMain, options);
63
- } catch (err) {
71
+ }
72
+ catch (err) {
64
73
  // If it fails, continue to the next path
65
74
  continue;
66
75
  }
@@ -70,3 +79,5 @@ Module._resolveFilename = function (request, parent, isMain, options) {
70
79
  // Fallback to the original resolution if no alias matches
71
80
  return originalResolveFilename.call(this, request, parent, isMain, options);
72
81
  };
82
+
83
+ module.exports = settings;
@@ -1,61 +1,36 @@
1
1
 
2
+ // Generic
3
+ function isPrimitive(input) {
4
+ return input === null || (typeof input !== 'object' && typeof input !== 'function');
5
+ }
6
+
2
7
  function typeOf(input) {
3
8
  return Object.prototype.toString.call(input).slice(8, -1);
4
9
  }
5
10
 
6
- function isObject(input) {
7
- return typeOf(input) === 'Object';
8
- }
11
+ function strictTypeOf(input) {
12
+ if (input === null) return 'Null';
9
13
 
10
- function mustObject(input, defaultValue = {}) {
11
- return isObject(input) ? input : isObject(defaultValue) ? defaultValue : {};
12
- }
14
+ if (typeof input === 'object') {
15
+ if (input instanceof Number) return 'NumberObject';
16
+ if (input instanceof String) return 'StringObject';
17
+ if (input instanceof Boolean) return 'BooleanObject';
18
+ }
13
19
 
14
- function isEmptyObject(input) {
15
- return isObject(input) && Object.keys(input).length === 0;
16
- }
17
-
18
- const { isArray } = Array;
19
-
20
- function isEmptyArray(input) {
21
- return isArray(input) && input.length === 0;
22
- }
23
-
24
- function mustArray(input, defaultValue = []) {
25
- return isArray(input) ? input : isArray(defaultValue) ? defaultValue : [];
26
- }
27
-
28
- function isFunction(input) {
29
- return typeof input === 'function';
20
+ return typeOf(input);
30
21
  }
31
22
 
23
+ // Primitive
32
24
  function isString(input) {
33
- return typeof input === 'string' || typeOf(input) === 'String';
25
+ return typeof input === 'string';
34
26
  }
35
27
 
36
28
  function isEmptyString(input) {
37
29
  return isString(input) && input.length === 0;
38
30
  }
39
31
 
40
- function mustString(input) {
41
- return isString(input) ? input : '';
42
- }
43
-
44
32
  function isNumber(input) {
45
- return typeof input === 'number' || typeOf(input) === 'Number';
46
- }
47
-
48
- function mustNumber(input, defaultValue) {
49
- return isNumber(input) ? input : isNumber(defaultValue) ? defaultValue : 0;
50
- }
51
-
52
- function isDate(input) {
53
- return typeOf(input) === 'Date' && !isNaN(input.getTime());
54
- }
55
-
56
- function isDateLike(value) {
57
- const date = new Date(value);
58
- return !isNaN(date.getTime());
33
+ return typeof input === 'number' && Number.isFinite(input);
59
34
  }
60
35
 
61
36
  function isBigInt(input) {
@@ -67,7 +42,7 @@ function isSymbol(input) {
67
42
  }
68
43
 
69
44
  function isBoolean(input) {
70
- return typeof input === 'boolean' || typeOf(input) === 'Boolean';
45
+ return typeof input === 'boolean';
71
46
  }
72
47
 
73
48
  function isNull(input) {
@@ -86,27 +61,70 @@ function isNullish(input) {
86
61
  return input == null;
87
62
  }
88
63
 
64
+
65
+ // Class, object
66
+ function isObject(input) {
67
+ return typeOf(input) === 'Object'; // typeOf() excludes null, arrays, functions, dates, etc.
68
+ }
69
+
70
+ function isPlainObject(input) {
71
+ // [input != null] => not allow input values as undefined or null, since Object.getPrototypeOf(input) throws error for undefined and null.
72
+ return input != null && Object.getPrototypeOf(input) === Object.prototype;
73
+ }
74
+
75
+ function isEmptyObject(input) {
76
+ return isObject(input) && Object.keys(input).length === 0;
77
+ }
78
+
79
+ const { isArray } = Array;
80
+
81
+ function isEmptyArray(input) {
82
+ return isArray(input) && input.length === 0;
83
+ }
84
+
85
+ function isFunction(input) {
86
+ return typeof input === 'function';
87
+ }
88
+
89
+ function isDate(input) {
90
+ return input instanceof Date && !isNaN(input.getTime());
91
+ }
92
+
93
+ function isDateLike(input) {
94
+ const date = new Date(input);
95
+ return !isNaN(date.getTime());
96
+ }
97
+
98
+
89
99
  module.exports = {
100
+ // Generic
90
101
  typeOf,
102
+ strictTypeOf,
103
+ isPrimitive,
104
+
105
+ // Primitive
106
+ isString,
107
+ isEmptyString,
108
+ isNumber,
109
+ isBoolean,
110
+ isSymbol,
111
+ isBigInt,
112
+
113
+ isNull,
114
+ isUndefined,
115
+ isNullOrUndefined,
116
+ isNullish,
117
+
118
+ // Class, object
91
119
  isObject,
120
+ isPlainObject,
92
121
  isEmptyObject,
93
- mustObject,
122
+
94
123
  isArray,
95
124
  isEmptyArray,
96
- mustArray,
125
+
97
126
  isFunction,
98
- isNull,
99
- isUndefined,
100
- isNullOrUndefined,
101
- isNullish,
102
- isBoolean,
103
- isNumber,
104
- mustNumber,
105
- isString,
106
- isEmptyString,
107
- mustString,
108
- isSymbol,
109
- isBigInt,
127
+
110
128
  isDate,
111
129
  isDateLike
112
130
  };
@@ -10,6 +10,7 @@
10
10
  "@binhend/core": "^1.0.0",
11
11
  "@binhend/types": "^1.0.0",
12
12
  "@binhend/config": "^1.0.0",
13
+ "@binhend/validation": "^1.0.0",
13
14
  "express": "^4.17.1",
14
15
  "js-beautify": "^1.15.1",
15
16
  "uglify-js": "^3.17.4",
@@ -1,13 +1,15 @@
1
1
 
2
2
  const path = require('path');
3
- // @ts-ignore
4
- const { isEmptyString, isString, mustString } = require('@binhend/types');
3
+ const express = require('express');
4
+
5
+ const { server } = require('@binhend/core');
6
+ const { isEmptyString, isString } = require('@binhend/types');
7
+ const must = require('@binhend/validation');
8
+ const moduleAlias = require('@binhend/module-alias');
9
+
5
10
  const ComponentFormat = require('./component.format');
6
11
  const ComponentBuild = require('./component.build');
7
12
  const Component = require('./component');
8
- // @ts-ignore
9
- const { server } = require('@binhend/core');
10
- const express = require('express');
11
13
 
12
14
  const DEFAULT_DIR_BUILD = 'build';
13
15
  const DEFAULT_DIR_BUILD_WEB = 'web';
@@ -26,7 +28,7 @@ function WebBuild(source, { output, external } = {}) {
26
28
 
27
29
  var web = path.resolve(output, DEFAULT_DIR_BUILD_WEB);
28
30
  var module = path.resolve(output, DEFAULT_DIR_BUILD_MODULE);
29
- var external = mustString(external) || DEFAULT_DIR_BUILD_EXTERNAL;
31
+ var external = must.String(external, { default: DEFAULT_DIR_BUILD_EXTERNAL });
30
32
 
31
33
  this.bundle = () => {
32
34
  Component.isLazy = false;
@@ -57,15 +59,11 @@ function WebBuild(source, { output, external } = {}) {
57
59
 
58
60
  setTimeout(() => {
59
61
  server().use(express.static(web));
60
- // @ts-ignore
61
62
  server().get('/*', (req, res) => res.sendFile('index.html', { root: web }));
62
63
  });
63
64
 
64
- // @ts-ignore
65
- binh.webModulePath = module;
66
- // @ts-ignore
67
- binh.webSourcePath = source;
68
-
65
+ moduleAlias.webModulePath = module;
66
+ moduleAlias.webSourcePath = source;
69
67
  }
70
68
 
71
69
  module.exports = {
@@ -1,110 +1,5 @@
1
- // @ts-nocheck
2
1
 
3
- const { readdir, statSync, writeFile, existsSync, mkdirSync, copyFile, readFileSync } = require('fs');
4
- const { join, dirname, parse } = require('path');
5
- const CodeFormat = require('./code');
6
-
7
- // TODO
8
- // [-] Enhance code by removing sync logics (except for existsSync, mkdirSync): readdir, readFileSync, (statSync?)
9
- // [-] Able to minify/beautify generated files
10
-
11
- function generate(source, destination) {
12
- if (source == undefined) return;
13
- console.log('[BINHEND][WEB-BUILD] Start building components from:', source);
14
- console.log('[BINHEND][WEB-BUILD] to:', destination);
15
- processDirectory(source, source, destination);
16
- }
17
-
18
- function processDirectory(root, current, destination) {
19
- readdir(current, function (error, dirents) {
20
- if (error) {
21
- showError('Failed scanning directory', current, error);
22
- return;
23
- }
24
-
25
- dirents.forEach(function (dirent) {
26
- var filepath = join(current, dirent);
27
-
28
- try {
29
- var stat = statSync(filepath);
30
-
31
- if (stat.isFile()) {
32
- var outpath = filepath.replace(root, destination);
33
- ensureDirectoryExistence(outpath);
34
-
35
- if (parse(dirent).ext !== '.js') {
36
- return cloneFile(filepath, outpath);
37
- }
38
-
39
- try {
40
- var component = require(filepath);
41
- }
42
- catch (error) {
43
- var content = readFileSync(filepath, { encoding: 'utf8', flag: 'r' });
44
- var isComponent = content.match(/\s*binh\s*.(ui|service|style)\s*\(\s*module\s*,.*\)/);
45
- if (isComponent) {
46
- showError('Failed requiring module', filepath, error);
47
- return;
48
- }
49
- return cloneFile(filepath, outpath);
50
- }
51
-
52
- if (!(component instanceof Function) || component.constructor !== Component) {
53
- return cloneFile(filepath, outpath);
54
- }
55
-
56
- var code = joinCodes(component, root);
57
-
58
- writeFile(outpath, code, { encoding: 'utf8', flag: 'w' }, function(error) {
59
- if (error) {
60
- showError('Failed writing file', filepath, error);
61
- return;
62
- }
63
- });
64
- }
65
- else if (stat.isDirectory()) {
66
- processDirectory(root, filepath, destination);
67
- }
68
- }
69
- catch (error) {
70
- showError('Failed processing file', filepath, error);
71
- }
72
- });
73
- });
74
- }
75
-
76
- function ensureDirectoryExistence(filepath) {
77
- var dirpath = dirname(filepath);
78
- if (existsSync(dirpath)) return true;
79
- ensureDirectoryExistence(dirpath);
80
- mkdirSync(dirpath);
81
- }
82
-
83
- function joinCodes(component, root) {
84
- var fullcode = CodeFormat.prequire(component, [
85
- CodeFormat.bundle(component, root),
86
- CodeFormat.htmltags(component)
87
- ]);
88
-
89
- return fullcode;
90
- }
91
-
92
- function cloneFile(filepath, outpath) {
93
- copyFile(filepath, outpath, function(error) {
94
- if (error) {
95
- showError('Failed copying file to', outpath, error);
96
- }
97
- });
98
- }
99
-
100
- function showError(message, id, error) {
101
- console.log(`[BINHEND][WEB-BUILD] ${message}:`, id);
102
- console.log('[BINHEND][WEB-BUILD] Error details:', error);
103
- }
104
-
105
- var Component = {
106
- generate,
2
+ module.exports = {
3
+ isLazy: false,
107
4
  minification: false
108
5
  };
109
-
110
- module.exports = Component;