kdjskdjsk
Some checks failed
Test / test (push) Failing after 3s
Build and Deploy Nuxt / build (push) Failing after 48s

This commit is contained in:
2026-10-19 19:23:58 +02:00
parent 6854a650fb
commit 18d3805b60
3 changed files with 32 additions and 11 deletions

View File

@@ -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 example one pasted while Dragonroll was restarting — retrying it at world startup and once a minute
until the world finishes linking. until the world finishes linking.
For a local backend, use `http://localhost:5000`; the module omits `/api` and the backend accepts For a local backend, use `http://localhost:5000`; the module adds `/api` like it does for any other
the local pairing route at its root. Character synchronization works without the optional snapshot 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.
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 **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 and, once Dragonroll has answered `GET /api/foundry/sync/status`, which campaign the world is

View File

@@ -196,24 +196,33 @@ function dragonrollEndpoint(path) {
const apiBase = new URL(endpoint); const apiBase = new URL(endpoint);
const basePath = apiBase.pathname.replace(/\/+$/, ""); const basePath = apiBase.pathname.replace(/\/+$/, "");
const isLocalBackend = ["localhost", "127.0.0.1", "[::1]"].includes(apiBase.hostname)
&& apiBase.port === "5000";
const hasApiPath = basePath.endsWith("/api"); const hasApiPath = basePath.endsWith("/api");
apiBase.pathname = `${hasApiPath || isLocalBackend ? basePath : `${basePath}/api`}/`; apiBase.pathname = `${hasApiPath ? basePath : `${basePath}/api`}/`;
apiBase.search = ""; apiBase.search = "";
apiBase.hash = ""; apiBase.hash = "";
return new URL(path.replace(/^\/+/, ""), apiBase); 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 = "") { async function postDragonroll(path, body, token = "") {
const headers = { "Content-Type": "application/json" }; const headers = { "Content-Type": "application/json" };
if (token) headers.Authorization = `Bearer ${token}`; if (token) headers.Authorization = `Bearer ${token}`;
const response = await fetch(dragonrollEndpoint(path), { const url = dragonrollEndpoint(path);
const response = await fetch(url, {
method: "POST", method: "POST",
headers, headers,
body: JSON.stringify(body), body: JSON.stringify(body),
}); });
const result = await response.json(); const result = await readDragonrollJson(response, url);
if (!response.ok || result.status !== "ok") { if (!response.ok || result.status !== "ok") {
throw new Error(result.msg || `Dragonroll returned HTTP ${response.status}.`); 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) { async function getDragonroll(path, token) {
const response = await fetch(dragonrollEndpoint(path), { const url = dragonrollEndpoint(path);
const response = await fetch(url, {
headers: { Authorization: `Bearer ${token}` }, headers: { Authorization: `Bearer ${token}` },
}); });
const result = await response.json(); const result = await readDragonrollJson(response, url);
if (!response.ok || result.status !== "ok") { if (!response.ok || result.status !== "ok") {
throw new Error(result.msg || `Dragonroll returned HTTP ${response.status}.`); throw new Error(result.msg || `Dragonroll returned HTTP ${response.status}.`);
} }

View File

@@ -35,6 +35,7 @@ function makeStubWorld({
linkCode = "", linkCode = "",
linkUrl = "", linkUrl = "",
answers = {}, answers = {},
endpoint = "http://backend.test:5000",
} = {}) { } = {}) {
const state = { const state = {
hooks: new Map(), hooks: new Map(),
@@ -48,7 +49,7 @@ function makeStubWorld({
syncToken, syncToken,
linkCode, linkCode,
linkUrl, linkUrl,
dragonrollApiUrl: "http://backend.test:5000", dragonrollApiUrl: endpoint,
}; };
const sandbox = { 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"); 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 () => { test("a world that holds a token neither asks for a link nor waits on one", async () => {
const world = await makeStubWorld({ const world = await makeStubWorld({
answers: { "foundry/sync/status": { status: "ok", campaign: LINKED_CAMPAIGN, worldId: "world-1" } }, answers: { "foundry/sync/status": { status: "ok", campaign: LINKED_CAMPAIGN, worldId: "world-1" } },