Browse Source

Hotfix for #416 (#429):

- support ip:port scheme
- url-parse -> url
pull/1/head
rozetko 6 years ago
parent
commit
10f46a6e9b
  1. 2
      server/package.json
  2. 2
      server/spec/utils/url.jest.ts
  3. 10
      server/src/utils/url.ts

2
server/package.json

@ -50,7 +50,7 @@
"ts-jest": "^23.1.1", "ts-jest": "^23.1.1",
"ts-loader": "^4.4.1", "ts-loader": "^4.4.1",
"typescript": "^2.8.3", "typescript": "^2.8.3",
"url-parse": "^1.4.4", "url": "^0.11.0",
"webpack": "^4.12.0", "webpack": "^4.12.0",
"webpack-cli": "^3.0.8", "webpack-cli": "^3.0.8",
"zeromq": "^4.6.0" "zeromq": "^4.6.0"

2
server/spec/utils/url.jest.ts

@ -2,6 +2,8 @@ import { normalizeUrl } from '../../src/utils/url';
describe('Normalize URL', function() { describe('Normalize URL', function() {
const cases = [ const cases = [
{ value: '127.0.0.1:8000', expected: 'http://127.0.0.1:8000' },
{ value: '127.0.0.1:8000/', expected: 'http://127.0.0.1:8000' },
{ value: 'localhost:8000', expected: 'http://localhost:8000' }, { value: 'localhost:8000', expected: 'http://localhost:8000' },
{ value: 'localhost:8000/', expected: 'http://localhost:8000' }, { value: 'localhost:8000/', expected: 'http://localhost:8000' },
{ value: 'http://localhost:3000', expected: 'http://localhost:3000' }, { value: 'http://localhost:3000', expected: 'http://localhost:3000' },

10
server/src/utils/url.ts

@ -1,17 +1,17 @@
import * as url from 'url-parse'; import * as url from 'url';
export function normalizeUrl(grafanaUrl: string) { export function normalizeUrl(grafanaUrl: string) {
if(!grafanaUrl) { if(!grafanaUrl) {
return grafanaUrl; return grafanaUrl;
} }
let urlObj = new url(grafanaUrl); let urlObj = url.parse(grafanaUrl);
if(urlObj.protocol !== 'http:' && urlObj.protocol !== 'https:') { if(urlObj.protocol !== 'http:' && urlObj.protocol !== 'https:') {
grafanaUrl = `http://${grafanaUrl}`; grafanaUrl = `http://${grafanaUrl}`;
urlObj = new url(grafanaUrl); urlObj = url.parse(grafanaUrl);
console.log('No protocol provided in GRAFANA_URL -> inserting "http://"'); console.log('No protocol provided in GRAFANA_URL -> inserting "http://"');
} }
if(urlObj.slashes === false) { if(urlObj.slashes === false) {
urlObj = new url(`${urlObj.protocol}//${urlObj.pathname}`); urlObj = url.parse(`${urlObj.protocol}//${urlObj.pathname}`);
console.log('No slashes were provided after the protocol -> inserting slashes'); console.log('No slashes were provided after the protocol -> inserting slashes');
} }
if(urlObj.pathname.slice(-1) === '/') { if(urlObj.pathname.slice(-1) === '/') {
@ -19,7 +19,7 @@ export function normalizeUrl(grafanaUrl: string) {
console.log('Removing the slash at the end of GRAFANA_URL'); console.log('Removing the slash at the end of GRAFANA_URL');
} }
let finalUrl = `${urlObj.protocol}//${urlObj.hostname}`; let finalUrl = `${urlObj.protocol}//${urlObj.hostname}`;
if(urlObj.port !== '') { if(urlObj.port !== null) {
finalUrl = finalUrl + ':' + urlObj.port; finalUrl = finalUrl + ':' + urlObj.port;
} }
if(urlObj.pathname !== '') { if(urlObj.pathname !== '') {

Loading…
Cancel
Save