noxt-server 0.1.14 → 0.1.16

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/units.txt CHANGED
@@ -1,6 +1,4 @@
1
- ------
2
- fetch.js
3
- -----
1
+ === ./fetch.js ===
4
2
  export const info = {
5
3
  version: '1.0.0',
6
4
  description: 'Preload props from urls',
@@ -27,10 +25,7 @@ export default mlm => ({
27
25
  }
28
26
  }
29
27
  },
30
- })
31
- ------
32
- config.js
33
- -----
28
+ })=== ./config.js ===
34
29
  export const info = {
35
30
  name: 'config',
36
31
  version: '1.0.0',
@@ -38,6 +33,7 @@ export const info = {
38
33
  requires: ['utils','services'],
39
34
  }
40
35
 
36
+ const config = {};
41
37
  const config_is = {};
42
38
  const config_defaults = {};
43
39
  const config_defs = {}
@@ -87,8 +83,12 @@ function isPlainObject(value) {
87
83
  }
88
84
 
89
85
  export default mlm => ({
90
- '$global.config': () => ({}),
91
- '$define.config': (defs, unit) => {
86
+ 'define.config': {
87
+ get: () => config,
88
+ enumerable: true,
89
+ configurable: false
90
+ },
91
+ 'register.config': (defs, unit) => {
92
92
  for (const key in defs) {
93
93
  const def = defs[key];
94
94
  mlm.assert.not(key in config_defs, 'Duplicate config key ' + key);
@@ -111,8 +111,8 @@ export default mlm => ({
111
111
  },
112
112
  'utils.merge_deep': merge_deep,
113
113
  'services.config': () => new class ConfigService {
114
- process(config) {
115
- const merged = merge_deep({}, config_defaults, config);
114
+ process(userConfig) {
115
+ const merged = merge_deep({}, config_defaults, userConfig);
116
116
  const ret = {};
117
117
  for (const key in config_defs) {
118
118
  const { is: type, normalize } = config_defs[key];
@@ -127,10 +127,10 @@ export default mlm => ({
127
127
  }
128
128
  return ret;
129
129
  }
130
- merge(config) {
131
- mlm.log('config', config,mlm.config);
132
- Object.assign(mlm.config, this.process(config));
133
-
130
+ merge(userConfig) {
131
+ console.log('initial',config_defaults,userConfig)
132
+ Object.assign(config, this.process(userConfig));
133
+ console.log(userConfig,'final',config)
134
134
  }
135
135
  get_defs() {
136
136
  return {
@@ -140,10 +140,7 @@ export default mlm => ({
140
140
  };
141
141
  }
142
142
  },
143
- })
144
- ------
145
- fetch-cache.js
146
- -----
143
+ })=== ./fetch-cache.js ===
147
144
  export const info = {
148
145
  name: 'fetch-cache',
149
146
  version: '1.0.0',
@@ -191,10 +188,7 @@ async function globalFetch(ctx, url, options = {}) {
191
188
  const res = await fetchCache(url, options);
192
189
  if (!res.ok) throw new Error(res.statusText);
193
190
  return res;
194
- }
195
- ------
196
- services.js
197
- -----
191
+ }=== ./services.js ===
198
192
  export const info = {
199
193
  name: 'services',
200
194
  version: '1.0.0',
@@ -205,8 +199,8 @@ export const info = {
205
199
  const services = {}
206
200
 
207
201
  export default mlm => ({
208
- '$global.services': () => mlm.utils.readOnly(services, 'services'),
209
- '$define.services': mlm.utils.collector(services, {
202
+ 'define.services': () => mlm.utils.readOnly(services, 'services'),
203
+ 'register.services': mlm.utils.collector(services, {
210
204
  is: 'function',
211
205
  mode: 'object',
212
206
  map: (fn, key) => {
@@ -215,10 +209,7 @@ export default mlm => ({
215
209
  return service
216
210
  }
217
211
  }),
218
- })
219
- ------
220
- noxt.js
221
- -----
212
+ })=== ./noxt.js ===
222
213
  export const info = {
223
214
  name: 'noxt',
224
215
  version: '1.0.0',
@@ -231,10 +222,7 @@ export const info = {
231
222
  'fetch-cache-fs',
232
223
  'fetch',
233
224
  ],
234
- }
235
- ------
236
- utils.js
237
- -----
225
+ }=== ./utils.js ===
238
226
  export const info = {
239
227
  name: 'utils',
240
228
  version: '1.0.0',
@@ -244,16 +232,16 @@ export const info = {
244
232
  export default mlm => {
245
233
 
246
234
  const utils = {};
247
- utils.eval = (fn,...args) => typeof fn === 'function' ? fn(...args) : fn;
235
+ utils.eval = (fn, ...args) => typeof fn === 'function' ? fn(...args) : fn;
248
236
  utils.readOnly = (obj, { label = 'object' } = {}) => new Proxy(obj, {
249
237
  get: (t, k) => t[k],
250
238
  set: (t, k, v) => { mlm.throw(label + ' is read-only'); }
251
239
  });
252
- utils.collector = (target, {
253
- map, is, filter,
254
- label = 'object',
255
- mode = 'object',
256
- override = false
240
+ utils.collector = (target, {
241
+ map, is, filter,
242
+ label = 'object',
243
+ mode = 'object',
244
+ override = false
257
245
  } = {}) => {
258
246
  const modes = {
259
247
  array: () => source => {
@@ -271,7 +259,7 @@ export default mlm => {
271
259
  }
272
260
  let value = source[key];
273
261
  if (filter && !filter(value, key)) continue;
274
- if (is) mlm.assert.is( is,value, label + '.' + key);
262
+ if (is) mlm.assert.is(is, value, label + '.' + key);
275
263
  if (map) value = map(value, key);
276
264
  target[key] = value;
277
265
  }
@@ -309,13 +297,10 @@ export default mlm => {
309
297
  return modes[mode]();
310
298
  }
311
299
  return ({
312
- '$global.utils': () => utils.readOnly(utils, 'utils'),
313
- '$define.utils': utils.collector(utils, { is: 'function', mode: 'object' }),
300
+ 'define.utils': () => utils.readOnly(utils, 'utils'),
301
+ 'register.utils': utils.collector(utils, { is: 'function', mode: 'object', map: (fn, key) => { mlm.log('utils', key); return fn; } }),
314
302
  })
315
- }
316
- ------
317
- fetch-node.js
318
- -----
303
+ }=== ./fetch-node.js ===
319
304
  export const info = {
320
305
  name: 'fetch-node',
321
306
  version: '1.0.0',
@@ -336,19 +321,13 @@ async function fetchOrThrow(ctx, url, options = {}) {
336
321
  const res = await fetch(url, options);
337
322
  if (!res.ok) throw new Error(res.statusText);
338
323
  return res;
339
- }
340
- ------
341
- plugin.js
342
- -----
324
+ }=== ./plugin.js ===
343
325
  export const info = {
344
326
  name: 'plugin',
345
327
  version: '1.0.0',
346
328
  description: 'Base',
347
- requires: ['utils','services','hooks','config']
348
- }
349
- ------
350
- reload.js
351
- -----
329
+ requires: ['env','utils','services','hooks','config']
330
+ }=== ./reload.js ===
352
331
  export const info = {
353
332
  name: 'reload',
354
333
  description: 'Hot reload middleware',
@@ -424,28 +403,31 @@ eventSource.addEventListener('reload', () => {
424
403
  window.addEventListener('beforeunload', () => {
425
404
  eventSource.close();
426
405
  });
427
- `
428
- ------
429
- logger.js
430
- -----
406
+ `=== ./logger.js ===
431
407
  export const info = {
432
408
  name: 'logger',
433
- version: '1.0.0',
434
- description: 'Minimal logger',
409
+ version: '1.0.1',
410
+ description: 'Noxt logger',
435
411
  requires: ['express'],
436
412
  }
437
413
 
438
414
  export default mlm => ({
439
- 'middleware.logger': async (app) => {
440
- return (req, res, next) => {
441
- mlm.log(req.method, req.url);
442
- next();
443
- }
415
+ 'middleware.logger': async () => (req, res, next) => {
416
+ const start = process.hrtime.bigint();
417
+ const ip = req.headers['x-forwarded-for']?.split(',')[0] || req.socket.remoteAddress;
418
+ const ua = req.headers['user-agent'] || '-';
419
+ res.on('finish', () => {
420
+ const dur = Number(process.hrtime.bigint() - start) / 1e6; // ms
421
+ const date = new Date().toISOString();
422
+ mlm.log(
423
+ `[${date}] ${ip} ${req.method} ${req.originalUrl || req.url} ` +
424
+ `| HTTP/${req.httpVersion} | ${res.statusCode} | ${ua} | ${dur.toFixed(2)}ms `
425
+ );
426
+ });
427
+ next();
444
428
  },
445
- })
446
- ------
447
- express.js
448
- -----
429
+ });
430
+ === ./express.js ===
449
431
  export const info = {
450
432
  name: 'express',
451
433
  description: 'Sets up an Express server',
@@ -460,8 +442,8 @@ let app;
460
442
  const middlewareNames = new Set();
461
443
  const middlewares = [];
462
444
  export default mlm => ({
463
- '$global.express': { get: () => app },
464
- '$define.middleware': (conf, unit) => {
445
+ 'define.express': { get: () => app },
446
+ 'register.middleware': (conf, unit) => {
465
447
  for (const key in conf) {
466
448
  mlm.assert.not(key in middlewareNames, 'Duplicate middleware ' + key);
467
449
  middlewareNames.add(key);
@@ -504,14 +486,20 @@ export default mlm => ({
504
486
  app.use(mw);
505
487
  }
506
488
  }
507
- app.listen(mlm.config.port, mlm.config.host, () => {
489
+ app.listen(mlm.config.port, mlm.config.host, (error) => {
490
+ if (error) {
491
+ mlm.throw(error);
492
+ }
508
493
  mlm.log(`Listening on ${mlm.config.host}:${mlm.config.port}`);
509
494
  });
495
+ app.on('close', () => {
496
+ mlm.log('Server closed');
497
+ });
498
+ app.on('error', (error) => {
499
+ mlm.throw(error);
500
+ });
510
501
  },
511
- })
512
- ------
513
- noxt-dev.js
514
- -----
502
+ })=== ./noxt-dev.js ===
515
503
  export const info = {
516
504
  name: 'noxt-dev',
517
505
  description: 'Noxt Dev server',
@@ -524,10 +512,7 @@ export const info = {
524
512
  'fetch-cache-fs',
525
513
  'fetch',
526
514
  ],
527
- }
528
- ------
529
- fetch-cache-fs.js
530
- -----
515
+ }=== ./fetch-cache-fs.js ===
531
516
  export const info = {
532
517
  name: 'fetch-cache-fs',
533
518
  version: '1.0.0',
@@ -581,10 +566,7 @@ async function globalFetch(ctx, url, options = {}) {
581
566
  }
582
567
  if (!res.ok) throw new Error(res.statusText);
583
568
  return res;
584
- }
585
- ------
586
- noxt-plugin.js
587
- -----
569
+ }=== ./noxt-plugin.js ===
588
570
  export const info = {
589
571
  name: 'noxt-plugin',
590
572
  description: 'Noxt Plugin',
@@ -638,12 +620,12 @@ export default mlm => {
638
620
  }
639
621
  const globalServerContext = {};
640
622
  return ({
641
- '$define.pageContext': arrayCollector(pageContextHooks, 'page context handler'),
642
- '$define.serverContext': arrayCollector(serverContextHooks, 'server context handler'),
643
- '$define.componentExports': arrayCollector(componentExportsHooks, 'page export handler'),
644
- '$define.registerPage': collector(registerPageHooks, 'register page handler'),
645
- '$define.registerComponent': collector(registerComponentHooks, 'register component handler'),
646
- '$global.noxt_context': {
623
+ 'register.pageContext': arrayCollector(pageContextHooks, 'page context handler'),
624
+ 'register.serverContext': arrayCollector(serverContextHooks, 'server context handler'),
625
+ 'register.componentExports': arrayCollector(componentExportsHooks, 'page export handler'),
626
+ 'register.registerPage': collector(registerPageHooks, 'register page handler'),
627
+ 'register.registerComponent': collector(registerComponentHooks, 'register component handler'),
628
+ 'define.noxt_context': {
647
629
  get: () => globalServerContext,
648
630
  enumerable: true
649
631
  },
@@ -709,10 +691,7 @@ export default mlm => {
709
691
  }
710
692
  }
711
693
  })
712
- }
713
- ------
714
- noxt-router-dev.js
715
- -----
694
+ }=== ./noxt-router-dev.js ===
716
695
  export const info = {
717
696
  name: 'noxt-router-dev',
718
697
  description: 'Sets up a Noxt Router',
@@ -745,10 +724,7 @@ export default mlm => ({
745
724
  noxtRouter.use('/dev', devRouter)
746
725
  return noxtRouter
747
726
  }
748
- })
749
- ------
750
- hooks.js
751
- -----
727
+ })=== ./hooks.js ===
752
728
  export const info = {
753
729
  name: 'hooks',
754
730
  version: '1.0.0',
@@ -760,8 +736,8 @@ const hook_handlers = {}
760
736
 
761
737
  export default mlm => {
762
738
  return ({
763
- '$global.hooks': () => mlm.utils.readOnly(hooks, 'hooks'),
764
- '$define.hooks': mlm.utils.collector(hooks, {
739
+ 'define.hooks': () => mlm.utils.readOnly(hooks, 'hooks'),
740
+ 'register.hooks': mlm.utils.collector(hooks, {
765
741
  is: 'function',
766
742
  mode: 'object',
767
743
  map: (fn, key) => (...args) => {
@@ -770,15 +746,12 @@ export default mlm => {
770
746
  }
771
747
  }
772
748
  }),
773
- '$define.on': mlm.utils.collector(hook_handlers, {
749
+ 'register.on': mlm.utils.collector(hook_handlers, {
774
750
  is: 'function',
775
751
  mode: 'array'
776
752
  })
777
753
  })
778
- }
779
- ------
780
- static.js
781
- -----
754
+ }=== ./static.js ===
782
755
  export const info = {
783
756
  name: 'static',
784
757
  description: 'Static middleware',
@@ -799,27 +772,21 @@ export default mlm => ({
799
772
  const dir = mlm.config.static;
800
773
  return serve_static(resolve(dir));
801
774
  },
802
- })
803
- ------
804
- env.js
805
- -----
775
+ })=== ./env.js ===
806
776
  export const info = {
807
777
  name: 'env',
808
778
  version: '1.0.0',
809
779
  description: 'Environment',
810
780
  }
811
- /*
781
+
812
782
  export default mlm => ({
813
- '$global.DEV': () => process.env.NODE_ENV !== 'production',
814
- '$global.PROD': () => process.env.NODE_ENV === 'production',
783
+ 'define.DEV': () => process.env.NODE_ENV !== 'production',
784
+ 'define.PROD': () => process.env.NODE_ENV === 'production',
815
785
  'onBeforeLoad': () => {
816
- //process.env.NODE_ENV ||= 'development'
786
+ process.env.NODE_ENV ||= 'development'
817
787
  }
818
788
  })
819
- */
820
- ------
821
- noxt-router.js
822
- -----
789
+ === ./noxt-router.js ===
823
790
  export const info = {
824
791
  name: 'noxt-router',
825
792
  description: 'Sets up a Noxt Router',
@@ -845,4 +812,4 @@ export default mlm => ({
845
812
  })
846
813
  return noxtRouter
847
814
  },
848
- })
815
+ })
@@ -1,33 +0,0 @@
1
- export const info = {
2
- name: 'noxt-router-dev',
3
- description: 'Sets up a Noxt Router',
4
- requires: ['plugin'],
5
- provides: ['#noxt-router'],
6
- npm: {
7
- 'noxt-js-middleware': '^1.0.4'
8
- },
9
- }
10
-
11
- export default mlm => ({
12
- 'config.views': {
13
- normalize: p => [p].flat(),
14
- is: ['string'],
15
- default: ['views']
16
- },
17
- 'middleware.noxt': async () => {
18
- const { default: noxt } = await mlm.import('noxt-js-middleware');
19
- const noxtRouter = await noxt({
20
- context: mlm.noxt_context,
21
- views: mlm.config.views,
22
- hooks: mlm.services.noxt.noxt_hooks
23
- })
24
- const devRouter = await noxt({
25
- context: mlm.noxt_context,
26
- views: mlm.config.views,
27
- hooks: mlm.services.noxt.noxt_hooks,
28
- noxt: noxtRouter
29
- })
30
- noxtRouter.use('/dev', devRouter)
31
- return noxtRouter
32
- }
33
- })