binhend 2.1.13 → 2.1.15

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 ADDED
@@ -0,0 +1,10 @@
1
+
2
+ const { HttpCodes, ConfigLoader, WebBuilder } = require('./index');
3
+
4
+ HttpCodes.ACCEPTED;
5
+
6
+ new ConfigLoader({}).cli;
7
+
8
+ // const builder = new WebBuilder('src', { output: 'build/bundle' });
9
+
10
+ // builder.bundle();
package/index.js CHANGED
@@ -6,4 +6,46 @@
6
6
 
7
7
  'use strict';
8
8
 
9
- module.exports = require('./src/binh');
9
+ /** SERVER - Backend */
10
+ const { server } = require('./src/server');
11
+ const { ConfigLoader, config } = require('./src/configuration');
12
+ const { HTTPS } = require('./src/https');
13
+
14
+ const HttpError = require('./src/api/HttpError');
15
+ const HttpCodes = require('./src/api/HttpCodes');
16
+
17
+ const Router = require('./src/api/Router');
18
+ const { routes, loadRoutes, buildRoutes, mapRoutes } = require('./src/api/routes');
19
+ const trycatch = require('./src/api/trycatch');
20
+
21
+ const cors = require('./src/middleware/cors');
22
+
23
+ const createCSD = require('./src/csd');
24
+ const security = require('./src/security');
25
+ const typeOf = require('./src/utils/typeOf');
26
+ const Bromise = require('./src/utils/bromise');
27
+
28
+ /** CLIENT - Frontend */
29
+ const { WebBuilder } = require('./src/web');
30
+ const { binh } = require('./src/web/component.method');
31
+
32
+ // Run scripts
33
+ require('./src/supportRequireAliasPath');
34
+
35
+ module.exports = {
36
+ server,
37
+ ConfigLoader, config,
38
+ HTTPS,
39
+ HttpError,
40
+ HttpCodes,
41
+ Router,
42
+ routes, buildRoutes, loadRoutes, mapRoutes,
43
+ trycatch,
44
+ cors,
45
+ createCSD,
46
+ security,
47
+ typeOf,
48
+ Bromise,
49
+ WebBuilder,
50
+ binh
51
+ };
package/jsconfig.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "compilerOptions": {
3
+ "moduleResolution": "node",
4
+ "target": "ES2017",
5
+ "checkJs": true,
6
+ "allowJs": true,
7
+ "noEmit": true,
8
+
9
+ "baseUrl": "./src",
10
+ "paths": {
11
+ "@/*": [
12
+ "./*"
13
+ ],
14
+ "@test/*": [
15
+ "../test/*"
16
+ ]
17
+ }
18
+ }
19
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "2.1.13",
3
+ "version": "2.1.15",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -1,4 +1,4 @@
1
- const HttpCodes = {
1
+ module.exports = {
2
2
  CONTINUE: 100,
3
3
  SWITCHING_PROTOCOLS: 101,
4
4
  PROCESSING: 102,
@@ -63,7 +63,3 @@ const HttpCodes = {
63
63
  NOT_EXTENDED: 510,
64
64
  NETWORK_AUTHENTICATION_REQUIRED: 511,
65
65
  };
66
-
67
- module.exports = {
68
- HttpCodes
69
- };
package/src/api/Router.js CHANGED
@@ -35,6 +35,11 @@ function Router(moduleInstance) {
35
35
  return output;
36
36
  }
37
37
 
38
+ /**
39
+ * @param {any} router
40
+ * @param {Function & { isAppliedTryCatch?: boolean }} callback
41
+ * @returns {Function}
42
+ */
38
43
  function createErrorHandler(router, callback) {
39
44
  if (callback.isAppliedTryCatch) return callback;
40
45
 
@@ -0,0 +1,83 @@
1
+ const { readdir, stat } = require('fs/promises');
2
+ const { join, parse, resolve } = require('path');
3
+ const { isUndefined } = require('../utils/typeOf');
4
+ const { server } = require('../server');
5
+ const cors = require('../middleware/cors');
6
+ const express = require('express');
7
+ const parseBasicAuthToken = require('../middleware/parseBasicAuthToken');
8
+
9
+ /**
10
+ * Maps routes recursively from a directory
11
+ * @param {express.Router} router - Express router
12
+ * @param {string} dirpath - Directory path to scan
13
+ */
14
+ async function mapRoutes(router, dirpath) {
15
+ try {
16
+ const files = await readdir(dirpath);
17
+
18
+ for (const direntName of files) {
19
+ const path = join(dirpath, direntName);
20
+ const route = parse(direntName).name;
21
+ const stats = await stat(path);
22
+ let childRouter;
23
+
24
+ if (stats.isFile()) {
25
+ childRouter = require(path);
26
+ } else if (stats.isDirectory()) {
27
+ childRouter = express.Router();
28
+ await mapRoutes(childRouter, path);
29
+ }
30
+
31
+ router.use(`/${route}`, childRouter);
32
+ console.log('[BINHEND] Mapping routes from:', path);
33
+ }
34
+ } catch (error) {
35
+ console.error(`[BINHEND] Error in mapping routes for directory: ${dirpath}`, error);
36
+ throw error;
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Loads router with routes from the specified directory
42
+ * @param {string} dirPath - Directory path to load routes from
43
+ * @returns {Promise<express.Router>} router - Configured Express router
44
+ */
45
+ async function buildRoutes(dirPath) {
46
+ if (isUndefined(dirPath)) {
47
+ console.error(`[BINHEND] Error missing directory path: ${dirPath}.\n`);
48
+ throw new Error('Require directory path to load routes.');
49
+ }
50
+
51
+ const router = express.Router();
52
+ router.use(express.urlencoded({ extended: false }));
53
+ router.use(express.json());
54
+ router.use(parseBasicAuthToken);
55
+
56
+ try {
57
+ await mapRoutes(router, resolve(dirPath));
58
+ } catch (error) {
59
+ console.error(`[BINHEND] Error mapping routes from: ${resolve(dirPath)}.\n` + error);
60
+ throw error;
61
+ }
62
+
63
+ return router;
64
+ }
65
+
66
+ /**
67
+ * Loads routes into the server with CORS options
68
+ * @param {string} dirPath - Directory path to load routes from
69
+ * @param {Object} corsOptions - CORS configuration options
70
+ */
71
+ async function loadRoutes(dirPath, corsOptions) {
72
+ server.use(cors(corsOptions));
73
+ const router = await buildRoutes(dirPath);
74
+ server.use(router);
75
+ }
76
+
77
+ const routes = {
78
+ load: loadRoutes,
79
+ build: buildRoutes,
80
+ map: mapRoutes
81
+ };
82
+
83
+ module.exports = { routes, loadRoutes, buildRoutes, mapRoutes };
@@ -2,7 +2,7 @@ const fs = require('fs');
2
2
  const path = require('path');
3
3
  const { isEmptyArray, isArray, isObject } = require('./utils/typeOf');
4
4
 
5
-
5
+ // @ts-ignore
6
6
  function ConfigLoader(configObject, { module } = {}) {
7
7
  const configs = configObject || {};
8
8
  const rootPath = module?.path || require.main.path;
@@ -15,6 +15,7 @@ function ConfigLoader(configObject, { module } = {}) {
15
15
  return key == undefined ? configs : configs[key] || (configs[key] = {});
16
16
  };
17
17
 
18
+ // @ts-ignore
18
19
  this.object = (object, { key, filter: filters } = {}) => {
19
20
  try {
20
21
  var config = getConfigPosition(key);
@@ -74,7 +75,9 @@ function file(path, encoding) {
74
75
  encoding = encoding || 'utf8';
75
76
  try {
76
77
  var source = fs.readFileSync(path, { encoding });
78
+ // @ts-ignore
77
79
  source = source.replace(/\r\n?/mg, '\n');
80
+ // @ts-ignore
78
81
  var lines = source.split('\n');
79
82
 
80
83
  return processKeyValueStrings(lines);
@@ -1,6 +1,9 @@
1
- const { trycatch, HttpError } = require('../api');
1
+
2
2
  const { isEmptyObject, isString } = require('../utils/typeOf');
3
- const { OK, CREATED, BAD_REQUEST } = require('../utils/httpCodes').HttpCodes;
3
+ const trycatch = require('../api/trycatch');
4
+ const HttpError = require('../api/HttpError');
5
+
6
+ const { OK, CREATED, BAD_REQUEST } = require('../api/HttpCodes');
4
7
 
5
8
  const
6
9
  ERROR_MISSING_ID = 'Missing required parameter: id',
package/src/https.js CHANGED
@@ -1,3 +1,4 @@
1
+ // @ts-nocheck
1
2
 
2
3
  const https = require('https');
3
4
 
@@ -1,5 +1,5 @@
1
1
 
2
- const { isArray, mustArray, mustNumber } = require('./utils/typeOf');
2
+ const { isArray, mustArray, mustNumber } = require('../utils/typeOf');
3
3
 
4
4
  const defaultOptions = {
5
5
  origins: [],
package/src/security.js CHANGED
@@ -1,3 +1,4 @@
1
+ // @ts-nocheck
1
2
 
2
3
  const crypto = require('crypto');
3
4
  const algorithm = 'aes-256-cbc';
@@ -1,3 +1,4 @@
1
+ // @ts-nocheck
1
2
 
2
3
  const path = require('path');
3
4
  const DefaultModule = require('module');
@@ -37,7 +38,6 @@ for (const alias in paths) {
37
38
  const originalResolveFilename = Module._resolveFilename;
38
39
 
39
40
  Module._resolveFilename = function (request, parent, isMain, options) {
40
- // console.log(request, parent, isMain, options);
41
41
  for (const alias in aliasMap) {
42
42
  if (!request.startsWith(alias)) continue;
43
43
  // Loop through the array of resolved paths for this alias
@@ -2,19 +2,17 @@
2
2
  function Bromise() {
3
3
  var resolve, reject, promise = new Promise((res, rej) => (resolve = res) && (reject = rej));
4
4
 
5
- return {
6
- promise,
7
- resolve: (value) => {
8
- resolve(value);
9
- return promise;
10
- },
11
- reject: (error) => {
12
- reject(error);
13
- return promise;
14
- }
5
+ this.promise = promise;
6
+
7
+ this.resolve = (value) => {
8
+ resolve(value);
9
+ return promise;
10
+ };
11
+
12
+ this.reject = (error) => {
13
+ reject(error);
14
+ return promise;
15
15
  };
16
16
  }
17
17
 
18
- module.exports = {
19
- Bromise
20
- };
18
+ module.exports = Bromise;
@@ -69,7 +69,7 @@ function isNullOrUndefined(input) {
69
69
  return isNull(input) || isUndefined(input);
70
70
  }
71
71
 
72
- module.exports = Object.assign(typeOf, {
72
+ module.exports = {
73
73
  typeOf,
74
74
  isObject,
75
75
  isEmptyObject,
@@ -88,4 +88,4 @@ module.exports = Object.assign(typeOf, {
88
88
  mustString,
89
89
  isSymbol,
90
90
  isBigInt
91
- });
91
+ };
package/src/web/code.js CHANGED
@@ -66,7 +66,7 @@ function getDependencyDelaration(component) {
66
66
  var declarationCode = codes.join(',');
67
67
 
68
68
  codes = [];
69
- codes.push(`var Binh = _Binh;`);
69
+ codes.push(`var Binh = _Binh, require = Binh.r;`);
70
70
 
71
71
  if (declarationCode) {
72
72
  codes.push(`var${declarationCode};`);
@@ -1,3 +1,4 @@
1
+ // @ts-nocheck
1
2
 
2
3
  const path = require('path');
3
4
  const { scanNestedFiles, cloneFileIfNew, writeToFileIfNew, makeFullDirPath } = require('./component.file');
@@ -1,3 +1,4 @@
1
+ // @ts-nocheck
1
2
 
2
3
  const { readdir, statSync, existsSync, copyFile, writeFile, writeFileSync, readFileSync, mkdirSync, rm } = require('fs');
3
4
  const { join, dirname } = require('path');
@@ -7,7 +7,7 @@ const PREFIX_CODE =
7
7
  `var { context, tag, svg, script, require, css } = binh.context(module, require);
8
8
  var Binh = binh.Binh;
9
9
  binh.component(context, ui, service, style);
10
- var ui = null, service = null, style = null;\r\n\r\n`;
10
+ var ui = ui || null, service = service || null, style = style || null;\r\n\r\n`;
11
11
 
12
12
  function generate(sourceRootPath, outputRootPath, callbackDone) {
13
13
  if (sourceRootPath == undefined || outputRootPath == undefined) return;
@@ -1,3 +1,4 @@
1
+ // @ts-nocheck
1
2
 
2
3
  const { readdir, statSync, writeFile, existsSync, mkdirSync, copyFile, readFileSync } = require('fs');
3
4
  const { join, dirname, parse } = require('path');
@@ -1,3 +1,4 @@
1
+ // @ts-nocheck
1
2
 
2
3
  const path = require('path');
3
4
  const fs = require('fs');
@@ -74,6 +75,7 @@ binh.component = function (context, ui, service, style) {
74
75
 
75
76
  var module = context.module;
76
77
  module.exports = component;
78
+ module.exportss = component;
77
79
 
78
80
  component.module = module;
79
81
  component.constructor = Component;
@@ -93,7 +95,8 @@ binh.component = function (context, ui, service, style) {
93
95
  };
94
96
 
95
97
  binh.final = function (module) {
96
- var component = module.exports;
98
+ var component = module.exportss || {};
99
+ module.exports = component;
97
100
 
98
101
  component.eachChild && component.eachChild(childComponent => {
99
102
  if (childComponent.alias?.[component.filename]) {
@@ -125,7 +128,9 @@ binh.css = function (module, cssFilename) {
125
128
  binh.final(module);
126
129
  };
127
130
 
128
- binh.Binh = function () { }; // dummy Binh object for binhjs => build web not logging error of undefined
131
+ /** Dummy Binh object for binhjs => build web not logging error of undefined */
132
+ binh.Binh = function () { };
133
+ binh.Binh.Util = { require: function () {} };
129
134
 
130
135
  function alias(name) {
131
136
  if (!isString(name)) return this;
package/src/web/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ // @ts-nocheck
1
2
 
2
3
  const path = require('path');
3
4
  const { isEmptyString, isString, mustString } = require('../utils/typeOf');
File without changes
@@ -1,8 +0,0 @@
1
-
2
- tag('div');
3
-
4
- function ui() {
5
- var content = div({ class: 'new-ui' });
6
-
7
- return content;
8
- }
@@ -1,26 +0,0 @@
1
-
2
- require('./UI');
3
- require('./Service');
4
-
5
- css(
6
- require('./Style.css')
7
- );
8
-
9
- function ui() {
10
- Service(); // initialize service instance (singleton)
11
-
12
- var component = UI(),
13
- handleStateChanged = component.action('handleStateChanged'),
14
- makeRequest = Service.instant('makeRequest');
15
-
16
- component.listen('stateId', function(data) {
17
- // ... transform data if need
18
- handleStateChanged(data);
19
- })
20
- .when('request', function(data) {
21
- // ... transform data if need
22
- makeRequest(data);
23
- });
24
-
25
- return component;
26
- }
@@ -1,7 +0,0 @@
1
-
2
- function service({ state, http, sid, define, _this }) {
3
- this.makeRequest = function(data) {
4
- // use data to handle logic, call API, or change state
5
- console.log('Service received data from UI:', data);
6
- }
7
- }
@@ -1,3 +0,0 @@
1
- .new-ui {
2
- color: red;
3
- }
@@ -1,17 +0,0 @@
1
-
2
- tag('div');
3
-
4
- function ui() {
5
- var newUI = div({ class: 'new-ui' }),
6
- requestService = newUI.action('request');
7
-
8
- newUI.when('handleStateChanged', function(data) {
9
- // use data to handle logic, e.g. render UI
10
- console.log('UI received state data:', data);
11
- })
12
- .on('click', function(event) {
13
- requestService(event.currentTarget.innerText);
14
- });
15
-
16
- return newUI('New UI');
17
- }
package/src/api/index.js DELETED
@@ -1,9 +0,0 @@
1
-
2
- const { loadRouter, loadRoutes, mapRoutes } = require('./loader');
3
- const HttpError = require('./HttpError');
4
- const Router = require('./Router');
5
- const trycatch = require('./trycatch');
6
-
7
- module.exports = {
8
- loadRouter, loadRoutes, mapRoutes, trycatch, Router, HttpError
9
- };
package/src/api/loader.js DELETED
@@ -1,59 +0,0 @@
1
- const { readdirSync, statSync } = require('fs');
2
- const { join, parse, resolve } = require('path');
3
- const { isUndefined } = require('../utils/typeOf');
4
- const { server } = require('../server');
5
- const cors = require('../cors');
6
- const express = require('express');
7
- const parseBasicAuthToken = require('../middleware/parseBasicAuthToken');
8
-
9
-
10
- function mapRoutes(router, dirpath) {
11
- readdirSync(dirpath).forEach(function(direntName) {
12
- var path = join(dirpath, direntName),
13
- route = parse(direntName).name;
14
-
15
- var stat = statSync(path);
16
- var childRouter;
17
-
18
- if (stat.isFile()) {
19
- childRouter = require(path);
20
- }
21
- else if (stat.isDirectory()) {
22
- childRouter = express.Router();
23
- mapRoutes(childRouter, path);
24
- }
25
-
26
- router.use(`/${route}`, childRouter);
27
- console.log('[BINHEND] Mapping routes from:', path);
28
- });
29
- }
30
-
31
- function loadRouter(dirPath) {
32
- if (isUndefined(dirPath)) {
33
- console.error(`[BINHEND] Error missing directory path: ${dirPath}.\n`);
34
- throw new Error('Require directory path to load routes.');
35
- }
36
-
37
- const router = express.Router();
38
- router.use(express.urlencoded({ extended: false }));
39
- router.use(express.json());
40
- router.use(parseBasicAuthToken);
41
-
42
- try {
43
- mapRoutes(router, resolve(dirPath));
44
- }
45
- catch (error) {
46
- console.error(`[BINHEND] Error mapping routes from: ${resolve(dirPath)}.\n` + error);
47
- throw error;
48
- }
49
-
50
- return router;
51
- }
52
-
53
- function loadRoutes(dirPath, corsOptions) {
54
- server.use(cors(corsOptions));
55
- const router = loadRouter(dirPath);
56
- server.use(router);
57
- }
58
-
59
- module.exports = { loadRouter, loadRoutes, mapRoutes };
package/src/binh.js DELETED
@@ -1,29 +0,0 @@
1
-
2
- const typeOf = require('./utils/typeOf');
3
- const security = require('./security');
4
- const createCSD = require('./csd');
5
- const cors = require('./cors');
6
- const { server } = require('./server');
7
- const { loadRouter, loadRoutes, mapRoutes, trycatch, Router, HttpError } = require('./api');
8
- const { ConfigLoader, config } = require('./configuration');
9
- const { WebBuilder } = require('./web');
10
- const { binh } = require('./web/component.method');
11
- const { HttpCodes } = require('./utils/httpCodes');
12
- const { HTTPS } = require('./https');
13
-
14
- // Run scripts
15
- require('./alias');
16
-
17
- module.exports = {
18
- typeOf,
19
- security,
20
- createCSD,
21
- cors,
22
- server,
23
- loadRouter, loadRoutes, mapRoutes, trycatch, Router, HttpError,
24
- ConfigLoader, config,
25
- WebBuilder,
26
- binh,
27
- HttpCodes,
28
- HTTPS
29
- };