Skip to content

Commit 4b78fd7

Browse files
support logs
1 parent 767d57c commit 4b78fd7

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
dist
33
module
4+
logs

client/standalone.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
11
import { init } from './docs-lib.js';
22

3+
const SIM_ID = 'docs-lib';
4+
const logBuffer = [];
5+
let flushTimer = null;
6+
7+
function flushLogs() {
8+
const entries = logBuffer.splice(0);
9+
flushTimer = null;
10+
if (entries.length === 0) return;
11+
fetch('/api/log', {
12+
method: 'POST',
13+
headers: { 'Content-Type': 'application/json' },
14+
body: JSON.stringify({ entries })
15+
}).catch(err => console.error('Failed to flush logs:', err));
16+
}
17+
18+
function pushLog(entry) {
19+
logBuffer.push({ ...entry, ts: new Date().toISOString() });
20+
if (!flushTimer) flushTimer = setTimeout(flushLogs, 1000);
21+
}
22+
23+
const context = {
24+
config: { id: SIM_ID, basePath: '' },
25+
emit: (eventType, payload = {}) => {
26+
pushLog({ simId: SIM_ID, dir: 'event', type: eventType, payload });
27+
}
28+
};
29+
330
if (document.readyState === 'loading') {
4-
document.addEventListener('DOMContentLoaded', () => init({}));
31+
document.addEventListener('DOMContentLoaded', () => init(context));
532
} else {
6-
init({});
33+
init(context);
734
}

server.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ if (isProduction && !fs.existsSync(STATIC_DIR)) throw new Error(`Serve directory
1717
const PORT = process.env.PORT || 3000;
1818
const wsClients = new Set();
1919

20+
const LOG_DIR = path.join(__dirname, 'logs');
21+
const LOG_FILE = path.join(LOG_DIR, 'events.jsonl');
22+
if (!fs.existsSync(LOG_DIR)) fs.mkdirSync(LOG_DIR, { recursive: true });
23+
2024
const mimeTypes = {
2125
'.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css',
2226
'.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg',
@@ -32,7 +36,45 @@ function serveFile(filePath, res) {
3236
});
3337
}
3438

39+
function readBody(req) {
40+
return new Promise((resolve, reject) => {
41+
let body = '';
42+
req.on('data', chunk => { body += chunk.toString(); });
43+
req.on('end', () => {
44+
try { resolve(JSON.parse(body)); }
45+
catch (e) { reject(e); }
46+
});
47+
req.on('error', reject);
48+
});
49+
}
50+
51+
async function handleLogRequest(req, res) {
52+
try {
53+
const data = await readBody(req);
54+
const entries = data.entries;
55+
if (!Array.isArray(entries) || entries.length === 0) {
56+
res.writeHead(400, { 'Content-Type': 'application/json' });
57+
res.end(JSON.stringify({ error: 'entries array is required' }));
58+
return;
59+
}
60+
const lines = entries.map(e => JSON.stringify(e)).join('\n') + '\n';
61+
fs.appendFile(LOG_FILE, lines, (err) => {
62+
if (err) console.error('Failed to write log:', err);
63+
});
64+
res.writeHead(200, { 'Content-Type': 'application/json' });
65+
res.end(JSON.stringify({ ok: true, count: entries.length }));
66+
} catch (error) {
67+
res.writeHead(400, { 'Content-Type': 'application/json' });
68+
res.end(JSON.stringify({ error: 'Invalid JSON' }));
69+
}
70+
}
71+
3572
function handlePostRequest(req, res, parsedUrl) {
73+
if (parsedUrl.pathname === '/api/log') {
74+
handleLogRequest(req, res);
75+
return;
76+
}
77+
3678
if (parsedUrl.pathname === '/message') {
3779
let body = '';
3880
req.on('data', c => { body += c.toString(); });

vite.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default defineConfig({
55
server: {
66
host: '0.0.0.0', hmr: true, allowedHosts: true, port: 3000,
77
proxy: {
8+
'/api': { target: 'http://localhost:3001', changeOrigin: true },
89
'/message': { target: 'http://localhost:3001', changeOrigin: true },
910
'/ws': { target: 'ws://localhost:3001', ws: true, changeOrigin: true }
1011
}

0 commit comments

Comments
 (0)