Class DNSResponse

Default class representing a DNS Response.

DNS Responses contain the serialized packet data, and data about the connection.

Hierarchy

  • TypedEventEmitter<DNSResponseEvents>
    • DNSResponse

Constructors

Properties

connection: Connection

The connection object representing the client connection

errors: {
    notImplemented: (() => void);
    nxDomain: (() => void);
    refused: (() => void);
    serverFailure: (() => void);
} = ...

Helper object that contains a set of common error responses that can be sent to the client.

Calling any of these errors will set the appropriate RCode in the DNS packet and send the response.

Docs

extra: undefined | object

Any extra data that can be attached to the response. Handlers should use this object to attach any extra metadata if desired

fin: boolean = false

A flag to indicate whether the response has been sent

metadata: MessageMetadata

The metadata object for the response

The packet wrapper containing the raw DNS packet

Accessors

Methods

  • Alias for emitter.on(eventName, listener).

    Type Parameters

    • K extends "done" | "answer"

    Parameters

    • event: K
    • listener: ((arg: DNSResponseEvents[K]) => void)
        • (arg): void
        • Parameters

          • arg: DNSResponseEvents[K]

          Returns void

    Returns this

    v0.1.26

  • Send an answer or answers in the response. This method overrides any data that was previously set in the answers section of the DNS packet with the provided answer.

    Parameters

    Returns void

    Docs

  • Return an object representing the data contained in the response.

    Returns {
        connection: Connection;
        extra?: object;
        metadata: MessageMetadata;
        packet: Packet;
    }

    The data object containing the packet, connection, and metadata.

  • Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each.

    Returns true if the event had listeners, false otherwise.

    import { EventEmitter } from 'node:events';
    const myEmitter = new EventEmitter();

    // First listener
    myEmitter.on('event', function firstListener() {
    console.log('Helloooo! first listener');
    });
    // Second listener
    myEmitter.on('event', function secondListener(arg1, arg2) {
    console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
    });
    // Third listener
    myEmitter.on('event', function thirdListener(...args) {
    const parameters = args.join(', ');
    console.log(`event with parameters ${parameters} in third listener`);
    });

    console.log(myEmitter.listeners('event'));

    myEmitter.emit('event', 1, 2, 3, 4, 5);

    // Prints:
    // [
    // [Function: firstListener],
    // [Function: secondListener],
    // [Function: thirdListener]
    // ]
    // Helloooo! first listener
    // event with parameters 1, 2 in second listener
    // event with parameters 1, 2, 3, 4, 5 in third listener

    Type Parameters

    • K extends "done" | "answer"

    Parameters

    • event: K
    • arg: DNSResponseEvents[K]

    Returns boolean

    v0.1.26

  • Alias for emitter.removeListener().

    Type Parameters

    • K extends "done" | "answer"

    Parameters

    • event: K
    • listener: ((arg: DNSResponseEvents[K]) => void)
        • (arg): void
        • Parameters

          • arg: DNSResponseEvents[K]

          Returns void

    Returns this

    v10.0.0

  • Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

    server.on('connection', (stream) => {
    console.log('someone connected!');
    });

    Returns a reference to the EventEmitter, so that calls can be chained.

    By default, event listeners are invoked in the order they are added. The emitter.prependListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

    import { EventEmitter } from 'node:events';
    const myEE = new EventEmitter();
    myEE.on('foo', () => console.log('a'));
    myEE.prependListener('foo', () => console.log('b'));
    myEE.emit('foo');
    // Prints:
    // b
    // a

    Type Parameters

    • K extends "done" | "answer"

    Parameters

    • event: K
    • listener: ((arg: DNSResponseEvents[K]) => void)

      The callback function

        • (arg): void
        • Parameters

          • arg: DNSResponseEvents[K]

          Returns void

    Returns this

    v0.1.101

  • Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.

    server.once('connection', (stream) => {
    console.log('Ah, we have our first user!');
    });

    Returns a reference to the EventEmitter, so that calls can be chained.

    By default, event listeners are invoked in the order they are added. The emitter.prependOnceListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

    import { EventEmitter } from 'node:events';
    const myEE = new EventEmitter();
    myEE.once('foo', () => console.log('a'));
    myEE.prependOnceListener('foo', () => console.log('b'));
    myEE.emit('foo');
    // Prints:
    // b
    // a

    Type Parameters

    • K extends "done" | "answer"

    Parameters

    • event: K
    • listener: ((arg: DNSResponseEvents[K]) => void)

      The callback function

        • (arg): void
        • Parameters

          • arg: DNSResponseEvents[K]

          Returns void

    Returns this

    v0.3.0

  • Removes the specified listener from the listener array for the event named eventName.

    const callback = (stream) => {
    console.log('someone connected!');
    };
    server.on('connection', callback);
    // ...
    server.removeListener('connection', callback);

    removeListener() will remove, at most, one instance of a listener from the listener array. If any single listener has been added multiple times to the listener array for the specified eventName, then removeListener() must be called multiple times to remove each instance.

    Once an event is emitted, all listeners attached to it at the time of emitting are called in order. This implies that any removeListener() or removeAllListeners() calls after emitting and before the last listener finishes execution will not remove them fromemit() in progress. Subsequent events behave as expected.

    import { EventEmitter } from 'node:events';
    class MyEmitter extends EventEmitter {}
    const myEmitter = new MyEmitter();

    const callbackA = () => {
    console.log('A');
    myEmitter.removeListener('event', callbackB);
    };

    const callbackB = () => {
    console.log('B');
    };

    myEmitter.on('event', callbackA);

    myEmitter.on('event', callbackB);

    // callbackA removes listener callbackB but it will still be called.
    // Internal listener array at time of emit [callbackA, callbackB]
    myEmitter.emit('event');
    // Prints:
    // A
    // B

    // callbackB is now removed.
    // Internal listener array [callbackA]
    myEmitter.emit('event');
    // Prints:
    // A

    Because listeners are managed using an internal array, calling this will change the position indices of any listener registered after the listener being removed. This will not impact the order in which listeners are called, but it means that any copies of the listener array as returned by the emitter.listeners() method will need to be recreated.

    When a single function has been added as a handler multiple times for a single event (as in the example below), removeListener() will remove the most recently added instance. In the example the once('ping') listener is removed:

    import { EventEmitter } from 'node:events';
    const ee = new EventEmitter();

    function pong() {
    console.log('pong');
    }

    ee.on('ping', pong);
    ee.once('ping', pong);
    ee.removeListener('ping', pong);

    ee.emit('ping');
    ee.emit('ping');

    Returns a reference to the EventEmitter, so that calls can be chained.

    Type Parameters

    • K extends "done" | "answer"

    Parameters

    • event: K
    • listener: ((arg: DNSResponseEvents[K]) => void)
        • (arg): void
        • Parameters

          • arg: DNSResponseEvents[K]

          Returns void

    Returns this

    v0.1.26

  • Resolve the response with the data that has been set in the packet. This method should be called whenever the server has made direct modifications to the packet and wants to send the response to the client directly as-is.

    Returns void

    Docs