waibu 2.14.0 → 2.15.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.js +70 -0
- package/package.json +1 -1
- package/wiki/CHANGES.md +5 -0
- package/extend/bajo/hook/waibu@on-close.js +0 -5
- package/extend/bajo/hook/waibu@on-ready.js +0 -5
- package/extend/bajo/hook/waibu@on-request.js +0 -37
- package/extend/bajo/hook/waibu@on-response.js +0 -48
- package/extend/bajo/hook/waibu@on-route.js +0 -8
- package/extend/bajo/hook/waibu@pre-parsing.js +0 -10
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export const methodColor = {
|
|
2
|
+
GET: 'blue',
|
|
3
|
+
POST: 'green',
|
|
4
|
+
UPDATE: 'yellow',
|
|
5
|
+
PATCH: 'yellow',
|
|
6
|
+
DELETE: 'red'
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async function hook () {
|
|
10
|
+
return [{
|
|
11
|
+
name: 'waibu:onClose',
|
|
12
|
+
handler: async function () {
|
|
13
|
+
this.log.info('serverIs%s', this.t('closedL'))
|
|
14
|
+
}
|
|
15
|
+
}, {
|
|
16
|
+
name: 'waibu:onReady',
|
|
17
|
+
handler: async function () {
|
|
18
|
+
this.log.info('serverIs%s', this.t('readyL'))
|
|
19
|
+
}
|
|
20
|
+
}, {
|
|
21
|
+
level: 5,
|
|
22
|
+
name: 'waibu:onRequest',
|
|
23
|
+
handler: async function onRequest (req, reply) {
|
|
24
|
+
const { importPkg } = this.app.bajo
|
|
25
|
+
const { get } = this.app.lib._
|
|
26
|
+
const chalk = await importPkg('bajo:chalk')
|
|
27
|
+
const { plain } = this.app.bajo.config.log
|
|
28
|
+
|
|
29
|
+
req.site = this.config.siteInfo
|
|
30
|
+
req.ns = get(reply.request, 'routeOptions.config.ns') ?? this.ns
|
|
31
|
+
const ns = get(reply.request, 'routeOptions.config.webApp') ?? this.ns
|
|
32
|
+
const arrow = plain ? '<' : chalk.bold('🡨')
|
|
33
|
+
const c = methodColor[req.method] ?? 'gray'
|
|
34
|
+
const method = plain ? req.method : chalk[c](req.method)
|
|
35
|
+
const url = plain ? (':' + req.url) : chalk.gray(':' + req.url)
|
|
36
|
+
const ip = plain ? this.getIp(req) : chalk.magenta(this.getIp(req))
|
|
37
|
+
let msg = this.app[ns].t('httpReq%s%s%s%s', arrow, method, url.replaceAll('%', '%%'), ip)
|
|
38
|
+
if (req.headers['content-length']) msg += this.app[ns].t('httpReqExt%s', req.headers['content-length'])
|
|
39
|
+
if (this.config.log.defer) {
|
|
40
|
+
this.reqLog = this.reqLog ?? {}
|
|
41
|
+
this.reqLog[req.id] = msg
|
|
42
|
+
} else if (!this.config.log.noReq) this.app[ns].log.info(msg)
|
|
43
|
+
if (Object.keys(this.config.paramsCharMap).length === 0) return
|
|
44
|
+
for (const key in req.params) {
|
|
45
|
+
let val = req.params[key]
|
|
46
|
+
if (typeof val !== 'string') continue
|
|
47
|
+
for (const char in this.config.paramsCharMap) {
|
|
48
|
+
val = val.replaceAll(char, this.config.paramsCharMap[char])
|
|
49
|
+
}
|
|
50
|
+
req.params[key] = val
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}, {
|
|
54
|
+
level: 5,
|
|
55
|
+
name: 'waibu:onRoute',
|
|
56
|
+
handler: async function (opts) {
|
|
57
|
+
this.routes.push(opts)
|
|
58
|
+
}
|
|
59
|
+
}, {
|
|
60
|
+
level: 9,
|
|
61
|
+
name: 'waibu:preParsing',
|
|
62
|
+
handler: async function (req, reply) {
|
|
63
|
+
const { importModule } = this.app.bajo
|
|
64
|
+
const attachIntl = await importModule('waibu:/lib/webapp-scope/attach-intl.js')
|
|
65
|
+
await attachIntl.call(this, this.config.intl.detectors, req, reply)
|
|
66
|
+
}
|
|
67
|
+
}]
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export default hook
|
package/package.json
CHANGED
package/wiki/CHANGES.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# Changes
|
|
2
2
|
|
|
3
|
+
## 2026-05-28
|
|
4
|
+
|
|
5
|
+
- [2.15.0] Change hooks to be written in one ```hook.js``` file
|
|
6
|
+
- [2.15.0] Change model schemas to be written in one ```model.js``` file
|
|
7
|
+
|
|
3
8
|
## 2026-05-24
|
|
4
9
|
|
|
5
10
|
- [2.14.0] Add auto detection of theme & iconset for all widgets incl the dynamic one
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { methodColor } from './waibu@on-response.js'
|
|
2
|
-
|
|
3
|
-
const onRequest = {
|
|
4
|
-
level: 5,
|
|
5
|
-
handler: async function onRequest (req, reply) {
|
|
6
|
-
const { importPkg } = this.app.bajo
|
|
7
|
-
const { get } = this.app.lib._
|
|
8
|
-
const chalk = await importPkg('bajo:chalk')
|
|
9
|
-
const { plain } = this.app.bajo.config.log
|
|
10
|
-
|
|
11
|
-
req.site = this.config.siteInfo
|
|
12
|
-
req.ns = get(reply.request, 'routeOptions.config.ns') ?? this.ns
|
|
13
|
-
const ns = get(reply.request, 'routeOptions.config.webApp') ?? this.ns
|
|
14
|
-
const arrow = plain ? '<' : chalk.bold('🡨')
|
|
15
|
-
const c = methodColor[req.method] ?? 'gray'
|
|
16
|
-
const method = plain ? req.method : chalk[c](req.method)
|
|
17
|
-
const url = plain ? (':' + req.url) : chalk.gray(':' + req.url)
|
|
18
|
-
const ip = plain ? this.getIp(req) : chalk.magenta(this.getIp(req))
|
|
19
|
-
let msg = this.app[ns].t('httpReq%s%s%s%s', arrow, method, url.replaceAll('%', '%%'), ip)
|
|
20
|
-
if (req.headers['content-length']) msg += this.app[ns].t('httpReqExt%s', req.headers['content-length'])
|
|
21
|
-
if (this.config.log.defer) {
|
|
22
|
-
this.reqLog = this.reqLog ?? {}
|
|
23
|
-
this.reqLog[req.id] = msg
|
|
24
|
-
} else if (!this.config.log.noReq) this.app[ns].log.info(msg)
|
|
25
|
-
if (Object.keys(this.config.paramsCharMap).length === 0) return
|
|
26
|
-
for (const key in req.params) {
|
|
27
|
-
let val = req.params[key]
|
|
28
|
-
if (typeof val !== 'string') continue
|
|
29
|
-
for (const char in this.config.paramsCharMap) {
|
|
30
|
-
val = val.replaceAll(char, this.config.paramsCharMap[char])
|
|
31
|
-
}
|
|
32
|
-
req.params[key] = val
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export default onRequest
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
export const methodColor = {
|
|
2
|
-
GET: 'blue',
|
|
3
|
-
POST: 'green',
|
|
4
|
-
UPDATE: 'yellow',
|
|
5
|
-
PATCH: 'yellow',
|
|
6
|
-
DELETE: 'red'
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const stateColor = {
|
|
10
|
-
2: 'green',
|
|
11
|
-
3: 'yellow',
|
|
12
|
-
4: 'red',
|
|
13
|
-
5: 'red'
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const onResponse = {
|
|
17
|
-
level: 5,
|
|
18
|
-
handler: async function onResponse (req, reply) {
|
|
19
|
-
const { importPkg } = this.app.bajo
|
|
20
|
-
const { get } = this.app.lib._
|
|
21
|
-
const { plain } = this.app.bajo.config.log
|
|
22
|
-
const chalk = await importPkg('bajo:chalk')
|
|
23
|
-
let level = 'info'
|
|
24
|
-
if (reply.statusCode >= 300 && reply.statusCode < 400) level = 'warn'
|
|
25
|
-
else if (reply.statusCode >= 400) level = 'error'
|
|
26
|
-
const ns = get(reply.request, 'routeOptions.config.webApp') ?? this.ns
|
|
27
|
-
const arrow = plain ? '>' : chalk.bold('🡪')
|
|
28
|
-
const mc = methodColor[req.method] ?? 'gray'
|
|
29
|
-
const method = plain ? req.method : chalk[mc](req.method)
|
|
30
|
-
const url = plain ? (':' + req.url) : chalk.gray(':' + req.url)
|
|
31
|
-
let state = plain ? reply.statusCode : chalk.gray(reply.statusCode)
|
|
32
|
-
const sc = stateColor[Math.floor(reply.statusCode / 100)]
|
|
33
|
-
if (!plain && sc) state = chalk[sc](reply.statusCode)
|
|
34
|
-
const elapsed = reply.elapsedTime ?? 0
|
|
35
|
-
let tc = 'red'
|
|
36
|
-
if (elapsed < 1000) tc = 'yellow'
|
|
37
|
-
if (elapsed < 500) tc = 'green'
|
|
38
|
-
const time = plain ? elapsed.toFixed(2) : chalk[tc](elapsed.toFixed(2))
|
|
39
|
-
if (this.config.log.defer) {
|
|
40
|
-
this.reqLog = this.reqLog ?? {}
|
|
41
|
-
if (this.reqLog[req.id] && !this.config.log.noReq) this.app[ns].log.info(this.reqLog[req.id])
|
|
42
|
-
delete this.reqLog[req.id]
|
|
43
|
-
}
|
|
44
|
-
if (!this.config.log.noReply) this.app[ns].log[level]('httpResp%s%s%s%s%s', arrow, method, url, state, time)
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export default onResponse
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
const waibuPreParsing = {
|
|
2
|
-
level: 9,
|
|
3
|
-
handler: async function (req, reply) {
|
|
4
|
-
const { importModule } = this.app.bajo
|
|
5
|
-
const attachIntl = await importModule('waibu:/lib/webapp-scope/attach-intl.js')
|
|
6
|
-
await attachIntl.call(this, this.config.intl.detectors, req, reply)
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export default waibuPreParsing
|