huxy-server 1.0.7 → 1.0.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/package.json +1 -1
- package/src/index.js +42 -41
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -12,9 +12,9 @@ import {
|
|
|
12
12
|
resolvePath as ce,
|
|
13
13
|
} from 'huxy-node-server';
|
|
14
14
|
import {createProxyMiddleware as E, fixRequestBody as S} from 'http-proxy-middleware';
|
|
15
|
-
import {dateTime as
|
|
16
|
-
import
|
|
17
|
-
var
|
|
15
|
+
import {dateTime as k} from 'huxy-node-server';
|
|
16
|
+
import j from 'jsonwebtoken';
|
|
17
|
+
var f = (r, {secret: e = '', ...t} = {}) => j.verify(r, e, t);
|
|
18
18
|
var x =
|
|
19
19
|
(r = {}) =>
|
|
20
20
|
(e, t, s) => {
|
|
@@ -22,32 +22,33 @@ var x =
|
|
|
22
22
|
if (!o) return (e.log.warn('\u8BA4\u8BC1\u5931\u8D25: \u7F3A\u5C11\u8BA4\u8BC1\u4FE1\u606F'), t.status(401).json({message: '\u7F3A\u5C11\u8BA4\u8BC1\u4FE1\u606F'}));
|
|
23
23
|
if (!o.startsWith('Bearer '))
|
|
24
24
|
return (e.log.warn('\u8BA4\u8BC1\u5931\u8D25: \u672A\u63D0\u4F9B\u6709\u6548\u8BA4\u8BC1\u4FE1\u606F'), t.status(401).json({message: '\u672A\u63D0\u4F9B\u6709\u6548\u8BA4\u8BC1\u4FE1\u606F'}));
|
|
25
|
-
let
|
|
26
|
-
if (!
|
|
25
|
+
let a = o.split(' ')[1];
|
|
26
|
+
if (!a) return (e.log.warn('\u8BA4\u8BC1\u5931\u8D25: \u8BBF\u95EE\u4EE4\u724C\u7F3A\u5931'), t.status(401).json({message: '\u8BBF\u95EE\u4EE4\u724C\u7F3A\u5931'}));
|
|
27
27
|
try {
|
|
28
|
-
let
|
|
29
|
-
(e.log.info(
|
|
30
|
-
} catch (
|
|
31
|
-
|
|
28
|
+
let n = f(a, r);
|
|
29
|
+
(e.log.info(n, '\u8BA4\u8BC1\u6210\u529F'), (e.user = n), s());
|
|
30
|
+
} catch (n) {
|
|
31
|
+
let i = n.type || n.name;
|
|
32
|
+
return i === 'TokenExpiredError'
|
|
32
33
|
? (e.log.warn({ip: e.ip}, '\u8BA4\u8BC1\u5931\u8D25: \u4EE4\u724C\u5DF2\u8FC7\u671F'), t.status(401).json({message: '\u4EE4\u724C\u5DF2\u8FC7\u671F'}))
|
|
33
|
-
:
|
|
34
|
+
: i === 'JsonWebTokenError'
|
|
34
35
|
? (e.log.warn({ip: e.ip}, '\u8BA4\u8BC1\u5931\u8D25: \u65E0\u6548\u7684\u4EE4\u724C'), t.status(403).json({message: '\u65E0\u6548\u7684\u4EE4\u724C'}))
|
|
35
|
-
:
|
|
36
|
-
? (e.log.warn({ip: e.ip}, `\u8BA4\u8BC1\u5931\u8D25: ${
|
|
37
|
-
: (e.log.warn({err:
|
|
36
|
+
: i === 'AuthorizationError'
|
|
37
|
+
? (e.log.warn({ip: e.ip}, `\u8BA4\u8BC1\u5931\u8D25: ${n.message}`), t.status(n.status).json({message: n.message}))
|
|
38
|
+
: (e.log.warn({err: n, ip: e.ip}, '\u8BA4\u8BC1\u5931\u8D25: \u5185\u90E8\u670D\u52A1\u5668\u9519\u8BEF'), t.status(500).json({message: '\u5185\u90E8\u670D\u52A1\u5668\u9519\u8BEF'}));
|
|
38
39
|
}
|
|
39
40
|
};
|
|
40
41
|
var H =
|
|
41
42
|
({whiteAuthKeys: r = [], whiteAuthPaths: e = [], config: t = {}}) =>
|
|
42
|
-
(s, o,
|
|
43
|
-
if (s.method === 'OPTIONS' || e.includes(s.path)) return
|
|
44
|
-
let {authToken:
|
|
45
|
-
if (
|
|
46
|
-
let
|
|
47
|
-
|
|
48
|
-
if ((
|
|
43
|
+
(s, o, a) => {
|
|
44
|
+
if (s.method === 'OPTIONS' || e.includes(s.path)) return a();
|
|
45
|
+
let {authToken: n} = t;
|
|
46
|
+
if (n === !1 || n === 'false') return a();
|
|
47
|
+
let i = s.headers,
|
|
48
|
+
p = i['x-huxy-auth'] || i['x-api-key'] || i.authorization?.split('Bearer ')[1];
|
|
49
|
+
if ((p && p === n) || r.includes(p)) return a();
|
|
49
50
|
let {secret: u, expiresIn: c, algorithm: l, issuer: h} = t;
|
|
50
|
-
x({secret: u, expiresIn: c, algorithm: l, issuer: h})(s, o,
|
|
51
|
+
x({secret: u, expiresIn: c, algorithm: l, issuer: h})(s, o, a);
|
|
51
52
|
},
|
|
52
53
|
y = H;
|
|
53
54
|
var v = ['origin', 'referer', 'x-forwarded-for', 'x-real-ip', 'cf-connecting-ip', 'cf-ipcountry', 'cf-ray', 'x-huxy-auth'],
|
|
@@ -70,25 +71,25 @@ var I = r => Object.prototype.toString.call(r).slice(8, -1).toLowerCase(),
|
|
|
70
71
|
R = r => (I(r) === 'object' ? [r] : Array.isArray(r) ? r : []),
|
|
71
72
|
A = (r, e) => R(r).map(t => ((t.prefix = `${e}${(t.prefix ?? t.name) ? `/${t.name}` : ''}`.replace('//', '/')), t)),
|
|
72
73
|
P = r => (Array.isArray(r) ? r : []).filter(Boolean),
|
|
73
|
-
|
|
74
|
-
var
|
|
74
|
+
T = (r, e) => [...new Set(['/', '/health', e, ...(Array.isArray(r) ? r : [])])].filter(Boolean).map(t => `${e}${t}`.replace('//', '/'));
|
|
75
|
+
var O = ({target: r = 'http://', prefix: e, ...t} = {}, s = !1) => ({
|
|
75
76
|
target: r,
|
|
76
77
|
pathRewrite: e?.length > 1 ? {[`^${e}`]: ''} : void 0,
|
|
77
78
|
changeOrigin: !0,
|
|
78
79
|
selfHandleResponse: !1,
|
|
79
80
|
on: {
|
|
80
|
-
proxyReq: (o,
|
|
81
|
-
proxyRes: (o,
|
|
81
|
+
proxyReq: (o, a, n) => (!s && w(o.headers, r), S(o, a, n)),
|
|
82
|
+
proxyRes: (o, a, n) => {
|
|
82
83
|
!s && g(o.headers);
|
|
83
84
|
},
|
|
84
|
-
error: (o,
|
|
85
|
-
(
|
|
85
|
+
error: (o, a, n) => {
|
|
86
|
+
(a.log.error({err: o}, '\u4EE3\u7406\u9519\u8BEF'), n.headersSent || n.status(502).json({error: '\u7F51\u5173\u9519\u8BEF'}));
|
|
86
87
|
},
|
|
87
88
|
},
|
|
88
89
|
...t,
|
|
89
90
|
}),
|
|
90
|
-
|
|
91
|
-
let t = {status: 'OK', message: `API \u670D\u52A1\u5668\u8FD0\u884C\u4E2D \u{1F449} ${e}`, timestamp:
|
|
91
|
+
C = (r, e) => {
|
|
92
|
+
let t = {status: 'OK', message: `API \u670D\u52A1\u5668\u8FD0\u884C\u4E2D \u{1F449} ${e}`, timestamp: k(), uptime: process.uptime(), memoryUsage: process.memoryUsage()};
|
|
92
93
|
(r.get(e, (s, o) => {
|
|
93
94
|
o.status(200).json(t);
|
|
94
95
|
}),
|
|
@@ -97,13 +98,13 @@ var k = ({target: r = 'http://', prefix: e, ...t} = {}, s = !1) => ({
|
|
|
97
98
|
}));
|
|
98
99
|
},
|
|
99
100
|
W = (r, e = {}, t) => {
|
|
100
|
-
let {apiPrefix: s = '/', proxys: o = [], whiteAuthKeys:
|
|
101
|
-
|
|
102
|
-
if (!
|
|
103
|
-
(t.info(`\u{1F4DD} API \u63A5\u53E3\u5730\u5740: ${e.protocol}://${e.host}:${e.port}${s}`),
|
|
104
|
-
let u = y({whiteAuthKeys: P(
|
|
105
|
-
|
|
106
|
-
(r.use(c, u, E(
|
|
101
|
+
let {apiPrefix: s = '/', proxys: o = [], whiteAuthKeys: a = [], whitePathList: n = [], preserve: i = !1} = e,
|
|
102
|
+
p = A(o, s);
|
|
103
|
+
if (!p.length) return;
|
|
104
|
+
(t.info(`\u{1F4DD} API \u63A5\u53E3\u5730\u5740: ${e.protocol}://${e.host}:${e.port}${s}`), C(r, s));
|
|
105
|
+
let u = y({whiteAuthKeys: P(a), whitePathList: T(n, s), config: e});
|
|
106
|
+
p.map(({prefix: c, target: l, ...h}) => {
|
|
107
|
+
(r.use(c, u, E(O({prefix: c, target: l, ...h}, i))), t.info(`\u2705 \u4EE3\u7406\u4E2D ${c} \u{1F449} ${l}`));
|
|
107
108
|
});
|
|
108
109
|
},
|
|
109
110
|
d = W;
|
|
@@ -119,15 +120,15 @@ var B = {
|
|
|
119
120
|
expiresIn: process.env.JWT_EXPIRES_IN || '30d',
|
|
120
121
|
issuer: process.env.JWT_ISSUER || 'huxyApp',
|
|
121
122
|
},
|
|
122
|
-
|
|
123
|
+
m = B;
|
|
123
124
|
var U = (r, e) =>
|
|
124
|
-
M({...
|
|
125
|
-
(await e?.(t, s, o,
|
|
125
|
+
M({...m, ...r}, async (t, s, o, a) => {
|
|
126
|
+
(await e?.(t, s, o, a), d(s, t, a));
|
|
126
127
|
}),
|
|
127
128
|
he = U,
|
|
128
129
|
de = (r, e) =>
|
|
129
|
-
L({...
|
|
130
|
-
(await e?.(t, s, o,
|
|
130
|
+
L({...m, ...r}, async (t, s, o, a) => {
|
|
131
|
+
(await e?.(t, s, o, a), d(s, t, a));
|
|
131
132
|
});
|
|
132
133
|
export {
|
|
133
134
|
d as appProxy,
|