huxy-server 1.0.6 → 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 +2 -2
- package/src/index.js +92 -91
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "huxy-server",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "一个精炼、高性能的 Express.js 服务器模板,为现代 Node.js 应用程序设计,提供灵活的功能和最佳实践。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./src/index.js",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"http-proxy-middleware": "^3.0.5",
|
|
39
|
-
"huxy-node-server": "^1.0.
|
|
39
|
+
"huxy-node-server": "^1.0.5",
|
|
40
40
|
"jsonwebtoken": "^9.0.3"
|
|
41
41
|
},
|
|
42
42
|
"engines": {
|
package/src/index.js
CHANGED
|
@@ -1,93 +1,95 @@
|
|
|
1
1
|
import {
|
|
2
|
-
startServer as
|
|
2
|
+
startServer as M,
|
|
3
3
|
startStatic as L,
|
|
4
|
-
logger as
|
|
5
|
-
createLogger as
|
|
6
|
-
dateTime as
|
|
7
|
-
localIPs as
|
|
8
|
-
nodeArgs as
|
|
9
|
-
getEnvConfig as
|
|
10
|
-
checkPort as
|
|
11
|
-
getDirName as
|
|
12
|
-
resolvePath as
|
|
4
|
+
logger as te,
|
|
5
|
+
createLogger as re,
|
|
6
|
+
dateTime as se,
|
|
7
|
+
localIPs as oe,
|
|
8
|
+
nodeArgs as ae,
|
|
9
|
+
getEnvConfig as ne,
|
|
10
|
+
checkPort as ie,
|
|
11
|
+
getDirName as pe,
|
|
12
|
+
resolvePath as ce,
|
|
13
13
|
} from 'huxy-node-server';
|
|
14
|
-
import {createProxyMiddleware as E} from 'http-proxy-middleware';
|
|
15
|
-
import {dateTime as
|
|
16
|
-
import
|
|
17
|
-
var
|
|
18
|
-
var
|
|
14
|
+
import {createProxyMiddleware as E, fixRequestBody as S} from 'http-proxy-middleware';
|
|
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
|
+
var x =
|
|
19
19
|
(r = {}) =>
|
|
20
20
|
(e, t, s) => {
|
|
21
21
|
let o = e.headers.authorization;
|
|
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 ((
|
|
49
|
-
let {secret:
|
|
50
|
-
|
|
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();
|
|
50
|
+
let {secret: u, expiresIn: c, algorithm: l, issuer: h} = t;
|
|
51
|
+
x({secret: u, expiresIn: c, algorithm: l, issuer: h})(s, o, a);
|
|
51
52
|
},
|
|
52
|
-
|
|
53
|
-
var v = ['x-
|
|
54
|
-
|
|
53
|
+
y = H;
|
|
54
|
+
var v = ['origin', 'referer', 'x-forwarded-for', 'x-real-ip', 'cf-connecting-ip', 'cf-ipcountry', 'cf-ray', 'x-huxy-auth'],
|
|
55
|
+
$ = ['x-powered-by', 'server'],
|
|
56
|
+
w = (r, e) => {
|
|
55
57
|
let t = new Headers(r);
|
|
56
|
-
return (
|
|
58
|
+
return (v.forEach(s => t.delete(s)), t.set('Host', e), t.set('User-Agent', 'IHUXY-API/1.0'), t);
|
|
57
59
|
},
|
|
58
|
-
|
|
60
|
+
g = r => {
|
|
59
61
|
let e = new Headers(r);
|
|
60
62
|
return (
|
|
61
|
-
|
|
63
|
+
$.forEach(t => e.delete(t)),
|
|
62
64
|
e.set('Access-Control-Allow-Origin', '*'),
|
|
63
65
|
e.set('X-Content-Type-Options', 'nosniff'),
|
|
64
66
|
e.get('content-type')?.includes('text/event-stream') && ((e['Cache-Control'] = 'no-cache, no-transform'), (e.Connection = 'keep-alive'), (e['X-Accel-Buffering'] = 'no')),
|
|
65
67
|
e
|
|
66
68
|
);
|
|
67
69
|
};
|
|
68
|
-
var
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
var
|
|
70
|
+
var I = r => Object.prototype.toString.call(r).slice(8, -1).toLowerCase(),
|
|
71
|
+
R = r => (I(r) === 'object' ? [r] : Array.isArray(r) ? r : []),
|
|
72
|
+
A = (r, e) => R(r).map(t => ((t.prefix = `${e}${(t.prefix ?? t.name) ? `/${t.name}` : ''}`.replace('//', '/')), t)),
|
|
73
|
+
P = r => (Array.isArray(r) ? r : []).filter(Boolean),
|
|
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) => ({
|
|
74
76
|
target: r,
|
|
75
|
-
pathRewrite: {[`^${e}`]: ''},
|
|
77
|
+
pathRewrite: e?.length > 1 ? {[`^${e}`]: ''} : void 0,
|
|
76
78
|
changeOrigin: !0,
|
|
77
79
|
selfHandleResponse: !1,
|
|
78
|
-
|
|
79
|
-
!s &&
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
80
|
+
on: {
|
|
81
|
+
proxyReq: (o, a, n) => (!s && w(o.headers, r), S(o, a, n)),
|
|
82
|
+
proxyRes: (o, a, n) => {
|
|
83
|
+
!s && g(o.headers);
|
|
84
|
+
},
|
|
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'}));
|
|
87
|
+
},
|
|
86
88
|
},
|
|
87
89
|
...t,
|
|
88
90
|
}),
|
|
89
|
-
|
|
90
|
-
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()};
|
|
91
93
|
(r.get(e, (s, o) => {
|
|
92
94
|
o.status(200).json(t);
|
|
93
95
|
}),
|
|
@@ -95,22 +97,21 @@ var S = ({target: r = 'http://localhost:11434', prefix: e = '/api', ...t} = {},
|
|
|
95
97
|
o.status(200).json(t);
|
|
96
98
|
}));
|
|
97
99
|
},
|
|
98
|
-
|
|
99
|
-
let {apiPrefix: s, proxys: o = [], whiteAuthKeys:
|
|
100
|
-
|
|
101
|
-
if (!
|
|
102
|
-
(t.info(`\u{1F4DD} API \u63A5\u53E3\u5730\u5740: ${e.protocol}://${e.host}:${e.port}${s}`),
|
|
103
|
-
let
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
(r.use(c, h, E(u)), t.info(`\u2705 \u4EE3\u7406\u4E2D ${c} \u{1F449} ${l}`));
|
|
100
|
+
W = (r, e = {}, t) => {
|
|
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
|
-
var
|
|
110
|
+
d = W;
|
|
111
|
+
var B = {
|
|
111
112
|
port: parseInt(process.env.PORT || '8080', 10),
|
|
112
113
|
host: process.env.HOST || 'localhost',
|
|
113
|
-
apiPrefix: process.env.API_PREFIX || '/
|
|
114
|
+
apiPrefix: process.env.API_PREFIX || '/',
|
|
114
115
|
authToken: !1,
|
|
115
116
|
proxys: [],
|
|
116
117
|
whitePathList: ['/health'],
|
|
@@ -119,30 +120,30 @@ var O = {
|
|
|
119
120
|
expiresIn: process.env.JWT_EXPIRES_IN || '30d',
|
|
120
121
|
issuer: process.env.JWT_ISSUER || 'huxyApp',
|
|
121
122
|
},
|
|
122
|
-
|
|
123
|
-
var
|
|
124
|
-
|
|
125
|
-
(await e?.(t, s, o,
|
|
123
|
+
m = B;
|
|
124
|
+
var U = (r, e) =>
|
|
125
|
+
M({...m, ...r}, async (t, s, o, a) => {
|
|
126
|
+
(await e?.(t, s, o, a), d(s, t, a));
|
|
126
127
|
}),
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
L({...
|
|
130
|
-
(await e?.(t, s, o,
|
|
128
|
+
he = U,
|
|
129
|
+
de = (r, e) =>
|
|
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
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
134
|
+
d as appProxy,
|
|
135
|
+
ie as checkPort,
|
|
136
|
+
re as createLogger,
|
|
137
|
+
se as dateTime,
|
|
138
|
+
he as default,
|
|
139
|
+
pe as getDirName,
|
|
140
|
+
ne as getEnvConfig,
|
|
141
|
+
oe as localIPs,
|
|
142
|
+
te as logger,
|
|
143
|
+
ae as nodeArgs,
|
|
144
|
+
ce as resolvePath,
|
|
145
|
+
U as startApp,
|
|
146
|
+
M as startServer,
|
|
146
147
|
L as startStatic,
|
|
147
|
-
|
|
148
|
+
de as startStaticApp,
|
|
148
149
|
};
|