From 43d8a977fe3cac3a8d05dcedc16be8b6a497f9f4 Mon Sep 17 00:00:00 2001 From: Endel Dreyer Date: Thu, 29 Aug 2024 14:03:39 -0300 Subject: [PATCH] improve url builder for Discord Activities --- package.json | 2 +- src/3rd_party/discord.ts | 42 +++++++++++++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 4b6283b..a070f8e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "colyseus.js", - "version": "0.15.25", + "version": "0.15.26", "description": "Colyseus Multiplayer SDK for JavaScript/TypeScript", "author": "Endel Dreyer", "license": "MIT", diff --git a/src/3rd_party/discord.ts b/src/3rd_party/discord.ts index 320e99b..707f331 100644 --- a/src/3rd_party/discord.ts +++ b/src/3rd_party/discord.ts @@ -3,22 +3,46 @@ * https://github.com/colyseus/colyseus/issues/707 * * All URLs must go through the local proxy from - * https://.discordsays.com//... + * https://.discordsays.com/.proxy//... * - * You must configure your URL Mappings with: - * - /colyseus/{subdomain} -> {subdomain}.colyseus.cloud + * URL Mapping Examples: * - * Example: - * const client = new Client("https://xxxx.colyseus.cloud"); + * 1. Using Colyseus Cloud: + * - /colyseus/{subdomain} -> {subdomain}.colyseus.cloud + * + * Example: + * const client = new Client("https://xxxx.colyseus.cloud"); + * + * ------------------------------------------------------------- + * + * 2. Using `cloudflared` tunnel: + * - /colyseus/ -> .trycloudflare.com + * + * Example: + * const client = new Client("https://.trycloudflare.com"); + * + * ------------------------------------------------------------- + * + * 3. Providing a manual /.proxy/your-mapping: + * - /your-mapping/ -> your-endpoint.com + * + * Example: + * const client = new Client("/.proxy/your-mapping"); * */ - export function discordURLBuilder (url: URL): string { +export function discordURLBuilder (url: URL): string { const localHostname = window?.location?.hostname || "localhost"; const remoteHostnameSplitted = url.hostname.split('.'); - const subdomain = (remoteHostnameSplitted.length > 2) + const subdomain = ( + !url.hostname.includes("trycloudflare.com") && // ignore cloudflared subdomains + !url.hostname.includes("discordsays.com") && // ignore discordsays.com subdomains + remoteHostnameSplitted.length > 2 + ) ? `/${remoteHostnameSplitted[0]}` : ''; - return `${url.protocol}//${localHostname}/colyseus${subdomain}${url.pathname}${url.search}`; - } \ No newline at end of file + return (url.pathname.startsWith("/.proxy")) + ? `${url.protocol}//${localHostname}${subdomain}${url.pathname}${url.search}` + : `${url.protocol}//${localHostname}/.proxy/colyseus${subdomain}${url.pathname}${url.search}`; +}