← back to Cred Launcher
server.js
45 lines
#!/usr/bin/env node
// Credential Launcher — tiny static server, localhost only, kept alive by launchd.
// Serves page.html (authored for the Artifact skeleton) wrapped in a standalone
// HTML document so it renders in a normal browser at http://127.0.0.1:51858/.
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = Number(process.env.PORT) || 51858;
const HOST = '127.0.0.1';
const PAGE = path.join(__dirname, 'page.html');
const HEAD = '<!doctype html><html lang="en"><head>' +
'<meta charset="utf-8">' +
'<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">' +
'<style>html,body{margin:0}img{max-width:100%}[hidden]{display:none!important}</style>';
function render() {
// Read fresh each request so editing page.html shows up on reload.
const body = fs.readFileSync(PAGE, 'utf8');
return HEAD + body + '</html>';
}
const server = http.createServer((req, res) => {
if (req.url === '/health') {
res.writeHead(200, { 'content-type': 'text/plain' });
return res.end('ok');
}
try {
const html = render();
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store' });
res.end(html);
} catch (e) {
res.writeHead(500, { 'content-type': 'text/plain' });
res.end('cred-launcher error: ' + e.message);
}
});
server.listen(PORT, HOST, () => {
console.log(`[${new Date().toISOString()}] cred-launcher listening on http://${HOST}:${PORT}/`);
});
// Fail loud but let launchd KeepAlive restart us.
process.on('uncaughtException', (e) => { console.error('uncaught', e); process.exit(1); });