From ee658d0c5ef38934422e7e60ebd10e7d16b8a6e5 Mon Sep 17 00:00:00 2001 From: mrkishi Date: Fri, 25 Feb 2022 15:07:35 -0300 Subject: [PATCH 1/2] fix: allow `localhost` as a valid hostname (#7075) --- .../vite/src/node/__tests__/utils.spec.ts | 78 ++++++++++++------- packages/vite/src/node/utils.ts | 6 +- 2 files changed, 49 insertions(+), 35 deletions(-) diff --git a/packages/vite/src/node/__tests__/utils.spec.ts b/packages/vite/src/node/__tests__/utils.spec.ts index 3bf82f6b86f84b..b4870201374c9c 100644 --- a/packages/vite/src/node/__tests__/utils.spec.ts +++ b/packages/vite/src/node/__tests__/utils.spec.ts @@ -1,40 +1,58 @@ -import { injectQuery, isWindows } from '../utils' +import { injectQuery, isWindows, resolveHostname } from '../utils' -if (isWindows) { - // this test will work incorrectly on unix systems - test('normalize windows path', () => { - expect(injectQuery('C:\\User\\Vite\\Project', 'direct')).toEqual( - 'C:/User/Vite/Project?direct' +describe('injectQuery', () => { + if (isWindows) { + // this test will work incorrectly on unix systems + test('normalize windows path', () => { + expect(injectQuery('C:\\User\\Vite\\Project', 'direct')).toEqual( + 'C:/User/Vite/Project?direct' + ) + }) + } + + test('path with multiple spaces', () => { + expect(injectQuery('/usr/vite/path with space', 'direct')).toEqual( + '/usr/vite/path with space?direct' ) }) -} -test('path with multiple spaces', () => { - expect(injectQuery('/usr/vite/path with space', 'direct')).toEqual( - '/usr/vite/path with space?direct' - ) -}) + test('path with multiple % characters', () => { + expect(injectQuery('/usr/vite/not%20a%20space', 'direct')).toEqual( + '/usr/vite/not%20a%20space?direct' + ) + }) -test('path with multiple % characters', () => { - expect(injectQuery('/usr/vite/not%20a%20space', 'direct')).toEqual( - '/usr/vite/not%20a%20space?direct' - ) -}) + test('path with %25', () => { + expect(injectQuery('/usr/vite/%25hello%25', 'direct')).toEqual( + '/usr/vite/%25hello%25?direct' + ) + }) -test('path with %25', () => { - expect(injectQuery('/usr/vite/%25hello%25', 'direct')).toEqual( - '/usr/vite/%25hello%25?direct' - ) -}) + test('path with unicode', () => { + expect(injectQuery('/usr/vite/東京', 'direct')).toEqual( + '/usr/vite/東京?direct' + ) + }) -test('path with unicode', () => { - expect(injectQuery('/usr/vite/東京', 'direct')).toEqual( - '/usr/vite/東京?direct' - ) + test('path with unicode, space, and %', () => { + expect(injectQuery('/usr/vite/東京 %20 hello', 'direct')).toEqual( + '/usr/vite/東京 %20 hello?direct' + ) + }) }) -test('path with unicode, space, and %', () => { - expect(injectQuery('/usr/vite/東京 %20 hello', 'direct')).toEqual( - '/usr/vite/東京 %20 hello?direct' - ) +describe('resolveHostname', () => { + test('defaults to 127.0.0.1', () => { + expect(resolveHostname(undefined)).toEqual({ + host: '127.0.0.1', + name: 'localhost' + }) + }) + + test('accepts localhost', () => { + expect(resolveHostname('localhost')).toEqual({ + host: 'localhost', + name: 'localhost' + }) + }) }) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index bc1b8588cb212d..818924ad2f785a 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -584,11 +584,7 @@ export function resolveHostname( optionsHost: string | boolean | undefined ): Hostname { let host: string | undefined - if ( - optionsHost === undefined || - optionsHost === false || - optionsHost === 'localhost' - ) { + if (optionsHost === undefined || optionsHost === false) { // Use a secure default host = '127.0.0.1' } else if (optionsHost === true) { From 47a0a2ef9f32cb1aee60c950457b0e6b908a95d2 Mon Sep 17 00:00:00 2001 From: patak-dev Date: Sat, 5 Mar 2022 21:02:53 +0100 Subject: [PATCH 2/2] chore: format --- packages/vite/src/node/__tests__/utils.spec.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/__tests__/utils.spec.ts b/packages/vite/src/node/__tests__/utils.spec.ts index a06235bd65aadf..1162105c3f19c7 100644 --- a/packages/vite/src/node/__tests__/utils.spec.ts +++ b/packages/vite/src/node/__tests__/utils.spec.ts @@ -1,4 +1,9 @@ -import { getPotentialTsSrcPaths, injectQuery, isWindows, resolveHostname } from '../utils' +import { + getPotentialTsSrcPaths, + injectQuery, + isWindows, + resolveHostname +} from '../utils' describe('injectQuery', () => { if (isWindows) {