| 43 | ) |
| 44 | |
| 45 | const parseRequest = async ( |
| 46 | c: Context |
| 47 | ): Promise<ValidRequest | InvalidRequest> => { |
| 48 | const url = new URL(c.req.url) |
| 49 | const room = url.searchParams.get(`room`) |
| 50 | |
| 51 | if (!room) { |
| 52 | return { isValid: false, error: `Room is required` } |
| 53 | } |
| 54 | |
| 55 | const client_id = url.searchParams.get(`client_id`) ?? undefined |
| 56 | |
| 57 | // Get update binary from request body |
| 58 | const arrayBuffer = await c.req.arrayBuffer() |
| 59 | const update = new Uint8Array(arrayBuffer) |
| 60 | |
| 61 | if (update.length === 0) { |
| 62 | return { isValid: false, error: `No update provided` } |
| 63 | } |
| 64 | if (client_id) { |
| 65 | return { isValid: true, room, client_id, update } |
| 66 | } else { |
| 67 | return { isValid: true, room, update } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | app.put(`/api/update`, async (c: Context) => { |
| 72 | try { |