()
| 197 | } |
| 198 | |
| 199 | async setup() { |
| 200 | if (this.serverInfo) throw new Error('Server already set up'); |
| 201 | const app = new Hono(); |
| 202 | app.get('/', (c) => { |
| 203 | const key = c.req.query('key') || ''; |
| 204 | if (!this.providers[key]) return c.notFound(); |
| 205 | return c.html(this.html); |
| 206 | }); |
| 207 | app.get('/~data', async (c) => { |
| 208 | const key = c.req.query('key') || ''; |
| 209 | const provider = this.providers[key]; |
| 210 | if (!provider) return c.json({}, 404); |
| 211 | const query = Object.fromEntries( |
| 212 | ['content', 'line'].map((key) => [key, +(c.req.query(key) || '') || 0]), |
| 213 | ); |
| 214 | await provider.getUpdate(query); |
| 215 | const updatedKeys = Object.keys(query).filter( |
| 216 | (key) => query[key] < provider.state[key].ts, |
| 217 | ); |
| 218 | const result = Object.fromEntries( |
| 219 | updatedKeys.map((key) => { |
| 220 | let data = provider.state[key]; |
| 221 | if (key === 'content') { |
| 222 | const result = this.transformer.transform(data.value as string); |
| 223 | data = { |
| 224 | ...data, |
| 225 | value: { |
| 226 | frontmatter: result.frontmatter, |
| 227 | root: result.root, |
| 228 | }, |
| 229 | }; |
| 230 | } |
| 231 | return [key, data]; |
| 232 | }), |
| 233 | ); |
| 234 | return c.json(result); |
| 235 | }); |
| 236 | app.post('/~api', async (c) => { |
| 237 | const key = c.req.query('key') || ''; |
| 238 | const provider = this.providers[key]; |
| 239 | if (!provider) return c.json({}, 404); |
| 240 | const { cmd, args } = await c.req.json(); |
| 241 | await provider[cmd]?.(...args); |
| 242 | return c.body(null, 204); |
| 243 | }); |
| 244 | const { distDir, assetsDir } = config; |
| 245 | app.get('/~client.*', async (c) => { |
| 246 | const realpath = join(distDir, c.req.path.slice(2)); |
| 247 | return sendStatic(c, realpath); |
| 248 | }); |
| 249 | app.get(`${ASSETS_PREFIX}*`, async (c) => { |
| 250 | const relpath = c.req.path.slice(ASSETS_PREFIX.length); |
| 251 | const realpath = join(assetsDir, relpath); |
| 252 | return sendStatic(c, realpath); |
| 253 | }); |
| 254 | |
| 255 | const deferred = defer<AddressInfo>(); |
| 256 | const server = serve( |
no test coverage detected