luci-mod-network: DNS; ES6 treatment

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
This commit is contained in:
Paul Donald
2025-10-26 19:02:51 +01:00
parent 2901c18c71
commit 5193e7bcaf

View File

@@ -10,39 +10,36 @@
'require tools.widgets as widgets'; 'require tools.widgets as widgets';
'require tools.dnsrecordhandlers as drh'; 'require tools.dnsrecordhandlers as drh';
var callHostHints; const callHostHints = rpc.declare({
callHostHints = rpc.declare({
object: 'luci-rpc', object: 'luci-rpc',
method: 'getHostHints', method: 'getHostHints',
expect: { '': {} } expect: { '': {} }
}); });
function validateHostname(sid, s) { function validateHostname(sid, s) {
if (s == null || s == '') if (!s) return true;
return true;
if (s.length > 256) if (s.length > 256)
return _('Expecting: %s').format(_('valid hostname')); return _('Expecting: %s').format(_('valid hostname'));
var labels = s.replace(/^\*?\.?|\.$/g, '').split(/\./); const labels = s.replace(/^\*?\.?|\.$/g, '').split(/\./);
for (var i = 0; i < labels.length; i++) for (const label of labels) {
if (!labels[i].match(/^[a-z0-9_](?:[a-z0-9-]{0,61}[a-z0-9])?$/i)) if (!label.match(/^[a-z0-9_](?:[a-z0-9-]{0,61}[a-z0-9])?$/i))
return _('Expecting: %s').format(_('valid hostname')); return _('Expecting: %s').format(_('valid hostname'));
}
return true; return true;
} }
function validateAddressList(sid, s) { function validateAddressList(sid, s) {
if (s == null || s == '') if (!s) return true;
return true;
var m = s.match(/^\/(.+)\/$/), const m = s.match(/^\/(.+)\/$/);
names = m ? m[1].split(/\//) : [ s ]; const names = m ? m[1].split(/\//) : [ s ];
for (var i = 0; i < names.length; i++) { for (const name of names) {
var res = validateHostname(sid, names[i]); const res = validateHostname(sid, name);
if (res !== true) if (res !== true)
return res; return res;
@@ -52,15 +49,14 @@ function validateAddressList(sid, s) {
} }
function validateServerSpec(sid, s) { function validateServerSpec(sid, s) {
if (s == null || s == '') if (!s) return true;
return true;
var m = s.match(/^(\/.*\/)?(.*)$/); let m = s.match(/^(\/.*\/)?(.*)$/);
if (!m) if (!m)
return _('Expecting: %s').format(_('valid hostname')); return _('Expecting: %s').format(_('valid hostname'));
if (m[1] != '//' && m[1] != '/#/') { if (m[1] != '//' && m[1] != '/#/') {
var res = validateAddressList(sid, m[1]); const res = validateAddressList(sid, m[1]);
if (res !== true) if (res !== true)
return res; return res;
} }
@@ -102,7 +98,7 @@ return view.extend({
}, },
render: function([hosts]) { render: function([hosts]) {
var m, s, o, ss, so, dnss; let m, s, o, ss, so, dnss;
let noi18nstrings = { let noi18nstrings = {
etc_hosts: '<code>/etc/hosts</code>', etc_hosts: '<code>/etc/hosts</code>',
@@ -164,8 +160,8 @@ return view.extend({
s.addbtntitle = _('Add server instance', 'Dnsmasq instance'); s.addbtntitle = _('Add server instance', 'Dnsmasq instance');
s.renderContents = function(/* ... */) { s.renderContents = function(/* ... */) {
var renderTask = form.TypedSection.prototype.renderContents.apply(this, arguments), const renderTask = form.TypedSection.prototype.renderContents.apply(this, arguments);
sections = this.cfgsections(); const sections = this.cfgsections();
return Promise.resolve(renderTask).then(function(nodes) { return Promise.resolve(renderTask).then(function(nodes) {
if (sections.length < 2) { if (sections.length < 2) {
@@ -174,9 +170,9 @@ return view.extend({
} }
else { else {
nodes.querySelectorAll('#cbi-dhcp-dnsmasq > .cbi-section-remove').forEach(function(div, i) { nodes.querySelectorAll('#cbi-dhcp-dnsmasq > .cbi-section-remove').forEach(function(div, i) {
var section = uci.get('dhcp', sections[i]), const section = uci.get('dhcp', sections[i]);
hline = div.nextElementSibling, const hline = div.nextElementSibling;
btn = div.firstElementChild; const btn = div.firstElementChild;
if (!section || section['.anonymous']) { if (!section || section['.anonymous']) {
hline.innerText = i ? _('Unnamed instance #%d', 'Dnsmasq instance').format(i+1) : _('Default instance', 'Dnsmasq instance'); hline.innerText = i ? _('Unnamed instance #%d', 'Dnsmasq instance').format(i+1) : _('Default instance', 'Dnsmasq instance');
@@ -447,13 +443,11 @@ return view.extend({
so.rmempty = false; so.rmempty = false;
so.datatype = 'ipaddr("nomask")'; so.datatype = 'ipaddr("nomask")';
var ipaddrs = {}; const ipaddrs = {};
Object.keys(hosts).forEach(function(mac) { Object.keys(hosts).forEach(function(mac) {
var addrs = L.toArray(hosts[mac].ipaddrs || hosts[mac].ipv4); for (const addr of L.toArray(hosts[mac].ipaddrs || hosts[mac].ipv4))
ipaddrs[addr] = hosts[mac].name || mac;
for (var i = 0; i < addrs.length; i++)
ipaddrs[addrs[i]] = hosts[mac].name || mac;
}); });
L.sortedKeys(ipaddrs, null, 'addr').forEach(function(ipv4) { L.sortedKeys(ipaddrs, null, 'addr').forEach(function(ipv4) {