waibu 1.2.9 → 1.2.10

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.
@@ -1,14 +1,27 @@
1
+ import { methodColor } from './on-response.js'
2
+
1
3
  const onRequest = {
2
4
  level: 5,
3
5
  handler: async function onRequest (req, reply) {
6
+ const { importPkg } = this.app.bajo
4
7
  const { get } = this.lib._
8
+ const chalk = await importPkg('bajo:chalk')
9
+ const { plain } = this.app.bajo.config.log
5
10
 
6
11
  req.site = this.config.siteInfo
7
12
  req.ns = get(reply.request, 'routeOptions.config.ns') ?? this.name
8
13
  const ns = get(reply.request, 'routeOptions.config.webApp') ?? this.name
9
- let msg = '< %s:%s from IP %s'
10
- if (req.headers['content-length']) msg += ', content length: %s'
11
- this.app[ns].log.info(msg, req.method, req.url, this.getIp(req), req.headers['content-length'])
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].print.write('httpReq%s%s%s%s', arrow, method, url, ip)
20
+ if (req.headers['content-length']) msg += this.app[ns].print.write('httpReqExt%s', req.headers['content-length'])
21
+ if (this.config.deferLog) {
22
+ this.reqLog = this.reqLog ?? {}
23
+ this.reqLog[req.id] = msg
24
+ } else this.app[ns].log.info(msg)
12
25
  if (Object.keys(this.config.paramsCharMap).length === 0) return
13
26
  for (const key in req.params) {
14
27
  let val = req.params[key]
@@ -1,13 +1,47 @@
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
+
1
16
  const onResponse = {
2
17
  level: 5,
3
18
  handler: async function onResponse (req, reply) {
19
+ const { importPkg } = this.app.bajo
4
20
  const { get } = this.lib._
5
- let method = 'info'
6
- if (reply.statusCode >= 300 && reply.statusCode < 400) method = 'warn'
7
- else if (reply.statusCode >= 400) method = 'error'
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'
8
26
  const ns = get(reply.request, 'routeOptions.config.webApp') ?? this.name
9
- this.app[ns].log[method]('> %s:%s with a %d-status took %dms', req.method, req.url, reply.statusCode,
10
- (reply.elapsedTime ?? 0).toFixed(3))
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.deferLog) {
40
+ this.reqLog = this.reqLog ?? {}
41
+ if (this.reqLog[req.id]) this.app[ns].log.info(this.reqLog[req.id])
42
+ delete this.reqLog[req.id]
43
+ }
44
+ this.app[ns].log[level]('httpResp%s%s%s%s%s', arrow, method, url, state, time)
11
45
  }
12
46
  }
13
47
 
@@ -11,5 +11,8 @@
11
11
  "rerouted%s%s": "Rerouted %s -> %s",
12
12
  "bootSubApp%s": "Boot sub app: %s",
13
13
  "routeNotFound%s%s": "Route '%s (%s)' not found",
14
- "middlewareDisabled%s": "Middleware '%s' is disabled"
14
+ "middlewareDisabled%s": "Middleware '%s' is disabled",
15
+ "httpResp%s%s%s%s%s": "%s %s%s with a %s-status took %sms",
16
+ "httpReq%s%s%s%s": "%s %s%s from IP %s",
17
+ "httpReqExt%s": ", content length: %s"
15
18
  }
package/bajo/intl/id.json CHANGED
@@ -11,5 +11,8 @@
11
11
  "rerouted%s%s": "Jalur dipindahkan %s -> %s",
12
12
  "bootSubApp%s": "Boot sub app: %s",
13
13
  "routeNotFound%s%s": "Jalur '%s (%s)' tidak ditemukan",
14
- "middlewareDisabled%s": "Middleware '%s' dimatikan"
14
+ "middlewareDisabled%s": "Middleware '%s' dimatikan",
15
+ "httpResp%s%s%s%s%s": "%s %s:%s dengan status %s selama %sms",
16
+ "httpReq%s%s%s%s": "%s %s:%s dari IP %s",
17
+ "httpReqExt%s": ", panjang konten: %s"
15
18
  }
package/index.js CHANGED
@@ -19,7 +19,7 @@ async function factory (pkgName) {
19
19
  constructor () {
20
20
  super(pkgName, me.app)
21
21
  this.alias = 'w'
22
- this.dependencies = ['bajo-logger', 'bajo-extra']
22
+ this.dependencies = ['bajo-extra']
23
23
  this.config = {
24
24
  server: {
25
25
  host: '127.0.0.1',
@@ -30,6 +30,7 @@ async function factory (pkgName) {
30
30
  bodyLimit: 10485760,
31
31
  pluginTimeout: 30000
32
32
  },
33
+ deferLog: false,
33
34
  prefixVirtual: '~',
34
35
  qsKey: {
35
36
  bbox: 'bbox',
@@ -104,10 +105,12 @@ async function factory (pkgName) {
104
105
  start = async () => {
105
106
  const { generateId, runHook } = this.app.bajo
106
107
  const cfg = this.getConfig()
107
- cfg.factory.loggerInstance = this.app.bajoLogger.instance.child(
108
- {},
109
- { msgPrefix: '[waibu] ' }
110
- )
108
+ if (this.app.bajoLogger) {
109
+ cfg.factory.loggerInstance = this.app.bajoLogger.instance.child(
110
+ {},
111
+ { msgPrefix: '[waibu] ' }
112
+ )
113
+ }
111
114
  cfg.factory.genReqId = req => generateId()
112
115
  cfg.factory.disableRequestLogging = true
113
116
  cfg.factory.querystringParser = str => this.qs.parse(str)
@@ -180,9 +183,10 @@ async function factory (pkgName) {
180
183
  }
181
184
 
182
185
  getIp = (req) => {
186
+ const { isEmpty } = this.lib._
183
187
  let fwd = req.headers['x-forwarded-for'] ?? ''
184
188
  if (!Array.isArray(fwd)) fwd = fwd.split(',').map(ip => ip.trim())
185
- return fwd[0] ?? req.ip
189
+ return isEmpty(fwd[0]) ? req.ip : fwd[0]
186
190
  }
187
191
 
188
192
  getOrigin = (req) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "waibu",
3
- "version": "1.2.9",
3
+ "version": "1.2.10",
4
4
  "description": "Web Framework for Bajo",
5
5
  "main": "index.js",
6
6
  "scripts": {