binhend 2.1.26 → 2.1.28
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 +8 -39
- package/index.js +1 -1
- package/package.json +8 -2
- package/src/{supportRequireAliasPath.js → alias-cjs-loader.js} +4 -1
- package/src/alias-esm-loader.js +40 -0
- package/src/alias-module.js +4 -0
- package/src/api/routes.js +2 -2
- package/src/configuration.js +15 -8
- package/src/crypto.js +1 -1
- package/src/server.js +6 -2
- package/src/utils/Bromise.js +6 -2
- package/src/utils/validation.js +1 -1
- package/src/web/index.js +2 -2
package/demo.js
CHANGED
|
@@ -1,45 +1,14 @@
|
|
|
1
1
|
|
|
2
2
|
const { HttpCodes, ConfigLoader, WebBuilder, HttpError, validation } = require('./index');
|
|
3
3
|
|
|
4
|
-
//
|
|
4
|
+
// var jsonPath = __dirname + '/jsconfig.json';
|
|
5
|
+
// var cc = ConfigLoader.json(jsonPath);
|
|
6
|
+
// console.log('cc', cc);
|
|
5
7
|
|
|
6
|
-
|
|
8
|
+
const config = {};
|
|
9
|
+
console.log('config', config);
|
|
10
|
+
const loader = new ConfigLoader(config);
|
|
7
11
|
|
|
8
|
-
|
|
12
|
+
loader.object({ foo: '1', koo: '2', hoo: '3' }, { filter: ['koo', 'hoo'] });
|
|
9
13
|
|
|
10
|
-
|
|
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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "binhend",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.28",
|
|
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": "
|
|
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
|
|
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
|
+
}
|
package/src/api/routes.js
CHANGED
|
@@ -71,9 +71,9 @@ async function buildRoutes(dirPath) {
|
|
|
71
71
|
* @param {Object} corsOptions - CORS configuration options
|
|
72
72
|
*/
|
|
73
73
|
async function loadRoutes(dirPath, corsOptions) {
|
|
74
|
-
server.use(cors(corsOptions));
|
|
74
|
+
server().use(cors(corsOptions));
|
|
75
75
|
const router = await buildRoutes(dirPath);
|
|
76
|
-
server.use(router);
|
|
76
|
+
server().use(router);
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
const routes = {
|
package/src/configuration.js
CHANGED
|
@@ -13,14 +13,19 @@ function ConfigLoader(configObject, { rootPath } = {}) {
|
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
// @ts-ignore
|
|
16
|
-
this.object = (object, { key, filter: filters } = {}) => {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
+
}
|
|
21
26
|
}
|
|
22
|
-
|
|
23
|
-
|
|
27
|
+
else {
|
|
28
|
+
Object.assign(config, filtered);
|
|
24
29
|
}
|
|
25
30
|
|
|
26
31
|
return this;
|
|
@@ -57,6 +62,7 @@ function ConfigLoader(configObject, { rootPath } = {}) {
|
|
|
57
62
|
ConfigLoader.cli = cli;
|
|
58
63
|
ConfigLoader.json = json;
|
|
59
64
|
ConfigLoader.file = file;
|
|
65
|
+
ConfigLoader.require = require.main.require;
|
|
60
66
|
|
|
61
67
|
function filter(object, filterKeys) {
|
|
62
68
|
if (!isObject(object) || isEmptyArray(filterKeys)) return {};
|
|
@@ -74,7 +80,8 @@ function filter(object, filterKeys) {
|
|
|
74
80
|
|
|
75
81
|
function json(path) {
|
|
76
82
|
try {
|
|
77
|
-
return require.main.require(path);
|
|
83
|
+
// return require.main.require(path);
|
|
84
|
+
return ConfigLoader.require(path);
|
|
78
85
|
}
|
|
79
86
|
catch (e) {
|
|
80
87
|
return failed(e, path);
|
package/src/crypto.js
CHANGED
package/src/server.js
CHANGED
package/src/utils/Bromise.js
CHANGED
|
@@ -4,12 +4,16 @@ function Bromise() {
|
|
|
4
4
|
|
|
5
5
|
this.promise = promise;
|
|
6
6
|
|
|
7
|
-
this.
|
|
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
|
};
|
package/src/utils/validation.js
CHANGED
|
@@ -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
|
/**
|
package/src/web/index.js
CHANGED
|
@@ -45,8 +45,8 @@ function WebBuilder(source, { output, external } = {}) {
|
|
|
45
45
|
};
|
|
46
46
|
|
|
47
47
|
setTimeout(() => {
|
|
48
|
-
server.use(express.static(web));
|
|
49
|
-
server.get('/*', (req, res) => res.sendFile('index.html', { root: web }));
|
|
48
|
+
server().use(express.static(web));
|
|
49
|
+
server().get('/*', (req, res) => res.sendFile('index.html', { root: web }));
|
|
50
50
|
});
|
|
51
51
|
|
|
52
52
|
binh.webModulePath = module;
|