Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
274 lines (248 loc) · 8.92 KB
/
Copy pathserver.js
File metadata and controls
274 lines (248 loc) · 8.92 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
'use strict';
const path = require('node:path');
const fs = require('node:fs');
const Fastify = require('fastify');
const fastifyView = require('@fastify/view');
const fastifyStatic = require('@fastify/static');
const { Eta } = require('eta');
const WOTS = require('wots');
const db = require('./db');
const PORT = process.env.PORT || 3000;
const MAX_BODY_BYTES = 15 * 1024 * 1024;
const IS_DEV = process.env.NODE_ENV !== 'production';
const CLIENT_ERRORS = new Set([
'INVALID_PHONE', 'INVALID_CODE_FORMAT', 'INVALID_JWT',
'INVALID_INCIDENT_ID', 'INVALID_REPORT', 'UNKNOWN_TYPE',
'REDIRECT_911', 'INVALID_CANCEL_INFO',
]);
function userIdFromToken(token) {
try { return WOTS.decodeJwtPayload(token).sub || ''; }
catch (_) { return ''; }
}
function bearerToken(req) {
const auth = req.headers.authorization || '';
return auth.startsWith('Bearer ') ? auth.slice(7) : '';
}
function requireToken(req, reply) {
const token = bearerToken(req);
if (!token) {
reply.status(401).send({ error: 'missing token' });
return null;
}
return token;
}
// Dev-only in-memory sim store: userId -> Map<incidentId, incident>.
// Lives for the life of the process and is wiped on restart. Kept out of the
// real SQLite cache so nothing sim ever leaks to a prod DB file.
const SIM = new Map();
function simFor(userId) {
if (!SIM.has(userId)) SIM.set(userId, new Map());
return SIM.get(userId);
}
// Sim keys need a stable owner even when the JWT sub can't be decoded, so we
// fall back to 'anon'. Kept separate from the real userId used for DB caching.
function simUserId(token) { return userIdFromToken(token) || 'anon'; }
function isSimId(id) { return typeof id === 'string' && id.startsWith('sim-'); }
function makeSimIncident(body) {
const nowSec = Math.floor(Date.now() / 1000);
const rand = Math.random().toString(36).slice(2, 8);
const type = body.type || 'UNKNOWN';
const typeMeta = (WOTS.TYPES && WOTS.TYPES[type]) || {};
const comments = body.comment
? [{ text: String(body.comment), createdAt: nowSec }]
: [];
const imageUrls = body.imageBase64
? [`data:image/jpeg;base64,${body.imageBase64}`]
: [];
const lat = Number(body.lat), lon = Number(body.lon);
const fallbackAddr = Number.isFinite(lat) && Number.isFinite(lon)
? `Simulated near ${lat.toFixed(4)}, ${lon.toFixed(4)}`
: 'Simulated location';
return {
id: `sim-${nowSec.toString(36)}-${rand}`,
typeName: typeMeta.typeName || type,
address: body.address ? String(body.address) : fallbackAddr,
primaryText: 'Open',
receivedAt: nowSec,
props: {},
userContent: { comments, imageUrls },
};
}
function simSummary(inc) {
return {
id: inc.id,
typeName: inc.typeName,
address: inc.address,
primaryText: inc.primaryText,
};
}
const app = Fastify({ bodyLimit: MAX_BODY_BYTES });
app.register(fastifyView, {
engine: { eta: new Eta() },
root: path.join(__dirname, 'views'),
viewExt: 'html',
});
app.register(fastifyStatic, {
root: path.join(__dirname, 'public', 'img'),
prefix: '/img/',
cacheControl: true,
maxAge: 604800000,
immutable: true,
});
app.register(fastifyStatic, {
root: path.join(__dirname, 'public', 'data'),
prefix: '/data/',
cacheControl: true,
maxAge: 86400000,
decorateReply: false,
});
// SPA build output from Vite (`npm run build`). /assets/* holds hashed JS/CSS
// bundles; the built index.html is served for each ported SPA route below.
// In dev the Vite dev server (npm run dev:client) serves these directly with
// HMR and proxies /api etc. back to Fastify, so this block is inert without
// a prior `npm run build`.
const DIST_DIR = path.join(__dirname, 'dist');
const DIST_INDEX = path.join(DIST_DIR, 'index.html');
const HAS_DIST = fs.existsSync(DIST_INDEX);
if (HAS_DIST) {
app.register(fastifyStatic, {
root: path.join(DIST_DIR, 'assets'),
prefix: '/assets/',
cacheControl: true,
maxAge: 31536000000,
immutable: true,
decorateReply: false,
});
}
app.setErrorHandler((err, req, reply) => {
const status = err.statusCode
|| (CLIENT_ERRORS.has(err.code) ? 400 : 500);
reply.status(status).send({
error: err.code || 'INTERNAL',
message: err.message || String(err),
});
});
// Legacy Eta routes — pages not yet ported to the React SPA.
for (const [route, template] of [
['/', 'index'],
['/new', 'index'],
]) {
app.get(route, (req, reply) => reply.view(template, { isDev: IS_DEV }));
}
app.get('/report/:id', (req, reply) => reply.view('index', { isDev: IS_DEV }));
// SPA routes — served the built React app. If the dist bundle is missing
// (dev before first build), fall back to a hint so nothing 404s silently.
const SPA_ROUTES = ['/stats', '/about', '/terms'];
const spaHandler = HAS_DIST
? (_req, reply) => reply.type('text/html').send(fs.readFileSync(DIST_INDEX))
: (_req, reply) => reply.type('text/plain').status(503).send(
'SPA bundle missing. Run `npm run build` (or `npm run dev:client` for HMR at :5173).'
);
for (const route of SPA_ROUTES) app.get(route, spaHandler);
app.post('/api/start-login', async (req) => {
const { phone } = req.body || {};
const session = await WOTS.startLogin(phone);
return { session };
});
app.post('/api/complete-login', async (req) => {
const { session, code } = req.body || {};
return WOTS.completeLogin(session, code);
});
app.post('/api/resend-code', async (req) => {
const { session } = req.body || {};
await WOTS.resendCode(session);
return { ok: true };
});
app.get('/api/incidents', async (req, reply) => {
const token = requireToken(req, reply);
if (!token) return reply;
let items;
try {
items = await WOTS.all(token);
} catch (err) {
// In dev, keep the list usable when WOTS is unreachable so sim items
// still render. In prod, surface the real error.
if (!IS_DEV) throw err;
req.log.warn({ err: err && (err.code || err.message) }, 'WOTS.all failed; returning sim-only list');
items = [];
}
const userId = userIdFromToken(token);
const cached = userId
? db.getMany(userId, items.map((it) => it && it.id).filter(Boolean))
: {};
if (IS_DEV) {
const sims = [...simFor(simUserId(token)).values()];
if (sims.length) {
// Prepend so newest sim shows on top; seed detailsCache so the client
// doesn't hit /api/incidents/:id for them.
const summaries = sims.map(simSummary);
for (const inc of sims) cached[inc.id] = inc;
return { items: [...summaries, ...items], cached };
}
}
return { items, cached };
});
app.get('/api/incidents/:id', async (req, reply) => {
const token = requireToken(req, reply);
if (!token) return reply;
const { id } = req.params;
const userId = userIdFromToken(token);
if (IS_DEV && isSimId(id)) {
const inc = simFor(simUserId(token)).get(id);
if (inc) return { incident: inc, cached: true };
reply.status(404);
return { error: 'INVALID_INCIDENT_ID', message: 'sim incident not found' };
}
if (userId) {
const hit = db.get(id, userId);
if (hit) return { incident: hit, cached: true };
}
const incident = await WOTS.detail(token, id);
if (userId) db.put(id, userId, incident);
return { incident: db.sanitizeIncident(incident) };
});
app.get('/api/types', async () => ({ types: WOTS.TYPES }));
app.get('/api/stats', async () => db.stats());
app.post('/api/submit', async (req, reply) => {
const token = requireToken(req, reply);
if (!token) return reply;
const { type, lat, lon, phone, address, comment, imageBase64 } = req.body || {};
const image = imageBase64 ? Buffer.from(imageBase64, 'base64') : undefined;
return WOTS.submit(token, { type, lat, lon, phone, address, comment, image });
});
if (IS_DEV) {
app.post('/api/simulate/submit', async (req, reply) => {
const token = requireToken(req, reply);
if (!token) return reply;
const incident = makeSimIncident(req.body || {});
simFor(simUserId(token)).set(incident.id, incident);
return {
incidentId: incident.id,
cancelInfo: { sim: true, incidentId: incident.id },
};
});
}
app.post('/api/cancel', async (req, reply) => {
const token = requireToken(req, reply);
if (!token) return reply;
const { cancelInfo } = req.body || {};
const userId = userIdFromToken(token);
if (IS_DEV && cancelInfo && cancelInfo.sim && cancelInfo.incidentId) {
const inc = simFor(simUserId(token)).get(cancelInfo.incidentId);
if (!inc) {
reply.status(404);
return { error: 'INVALID_CANCEL_INFO', message: 'sim incident not found' };
}
inc.canceledAt = Math.floor(Date.now() / 1000);
inc.primaryText = 'Canceled';
return { incident: inc };
}
const incident = await WOTS.cancel(token, cancelInfo);
if (userId && incident && cancelInfo && cancelInfo.incidentId) {
db.put(cancelInfo.incidentId, userId, incident);
}
return { incident: db.sanitizeIncident(incident) };
});
app.listen({ port: PORT, host: '0.0.0.0' })
.then(() => console.log(`wots-clone listening on http://localhost:${PORT}`))
.catch((err) => { console.error(err); process.exit(1); });