-
Notifications
You must be signed in to change notification settings - Fork 8
/
example-raw.js
59 lines (47 loc) · 1.79 KB
/
example-raw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Sample code to demonstrate the nodejs getdns API usage.
/* eslint-disable no-console */
"use strict";
// NOTE: here using a relative path to getdns for testing purposes.
//const getdns = require("getdns");
const getdns = require("../");
// Usage: node example-raw.js [hostname] [record type]
// Usage: node example-raw.js hostname.example.com AAAA
const hostname = process.argv[2] || "labs.verisigninc.com";
const recordTypeCode = process.argv[3] || "A";
const recordType = getdns["RRTYPE_" + recordTypeCode.toUpperCase()];
const options = {
// Request timeout time in milliseconds.
timeout: 5000,
"upstreams": [
"8.8.8.8",
],
// Always return dnssec status.
return_dnssec_status: true,
};
const callback = (err, result) => {
// If not null, err is an object with msg and code.
// Code maps to a GETDNS_CALLBACK_TYPE.
// Result is a response dictionary.
// A third argument is also supplied as the transaction id.
if (err) {
console.error("Error", "Bad result for lookup", err);
return;
}
console.log(JSON.stringify(result, null, 2));
};
// Create the context with the above options.
// When done with a context, it must be explicitly destroyed, for example in a callback.
const context = getdns.createContext(options);
// Getdns general.
// Third argument may be a dictionary for extensions.
// Last argument must be a callback.
// Returns a transaction id, which may be used to cancel the request.
/* eslint-disable no-unused-vars */
const transactionId = context.general(hostname, recordType, callback);
/* eslint-disable no-unused-vars */
// Cancel a request.
//context.cancel(transactionId);
process.on("beforeExit", () => {
// NOTE: remember to explicitly destroy the context after being done with lookups.
context.destroy();
});