This commit is contained in:
dahyeon committed 2026-04-16 13:48:53 -07:00
commit 7fa293c9e8
59 files changed
+15526

No files matched your search

+55
View File
@@ -0,0 +1,55 @@
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import * as dotenv from "dotenv";
import { getLogger } from "@obs/logger";
type NodeEnvMode = "dev" | "test" | "prod";
const VALID_MODES = new Set<NodeEnvMode>(["dev", "test", "prod"]);
const DEFAULT_MODE: NodeEnvMode = "dev";
let loaded = false;
const logger = getLogger();
function normalizeMode(raw: string | undefined): NodeEnvMode {
const input = (raw ?? "").trim().toLowerCase();
if (input === "development") return "dev";
if (input === "testing") return "test";
if (input === "production") return "prod";
if (VALID_MODES.has(input as NodeEnvMode)) return input as NodeEnvMode;
return DEFAULT_MODE;
}
function resolveEnvFile(mode: NodeEnvMode): string | null {
const moduleDir = path.dirname(fileURLToPath(import.meta.url));
const candidates = [
path.resolve(process.cwd(), "env-vars", `${mode}.env`),
path.resolve(moduleDir, "../../env-vars", `${mode}.env`),
];
for (const candidate of candidates) {
if (fs.existsSync(candidate)) return candidate;
}
return null;
}
export function loadEnvironmentFromNodeEnv(): void {
if (loaded) return;
loaded = true;
const mode = normalizeMode(process.env["NODE_ENV"]);
process.env["NODE_ENV"] = mode;
const envFile = resolveEnvFile(mode);
if (!envFile) {
logger.warn(`[Env] env-vars/${mode}.env not found. Skipping dotenv load.`);
return;
}
const result = dotenv.config({ path: envFile, override: false });
if (result.error) {
logger.warn(`[Env] Failed to load ${envFile}: ${result.error.message}`);
return;
}
}
+164
View File
@@ -0,0 +1,164 @@
import { createClient } from "redis";
import { getSecret } from "@common/secret-manager";
import { getLogger } from "@obs/logger";
type GarnetRedisClient = ReturnType<typeof createClient>;
function normalizeGarnetUrl(endpoint: string): string {
const trimmed = endpoint.trim();
if (!trimmed) {
throw new Error("GARNET_ENDPOINT cannot be empty");
}
if (/^redis(s)?:\/\//iu.test(trimmed)) {
return trimmed;
}
return `redis://${trimmed}`;
}
export class GarnetClient {
private client: GarnetRedisClient | null = null;
private readonly logger = getLogger();
private getClient(): GarnetRedisClient {
if (!this.client) {
throw new Error("Garnet client not connected");
}
return this.client;
}
async connect(): Promise<void> {
const endpoint = getSecret("GARNET_ENDPOINT");
const username = getSecret("GARNET_USERID");
const password = getSecret("GARNET_PASSWORD");
if (!endpoint || !username || !password) {
throw new Error("Missing Garnet credentials in secrets");
}
const client = createClient({
password,
url: normalizeGarnetUrl(endpoint),
username,
});
client.on("error", (error) => {
this.logger.error({ err: error }, "[Garnet] Client error");
});
await client.connect();
this.client = client;
this.logger.info("✓ Garnet connected");
}
async getJson<T>(key: string): Promise<T | null> {
const rawValue = await this.getClient().get(key);
if (!rawValue) {
return null;
}
return JSON.parse(rawValue) as T;
}
async setJson(
key: string,
value: unknown,
ttlSeconds?: number,
): Promise<void> {
const serialized = JSON.stringify(value);
const client = this.getClient();
if (ttlSeconds && ttlSeconds > 0) {
await client.set(key, serialized, { EX: ttlSeconds });
return;
}
await client.set(key, serialized);
}
async getString(key: string): Promise<string | null> {
return this.getClient().get(key);
}
async setString(
key: string,
value: string,
ttlSeconds?: number,
): Promise<void> {
const client = this.getClient();
if (ttlSeconds && ttlSeconds > 0) {
await client.set(key, value, { EX: ttlSeconds });
return;
}
await client.set(key, value);
}
async delete(key: string): Promise<number> {
return this.getClient().del(key);
}
async getHash(key: string): Promise<Record<string, string>> {
return this.getClient().hGetAll(key);
}
async setHash(
key: string,
fields: Record<string, string>,
): Promise<void> {
await this.getClient().hSet(key, fields);
}
async appendStream(
key: string,
fields: Record<string, string>,
options?: { maxLen?: number | undefined },
): Promise<string> {
const trimOptions =
options?.maxLen && options.maxLen > 0
? {
TRIM: {
strategy: "MAXLEN" as const,
strategyModifier: "~" as const,
threshold: options.maxLen,
},
}
: undefined;
return this.getClient().xAdd(key, "*", fields, trimOptions);
}
async scanKeys(pattern: string): Promise<string[]> {
const keys: string[] = [];
for await (const batch of this.getClient().scanIterator({
COUNT: 100,
MATCH: pattern,
})) {
for (const key of batch) {
keys.push(String(key));
}
}
return keys;
}
async close(): Promise<void> {
if (!this.client) {
return;
}
const client = this.client;
this.client = null;
try {
await client.quit();
} catch {
client.destroy();
}
}
}
+12
View File
@@ -0,0 +1,12 @@
export const KNOWN_SECRET_KEYS = [
"NATS_ENDPOINT",
"NATS_PASS",
"NATS_USER",
"GARNET_ENDPOINT",
"GARNET_USERID",
"GARNET_PASSWORD",
"MONGODB_CONNECTION_STRING",
"MONGODB_PROJECT_ID",
] as const;
export type KnownSecretKey = (typeof KNOWN_SECRET_KEYS)[number];
+43
View File
@@ -0,0 +1,43 @@
import mongoose from "mongoose";
import { getLogger } from "@obs/logger";
let connection: mongoose.Connection | null = null;
const logger = getLogger();
export async function initMongoDB(
connectionString: string,
): Promise<mongoose.Connection> {
if (connection !== null) {
return connection;
}
if (!connectionString) {
throw new Error(
"MongoDB connection string is empty. Set MONGODB_CONNECTION_STRING.",
);
}
connection = await mongoose
.createConnection(connectionString, { dbName: "dakao" })
.asPromise();
logger.info("✓ MongoDB connection initialized");
return connection;
}
export function getMongoConnection(): mongoose.Connection {
if (connection === null) {
throw new Error(
"MongoDB connection is not initialized. Call initMongoDB() in main.ts first.",
);
}
return connection;
}
export async function closeMongoDB(): Promise<void> {
if (connection === null) {
return;
}
await connection.close();
connection = null;
}
+137
View File
@@ -0,0 +1,137 @@
import {
type Msg,
type NatsConnection,
StringCodec,
connect,
type Subscription,
} from "nats";
import { getSecret } from "@common/secret-manager";
import { getLogger } from "@obs/logger";
export interface NatsMessageContext<T = unknown> {
data: T;
raw: Msg;
reply: string | undefined;
subject: string;
subscriptionSubject: string;
}
export class NatsClient {
private client: NatsConnection | null = null;
private readonly stringCodec = StringCodec();
private readonly logger = getLogger();
private getConnection(): NatsConnection {
if (!this.client) {
throw new Error("NATS client not connected");
}
return this.client;
}
private decodeMessage<T>(msg: Msg): T {
return JSON.parse(this.stringCodec.decode(msg.data)) as T;
}
async connect(): Promise<void> {
const endpoint = getSecret("NATS_ENDPOINT");
const user = getSecret("NATS_USER");
const password = getSecret("NATS_PASS");
if (!endpoint || !user || !password) {
throw new Error("Missing NATS credentials in secrets");
}
this.client = await connect({
name: process.env["SERVICE_NAME"] || "lolympus-agent",
servers: [endpoint],
tls: {},
maxReconnectAttempts: -1,
reconnectTimeWait: 500,
reconnect: true,
pingInterval: 20000,
timeout: 5000,
user: user,
pass: password,
});
this.logger.info("✓ NATS connected");
}
async subscribe<T = unknown>(
subject: string,
handler: (
data: T,
context: NatsMessageContext<T>,
) => void | Promise<void>,
): Promise<Subscription> {
return this.subscribeRaw(subject, async (msg) => {
const data = this.decodeMessage<T>(msg);
await handler(data, {
data,
raw: msg,
reply: msg.reply || undefined,
subject: msg.subject,
subscriptionSubject: subject,
});
});
}
subscribeRaw(
subject: string,
handler: (msg: Msg) => void | Promise<void>,
): Promise<Subscription> {
const subscription = this.getConnection().subscribe(subject);
void (async () => {
for await (const msg of subscription) {
try {
await handler(msg);
} catch (error) {
this.logger.error(
{ err: error, subject: msg.subject },
"[NATS] Error processing message",
);
}
}
})();
this.logger.info({ subject }, "✓ Subscribed to subject");
return Promise.resolve(subscription);
}
async publish<T = unknown>(subject: string, data: T): Promise<void> {
const client = this.getConnection();
client.publish(subject, this.stringCodec.encode(JSON.stringify(data)));
await client.flush();
}
respond(msg: Msg, data: unknown): Promise<void> {
if (!msg.reply) {
this.logger.warn(
{ subject: msg.subject },
"[NATS] Cannot respond: no reply subject",
);
return Promise.resolve();
}
msg.respond(this.stringCodec.encode(JSON.stringify(data)));
return Promise.resolve();
}
async close(): Promise<void> {
if (!this.client) return;
try {
const closed = this.client.closed();
await this.client.drain();
const err = await closed;
if (err) {
this.logger.error({ err }, "[NATS] Closed with error");
}
} catch {
try {
await this.client.close();
} catch {}
}
}
}
+84
View File
@@ -0,0 +1,84 @@
import { InfisicalSDK } from "@infisical/sdk";
import { KNOWN_SECRET_KEYS, type KnownSecretKey } from "./known-secret-keys.js";
export type SecretMap = Partial<Record<KnownSecretKey, string>>;
export type RawSecretMap = Record<string, string>;
const INFISICAL_SITE_URL = "https://secret-manager.dahyeon.us/";
const KNOWN_SECRET_KEY_SET = new Set<string>(KNOWN_SECRET_KEYS);
const infisicalClient = new InfisicalSDK({ siteUrl: INFISICAL_SITE_URL });
export let secrets: SecretMap | null = null;
export let rawSecrets: RawSecretMap | null = null;
function isKnownSecretKey(value: string): value is KnownSecretKey {
return KNOWN_SECRET_KEY_SET.has(value);
}
function getInfisicalCredentials(): {
clientSecret: string;
clientId: string;
projectId: string;
} | null {
const clientSecret = process.env["INFISICAL_CLIENT_SECRET"] ?? "";
const clientId = process.env["INFISICAL_CLIENT_ID"] ?? "";
const projectId = process.env["INFISICAL_PROJECT_ID"] ?? "";
if (!clientSecret || !clientId || !projectId) {
return null;
}
return { clientSecret, clientId, projectId };
}
function getInfisicalEnvironment(): "dev" | "staging" | "prod" {
if (process.env["NODE_ENV"] === "prod") return "prod";
if (process.env["NODE_ENV"] === "test") return "staging";
return "dev";
}
export function getSecret(key: KnownSecretKey): string | undefined {
return secrets?.[key] ?? process.env[key];
}
export function getRawSecret(key: string): string | undefined {
return rawSecrets?.[key] ?? process.env[key];
}
const fetchSecrets = async (): Promise<{
known: SecretMap;
raw: RawSecretMap;
}> => {
const credentials = getInfisicalCredentials();
if (!credentials) {
return { known: {}, raw: {} };
}
try {
await infisicalClient.auth().universalAuth.login({
clientId: credentials.clientId,
clientSecret: credentials.clientSecret,
});
const res = await infisicalClient.secrets().listSecrets({
environment: getInfisicalEnvironment(),
projectId: credentials.projectId,
});
const known: SecretMap = {};
const raw: RawSecretMap = {};
for (const s of res.secrets) {
if (!s.secretKey) continue;
raw[s.secretKey] = s.secretValue ?? "";
if (isKnownSecretKey(s.secretKey)) {
known[s.secretKey] = s.secretValue ?? "";
}
}
return { known, raw };
} catch {
return { known: {}, raw: {} };
}
};
const loaded = await fetchSecrets();
secrets = loaded.known;
rawSecrets = loaded.raw;