Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(standalone): give routes the relative request URL, as the server
Elian Doran8f67d85be18f
diff --git a/apps/standalone/src/lightweight/browser_routes.spec.ts b/apps/standalone/src/lightweight/browser_routes.spec.tsindex 0025eb482f..0e6cd36732 100644--- a/apps/standalone/src/lightweight/browser_routes.spec.ts+++ b/apps/standalone/src/lightweight/browser_routes.spec.ts@@ -209,8 +209,8 @@ describe("route wrapper branches (via controlled handlers)", () => {it("runs transactional and non-transactional api routes (and reads originalUrl)", async () => {const router = buildRouter();- const apiRes = await router.dispatch("GET", "http://localhost/t/api");- expect((parseJson(apiRes.body) as { url: string }).url).toBe("http://localhost/t/api");+ const apiRes = await router.dispatch("GET", "http://localhost/t/api?x=1");+ expect((parseJson(apiRes.body) as { url: string }).url).toBe("/t/api?x=1");expect(parseJson((await router.dispatch("GET", "http://localhost/t/asyncapi")).body)).toEqual({ ok: true });});diff --git a/apps/standalone/src/lightweight/browser_routes.ts b/apps/standalone/src/lightweight/browser_routes.tsindex 94a8909da9..93a4a9b6c3 100644--- a/apps/standalone/src/lightweight/browser_routes.ts+++ b/apps/standalone/src/lightweight/browser_routes.ts@@ -35,18 +35,20 @@ type HttpMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';function toCoreRequest(req: BrowserRequest): Request {/* v8 ignore next -- @preserve: BrowserRouter.dispatch always sets req.headers, so the ?? fallback is unreachable. */const headers = req.headers ?? {};+ // Express gives both URLs relative to the host, path and query string only.const { pathname, search } = new URL(req.url);+ const url = pathname + search;return {params: req.params,query: req.query,body: req.body,headers,method: req.method,- url: pathname + search,+ url,+ originalUrl: url,// A file posted for an import route has no `buffer` until an importer that needs one calls// `loadUploadedBytes()`, as on the server when multer leaves a zip on disk.file: req.file as Request["file"],- get originalUrl() { return req.url; },// `fetch` lower-cases the header names it sends, which is the form BrowserRouter stores them in.get: (name: string) => headers[name.toLowerCase()]};