getting a reference to currently required file name

32 views
Skip to first unread message

Stefan

unread,
Jul 27, 2025, 6:31:28 AMJul 27
to DNSControl-discuss
Hello

Is there a way to get a reference to get a reference to the filename that is currently being require'd (without the .js part)?
I would like to be able to setup domains like this:
D( FILENAME_WITHOUT_JS, [...] )

The reason for this:
I have a somewhat bigger list of domains and do sometimes impulsively buy new ones (I'm sure some people using DNSControl can relate). I have separated all domain definitions into separate files that have the domain's name as filename, e.g. "example.com" is defined in "example.com.js". My dnscontrol.js file is basically just:
require_glob("./domains/");

I have a script that collects all available domain names using a combination of list-zones and API calls for registrars that are not supported by DNSControl, and compares that to the domain definition files on disk. That way I get a notification if I un-/register a domain and have not yet added it to DNSControl.

And with that reference, I could save a few seconds each time, when copying the parked domain template by not having to replace the domain string in the file. I could just symlink the template, which would make it very easy to separate all parked domains from the in-use ones.

Thanks,
Stefan

Tom Limoncelli

unread,
Jul 28, 2025, 11:04:11 AMJul 28
to Stefan, DNSControl-discuss
Hey friend!

The require*() functions don't expose the current filename.

How about something like this?

Here's a list of all the domains mentioned in dnsconfig.js

    dnscontrol print-ir | jq -r '.domains[].name'  | sort >/tmp/mentioned

Put the list of domains that you gathered via the API in a file called /tmp/gathered (the list must be sorted for "comm" to work)

    # List the domains that are new and aren't mentioned in dnsconfig.js:
    comm -13 /tmp/mentioned  /tmp/gathered

    # List the domains that are obsolete (don't appear in /tmp/gathered)
    comm -23 /tmp/mentioned  /tmp/gathered

Tom

--
You received this message because you are subscribed to the Google Groups "DNSControl-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dnscontrol-disc...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/dnscontrol-discuss/78386eaa-3205-4ddf-a1df-2dd78cb53376n%40googlegroups.com.
Message has been deleted
Message has been deleted

Stefan

unread,
Jul 29, 2025, 6:23:38 AMJul 29
to DNSControl-discuss
Thank you for the "print-ir" hint, that is very useful in another context.

Luckily for the solution to my original issue, it turns out "require_glob" is implemented in JS inside DNSControl, so I just copy&paste'd the code from https://github.com/StackExchange/dnscontrol/blob/74f2005ee7f72c410f993d37277396c81160fbfb/pkg/js/helpers.js#L2107 and added setting a global variable before the require statement.

Kind regards,
Stefan

Stefan

unread,
Jul 29, 2025, 6:23:43 AMJul 29
to DNSControl-discuss
Oh nice, I did not know about `print-ir`. That is useful. Thanks
It does not solve the problem I wanted to solve - not having to type the domains name in the definitions.
But it might allow me to do it another way ... and it will definitely allow me to get rid of some grep commands that require conventions in my code to work.

Also I just stumbled upon https://github.com/is-a-dev/register/blob/main/dnsconfig.js#L5 ... which does a file glob "by hand", and then requires them manually, maybe that will allow for some interesting approach.  I will tinker on.

Thanks for your pointers & quick responses.
DNSControl is a gem of a program, and I do like the community very much.  🙇🏻

Kind regards,
Stefan
On Monday, July 28, 2025 at 5:04:11 PM UTC+2 tlimo...@stackoverflow.com wrote:

Tom Limoncelli

unread,
Jul 29, 2025, 9:28:51 AMJul 29
to Stefan, DNSControl-discuss
On Tue, Jul 29, 2025 at 6:23 AM Stefan <ste...@automat.berlin> wrote:
Thank you for the "print-ir" hint, that is very useful in another context.
 
Glad to help!
 
Luckily for the solution to my original issue, it turns out "require_glob" is implemented in JS inside DNSControl, so I just copy&paste'd the code from https://github.com/StackExchange/dnscontrol/blob/74f2005ee7f72c410f993d37277396c81160fbfb/pkg/js/helpers.js#L2107 and added setting a global variable before the require statement.

If you think others would find this useful, please submit a PR.

Tom

TmOnlineMapper

unread,
Jul 29, 2025, 1:16:06 PMJul 29
to DNSControl-discuss
In general I think a documented glob function that's part of the API would be the better choice here.

After all, having the path name of your current file is nice, but being able to customize globbing and all that jazz seems so much more powerful without introducing a lot of risk I believe.
If you believe there to be a general security risk through offering general file access through a public API, then restricting it to the path of either the dnsconfig.js would be a compromise. Then you still can go wild with globbing, while making sure the file system as a whole isn't accessible.

In any case I'm a huge fan of self referential code like this. Always opens a lot of possibilities for creating very human readable/parsable configs at the cost of having to hide some ugly magic, but if done right it's always an improvement to people not involved in that magic part.
Reply all
Reply to author
Forward
0 new messages