Skip to content

Commit

Permalink
Merge pull request #579 from kubeshop/f1ames/fix/synchronizer-origin-…
Browse files Browse the repository at this point in the history
…protocol

Improve origin config fetching
  • Loading branch information
f1ames authored Dec 1, 2023
2 parents f1c616d + f9780f9 commit 2b73017
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-rockets-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@monokle/synchronizer": patch
---

Imporve origin config fetching by assuring response status and using https as default proto
2 changes: 1 addition & 1 deletion packages/synchronizer/src/__tests__/fetcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
16 changes: 14 additions & 2 deletions packages/synchronizer/src/handlers/configHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
Expand All @@ -59,3 +64,10 @@ export async function fetchOriginConfig(origin: string) {
throw error;
}
}

function normalize(url: string) {
return normalizeUrl(url, {
defaultProtocol: 'https:',
normalizeProtocol: true,
});
}

0 comments on commit 2b73017

Please sign in to comment.