Skip to content

Commit 8a6a66f

Browse files
committed
feat: add snippets
Signed-off-by: Innei <[email protected]>
1 parent 3882aa9 commit 8a6a66f

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

global.d.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// THIS FILE FOR MONACO EDITOR CODE COMPLETION
2-
2+
// TYPE DECLARATION FOR SERVERLESS FUNCTION
33
declare interface FunctionContextResponse {
44
throws(code: number, message: any): void
55
type(type: string): FunctionContextResponse
@@ -12,11 +12,23 @@ declare interface FunctionContextRequest {
1212
params: Record<string, string>
1313
}
1414

15-
declare interface FastifyRequest {
15+
declare interface FastifyRequest extends FunctionContextRequest {
16+
body: any
1617
[k: string]: any
1718
}
19+
20+
declare interface ServiceGetter {
21+
getService(name: 'http'): {
22+
axios: any
23+
requestWithCatch: (url: string) => Promise<any>
24+
}
25+
getService(name: 'config'): {
26+
get: (key: string) => Promise<any>
27+
}
28+
}
1829
declare interface Context extends FunctionContextResponse {}
1930
declare interface Context extends FunctionContextRequest {}
31+
declare interface Context extends ServiceGetter {}
2032
declare interface Context {
2133
req: FunctionContextRequest & FastifyRequest
2234
res: FunctionContextResponse
@@ -32,6 +44,8 @@ declare interface Context {
3244

3345
writeAsset: (path: string, data: any, options: any) => void
3446
readAsset: (path: string, options: any) => void
47+
48+
secret: Record<string, any>
3549
}
3650

3751
declare interface IDb {
@@ -47,7 +61,7 @@ declare interface IStorage {
4761
db: IDb
4862
cache: {
4963
get(key: string): Promise<string>
50-
set(key: string, value: string | object): Promise<string>
64+
set(key: string, value: string | object, ttl?: number): Promise<string>
5165
del(key: string): Promise<string>
5266
}
5367
dangerousAccessDbInstance: () => any

shiro/functions/ps.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
export default async function handler(ctx: Context) {
2+
const { timestamp, process, key, media } = ctx.req.body || {}
3+
4+
const [processName, mediaInfo] = await Promise.all([
5+
ctx.storage.cache.get('ps'),
6+
ctx.storage.cache.get('media').then((JSONString) => {
7+
if (JSONString) {
8+
try {
9+
return JSON.parse(JSONString)
10+
} catch {
11+
return undefined
12+
}
13+
}
14+
}),
15+
])
16+
17+
if (!key) {
18+
return {
19+
processName,
20+
mediaInfo,
21+
}
22+
}
23+
24+
const validKey = (await ctx.secret.key) || 'testing'
25+
if (key != validKey)
26+
ctx.throws(401, "You haven't permission to update process info")
27+
await ctx.storage.cache.set('ps', process, 60)
28+
const ts = +new Date()
29+
30+
if (process !== processName)
31+
ctx.broadcast('ps-update', {
32+
process,
33+
ts,
34+
})
35+
if (media) {
36+
await ctx.storage.cache.set('media', JSON.stringify(media), 10)
37+
38+
if (mediaInfo.title !== media.title) ctx.broadcast('media-update', media)
39+
}
40+
return {
41+
ok: 1,
42+
process,
43+
timestamp: +new Date(),
44+
}
45+
}

shiro/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "shiro",
3+
"dependencies": {}
4+
}

0 commit comments

Comments
 (0)