feat(server): emit query events with questions, answered status, response time and timestamp

This commit is contained in:
2026-02-20 15:18:30 +00:00
parent ca36d3be0a
commit 4581a6a1e0
3 changed files with 31 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
import * as plugins from './plugins.js';
import { RustDnsBridge, type IDnsQueryEvent, type IIpcDnsAnswer, type IRustDnsConfig } from './classes.rustdnsbridge.js';
import { RustDnsBridge, type IDnsQueryEvent, type IIpcDnsAnswer, type IIpcDnsQuestion, type IRustDnsConfig } from './classes.rustdnsbridge.js';
export interface IDnsServerOptions {
httpsKey: string;
@@ -45,7 +45,18 @@ interface LetsEncryptOptions {
certDir?: string;
}
export class DnsServer {
export interface IDnsQueryCompletedEvent {
/** The original questions from the query */
questions: IIpcDnsQuestion[];
/** Whether any handler answered the query */
answered: boolean;
/** How long handler resolution took (ms) */
responseTimeMs: number;
/** Timestamp of the query */
timestamp: number;
}
export class DnsServer extends plugins.events.EventEmitter {
private bridge: RustDnsBridge;
private handlers: IDnsHandler[] = [];
@@ -57,12 +68,21 @@ export class DnsServer {
private udpServer: any = null;
constructor(private options: IDnsServerOptions) {
super();
this.bridge = new RustDnsBridge();
// Wire up the dnsQuery event to run TypeScript handlers
this.bridge.on('dnsQuery', async (event: IDnsQueryEvent) => {
try {
const startTime = Date.now();
const answers = this.resolveQuery(event);
const responseTimeMs = Date.now() - startTime;
this.emit('query', {
questions: event.questions,
answered: answers.answered,
responseTimeMs,
timestamp: startTime,
} satisfies IDnsQueryCompletedEvent);
await this.bridge.sendQueryResult(
event.correlationId,
answers.answers,