befly 3.19.0 → 3.19.5
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/apis/auth/login.js +0 -2
- package/apis/tongJi/errorList.js +102 -0
- package/apis/tongJi/errorReport.js +90 -0
- package/apis/tongJi/errorStats.js +70 -0
- package/apis/tongJi/visitReport.js +241 -0
- package/apis/tongJi/visitStats.js +380 -0
- package/checks/config.js +1 -0
- package/configs/beflyConfig.json +1 -0
- package/configs/beflyMenus.json +13 -3
- package/index.js +1 -1
- package/lib/logger.js +3 -3
- package/lib/redisHelper.js +38 -0
- package/lib/sqlBuilder/compiler.js +0 -9
- package/lib/sqlBuilder/index.js +1 -19
- package/lib/sqlBuilder/parser.js +0 -21
- package/package.json +4 -4
- package/paths.js +14 -1
- package/router/static.js +3 -3
- package/sql/befly.sql +54 -0
- package/utils/datetime.js +76 -0
- package/utils/visitStats.js +121 -0
- package/utils/formatYmdHms.js +0 -23
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import { addDays, getDateYmdNumber, getTimeBucketStart } from "../../utils/datetime.js";
|
|
2
|
+
import { VISIT_STATS_BUCKET_MS, VISIT_STATS_REDIS_TTL_SECONDS, VISIT_STATS_UA_FIELDS, getVisitStatsBucketKey, getVisitStatsBucketProductPvKey, getVisitStatsBucketProductUvKey, getVisitStatsDayKey, getVisitStatsDayProductKeysKey, getVisitStatsDayProductPvKey, getVisitStatsDayProductUvKey, getVisitStatsDayUaCountKey, getVisitStatsDayUaValuesKey, getVisitStatsProductMetaKey, normalizeVisitStatsUaValue, parseVisitStatsProductKey, toVisitStatsNumber } from "../../utils/visitStats.js";
|
|
3
|
+
|
|
4
|
+
const VISIT_STATS_DAY_LIMIT = 30;
|
|
5
|
+
|
|
6
|
+
async function ensureVisitStatsTableReady(befly) {
|
|
7
|
+
if (befly.visitStatsTableReady !== undefined) {
|
|
8
|
+
return befly.visitStatsTableReady;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const tableExistsResult = await befly.mysql.tableExists("beflyVisitStats");
|
|
12
|
+
befly.visitStatsTableReady = tableExistsResult.data === true;
|
|
13
|
+
return befly.visitStatsTableReady;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async function flushVisitStatsBucket(befly, bucketTime) {
|
|
17
|
+
if (bucketTime <= 0) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const currentBucketTime = getTimeBucketStart(Date.now(), VISIT_STATS_BUCKET_MS);
|
|
22
|
+
if (bucketTime >= currentBucketTime) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const tableReady = await ensureVisitStatsTableReady(befly);
|
|
27
|
+
if (!tableReady) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const flushedKey = `visitStats:flushed:${bucketTime}`;
|
|
32
|
+
const flushed = await befly.redis.exists(flushedKey);
|
|
33
|
+
if (flushed) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const bucketDate = getDateYmdNumber(bucketTime);
|
|
38
|
+
const bucketKey = getVisitStatsBucketKey(bucketTime);
|
|
39
|
+
const pv = toVisitStatsNumber(await befly.redis.getString(`${bucketKey}:pv`));
|
|
40
|
+
const uv = await befly.redis.scard(`${bucketKey}:uv`);
|
|
41
|
+
const errorCount = toVisitStatsNumber(await befly.redis.getString(`${bucketKey}:errorCount`));
|
|
42
|
+
const durationSum = toVisitStatsNumber(await befly.redis.getString(`${bucketKey}:durationSum`));
|
|
43
|
+
const durationCount = toVisitStatsNumber(await befly.redis.getString(`${bucketKey}:durationCount`));
|
|
44
|
+
const avgDuration = durationCount > 0 ? Math.round(durationSum / durationCount) : 0;
|
|
45
|
+
|
|
46
|
+
if (pv <= 0 && uv <= 0 && errorCount <= 0 && durationCount <= 0) {
|
|
47
|
+
await befly.redis.setString(flushedKey, "1", VISIT_STATS_REDIS_TTL_SECONDS);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const data = {
|
|
52
|
+
bucketTime: bucketTime,
|
|
53
|
+
bucketDate: bucketDate,
|
|
54
|
+
pv: pv,
|
|
55
|
+
uv: uv,
|
|
56
|
+
errorCount: errorCount,
|
|
57
|
+
durationSum: durationSum,
|
|
58
|
+
durationCount: durationCount,
|
|
59
|
+
avgDuration: avgDuration
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const detail = await befly.mysql.getOne({
|
|
63
|
+
table: "beflyVisitStats",
|
|
64
|
+
where: { bucketTime: bucketTime }
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
if (detail.data?.id) {
|
|
68
|
+
await befly.mysql.updData({
|
|
69
|
+
table: "beflyVisitStats",
|
|
70
|
+
data: data,
|
|
71
|
+
where: { bucketTime: bucketTime }
|
|
72
|
+
});
|
|
73
|
+
} else {
|
|
74
|
+
await befly.mysql.insData({
|
|
75
|
+
table: "beflyVisitStats",
|
|
76
|
+
data: data
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
await befly.redis.setString(flushedKey, "1", VISIT_STATS_REDIS_TTL_SECONDS);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function flushVisitStatsPreviousBucket(befly, now = Date.now()) {
|
|
84
|
+
const currentBucketTime = getTimeBucketStart(now, VISIT_STATS_BUCKET_MS);
|
|
85
|
+
await flushVisitStatsBucket(befly, currentBucketTime - VISIT_STATS_BUCKET_MS);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function getVisitStatsOnlineCount(befly) {
|
|
89
|
+
const members = await befly.redis.smembers("visitStats:online:visitors");
|
|
90
|
+
if (members.length === 0) {
|
|
91
|
+
return 0;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const existsList = await befly.redis.existsBatch(members.map((member) => `visitStats:online:visitor:${member}`));
|
|
95
|
+
const expiredMembers = [];
|
|
96
|
+
let onlineCount = 0;
|
|
97
|
+
|
|
98
|
+
for (let i = 0; i < members.length; i++) {
|
|
99
|
+
if (existsList[i]) {
|
|
100
|
+
onlineCount += 1;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
expiredMembers.push(members[i]);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (expiredMembers.length > 0) {
|
|
107
|
+
await befly.redis.srem("visitStats:online:visitors", expiredMembers);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return onlineCount;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function getVisitStatsUaFieldList(befly, bucketDate, field, defaultValue) {
|
|
114
|
+
const valuesKey = getVisitStatsDayUaValuesKey(bucketDate, field);
|
|
115
|
+
const values = await befly.redis.smembers(valuesKey);
|
|
116
|
+
const list = [];
|
|
117
|
+
|
|
118
|
+
for (const rawValue of values) {
|
|
119
|
+
const value = normalizeVisitStatsUaValue(rawValue, defaultValue);
|
|
120
|
+
const count = toVisitStatsNumber(await befly.redis.getString(getVisitStatsDayUaCountKey(bucketDate, field, value)));
|
|
121
|
+
if (count <= 0) {
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
list.push({
|
|
126
|
+
name: value,
|
|
127
|
+
count: count
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
list.sort((a, b) => {
|
|
132
|
+
if (b.count !== a.count) {
|
|
133
|
+
return b.count - a.count;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return String(a.name).localeCompare(String(b.name));
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
return list.slice(0, 10);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async function getVisitStatsUaStats(befly, bucketDate) {
|
|
143
|
+
const data = {};
|
|
144
|
+
|
|
145
|
+
for (const item of VISIT_STATS_UA_FIELDS) {
|
|
146
|
+
const field = item.field;
|
|
147
|
+
const defaultValue = item.defaultValue;
|
|
148
|
+
data[field] = await getVisitStatsUaFieldList(befly, bucketDate, field, defaultValue);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
deviceTypes: data.deviceType || [],
|
|
153
|
+
browsers: data.browserName || [],
|
|
154
|
+
browserVersions: data.browserVersion || [],
|
|
155
|
+
osList: data.osName || [],
|
|
156
|
+
osVersions: data.osVersion || [],
|
|
157
|
+
deviceVendors: data.deviceVendor || [],
|
|
158
|
+
deviceModels: data.deviceModel || [],
|
|
159
|
+
engines: data.engineName || [],
|
|
160
|
+
cpuArchitectures: data.cpuArchitecture || []
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function getVisitStatsRecentDayList(now, limit = VISIT_STATS_DAY_LIMIT) {
|
|
165
|
+
const list = [];
|
|
166
|
+
|
|
167
|
+
for (let i = limit - 1; i >= 0; i--) {
|
|
168
|
+
list.push(getDateYmdNumber(addDays(now, -i)));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return list;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function getVisitStatsBucketList(bucketDate, currentBucketTime) {
|
|
175
|
+
const list = [];
|
|
176
|
+
let startBucketTime = currentBucketTime;
|
|
177
|
+
|
|
178
|
+
while (getDateYmdNumber(startBucketTime - VISIT_STATS_BUCKET_MS) === bucketDate) {
|
|
179
|
+
startBucketTime -= VISIT_STATS_BUCKET_MS;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
for (let bucketTime = startBucketTime; bucketTime <= currentBucketTime; bucketTime += VISIT_STATS_BUCKET_MS) {
|
|
183
|
+
list.push(bucketTime);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return list;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
async function getVisitStatsProductMeta(befly, productKey) {
|
|
190
|
+
const metaText = await befly.redis.getString(getVisitStatsProductMetaKey(productKey));
|
|
191
|
+
|
|
192
|
+
if (!metaText) {
|
|
193
|
+
return parseVisitStatsProductKey(productKey);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
try {
|
|
197
|
+
const meta = JSON.parse(metaText);
|
|
198
|
+
return {
|
|
199
|
+
...parseVisitStatsProductKey(productKey),
|
|
200
|
+
...meta
|
|
201
|
+
};
|
|
202
|
+
} catch (_error) {
|
|
203
|
+
return parseVisitStatsProductKey(productKey);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async function getVisitStatsProducts(befly, now, bucketDate, currentBucketTime) {
|
|
208
|
+
const recentDayList = getVisitStatsRecentDayList(now);
|
|
209
|
+
const productKeySet = new Set();
|
|
210
|
+
|
|
211
|
+
for (const itemBucketDate of recentDayList) {
|
|
212
|
+
const productKeys = await befly.redis.smembers(getVisitStatsDayProductKeysKey(itemBucketDate));
|
|
213
|
+
|
|
214
|
+
for (const productKey of productKeys) {
|
|
215
|
+
if (productKey) {
|
|
216
|
+
productKeySet.add(productKey);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const bucketList = getVisitStatsBucketList(bucketDate, currentBucketTime);
|
|
222
|
+
const products = [];
|
|
223
|
+
|
|
224
|
+
for (const productKey of productKeySet) {
|
|
225
|
+
const meta = await getVisitStatsProductMeta(befly, productKey);
|
|
226
|
+
const trend = [];
|
|
227
|
+
const days = [];
|
|
228
|
+
let totalPv = 0;
|
|
229
|
+
let totalUv = 0;
|
|
230
|
+
|
|
231
|
+
for (const itemBucketTime of bucketList) {
|
|
232
|
+
trend.push({
|
|
233
|
+
bucketTime: itemBucketTime,
|
|
234
|
+
bucketDate: bucketDate,
|
|
235
|
+
pv: toVisitStatsNumber(await befly.redis.getString(getVisitStatsBucketProductPvKey(itemBucketTime, productKey))),
|
|
236
|
+
uv: await befly.redis.scard(getVisitStatsBucketProductUvKey(itemBucketTime, productKey))
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
for (const itemBucketDate of recentDayList) {
|
|
241
|
+
const pv = toVisitStatsNumber(await befly.redis.getString(getVisitStatsDayProductPvKey(itemBucketDate, productKey)));
|
|
242
|
+
const uv = await befly.redis.scard(getVisitStatsDayProductUvKey(itemBucketDate, productKey));
|
|
243
|
+
|
|
244
|
+
totalPv += pv;
|
|
245
|
+
totalUv += uv;
|
|
246
|
+
days.push({
|
|
247
|
+
bucketDate: itemBucketDate,
|
|
248
|
+
pv: pv,
|
|
249
|
+
uv: uv
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
products.push({
|
|
254
|
+
key: productKey,
|
|
255
|
+
productName: String(meta.productName || "Unknown"),
|
|
256
|
+
productCode: String(meta.productCode || "unknown"),
|
|
257
|
+
productVersion: String(meta.productVersion || "Unknown"),
|
|
258
|
+
today: days[days.length - 1] || {
|
|
259
|
+
bucketDate: bucketDate,
|
|
260
|
+
pv: 0,
|
|
261
|
+
uv: 0
|
|
262
|
+
},
|
|
263
|
+
trend: trend,
|
|
264
|
+
days: days,
|
|
265
|
+
totalPv: totalPv,
|
|
266
|
+
totalUv: totalUv
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
products.sort((a, b) => {
|
|
271
|
+
if (b.today.pv !== a.today.pv) {
|
|
272
|
+
return b.today.pv - a.today.pv;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (b.totalPv !== a.totalPv) {
|
|
276
|
+
return b.totalPv - a.totalPv;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return `${a.productCode}|${a.productVersion}`.localeCompare(`${b.productCode}|${b.productVersion}`);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
return products;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export default {
|
|
286
|
+
name: "获取访问统计",
|
|
287
|
+
method: "POST",
|
|
288
|
+
body: "none",
|
|
289
|
+
auth: true,
|
|
290
|
+
fields: {},
|
|
291
|
+
required: [],
|
|
292
|
+
handler: async (befly) => {
|
|
293
|
+
const now = Date.now();
|
|
294
|
+
await flushVisitStatsPreviousBucket(befly, now);
|
|
295
|
+
const tableReady = await ensureVisitStatsTableReady(befly);
|
|
296
|
+
|
|
297
|
+
const bucketTime = getTimeBucketStart(now, VISIT_STATS_BUCKET_MS);
|
|
298
|
+
const bucketDate = getDateYmdNumber(now);
|
|
299
|
+
const bucketKey = getVisitStatsBucketKey(bucketTime);
|
|
300
|
+
const dayKey = getVisitStatsDayKey(bucketDate);
|
|
301
|
+
|
|
302
|
+
const todayPv = toVisitStatsNumber(await befly.redis.getString(`${dayKey}:pv`));
|
|
303
|
+
const todayUv = await befly.redis.scard(`${dayKey}:uv`);
|
|
304
|
+
const trend = [];
|
|
305
|
+
const currentPv = toVisitStatsNumber(await befly.redis.getString(`${bucketKey}:pv`));
|
|
306
|
+
const currentUv = await befly.redis.scard(`${bucketKey}:uv`);
|
|
307
|
+
|
|
308
|
+
if (tableReady) {
|
|
309
|
+
const trendResult = await befly.mysql.getAll({
|
|
310
|
+
table: "beflyVisitStats",
|
|
311
|
+
where: { bucketDate: bucketDate },
|
|
312
|
+
orderBy: ["bucketTime#ASC"]
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
if (Array.isArray(trendResult.data?.lists)) {
|
|
316
|
+
trend.push(...trendResult.data.lists);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (currentPv > 0 || currentUv > 0) {
|
|
321
|
+
trend.push({
|
|
322
|
+
bucketTime: bucketTime,
|
|
323
|
+
bucketDate: bucketDate,
|
|
324
|
+
pv: currentPv,
|
|
325
|
+
uv: currentUv,
|
|
326
|
+
errorCount: 0,
|
|
327
|
+
durationSum: 0,
|
|
328
|
+
durationCount: 0,
|
|
329
|
+
avgDuration: 0
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const daysMap = new Map();
|
|
334
|
+
|
|
335
|
+
if (tableReady) {
|
|
336
|
+
const daysSqlRes = await befly.mysql.execute(`SELECT bucket_date as bucketDate, SUM(pv) as pv, SUM(uv) as uv FROM befly_visit_stats WHERE state = 1 GROUP BY bucket_date ORDER BY bucket_date DESC LIMIT ${VISIT_STATS_DAY_LIMIT}`, []);
|
|
337
|
+
|
|
338
|
+
for (const item of daysSqlRes.data || []) {
|
|
339
|
+
const itemBucketDate = toVisitStatsNumber(item.bucketDate);
|
|
340
|
+
daysMap.set(itemBucketDate, {
|
|
341
|
+
bucketDate: itemBucketDate,
|
|
342
|
+
pv: toVisitStatsNumber(item.pv),
|
|
343
|
+
uv: toVisitStatsNumber(item.uv)
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
daysMap.set(bucketDate, {
|
|
349
|
+
bucketDate: bucketDate,
|
|
350
|
+
pv: todayPv,
|
|
351
|
+
uv: todayUv
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
const days = [];
|
|
355
|
+
for (const item of daysMap.values()) {
|
|
356
|
+
days.push(item);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
days.sort((a, b) => a.bucketDate - b.bucketDate);
|
|
360
|
+
if (days.length > VISIT_STATS_DAY_LIMIT) {
|
|
361
|
+
days.splice(0, days.length - VISIT_STATS_DAY_LIMIT);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const onlineCount = await getVisitStatsOnlineCount(befly);
|
|
365
|
+
const uaStats = await getVisitStatsUaStats(befly, bucketDate);
|
|
366
|
+
const products = await getVisitStatsProducts(befly, now, bucketDate, bucketTime);
|
|
367
|
+
return befly.tool.Yes("获取成功", {
|
|
368
|
+
onlineCount: onlineCount,
|
|
369
|
+
today: {
|
|
370
|
+
bucketDate: bucketDate,
|
|
371
|
+
pv: todayPv,
|
|
372
|
+
uv: todayUv
|
|
373
|
+
},
|
|
374
|
+
trend: trend,
|
|
375
|
+
days: days,
|
|
376
|
+
uaStats: uaStats,
|
|
377
|
+
products: products
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
};
|
package/checks/config.js
CHANGED
package/configs/beflyConfig.json
CHANGED
package/configs/beflyMenus.json
CHANGED
|
@@ -65,20 +65,30 @@
|
|
|
65
65
|
"path": "/log",
|
|
66
66
|
"sort": 9003,
|
|
67
67
|
"children": [
|
|
68
|
+
{
|
|
69
|
+
"name": "访问统计",
|
|
70
|
+
"path": "/visit",
|
|
71
|
+
"sort": 1
|
|
72
|
+
},
|
|
68
73
|
{
|
|
69
74
|
"name": "登录日志",
|
|
70
75
|
"path": "/login",
|
|
71
|
-
"sort":
|
|
76
|
+
"sort": 2
|
|
72
77
|
},
|
|
73
78
|
{
|
|
74
79
|
"name": "邮件日志",
|
|
75
80
|
"path": "/email",
|
|
76
|
-
"sort":
|
|
81
|
+
"sort": 3
|
|
77
82
|
},
|
|
78
83
|
{
|
|
79
84
|
"name": "操作日志",
|
|
80
85
|
"path": "/operate",
|
|
81
|
-
"sort":
|
|
86
|
+
"sort": 4
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
"name": "错误报告",
|
|
90
|
+
"path": "/error",
|
|
91
|
+
"sort": 5
|
|
82
92
|
}
|
|
83
93
|
]
|
|
84
94
|
}
|
package/index.js
CHANGED
|
@@ -202,7 +202,7 @@ export class Befly {
|
|
|
202
202
|
|
|
203
203
|
// 启动 HTTP服务器
|
|
204
204
|
const apiFetch = apiHandler(this.apis, this.hooks, this.context);
|
|
205
|
-
const staticFetch = staticHandler(this.context.config.cors);
|
|
205
|
+
const staticFetch = staticHandler(this.context.config.cors, this.context.config.publicDir);
|
|
206
206
|
|
|
207
207
|
const server = Bun.serve({
|
|
208
208
|
port: this.context.config.appPort || 3000,
|
package/lib/logger.js
CHANGED
|
@@ -6,7 +6,7 @@ import { createWriteStream, existsSync, mkdirSync } from "node:fs";
|
|
|
6
6
|
import { stat } from "node:fs/promises";
|
|
7
7
|
import { join as nodePathJoin, resolve as nodePathResolve } from "node:path";
|
|
8
8
|
|
|
9
|
-
import { formatYmdHms } from "../utils/
|
|
9
|
+
import { formatYmdHms } from "../utils/datetime.js";
|
|
10
10
|
import { buildSensitiveKeyMatcher, sanitizeLogObject } from "../utils/loggerUtils.js";
|
|
11
11
|
import { isFiniteNumber, isNumber, isPlainObject, isString } from "../utils/is.js";
|
|
12
12
|
import { normalizePositiveInt } from "../utils/util.js";
|
|
@@ -236,7 +236,7 @@ class LogFileSink {
|
|
|
236
236
|
}
|
|
237
237
|
|
|
238
238
|
async ensureStreamReady(nextChunkBytes) {
|
|
239
|
-
const date = formatYmdHms(
|
|
239
|
+
const date = formatYmdHms(Date.now(), "date");
|
|
240
240
|
|
|
241
241
|
// 日期变化:切新文件
|
|
242
242
|
if (this.stream && this.streamDate && date !== this.streamDate) {
|
|
@@ -403,7 +403,7 @@ export function setMockLogger(mock) {
|
|
|
403
403
|
function buildJsonLine(level, timeMs, record) {
|
|
404
404
|
const base = {
|
|
405
405
|
level: level,
|
|
406
|
-
time: formatYmdHms(
|
|
406
|
+
time: formatYmdHms(timeMs),
|
|
407
407
|
pid: process.pid
|
|
408
408
|
};
|
|
409
409
|
|
package/lib/redisHelper.js
CHANGED
|
@@ -169,6 +169,22 @@ export class RedisHelper {
|
|
|
169
169
|
}
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
+
/**
|
|
173
|
+
* 原子按指定值自增
|
|
174
|
+
* @param key - 键名
|
|
175
|
+
* @param value - 自增值
|
|
176
|
+
* @returns 自增后的值
|
|
177
|
+
*/
|
|
178
|
+
async incrBy(key, value) {
|
|
179
|
+
try {
|
|
180
|
+
const pkey = `${this.prefix}${key}`;
|
|
181
|
+
return await this.client.incrby(pkey, value);
|
|
182
|
+
} catch (error) {
|
|
183
|
+
Logger.error("Redis incrBy 错误", error);
|
|
184
|
+
return 0;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
172
188
|
/**
|
|
173
189
|
* 原子自增并在首次自增时设置过期时间(常用于限流/计数)
|
|
174
190
|
* @param key - 键名
|
|
@@ -305,6 +321,28 @@ export class RedisHelper {
|
|
|
305
321
|
}
|
|
306
322
|
}
|
|
307
323
|
|
|
324
|
+
/**
|
|
325
|
+
* 从 Set 中删除一个或多个成员
|
|
326
|
+
* @param key - 键名
|
|
327
|
+
* @param members - 成员数组
|
|
328
|
+
* @returns 删除的成员数量
|
|
329
|
+
*/
|
|
330
|
+
async srem(key, members) {
|
|
331
|
+
try {
|
|
332
|
+
if (members.length === 0) return 0;
|
|
333
|
+
|
|
334
|
+
const pkey = `${this.prefix}${key}`;
|
|
335
|
+
const args = [pkey];
|
|
336
|
+
for (const member of members) {
|
|
337
|
+
args.push(member);
|
|
338
|
+
}
|
|
339
|
+
return await this.client.srem.apply(this.client, args);
|
|
340
|
+
} catch (error) {
|
|
341
|
+
Logger.error("Redis srem 错误", error);
|
|
342
|
+
return 0;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
308
346
|
/**
|
|
309
347
|
* 批量向多个 Set 添加成员(利用 Bun Redis 自动管道优化)
|
|
310
348
|
* @param items - [{ key, members }] 数组
|
|
@@ -48,15 +48,6 @@ export function compileSelect(model, quoteIdent) {
|
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
if (model.groupBy.length > 0) {
|
|
52
|
-
const groupSql = model.groupBy.map((field) => escapeField(field, quoteIdent)).join(", ");
|
|
53
|
-
sql += ` GROUP BY ${groupSql}`;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (model.having.length > 0) {
|
|
57
|
-
sql += ` HAVING ${model.having.join(" AND ")}`;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
51
|
if (model.orderBy.length > 0) {
|
|
61
52
|
const orderSql = model.orderBy
|
|
62
53
|
.map((item) => {
|
package/lib/sqlBuilder/index.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { resolveQuoteIdent, normalizeLimitValue, normalizeOffsetValue } from "./util.js";
|
|
7
|
-
import {
|
|
7
|
+
import { appendJoinItem, appendOrderByItems, appendSelectItems, appendSelectRaw, appendWhereInput, appendWhereRaw, createWhereRoot, setFromValue } from "./parser.js";
|
|
8
8
|
import { compileCount, compileDelete, compileInsert, compileSelect, compileUpdate, compileWhere } from "./compiler.js";
|
|
9
9
|
import { toDeleteInSql, toUpdateCaseByIdSql } from "./batch.js";
|
|
10
10
|
|
|
@@ -15,8 +15,6 @@ function createModel() {
|
|
|
15
15
|
where: createWhereRoot(),
|
|
16
16
|
joins: [],
|
|
17
17
|
orderBy: [],
|
|
18
|
-
groupBy: [],
|
|
19
|
-
having: [],
|
|
20
18
|
limit: null,
|
|
21
19
|
offset: null
|
|
22
20
|
};
|
|
@@ -131,22 +129,6 @@ export class SqlBuilder {
|
|
|
131
129
|
return this;
|
|
132
130
|
}
|
|
133
131
|
|
|
134
|
-
/**
|
|
135
|
-
* GROUP BY
|
|
136
|
-
*/
|
|
137
|
-
groupBy(field) {
|
|
138
|
-
appendGroupByItems(this._model.groupBy, field);
|
|
139
|
-
return this;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* HAVING
|
|
144
|
-
*/
|
|
145
|
-
having(condition) {
|
|
146
|
-
appendHavingItems(this._model.having, condition);
|
|
147
|
-
return this;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
132
|
/**
|
|
151
133
|
* LIMIT
|
|
152
134
|
*/
|
package/lib/sqlBuilder/parser.js
CHANGED
|
@@ -105,27 +105,6 @@ export function appendOrderByItems(list, fields) {
|
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
-
export function appendGroupByItems(list, field) {
|
|
109
|
-
if (Array.isArray(field)) {
|
|
110
|
-
for (const item of field) {
|
|
111
|
-
if (isString(item)) {
|
|
112
|
-
list.push(item);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (isString(field)) {
|
|
119
|
-
list.push(field);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export function appendHavingItems(list, condition) {
|
|
124
|
-
if (isString(condition)) {
|
|
125
|
-
list.push(condition);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
108
|
export function appendWhereInput(root, conditionOrField, value) {
|
|
130
109
|
if (conditionOrField && typeof conditionOrField === "object" && !Array.isArray(conditionOrField)) {
|
|
131
110
|
const node = parseWhereObject(conditionOrField);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly",
|
|
3
|
-
"version": "3.19.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "3.19.5",
|
|
4
|
+
"gitHead": "ba7fa7cc0534c48de2df4970d69a14853cf6b2a6",
|
|
5
5
|
"private": false,
|
|
6
6
|
"description": "Befly - 为 Bun 专属打造的 JavaScript API 接口框架核心引擎",
|
|
7
7
|
"keywords": [
|
|
@@ -52,8 +52,8 @@
|
|
|
52
52
|
"test": "bun test"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"fast-xml-parser": "^5.4
|
|
56
|
-
"nodemailer": "^8.0.
|
|
55
|
+
"fast-xml-parser": "^5.5.4",
|
|
56
|
+
"nodemailer": "^8.0.2",
|
|
57
57
|
"pathe": "^2.0.3",
|
|
58
58
|
"ua-parser-js": "^2.0.9",
|
|
59
59
|
"zod": "^4.0.0"
|
package/paths.js
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
import { fileURLToPath } from "node:url";
|
|
14
14
|
|
|
15
|
-
import { dirname, join, normalize } from "pathe";
|
|
15
|
+
import { dirname, isAbsolute, join, normalize, resolve } from "pathe";
|
|
16
16
|
|
|
17
17
|
// 当前文件的路径信息
|
|
18
18
|
const __filename = fileURLToPath(import.meta.url);
|
|
@@ -110,3 +110,16 @@ export const appApiDir = join(appDir, "apis");
|
|
|
110
110
|
* @usage 存放用户业务表定义(JSON 格式)
|
|
111
111
|
*/
|
|
112
112
|
export const appTableDir = join(appDir, "tables");
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 项目公共静态目录
|
|
116
|
+
* @description 默认 {appDir}/public,可通过 config.publicDir 覆盖
|
|
117
|
+
* @usage 用于静态文件访问与本地上传保存目录解析
|
|
118
|
+
*/
|
|
119
|
+
export function getAppPublicDir(publicDir = "./public") {
|
|
120
|
+
if (isAbsolute(publicDir)) {
|
|
121
|
+
return resolve(publicDir);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return resolve(appDir, publicDir);
|
|
125
|
+
}
|
package/router/static.js
CHANGED
|
@@ -8,19 +8,19 @@ import { join } from "pathe";
|
|
|
8
8
|
|
|
9
9
|
import { Logger } from "../lib/logger.js";
|
|
10
10
|
// 相对导入
|
|
11
|
-
import {
|
|
11
|
+
import { getAppPublicDir } from "../paths.js";
|
|
12
12
|
import { setCorsOptions } from "../utils/cors.js";
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* 静态文件处理器工厂
|
|
16
16
|
*/
|
|
17
|
-
export function staticHandler(corsConfig = undefined) {
|
|
17
|
+
export function staticHandler(corsConfig = undefined, publicDir = "./public") {
|
|
18
18
|
return async (req) => {
|
|
19
19
|
// 设置 CORS 响应头
|
|
20
20
|
const corsHeaders = setCorsOptions(req, corsConfig);
|
|
21
21
|
|
|
22
22
|
const url = new URL(req.url);
|
|
23
|
-
const filePath = join(
|
|
23
|
+
const filePath = join(getAppPublicDir(publicDir), url.pathname);
|
|
24
24
|
|
|
25
25
|
try {
|
|
26
26
|
// OPTIONS预检请求
|