Skip to content

Commit

Permalink
fix: do not prepend PROXY_PREFIX if already prepends path (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
afgiel authored Jul 30, 2024
1 parent c4294be commit c564b2d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/utils/__tests__/matchAndRewriteRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,20 @@ describe('matchAndRewriteURL', () => {
expect(base.toString()).toEqual('https://1234567890.discordsays.com/.proxy/');
});

it("Doesn't apply /.proxy/ if it already prepends the path", () => {
const noPrepend = attemptRemap({
url: new URL('https://1234567890.discordsays.com/.proxy/api/token'),
mappings: [],
});
expect(noPrepend.toString()).toEqual('https://1234567890.discordsays.com/.proxy/api/token');

const prepend = attemptRemap({
url: new URL('https://1234567890.discordsays.com/path/before/.proxy/api/token'),
mappings: [],
});
expect(prepend.toString()).toEqual('https://1234567890.discordsays.com/.proxy/path/before/.proxy/api/token');
});

it("Doesn't apply trailing slash to complete filenames", () => {
const prefixHost = '123456789012345678.discordsays.com';
const target = 'domain.com';
Expand Down
9 changes: 7 additions & 2 deletions src/utils/patchUrlMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ interface PatchUrlMappingsConfig {
patchSrcAttributes?: boolean;
}

const PROXY_PREFIX = '/.proxy';

export function patchUrlMappings(
mappings: Mapping[],
{patchFetch = true, patchWebSocket = true, patchXhr = true, patchSrcAttributes = false}: PatchUrlMappingsConfig = {},
Expand Down Expand Up @@ -132,8 +134,11 @@ function attemptSetNodeSrc(node: Node, mappings: Mapping[]) {

export function attemptRemap({url, mappings}: RemapInput): URL {
const newURL = new URL(url.toString());
if (newURL.hostname.includes('discordsays.com') || newURL.hostname.includes('discordsez.com')) {
newURL.pathname = '/.proxy' + newURL.pathname;
if (
(newURL.hostname.includes('discordsays.com') || newURL.hostname.includes('discordsez.com')) &&
!newURL.pathname.startsWith(PROXY_PREFIX)
) {
newURL.pathname = PROXY_PREFIX + newURL.pathname;
}
for (const mapping of mappings) {
const mapped = matchAndRewriteURL({
Expand Down

0 comments on commit c564b2d

Please sign in to comment.