1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

format helpers.js (#1848)

This commit is contained in:
Tom Limoncelli
2022-12-07 17:14:51 -05:00
committed by GitHub
parent fa52b4f2ae
commit fe03b29ab2

View File

@ -1,9 +1,16 @@
'use strict';
// If you are heavily debugging this code, the "-dev" flag will
// read this file directly instead of using the output of
// `go generate`. You'll still need to run `go generate` before
// you commit the changes.
// How to keep this file clean:
// 1. Add new functions in alphabetical order when it makes sense.
// 2. Run [prettier](https://github.com/prettier/prettier) on the file to ensure
// your code conforms to our coding standard:
// npm install prettier
// node_modules/.bin/prettier --write pkg/js/helpers.js
// Development tip:
// This file is embeded in the binary via "go build". If you are
// debugging/developing this code, it may be faster to specify the
// -dev file to have helpers.js read from the file instead.
var conf = {
registrars: [],
@ -24,7 +31,9 @@ function initialize() {
}
function _isDomain(d) {
return _.isArray(d.nameservers) && _.isArray(d.records) && _.isString(d.name);
return (
_.isArray(d.nameservers) && _.isArray(d.records) && _.isString(d.name)
);
}
// Returns an array of domains which were registered so far during runtime
@ -41,17 +50,17 @@ function NewRegistrar() {
// version of this function.
switch (arguments.length) {
case 1:
return oldNewRegistrar(arguments[0], "-")
return oldNewRegistrar(arguments[0], '-');
case 2:
// x = NewRegistrar("myThing", "THING")
// x = NewRegistrar("myThing", { metakey: metavalue } )
if (typeof arguments[1] === 'object') {
return oldNewRegistrar(arguments[0], "-", arguments[1])
return oldNewRegistrar(arguments[0], '-', arguments[1]);
}
break;
default: // do nothing
}
return oldNewRegistrar.apply(null, arguments)
return oldNewRegistrar.apply(null, arguments);
}
function oldNewRegistrar(name, type, meta) {
if (type) {
@ -67,17 +76,17 @@ function NewDnsProvider(name, type, meta) {
// version of this function.
switch (arguments.length) {
case 1:
return oldNewDnsProvider(arguments[0], "-")
return oldNewDnsProvider(arguments[0], '-');
case 2:
// x = NewDnsProvider("myThing", "THING")
// x = NewDnsProvider("myThing", { metakey: metavalue } )
if (typeof arguments[1] === 'object') {
return oldNewDnsProvider(arguments[0], "-", arguments[1])
return oldNewDnsProvider(arguments[0], '-', arguments[1]);
}
break;
default: // do nothing
}
return oldNewDnsProvider.apply(null, arguments)
return oldNewDnsProvider.apply(null, arguments);
}
function oldNewDnsProvider(name, type, meta) {
if (typeof meta === 'object' && 'ip_conversions' in meta) {
@ -117,10 +126,12 @@ function processDargs(m, domain) {
} else if (_.isObject(m)) {
_.extend(domain.meta, m);
} else {
throw 'WARNING: domain modifier type unsupported: ' +
throw (
'WARNING: domain modifier type unsupported: ' +
typeof m +
' Domain: ' +
domain.name;
domain.name
);
}
}
@ -144,20 +155,29 @@ function D(name, registrar) {
function INCLUDE(name) {
var domain = _getDomainObject(name);
if (domain == null) {
throw name + ' was not declared yet and therefore cannot be updated. Use D() before.';
throw (
name +
' was not declared yet and therefore cannot be updated. Use D() before.'
);
}
return function (d) {
d.records.push.apply(d.records, domain.obj.records);
}
};
}
// D_EXTEND(name): Update a DNS Domain already added with D(), or subdomain thereof
function D_EXTEND(name) {
var domain = _getDomainObject(name);
if (domain == null) {
throw name + ' was not declared yet and therefore cannot be updated. Use D() before.';
throw (
name +
' was not declared yet and therefore cannot be updated. Use D() before.'
);
}
domain.obj.subdomain = name.substr(0, name.length-domain.obj.name.length - 1);
domain.obj.subdomain = name.substr(
0,
name.length - domain.obj.name.length - 1
);
for (var i = 1; i < arguments.length; i++) {
var m = arguments[i];
processDargs(m, domain.obj);
@ -173,8 +193,8 @@ function _getDomainObject(name) {
var domain = null;
var domainLen = 0;
for (var i = 0; i < conf.domains.length; i++) {
var thisName = conf.domains[i]["name"];
var desiredSuffix = "." + thisName;
var thisName = conf.domains[i]['name'];
var desiredSuffix = '.' + thisName;
var foundSuffix = name.substr(-desiredSuffix.length);
// If this is an exact match or the suffix matches...
if (name === thisName || foundSuffix === desiredSuffix) {
@ -367,13 +387,13 @@ var CAA = recordBuilder('CAA', {
var CNAME = recordBuilder('CNAME');
// DS(name, keytag, algorithm, digestype, digest)
var DS = recordBuilder("DS", {
var DS = recordBuilder('DS', {
args: [
['name', _.isString],
['keytag', _.isNumber],
['algorithm', _.isNumber],
['digesttype', _.isNumber],
['digest', _.isString]
['digest', _.isString],
],
transform: function (record, args, modifiers) {
record.name = args.name;
@ -488,7 +508,6 @@ function isStringOrArray(x) {
return _.isString(x) || _.isArray(x);
}
// AUTOSPLIT is deprecated. It is now a no-op.
var AUTOSPLIT = {};
@ -506,7 +525,7 @@ var TXT = recordBuilder('TXT', {
record.target = args.target; // Overwritten by the Go code
} else {
record.txtstrings = args.target;
record.target = args.target.join(""); // Overwritten by the Go code
record.target = args.target.join(''); // Overwritten by the Go code
}
},
});
@ -587,7 +606,7 @@ function IGNORE(name) {
// IGNORE_NAME(name, rTypes)
function IGNORE_NAME(name, rTypes) {
if (rTypes === undefined) {
rTypes = "*";
rTypes = '*';
}
return function (d) {
d.ignored_names.push({ pattern: name, types: rTypes });
@ -595,7 +614,7 @@ function IGNORE_NAME(name, rTypes) {
}
var IGNORE_NAME_DISABLE_SAFETY_CHECK = {
ignore_name_disable_safety_check: "true"
ignore_name_disable_safety_check: 'true',
// This disables a safety check intended to prevent:
// 1. Two owners toggling a record between two settings.
// 2. The other owner wiping all records at this label, which won't
@ -610,7 +629,6 @@ function IGNORE_TARGET(target, rType) {
};
}
// IMPORT_TRANSFORM(translation_table, domain)
var IMPORT_TRANSFORM = recordBuilder('IMPORT_TRANSFORM', {
args: [['translation_table'], ['domain'], ['ttl', _.isNumber]],
@ -638,14 +656,14 @@ function NO_PURGE(d) {
// "on" Enable AUTODNSSEC for this domain
// "off" Disable AUTODNSSEC for this domain
function AUTODNSSEC_ON(d) {
d.auto_dnssec = "on";
d.auto_dnssec = 'on';
}
function AUTODNSSEC_OFF(d) {
d.auto_dnssec = "off";
d.auto_dnssec = 'off';
}
function AUTODNSSEC(d) {
console.log(
"WARNING: AUTODNSSEC is deprecated. It is now a no-op. Please use AUTODNSSEC_ON or AUTODNSSEC_OFF. The default is to make no modifications. This message will disappear in a future release."
'WARNING: AUTODNSSEC is deprecated. It is now a no-op. Please use AUTODNSSEC_ON or AUTODNSSEC_OFF. The default is to make no modifications. This message will disappear in a future release.'
);
}
@ -714,14 +732,16 @@ function recordBuilder(type, opts) {
return item[0];
})
.join(', ');
throw type +
throw (
type +
' record requires ' +
opts.args.length +
' arguments (' +
argumentsList +
'). Only ' +
arguments.length +
' were supplied';
' were supplied'
);
return;
}
@ -732,10 +752,12 @@ function recordBuilder(type, opts) {
if (argDefinition.length > 1) {
// run validator if supplied
if (!argDefinition[1](value)) {
throw type +
throw (
type +
' record ' +
argDefinition[0] +
' argument validation failed';
' argument validation failed'
);
}
}
parsedArgs[argDefinition[0]] = value;
@ -757,11 +779,13 @@ function recordBuilder(type, opts) {
opts.transform(record, parsedArgs, modifiers);
// Handle D_EXTEND() with subdomains.
if (d.subdomain &&
if (
d.subdomain &&
record.type != 'CF_REDIRECT' &&
record.type != 'CF_TEMP_REDIRECT' &&
record.type != 'CF_WORKER_ROUTE') {
fqdn = [d.subdomain, d.name].join(".")
record.type != 'CF_WORKER_ROUTE'
) {
fqdn = [d.subdomain, d.name].join('.');
record.subdomain = d.subdomain;
if (record.name == '@') {
@ -905,7 +929,6 @@ var CF_WORKER_ROUTE = recordBuilder('CF_WORKER_ROUTE', {
},
});
var URL = recordBuilder('URL');
var URL301 = recordBuilder('URL301');
var FRAME = recordBuilder('FRAME');
@ -1055,7 +1078,11 @@ function DMARC_BUILDER(value) {
value.policy = 'none';
}
if (!value.policy === 'none' || !value.policy === 'quarantine' || !value.policy === 'reject') {
if (
!value.policy === 'none' ||
!value.policy === 'quarantine' ||
!value.policy === 'reject'
) {
throw 'Invalid DMARC policy';
}
@ -1064,7 +1091,11 @@ function DMARC_BUILDER(value) {
record.push('p=' + value.policy);
// Subdomain policy
if (!value.subdomainPolicy === 'none' || !value.subdomainPolicy === 'quarantine' || !value.subdomainPolicy === 'reject') {
if (
!value.subdomainPolicy === 'none' ||
!value.subdomainPolicy === 'quarantine' ||
!value.subdomainPolicy === 'reject'
) {
throw 'Invalid DMARC subdomain policy';
}
if (value.subdomainPolicy) {
@ -1173,19 +1204,19 @@ function DKIM(arr) {
// As the main function (in Go) is in our control anyway, all the values here are already sanity-checked.
// Note: glob() is only an internal undocumented helper function. So use it on your own risk.
function require_glob() {
arguments[2] = "js"; // force to only include .js files.
arguments[2] = 'js'; // force to only include .js files.
var files = glob.apply(null, arguments);
for (i = 0; i < files.length; i++) {
require(files[i]);
}
return files
return files;
}
// Set default values for CLI variables
function CLI_DEFAULTS(defaults) {
for (var key in defaults) {
if (typeof this[key] === "undefined") {
this[key] = defaults[key]
if (typeof this[key] === 'undefined') {
this[key] = defaults[key];
}
}
}
@ -1233,7 +1264,7 @@ var END = {}; // This is null. It permits the last item to include a comma.
// Record modifiers:
// Permit labels like "foo.bar.com.bar.com" (normally an error):
var DISABLE_REPEATED_DOMAIN_CHECK = { skip_fqdn_check: "true" };
var DISABLE_REPEATED_DOMAIN_CHECK = { skip_fqdn_check: 'true' };
// D("bar.com", ...
// A("foo.bar.com", "10.1.1.1", DISABLE_REPEATED_DOMAIN_CHECK),
// )