mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 00:32:21 +00:00
Merge branch 'main' into fix/realtime-voice-unicode-patterns
This commit is contained in:
89 files changed
+2980
-253
No files matched your search
@@ -313,9 +313,9 @@ test('FIRMS retains a large successful source during partial failure and filters
|
||||
const first = json(await request());
|
||||
assert.equal(first.count, 130001);
|
||||
assert.equal(first.sources.filter((s) => s.ok).length, 1);
|
||||
assert.equal(calls, 4);
|
||||
assert.equal(calls, 3);
|
||||
assert.equal(json(await request()).count, 130001);
|
||||
assert.equal(calls, 4);
|
||||
assert.equal(calls, 3);
|
||||
assert.deepEqual(json(await request('/status')).transactions, {
|
||||
used: 3,
|
||||
limit: 5000,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { once } from 'node:events';
|
||||
import { gunzipSync } from 'node:zlib';
|
||||
import { WebSocketServer } from 'ws';
|
||||
import { setTimeout as delay } from 'node:timers/promises';
|
||||
import * as providers from '../../server/providers/live.js';
|
||||
@@ -15,7 +16,7 @@ function install(plugin, preview = false) {
|
||||
},
|
||||
},
|
||||
});
|
||||
return async (route, url = '/', method = 'GET') => {
|
||||
return async (route, url = '/', method = 'GET', headers = {}) => {
|
||||
assert.ok(routes.has(route), `registered route: ${route}`);
|
||||
const response = {
|
||||
statusCode: 200,
|
||||
@@ -32,7 +33,7 @@ function install(plugin, preview = false) {
|
||||
this.body = body;
|
||||
},
|
||||
};
|
||||
await routes.get(route)({ url, method }, response);
|
||||
await routes.get(route)({ url, method, headers }, response);
|
||||
return response;
|
||||
};
|
||||
}
|
||||
@@ -584,6 +585,168 @@ test('track backfill proxy returns 502 on an oversized upstream body and caches
|
||||
assert.equal(callCount, 1);
|
||||
});
|
||||
|
||||
test('the aircraft snapshot routes decline a non-GET before any upstream work', async (t) => {
|
||||
// A fetch that throws proves the refusal comes before any upstream call.
|
||||
t.mock.method(globalThis, 'fetch', async (url) => {
|
||||
throw new Error(`upstream must not be reached: ${url}`);
|
||||
});
|
||||
const cases = [
|
||||
[install(providers.openSkyProxy()), '/api/flights'],
|
||||
[install(providers.adsbLolProxy()), '/api/military'],
|
||||
];
|
||||
for (const [request, route] of cases) {
|
||||
for (const method of ['POST', 'PUT', 'DELETE', 'PATCH']) {
|
||||
const response = await request(route, '/', method);
|
||||
assert.equal(response.statusCode, 405, `${method} ${route}`);
|
||||
// RFC 9110 section 15.5.6: a 405 names the methods it accepts.
|
||||
assert.equal(response.headers.allow, 'GET', `${method} ${route}`);
|
||||
assert.deepEqual(JSON.parse(response.body), {
|
||||
error: 'Method Not Allowed',
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('the snapshot method check hands each /track route on to its own handler', async () => {
|
||||
// The snapshot mount also matches /track, so it must hand /track on
|
||||
// before it checks the method.
|
||||
for (const [plugin, route] of [
|
||||
[providers.openSkyProxy(), '/api/flights'],
|
||||
[providers.adsbLolProxy(), '/api/military'],
|
||||
]) {
|
||||
const routes = new Map();
|
||||
plugin.configureServer({
|
||||
middlewares: { use: (path, handler) => routes.set(path, handler) },
|
||||
});
|
||||
const res = {
|
||||
statusCode: 200,
|
||||
writeHead(s) {
|
||||
this.statusCode = s;
|
||||
},
|
||||
end() {},
|
||||
};
|
||||
let handedOn = false;
|
||||
await routes.get(route)(
|
||||
{ url: '/track?icao24=abc123', method: 'POST' },
|
||||
res,
|
||||
() => {
|
||||
handedOn = true;
|
||||
},
|
||||
);
|
||||
assert.ok(handedOn, `${route}/track reaches next()`);
|
||||
assert.equal(
|
||||
res.statusCode,
|
||||
200,
|
||||
`${route}/track is not answered by the snapshot handler`,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('the OpenSky snapshot is compressed for clients that ask, and an error body never inherits the cached frame', async (t) => {
|
||||
environment(t, {
|
||||
OPENSKY_AUTH_MODE: 'anon',
|
||||
OPENSKY_CLIENT_ID: undefined,
|
||||
OPENSKY_CLIENT_SECRET: undefined,
|
||||
OPENSKY_USERNAME: undefined,
|
||||
OPENSKY_PASSWORD: undefined,
|
||||
});
|
||||
let now = Date.now();
|
||||
t.mock.method(Date, 'now', () => now);
|
||||
t.mock.method(console, 'log', () => {});
|
||||
t.mock.method(console, 'error', () => {});
|
||||
|
||||
let upstreamStatus = 200;
|
||||
let snapshotAgeMs = 0;
|
||||
t.mock.method(globalThis, 'fetch', async (url) => {
|
||||
if (url.includes('/lat/'))
|
||||
return Response.json({
|
||||
now: now / 1000,
|
||||
ac: Array.from({ length: 200 }, (_, index) => ({
|
||||
hex: `abc${index}`,
|
||||
lat: 30,
|
||||
lon: -97,
|
||||
alt_baro: 10000,
|
||||
})),
|
||||
});
|
||||
assert.ok(url.includes('/states/'), `unexpected upstream call: ${url}`);
|
||||
if (upstreamStatus !== 200)
|
||||
return new Response('{"error":"upstream"}', { status: upstreamStatus });
|
||||
const sourceSeconds = Math.floor((now - snapshotAgeMs) / 1000);
|
||||
// Healthy credits keep the 9 s base TTL, so the clock jumps below clear it.
|
||||
return Response.json(
|
||||
{
|
||||
time: sourceSeconds,
|
||||
states: Array.from({ length: 200 }, (_, index) => [
|
||||
`abc${index}`,
|
||||
'CALLSIGN',
|
||||
'United States',
|
||||
sourceSeconds,
|
||||
-97.7,
|
||||
30.1,
|
||||
10000,
|
||||
]),
|
||||
},
|
||||
{ headers: { 'x-rate-limit-remaining': '4000' } },
|
||||
);
|
||||
});
|
||||
|
||||
// A fresh module: the snapshot cache, its frame and the TTL are module state.
|
||||
const isolated = await import(
|
||||
`../../server/providers/aircraft/opensky.js?compression=${now}`
|
||||
);
|
||||
const states = install(isolated.openSkyProxy());
|
||||
const compressed = await states('/api/flights', '/', 'GET', {
|
||||
'accept-encoding': 'gzip, deflate, br',
|
||||
});
|
||||
assert.equal(compressed.statusCode, 200);
|
||||
assert.equal(compressed.headers['content-encoding'], 'gzip');
|
||||
assert.equal(compressed.headers.vary, 'Accept-Encoding');
|
||||
assert.ok(Buffer.isBuffer(compressed.body));
|
||||
|
||||
// Same cached snapshot, no Accept-Encoding: identity bytes, which the frame
|
||||
// above must decode to.
|
||||
const identity = await states('/api/flights');
|
||||
assert.equal(identity.headers['x-opensky-cache'], 'HIT');
|
||||
assert.equal(identity.headers['content-encoding'], undefined);
|
||||
assert.equal(identity.headers.vary, 'Accept-Encoding');
|
||||
assert.equal(gunzipSync(compressed.body).toString(), identity.body);
|
||||
|
||||
// `gzip;q=0` refuses gzip.
|
||||
const refused = await states('/api/flights', '/', 'GET', {
|
||||
'accept-encoding': 'gzip;q=0',
|
||||
});
|
||||
assert.equal(refused.headers['content-encoding'], undefined);
|
||||
assert.equal(refused.body, identity.body);
|
||||
|
||||
// An upstream rejection is not cached, so it must not carry the previous
|
||||
// snapshot's Content-Encoding.
|
||||
upstreamStatus = 403;
|
||||
now += 60000; // past the 9 s base TTL
|
||||
const rejected = await states('/api/flights', '/', 'GET', {
|
||||
'accept-encoding': 'gzip',
|
||||
});
|
||||
assert.equal(rejected.statusCode, 403);
|
||||
assert.equal(rejected.headers['content-encoding'], undefined);
|
||||
assert.ok(JSON.parse(rejected.body).error, 'a readable error body');
|
||||
|
||||
// A snapshot too old to serve falls back to adsb.lol, which negotiates the
|
||||
// same way.
|
||||
upstreamStatus = 200;
|
||||
snapshotAgeMs = 3600000;
|
||||
now += 60000;
|
||||
const regional = await states('/api/flights', '?lat=30&lon=-97', 'GET', {
|
||||
'accept-encoding': 'gzip',
|
||||
});
|
||||
assert.equal(regional.statusCode, 200);
|
||||
assert.equal(regional.headers['x-flight-source'], 'adsb.lol');
|
||||
assert.equal(regional.headers['content-encoding'], 'gzip');
|
||||
assert.equal(regional.headers.vary, 'Accept-Encoding');
|
||||
assert.equal(
|
||||
JSON.parse(gunzipSync(regional.body).toString()).states.length,
|
||||
200,
|
||||
);
|
||||
});
|
||||
|
||||
test('OpenSky Basic mode no longer sends username and password: it asks anonymously and warns once', async (t) => {
|
||||
environment(t, {
|
||||
OPENSKY_CLIENT_ID: undefined,
|
||||
|
||||
@@ -684,25 +684,34 @@ test('the disk cache survives a restart and stays within its bounds', async () =
|
||||
await createNominatimCache({ dir, now: time.now }).get('k0'),
|
||||
null,
|
||||
);
|
||||
assert.equal(
|
||||
fs.statSync(dir).mode & 0o777,
|
||||
0o700,
|
||||
'the query cache directory is private',
|
||||
);
|
||||
// The cache is private only through POSIX mode bits. Windows has none
|
||||
// (directories stat as 0o666) and the cache keeps the checkout's ACLs,
|
||||
// so these checks run, and privacy holds, only on POSIX systems.
|
||||
const posixModes = process.platform !== 'win32';
|
||||
if (posixModes) {
|
||||
assert.equal(
|
||||
fs.statSync(dir).mode & 0o777,
|
||||
0o700,
|
||||
'the query cache directory is private (POSIX modes)',
|
||||
);
|
||||
}
|
||||
const text = fs
|
||||
.readdirSync(dir)
|
||||
.filter((name) => name.endsWith('.json'))
|
||||
.map((name) => fs.readFileSync(path.join(dir, name), 'utf8'))
|
||||
.join('\n');
|
||||
assert.equal(text.includes('k4'), false, 'disk entries never store raw keys');
|
||||
for (const name of fs
|
||||
.readdirSync(dir)
|
||||
.filter((entry) => entry.endsWith('.json')))
|
||||
assert.equal(
|
||||
fs.statSync(path.join(dir, name)).mode & 0o777,
|
||||
0o600,
|
||||
`${name} is private`,
|
||||
);
|
||||
if (posixModes) {
|
||||
for (const name of fs
|
||||
.readdirSync(dir)
|
||||
.filter((entry) => entry.endsWith('.json'))) {
|
||||
assert.equal(
|
||||
fs.statSync(path.join(dir, name)).mode & 0o777,
|
||||
0o600,
|
||||
`${name} is private (POSIX modes)`,
|
||||
);
|
||||
}
|
||||
}
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
|
||||
@@ -501,6 +501,25 @@ test('the upstream host is pinned: a redirect is refused, not followed', async (
|
||||
assert.deepEqual(seen, ['error'], 'a redirect would escape the pinned host');
|
||||
});
|
||||
|
||||
test('the routing proxy declines a non-GET before rate limiting or upstream', async (t) => {
|
||||
// A fetch that throws proves the refusal comes before any upstream call.
|
||||
const request = install(installRouteMiddleware);
|
||||
t.mock.method(globalThis, 'fetch', async () => {
|
||||
throw new Error('upstream must not be reached');
|
||||
});
|
||||
for (const method of ['POST', 'PUT', 'DELETE', 'PATCH']) {
|
||||
const response = await request(
|
||||
'/api/route',
|
||||
'?profile=car&coords=-97,30;-97.01,30.01',
|
||||
method,
|
||||
);
|
||||
assert.equal(response.statusCode, 405, method);
|
||||
// RFC 9110 section 15.5.6: a 405 names the methods it accepts.
|
||||
assert.equal(response.headers.allow, 'GET', method);
|
||||
assert.deepEqual(response.body, { error: 'Method Not Allowed' });
|
||||
}
|
||||
});
|
||||
|
||||
test('both paid Google routes refuse cross-site requests before calling Google', async (t) => {
|
||||
const plugin = googlePlacesContextProxy({ resolveApiKey: () => 'key' });
|
||||
const routes = new Map();
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { gunzipSync } from 'node:zlib';
|
||||
import {
|
||||
acceptsGzip,
|
||||
encodedBody,
|
||||
gzipBody,
|
||||
} from '../../server/providers/common/compress.js';
|
||||
|
||||
/** A request shaped like the one Node hands a middleware. */
|
||||
function request(acceptEncoding) {
|
||||
return {
|
||||
url: '/api/flights',
|
||||
method: 'GET',
|
||||
headers:
|
||||
acceptEncoding === undefined ? {} : { 'accept-encoding': acceptEncoding },
|
||||
};
|
||||
}
|
||||
|
||||
/** Repetitive JSON shaped like a snapshot body. */
|
||||
function snapshotBody(states = 200) {
|
||||
return JSON.stringify({
|
||||
time: 1700000000,
|
||||
states: Array.from({ length: states }, (_, index) => [
|
||||
`abc${index}`,
|
||||
'CALLSIGN',
|
||||
'United States',
|
||||
1700000000,
|
||||
30.1,
|
||||
-97.7,
|
||||
10000,
|
||||
]),
|
||||
});
|
||||
}
|
||||
|
||||
/** Seeded high-entropy bytes, so the "gzip made it bigger" check is stable. */
|
||||
function incompressibleBytes(length) {
|
||||
const bytes = Buffer.alloc(length);
|
||||
let state = 123456789;
|
||||
for (let index = 0; index < length; index += 1) {
|
||||
state = (state * 1103515245 + 12345) % 2147483648;
|
||||
bytes[index] = (state >>> 16) & 0xff;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
test('accept-encoding is read the way clients actually write it', () => {
|
||||
assert.equal(acceptsGzip(request('gzip')), true);
|
||||
assert.equal(acceptsGzip(request('gzip, deflate, br')), true);
|
||||
assert.equal(acceptsGzip(request('br;q=1.0, gzip;q=0.8')), true);
|
||||
assert.equal(acceptsGzip(request('GZIP')), true);
|
||||
assert.equal(acceptsGzip(request(' gzip ')), true);
|
||||
|
||||
assert.equal(acceptsGzip(request('br, deflate')), false);
|
||||
assert.equal(acceptsGzip(request('identity')), false);
|
||||
assert.equal(acceptsGzip(request('')), false);
|
||||
assert.equal(acceptsGzip(request(undefined)), false);
|
||||
assert.equal(acceptsGzip({}), false);
|
||||
assert.equal(acceptsGzip(undefined), false);
|
||||
|
||||
// `q=0` names gzip and refuses it (RFC 9110 section 12.4.2).
|
||||
assert.equal(acceptsGzip(request('gzip;q=0')), false);
|
||||
assert.equal(acceptsGzip(request('gzip;q=0.000')), false);
|
||||
assert.equal(acceptsGzip(request('gzip;q=0.001')), true);
|
||||
assert.equal(acceptsGzip(request('gzip;q=1')), true);
|
||||
assert.equal(acceptsGzip(request('gzip;q=1.000')), true);
|
||||
|
||||
// A q that is not a whole `qvalue` is unreadable and must not round into
|
||||
// acceptance.
|
||||
assert.equal(acceptsGzip(request('gzip;q=bogus')), false);
|
||||
assert.equal(acceptsGzip(request('gzip;q=1oops')), false);
|
||||
assert.equal(acceptsGzip(request('gzip;q=0.5abc')), false);
|
||||
assert.equal(acceptsGzip(request('gzip;q=2')), false);
|
||||
assert.equal(acceptsGzip(request('gzip;q=1.5')), false);
|
||||
assert.equal(acceptsGzip(request('gzip;q=0.0001')), false);
|
||||
assert.equal(acceptsGzip(request('gzip;q=')), false);
|
||||
|
||||
// `gzip` is not matched as a substring of a neighbouring token.
|
||||
assert.equal(acceptsGzip(request('x-gzip')), false);
|
||||
assert.equal(acceptsGzip(request('gzipped')), false);
|
||||
});
|
||||
|
||||
test('a body is only compressed when compression pays for itself', async () => {
|
||||
assert.equal(await gzipBody(''), null);
|
||||
assert.equal(
|
||||
await gzipBody('{"states":[]}'),
|
||||
null,
|
||||
'below the frame threshold',
|
||||
);
|
||||
assert.equal(await gzipBody(Buffer.alloc(1023, 'a')), null, 'one byte short');
|
||||
|
||||
const identity = snapshotBody();
|
||||
assert.ok(identity.length > 1024, 'fixture is worth compressing');
|
||||
const compressed = await gzipBody(identity);
|
||||
assert.ok(Buffer.isBuffer(compressed));
|
||||
assert.ok(compressed.byteLength < Buffer.byteLength(identity));
|
||||
assert.equal(gunzipSync(compressed).toString(), identity);
|
||||
|
||||
// Buffers are accepted as-is; data gzip cannot shrink stays uncompressed.
|
||||
assert.ok((await gzipBody(Buffer.from(identity))) !== null);
|
||||
assert.equal(await gzipBody(incompressibleBytes(4096)), null);
|
||||
});
|
||||
|
||||
test('the encoded body and the header that names it are chosen together', async () => {
|
||||
const identity = snapshotBody();
|
||||
const compressed = await gzipBody(identity);
|
||||
|
||||
const negotiated = encodedBody(request('gzip'), identity, compressed);
|
||||
assert.equal(negotiated.body, compressed);
|
||||
assert.equal(negotiated.headers['Content-Encoding'], 'gzip');
|
||||
assert.equal(negotiated.headers.Vary, 'Accept-Encoding');
|
||||
assert.equal(gunzipSync(negotiated.body).toString(), identity);
|
||||
|
||||
// Offered but refused: identity bytes and no Content-Encoding.
|
||||
const refused = encodedBody(request('br'), identity, compressed);
|
||||
assert.equal(refused.body, identity);
|
||||
assert.equal(refused.headers['Content-Encoding'], undefined);
|
||||
assert.equal(refused.headers.Vary, 'Accept-Encoding');
|
||||
|
||||
// Wanted but not available: same answer.
|
||||
const unavailable = encodedBody(request('gzip'), identity, null);
|
||||
assert.equal(unavailable.body, identity);
|
||||
assert.equal(unavailable.headers['Content-Encoding'], undefined);
|
||||
|
||||
// Vary is on both branches, so a shared cache cannot serve the wrong encoding.
|
||||
assert.equal(unavailable.headers.Vary, 'Accept-Encoding');
|
||||
assert.equal(
|
||||
encodedBody(request(undefined), identity, compressed).headers.Vary,
|
||||
'Accept-Encoding',
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user