binhend 2.1.23 → 2.1.25

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "2.1.23",
3
+ "version": "2.1.25",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -1,11 +1,12 @@
1
1
  const fs = require('fs');
2
2
  const path = require('path');
3
- const { isEmptyArray, isArray, isObject } = require('./utils/types');
3
+ const { isEmptyArray, isArray, isObject, mustString } = require('./utils/types');
4
4
 
5
5
  // @ts-ignore
6
- function ConfigLoader(configObject, { module } = {}) {
6
+ function ConfigLoader(configObject, { rootPath } = {}) {
7
7
  const configs = configObject || {};
8
- const rootPath = module?.path || require.main.path;
8
+
9
+ rootPath = mustString(rootPath) || require.main.path;
9
10
 
10
11
  this.getConfigs = () => {
11
12
  return configs;
@@ -22,8 +23,8 @@ function ConfigLoader(configObject, { module } = {}) {
22
23
  var filtered = isArray(filters) ? filter(object, filters) : object;
23
24
  Object.assign(config, filtered);
24
25
  }
25
- catch(e) {
26
- return failed(e, object);
26
+ catch (error) {
27
+ return failed(error, object);
27
28
  }
28
29
 
29
30
  return this;
@@ -46,7 +47,7 @@ function ConfigLoader(configObject, { module } = {}) {
46
47
 
47
48
  function filter(object, filterKeys) {
48
49
  if (!isObject(object) || isEmptyArray(filterKeys)) return {};
49
-
50
+
50
51
  var filtered = {};
51
52
 
52
53
  filterKeys.forEach((field) => {
@@ -62,7 +63,7 @@ function json(path) {
62
63
  try {
63
64
  return require.main.require(path);
64
65
  }
65
- catch(e) {
66
+ catch (e) {
66
67
  return failed(e, path);
67
68
  }
68
69
  }
@@ -82,7 +83,7 @@ function file(path, encoding) {
82
83
 
83
84
  return processKeyValueStrings(lines);
84
85
  }
85
- catch(e) {
86
+ catch (e) {
86
87
  return failed(e, path);
87
88
  }
88
89
  }
@@ -96,7 +97,7 @@ function processKeyValueStrings(strings) {
96
97
 
97
98
  var regex_is_comment = /^#/;
98
99
 
99
- strings.forEach(function(string) {
100
+ strings.forEach(function (string) {
100
101
  if (regex_is_comment.test(string.trim())) return;
101
102
 
102
103
  var parsed = parseKeyValue(string);
package/src/https.js CHANGED
@@ -1,91 +1,92 @@
1
- // @ts-nocheck
2
1
 
3
2
  const https = require('https');
4
3
 
5
4
  function HTTPS(url) {
6
5
  this.url = url;
7
6
  this.parameters = [];
7
+ };
8
8
 
9
- this.get = function(callback) {
10
- return this.request('GET', callback);
11
- }
12
-
13
- this.post = function(callback) {
14
- return this.request('POST', callback);
15
- }
16
-
17
- this.request = function(method, callback) {
18
- var URL = this.url + this.parameters.join('/') + (this.queries || '');
19
-
20
- var options = {
21
- method,
22
- headers: this.headers
23
- };
24
-
25
- var req = https.request(URL, options, function(res) {
26
- res.setEncoding('utf8');
27
-
28
- var data = '';
29
-
30
- res.on('data', (chunk) => {
31
- data += chunk;
32
- });
33
-
34
- res.on('end', () => {
35
- var response;
36
-
37
- try {
38
- response = JSON.parse(data);
39
- }
40
- catch(error) {
41
- response = data;
42
- }
43
-
44
- if (callback instanceof Function) {
45
- callback(response);
46
- }
47
- });
9
+ HTTPS.prototype.get = function(callback) {
10
+ return this.request('GET', callback);
11
+ }
12
+
13
+ HTTPS.prototype.post = function(callback) {
14
+ return this.request('POST', callback);
15
+ }
16
+
17
+ HTTPS.prototype.request = function(method, callback) {
18
+
19
+ var URL = this.url + this.parameters.join('/') + (this.queries || '');
20
+
21
+ var options = {
22
+ method,
23
+ headers: this.headers
24
+ };
25
+
26
+ var req = https.request(URL, options, function(res) {
27
+ res.setEncoding('utf8');
28
+
29
+ var data = '';
30
+
31
+ res.on('data', (chunk) => {
32
+ data += chunk;
48
33
  });
49
-
50
- req.on("error", (err) => {
51
- console.log("HTTPS request encounters error:" + err.message);
34
+
35
+ res.on('end', () => {
36
+ var response;
37
+
38
+ try {
39
+ response = JSON.parse(data);
40
+ }
41
+ catch(error) {
42
+ response = data;
43
+ }
44
+
45
+ if (callback instanceof Function) {
46
+ callback(response);
47
+ }
52
48
  });
53
-
54
- if (this.payload) {
55
- req.write(JSON.stringify(this.payload));
56
- }
57
-
58
- req.end();
59
-
60
- return this;
61
- }
62
-
63
- this.header = function(map) {
64
- this.headers = map;
65
- return this;
66
- }
67
-
68
- this.body = function(map) {
69
- this.payload = JSON.stringify(map);
70
- return this;
49
+ });
50
+
51
+ req.on('error', (err) => {
52
+ console.error(`[BINHEND][HTTPS] Error request: ${method} ${URL}`);
53
+ console.error(`[BINHEND][HTTPS] --- Error message: ${err.message}`);
54
+ });
55
+
56
+ if (this.payload) {
57
+ req.write(JSON.stringify(this.payload));
71
58
  }
72
-
73
- this.parameter = function(array) {
74
- if (array instanceof Array) {
75
- var last_char = this.url[this.url.length - 1];
76
- this.parameters = (last_char === '/' ? [] : ['']).concat(array);
77
- }
78
- return this;
59
+
60
+ req.end();
61
+
62
+ return this;
63
+ }
64
+
65
+ HTTPS.prototype.header = function(map) {
66
+ this.headers = map;
67
+ return this;
68
+ }
69
+
70
+ HTTPS.prototype.body = function(map) {
71
+ this.payload = JSON.stringify(map);
72
+ return this;
73
+ }
74
+
75
+ HTTPS.prototype.parameter = function(array) {
76
+ if (array instanceof Array) {
77
+ var lastChar = this.url[this.url.length - 1];
78
+ this.parameters = (lastChar === '/' ? [] : ['']).concat(array);
79
79
  }
80
-
81
- this.query = function(map) {
82
- var query_string = '';
83
- for (var key in map) {
84
- query_string += key + '=' + map[key] + '&';
85
- }
86
- this.queries = '?' + query_string;
87
- return this;
80
+ return this;
81
+ }
82
+
83
+ HTTPS.prototype.query = function(map) {
84
+ var queryString = '';
85
+ for (var key in map) {
86
+ queryString += key + '=' + map[key] + '&';
88
87
  }
89
- };
88
+ this.queries = '?' + queryString;
89
+ return this;
90
+ }
90
91
 
91
92
  module.exports = { HTTPS };
@@ -53,7 +53,8 @@ module.exports = (options) => {
53
53
 
54
54
  // Handle preflight OPTIONS request
55
55
  if (req.method === 'OPTIONS' && !preflightContinue) {
56
- return res.sendStatus(mustNumber(optionsSuccessStatus, 204));
56
+ var statusCode = mustNumber(optionsSuccessStatus) || 204; // 204 - No Content
57
+ return res.sendStatus(statusCode);
57
58
  }
58
59
 
59
60
  return next();
@@ -30,16 +30,22 @@ function generate(sourceRootPath, outputRootPath, callbackDone) {
30
30
  writeToFileIfNew(fileOutputPath + '.js', cssModuleCode);
31
31
  // not return here, but wait for next "if" statement to be copied to build folder, then return there.
32
32
  }
33
-
34
- if (fileExtension !== '.js' && fileExtension !== '.mjs') {
33
+
34
+ if (fileExtension !== '.js' && fileExtension !== '.cjs' && fileExtension !== '.mjs') {
35
35
  // any files not JS (.css, .jpg, etc.) will be copied to build folder.
36
- return cloneFileIfNew(fileSourcePath, fileOutputPath);
36
+ cloneFileIfNew(fileSourcePath, fileOutputPath);
37
+ return;
37
38
  }
38
39
 
39
- var content = readFileSync(fileSourcePath, { encoding: 'utf8', flag: 'r' });
40
- var code = PREFIX_CODE + mapConfigValues(content.trim()) + '\r\n\r\n;binh.final(module);';
40
+ var fileContent = readFileSync(fileSourcePath, { encoding: 'utf8', flag: 'r' });
41
+ var formatCode = mapConfigValues(fileContent.trim()); // for .cjs (common js) and .mjs (module js) files, not build code but still bind config values
42
+
43
+ if (fileExtension === '.js') {
44
+ // for .js files, build code and bind config values
45
+ formatCode = PREFIX_CODE + formatCode + '\r\n\r\n;binh.final(module);';
46
+ }
41
47
 
42
- writeToFileIfNew(fileOutputPath, code);
48
+ writeToFileIfNew(fileOutputPath, formatCode);
43
49
  }
44
50
  catch (error) {
45
51
  printError('Failed formatting file:', fileSourcePath, error);