Closed subins2000 closed 2 years ago
If it returns v4 even though you requested v6, it's a bug and should be properly fixed.
It'd be good if there's a single function to return IPV6/IPV4, if v6 is available :
You can do this yourself with https://github.com/sindresorhus/p-any and eventually Promise.any
My ISP is only assigning me a IPv4 address. So, it is kinda valid that https://api6.ipify.org/ is giving me v4 result.
I'm interested in submitting a patch to add a generic function: IPV6 first, if no then IPV4 which can be done with Cloudflare's API and many others. Would you be interested in your library having such a function ?
Sure. It should be another method with a descriptive name. Any suggestions for the name?
How about simply get
? That'd go good with the module name :
const publicIp = require('public-ip');
(async () => {
console.log(await publicIp.get());
//=> IPv6, if not IPv4
})();
I don't think that's clear enough. If you were reading that code without having read the public-ip
readme, would you know that it would return either IPv6 or IPv4?
For someone who has an application where it doesn't matter if it's v6 or v4 can just use it right ? If they really wanted the specifics, they have 2 perfectly named functions for it.
The ambiguity in just the name get
will also help the dev who cares what the output should be read the README :sweat_smile:
How about .v6or4()
? Not super pretty, but I think it's nice that it's very descriptive what it does.
Ah, that's a better name. I shall work on this.
If anyone wants to work on this, see https://github.com/sindresorhus/public-ip/pull/46 and the feedback given there.
I have changed my mind. It can just be await publicIp()
to get either IPv6 or IPv4, with IPv6 being preferred.
@sindresorhus I'm making a draft for this proposal and I realized it would be fairly easy to implement a synchronous function that sets a timeout for IPv6 and then falls to IPv4. Of course, we could return a Promise to turn this back into asynchronous, but the timeout would still be there. Would this be good enough?
@sindresorhus P.S: how long do you think the default timeout should be?
Currently, in the browser version if v6 function is called and the API request returns a v4 version, then an error is made and we can't get the IP.
It'd be good if there's a single function to return IPV6/IPV4, if v6 is available :