-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbun.ts
52 lines (45 loc) · 1.52 KB
/
bun.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { Hono } from 'hono'
import { logger } from 'hono/logger'
import { timing } from 'hono/timing'
import { zValidator } from '@hono/zod-validator'
import { ThirdwebStorage } from "@thirdweb-dev/storage"
import * as z from 'zod'
import { runJs, saveSecret, secretSchema, getSecret, visitSecretSchema } from './src/app'
const app = new Hono()
app.use('*', logger())
app.use('*', timing())
app.all('/run_js_from_ipfs/:cid{[a-zA-Z0-9\/]+}', runJs)
app.all('/ipfs/:cid{[a-zA-Z0-9\/]+}', runJs)
app.post('/vaults', zValidator('json', secretSchema), saveSecret)
app.get('/vaults/:key/:token', zValidator('param', visitSecretSchema), getSecret)
const uploadSchema = z.object({
file: z
.custom<File>(i => i instanceof File, 'You need upload a file with name "file"')
.refine(
// max file size: 500kb
f => f.size < 1024 * 1024 * 500,
{ message: 'File size should be less than 500kb' }
)
.refine(
// only allow text
f => f.type.startsWith('text/'),
{ message: 'Only allow plain javascript file' }
)
})
app.post('/ipfs', async (c) => {
const body = await c.req.parseBody()
const result = uploadSchema.safeParse(body)
if (!result.success) {
return c.json({ error: result.error?.flatten() }, 400)
}
const plain = await result.data.file.text()
const storage = new ThirdwebStorage({
secretKey: process.env.THIRDWEB_SECRET,
})
const uri = await storage.upload(plain);
return c.json({ uri })
})
export default {
port: Number(process.env.PORT) || 3000,
fetch: app.fetch,
}