DEV Community

Devansh Shah
Devansh Shah

Posted on

Telescope: The Plan Is to fix a small bug (1/3)


I saw a message on the Telescope Slack that there was a bug with the URL parser for elastic search where the URL would have the port twice for example if the URL was http://elasticsearch:9200 the parser would add the port without checking if it had one. So, there would be an error because the URL was this http://elasticsearch:9200:9200. Which was causing an error for obvious reasons. My solution was to check if the URL has a port and it would not add a port but if it did not it would. I was going to implement it with the URL in NodeJS. The URL class in NodeJS allows us to check for port and host very easily and I though it would be the perfect solution.


The NodeJS URL Library has the URL class, to use it you can do the following

const link = new URL('http://localhost:3000');


  href: 'http://localhost:3000/',
  origin: 'http://localhost:3000',
  protocol: 'http:',
  username: '',
  password: '',
  host: 'localhost:3000',
  hostname: 'localhost',
  port: '3000',
  pathname: '/',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''

Enter fullscreen mode Exit fullscreen mode

This will create a class with a bunch of properties
which you can then call or add to.

The coolest part is, it will automatically decline invalid properties. For example lets say we have Class above and we wanted to change the port

const link = new URL('http://localhost:3000');
// valid port
link.port = 8000;
console.log(link.href); // http://localhost:8000/

// resetting port
link.port = "";

// invalid port
link.port = "invalid"

console.log(link.href); // http://localhost/

Enter fullscreen mode Exit fullscreen mode

As, you can see the port is automatically declined and just does not accept it as an input.

The Plan

The plan is to use this class to fix the small bug. The Issue is here and I had the following plan use the URL class to fix this at the root of the problem and it would not matter what our environment host and ports are.

Top comments (0)