Skip to content

Commit 324c007

Browse files
committed
fix: code review comments
1 parent 6b354ec commit 324c007

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

packages/auth-server/scripts/copy-snarkjs.mjs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22
import { copyFileSync, existsSync, mkdirSync, readdirSync, statSync } from "node:fs";
33
import { dirname, join, resolve } from "node:path";
44
import { fileURLToPath } from "node:url";
5+
import { createRequire } from "node:module";
56

67
function main() {
78
// Resolve the path to snarkjs browser bundle
89
// Resolve snarkjs package base
910
let baseDir;
1011
try {
11-
const pkgJson = require.resolve("snarkjs/package.json");
12-
baseDir = dirname(pkgJson);
12+
// Prefer import.meta.resolve for pure ESM; fallback to createRequire for older Node versions
13+
let pkgJsonPath;
14+
if (typeof import.meta.resolve === "function") {
15+
const resolvedUrl = import.meta.resolve("snarkjs/package.json");
16+
pkgJsonPath = fileURLToPath(resolvedUrl);
17+
} else {
18+
const require = createRequire(import.meta.url);
19+
pkgJsonPath = require.resolve("snarkjs/package.json");
20+
}
21+
baseDir = dirname(pkgJsonPath);
1322
} catch (e) {
1423
console.warn("[copy-snarkjs] snarkjs not installed yet; skipping copy", e);
1524
return; // don't fail install, maybe another step will install it

packages/oidc-server/src/salt-service.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,32 @@ const app = express();
4545
// Collect default metrics (Node.js process metrics, etc.)
4646
client.collectDefaultMetrics();
4747

48+
// Normalize a path to reduce metrics label cardinality. Any dynamic-looking
49+
// segment (numbers, long hex/base64-ish tokens, UUIDs) is replaced with a
50+
// placeholder. If Express matched a route we prefer route.path which is
51+
// already a pattern (e.g. "/user/:id").
52+
function normalizePath(req: express.Request): string {
53+
// If Express has a route pattern, use it (lowest cardinality already)
54+
const routePath = (req as any).route?.path; // eslint-disable-line @typescript-eslint/no-explicit-any
55+
if (routePath) return routePath;
56+
57+
const raw = req.path || "/";
58+
const uuidRe = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
59+
const hexRe = /^[0-9a-fA-F]{8,}$/; // long-ish hex
60+
const base64ishRe = /^[0-9a-zA-Z_-]{10,}$/; // tokens
61+
return raw
62+
.split("/")
63+
.map((seg) => {
64+
if (!seg) return seg;
65+
if (/^[0-9]+$/.test(seg)) return ":int";
66+
if (uuidRe.test(seg)) return ":uuid";
67+
if (hexRe.test(seg)) return ":hex";
68+
if (base64ishRe.test(seg) && seg.length > 16) return ":tok";
69+
return seg;
70+
})
71+
.join("/") || "/";
72+
}
73+
4874
// Custom metrics
4975
const requestCounter = new client.Counter({
5076
name: "salt_service_requests_total",
@@ -62,7 +88,7 @@ const requestDuration = new client.Histogram({
6288
app.use((req, res, next) => {
6389
const end = requestDuration.startTimer();
6490
res.on("finish", () => {
65-
const labels = { method: req.method, path: req.route?.path || req.path, status: res.statusCode.toString() };
91+
const labels = { method: req.method, path: normalizePath(req), status: res.statusCode.toString() };
6692
requestCounter.inc(labels);
6793
end(labels);
6894
});
@@ -121,7 +147,7 @@ app.listen(mainPort, () => {
121147
});
122148

123149
// Separate metrics server on port 9090
124-
const METRICS_PORT = 9090;
150+
const METRICS_PORT = process.env.METRICS_PORT || 9090;
125151
const metricsApp = express();
126152
metricsApp.get("/metrics", async (_req, res) => {
127153
try {

0 commit comments

Comments
 (0)