binhend 2.1.25 → 2.1.27

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,45 +1,14 @@
1
1
 
2
2
  const { HttpCodes, ConfigLoader, WebBuilder, HttpError, validation } = require('./index');
3
3
 
4
- // HttpCodes.ACCEPTED;
4
+ // var jsonPath = __dirname + '/jsconfig.json';
5
+ // var cc = ConfigLoader.json(jsonPath);
6
+ // console.log('cc', cc);
5
7
 
6
- // new ConfigLoader({}).cli;
8
+ const config = {};
9
+ console.log('config', config);
10
+ const loader = new ConfigLoader(config);
7
11
 
8
- // const builder = new WebBuilder('src', { output: 'build/bundle' });
12
+ loader.object({ foo: '1', koo: '2', hoo: '3' }, { filter: ['koo', 'hoo'] });
9
13
 
10
- // builder.bundle();
11
-
12
- // throw new HttpError(HttpCodes.BAD_REQUEST, '[BINHEND][VALIDATION] Must be a string.');
13
- // throw new HttpError(HttpCodes.BAD_REQUEST, '[BINHEND][VALIDATION] MUST BE A STRING.');
14
- // var typeName = `array`;
15
- // throw new HttpError(HttpCodes.BAD_REQUEST, `Invalid input: value not passed custom validator.`);
16
- // throw new HttpError(HttpCodes.BAD_REQUEST, `Invalid input: unknown reason.`);
17
- // throw new HttpError(HttpCodes.BAD_REQUEST, `Invalid input: value must be type of \`${typeName}\`.`);
18
- // throw new HttpError(HttpCodes.BAD_REQUEST, `Invalid input: Field "title" must be type of \`${typeName}\`.`);
19
-
20
- // validation.String('a', {});
21
-
22
- validation.String('a');
23
- // validation.String(null);
24
- // validation.String(1, { name: 'myNum', message: 'Require string for this field' });
25
-
26
- // validation.Validator('this is me', (input) => {
27
- // if (!input.startsWith('It')) validation.throwError(`"title" must start with 'it' or 'It'`)
28
- // return input.length < 5;
29
- // }, { message: '"title" must not exceed max length of 5' })
30
-
31
-
32
- validation.NotNull(1);
33
- // validation.NotNullish(null, { required: true, default: null });
34
- // validation.String(null, { required: true, default: 1 });
35
-
36
- validation.DateLike('2022-10-29a', { default: new Date().getTime() });
37
- validation.Date(new Date());
38
-
39
- function Abc(a) {
40
- this.a = a;
41
- }
42
-
43
- console.log(JSON.stringify(new Abc(123)));
44
-
45
- validation.Object(new Abc(123));
14
+ console.log('config', config);
package/index.js CHANGED
@@ -32,7 +32,7 @@ const { WebBuilder } = require('./src/web');
32
32
  const { binh } = require('./src/web/component.method');
33
33
 
34
34
  // Run scripts
35
- require('./src/supportRequireAliasPath');
35
+ require('./src/alias-module');
36
36
 
37
37
  module.exports = {
38
38
  server,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "2.1.25",
3
+ "version": "2.1.27",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -8,8 +8,14 @@
8
8
  "bin": {
9
9
  "binhend": "./bin/index.js"
10
10
  },
11
+ "workspaces": [
12
+ "test"
13
+ ],
11
14
  "scripts": {
12
- "test": "echo \"Error: no test specified\" && exit 1",
15
+ "test": "npm run test --workspace=test",
16
+ "test:coverage": "npm run test:coverage --workspace=test",
17
+ "test:jest": "jest --config=test/jest.config.js",
18
+
13
19
  "example-api": "node example_api PORT=5555",
14
20
  "example-webcomp": "node example_webcomp",
15
21
  "example-web2": "node example_web2"
@@ -2,6 +2,7 @@
2
2
 
3
3
  const path = require('path');
4
4
  const DefaultModule = require('module');
5
+ const { binh } = require('./web/component.method'); // TODO handle this 'binh' object again, more properly, workspaces will not recognize global 'binh'
5
6
 
6
7
  // Protect against improperly customized module constructors by other frameworks/packages
7
8
  const Module = module.constructor.length > 1 ? module.constructor : DefaultModule;
@@ -15,7 +16,7 @@ try {
15
16
  jsconfig = require(jsconfigPath);
16
17
  }
17
18
  catch (error) {
18
- return console.log(`[BINEND] Not load 'jsconfig.json' for alias paths.`);
19
+ return console.log(`[BINEND] Not found 'jsconfig.json' for alias paths.`);
19
20
  }
20
21
 
21
22
  const baseUrl = jsconfig?.compilerOptions?.baseUrl || '.';
@@ -45,7 +46,9 @@ Module._resolveFilename = function (request, parent, isMain, options) {
45
46
  // Construct the new request path
46
47
  var newRequest = request.replace(alias, resolvedPath);
47
48
 
49
+ // BINHEND web components load each others stil requiring src paths (src file paths), not module paths (formatted file paths)
48
50
  if (parent.filename.startsWith(binh.webModulePath)) {
51
+ // TODO test if this branch is acutally called.
49
52
  newRequest = newRequest.replace(binh.webSourcePath, binh.webModulePath);
50
53
  }
51
54
 
@@ -0,0 +1,40 @@
1
+
2
+ // TODO apply module alias loader for ES Modules
3
+ // TODO like in alias-cjs-loader.js, need to handle paths on UI components requiring each others (paths for web, src, module)
4
+
5
+ // alias-loader.mjs
6
+ import { fileURLToPath, pathToFileURL } from 'url';
7
+ import path from 'path';
8
+ import fs from 'fs';
9
+
10
+ // Load jsconfig paths
11
+ const configPath = path.resolve('jsconfig.json');
12
+ const tsconfig = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
13
+ const baseUrl = path.resolve(tsconfig.compilerOptions?.baseUrl || '.');
14
+ const paths = tsconfig.compilerOptions?.paths || {};
15
+
16
+ function resolveAlias(specifier) {
17
+ for (const alias in paths) {
18
+ if (!alias.endsWith('/*')) continue;
19
+ const aliasPrefix = alias.slice(0, -2);
20
+ if (specifier.startsWith(aliasPrefix)) {
21
+ const subPath = specifier.slice(aliasPrefix.length);
22
+ const targetPaths = paths[alias];
23
+ for (const target of targetPaths) {
24
+ const fullPath = path.resolve(baseUrl, target.replace('/*', subPath));
25
+ if (fs.existsSync(fullPath) || fs.existsSync(fullPath + '.js') || fs.existsSync(path.join(fullPath, 'index.js'))) {
26
+ return pathToFileURL(fullPath).href;
27
+ }
28
+ }
29
+ }
30
+ }
31
+ return null;
32
+ }
33
+
34
+ export async function resolve(specifier, context, defaultResolve) {
35
+ const resolved = resolveAlias(specifier);
36
+ if (resolved) {
37
+ return { url: resolved };
38
+ }
39
+ return defaultResolve(specifier, context, defaultResolve);
40
+ }
@@ -0,0 +1,4 @@
1
+
2
+ require('./alias-cjs-loader');
3
+
4
+ // require('./alias-esm-loader'); // TODO implement this alias loader for ES Modules
@@ -1,6 +1,6 @@
1
1
  const fs = require('fs');
2
2
  const path = require('path');
3
- const { isEmptyArray, isArray, isObject, mustString } = require('./utils/types');
3
+ const { isEmptyArray, isArray, isObject, mustString, isNullish } = require('./utils/types');
4
4
 
5
5
  // @ts-ignore
6
6
  function ConfigLoader(configObject, { rootPath } = {}) {
@@ -8,23 +8,24 @@ function ConfigLoader(configObject, { rootPath } = {}) {
8
8
 
9
9
  rootPath = mustString(rootPath) || require.main.path;
10
10
 
11
- this.getConfigs = () => {
12
- return configs;
13
- };
14
-
15
11
  function getConfigPosition(key) {
16
12
  return key == undefined ? configs : configs[key] || (configs[key] = {});
17
13
  };
18
14
 
19
15
  // @ts-ignore
20
- this.object = (object, { key, filter: filters } = {}) => {
21
- try {
22
- var config = getConfigPosition(key);
23
- var filtered = isArray(filters) ? filter(object, filters) : object;
24
- Object.assign(config, filtered);
16
+ this.object = (object, { key, filter: filters, overwrite } = {}) => {
17
+ let config = getConfigPosition(key);
18
+ let filtered = isArray(filters) ? filter(object, filters) : object;
19
+
20
+ if (overwrite === false) {
21
+ for (let key in filtered) {
22
+ if (isNullish(config[key])) {
23
+ config[key] = filtered[key];
24
+ }
25
+ }
25
26
  }
26
- catch (error) {
27
- return failed(error, object);
27
+ else {
28
+ Object.assign(config, filtered);
28
29
  }
29
30
 
30
31
  return this;
@@ -34,17 +35,35 @@ function ConfigLoader(configObject, { rootPath } = {}) {
34
35
  return this.object(cli(), options);
35
36
  };
36
37
 
37
- this.json = (filepath, options) => {
38
- var filePath = path.resolve(rootPath, filepath);
39
- return this.object(json(filePath), options);
38
+ this.json = (filepath, options = {}) => {
39
+ let filePath = path.resolve(rootPath, filepath);
40
+ let jsonObject = json(filePath);
41
+
42
+ if (options.hasOwnProperty('env')) {
43
+ let env = options.env;
44
+ for (let key in jsonObject) {
45
+ let value = jsonObject[key];
46
+ if (isObject(value) && value.hasOwnProperty(env)) {
47
+ // if object has no "env" key, it means the intended value is the whole object => no new assignment
48
+ jsonObject[key] = value[env];
49
+ }
50
+ }
51
+ }
52
+
53
+ return this.object(jsonObject, options);
40
54
  };
41
55
 
42
56
  this.file = (filepath, options) => {
43
- var filePath = path.resolve(rootPath, filepath);
57
+ let filePath = path.resolve(rootPath, filepath);
44
58
  return this.object(file(filePath, options?.encoding), options);
45
59
  };
46
60
  }
47
61
 
62
+ ConfigLoader.cli = cli;
63
+ ConfigLoader.json = json;
64
+ ConfigLoader.file = file;
65
+ ConfigLoader.require = require.main.require;
66
+
48
67
  function filter(object, filterKeys) {
49
68
  if (!isObject(object) || isEmptyArray(filterKeys)) return {};
50
69
 
@@ -61,7 +80,8 @@ function filter(object, filterKeys) {
61
80
 
62
81
  function json(path) {
63
82
  try {
64
- return require.main.require(path);
83
+ // return require.main.require(path);
84
+ return ConfigLoader.require(path);
65
85
  }
66
86
  catch (e) {
67
87
  return failed(e, path);
package/src/crypto.js CHANGED
@@ -6,7 +6,7 @@ const lenght_iv = 16;
6
6
  const lenght_key = 32;
7
7
 
8
8
  function randomUUID(disableEntropyCache = true) {
9
- crypto.randomUUID({ disableEntropyCache });
9
+ return crypto.randomUUID({ disableEntropyCache });
10
10
  }
11
11
 
12
12
  function generateSecurityKey() {
package/src/server.js CHANGED
@@ -1,7 +1,11 @@
1
1
  const express = require('express');
2
2
 
3
- const server = express();
3
+ var server;
4
+
5
+ function getServer() {
6
+ return server || (server = express());
7
+ }
4
8
 
5
9
  module.exports = {
6
- server
10
+ server: getServer
7
11
  };
@@ -4,12 +4,16 @@ function Bromise() {
4
4
 
5
5
  this.promise = promise;
6
6
 
7
- this.resolve = (value) => {
7
+ this.done = function() {
8
+ return promise;
9
+ };
10
+
11
+ this.returns = this.resolve = function(value) {
8
12
  resolve(value);
9
13
  return promise;
10
14
  };
11
15
 
12
- this.reject = (error) => {
16
+ this.throws = this.reject = function(error) {
13
17
  reject(error);
14
18
  return promise;
15
19
  };
@@ -189,7 +189,7 @@ function Enum(input, enumValues, options = {}) {
189
189
  * @throws {HttpError} - If validation fails
190
190
  */
191
191
  function Defined(input, options = {}) {
192
- return Validator(input, (input) => !types.isUndefined(input), { ...options, type: 'defined' });
192
+ return Validator(input, (input) => !types.isUndefined(input), { ...options, type: 'defined', required: true });
193
193
  }
194
194
 
195
195
  /**