q-koa 13.8.48 → 13.8.49

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.
Files changed (2) hide show
  1. package/core/app.js +51 -55
  2. package/package.json +1 -1
package/core/app.js CHANGED
@@ -101,6 +101,26 @@ const connectDatabase = (database) => (config) => {
101
101
  const defaultConfig = require('./config')
102
102
 
103
103
  const is_dev = process.env.NODE_ENV !== 'production'
104
+
105
+ // JWT 兜底配置(应用未配置 jwt 时使用),提为模块级常量避免每个请求重建对象
106
+ const DEFAULT_JWT = {
107
+ secret: 'token',
108
+ tokenKey: 'token',
109
+ expiresIn: '200000000',
110
+ _whiteList: [],
111
+ whiteList: [
112
+ 'find',
113
+ 'administrator/login',
114
+ 'administrator/checkLogin',
115
+ 'user/checkLogin',
116
+ 'user/login',
117
+ 'user/register',
118
+ 'common',
119
+ 'good/search',
120
+ 'category/tree',
121
+ 'system',
122
+ ],
123
+ }
104
124
  class APP {
105
125
  constructor(_config) {
106
126
  const config = _.defaultsDeep(_config, defaultConfig)
@@ -181,18 +201,13 @@ class APP {
181
201
  this.app.use(async (ctx, next) => {
182
202
  const { url, host, ip, method } = ctx.request
183
203
 
184
- if (
185
- ctx.request.header['user-agent'] &&
186
- ctx.request.header['user-agent'].includes('Tencent Security')
187
- ) {
204
+ const userAgent = ctx.request.header['user-agent'] || ''
205
+ if (userAgent.includes('Tencent Security')) {
188
206
  return ctx.ERROR('error')
189
207
  }
190
208
 
191
- if (
192
- ctx.request.header['user-agent'] &&
193
- ctx.request.header['user-agent'].includes('Apache-HttpClient')
194
- ) {
195
- throw new Error(`error`)
209
+ if (userAgent.includes('Apache-HttpClient')) {
210
+ throw new Error('error')
196
211
  }
197
212
 
198
213
  if (this.config.blackList.includes(ip)) {
@@ -208,24 +223,7 @@ class APP {
208
223
  const { secret, whiteList, _whiteList, tokenKey, expiresIn } =
209
224
  ctx.app[appName] && ctx.app[appName].appConfig
210
225
  ? ctx.app[appName].appConfig.jwt
211
- : {
212
- secret: 'token',
213
- tokenKey: 'token',
214
- expiresIn: '200000000',
215
- _whiteList: [],
216
- whiteList: [
217
- 'find',
218
- 'administrator/login',
219
- 'administrator/checkLogin',
220
- 'user/checkLogin',
221
- 'user/login',
222
- 'user/register',
223
- 'common',
224
- 'good/search',
225
- 'category/tree',
226
- 'system',
227
- ],
228
- }
226
+ : DEFAULT_JWT
229
227
 
230
228
  /**
231
229
  * 挂在jwt解密方法
@@ -296,7 +294,12 @@ class APP {
296
294
 
297
295
  this.app.on('error', async (err, ctx) => {
298
296
  // AggregateError(Node 17+ DNS 双栈解析等场景)取第一个子错误作为实际错误
299
- if (err && err.name === 'AggregateError' && Array.isArray(err.errors) && err.errors.length > 0) {
297
+ if (
298
+ err &&
299
+ err.name === 'AggregateError' &&
300
+ Array.isArray(err.errors) &&
301
+ err.errors.length > 0
302
+ ) {
300
303
  err = err.errors[0]
301
304
  }
302
305
  const { url } = ctx.request
@@ -333,9 +336,7 @@ class APP {
333
336
  ? this.config.includes[0]
334
337
  : this.config.includes[0].app
335
338
  if (!this.app[appName]) {
336
- throw new Error(
337
- `没有生成应用${this.config.includes[0]},请检查config includes`
338
- )
339
+ throw new Error(`没有生成应用${appName},请检查config includes`)
339
340
  }
340
341
  this.server = this.app.listen(this.port, () => {
341
342
  console.log(
@@ -444,9 +445,7 @@ class APP {
444
445
  dirList = dirList.filter((item) => includeList.some((i) => i.app === item))
445
446
 
446
447
  let dbFlag = false
447
- for (let i = 0; i < dirList.length; i++) {
448
- const appName = dirList[i]
449
-
448
+ for (const appName of dirList) {
450
449
  console.log(
451
450
  chalk.green(`==================== 启动 ${appName} ====================`)
452
451
  )
@@ -587,16 +586,11 @@ class APP {
587
586
  } catch (e) {
588
587
  push({
589
588
  appName,
590
- message: `${appName}启动失败:${e.message}`,
589
+ message: `${appName}数据库连接失败`,
591
590
  })
592
- console.log(chalk.red(`${appName}启动失败,跳过该应用并继续启动后续应用`))
591
+ console.log(`${appName}数据库连接失败`)
593
592
  console.log(e)
594
- // 清理可能已部分初始化的 app 对象,避免残留不完整状态
595
- if (this.app[appName]) {
596
- delete this.app[appName]
597
- }
598
- // 不抛出错误,跳过该应用的路由注册,继续启动后续应用
599
- continue
593
+ throw new Error(e.message)
600
594
  }
601
595
  if (
602
596
  this.app[appName] &&
@@ -676,8 +670,7 @@ class APP {
676
670
  (item) => !excludesModel.includes(item)
677
671
  )
678
672
 
679
- for (let i = 0; i < pluginDirResult.length; i++) {
680
- const folder = pluginDirResult[i]
673
+ for (const folder of pluginDirResult) {
681
674
  const isFolder = fs
682
675
  .lstatSync(path.resolve(pluginDir, folder))
683
676
  .isDirectory()
@@ -780,8 +773,7 @@ class APP {
780
773
  hookList = [...hookList, 'beforeBulkDestroy']
781
774
  }
782
775
  let reduceHook = {}
783
- for (let i = 0; i < hookList.length; i++) {
784
- const hook = hookList[i]
776
+ for (const hook of hookList) {
785
777
  reduceHook = {
786
778
  ...reduceHook,
787
779
  [hook]: (target, t) => {
@@ -984,8 +976,7 @@ class APP {
984
976
  },
985
977
  this.app[appName]
986
978
  )
987
- for (let i = 0; i < Object.keys(this.app[appName].model).length; i++) {
988
- const model = Object.keys(this.app[appName].model)[i]
979
+ for (const model of modelList) {
989
980
 
990
981
  // if (!this.app[appName].include || !this.app[appName].include[model]) {
991
982
  // this.app[appName] = _.defaultsDeep(
@@ -1166,8 +1157,8 @@ class APP {
1166
1157
  'controller',
1167
1158
  'afterExecute',
1168
1159
  ]
1169
- for (let i = 0; i < hooksList.length; i++) {
1170
- if (_.get(app, `${appName}.${hooksList[i]}.${controller}.${fn}`)) {
1160
+ for (const hook of hooksList) {
1161
+ if (_.get(app, `${appName}.${hook}.${controller}.${fn}`)) {
1171
1162
  // if (hooksList[i] === 'controller') {
1172
1163
  // process.once('unhandledRejection', (reason, promise) => {
1173
1164
  // if (
@@ -1186,7 +1177,7 @@ class APP {
1186
1177
  // }
1187
1178
  // })
1188
1179
  // }
1189
- await app[appName][hooksList[i]][controller][fn](ctx)
1180
+ await app[appName][hook][controller][fn](ctx)
1190
1181
  }
1191
1182
  }
1192
1183
 
@@ -1214,9 +1205,9 @@ class APP {
1214
1205
  }
1215
1206
 
1216
1207
  const hooksList = ['beforeValidate', 'validate', 'afterValidate']
1217
- for (let i = 0; i < hooksList.length; i++) {
1218
- if (_.get(app, `${appName}.${hooksList[i]}.${controller}.${fn}`)) {
1219
- await app[appName][hooksList[i]][controller][fn](ctx)
1208
+ for (const hook of hooksList) {
1209
+ if (_.get(app, `${appName}.${hook}.${controller}.${fn}`)) {
1210
+ await app[appName][hook][controller][fn](ctx)
1220
1211
  }
1221
1212
  }
1222
1213
  if (
@@ -2275,7 +2266,12 @@ class APP {
2275
2266
  })
2276
2267
  .catch((e) => {
2277
2268
  // AggregateError 取第一个子错误作为实际错误
2278
- if (e && e.name === 'AggregateError' && Array.isArray(e.errors) && e.errors.length > 0) {
2269
+ if (
2270
+ e &&
2271
+ e.name === 'AggregateError' &&
2272
+ Array.isArray(e.errors) &&
2273
+ e.errors.length > 0
2274
+ ) {
2279
2275
  e = e.errors[0]
2280
2276
  }
2281
2277
  console.log('错误', e)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "13.8.48",
3
+ "version": "13.8.49",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {