waibu 1.1.6 → 1.1.8
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/bajo/hook/on-request.js +1 -1
- package/bajo/hook/on-response.js +1 -1
- package/lib/app.js +1 -1
- package/lib/build-locals.js +7 -3
- package/lib/collect-route-path-handlers.js +1 -1
- package/lib/home.js +1 -1
- package/lib/log-routes.js +1 -1
- package/lib/webapp-scope/attach-intl.js +2 -2
- package/lib/webapp-scope/error-handler.js +1 -1
- package/lib/webapp-scope/handle-multipart-body.js +3 -3
- package/lib/webapp-scope/handle-rate-limit.js +1 -1
- package/lib/webapp-scope/is-route-disabled.js +1 -1
- package/lib/webapp-scope/merge-route-hooks.js +1 -1
- package/package.json +1 -1
- package/plugin/config-prod.json +3 -0
- package/plugin/factory.js +24 -11
package/bajo/hook/on-request.js
CHANGED
package/bajo/hook/on-response.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const onResponse = {
|
|
2
2
|
level: 5,
|
|
3
3
|
handler: async function onResponse (req, reply) {
|
|
4
|
-
const { get } = this.
|
|
4
|
+
const { get } = this.lib._
|
|
5
5
|
let method = 'info'
|
|
6
6
|
if (reply.statusCode >= 300 && reply.statusCode < 400) method = 'warn'
|
|
7
7
|
else if (reply.statusCode >= 400) method = 'error'
|
package/lib/app.js
CHANGED
|
@@ -2,7 +2,7 @@ import home from './home.js'
|
|
|
2
2
|
|
|
3
3
|
export async function collect (glob = 'boot.js', baseNs) {
|
|
4
4
|
const { eachPlugins, importModule } = this.app.bajo
|
|
5
|
-
const { orderBy, get } = this.
|
|
5
|
+
const { orderBy, get } = this.lib._
|
|
6
6
|
if (!baseNs) baseNs = this.name
|
|
7
7
|
const mods = []
|
|
8
8
|
|
package/lib/build-locals.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
function buildHomesMenu () {
|
|
2
|
-
const { get, find, orderBy } = this.
|
|
2
|
+
const { get, find, orderBy } = this.lib._
|
|
3
3
|
const routes = []
|
|
4
4
|
for (const ns of this.app.bajo.pluginNames) {
|
|
5
5
|
const href = get(this, `app.${ns}.config.waibuMpa.home`)
|
|
@@ -12,7 +12,7 @@ function buildHomesMenu () {
|
|
|
12
12
|
|
|
13
13
|
async function buildLocals ({ tpl, params = {}, opts = {} } = {}) {
|
|
14
14
|
const { runHook } = this.app.bajo
|
|
15
|
-
const { set, merge, pick, get, isEmpty, find } = this.
|
|
15
|
+
const { set, merge, pick, get, isEmpty, find } = this.lib._
|
|
16
16
|
const { req, reply } = opts
|
|
17
17
|
|
|
18
18
|
const appTitle = this.app.waibuMpa ? this.app.waibuMpa.getAppTitle(req.ns) : undefined
|
|
@@ -36,8 +36,12 @@ async function buildLocals ({ tpl, params = {}, opts = {} } = {}) {
|
|
|
36
36
|
if (params.error) {
|
|
37
37
|
if (params.error.statusCode) _meta.statusCode = params.error.statusCode
|
|
38
38
|
_meta.errorMessage = params.error.message
|
|
39
|
+
if (params.error.ns) {
|
|
40
|
+
params.page.ns = params.error.ns
|
|
41
|
+
params.page.appTitle = this.app.waibuMpa.getAppTitle(params.error.ns)
|
|
42
|
+
}
|
|
39
43
|
this.log.error('error%s', params.error.message)
|
|
40
|
-
if (this.app.bajo.config.env === 'dev') console.log(params.
|
|
44
|
+
if (this.app.bajo.config.env === 'dev') console.log(params.error)
|
|
41
45
|
}
|
|
42
46
|
if (reply && req.session && req.flash && !opts.partial) _meta.flash = reply.flash()
|
|
43
47
|
const merged = merge({}, params, { _meta })
|
package/lib/home.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
async function home () {
|
|
2
2
|
const { defaultsDeep } = this.app.bajo
|
|
3
|
-
const { isString, pick } = this.
|
|
3
|
+
const { isString, pick } = this.lib._
|
|
4
4
|
const config = this.getConfig()
|
|
5
5
|
if (config.home) {
|
|
6
6
|
if (isString(config.home)) config.home = { path: config.home }
|
package/lib/log-routes.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
function detect (detector = [], req, reply) {
|
|
2
|
-
const { get, map, trim, orderBy } = this.
|
|
2
|
+
const { get, map, trim, orderBy } = this.lib._
|
|
3
3
|
const supported = get(this, 'app.bajo.config.intl.supported', [this.app.bajo.config.lang])
|
|
4
4
|
const defLang = get(this, 'app.bajo.config.intl.fallback', this.app.bajo.config.lang)
|
|
5
5
|
let lang = null
|
|
@@ -49,7 +49,7 @@ function detect (detector = [], req, reply) {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
async function attachIntl (detector = [], req, reply) {
|
|
52
|
-
const { get } = this.
|
|
52
|
+
const { get } = this.lib._
|
|
53
53
|
detect.call(this, detector, req, reply)
|
|
54
54
|
const defNs = get(req, 'routeOptions.config.ns', this.name)
|
|
55
55
|
req.t = (text, ...args) => {
|
|
@@ -6,7 +6,7 @@ async function errorHandler (ctx, extHandler) {
|
|
|
6
6
|
reply.send(err.print)
|
|
7
7
|
return
|
|
8
8
|
}
|
|
9
|
-
if (me.app.bajo.config.env !== 'prod' &&
|
|
9
|
+
if (me.app.bajo.config.env !== 'prod' &&
|
|
10
10
|
!['_notfound', '_redirect'].includes(err.message.toLowerCase())) console.error(err)
|
|
11
11
|
if (extHandler) return await extHandler.call(me, err, req, reply, ctx)
|
|
12
12
|
if (err.message === '_notFound' || err.statusCode === 404) {
|
|
@@ -6,7 +6,7 @@ const pump = promisify(pipeline)
|
|
|
6
6
|
|
|
7
7
|
async function onFileHandler () {
|
|
8
8
|
const { getPluginDataDir } = this.app.bajo
|
|
9
|
-
const { fs } = this.
|
|
9
|
+
const { fs } = this.lib
|
|
10
10
|
const dir = `${getPluginDataDir('waibu')}/upload`
|
|
11
11
|
return async function (part) {
|
|
12
12
|
// 'this' is the fastify context here
|
|
@@ -18,8 +18,8 @@ async function onFileHandler () {
|
|
|
18
18
|
|
|
19
19
|
async function handleMultipartBody (ctx, options = {}) {
|
|
20
20
|
const { defaultsDeep, importPkg, isSet } = this.app.bajo
|
|
21
|
-
const { isArray, map, trim, isPlainObject, isEmpty } = this.
|
|
22
|
-
const parseVar = await importPkg('dotenv-parse-variables')
|
|
21
|
+
const { isArray, map, trim, isPlainObject, isEmpty } = this.lib._
|
|
22
|
+
const parseVar = await importPkg('bajo:dotenv-parse-variables')
|
|
23
23
|
if (options === false) return this.log.warn('middlewareDisabled%s', 'multipart')
|
|
24
24
|
const opts = defaultsDeep(options, this.app.waibu.config.multipart)
|
|
25
25
|
const onFile = await onFileHandler.call(this)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import rateLimit from '@fastify/rate-limit'
|
|
2
2
|
|
|
3
3
|
async function handleRateLimit (ctx, options = {}) {
|
|
4
|
-
const { cloneDeep } = this.
|
|
4
|
+
const { cloneDeep } = this.lib._
|
|
5
5
|
const { defaultsDeep } = this.app.bajo
|
|
6
6
|
if (options === false) return this.log.warn('middlewareDisabled%s', 'rateLimit')
|
|
7
7
|
const opts = defaultsDeep(options, this.app.waibu.config.rateLimit)
|
|
@@ -6,7 +6,7 @@ function santizeMethods (methods = '*') {
|
|
|
6
6
|
|
|
7
7
|
async function isRouteDisabled (url, method, matchers = []) {
|
|
8
8
|
const { outmatch } = this.app.bajo
|
|
9
|
-
const { intersection, cloneDeep } = this.
|
|
9
|
+
const { intersection, cloneDeep } = this.lib._
|
|
10
10
|
const items = []
|
|
11
11
|
for (const m of cloneDeep(matchers)) {
|
|
12
12
|
m.path = this.app.waibu.routePath(m.path)
|
package/package.json
CHANGED
package/plugin/factory.js
CHANGED
|
@@ -44,7 +44,7 @@ async function factory (pkgName) {
|
|
|
44
44
|
lang: 'lang'
|
|
45
45
|
},
|
|
46
46
|
paramsCharMap: {},
|
|
47
|
-
logRoutes:
|
|
47
|
+
logRoutes: true,
|
|
48
48
|
siteInfo: {
|
|
49
49
|
title: 'My Website',
|
|
50
50
|
orgName: 'My Organization'
|
|
@@ -129,8 +129,21 @@ async function factory (pkgName) {
|
|
|
129
129
|
this.instance.close()
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
+
findRoute (route) {
|
|
133
|
+
const { outmatch } = this.lib
|
|
134
|
+
const { find } = this.lib._
|
|
135
|
+
const { breakNsPath } = this.app.bajo
|
|
136
|
+
const { ns, subNs = '', path } = breakNsPath(route)
|
|
137
|
+
return find(this.routes, r => {
|
|
138
|
+
r.config = r.config ?? {}
|
|
139
|
+
const match = outmatch(r.config.pathSrc ?? r.path, { separator: false })
|
|
140
|
+
if (!match(path)) return false
|
|
141
|
+
return ns === r.config.ns && r.config.subNs === subNs
|
|
142
|
+
})
|
|
143
|
+
}
|
|
144
|
+
|
|
132
145
|
escape = (text) => {
|
|
133
|
-
const { forOwn } = this.
|
|
146
|
+
const { forOwn } = this.lib._
|
|
134
147
|
forOwn(this.escapeChars, (v, k) => {
|
|
135
148
|
text = text.replaceAll(k, v)
|
|
136
149
|
})
|
|
@@ -160,7 +173,7 @@ async function factory (pkgName) {
|
|
|
160
173
|
}
|
|
161
174
|
|
|
162
175
|
getPluginByPrefix = (prefix) => {
|
|
163
|
-
const { get } = this.
|
|
176
|
+
const { get } = this.lib._
|
|
164
177
|
let plugin
|
|
165
178
|
for (const p of this.app.bajo.pluginNames) {
|
|
166
179
|
if (get(this, `app.${p}.config.waibu.prefix`) === prefix) {
|
|
@@ -172,7 +185,7 @@ async function factory (pkgName) {
|
|
|
172
185
|
}
|
|
173
186
|
|
|
174
187
|
getPluginPrefix = (base, webApp = 'waibuMpa') => {
|
|
175
|
-
const { get, trim } = this.
|
|
188
|
+
const { get, trim } = this.lib._
|
|
176
189
|
let prefix = get(this, `app.${base}.config.waibu.prefix`, this.app[base].alias)
|
|
177
190
|
if (base === 'main') {
|
|
178
191
|
const cfg = this.app[webApp].config
|
|
@@ -183,7 +196,7 @@ async function factory (pkgName) {
|
|
|
183
196
|
}
|
|
184
197
|
|
|
185
198
|
getRoutes = (grouped, lite) => {
|
|
186
|
-
const { groupBy, orderBy, mapValues, map, pick } = this.
|
|
199
|
+
const { groupBy, orderBy, mapValues, map, pick } = this.lib._
|
|
187
200
|
const all = this.routes
|
|
188
201
|
let routes
|
|
189
202
|
if (grouped) {
|
|
@@ -196,7 +209,7 @@ async function factory (pkgName) {
|
|
|
196
209
|
|
|
197
210
|
getUploadedFiles = async (reqId, fileUrl, returnDir) => {
|
|
198
211
|
const { getPluginDataDir, resolvePath } = this.app.bajo
|
|
199
|
-
const { fastGlob } = this.
|
|
212
|
+
const { fastGlob } = this.lib
|
|
200
213
|
const dir = `${getPluginDataDir(this.name)}/upload/${reqId}`
|
|
201
214
|
const result = await fastGlob(`${dir}/*`)
|
|
202
215
|
if (!fileUrl) return returnDir ? { dir, files: result } : result
|
|
@@ -205,7 +218,7 @@ async function factory (pkgName) {
|
|
|
205
218
|
}
|
|
206
219
|
|
|
207
220
|
isIntlPath = (ns) => {
|
|
208
|
-
const { get } = this.
|
|
221
|
+
const { get } = this.lib._
|
|
209
222
|
return get(this.app[ns], 'config.intl.detectors', []).includes('path')
|
|
210
223
|
}
|
|
211
224
|
|
|
@@ -223,7 +236,7 @@ async function factory (pkgName) {
|
|
|
223
236
|
}
|
|
224
237
|
|
|
225
238
|
routeDir = (ns, base) => {
|
|
226
|
-
const { get } = this.
|
|
239
|
+
const { get } = this.lib._
|
|
227
240
|
if (!base) base = ns
|
|
228
241
|
const cfg = this.app[base].config
|
|
229
242
|
const prefix = get(cfg, 'waibu.prefix', this.app[base].alias)
|
|
@@ -237,7 +250,7 @@ async function factory (pkgName) {
|
|
|
237
250
|
|
|
238
251
|
routePath = (name = '', options = {}) => {
|
|
239
252
|
const { defaultsDeep, getPlugin } = this.app.bajo
|
|
240
|
-
const { isEmpty, get, trimEnd, trimStart } = this.
|
|
253
|
+
const { isEmpty, get, trimEnd, trimStart } = this.lib._
|
|
241
254
|
const { breakNsPath } = this.app.bajo
|
|
242
255
|
const { query = {}, base = 'waibu', params = {}, guessHost } = options
|
|
243
256
|
|
|
@@ -268,7 +281,7 @@ async function factory (pkgName) {
|
|
|
268
281
|
|
|
269
282
|
sendMail = async (tpl, { to, cc, bcc, from, subject, data = {}, conn, options = {} }) => {
|
|
270
283
|
if (!this.app.masohiMail) return
|
|
271
|
-
const { get, isString } = this.
|
|
284
|
+
const { get, isString } = this.lib._
|
|
272
285
|
const { generateId } = this.app.bajo
|
|
273
286
|
const { render } = this.app.bajoTemplate
|
|
274
287
|
if (isString(tpl)) tpl = [tpl]
|
|
@@ -294,7 +307,7 @@ async function factory (pkgName) {
|
|
|
294
307
|
}
|
|
295
308
|
|
|
296
309
|
unescape = (text) => {
|
|
297
|
-
const { forOwn, invert } = this.
|
|
310
|
+
const { forOwn, invert } = this.lib._
|
|
298
311
|
const mapping = invert(this.escapeChars)
|
|
299
312
|
forOwn(mapping, (v, k) => {
|
|
300
313
|
text = text.replaceAll(k, v)
|