parent
da5285ac9b
commit
55354c2381
@ -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 <dev@fabianstamm.de>",
|
||||
|
24
src/core.ts
24
src/core.ts
@ -31,6 +31,10 @@ export interface Plugin {
|
||||
init(core: DnsCore): Promise<void>
|
||||
}
|
||||
|
||||
export type ListenerCallback =
|
||||
((data: Buffer, sender: string, max_size?: number) => Promise<Buffer>) |
|
||||
((data: Buffer, sender: string, answer: Writable) => Promise<void>)
|
||||
|
||||
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<Buffer>): void
|
||||
registerCallback(callback: (data: Buffer, sender: string, answer: Writable) => Promise<void>)
|
||||
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<Buffer>) {
|
||||
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<void>[] = [];
|
||||
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) {
|
||||
(<Writable>r).write(res);
|
||||
} else {
|
||||
return r;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user