Skip to content
Snippets Groups Projects
Commit 73f3c98a authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

Use a single class, and pass in a synchronous flag.

parent d2733712
No related branches found
No related tags found
1 merge request!463MRC mods by Codefenix (2024-10-20)
...@@ -14,28 +14,33 @@ ...@@ -14,28 +14,33 @@
* dns = new DNS(); * dns = new DNS();
* dns.resolve('example.com', handle); * dns.resolve('example.com', handle);
* *
* The DNS_blocking does not use events, so callback arguments are optional: * In synchronous mode, DNS does not use events, so callback arguments are optional:
* *
* load('dns.js'); * load('dns.js');
* dns = new DNS_blocking(); * dns = new DNS(true);
* log(LOG_ERROR, dns.resolve('example.com').toSource()); * log(LOG_ERROR, dns.resolve('example.com').toSource());
* *
*/ */
require('sockdefs.js', 'SOCK_DGRAM'); require('sockdefs.js', 'SOCK_DGRAM');
function DNS(servers) { function DNS(synchronous, servers) {
var nextid = 0; var nextid = 0;
var synchronous = false;
if (this.synchronous === undefined) { if (synchronous === undefined)
Object.defineProperty(this, 'synchronous', {get: function() { synchronous = false;
return synchronous;
}}); Object.defineProperty(this, 'synchronous', {get: function() {
} return synchronous;
}});
this.outstanding = {}; this.outstanding = {};
this.sockets = []; this.sockets = [];
if (this.synchronous)
this.query = this.synchronous_query;
else
this.query = this.asynchronous_query;
function handle_response() { function handle_response() {
var resp = this.dnsObject.handle_response(this); var resp = this.dnsObject.handle_response(this);
...@@ -50,7 +55,8 @@ function DNS(servers) { ...@@ -50,7 +55,8 @@ function DNS(servers) {
var sock = new Socket(SOCK_DGRAM, "dns", server.indexOf(':') >= 0); var sock = new Socket(SOCK_DGRAM, "dns", server.indexOf(':') >= 0);
sock.bind(); sock.bind();
sock.connect(server, 53); sock.connect(server, 53);
sock.cbID = sock.on('read', handle_response); if (!this.synchronous)
sock.cbID = sock.on('read', handle_response);
sock.dnsObject = this; sock.dnsObject = this;
this.sockets.push(sock); this.sockets.push(sock);
}, this); }, this);
...@@ -69,21 +75,6 @@ function DNS(servers) { ...@@ -69,21 +75,6 @@ function DNS(servers) {
}; };
} }
function DNS_blocking(servers) {
var synchronous = false;
Object.defineProperty(this, 'synchronous', {get: function() {
return synchronous;
}});
DNS.call(this, servers);
// Disable callbacks
this.sockets.forEach(function(sock) {
sock.clearOn('read', sock.cbID);
});
}
DNS_blocking.prototype = Object.create(DNS.prototype);
/* /*
* Class properties and methods * Class properties and methods
*/ */
...@@ -497,7 +488,7 @@ DNS.prototype.handle_response = function(sock) { ...@@ -497,7 +488,7 @@ DNS.prototype.handle_response = function(sock) {
return {'q':q, 'ret':ret}; return {'q':q, 'ret':ret};
} }
DNS.prototype.query = function(queries, /* queryStr, type, class, */callback, thisObj, recursive, timeout, failures, failed) { DNS.prototype.asynchronous_query = function(queries, /* queryStr, type, class, */callback, thisObj, recursive, timeout, failures, failed) {
var query; var query;
if (callback === undefined) if (callback === undefined)
...@@ -533,7 +524,7 @@ DNS.prototype.query = function(queries, /* queryStr, type, class, */callback, th ...@@ -533,7 +524,7 @@ DNS.prototype.query = function(queries, /* queryStr, type, class, */callback, th
return undefined; return undefined;
}; };
DNS_blocking.prototype.query = function(queries, callback, thisObj, recursive, timeout, failures, failed) { DNS.prototype.synchronous_query = function(queries, callback, thisObj, recursive, timeout, failures, failed) {
var query; var query;
var ret; var ret;
var socket_array = []; var socket_array = [];
...@@ -609,7 +600,7 @@ DNS.prototype.resolve = function(host, callback, thisObj) ...@@ -609,7 +600,7 @@ DNS.prototype.resolve = function(host, callback, thisObj)
if (host === undefined) if (host === undefined)
throw new Error('No host specified'); throw new Error('No host specified');
if (this.synchronous && callback === undefined) if (!this.synchronous && callback === undefined)
throw new Error('No callback specified'); throw new Error('No callback specified');
if (thisObj === undefined) if (thisObj === undefined)
thisObj = this; thisObj = this;
...@@ -711,7 +702,7 @@ DNS.prototype.resolveTypeClass = function(host, type, class, callback, thisObj) ...@@ -711,7 +702,7 @@ DNS.prototype.resolveTypeClass = function(host, type, class, callback, thisObj)
if (class === undefined) if (class === undefined)
throw new Error('No class specified'); throw new Error('No class specified');
if (this.synchronous && callback === undefined) if (!this.synchronous && callback === undefined)
throw new Error('No callback specified'); throw new Error('No callback specified');
ctx.callback = callback; ctx.callback = callback;
...@@ -748,7 +739,7 @@ DNS.prototype.reverse = function(ip, callback, thisObj) ...@@ -748,7 +739,7 @@ DNS.prototype.reverse = function(ip, callback, thisObj)
if (ip === undefined) if (ip === undefined)
throw new Error('No IP specified'); throw new Error('No IP specified');
if (this.synchronous && callback === undefined) if (!this.synchronous && callback === undefined)
throw new Error('No callback specified'); throw new Error('No callback specified');
if (thisObj === undefined) if (thisObj === undefined)
...@@ -810,7 +801,7 @@ DNS.prototype.resolveMX = function(host, callback, thisObj) ...@@ -810,7 +801,7 @@ DNS.prototype.resolveMX = function(host, callback, thisObj)
if (host === undefined) if (host === undefined)
throw new Error('No host specified'); throw new Error('No host specified');
if (this.synchronous && callback === undefined) if (!this.synchronous && callback === undefined)
throw new Error('No callback specified'); throw new Error('No callback specified');
if (thisObj === undefined) if (thisObj === undefined)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment