forked from motion-canvas/motion-canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add viaProxy Utility to rewrite requests to proxy (motion…
- Loading branch information
1 parent
39443c4
commit 15264ba
Showing
6 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import {afterEach, describe, expect, test, vi} from 'vitest'; | ||
import {viaProxy} from './viaProxy'; | ||
|
||
const windowMock = { | ||
location: { | ||
toString: () => 'https://mockhostname:1234', | ||
}, | ||
}; | ||
|
||
describe('viaProxy()', () => { | ||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
vi.mock('virtual:proxysettings', () => ({enabled: false})); | ||
|
||
describe('Pass requests as-is if proxying is disabled', () => { | ||
test.each([ | ||
['Remote Address', 'https://example.com/image'], | ||
['Local Address', 'https://mockhostname:1234/image'], | ||
['Local Address No Host', '/image'], | ||
])('%s', (_, val) => { | ||
expect(viaProxy(val, false)).toEqual(val); | ||
}); | ||
}); | ||
|
||
describe('Pass requests as-is if proxying is enabled, but requests go to local host', () => { | ||
vi.stubGlobal('window', windowMock); | ||
|
||
test.each([ | ||
['Local Address', 'https://mockhostname:1234/image'], | ||
['Local Address No Host', '/image'], | ||
])('%s', (_, val) => { | ||
expect(viaProxy(val, true)).toEqual(val); | ||
}); | ||
}); | ||
|
||
test('Transform request if proxying is enabled and request is remote', () => { | ||
vi.stubGlobal('window', windowMock); | ||
|
||
const address = 'https://example.com/some/image_resource.png?with_query'; | ||
expect(viaProxy(address, true)).toEqual( | ||
`/cors-proxy/${encodeURIComponent(address)}`, | ||
); | ||
}); | ||
|
||
test('Unusual formats like base64 are to be ignored', () => { | ||
vi.stubGlobal('window', windowMock); // | ||
|
||
const address = | ||
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAIAAAD2HxkiAAAKUElEQVR4nO3bfWxV9R3H8c/t7fMTT4KlgDyKQ2BzYQrKBFcMSCSwCXMJy0ZckDEW4/AfF7PMZYl/oJtkW2YC4kI0otscxgGVuWx1gLABYeNB7BQGIhQEBbRQKPRhf3DK9ba3vb3Hls9N7/v11+np79z025x378M5jWhFswD4ZLl/ACDTESFgRoSAGRECZkQImBEhYEaEgBkRAmZECJgRIWBGhIAZEQJmRAiYESFgRoSAGRECZkQImBEhYEaEgBkRAmZECJgRIWBGhIAZEQJmRAiYESFgRoSAGRECZkQImBEhYEaEgBkRAmZECJgRIWBGhIAZEQJmRAiYESFgRoSAGRECZkQImBEhYEaEgBkRAmZECJgRIWBGhIAZEQJmRAiYESFgRoSAGRECZkQImBEhYEaEgBkRAmZECJgRIWBGhIAZEQJmRAiYESFgRoSAGRECZkQImBEhYEaEgBkRAmZECJgRIWBGhIAZEQJmRAiYESFgRoSAGRECZkQImBEhYEaEgBkRAmZECJgRIWBGhIAZEQJmRAiYESFgRoSAGRECZkQImBEhYEaEgBkRAmZECJgRIWBGhIAZEQJmRAiYESFgRoSAGRECZkQImBEhYEaEgBkRAmZECJgRIWBGhIAZEQJmRAiYESFgRoSAGRECZkQImBEhYEaEgBkRAmZECJgRIWBGhIAZEQJmRAiYESFgRoSAGRECZkQImBEhYEaEgBkRAmZECJgRIWBGhIAZEQJmRAiYESFgRoSAGRECZkQImBEhYEaEgBkRAmZECJhlu3+AdNE/Xw+P1+yhGlmqxmZ9cE4bP9DKav33bLuH5GRp0RjNH6VxfZQb1bHzeuOofr1P1e0ckur6ayDVqUOMkIZTp5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yIEzIgQMCNCwIwIATMiBMyIEDAjQsCMCAEzIgTMiBAwI0LAjAgBMyIEzIgQMCNCwIwIATMiBMyIEDAjQsCMCAEzIgTMiBAwI0LAjAgBMyIEzIgQMCNCwIwIATMiBMyIEDAjQsCMCAEzIgTMiBAwI0LAjAgBMyIEzIgQMCNCwIwIATMiBMyIEDAjQsCMCAEzIgTMiBAwI0LAjAgBMyIEzIgQMCNCwIwIATMiBMyIEDAjQsCMCAEzIgTMiBAwI0LAjAgBMyIEzIgQMCNCwIwIATMiBMyIEDAjQsCMCAEzIgTMiBAwI0LAjAgBMyIEzIgQMCNCwIwIATMiBMyIEDAjQsCMCAEzIgTMiBAwI0LAjAgBMyIEzIgQMCNCwIwIATMiBMyIEDAjQsCMCAEzIgTMiBAwI0LAjAgBMyIEzIgQMCNCwIwIATMiBMyIEDAjQsCMCAEzIgTMiBAwI0LAjAgBMyIEzIgQMCNCwIwIATMiBMz+Dzt6qG+Jr4RtAAAAAElFTkSuQmCC'; | ||
|
||
expect(viaProxy(address, true)).toEqual(address); | ||
}); | ||
|
||
test('Calling viaProxy multiple times should not break the result', () => { | ||
vi.stubGlobal('window', windowMock); | ||
const address = 'https://example.com/some/image_resource.png?with_query'; | ||
|
||
expect(viaProxy(address, true)).toEqual( | ||
viaProxy(viaProxy(viaProxy(address, true), true), true), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Utility to redirect remote sources via Proxy | ||
* | ||
* This utility is used to rewrite a request to be routed through | ||
* the Proxy instead. | ||
*/ | ||
|
||
/** | ||
* Virtual Module provided by the Motion Canvas | ||
* Plugin. Returns `false` if proxy is set to `false`, else `true` | ||
*/ | ||
//@ts-ignore | ||
import {enabled} from 'virtual:proxysettings'; | ||
|
||
/** | ||
* Helper-Function to route remote requests through a local proxy. | ||
* | ||
* @example This rewrites a remote url like `https://via.placeholder.com/300.png/09f/fff` into a URI-Component-Encoded string like `/cors-proxy/https%3A%2F%2Fvia.placeholder.com%2F300.png%2F09f%2Ffff` | ||
*/ | ||
export function viaProxy(url: string, overrideProxySetting?: boolean) { | ||
const isEnabled = overrideProxySetting ?? enabled; | ||
if (!isEnabled) { | ||
// Proxy is disabled, so we just pass as-is. | ||
return url; | ||
} | ||
|
||
if (url.startsWith('/cors-proxy/')) { | ||
// Already proxied, return as-is | ||
return url; | ||
} | ||
|
||
// window.location.hostname is being passed here to ensure that | ||
// this does not throw an Error for same-origin requests | ||
// e.g. /some/image -> localhost:9000/some/image | ||
|
||
const selfUrl = new URL(window.location.toString()); | ||
// inside a try-catch in case the URL cannot be understood | ||
try { | ||
const expandedUrl = new URL(url, selfUrl); | ||
if (!expandedUrl.protocol.startsWith('http')) { | ||
// this is probably some embedded image (e.g. image/png;base64). | ||
// don't touch and pass as is | ||
return url; | ||
} | ||
if (selfUrl.host === expandedUrl.host) { | ||
// This is a request to a "local" resource. | ||
// No need to rewrite | ||
return url; | ||
} | ||
} catch (_) { | ||
// in case of error just silently pass as-is | ||
return url; | ||
} | ||
|
||
// Everything else is a "remote" resource and requires a rewrite. | ||
return `/cors-proxy/${encodeURIComponent(url)}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import {vi} from 'vitest'; | ||
|
||
vi.stubGlobal('AudioContext', class {}); | ||
vi.mock('virtual:proxysettings', () => ({enabled: true})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters