169 lines
5.2 KiB
TypeScript
169 lines
5.2 KiB
TypeScript
import { z } from 'zod';
|
|
|
|
// PostgreSQL configuration
|
|
export const postgresConfigSchema = z.object({
|
|
enabled: z.boolean().default(true),
|
|
host: z.string().default('localhost'),
|
|
port: z.number().default(5432),
|
|
database: z.string(),
|
|
user: z.string(),
|
|
password: z.string(),
|
|
ssl: z.boolean().default(false),
|
|
poolSize: z.number().min(1).max(100).default(10),
|
|
connectionTimeout: z.number().default(30000),
|
|
idleTimeout: z.number().default(10000),
|
|
});
|
|
|
|
// QuestDB configuration
|
|
export const questdbConfigSchema = z.object({
|
|
enabled: z.boolean().default(true),
|
|
host: z.string().default('localhost'),
|
|
ilpPort: z.number().default(9009),
|
|
influxPort: z.number().optional(), // legacy flat alias; explicit alias wins at DI boundary
|
|
httpPort: z.number().default(9000),
|
|
pgPort: z.number().default(8812),
|
|
database: z.string().default('questdb'),
|
|
user: z.string().optional(), // No default - QuestDB doesn't require auth by default
|
|
password: z.string().optional(), // No default - QuestDB doesn't require auth by default
|
|
// Deprecated compatibility fields: DI uses the client, not a background buffered writer.
|
|
bufferSize: z.number().default(65536).describe('Deprecated: no effect on QuestDB client/writer'),
|
|
flushInterval: z
|
|
.number()
|
|
.default(1000)
|
|
.describe('Deprecated: no effect on QuestDB client/writer'),
|
|
});
|
|
|
|
function isValidMongoHostSegment(segment: string, allowPort: boolean): boolean {
|
|
if (!segment || /\s/.test(segment)) {
|
|
return false;
|
|
}
|
|
|
|
if (segment.startsWith('[')) {
|
|
const closeIdx = segment.indexOf(']');
|
|
if (closeIdx <= 1) {
|
|
return false;
|
|
}
|
|
const host = segment.slice(1, closeIdx);
|
|
if (!host || host.includes('[') || host.includes(']')) {
|
|
return false;
|
|
}
|
|
const after = segment.slice(closeIdx + 1);
|
|
if (after === '') {
|
|
return true;
|
|
}
|
|
if (!allowPort || !after.startsWith(':')) {
|
|
return false;
|
|
}
|
|
const portStr = after.slice(1);
|
|
if (portStr === '') {
|
|
return true;
|
|
}
|
|
if (!/^\d+$/.test(portStr)) {
|
|
return false;
|
|
}
|
|
const port = Number(portStr);
|
|
return port >= 1 && port <= 65535;
|
|
}
|
|
|
|
const colonIdx = segment.indexOf(':');
|
|
if (colonIdx === -1) {
|
|
return true;
|
|
}
|
|
if (!allowPort || segment.indexOf(':', colonIdx + 1) !== -1) {
|
|
return false;
|
|
}
|
|
|
|
const host = segment.slice(0, colonIdx);
|
|
const portStr = segment.slice(colonIdx + 1);
|
|
if (!host) {
|
|
return false;
|
|
}
|
|
if (portStr === '') {
|
|
return true;
|
|
}
|
|
if (!/^\d+$/.test(portStr)) {
|
|
return false;
|
|
}
|
|
const port = Number(portStr);
|
|
return port >= 1 && port <= 65535;
|
|
}
|
|
|
|
function isValidMongoUri(uri: string): boolean {
|
|
if (!uri || /\s/.test(uri)) {
|
|
return false;
|
|
}
|
|
|
|
const schemeMatch = /^(mongodb(?:\+srv)?):\/\/(.*)$/i.exec(uri);
|
|
const rawScheme = schemeMatch?.[1];
|
|
const rest = schemeMatch?.[2];
|
|
if (!rawScheme || !rest) {
|
|
return false;
|
|
}
|
|
|
|
const isSrv = rawScheme.toLowerCase() === 'mongodb+srv';
|
|
const delimiterMatch = /[/?#]/.exec(rest);
|
|
const authority = delimiterMatch ? rest.slice(0, delimiterMatch.index) : rest;
|
|
if (!authority) {
|
|
return false;
|
|
}
|
|
|
|
const atIndex = authority.lastIndexOf('@');
|
|
const hostList = atIndex >= 0 ? authority.slice(atIndex + 1) : authority;
|
|
if (!hostList) {
|
|
return false;
|
|
}
|
|
|
|
const segments = hostList.split(',');
|
|
if (isSrv && segments.length !== 1) {
|
|
return false;
|
|
}
|
|
|
|
return segments.every(segment => isValidMongoHostSegment(segment, !isSrv));
|
|
}
|
|
|
|
const mongodbUriSchema = z.string().refine(isValidMongoUri, {
|
|
message: 'Invalid MongoDB connection URI',
|
|
});
|
|
|
|
// MongoDB configuration
|
|
export const mongodbConfigSchema = z.object({
|
|
enabled: z.boolean().default(true),
|
|
uri: mongodbUriSchema, // URI is required and contains all connection info
|
|
readUri: mongodbUriSchema.optional(), // Optional read-only URI (e.g. dev data-pipeline reading QA MongoDB)
|
|
readOnly: z.boolean().default(false).optional(),
|
|
database: z.string(), // Database name for reference
|
|
poolSize: z.number().min(1).max(100).default(10),
|
|
// Optional fields for cases where URI parsing might fail
|
|
host: z.string().default('localhost').optional(),
|
|
port: z.number().default(27017).optional(),
|
|
user: z.string().optional(),
|
|
password: z.string().optional(),
|
|
authSource: z.string().default('admin').optional(),
|
|
replicaSet: z.string().optional(),
|
|
});
|
|
|
|
// Dragonfly/Redis configuration
|
|
export const dragonflyConfigSchema = z.object({
|
|
enabled: z.boolean().default(true),
|
|
host: z.string().default('localhost'),
|
|
port: z.number().default(6379),
|
|
password: z.string().optional(),
|
|
db: z.number().min(0).max(15).default(0),
|
|
username: z.string().optional(),
|
|
keyPrefix: z
|
|
.string()
|
|
.optional()
|
|
.describe('Deprecated at DI boundary: service cache namespaces are fixed'),
|
|
ttl: z.number().optional(),
|
|
// Cache/rate-limiter request retries. BullMQ retains its dedicated worker/producer policy.
|
|
maxRetries: z.number().default(3).describe('Cache/rate-limiter maxRetriesPerRequest'),
|
|
retryDelay: z.number().default(100).describe('Fixed Redis reconnect delay in milliseconds'),
|
|
});
|
|
|
|
// Combined database configuration
|
|
export const databaseConfigSchema = z.object({
|
|
postgres: postgresConfigSchema,
|
|
questdb: questdbConfigSchema,
|
|
mongodb: mongodbConfigSchema,
|
|
dragonfly: dragonflyConfigSchema,
|
|
});
|