|
| 1 | +import Database from 'better-sqlite3'; |
| 2 | +import { parse } from 'url'; |
| 3 | + |
| 4 | +import type { Plugin } from 'vite'; |
| 5 | + |
| 6 | +/** |
| 7 | + * # Glyph API Vite Plugin |
| 8 | + * |
| 9 | + * Exposes a lightweight SQLite glyph API at `/api/glyphs/:fontName` |
| 10 | + * @returns Vite plugin |
| 11 | + */ |
| 12 | +export function glyphApi(): Plugin { |
| 13 | + return { |
| 14 | + name: 'glyph-api', |
| 15 | + /** |
| 16 | + * Modify the server middleware |
| 17 | + * @param server - Vite server |
| 18 | + */ |
| 19 | + configureServer(server): void { |
| 20 | + server.middlewares.use((req, res, next) => { |
| 21 | + if (req.url === undefined) { |
| 22 | + next(); |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + const { pathname, query } = parse(req.url, true); |
| 27 | + |
| 28 | + if (pathname === null || !pathname.startsWith('/api/glyphs/')) { |
| 29 | + next(); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + const fontName = pathname.replace('/api/glyphs/', '').split('.')[0]; |
| 34 | + const { type, codes } = query as { type?: string; codes?: string }; |
| 35 | + |
| 36 | + if (fontName.length === 0 || (type !== 'metadata' && type !== 'glyph')) { |
| 37 | + res.statusCode = 400; |
| 38 | + res.end(JSON.stringify({ err: 'Invalid parameters' })); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + const db = new Database('./public/glyphs/GLYPHS.sqlite3', { readonly: true }); |
| 43 | + |
| 44 | + try { |
| 45 | + if (type === 'metadata') { |
| 46 | + const row = db.prepare('SELECT data FROM metadata WHERE name = ?').get(fontName) as |
| 47 | + | { data: string } |
| 48 | + | undefined; |
| 49 | + if (row === undefined) { |
| 50 | + res.statusCode = 404; |
| 51 | + res.end(JSON.stringify({ err: 'Metadata not found' })); |
| 52 | + return; |
| 53 | + } |
| 54 | + res.setHeader('Content-Type', 'application/x-protobuf'); |
| 55 | + res.end(Buffer.from(row.data, 'base64')); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (type === 'glyph') { |
| 60 | + const pieces = codes?.split(',') ?? []; |
| 61 | + const stmt = db.prepare('SELECT data FROM glyph_multi WHERE name = ? AND code = ?'); |
| 62 | + const buffers: Buffer[] = []; |
| 63 | + |
| 64 | + /** |
| 65 | + * Convert a base36 string to a number |
| 66 | + * @param num - base36 string |
| 67 | + * @returns number |
| 68 | + */ |
| 69 | + const base36 = (num: string) => parseInt(num, 36); |
| 70 | + |
| 71 | + for (const piece of pieces) { |
| 72 | + if (piece.includes('-')) { |
| 73 | + const [from, to] = piece.split('-').map(base36); |
| 74 | + for (let i = from; i <= to; i++) { |
| 75 | + const row = stmt.get(fontName, String(i)) as { data: string } | undefined; |
| 76 | + if (row !== undefined) { |
| 77 | + buffers.push(Buffer.from(row.data, 'base64')); |
| 78 | + } |
| 79 | + } |
| 80 | + } else { |
| 81 | + const code = piece.includes('.') ? piece : String(base36(piece)); |
| 82 | + const row = stmt.get(fontName, code) as { data: string } | undefined; |
| 83 | + if (row !== undefined) { |
| 84 | + buffers.push(Buffer.from(row.data, 'base64')); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + res.setHeader('Content-Type', 'application/x-protobuf'); |
| 90 | + res.end(Buffer.concat(buffers)); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + res.statusCode = 404; |
| 95 | + res.end(JSON.stringify({ err: 'Unknown request' })); |
| 96 | + } catch (_) { |
| 97 | + res.statusCode = 500; |
| 98 | + res.end(JSON.stringify({ err: 'Internal error' })); |
| 99 | + } finally { |
| 100 | + db.close(); |
| 101 | + } |
| 102 | + }); |
| 103 | + }, |
| 104 | + }; |
| 105 | +} |
0 commit comments