@bbki.ng/backend 0.3.12 → 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
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bbki.ng/backend",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.14",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@simplewebauthn/server": "13.2.2",
|
|
7
|
-
"hono": "^4.10.7"
|
|
7
|
+
"hono": "^4.10.7",
|
|
8
|
+
"showdown": "^2.1.0"
|
|
8
9
|
},
|
|
9
10
|
"devDependencies": {
|
|
10
11
|
"@cloudflare/workers-types": "4.20251128.0",
|
|
11
12
|
"@eslint/compat": "^1.0.0",
|
|
12
13
|
"@eslint/js": "^8.57.0",
|
|
13
14
|
"@types/node": "^20.0.0",
|
|
15
|
+
"@types/showdown": "^2.0.6",
|
|
14
16
|
"@typescript-eslint/eslint-plugin": "^7.0.0",
|
|
15
17
|
"@typescript-eslint/parser": "^7.0.0",
|
|
16
18
|
"eslint": "^8.57.0",
|
package/src/config/app.config.ts
CHANGED
|
@@ -12,10 +12,7 @@ const app = new Hono<{ Bindings: Bindings }>();
|
|
|
12
12
|
app.use(
|
|
13
13
|
'*',
|
|
14
14
|
cors({
|
|
15
|
-
origin:
|
|
16
|
-
// Allow bbki.ng and any localhost port
|
|
17
|
-
return /^https:\/\/bbki\.ng$|^http:\/\/localhost(:\d+)?$/.test(origin) ? origin : null;
|
|
18
|
-
},
|
|
15
|
+
origin: '*',
|
|
19
16
|
})
|
|
20
17
|
);
|
|
21
18
|
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { Context } from 'hono';
|
|
2
2
|
import { HTTPException } from 'hono/http-exception';
|
|
3
|
+
import showdown from 'showdown';
|
|
4
|
+
|
|
5
|
+
const converter = new showdown.Converter();
|
|
6
|
+
converter.setFlavor('github');
|
|
3
7
|
|
|
4
8
|
interface AddPostRequest {
|
|
5
9
|
title: string;
|
|
@@ -13,7 +17,7 @@ const MAX_CONTENT_LENGTH = 100000;
|
|
|
13
17
|
export const addPost = async (c: Context) => {
|
|
14
18
|
try {
|
|
15
19
|
// Parse and validate request body
|
|
16
|
-
const body = await c.req.json
|
|
20
|
+
const body = (await c.req.json()) as AddPostRequest;
|
|
17
21
|
|
|
18
22
|
if (!body.title || typeof body.title !== 'string') {
|
|
19
23
|
throw new HTTPException(400, { message: 'Title is required' });
|
|
@@ -44,32 +48,48 @@ export const addPost = async (c: Context) => {
|
|
|
44
48
|
|
|
45
49
|
const author = (body.author ?? 'bbki.ng').trim();
|
|
46
50
|
|
|
51
|
+
// Convert markdown to HTML
|
|
52
|
+
const html = converter.makeHtml(content);
|
|
53
|
+
const wrappedHtml = html.includes('<p>') ? html : `<p>${html}</p>`;
|
|
54
|
+
|
|
47
55
|
// Check database availability
|
|
48
56
|
if (!c.env?.DB) {
|
|
49
57
|
throw new HTTPException(503, { message: 'Database unavailable' });
|
|
50
58
|
}
|
|
51
59
|
|
|
52
|
-
// Insert post
|
|
53
|
-
const id = crypto.randomUUID();
|
|
54
60
|
const now = new Date().toISOString();
|
|
55
61
|
|
|
56
|
-
|
|
62
|
+
// Check if post with same title already exists (upsert)
|
|
63
|
+
const existingPost = (await c.env.DB.prepare('SELECT id FROM posts WHERE title = ?')
|
|
64
|
+
.bind(title)
|
|
65
|
+
.first()) as { id: string } | null;
|
|
66
|
+
|
|
67
|
+
if (existingPost) {
|
|
57
68
|
await c.env.DB.prepare(
|
|
58
|
-
'
|
|
69
|
+
'UPDATE posts SET content = ?, author = ?, updated_at = ? WHERE id = ?'
|
|
59
70
|
)
|
|
60
|
-
.bind(
|
|
71
|
+
.bind(wrappedHtml, author, now, existingPost.id)
|
|
61
72
|
.run();
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
73
|
+
|
|
74
|
+
return c.json({
|
|
75
|
+
status: 'success',
|
|
76
|
+
data: { id: existingPost.id, title, content: wrappedHtml, author, updatedAt: now },
|
|
77
|
+
});
|
|
67
78
|
}
|
|
68
79
|
|
|
80
|
+
// Insert new post
|
|
81
|
+
const id = crypto.randomUUID();
|
|
82
|
+
|
|
83
|
+
await c.env.DB.prepare(
|
|
84
|
+
'INSERT INTO posts (id, title, content, author, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)'
|
|
85
|
+
)
|
|
86
|
+
.bind(id, title, wrappedHtml, author, now, now)
|
|
87
|
+
.run();
|
|
88
|
+
|
|
69
89
|
return c.json(
|
|
70
90
|
{
|
|
71
91
|
status: 'success',
|
|
72
|
-
data: { id, title, content, author, createdAt: now, updatedAt: now },
|
|
92
|
+
data: { id, title, content: wrappedHtml, author, createdAt: now, updatedAt: now },
|
|
73
93
|
},
|
|
74
94
|
201
|
|
75
95
|
);
|
|
@@ -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
|
+
};
|