chanjs 2.0.19 → 2.1.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/core/service.js +2 -1
- package/global/global.js +12 -1
- package/helper/ip.js +0 -2
- package/index.js +6 -4
- package/package.json +1 -1
package/core/service.js
CHANGED
package/global/global.js
CHANGED
|
@@ -1,15 +1,26 @@
|
|
|
1
|
-
import { pathToFileURL, fileURLToPath } from "url";
|
|
2
1
|
import path from "path";
|
|
2
|
+
import { readFileSync } from "fs";
|
|
3
|
+
import { pathToFileURL, fileURLToPath } from "url";
|
|
4
|
+
|
|
3
5
|
const ROOT_PATH = process.cwd();
|
|
4
6
|
const APP_PATH = path.join(ROOT_PATH, "app");
|
|
5
7
|
const __filename = fileURLToPath(import.meta.url);
|
|
6
8
|
const __dirname = path.dirname(__filename);
|
|
7
9
|
|
|
10
|
+
const getVersion = () => {
|
|
11
|
+
const packageJsonPath = path.join(ROOT_PATH, "package.json");
|
|
12
|
+
const packageJson = JSON.parse(
|
|
13
|
+
readFileSync(packageJsonPath, "utf8")
|
|
14
|
+
);
|
|
15
|
+
return packageJson?.version || "1.0.0";
|
|
16
|
+
};
|
|
17
|
+
|
|
8
18
|
const globals = {
|
|
9
19
|
__dirname,
|
|
10
20
|
__filename,
|
|
11
21
|
ROOT_PATH,
|
|
12
22
|
APP_PATH,
|
|
23
|
+
APP_VERSION: getVersion(),
|
|
13
24
|
CONFIG_PATH: path.join(ROOT_PATH, "config"),
|
|
14
25
|
EXTEND_PATH: path.join(APP_PATH, "extend"),
|
|
15
26
|
PUBLIC_PATH: path.join(ROOT_PATH, "public"),
|
package/helper/ip.js
CHANGED
|
@@ -6,8 +6,6 @@
|
|
|
6
6
|
export const getIp = (req) => {
|
|
7
7
|
// 优先级从高到低获取可能的IP来源
|
|
8
8
|
const ipSources = [
|
|
9
|
-
req.headers["x-forwarded-for"], // 代理场景下的真实IP(可能多个,逗号分隔)
|
|
10
|
-
req.headers["x-real-ip"], // 部分代理服务器使用
|
|
11
9
|
req.ip, // Express内置的IP获取(已处理代理)
|
|
12
10
|
req.connection?.remoteAddress, // 底层连接的远程地址
|
|
13
11
|
req.socket?.remoteAddress, // 套接字的远程地址
|
package/index.js
CHANGED
|
@@ -21,7 +21,7 @@ import { dirname } from "./helper/file.js";
|
|
|
21
21
|
|
|
22
22
|
class Chan {
|
|
23
23
|
//版本号
|
|
24
|
-
#version = "
|
|
24
|
+
#version = "1.0.0";
|
|
25
25
|
static helper = {};
|
|
26
26
|
static common = {}; //公共方法
|
|
27
27
|
static config = {}; //配置
|
|
@@ -50,6 +50,8 @@ class Chan {
|
|
|
50
50
|
//加载配置文件
|
|
51
51
|
async config() {
|
|
52
52
|
let config = await loadConfig();
|
|
53
|
+
//版本号,先从package.json中获取,如果没有,则使用默认版本号
|
|
54
|
+
config.APP_VERSION = APP_VERSION || config.APP_VERSION;
|
|
53
55
|
Chan.config = config;
|
|
54
56
|
}
|
|
55
57
|
|
|
@@ -65,9 +67,10 @@ class Chan {
|
|
|
65
67
|
statics,
|
|
66
68
|
logger,
|
|
67
69
|
cors,
|
|
70
|
+
PROXY,
|
|
68
71
|
} = Chan.config;
|
|
69
72
|
|
|
70
|
-
this.app.set("trust proxy", true);
|
|
73
|
+
this.app.set("trust proxy", PROXY === "true" ? true : false);
|
|
71
74
|
log(this.app, logger);
|
|
72
75
|
setFavicon(this.app);
|
|
73
76
|
setCookie(this.app, cookieKey);
|
|
@@ -164,11 +167,10 @@ class Chan {
|
|
|
164
167
|
path: r.route.path,
|
|
165
168
|
methods: Object.keys(r.route.methods),
|
|
166
169
|
}));
|
|
167
|
-
|
|
168
170
|
}
|
|
169
171
|
|
|
170
172
|
run(cb) {
|
|
171
|
-
const port = parseInt(
|
|
173
|
+
const port = parseInt(Chan.config.PORT) || 3000;
|
|
172
174
|
this.app.listen(port, () => {
|
|
173
175
|
cb ? cb(port) : console.log(`Server is running on port ${port}`);
|
|
174
176
|
});
|