noxt-server 0.1.6 → 0.1.13

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/noxt-server.js CHANGED
@@ -16,14 +16,20 @@ export async function startServer({ config, recipe }) {
16
16
  : `${__dirname}/units/${name}.js`
17
17
  });
18
18
  try {
19
+
19
20
  const report = await mlmInstance.analyze(recipe ?? 'noxt-dev');
20
21
  if (!report.success) {
21
- console.log(report.order.join(', '));
22
+ console.log('Bad recipe: ' + recipe + '\n' + report.order.join(', '));
23
+ console.log(report.errors.join('\n'));
22
24
  process.exit(1);
23
25
  }
24
26
 
25
27
  await mlmInstance.install(recipe ?? 'noxt-dev');
26
- await mlmInstance.start(config);
28
+ const mlm = mlmInstance.context;
29
+ await mlm.services.config.merge(config);
30
+ console.log(mlm.config);
31
+ await mlmInstance.start();
32
+
27
33
  } catch (e) {
28
34
  console.log(await mlmInstance.analyze(recipe ?? 'noxt-dev'));
29
35
  console.error(e);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "noxt-server",
3
- "version": "0.1.6",
3
+ "version": "0.1.13",
4
4
  "description": "Server for noxt-js-middleware with CLI and config support",
5
5
  "type": "module",
6
6
  "main": "noxt-server.js",
package/units/config.js CHANGED
@@ -10,34 +10,47 @@ const config_defaults = {};
10
10
  const config_defs = {}
11
11
 
12
12
  function merge_deep(target, ...sources) {
13
- if (!sources.length) return target;
14
-
15
- const source = sources.shift();
16
-
17
- if (is_plain(target) && is_plain(source)) {
18
- for (const key in source) {
19
- if (source.hasOwnProperty(key)) {
20
- // If target doesn't have this key, simply assign
21
- if (!target.hasOwnProperty(key)) {
22
- target[key] = source[key];
23
- }
24
- // If both target and source have this key and both are plain objects, recurse
25
- else if (is_plain(target[key]) && is_plain(source[key])) {
26
- merge_deep(target[key], source[key]);
13
+ // If no sources, return target
14
+ if (sources.length === 0) return target;
15
+
16
+ // If target is not a plain object, return the first source (overwrite)
17
+ if (!isPlainObject(target)) {
18
+ return sources[0];
19
+ }
20
+
21
+ const result = Object.assign({}, target);
22
+
23
+ for (const source of sources) {
24
+ // If source is not a plain object, skip (treat as scalar)
25
+ if (!isPlainObject(source)) {
26
+ continue;
27
+ }
28
+
29
+ for (const [key, sourceValue] of Object.entries(source)) {
30
+ const targetValue = result[key];
31
+
32
+ // If both values are plain objects, merge recursively
33
+ if (isPlainObject(targetValue) && isPlainObject(sourceValue)) {
34
+ result[key] = merge_deep(targetValue, sourceValue);
35
+ }
36
+ // Otherwise, overwrite target with source value
37
+ else {
38
+ result[key] = sourceValue;
39
+ }
27
40
  }
28
- // If target has a non-plain object value, ignore (don't overwrite or try to merge into it)
29
- // else: do nothing (keep target's existing value)
30
- }
31
41
  }
32
- }
33
-
34
- return merge_deep(target, ...sources);
42
+
43
+ return result;
35
44
  }
36
45
 
37
- function is_plain(obj) {
38
- return obj !== null &&
39
- typeof obj === 'object' &&
40
- (obj.constructor === Object || obj.constructor === null);
46
+ // Helper function to check if value is a plain object
47
+ function isPlainObject(value) {
48
+ if (value === null || typeof value !== 'object') {
49
+ return false;
50
+ }
51
+
52
+ const proto = Object.getPrototypeOf(value);
53
+ return proto === null || proto === Object.prototype;
41
54
  }
42
55
 
43
56
  export default mlm => ({
@@ -48,13 +61,15 @@ export default mlm => ({
48
61
  mlm.assert.not(key in config_defs, 'Duplicate config key ' + key);
49
62
  mlm.assert.is({
50
63
  is: 'any|none',
51
- default: 'any|none'
64
+ default: 'any|none',
65
+ normalize: 'function|none'
52
66
  }, def, `config.${key}`);
53
67
 
54
68
  if ('default' in def) {
55
69
  config_defaults[key] = mlm.utils.eval(def.default);
56
70
  }
57
71
  config_defs[key] = {
72
+ normalize: def.normalize,
58
73
  is: def.is,
59
74
  default: def.default,
60
75
  unit: unit
@@ -67,14 +82,23 @@ export default mlm => ({
67
82
  const merged = merge_deep({}, config_defaults, config);
68
83
  const ret = {};
69
84
  for (const key in config_defs) {
70
- const { is: type } = config_defs[key];
85
+ const { is: type, normalize } = config_defs[key];
71
86
  if (key in merged) {
87
+ if (normalize) {
88
+ merged[key] = normalize(merged[key]);
89
+ }
90
+
72
91
  mlm.assert.is(type, merged[key], `config.${key}`);
73
92
  ret[key] = merged[key];
74
93
  }
75
94
  }
76
95
  return ret;
77
96
  }
97
+ merge(config) {
98
+ mlm.log('config', config,mlm.config);
99
+ Object.assign(mlm.config, this.process(config));
100
+
101
+ }
78
102
  get_defs() {
79
103
  return {
80
104
  config_defs,
@@ -83,7 +107,4 @@ export default mlm => ({
83
107
  };
84
108
  }
85
109
  },
86
- onStart(config) {
87
- Object.assign(mlm.config, mlm.services.config.process(config));
88
- }
89
110
  })
package/units/express.js CHANGED
@@ -48,10 +48,6 @@ export default mlm => ({
48
48
  async onStart() {
49
49
  const { default: express } = await mlm.import('express');
50
50
  app = express();
51
- app.use((req, res, next) => {
52
- mlm.log(req.method, req.url);
53
- next();
54
- })
55
51
  for (const middleware of middlewares) {
56
52
  const mw = await middleware.create(app);
57
53
  if (middleware.path) {
package/units/logger.js CHANGED
@@ -8,7 +8,7 @@ export const info = {
8
8
  export default mlm => ({
9
9
  'middleware.logger': async (app) => {
10
10
  return (req, res, next) => {
11
- console.log(req.method, req.url);
11
+ mlm.log(req.method, req.url);
12
12
  next();
13
13
  }
14
14
  },
@@ -10,6 +10,7 @@ export const info = {
10
10
 
11
11
  export default mlm => ({
12
12
  'config.views': {
13
+ normalize: p => [p].flat(),
13
14
  is: ['string'],
14
15
  default: ['views']
15
16
  },
@@ -10,6 +10,7 @@ export const info = {
10
10
 
11
11
  export default mlm => ({
12
12
  'config.views': {
13
+ normalize: p => [p].flat(),
13
14
  is: ['string'],
14
15
  default: ['views']
15
16
  },