diff --git a/.changeset/polite-rockets-train.md b/.changeset/polite-rockets-train.md new file mode 100644 index 000000000..6039a5411 --- /dev/null +++ b/.changeset/polite-rockets-train.md @@ -0,0 +1,5 @@ +--- +"@monokle/synchronizer": patch +--- + +Imporve origin config fetching by assuring response status and using https as default proto diff --git a/packages/synchronizer/src/__tests__/fetcher.spec.ts b/packages/synchronizer/src/__tests__/fetcher.spec.ts index cc3880937..c8267cd80 100644 --- a/packages/synchronizer/src/__tests__/fetcher.spec.ts +++ b/packages/synchronizer/src/__tests__/fetcher.spec.ts @@ -143,7 +143,7 @@ describe('Fetcher Tests', () => { const server = app.listen(13000, async () => { try { - const originData = await fetchOriginConfig('localhost:13000'); + const originData = await fetchOriginConfig('http://localhost:13000'); assert.equal(originData?.origin, 'http://localhost:13000'); assert.equal(originData?.apiOrigin, 'https://api.monokle.local'); diff --git a/packages/synchronizer/src/handlers/configHandler.ts b/packages/synchronizer/src/handlers/configHandler.ts index 0c74e80e8..4606239a5 100644 --- a/packages/synchronizer/src/handlers/configHandler.ts +++ b/packages/synchronizer/src/handlers/configHandler.ts @@ -26,8 +26,13 @@ export async function fetchOriginConfig(origin: string) { } try { - const configUrl = normalizeUrl(`${origin}/config.js`); + const configUrl = normalize(`${origin}/config.js`); const response = await fetch(configUrl); + + if (!response.ok) { + throw new Error(`Failed to fetch config from ${configUrl} with status ${response.status}: ${response.statusText}`); + } + const responseText = await response.text(); const values = Array.from(responseText.matchAll(/([A-Z_]+)\s*:\s*"(.*?)"/gm)).reduce( @@ -41,7 +46,7 @@ export async function fetchOriginConfig(origin: string) { ); if (values) { - values.origin = normalizeUrl(origin); + values.origin = normalize(origin); values.apiOrigin = values.API_ORIGIN; values.authOrigin = values.OIDC_DISCOVERY_URL; values.schemasOrigin = values.SCHEMA_BASE_URL; @@ -59,3 +64,10 @@ export async function fetchOriginConfig(origin: string) { throw error; } } + +function normalize(url: string) { + return normalizeUrl(url, { + defaultProtocol: 'https:', + normalizeProtocol: true, + }); +}