Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add proxy for CosmosAsync calls #2846

Merged
merged 15 commits into from
Feb 20, 2024
Merged

feat: add proxy for CosmosAsync calls #2846

merged 15 commits into from
Feb 20, 2024

Conversation

rxri
Copy link
Member

@rxri rxri commented Feb 16, 2024

If it's spotify api - add necessary headers
It it's external uri - redirect the request via cors proxy

fixes #2844

rxri added 6 commits February 16, 2024 15:31
If it's spotify api - add necessary headers
It it's external uri - redirect the request via cors proxy
It doesn't work, but I don't want the proxy to be spammed with these
@Delusoire
Copy link
Contributor

Delusoire commented Feb 17, 2024

Would you consider something more like:

	const corsProxyURL = "https://cors-proxy.ririxi.workers.dev/";
	const allowedMethodsMap = {
		get: "get",
		post: "post",
		del: "delete",
		put: "put",
		patch: "patch"
	};
	const allowedMethodsSet = new Set(allowedMethodsMap.keys());
	const internalEndpoints = new Set(["sp", "wg"]);

	const handler = {
		// biome-ignore lint/complexity/useArrowFunction: <explanation>
		get: function (target, prop, receiver) {
			const internalFetch = Reflect.get(target, prop, receiver);

			if (typeof internalFetch !== "function" || !allowedMethodsSet.has(prop) || Spicetify.Platform.version >= "1.2.31") {
				return internalFetch;
			}

			return function (url, paramsOrBody) {
				const urlObj = new URL(url);

				const isWebAPI = urlObj.hostname === "api.spotify.com";
				const isSpClientAPI = urlObj.hostname.includes(".spotify.com") && urlObj.hostname.includes("spclient");
				const isInternalURL =  internalEndpoints.has(urlObj.protocol);
				const shouldUseCORSProxy = !isWebAPI && !isSpClientAPI && !isInternalURL;

				const method = allowedMethodsMap.get(prop.toLowerCase());
				const headers = {
					"Content-Type": "application/json"
				};

				const options = {
					method,
					headers,
					timeout: 1000 * 15
				};
				
				let finalURL = urlObj.toString();
				if (paramsOrBody) {
					if (method === "get") {
						const params = new URLSearchParams(paramsOrBody)
						finalURL += `?${params.toString()}`;
					} else {
						options.body = JSON.stringify(paramsOrBody);
					}
				}
				if (shouldUseCORSProxy) {
					finalURL = `${corsProxyURL}${finalURL}`;
				}

				const Authorization = `Bearer ${Spicetify.Platform.Session.accessToken}`;
				let injectedHeaders = {};
				if (isWebAPI) {
					injectedHeaders = { Authorization };
				};
				if (isSpClientAPI) {
					injectedHeaders = {
						Authorization,
						"Spotify-App-Version": Spicetify.Platform.version,
						"App-Platform": Spicetify.Platform.PlatformData.app_platform
					};
				}
				Object.assign(options.headers, injectedHeaders);

				try {
					if (isInternalURL) {
						return internalFetch.apply(this, args);
					}
					return fetch(finalURL, options).then(res => res.json());
				} catch (e) {
					console.error(e);
				}
			};
		}
	};

Which is pretty much the same with minor variable name & control flow changes.

jsHelper/spicetifyWrapper.js Outdated Show resolved Hide resolved
@rxri rxri marked this pull request as ready for review February 19, 2024 17:11
@rxri
Copy link
Member Author

rxri commented Feb 19, 2024

it's added automatically via biome, I cannot remove the <explanation> part because then it's not valid "ignore line"

@afonsojramos
Copy link
Member

afonsojramos commented Feb 19, 2024

Best of luck @rxri, let's hope this all goes well! 🙏

@rxri
Copy link
Member Author

rxri commented Feb 19, 2024

Best of luck @rxri, let's hope this all goes well! 🙏

5379945823893196604

@rxri rxri merged commit 1205ea4 into master Feb 20, 2024
9 checks passed
@rxri rxri deleted the feat/wrapper branch February 20, 2024 17:47
@kuba2k2
Copy link
Contributor

kuba2k2 commented Mar 25, 2024

What about localhost? It should never be proxied, but it is. The cors proxy obviously denies that request.

See kuba2k2/spicetify-beat-saber#14

@rxri
Copy link
Member Author

rxri commented Mar 25, 2024

What about localhost? It should never be proxied, but it is. The cors proxy obviously denies that request.

See kuba2k2/spicetify-beat-saber#14

@kuba2k2 please use fetch in this instance. CosmosAsync shouldn't be used unless you need to bypass cors or want to easily get data from spotify api without managing headers manually

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PSA: CosmosAsync - Resolver not found
6 participants