@bbki.ng/backend 0.3.13 → 0.3.14
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/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -5,9 +5,12 @@ import { addPost } from '../controllers/posts/add.controller';
|
|
|
5
5
|
import { updatePost } from '../controllers/posts/update.controller';
|
|
6
6
|
import { removePost } from '../controllers/posts/remove.controller';
|
|
7
7
|
import { requireAuth } from '../utils/auth';
|
|
8
|
+
import { trackDeviceActivity } from '../utils/deviceActivity';
|
|
8
9
|
|
|
9
10
|
const postsRouter = new Hono();
|
|
10
11
|
|
|
12
|
+
postsRouter.use('*', trackDeviceActivity);
|
|
13
|
+
|
|
11
14
|
// Public routes
|
|
12
15
|
postsRouter.get('/', listPosts);
|
|
13
16
|
postsRouter.get('/:title', getPost);
|
|
@@ -2,9 +2,12 @@ import { Hono } from 'hono';
|
|
|
2
2
|
import { listStreaming } from '../controllers/streaming/list.controller';
|
|
3
3
|
import { addStreaming } from '../controllers/streaming/add.controller';
|
|
4
4
|
import { removeStreaming } from '../controllers/streaming/remove.controller';
|
|
5
|
+
import { trackDeviceActivity } from '../utils/deviceActivity';
|
|
5
6
|
|
|
6
7
|
const streamingRouter = new Hono();
|
|
7
8
|
|
|
9
|
+
streamingRouter.use('*', trackDeviceActivity);
|
|
10
|
+
|
|
8
11
|
streamingRouter.get('/', listStreaming);
|
|
9
12
|
streamingRouter.post('/', addStreaming);
|
|
10
13
|
streamingRouter.delete('/:id', removeStreaming);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Context } from 'hono';
|
|
2
|
+
|
|
3
|
+
interface DeviceActivity {
|
|
4
|
+
lastActiveAt: string;
|
|
5
|
+
paths: string[]; // 最近访问路径(保留最近10条)
|
|
6
|
+
visitCount: number; // 总访问次数
|
|
7
|
+
firstSeenAt: string; // 首次访问时间
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const trackDeviceActivity = async (c: Context, next: () => Promise<void>) => {
|
|
11
|
+
// HTTP headers are case-insensitive, Hono normalizes them
|
|
12
|
+
const fingerprint = c.req.header('X-Device-Fingerprint');
|
|
13
|
+
|
|
14
|
+
if (fingerprint) {
|
|
15
|
+
const key = `device:${fingerprint}:activity`;
|
|
16
|
+
const existing = await c.env.KV.get(key);
|
|
17
|
+
|
|
18
|
+
let activity: DeviceActivity;
|
|
19
|
+
const now = new Date().toISOString();
|
|
20
|
+
const currentPath = c.req.path;
|
|
21
|
+
|
|
22
|
+
if (existing) {
|
|
23
|
+
activity = JSON.parse(existing);
|
|
24
|
+
activity.lastActiveAt = now;
|
|
25
|
+
activity.visitCount += 1;
|
|
26
|
+
// 保留最近10条路径,避免数据过大
|
|
27
|
+
activity.paths = [currentPath, ...activity.paths].slice(0, 10);
|
|
28
|
+
} else {
|
|
29
|
+
activity = {
|
|
30
|
+
lastActiveAt: now,
|
|
31
|
+
firstSeenAt: now,
|
|
32
|
+
visitCount: 1,
|
|
33
|
+
paths: [currentPath],
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 存储30天
|
|
38
|
+
await c.env.KV.put(key, JSON.stringify(activity), {
|
|
39
|
+
expirationTtl: 86400 * 30,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
await next();
|
|
44
|
+
};
|