Skip to content

Proposal: URL.parseIPv4() URL.parseIPv6() #696

Open
@jasnell

Description

@jasnell

I've had a few discussions with folks (most recently at the OpenJS World conference in Austin last week) about the possibility of exposing the IPv4/IPv6 parser included in the URL parser via static functions on the URL class, e.g. something like a URL.parseIPv4(input) and URL.parseIPv6(input). The intent is to both validate and normalize the input (e.g. validate that an IPv6 address is conformant, or converting some IPv4 variant into the standard 4-segment syntax, etc). Hanging these APIs of the proposed URLHost would also make sense.

The usage of the proposed APIs would be straightforward:

const normalizedIpv4 = URL.parseIPv4(input);
// normalizedIpv4 is a string with the serialized parsed result of `input`.

If the input is invalid, a TypeError is thrown.

To make quick checks possible without incurring the cost of allocating the TypeError and associated stack, corresponding URL.isIPv4() and URL.isIPv6() utilities could also be provided.

To give an idea as to whether this would be useful for the ecosystem, consider that the ip-regex module on npm currently has over 12 million downloads per week. Given that this is functionality that is already included internally by URL implementations, it would be fairly trivial to expose it via a new API, thereby eliminating yet another dependency.

I'm not super concerned about the naming of the functions or the exact shape of the API. The key for me is just that we should expose this functionality somehow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions