Railyard side of PY1.3 (RYP-D2, RYP-E1, SC-E3). Acceptance:
1. apps/insights: umami-software/umami v3.4.0 (MIT, commit ec0ff503),
vendored --depth 1, LICENSE kept, NOTICE added. Fork additions only:
src/railyard/{ingest,summary,tenancy,api-only}.ts, /v1/ingest/batch (the
edge collector contract v1, bearer INSIGHTS_INGEST_TOKEN, no IP ever,
retried batches de-duplicated by migration 90_railyard_ingest_receipts),
/api/v1/tenants/{org}/summary (bearer INSIGHTS_API_TOKEN), src/proxy.ts
(INSIGHTS_API_ONLY=1 serves only the Railyard API and the heartbeat).
Tenant isolation is tested at the API level (an app id owned by another org
is refused) and at the SQL level (upstream migrations 01-26 plus 90 on a
throwaway cluster: org B never reads org A's rows, even naming its app).
Upstream vitest suite: not run on the Mac (pnpm 12.3.4 pinned upstream,
disk floor); it runs in the image build on box 2 or CI.
2. No upstream product name in anything a customer loads: the page tag, the
console pages and the fork's additions (tests); the upstream dashboard is
never served in API-only mode.
3. The console writes the collector's site entry on "Add app", on
auto-created apps from deploys, and rewrites every entry on add-on, plan or
pause changes (lib/sites.ts, SITES KV binding). The bridge gains
`sites` (the project's app, site key, page tag and collector URLs, entry
written) and the analytics proof is the first event from Insights. The
console's Insights page shows visitors, events, first event and the
visit -> signup -> first action funnel.
5. insights-edge/src/caps.ts: at 100% of the cap events are still counted and
not stored until the next UTC month; the console shows the same message
from a byte-identical copy (lib/caps.ts, tested).
4 (384 MB at 50 events a second for 10 min) is measured on staging before deploy.
Env: INSIGHTS_API_URL, INSIGHTS_API_TOKEN, SITES KV binding (console);
INSIGHTS_INGEST_TOKEN, INSIGHTS_API_TOKEN, INSIGHTS_API_ONLY (Insights).
Tests: console 54/54 (tsc clean), insights fork 7/7 (incl. SQL isolation),
insights-edge caps + collector + sim 14/14, tests/contract 3/3.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
114 lines
2.9 KiB
JavaScript
114 lines
2.9 KiB
JavaScript
/* eslint-disable no-console */
|
|
import 'dotenv/config';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import https from 'https';
|
|
import { list } from 'tar';
|
|
import zlib from 'zlib';
|
|
|
|
if (process.env.SKIP_BUILD_GEO) {
|
|
console.log('SKIP_BUILD_GEO is set. Skipping geo setup.');
|
|
process.exit(0);
|
|
}
|
|
|
|
if (process.env.VERCEL && !process.env.BUILD_GEO) {
|
|
console.log('Vercel environment detected. Skipping geo setup.');
|
|
process.exit(0);
|
|
}
|
|
|
|
const db = 'GeoLite2-City';
|
|
|
|
// Support custom URL via environment variable
|
|
let url = process.env.GEO_DATABASE_URL;
|
|
|
|
// Fallback to default URLs if not provided
|
|
if (!url) {
|
|
if (process.env.MAXMIND_LICENSE_KEY) {
|
|
url =
|
|
`https://download.maxmind.com/app/geoip_download` +
|
|
`?edition_id=${db}&license_key=${process.env.MAXMIND_LICENSE_KEY}&suffix=tar.gz`;
|
|
} else {
|
|
url = `https://raw.githubusercontent.com/GitSquared/node-geolite2-redist/master/redist/${db}.tar.gz`;
|
|
}
|
|
}
|
|
|
|
const dest = path.resolve(process.cwd(), 'geo');
|
|
|
|
if (!fs.existsSync(dest)) {
|
|
fs.mkdirSync(dest);
|
|
}
|
|
|
|
// Check if URL points to a direct .mmdb file (already extracted)
|
|
const isDirectMmdb = url.endsWith('.mmdb');
|
|
|
|
// Download handler for compressed tar.gz files
|
|
const downloadCompressed = url =>
|
|
new Promise(resolve => {
|
|
https.get(url, res => {
|
|
resolve(res.pipe(zlib.createGunzip({})).pipe(list()));
|
|
});
|
|
});
|
|
|
|
// Download handler for direct .mmdb files
|
|
const downloadDirect = (url, originalUrl) =>
|
|
new Promise((resolve, reject) => {
|
|
https.get(url, res => {
|
|
// Follow redirects
|
|
if (res.statusCode === 301 || res.statusCode === 302) {
|
|
downloadDirect(res.headers.location, originalUrl || url)
|
|
.then(resolve)
|
|
.catch(reject);
|
|
return;
|
|
}
|
|
|
|
const filename = path.join(dest, path.basename(originalUrl || url));
|
|
const fileStream = fs.createWriteStream(filename);
|
|
|
|
res.pipe(fileStream);
|
|
|
|
fileStream.on('finish', () => {
|
|
fileStream.close();
|
|
console.log('Saved geo database:', filename);
|
|
resolve();
|
|
});
|
|
|
|
fileStream.on('error', e => {
|
|
reject(e);
|
|
});
|
|
});
|
|
});
|
|
|
|
// Execute download based on file type
|
|
if (isDirectMmdb) {
|
|
downloadDirect(url).catch(e => {
|
|
console.error('Failed to download geo database:', e);
|
|
process.exit(1);
|
|
});
|
|
} else {
|
|
downloadCompressed(url)
|
|
.then(
|
|
res =>
|
|
new Promise((resolve, reject) => {
|
|
res.on('entry', entry => {
|
|
if (entry.path.endsWith('.mmdb')) {
|
|
const filename = path.join(dest, path.basename(entry.path));
|
|
entry.pipe(fs.createWriteStream(filename));
|
|
|
|
console.log('Saved geo database:', filename);
|
|
}
|
|
});
|
|
|
|
res.on('error', e => {
|
|
reject(e);
|
|
});
|
|
res.on('finish', () => {
|
|
resolve();
|
|
});
|
|
}),
|
|
)
|
|
.catch(e => {
|
|
console.error('Failed to download geo database:', e);
|
|
process.exit(1);
|
|
});
|
|
}
|