From e98d4aba59cb81744a7c3392f70b952f05d475df Mon Sep 17 00:00:00 2001 From: Amir Tadrisi Date: Tue, 14 Nov 2023 10:20:35 -0500 Subject: [PATCH 1/4] Fix: Handle Request objects in Segment fetch override --- lms/templates/widgets/segment-io.html | 45 +++++++++++++++++++++------ 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/lms/templates/widgets/segment-io.html b/lms/templates/widgets/segment-io.html index 5f8da23000e8..bfde404d9bec 100644 --- a/lms/templates/widgets/segment-io.html +++ b/lms/templates/widgets/segment-io.html @@ -9,18 +9,45 @@ !function(){ var originalAPI = '${settings.SEGMENT_ORIGINAL_API}'; var replicateAPI = '${settings.SEGMENT_REPLICATE_API}'; - function replaceFetchResourceForSegmentSite(resource){ - if (resource.substr(0, originalAPI.length) === originalAPI) { - resource = replicateAPI + resource.substr(originalAPI.length); + function replaceFetchResourceForSegmentSite(resource) { + // Helper function to replace the URL + function replaceUrl(url) { + if (url.substr(0, originalAPI.length) === originalAPI) { + return replicateAPI + url.substr(originalAPI.length); + } + return url; + } + + // Check if resource is a string (a URL) + if (typeof resource === 'string') { + return replaceUrl(resource); + } else if (resource instanceof Request) { + // If resource is a Request object, create a new Request with a replaced URL + const newUrl = replaceUrl(resource.url); + return new Request(newUrl, { + method: resource.method, + headers: resource.headers, + body: resource.body, + mode: resource.mode, + credentials: resource.credentials, + cache: resource.cache, + redirect: resource.redirect, + referrer: resource.referrer, + integrity: resource.integrity, + keepalive: resource.keepalive, + signal: resource.signal + }); + } else { + // If it's neither a string nor a Request object, log an error or handle as needed + console.error('replaceFetchResourceForSegmentSite was called with an unexpected argument type'); + return resource; } - return resource; } - const { fetch: originalFetch } = window; + // Override the fetch function to use the replaceFetchResourceForSegmentSite function + const originalFetch = window.fetch; window.fetch = async (...args) => { - let [resource, config ] = args; - resource = replaceFetchResourceForSegmentSite(resource); - const response = await originalFetch(resource, config); - return response; + args[0] = replaceFetchResourceForSegmentSite(args[0]); + return originalFetch.apply(window, args); }; }(); From 17ec2c0ffa97066784aa7ba9713e2c60f9ce9b95 Mon Sep 17 00:00:00 2001 From: Amir Tadrisi Date: Tue, 14 Nov 2023 16:38:37 -0500 Subject: [PATCH 2/4] chore: log the type of resource passed to replaceFetchResourceForSegmentSite in case of error --- lms/templates/widgets/segment-io.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lms/templates/widgets/segment-io.html b/lms/templates/widgets/segment-io.html index bfde404d9bec..e794d82cbf1f 100644 --- a/lms/templates/widgets/segment-io.html +++ b/lms/templates/widgets/segment-io.html @@ -39,7 +39,7 @@ }); } else { // If it's neither a string nor a Request object, log an error or handle as needed - console.error('replaceFetchResourceForSegmentSite was called with an unexpected argument type'); + console.error('replaceFetchResourceForSegmentSite was called with an unexpected argument type:', typeof resource, resource); return resource; } } From e5071ecb0f580b974b8f5ef6822dff84fda32574 Mon Sep 17 00:00:00 2001 From: Amir Tadrisi Date: Tue, 14 Nov 2023 16:39:24 -0500 Subject: [PATCH 3/4] chore: log warning instead of error for replaceFetchResourceForSegmentSite --- lms/templates/widgets/segment-io.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lms/templates/widgets/segment-io.html b/lms/templates/widgets/segment-io.html index e794d82cbf1f..ed3f549bfb7d 100644 --- a/lms/templates/widgets/segment-io.html +++ b/lms/templates/widgets/segment-io.html @@ -38,8 +38,8 @@ signal: resource.signal }); } else { - // If it's neither a string nor a Request object, log an error or handle as needed - console.error('replaceFetchResourceForSegmentSite was called with an unexpected argument type:', typeof resource, resource); + // If it's neither a string nor a Request object, log a warning or handle as needed + console.warn('replaceFetchResourceForSegmentSite was called with an unexpected argument type:', typeof resource, resource); return resource; } } From eab140aa57ea8f1dc1fa43b4029c3582b3d5bc63 Mon Sep 17 00:00:00 2001 From: Amir Tadrisi Date: Tue, 14 Nov 2023 16:50:42 -0500 Subject: [PATCH 4/4] chore: handle URL objects for replaceFetchResourceForSegmentSite --- lms/templates/widgets/segment-io.html | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lms/templates/widgets/segment-io.html b/lms/templates/widgets/segment-io.html index ed3f549bfb7d..49a9394acb44 100644 --- a/lms/templates/widgets/segment-io.html +++ b/lms/templates/widgets/segment-io.html @@ -37,6 +37,9 @@ keepalive: resource.keepalive, signal: resource.signal }); + } else if (resource instanceof URL) { + // If resource is a URL object, convert it to a string and replace the URL + return replaceUrl(resource.href); } else { // If it's neither a string nor a Request object, log a warning or handle as needed console.warn('replaceFetchResourceForSegmentSite was called with an unexpected argument type:', typeof resource, resource);