waibu 2.11.4 → 2.13.0
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/extend/bajo/hook/waibu@on-request.js +2 -2
- package/extend/bajo/hook/waibu@on-response.js +3 -3
- package/index.js +15 -8
- package/lib/build-locals.js +7 -6
- package/lib/decorate.js +1 -0
- package/lib/handle-error.js +1 -1
- package/package.json +1 -1
- package/wiki/CHANGES.md +13 -0
|
@@ -18,10 +18,10 @@ const onRequest = {
|
|
|
18
18
|
const ip = plain ? this.getIp(req) : chalk.magenta(this.getIp(req))
|
|
19
19
|
let msg = this.app[ns].t('httpReq%s%s%s%s', arrow, method, url.replaceAll('%', '%%'), ip)
|
|
20
20
|
if (req.headers['content-length']) msg += this.app[ns].t('httpReqExt%s', req.headers['content-length'])
|
|
21
|
-
if (this.config.
|
|
21
|
+
if (this.config.log.defer) {
|
|
22
22
|
this.reqLog = this.reqLog ?? {}
|
|
23
23
|
this.reqLog[req.id] = msg
|
|
24
|
-
} else this.app[ns].log.info(msg)
|
|
24
|
+
} else if (!this.config.log.noReq) this.app[ns].log.info(msg)
|
|
25
25
|
if (Object.keys(this.config.paramsCharMap).length === 0) return
|
|
26
26
|
for (const key in req.params) {
|
|
27
27
|
let val = req.params[key]
|
|
@@ -36,12 +36,12 @@ const onResponse = {
|
|
|
36
36
|
if (elapsed < 1000) tc = 'yellow'
|
|
37
37
|
if (elapsed < 500) tc = 'green'
|
|
38
38
|
const time = plain ? elapsed.toFixed(2) : chalk[tc](elapsed.toFixed(2))
|
|
39
|
-
if (this.config.
|
|
39
|
+
if (this.config.log.defer) {
|
|
40
40
|
this.reqLog = this.reqLog ?? {}
|
|
41
|
-
if (this.reqLog[req.id]) this.app[ns].log.info(this.reqLog[req.id])
|
|
41
|
+
if (this.reqLog[req.id] && !this.config.log.noReq) this.app[ns].log.info(this.reqLog[req.id])
|
|
42
42
|
delete this.reqLog[req.id]
|
|
43
43
|
}
|
|
44
|
-
this.app[ns].log[level]('httpResp%s%s%s%s%s', arrow, method, url, state, time)
|
|
44
|
+
if (!this.config.log.noReply) this.app[ns].log[level]('httpResp%s%s%s%s%s', arrow, method, url, state, time)
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
|
package/index.js
CHANGED
|
@@ -91,7 +91,11 @@ async function factory (pkgName) {
|
|
|
91
91
|
intl: {
|
|
92
92
|
detectors: ['qs']
|
|
93
93
|
},
|
|
94
|
-
|
|
94
|
+
log: {
|
|
95
|
+
noReq: false,
|
|
96
|
+
noReply: false,
|
|
97
|
+
defer: false
|
|
98
|
+
},
|
|
95
99
|
prefixVirtual: '~',
|
|
96
100
|
qsKey: {
|
|
97
101
|
bbox: 'bbox',
|
|
@@ -215,9 +219,9 @@ async function factory (pkgName) {
|
|
|
215
219
|
* @param {string} name - ns based route name
|
|
216
220
|
* @returns {Object} Route object
|
|
217
221
|
*/
|
|
218
|
-
findRoute = (name) => {
|
|
222
|
+
findRoute = (name, method = 'GET') => {
|
|
219
223
|
const { outmatch } = this.app.lib
|
|
220
|
-
const { find } = this.app.lib._
|
|
224
|
+
const { find, isString } = this.app.lib._
|
|
221
225
|
const { breakNsPath } = this.app.bajo
|
|
222
226
|
let { ns, subNs = '', path } = breakNsPath(name)
|
|
223
227
|
const params = path.split('|')
|
|
@@ -227,7 +231,8 @@ async function factory (pkgName) {
|
|
|
227
231
|
r.config = r.config ?? {}
|
|
228
232
|
const match = outmatch(r.config.pathSrc ?? r.path, { separator: false })
|
|
229
233
|
if (!match(path)) return false
|
|
230
|
-
|
|
234
|
+
const methods = isString(r.method) ? [r.method] : r.method
|
|
235
|
+
return ns === r.config.ns && r.config.subNs === subNs && methods.includes(method)
|
|
231
236
|
})
|
|
232
237
|
}
|
|
233
238
|
|
|
@@ -449,12 +454,13 @@ async function factory (pkgName) {
|
|
|
449
454
|
/**
|
|
450
455
|
* Get route path by route's name:
|
|
451
456
|
* - If it is a ```mailto:``` or ```tel:``` url, it returns as is
|
|
457
|
+
* - If it starts with ```:/```, name will be prefixed with its ```ns``` automatically
|
|
452
458
|
* - If it is a ns based name, it will be parsed first
|
|
453
459
|
*
|
|
454
460
|
* @method
|
|
455
461
|
* @param {string} name
|
|
456
462
|
* @param {Object} [options={}] - Options object
|
|
457
|
-
* @param {string} [options.
|
|
463
|
+
* @param {string} [options.ns=waibu] - Base namespace
|
|
458
464
|
* @param {boolean} [options.guessHost] - If true, guest host if host is not set
|
|
459
465
|
* @param {Object} [options.query={}] - Query string's object. If provided, it will be added to returned value
|
|
460
466
|
* @param {Object} [options.params={}] - Parameter object. If provided, it will be merged to returned value
|
|
@@ -465,12 +471,13 @@ async function factory (pkgName) {
|
|
|
465
471
|
const { defaultsDeep } = this.app.lib.aneka
|
|
466
472
|
const { isEmpty, get, trimEnd, trimStart } = this.app.lib._
|
|
467
473
|
const { breakNsPath } = this.app.bajo
|
|
468
|
-
const { query = {},
|
|
474
|
+
const { query = {}, ns = this.ns, params = {}, guessHost, defaults = {}, uriEncoded } = options
|
|
469
475
|
|
|
470
|
-
const plugin = getPlugin(
|
|
476
|
+
const plugin = getPlugin(ns)
|
|
471
477
|
const cfg = plugin.config ?? {}
|
|
472
478
|
let info = {}
|
|
473
479
|
if (name.startsWith('mailto:') || name.startsWith('tel:')) return name
|
|
480
|
+
if (name.slice(0, 2) === ':/') name = ns + name
|
|
474
481
|
if (['%', '.', '/', '?', '#'].includes(name[0]) || name.slice(1, 2) === ':') info.path = name
|
|
475
482
|
else if (['~'].includes(name[0])) info.path = name.slice(1)
|
|
476
483
|
else {
|
|
@@ -491,7 +498,7 @@ async function factory (pkgName) {
|
|
|
491
498
|
let url = info.path
|
|
492
499
|
const langDetector = get(cfg, 'intl.detectors', [])
|
|
493
500
|
if (info.ns) url = trimEnd(langDetector.includes('path') ? `/${params.lang ?? ''}${this.routeDir(info.ns)}${info.path}` : `${this.routeDir(info.ns)}${info.path}`, '/')
|
|
494
|
-
if (
|
|
501
|
+
if (uriEncoded) url = url.split('/').map(u => encodeURI(u)).join('/')
|
|
495
502
|
info.qs = defaultsDeep({}, query, info.qs)
|
|
496
503
|
if (!isEmpty(info.qs)) url += '?' + this.qs.stringify(info.qs)
|
|
497
504
|
if (!url.startsWith('http') && guessHost) url = `http://${this.config.server.host}:${this.config.server.port}/${trimStart(url, '/')}`
|
package/lib/build-locals.js
CHANGED
|
@@ -73,7 +73,6 @@ async function buildLocals ({ tpl, params = {}, opts = {} } = {}) {
|
|
|
73
73
|
_meta.template = tpl
|
|
74
74
|
_meta.hostHeader = req.headers.host
|
|
75
75
|
_meta.statusCode = 200
|
|
76
|
-
_meta.isAdmin = _meta.user && find(_meta.user.teams, { alias: 'administrator' })
|
|
77
76
|
const pulled = []
|
|
78
77
|
for (const k in errs) {
|
|
79
78
|
if (Date.now() - errs[k] > 5000) pulled.push(k)
|
|
@@ -89,12 +88,14 @@ async function buildLocals ({ tpl, params = {}, opts = {} } = {}) {
|
|
|
89
88
|
}
|
|
90
89
|
errs[req.id] = Date.now()
|
|
91
90
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
91
|
+
_meta.flash = reply && req.session && req.flash && !opts.partial && !opts.noFlash ? reply.flash() : {}
|
|
92
|
+
if (!opts.partial) {
|
|
93
|
+
set(params, 'menu.homes', await buildHomesMenu.call(this, req))
|
|
94
|
+
set(params, 'menu.pages', await buildPagesMenu.call(this, req))
|
|
95
|
+
}
|
|
95
96
|
const merged = merge({}, params, { _meta })
|
|
96
|
-
await runHook(`${this.ns}:afterBuildLocals`, merged, req)
|
|
97
|
-
if (!isEmpty(routeOpts.ns)) await runHook(`${this.ns}.${routeOpts.ns}:afterBuildLocals`, merged, req)
|
|
97
|
+
await runHook(`${this.ns}:afterBuildLocals`, merged, req, opts)
|
|
98
|
+
if (!isEmpty(routeOpts.ns)) await runHook(`${this.ns}.${routeOpts.ns}:afterBuildLocals`, merged, req, opts)
|
|
98
99
|
return merged
|
|
99
100
|
}
|
|
100
101
|
|
package/lib/decorate.js
CHANGED
|
@@ -4,6 +4,7 @@ async function decorate () {
|
|
|
4
4
|
const me = this
|
|
5
5
|
this.instance.decorateRequest('lang', null)
|
|
6
6
|
this.instance.decorateRequest('t', () => {})
|
|
7
|
+
this.instance.decorateRequest('te', () => {})
|
|
7
8
|
this.instance.decorateRequest('format', () => {})
|
|
8
9
|
this.instance.decorateRequest('langDetector', null)
|
|
9
10
|
this.instance.decorateRequest('site', null)
|
package/lib/handle-error.js
CHANGED
|
@@ -2,9 +2,9 @@ import { redirect } from './handle-redirect.js'
|
|
|
2
2
|
import { notFound, writeHtml, interceptor } from './handle-not-found.js'
|
|
3
3
|
|
|
4
4
|
async function error (err, req, reply) {
|
|
5
|
-
this.log.error(err)
|
|
6
5
|
const resp = await interceptor.call(this, 'errorHandler', err, req, reply)
|
|
7
6
|
if (resp) return resp
|
|
7
|
+
this.log.error(err)
|
|
8
8
|
const payload = {
|
|
9
9
|
text: this.app.log.getErrorMessage(err),
|
|
10
10
|
title: req.t('internalServerError')
|
package/package.json
CHANGED
package/wiki/CHANGES.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# Changes
|
|
2
2
|
|
|
3
|
+
## 2026-05-22
|
|
4
|
+
|
|
5
|
+
- [2.13.0] Add ```config.log.noReq``` & ```config.log.noReply```
|
|
6
|
+
- [2.13.0] Change ```config.deferLog``` to ```config.log.defer```
|
|
7
|
+
- [2.13.0] Bug fix in ```build-Locals.js```
|
|
8
|
+
- [2.13.0] Add ```decorator.te```
|
|
9
|
+
- [2.13.0] Bug fix in ```handle-error.js```
|
|
10
|
+
|
|
11
|
+
## 2026-05-16
|
|
12
|
+
|
|
13
|
+
- [2.12.0] Change in ```routeDir()```
|
|
14
|
+
- [2.12.0] Change in ```routePath()```
|
|
15
|
+
|
|
3
16
|
## 2026-05-11
|
|
4
17
|
|
|
5
18
|
- [2.11.4] Bug fix in ```handle-not-found.js```
|