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
desktop client: use Content-Length header for the trilium-app:// protocol which fixes media playback on the desktop client
Adorian Dorana505f7354c66
diff --git a/apps/desktop/src/protocol.spec.ts b/apps/desktop/src/protocol.spec.tsindex 23a1772acf..f8b4589849 100644--- a/apps/desktop/src/protocol.spec.ts+++ b/apps/desktop/src/protocol.spec.ts@@ -405,6 +405,31 @@ describe("trilium-app protocol dispatcher", () => {expect(new Uint8Array(await response.arrayBuffer())).toEqual(new Uint8Array([1, 2, 3, 4]));});+ // The <audio>/<video> media data source refuses a response with no length, unlike+ // fetch() which reads to EOF. Express's own Content-Length is stripped as a transport+ // header (it can mismatch the buffered body), so the buffered path must restore one+ // matching the exact bytes it emits — otherwise media playback fails over trilium-app://+ // with MEDIA_ERR_SRC_NOT_SUPPORTED even though the same note plays over HTTP.+ it("sets an accurate Content-Length on buffered responses (media playback)", async () => {+ const app = express();+ app.get("/media", (_req, res) => {+ res.setHeader("Content-Type", "audio/mpeg");+ res.send(Buffer.from([0, 1, 2, 3, 4]));+ });++ const response = await dispatch(app, new Request("trilium-app://app/media"));+ expect(response.headers.get("content-length")).toBe("5");+ expect(new Uint8Array(await response.arrayBuffer())).toEqual(new Uint8Array([0, 1, 2, 3, 4]));+ });++ it("does not set Content-Length on a null-body status response", async () => {+ const app = express();+ app.get("/nc", (_req, res) => res.status(204).send());++ const response = await dispatch(app, new Request("trilium-app://app/nc"));+ expect(response.headers.has("content-length")).toBe(false);+ });+it("ignores a fetch abort that fires after a buffered response already completed", async () => {const app = express();app.get("/done", (_req, res) => res.send("done"));diff --git a/apps/desktop/src/protocol.ts b/apps/desktop/src/protocol.tsindex 0e0f02c7b4..e0aeddbeab 100644--- a/apps/desktop/src/protocol.ts+++ b/apps/desktop/src/protocol.ts@@ -218,10 +218,17 @@ export async function dispatch(app: Application, request: Request): Promise<Resp// null-body status codes (101 / 204 / 205 / 304). Express is// happy to call `res.status(204).send()` so we must filter here.const body = NULL_BODY_STATUSES.has(res.statusCode) ? null : toUint8Array(rawPayload);+ // Express's own Content-Length is stripped (it can mismatch the buffered body), but the+ // media data source (<audio>/<video>) refuses a response with no length — unlike fetch(),+ // which reads to EOF. We own the exact bytes here, so restore an accurate Content-Length.+ const headers = normalizeResponseHeaders(res.getHeaders());+ if (body) {+ headers.push(["content-length", String(body.byteLength)]);+ }try {resolve(new Response(body as BodyInit | null, {status: res.statusCode,- headers: normalizeResponseHeaders(res.getHeaders())+ headers}));} catch (err) {reject(err);