diff --git a/src/lib/bootstrap.js b/src/lib/bootstrap.js index e233ccd67..358177a19 100644 --- a/src/lib/bootstrap.js +++ b/src/lib/bootstrap.js @@ -13,6 +13,7 @@ import {parse_address} from './utils'; import type {Callback} from '@verdaccio/types'; import type {$Application} from 'express'; +import {DEFAULT_PORT} from './constants'; const logger = require('./logger'); @@ -37,7 +38,7 @@ export function getListListenAddresses(argListen: string, configListen: mixed) { } else if (configListen) { addresses = [configListen]; } else { - addresses = ['4873']; + addresses = [DEFAULT_PORT]; } addresses = addresses.map(function(addr) { const parsedAddr = parse_address(addr); diff --git a/src/lib/constants.js b/src/lib/constants.js index 8121a7a06..c4281a2c3 100644 --- a/src/lib/constants.js +++ b/src/lib/constants.js @@ -1,5 +1,8 @@ // @flow +export const DEFAULT_PORT = '4873'; +export const DEFAULT_DOMAIN = 'localhost'; + export const HEADERS = { JSON: 'application/json', JSON_CHARSET: 'application/json; charset=utf-8', diff --git a/src/lib/utils.js b/src/lib/utils.js index 5c87cd2f1..18669b2ae 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -1,16 +1,16 @@ // @flow - -import {generateGravatarUrl} from '../utils/user'; +import _ from 'lodash'; +import fs from 'fs'; import assert from 'assert'; import semver from 'semver'; import YAML from 'js-yaml'; import URL from 'url'; -import fs from 'fs'; -import _ from 'lodash'; import asciidoctor from 'asciidoctor.js'; import createError from 'http-errors'; import marked from 'marked'; -import {HTTP_STATUS, API_ERROR} from './constants'; + +import {HTTP_STATUS, API_ERROR, DEFAULT_PORT, DEFAULT_DOMAIN} from './constants'; +import {generateGravatarUrl} from '../utils/user'; import type {Package} from '@verdaccio/types'; import type {$Request} from 'express'; @@ -219,8 +219,8 @@ function parse_address(urlAddress: any) { if (urlPattern) { return { proto: urlPattern[2] || 'http', - host: urlPattern[6] || urlPattern[7] || 'localhost', - port: urlPattern[8] || '4873', + host: urlPattern[6] || urlPattern[7] || DEFAULT_DOMAIN, + port: urlPattern[8] || DEFAULT_PORT, }; } diff --git a/test/unit/api/cli.spec.js b/test/unit/api/cli.spec.js index 891abe3df..3344d01b5 100644 --- a/test/unit/api/cli.spec.js +++ b/test/unit/api/cli.spec.js @@ -4,6 +4,7 @@ import _ from 'lodash'; import startServer from '../../../src/index'; import {getListListenAddresses} from '../../../src/lib/bootstrap'; import config from '../partials/config/index'; +import {DEFAULT_DOMAIN, DEFAULT_PORT} from '../../../src/lib/constants'; require('../../../src/lib/logger').setup([]); @@ -42,12 +43,12 @@ describe('startServer via API', () => { }); describe('getListListenAddresses test', () => { - test('should return by default 4873', () => { + test(`should return by default ${DEFAULT_PORT}`, () => { const addrs = getListListenAddresses()[0]; expect(addrs.proto).toBe('http'); - expect(addrs.host).toBe('localhost'); - expect(addrs.port).toBe('4873'); + expect(addrs.host).toBe(DEFAULT_DOMAIN); + expect(addrs.port).toBe(DEFAULT_PORT); }); test('should return a list of address and no cli argument provided', () => { diff --git a/test/unit/api/listen_addr.spec.js b/test/unit/api/listen_addr.spec.js index de46e0650..25eabce26 100644 --- a/test/unit/api/listen_addr.spec.js +++ b/test/unit/api/listen_addr.spec.js @@ -1,39 +1,44 @@ import _ from 'lodash'; import {parse_address as parse} from '../../../src/lib/utils'; +import {DEFAULT_DOMAIN, DEFAULT_PORT} from '../../../src/lib/constants'; describe('Parse listen address', () => { function addTest(uri, proto, host, port) { - test(uri, () => { + test(`should parse ${uri}`, () => { + const parsed = parse(uri); + if (_.isNull(proto)) { - expect(parse(uri)).toBeNull(); + expect(parsed).toBeNull(); } else if (port) { - expect(parse(uri)).toEqual({ + expect(parsed).toEqual({ proto, host, port, }); } else { - expect(parse(uri)).toEqual({ + expect(parsed).toEqual({ proto, - host, + path: host, }); } }); } - addTest('4873', 'http', 'localhost', '4873'); - addTest(':4873', 'http', 'localhost', '4873'); - addTest('blah:4873', 'http', 'blah', '4873'); - addTest('http://:4873', 'http', 'localhost', '4873'); - addTest('https::4873', 'https', 'localhost', '4873'); - addTest('https:blah:4873', 'https', 'blah', '4873'); - addTest('https://blah:4873/', 'https', 'blah', '4873'); - addTest('[::1]:4873', 'http', '::1', '4873'); - addTest('https:[::1]:4873', 'https', '::1', '4873'); + addTest(DEFAULT_PORT, 'http', DEFAULT_DOMAIN, DEFAULT_PORT); + addTest(':4873', 'http', DEFAULT_DOMAIN, DEFAULT_PORT); + addTest('blah:4873', 'http', 'blah', DEFAULT_PORT); + addTest('http://:4873', 'http', DEFAULT_DOMAIN, DEFAULT_PORT); + addTest('https::4873', 'https', DEFAULT_DOMAIN, DEFAULT_PORT); + addTest('https:blah:4873', 'https', 'blah', DEFAULT_PORT); + addTest('https://blah:4873/', 'https', 'blah', DEFAULT_PORT); + addTest('[::1]:4873', 'http', '::1', DEFAULT_PORT); + addTest('https:[::1]:4873', 'https', '::1', DEFAULT_PORT); addTest('unix:/tmp/foo.sock', 'http', '/tmp/foo.sock'); addTest('http:unix:foo.sock', 'http', 'foo.sock'); addTest('https://unix:foo.sock', 'https', 'foo.sock'); + addTest('https://unix:foo.sock:34', 'https', 'foo.sock:34'); + addTest('http://foo.sock:34', 'http', 'foo.sock', '34'); addTest('blah', null); addTest('blah://4873', null);