-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-dev.js
More file actions
69 lines (59 loc) · 2.28 KB
/
server-dev.js
File metadata and controls
69 lines (59 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Quick dev server for testing the extension locally.
// Usage: node server-dev.js
// Listens on http://localhost:3737/ingest
import http from 'http';
import zlib from 'zlib';
const PORT = 3737;
const server = http.createServer((req, res) => {
// CORS — allow requests from any page origin
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Content-Encoding, X-Org-Id');
// Private Network Access (PNA) — allow extension/background fetches to http://localhost
// Chrome may send an OPTIONS preflight with Access-Control-Request-Private-Network: true
// which requires this response header to proceed.
res.setHeader('Access-Control-Allow-Private-Network', 'true');
if (req.method === 'OPTIONS') {
res.writeHead(204);
res.end();
return;
}
if (req.method !== 'POST' || !req.url.startsWith('/ingest')) {
res.writeHead(404);
res.end();
return;
}
const orgId = req.headers['x-org-id'] ?? new URL(req.url, `http://localhost:${PORT}`).searchParams.get('orgId') ?? '(none)';
const chunks = [];
req.on('data', (chunk) => chunks.push(chunk));
req.on('end', () => {
const raw = Buffer.concat(chunks);
const encoding = req.headers['content-encoding'];
const parse = (buf) => {
try {
const batch = JSON.parse(buf.toString('utf8'));
console.log(`\n--- batch received org=${orgId} events=${batch.events.length} sdk=${batch.sdkVersion} ---`);
for (const entry of batch.events) {
const d = entry.data;
const op = d.graphqlOperationName ? ` op=${d.graphqlOperationName}` : '';
console.log(` [${entry.count}x] ${d.method} ${d.protocol}://${d.domain}${d.path} status=${d.responseStatus}${op}`);
}
} catch (e) {
console.error('Failed to parse batch:', e.message);
}
};
if (encoding === 'gzip') {
zlib.gunzip(raw, (err, buf) => {
if (err) { console.error('gunzip failed:', err.message); return; }
parse(buf);
});
} else {
parse(raw);
}
res.writeHead(204);
res.end();
});
});
server.listen(PORT, () => {
console.log(`dev-tools-observer receiver listening on http://localhost:${PORT}/ingest`);
});