-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
228 lines (196 loc) · 4.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
'use strict';
const fs = require("fs");
const Koa = require("koa");
const KoaWeb = require("koa-web");
const KoaSession = require("koa-session");
const crypto = require('crypto');
const app = new Koa();
module.exports = class Xweb {
static __config;
/**
* 配置设置
* @param {*} config
* @returns
*/
config (config) {
if (config === undefined || config.constructor.name !== 'Object') {
return;
}
this.__config = config;
}
/**
* 创建文件夹
* @param {*} path
* @returns
*/
mkdir (path = '') {
path = path.replace(/[\\]/g, "/");
let pos = path.lastIndexOf("/");
if (pos >= 0) {
path = path.substring(0, pos);
}
let pathTmp = "";
let pathList = path.split("/");
try {
for (const i in pathList) {
pathTmp += "/" + pathList[i];
if (fs.existsSync(pathTmp)) {
if (fs.statSync(pathTmp).isFile()) {
return false;
}
} else {
fs.mkdirSync(pathTmp);
}
}
} catch (e) {
return false;
}
return true;
}
/**
* 读取文件
* @param {*} jsonFile
* @param {*} maxAge
* @returns
*/
read (jsonFile = '', maxAge = 0) {
if (!fs.existsSync(jsonFile) || !fs.statSync(jsonFile).isFile()) {
return {};
}
const statSync = fs.statSync(jsonFile);
const age = (new Date()).getTime() - parseInt(statSync.mtimeMs);
try {
if (age > maxAge) {
fs.unlinkSync(jsonFile);
return {};
}
return JSON.parse(fs.readFileSync(jsonFile).toString());
} catch (e) {
console.error(e);
return {};
}
}
/**
* 获取键值
* @param {*} sessionId
* @returns
*/
getKey(sessionId) {
const md5 = crypto.createHash('md5').update(sessionId).digest("hex");
return md5.substring(0, 2) + "/" + md5.substring(2, 16) + ".json";
}
/**
* 清理文件夹过期的cookie文件
* @param {*} path
* @param {*} maxAge
*/
clearKeys(path = '', maxAge = 0) {
fs.readdir(path, function (err, files) {
if (err) {
return '目录不存在';
}
for (const i in files) {
let tPath = path + "/" + files[i];
if (!fs.statSync(tPath).isDirectory()) {
continue;
}
fs.readdir(tPath, function (e, _fs) {
if (e) {
return '目录不存在';
}
let delCot = 0;
let fCounts = _fs.length;
for (const j in _fs) {
let jvPath = tPath + "/" + _fs[j];
const statSync = fs.statSync(jvPath);
const age = (new Date()).getTime() - parseInt(statSync.mtimeMs);
if (age > maxAge) {
delCot ++;
fs.unlinkSync(jvPath);
}
}
if (delCot >= fCounts) {
fs.rmdirSync(tPath);
}
});
}
})
}
/**
* Koa应用
* @param {*} func
*/
use (func) {
app.use(func);
}
/**
* 初始化
*/
init () {
app.keys = ['xweb'];
let sKey = this.__config.sessionKey;
let sMaxAge = this.__config.sessionMaxAge;
let config = {
key: 'xweb',
renew: true,
};
if (sKey !== undefined && typeof sKey === 'string') {
config['key'] = sKey;
}
if (sMaxAge !== undefined && typeof sMaxAge === 'number') {
config['maxAge'] = parseInt(sMaxAge);
} else {
sMaxAge = 86400000;
config['maxAge'] = sMaxAge;
}
if (this.__config !== undefined) {
let path = this.__config.path;
if (typeof path === 'string') {
path = path.trim();
let pathLast = path[path.length - 1];
if (pathLast !== '\\' && pathLast !== '/') {
path += "/";
}
let cachePath = this.__config.sessionPath;
if (cachePath === undefined || typeof cachePath !== 'string') {
cachePath = path + ".cache/session/";
} else {
cachePath = cachePath.trim();
if (!["/", "\\"].includes(cachePath[cachePath.length - 1])) {
cachePath += "/";
}
cachePath += ".cache/session/";
}
this.clearKeys(cachePath, sMaxAge);
config['store'] = {
// 获取 session 数据
get: (key, maxAge) => {
return this.read(cachePath + this.getKey(key), maxAge);
},
// 设置 session 数据
set: (key, sess) => {
const jsonFile = cachePath + this.getKey(key);
if (this.mkdir(jsonFile)) {
fs.writeFileSync(jsonFile, JSON.stringify(sess));
}
},
// 销毁 session 数据
destroy: (key) => {}
};
}
}
app.use(KoaSession(config, app));
}
/**
* 页面监听
* @param {*} port
* @param {*} func
*/
listen (port, func) {
this.init();
if (this.__config !== undefined) {
app.use(KoaWeb(this.__config));
}
app.listen(port, func);
}
};