From c7fd781950cf50104ca75df51e98f51516ee6541 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 4 Nov 2022 15:13:59 +0700 Subject: [PATCH] Make `defaultProtocol` option accept protocol without colon --- index.d.ts | 12 +++++------- index.js | 9 +++++++-- index.test-d.ts | 2 +- readme.md | 10 +++++----- test.js | 12 ++++++++++-- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/index.d.ts b/index.d.ts index d9e1635..0dd9203 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,10 +1,8 @@ export type Options = { /** - @default 'http:' - - Values: `'https:' | 'http:'` + @default 'http' */ - readonly defaultProtocol?: string; // TODO: Make this `'https:' | 'http:'` in the next major version. + readonly defaultProtocol?: 'https' | 'http'; /** Prepends `defaultProtocol` to the URL if it's protocol-relative. @@ -23,7 +21,7 @@ export type Options = { readonly normalizeProtocol?: boolean; /** - Normalizes `https:` URLs to `http:`. + Normalizes HTTPS URLs to HTTP. @default false @@ -39,9 +37,9 @@ export type Options = { readonly forceHttp?: boolean; /** - Normalizes `http:` URLs to `https:`. + Normalizes HTTP URLs to HTTPS. - This option can't be used with the `forceHttp` option at the same time. + This option cannot be used with the `forceHttp` option at the same time. @default false diff --git a/index.js b/index.js index 3a24713..47ae24c 100644 --- a/index.js +++ b/index.js @@ -37,7 +37,7 @@ const normalizeDataURL = (urlString, {stripHash}) => { } // Lowercase MIME type - const mimeType = (mediaType.shift() || '').toLowerCase(); + const mimeType = mediaType.shift()?.toLowerCase() ?? ''; const attributes = mediaType .map(attribute => { let [key, value = ''] = attribute.split('=').map(string => string.trim()); @@ -72,7 +72,7 @@ const normalizeDataURL = (urlString, {stripHash}) => { export default function normalizeUrl(urlString, options) { options = { - defaultProtocol: 'http:', + defaultProtocol: 'http', normalizeProtocol: true, forceHttp: false, forceHttps: false, @@ -89,6 +89,11 @@ export default function normalizeUrl(urlString, options) { ...options, }; + // Legacy: Append `:` to the protocol if missing. + if (typeof options.defaultProtocol === 'string' && !options.defaultProtocol.endsWith(':')) { + options.defaultProtocol = `${options.defaultProtocol}:`; + } + urlString = urlString.trim(); // Data URL diff --git a/index.test-d.ts b/index.test-d.ts index 53c17b2..e08b060 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -4,7 +4,7 @@ import normalizeUrl from './index.js'; expectType(normalizeUrl('sindresorhus.com')); expectType(normalizeUrl('HTTP://xn--xample-hva.com:80/?b=bar&a=foo')); -normalizeUrl('//sindresorhus.com:80/', {defaultProtocol: 'https:'}); +normalizeUrl('//sindresorhus.com:80/', {defaultProtocol: 'https'}); normalizeUrl('//sindresorhus.com:80/', {normalizeProtocol: false}); normalizeUrl('https://sindresorhus.com:80/', {forceHttp: true}); normalizeUrl('http://sindresorhus.com:80/', {forceHttps: true}); diff --git a/readme.md b/readme.md index 4b61182..a04c8b6 100644 --- a/readme.md +++ b/readme.md @@ -45,8 +45,8 @@ Type: `object` ##### defaultProtocol Type: `string`\ -Default: `http:`\ -Values: `'https:' | 'http:'` +Default: `'http'`\ +Values: `'https' | 'http'` ##### normalizeProtocol @@ -68,7 +68,7 @@ normalizeUrl('//sindresorhus.com', {normalizeProtocol: false}); Type: `boolean`\ Default: `false` -Normalize `https:` to `http:`. +Normalize HTTPS to HTTP. ```js normalizeUrl('https://sindresorhus.com'); @@ -83,7 +83,7 @@ normalizeUrl('https://sindresorhus.com', {forceHttp: true}); Type: `boolean`\ Default: `false` -Normalize `http:` to `https:`. +Normalize HTTP to HTTPS. ```js normalizeUrl('http://sindresorhus.com'); @@ -93,7 +93,7 @@ normalizeUrl('http://sindresorhus.com', {forceHttps: true}); //=> 'https://sindresorhus.com' ``` -This option can't be used with the `forceHttp` option at the same time. +This option cannot be used with the `forceHttp` option at the same time. ##### stripAuthentication diff --git a/test.js b/test.js index 454f724..4c74a88 100644 --- a/test.js +++ b/test.js @@ -6,7 +6,6 @@ test('main', t => { t.is(normalizeUrl('sindresorhus.com '), 'http://sindresorhus.com'); t.is(normalizeUrl('sindresorhus.com.'), 'http://sindresorhus.com'); t.is(normalizeUrl('SindreSorhus.com'), 'http://sindresorhus.com'); - t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'https:'}), 'https://sindresorhus.com'); t.is(normalizeUrl('HTTP://sindresorhus.com'), 'http://sindresorhus.com'); t.is(normalizeUrl('//sindresorhus.com'), 'http://sindresorhus.com'); t.is(normalizeUrl('http://sindresorhus.com'), 'http://sindresorhus.com'); @@ -42,6 +41,15 @@ test('main', t => { t.is(normalizeUrl('https://i.vimeocdn.com/filter/overlay?src0=https://i.vimeocdn.com/video/598160082_1280x720.jpg&src1=https://f.vimeocdn.com/images_v6/share/play_icon_overlay.png'), 'https://i.vimeocdn.com/filter/overlay?src0=https://i.vimeocdn.com/video/598160082_1280x720.jpg&src1=https://f.vimeocdn.com/images_v6/share/play_icon_overlay.png'); }); +test('defaultProtocol option', t => { + t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'https'}), 'https://sindresorhus.com'); + t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'http'}), 'http://sindresorhus.com'); + + // Legacy + t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'https:'}), 'https://sindresorhus.com'); + t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'http:'}), 'http://sindresorhus.com'); +}); + test('stripAuthentication option', t => { t.is(normalizeUrl('http://user:password@www.sindresorhus.com'), 'http://sindresorhus.com'); t.is(normalizeUrl('https://user:password@www.sindresorhus.com'), 'https://sindresorhus.com'); @@ -365,7 +373,7 @@ test('data URL', t => { // Options. const options = { - defaultProtocol: 'http:', + defaultProtocol: 'http', normalizeProtocol: true, forceHttp: true, stripHash: true,