diff --git a/package.json b/package.json index a86accd..bbc5124 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nodename-core", - "version": "0.3.1", + "version": "0.3.2", "main": "lib/index.js", "types": "lib/index.d.ts", "author": "Fabian Stamm ", diff --git a/src/core.ts b/src/core.ts index dfb48cf..afa103a 100644 --- a/src/core.ts +++ b/src/core.ts @@ -31,6 +31,10 @@ export interface Plugin { init(core: DnsCore): Promise } +export type ListenerCallback = + ((data: Buffer, sender: string, max_size?: number) => Promise) | + ((data: Buffer, sender: string, answer: Writable) => Promise) + export interface ListenerPlugin extends Plugin { /** * This method is for registering a callback that is called @@ -38,8 +42,7 @@ export interface ListenerPlugin extends Plugin { * The function returns the Answer data. Errors shouldn't be possible * by this function */ - registerCallback(callback: (data: Buffer, sender: string, max_size?: number) => Promise): void - registerCallback(callback: (data: Buffer, sender: string, answer: Writable) => Promise) + registerCallback(callback: ListenerCallback): void } export class ListenerManager { @@ -53,7 +56,7 @@ export class ListenerManager { return Promise.all(this._listener.map(e => e.init(core))) } - registerCallback(callback: (data: Buffer, sender: string, max_size?: number) => Promise) { + registerCallback(callback: ListenerCallback) { this._listener.forEach(e => e.registerCallback(callback)) } } @@ -282,7 +285,13 @@ export class DnsCore { this.listenerManager.registerCallback(this.onMessage.bind(this)); } - private async onMessage(data: Buffer, sender: string, max_size: number = 0) { + private onMessage: ListenerCallback = async (data: Buffer, sender: string, r) => { + let max_size = 128 * 1024; + let stream = true; + if (typeof r === "number") { + max_size = r; + stream = false; + } let request = new Request(data, sender, max_size); let questionqueue: Promise[] = []; let handleQuestion = question => { @@ -304,6 +313,11 @@ export class DnsCore { } request.questions.map(q => handleQuestion(q)); await Promise.all(questionqueue); - return request.serialize(); + let res = request.serialize(); + if (stream) { + (r).write(res); + } else { + return r; + } } } \ No newline at end of file