From 4194cce60f7d9a5664ef9aea279f1f69631cdc84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Kishi?= Date: Sat, 5 Mar 2022 17:16:47 -0300 Subject: [PATCH] fix: allow `localhost` as a valid hostname (#7092) --- .../vite/src/node/__tests__/utils.spec.ts | 83 ++++++++++++------- packages/vite/src/node/utils.ts | 6 +- 2 files changed, 54 insertions(+), 35 deletions(-) diff --git a/packages/vite/src/node/__tests__/utils.spec.ts b/packages/vite/src/node/__tests__/utils.spec.ts index 26b149cd48b8af..1162105c3f19c7 100644 --- a/packages/vite/src/node/__tests__/utils.spec.ts +++ b/packages/vite/src/node/__tests__/utils.spec.ts @@ -1,42 +1,65 @@ -import { getPotentialTsSrcPaths, injectQuery, isWindows } from '../utils' +import { + getPotentialTsSrcPaths, + 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' + }) + }) }) test('ts import of file with .js extension', () => { diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index f830944da879cc..e5b903a05a41ce 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -638,11 +638,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) {