From 18d3805b60bbea5f412c6d355a85f48e04107321 Mon Sep 17 00:00:00 2001 From: Aran Roig Date: Mon, 19 Oct 2026 19:23:58 +0200 Subject: [PATCH] kdjskdjsk --- dragonroll-foundry/README.md | 5 ++--- dragonroll-foundry/scripts/api.js | 24 +++++++++++++++++------- tests/foundry-pairing-status.test.js | 14 +++++++++++++- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/dragonroll-foundry/README.md b/dragonroll-foundry/README.md index 9feb9ea9..0c6d392b 100644 --- a/dragonroll-foundry/README.md +++ b/dragonroll-foundry/README.md @@ -43,9 +43,8 @@ settings screen re-displays it after a page reload, the window being closed, or example one pasted while Dragonroll was restarting — retrying it at world startup and once a minute until the world finishes linking. -For a local backend, use `http://localhost:5000`; the module omits `/api` and the backend accepts -the local pairing route at its root. Character synchronization works without the optional snapshot -bridge. Configure that bridge only if you also need the read-only world snapshot API described below. +For a local backend, use `http://localhost:5000`; the module adds `/api` like it does for any other +server. Character synchronization works without the optional snapshot bridge. Configure that bridge only if you also need the read-only world snapshot API described below. **Dragonroll Pairing Status** in the module settings always says whether this browser holds a pairing and, once Dragonroll has answered `GET /api/foundry/sync/status`, which campaign the world is diff --git a/dragonroll-foundry/scripts/api.js b/dragonroll-foundry/scripts/api.js index 14eed3fc..aec7e750 100644 --- a/dragonroll-foundry/scripts/api.js +++ b/dragonroll-foundry/scripts/api.js @@ -196,24 +196,33 @@ function dragonrollEndpoint(path) { const apiBase = new URL(endpoint); const basePath = apiBase.pathname.replace(/\/+$/, ""); - const isLocalBackend = ["localhost", "127.0.0.1", "[::1]"].includes(apiBase.hostname) - && apiBase.port === "5000"; const hasApiPath = basePath.endsWith("/api"); - apiBase.pathname = `${hasApiPath || isLocalBackend ? basePath : `${basePath}/api`}/`; + apiBase.pathname = `${hasApiPath ? basePath : `${basePath}/api`}/`; apiBase.search = ""; apiBase.hash = ""; return new URL(path.replace(/^\/+/, ""), apiBase); } +// A Dragonroll that is not there, or a URL that points at the web app instead of +// the API, answers with HTML. Say that plainly instead of a JSON parse error. +async function readDragonrollJson(response, url) { + try { + return await response.json(); + } catch { + throw new Error(`Dragonroll answered HTTP ${response.status} at ${url} but not with JSON. Check the Dragonroll Endpoint setting.`); + } +} + async function postDragonroll(path, body, token = "") { const headers = { "Content-Type": "application/json" }; if (token) headers.Authorization = `Bearer ${token}`; - const response = await fetch(dragonrollEndpoint(path), { + const url = dragonrollEndpoint(path); + const response = await fetch(url, { method: "POST", headers, body: JSON.stringify(body), }); - const result = await response.json(); + const result = await readDragonrollJson(response, url); if (!response.ok || result.status !== "ok") { throw new Error(result.msg || `Dragonroll returned HTTP ${response.status}.`); } @@ -221,10 +230,11 @@ async function postDragonroll(path, body, token = "") { } async function getDragonroll(path, token) { - const response = await fetch(dragonrollEndpoint(path), { + const url = dragonrollEndpoint(path); + const response = await fetch(url, { headers: { Authorization: `Bearer ${token}` }, }); - const result = await response.json(); + const result = await readDragonrollJson(response, url); if (!response.ok || result.status !== "ok") { throw new Error(result.msg || `Dragonroll returned HTTP ${response.status}.`); } diff --git a/tests/foundry-pairing-status.test.js b/tests/foundry-pairing-status.test.js index 5d7c0838..27eb2ce8 100644 --- a/tests/foundry-pairing-status.test.js +++ b/tests/foundry-pairing-status.test.js @@ -35,6 +35,7 @@ function makeStubWorld({ linkCode = "", linkUrl = "", answers = {}, + endpoint = "http://backend.test:5000", } = {}) { const state = { hooks: new Map(), @@ -48,7 +49,7 @@ function makeStubWorld({ syncToken, linkCode, linkUrl, - dragonrollApiUrl: "http://backend.test:5000", + dragonrollApiUrl: endpoint, }; const sandbox = { @@ -174,6 +175,17 @@ test("an unpaired world asks Dragonroll for a sign-up link and shows it", async assert.equal(world.setting("linkCode"), "link-code-1", "the code behind the link is kept to ask with"); }); +test("a local Dragonroll endpoint keeps the API path the backend mounts", async () => { + const world = await makeStubWorld({ + syncToken: "", + endpoint: "http://localhost:5000", + answers: { "link/start": { status: "ok", code: "link-code-1", linkUrl: SIGN_UP_LINK } }, + }).start(); + + assert.equal(world.linkStarts()[0].href, "http://localhost:5000/api/link/start", + "a local server is asked under /api like any other"); +}); + test("a world that holds a token neither asks for a link nor waits on one", async () => { const world = await makeStubWorld({ answers: { "foundry/sync/status": { status: "ok", campaign: LINKED_CAMPAIGN, worldId: "world-1" } },