@waline/vercel 1.30.5 → 1.31.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@waline/vercel",
3
- "version": "1.30.5",
3
+ "version": "1.31.0",
4
4
  "description": "vercel server for waline comment system",
5
5
  "keywords": [
6
6
  "waline",
@@ -19,21 +19,22 @@
19
19
  "@koa/cors": "^4.0.0",
20
20
  "akismet": "^2.0.7",
21
21
  "deta": "^1.1.0",
22
- "dompurify": "^3.0.2",
22
+ "dompurify": "^3.0.3",
23
23
  "dy-node-ip2region": "^1.0.1",
24
24
  "fast-csv": "^4.3.6",
25
25
  "form-data": "^4.0.0",
26
- "jsdom": "^21.1.1",
26
+ "jsdom": "^22.1.0",
27
27
  "jsonwebtoken": "^9.0.0",
28
- "katex": "^0.16.6",
28
+ "katex": "^0.16.7",
29
+ "koa-compose": "^4.1.0",
29
30
  "leancloud-storage": "^4.15.0",
30
31
  "markdown-it": "^13.0.1",
31
32
  "markdown-it-emoji": "^2.0.2",
32
33
  "markdown-it-sub": "^1.0.0",
33
34
  "markdown-it-sup": "^1.0.0",
34
35
  "mathjax-full": "^3.2.2",
35
- "node-fetch": "^2.6.9",
36
- "nodemailer": "^6.9.1",
36
+ "node-fetch": "^2.6.11",
37
+ "nodemailer": "^6.9.2",
37
38
  "nunjucks": "^3.2.4",
38
39
  "phpass": "^0.1.1",
39
40
  "prismjs": "^1.29.0",
@@ -45,6 +45,7 @@ const {
45
45
  LARK_TEMPLATE,
46
46
 
47
47
  LEVELS,
48
+ COMMENT_AUDIT,
48
49
  } = process.env;
49
50
 
50
51
  let storage = 'leancloud';
@@ -121,6 +122,8 @@ module.exports = {
121
122
  !LEVELS || isFalse(LEVELS)
122
123
  ? false
123
124
  : LEVELS.split(/\s*,\s*/).map((v) => Number(v)),
125
+
126
+ audit: !isFalse(COMMENT_AUDIT),
124
127
  avatarProxy,
125
128
  oauthUrl,
126
129
  markdown,
@@ -71,5 +71,8 @@ module.exports = [
71
71
  { handle: routerREST },
72
72
 
73
73
  'logic',
74
+ {
75
+ handle: 'plugin',
76
+ },
74
77
  'controller',
75
78
  ];
@@ -171,9 +171,7 @@ module.exports = class extends BaseRest {
171
171
  think.logger.debug(`Comment post frequency check OK!`);
172
172
 
173
173
  /** Akismet */
174
- const { COMMENT_AUDIT } = process.env;
175
-
176
- data.status = COMMENT_AUDIT ? 'waiting' : 'approved';
174
+ data.status = this.config('audit') ? 'waiting' : 'approved';
177
175
 
178
176
  think.logger.debug(`Comment initial status is ${data.status}`);
179
177
 
@@ -48,12 +48,22 @@ module.exports = class extends think.Controller {
48
48
 
49
49
  async hook(name, ...args) {
50
50
  const fn = this.config(name);
51
+ const plugins = think.getPluginHook(name);
51
52
 
52
- if (!think.isFunction(fn)) {
53
- return;
53
+ if (think.isFunction(fn)) {
54
+ plugins.unshift(fn);
54
55
  }
55
56
 
56
- return fn.call(this, ...args);
57
+ for(let i = 0; i < plugins.length; i++) {
58
+ if (!think.isFunction(plugins[i])) {
59
+ continue;
60
+ }
61
+
62
+ const resp = await plugins[i].call(this, ...args);
63
+ if (resp) {
64
+ return resp;
65
+ }
66
+ }
57
67
  }
58
68
 
59
69
  __call() {}
@@ -113,4 +113,46 @@ module.exports = {
113
113
 
114
114
  return level === -1 ? defaultLevel : level;
115
115
  },
116
+ pluginMap(type, callback) {
117
+ const plugins = think.config('plugins');
118
+ const fns = [];
119
+
120
+ if (!think.isArray(plugins)) {
121
+ return fns;
122
+ }
123
+
124
+
125
+ for (let i = 0; i < plugins.length; i++) {
126
+ const plugin = plugins[i];
127
+
128
+ if (!plugin || !plugin[type]) {
129
+ continue;
130
+ }
131
+
132
+ const res = callback(plugin[type]);
133
+ if (!res) {
134
+ continue;
135
+ }
136
+
137
+ fns.push(res);
138
+ }
139
+
140
+ return fns;
141
+ },
142
+ getPluginMiddlewares() {
143
+ const middlewares = think.pluginMap('middlewares', (middleware) => {
144
+ if (think.isFunction(middleware)) {
145
+ return middleware;
146
+ }
147
+
148
+ if (think.isArray(middleware)) {
149
+ return middleware.filter((m) => think.isFunction(m));
150
+ }
151
+ });
152
+
153
+ return middlewares.flat();
154
+ },
155
+ getPluginHook(hookName) {
156
+ return think.pluginMap('hooks', (hook) => think.isFunction(hook[hookName]) ? hook[hookName] : undefined).filter(v => v);
157
+ }
116
158
  };
@@ -0,0 +1,12 @@
1
+ const compose = require('koa-compose');
2
+
3
+ module.exports = function() {
4
+ return (ctx, next) => {
5
+ const middlewares = think.getPluginMiddlewares();
6
+ if (!think.isArray(middlewares) || !middlewares.length) {
7
+ next();
8
+ }
9
+
10
+ compose(middlewares)(ctx, next);
11
+ };
12
+ }