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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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