|
| 1 | +import { Router, Request, Response } from 'express'; |
| 2 | +import { GIFGenerator } from '../gif-generator.js'; |
| 3 | +import { compileProject } from '../compile.js'; |
| 4 | +import { CompileError } from '../errors.js'; |
| 5 | + |
| 6 | +const router = Router(); |
| 7 | + |
| 8 | +type Format = 'gif' | 'mp4'; |
| 9 | + |
| 10 | +function readSource(req: Request): { code: string; lang: string } | null { |
| 11 | + if (typeof req.body === 'string') { |
| 12 | + return req.body.trim() ? { code: req.body, lang: 'basic' } : null; |
| 13 | + } |
| 14 | + const body = req.body ?? {}; |
| 15 | + const code: unknown = body.code; |
| 16 | + const lang: unknown = body.lang; |
| 17 | + if (typeof code === 'string' && code.trim()) { |
| 18 | + return { code, lang: typeof lang === 'string' && lang ? lang : 'basic' }; |
| 19 | + } |
| 20 | + return null; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Compile inline source for a given language and render it. Generalises the |
| 25 | + * BASIC route via compileProject, so the bot can render pasted assembly |
| 26 | + * (lang=asm, pasmo) as well as BASIC. A compile failure responds 400 with the |
| 27 | + * compiler messages. |
| 28 | + */ |
| 29 | +async function handle(format: Format, req: Request, res: Response): Promise<void> { |
| 30 | + try { |
| 31 | + const source = readSource(req); |
| 32 | + if (!source) { |
| 33 | + res.status(400).json({ error: 'No source provided' }); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + const maxSeconds = parseInt(req.query.maxSeconds as string) || 30; |
| 38 | + const staleThreshold = parseInt(req.query.staleThreshold as string) || 150; |
| 39 | + const machineType = parseInt(req.query.machineType as string) || 48; |
| 40 | + const scale = parseInt(req.query.scale as string) || (format === 'mp4' ? 4 : 2); |
| 41 | + |
| 42 | + let tap: Buffer; |
| 43 | + try { |
| 44 | + tap = await compileProject(source.lang, source.code); |
| 45 | + } catch (err) { |
| 46 | + if (err instanceof CompileError) { |
| 47 | + res.status(400).json({ error: 'Compilation failed', detail: err.message }); |
| 48 | + return; |
| 49 | + } |
| 50 | + throw err; |
| 51 | + } |
| 52 | + |
| 53 | + const generator = new GIFGenerator({ |
| 54 | + maxDurationMs: maxSeconds * 1000, |
| 55 | + staleFrameThreshold: staleThreshold, |
| 56 | + scale, |
| 57 | + }); |
| 58 | + await generator.initialize(); |
| 59 | + |
| 60 | + console.log( |
| 61 | + `Generating ${format.toUpperCase()} from ${source.code.length} bytes of ${source.lang}...`, |
| 62 | + ); |
| 63 | + if (format === 'mp4') { |
| 64 | + const mp4 = await generator.generateMp4FromTAP(tap, machineType); |
| 65 | + res.setHeader('Content-Type', 'video/mp4'); |
| 66 | + res.send(mp4); |
| 67 | + } else { |
| 68 | + const gif = await generator.generateFromTAP(tap, machineType); |
| 69 | + res.setHeader('Content-Type', 'image/gif'); |
| 70 | + res.send(gif); |
| 71 | + } |
| 72 | + } catch (error: any) { |
| 73 | + console.error(`Error generating ${format} from source:`, error); |
| 74 | + res.status(500).json({ error: error.message }); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +router.post('/source-to-gif', (req, res) => handle('gif', req, res)); |
| 79 | +router.post('/source-to-mp4', (req, res) => handle('mp4', req, res)); |
| 80 | + |
| 81 | +export default router; |
0 commit comments