-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathinstrumentation.ts
37 lines (35 loc) · 1.31 KB
/
instrumentation.ts
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
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
// Flush the privilege cache for users when the app server is started
// This ensures that if privileges were changed they take immediate global effect.
// We cannot reuse the same function from the main application because Pino is not compatible
// with this runtime. We need to use the native console.log function instead.
if (!process.env.REDIS_URL) {
// eslint-disable-next-line no-console
console.log("No REDIS_URL environment variable found, skipping privilege cache flush");
return;
}
const Redis = await import("ioredis").then((m) => m.default);
const redis = new Redis(process.env.REDIS_URL);
const stream = redis.scanStream({
match: "auth:privileges:*",
});
stream.on("data", function (keys: string[]) {
// `keys` is an array of strings representing key names
if (keys.length) {
const pipeline = redis.pipeline();
keys.forEach(function (key: string) {
pipeline.del(key);
});
pipeline.exec();
}
});
return new Promise<void>((resolve) =>
stream.on("end", () => {
// eslint-disable-next-line no-console
console.log("Cached Privileges have been cleared");
resolve();
})
);
}
}