Skip to content

Commit 948d3db

Browse files
committed
next ps
Signed-off-by: Innei <tukon479@gmail.com>
1 parent 5aacde8 commit 948d3db

File tree

2 files changed

+67
-61
lines changed

2 files changed

+67
-61
lines changed

global.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ declare interface IDb {
6363
declare interface IStorage {
6464
db: IDb
6565
cache: {
66-
get(key: string): Promise<string>
66+
get<T>(key: string): Promise<T>
6767
set(key: string, value: string | object, ttl?: number): Promise<string>
6868
del(key: string): Promise<string>
6969
}

shiro/functions/ps.ts

Lines changed: 66 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,85 @@
1+
function assetAuth(ctx: Context) {
2+
const body = ctx.req.body
3+
const authKey = ctx.secret.key
4+
if (ctx.isAuthenticated) return
5+
if (body.key !== authKey) {
6+
ctx.throws(401, 'Unauthorized')
7+
}
8+
}
9+
110
export default async function handler(ctx: Context) {
2-
const {
3-
timestamp,
4-
process: processName,
5-
key,
6-
media,
7-
meta,
8-
} = ctx.req.body || {}
9-
// handle GET
10-
{
11-
const [processInfo, mediaInfo] = await Promise.all([
12-
ctx.storage.cache.get('ps') as any as Promise<Process | undefined>,
13-
ctx.storage.cache.get('media') as any as Promise<Media | undefined>,
14-
])
15-
if (!key) {
16-
return {
17-
processName: processInfo?.name,
18-
processInfo,
19-
mediaInfo,
20-
}
11+
const method = ctx.req.method.toLowerCase()
12+
13+
switch (method) {
14+
case 'get': {
15+
return GET(ctx)
16+
}
17+
case 'post': {
18+
assetAuth(ctx)
19+
return POST(ctx)
20+
}
21+
22+
default: {
23+
ctx.throws(405, 'Method Not Allowed')
2124
}
2225
}
26+
}
2327

24-
const ts = +new Date()
25-
// if (Math.abs(ts - timestamp) > 1000 * 10) {
26-
// ctx.throws(400, 'this request is outdate')
27-
// return
28-
// }
28+
interface Media {
29+
title: string
30+
artist: string
31+
}
2932

30-
const processInfo: Process = {
31-
name: processName,
32-
...meta,
33+
interface Process {
34+
name: string
35+
iconBase64?: string
36+
iconUrl?: string
37+
description?: string
38+
}
39+
40+
async function GET(ctx: Context) {
41+
const [processInfo, mediaInfo] = await Promise.all([
42+
ctx.storage.cache.get<Process>('ps'),
43+
ctx.storage.cache.get<Media>('media'),
44+
])
45+
46+
return {
47+
processName: processInfo?.name,
48+
processInfo,
49+
mediaInfo,
3350
}
51+
}
52+
async function POST(ctx: Context) {
53+
const [cachedProcessInfo, cachedMediaInfo] = await Promise.all([
54+
ctx.storage.cache.get<Process>('ps'),
55+
ctx.storage.cache.get<Media>('media'),
56+
])
3457

35-
const validKey = (await ctx.secret.key) || 'testing'
36-
if (key != validKey)
37-
ctx.throws(401, "You haven't permission to update process info")
58+
const ts = +new Date()
3859

39-
const originalPsInfo: Process | null = (await ctx.storage.cache.get(
40-
'ps',
41-
)) as any
42-
await ctx.storage.cache.set('ps', processInfo, 300)
60+
const psInfo = ctx.req.body.process as Process
61+
const mediaInfo = ctx.req.body.media as Media
4362

44-
if (originalPsInfo?.name !== processName)
63+
if (psInfo?.name !== cachedProcessInfo?.name)
4564
ctx?.broadcast?.('ps-update', {
46-
processInfo,
47-
process: processInfo.name,
65+
processInfo: psInfo,
66+
process: psInfo.name,
4867
ts,
4968
})
50-
if (media) {
51-
await ctx.storage.cache.set('media', media, 10)
52-
}
5369

54-
const mediaInfo: Media | undefined = (await ctx.storage.cache.get(
55-
'media',
56-
)) as any
57-
if (mediaInfo?.title !== media?.title)
58-
ctx?.broadcast?.('media-update', media || null)
70+
if (cachedMediaInfo?.title !== mediaInfo?.title)
71+
ctx?.broadcast?.('media-update', mediaInfo || null)
72+
73+
if (mediaInfo) {
74+
await ctx.storage.cache.set('media', mediaInfo, 10)
75+
}
76+
await ctx.storage.cache.set('ps', psInfo, 300)
5977

6078
return {
6179
ok: 1,
62-
mediaInfo,
63-
process: processInfo.name,
64-
processInfo,
80+
mediaInfo: cachedMediaInfo,
81+
process: cachedProcessInfo.name,
82+
processInfo: cachedProcessInfo,
6583
timestamp: +new Date(),
6684
}
6785
}
68-
69-
interface Media {
70-
title: string
71-
artist: string
72-
}
73-
74-
interface Process {
75-
name: string
76-
iconBase64?: string
77-
iconUrl?: string
78-
description?: string
79-
}

0 commit comments

Comments
 (0)