@qingfu/core-env 0.1.6 → 0.1.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.
package/lib/helper.js CHANGED
@@ -67,22 +67,29 @@ helper.parseLike = ( ...fields ) => {
67
67
  };
68
68
 
69
69
  helper.checkRestrict = async ( ctx, next ) => {
70
- let count = await brokers( 'core' ).call( 'check.count', ctx.ip );
70
+ let scene = ctx.state.user ? ctx.state.user.id : ctx.ip;
71
+ let count = await brokers( 'core' ).call( 'check.count', scene );
71
72
  if ( count > 50 ) return ctx.throw( 429 );
72
73
  try {
73
74
  await next();
74
75
  } catch ( err ) {
75
- await brokers( 'core' ).call( 'check.increase', ctx.ip );
76
+ await brokers( 'core' ).call( 'check.increase', scene );
76
77
  throw err;
77
78
  }
78
79
  };
79
80
 
80
81
  helper.validatePassword = async ( ctx, next ) => {
81
- if ( !ctx.header.password ) return ctx.throw( 428, 'PASSWORD_REQUIRED' );
82
- let match = await broker.call( 'user.validatePassword', {
83
- userID: ctx.state.user.id, password: ctx.header.password });
84
- if ( !match ) return ctx.throw( 412, 'PASSWORD_WRONG' );
85
- else return await next();
82
+ let userID = ctx.state.user.id, password = ctx.header.password;
83
+ if ( !password ) return ctx.throw( 428, 'PASSWORD_REQUIRED' );
84
+ let count = await brokers( 'core' ).call( 'check.count', userID );
85
+ if ( count > 50 ) return ctx.throw( 429 );
86
+ let match = await broker.call( 'user.validatePassword', { userID, password });
87
+ if ( match ) {
88
+ return await next();
89
+ } else {
90
+ await brokers( 'core' ).call( 'check.increase', userID );
91
+ return ctx.throw( 412, 'PASSWORD_WRONG' );
92
+ }
86
93
  };
87
94
 
88
95
  helper.wmpTestGet = async ctx => {
package/lib/server.js CHANGED
@@ -10,11 +10,17 @@ if ( process.env.HTTP_PORT ) {
10
10
  app.use( require( 'koa-helmet' )({ frameguard: false, contentSecurityPolicy: false }));
11
11
  app.use( require( 'koa-methodoverride' )() );
12
12
  app.use( require( 'koa-logger' )() );
13
+ app.use( dontparse );
13
14
  app.use( require( 'koa-bodyparser' )({ enableTypes: [ 'json', 'form', 'xml' ]}));
14
15
  app.use( require( 'koa-favicon' )( path.join( __dirname, '../favicon.ico' )));
15
16
  app.use( error );
16
17
  require( 'koa-qs' )( app );
17
18
 
19
+ async function dontparse( ctx, next ) {
20
+ if ( ctx.path.includes( '/dontparse' ) ctx.disableBodyParser = true;
21
+ await next();
22
+ };
23
+
18
24
  async function error( ctx, next ) {
19
25
  try {
20
26
  await next();
@@ -46,7 +52,8 @@ if ( process.env.HTTP_PORT ) {
46
52
  logger.error( Object.assign( err, {
47
53
  url: ctx.path,
48
54
  query: ctx.query,
49
- body: ctx.request.body
55
+ body: ctx.request.body,
56
+ user: ctx.state.user
50
57
  }));
51
58
  });
52
59
 
package/lib/utils.js CHANGED
@@ -17,6 +17,10 @@ utils.json2xml = json => {
17
17
  return builder.buildObject( json );
18
18
  };
19
19
 
20
+ utils.trimZeroWidthSpace = string => {
21
+ return _.isString( string ) ? string.trim().replace( /[\uFEFF\u0001\u200b-\u200f\u202a-\u202e]/g, '' ) : string;
22
+ };
23
+
20
24
  utils.json2xlsx = ( json, mapping, type = 'xlsx' ) => {
21
25
  let data = json.map( obj => mapping.map( v => {
22
26
  let value = v.value ? v.value( obj ) : _.get( obj, v.field );
@@ -56,7 +60,7 @@ utils.xlsx2json = ( buffer, mapping ) => {
56
60
  return _.transform( mapping, ( r, map ) => {
57
61
  if ( map.index === -1 ) return;
58
62
  let value = values[ map.index ];
59
- if ( _.isString( value )) value = value.trim().replace( /\u200B/g, '' ).replace( /\u0001/g, '' );
63
+ if ( _.isString( value )) value = utils.trimZeroWidthSpace( value );
60
64
  else if ( value instanceof Date ) value = fixImportedDate( value, date1904 );
61
65
  r[ map.field ] = ( map.format || _.identity )( value );
62
66
  }, {});
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@qingfu/core-env",
3
- "version": "0.1.6",
3
+ "version": "0.1.10",
4
4
  "readmeFilename": "README.md",
5
5
  "dependencies": {
6
6
  "@koa/cors": "3.1.0",
7
7
  "@koa/router": "10.1.1",
8
- "bull": "4.1.1",
8
+ "bull": "4.1.4",
9
9
  "busboy": "0.3.1",
10
10
  "cookie": "0.4.1",
11
11
  "dotenv-extended": "2.9.0",
12
12
  "glob": "7.2.0",
13
- "ioredis": "4.28.0",
13
+ "ioredis": "4.28.2",
14
14
  "jsonwebtoken": "8.5.1",
15
15
  "koa": "2.13.4",
16
16
  "koa-bodyparser": "4.3.0",
@@ -21,11 +21,11 @@
21
21
  "koa-qs": "3.0.0",
22
22
  "lodash": "4.17.21",
23
23
  "moleculer": "0.14.18",
24
- "mongoose": "6.0.13",
24
+ "mongoose": "6.1.2",
25
25
  "mongoose-cast-aggregation": "0.2.1",
26
26
  "mongoose-geojson-schema": "2.1.7",
27
27
  "mongoose-lean-defaults": "2.0.1",
28
- "mongoose-lean-getters": "0.2.1",
28
+ "mongoose-lean-getters": "0.2.2",
29
29
  "mongoose-lean-virtuals": "0.9.0",
30
30
  "ms": "2.1.3",
31
31
  "notepack.io": "2.3.0",