From 84d1a879c2627357edb747a20fae1d2ae204f7a0 Mon Sep 17 00:00:00 2001 From: anujchoudhary Date: Sat, 10 Jun 2023 00:42:12 -0400 Subject: [PATCH 1/2] src\index.ts added a boolean function name validatingURLusingRegex to verify with regex --- .../src/index.ts | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/docusaurus-plugin-stored-data/src/index.ts b/packages/docusaurus-plugin-stored-data/src/index.ts index 5b06e44..66cd764 100644 --- a/packages/docusaurus-plugin-stored-data/src/index.ts +++ b/packages/docusaurus-plugin-stored-data/src/index.ts @@ -48,9 +48,14 @@ const httpsPromise = ( const getData = async (location: string): Promise => { try { new URL(location); // will throw if invalid URL - - const response = await httpsPromise(location); - return response.body; + if (validatingURLusingRegex(location)){ + const response = await httpsPromise(location); + return response.body; + } + else{ + throw "Location URI is not in the proper format"; + } + } catch (error) { // If it's just an invalid URL, let it fall through to check if it's a file if ( @@ -78,6 +83,16 @@ const getData = async (location: string): Promise => { throw new Error(`Could not determine if ${location} is a URL or file`); }; + +const validatingURLusingRegex = (locationString : string): Boolean => { + const regex = new RegExp('^https?:'); + + if(regex.test(locationString)){ + return true; + } + + return false; +} const pluginStoredData = ( _: LoadContext, { data = {} }: Options, From 688c89abe6bfa70763afb2a42df60874b89ed2cc Mon Sep 17 00:00:00 2001 From: anujchoudhary Date: Mon, 12 Jun 2023 22:40:50 -0400 Subject: [PATCH 2/2] Update index.ts --- .../docusaurus-plugin-stored-data/src/index.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/docusaurus-plugin-stored-data/src/index.ts b/packages/docusaurus-plugin-stored-data/src/index.ts index 66cd764..1691626 100644 --- a/packages/docusaurus-plugin-stored-data/src/index.ts +++ b/packages/docusaurus-plugin-stored-data/src/index.ts @@ -48,7 +48,7 @@ const httpsPromise = ( const getData = async (location: string): Promise => { try { new URL(location); // will throw if invalid URL - if (validatingURLusingRegex(location)){ + if (validateUrl(location)){ const response = await httpsPromise(location); return response.body; } @@ -84,14 +84,13 @@ const getData = async (location: string): Promise => { }; -const validatingURLusingRegex = (locationString : string): Boolean => { - const regex = new RegExp('^https?:'); - - if(regex.test(locationString)){ - return true; - } - +const validateUrl = (locationString : string): Boolean => { + try { + const url = new URL(locationString); + return url.protocol === "http:" || url.protocol === "https:"; + } catch { return false; + } } const pluginStoredData = ( _: LoadContext,