25 lines
1.2 KiB
TypeScript
25 lines
1.2 KiB
TypeScript
|
|
// Publish a component to the library, with everything it uses. The server
|
||
|
|
// counts its transistors and pins itself, so the listing can be trusted.
|
||
|
|
// Returns its id and the edit key that updates or removes it later.
|
||
|
|
import { describeBundle } from '../../../app/lib/logic/library'
|
||
|
|
|
||
|
|
export default defineEventHandler(async event => {
|
||
|
|
rateLimit(event, 'library', 30, 10 * 60_000)
|
||
|
|
const { player, bundle } = await readLibraryBody(event)
|
||
|
|
const storage = libraryStorage()
|
||
|
|
const key = newEditKey()
|
||
|
|
const meta = await locked('library', async () => {
|
||
|
|
const index = await libraryIndex()
|
||
|
|
if (index.length >= MAX_ENTRIES) throw createError({ statusCode: 507, message: 'The library is full for now' })
|
||
|
|
let id = newShareId()
|
||
|
|
while (await storage.hasItem(entryKey(id))) id = newShareId()
|
||
|
|
const now = Date.now()
|
||
|
|
const meta: LibraryItem = { id, ...describeBundle(bundle), version: 1, created: now, updated: now, player }
|
||
|
|
await storage.setItem(entryKey(id), { meta, bundle, keyHash: hashKey(key) })
|
||
|
|
await storage.setItem('index', [meta, ...index])
|
||
|
|
return meta
|
||
|
|
})
|
||
|
|
setResponseStatus(event, 201)
|
||
|
|
return { id: meta.id, key, version: meta.version }
|
||
|
|
})
|