dev.ts
1 import { access, readFile } from 'node:fs/promises' 2 import { createSecureServer } from 'node:http2' 3 4 const exists = async (uri: string): boolean => { 5 try { 6 await access(uri) 7 return true 8 } catch { 9 return false 10 } 11 } 12 export const dev = async (response: string): Promise<void> => { 13 const cert = { 14 cert: await exists('localhost-cert.pem') 15 ? await readFile('localhost-cert.pem') 16 : throw new Error("missing cert"), 17 key: await exists('localhost-privkey.pem') 18 ? await readFile('localhost-privkey.pem') 19 : throw new Error("missing privkey") 20 } 21 const server = createSecureServer(cert) 22 23 server.on('error', (e) => console.error(e)) 24 server.on('stream', (stream) => { 25 stream.respond({ 26 ':status': 200, 27 'content-type': 'text/html; charset=utf-8' 28 }) 29 stream.end(response) 30 }) 31 32 server.listen(8443) 33 }