Skip to content

Commit 13f8e51

Browse files
committed
fix: content-length mismatch on torrent add, support MAM_TOKEN_FILE
- Fix #8: Set explicit Content-Length header on Transmission RPC requests to prevent Node.js fetch RequestContentLengthMismatchError - Fix #1: Support MAM_TOKEN_FILE env var to read the MAM session cookie from a file instead of an env var (useful when tokens expire frequently) - Also removed 'any' type from makeTransmissionRequest payload param Closes #8 Closes #1
1 parent e86ad91 commit 13f8e51

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

src/lib/env.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { readFileSync } from "fs";
2+
13
export const getEnvVariable = (key: string): string => {
24
const value = process.env[key];
35
if (!value) {
@@ -10,9 +12,25 @@ export const getOptionalEnvVariable = (key: string): string | undefined => {
1012
return process.env[key];
1113
};
1214

15+
const getMamToken = (): string | undefined => {
16+
const token = getOptionalEnvVariable("MAM_TOKEN");
17+
if (token) return token;
18+
19+
const filePath = getOptionalEnvVariable("MAM_TOKEN_FILE");
20+
if (filePath) {
21+
try {
22+
return readFileSync(filePath, "utf-8").trim();
23+
} catch {
24+
console.error(`Failed to read MAM token from file: ${filePath}`);
25+
}
26+
}
27+
28+
return undefined;
29+
};
30+
1331
export const getServerEnvVariables = () => {
1432
return {
15-
MAM_TOKEN: getOptionalEnvVariable("MAM_TOKEN"), // MAM_TOKEN is now optional, can be provided via UI
33+
MAM_TOKEN: getMamToken(),
1634
TRANSMISSION_URL: getEnvVariable("TRANSMISSION_URL"),
1735
AUDIOBOOK_DESTINATION_PATH: getEnvVariable("AUDIOBOOK_DESTINATION_PATH"),
1836
EBOOK_DESTINATION_PATH: getEnvVariable("EBOOK_DESTINATION_PATH"),

src/lib/transmission-api.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,13 @@ export async function addTorrent(
7878

7979
async function makeTransmissionRequest(
8080
url: string,
81-
payload: any,
81+
payload: Record<string, unknown>,
8282
sessionId?: string,
8383
): Promise<Response> {
84+
const body = JSON.stringify(payload);
8485
const headers: HeadersInit = {
8586
"Content-Type": "application/json",
86-
// 'Authorization': `Basic ${apiKey}`
87+
"Content-Length": Buffer.byteLength(body).toString(),
8788
};
8889

8990
if (sessionId) {
@@ -93,6 +94,6 @@ async function makeTransmissionRequest(
9394
return fetch(url, {
9495
method: "POST",
9596
headers,
96-
body: JSON.stringify(payload),
97+
body,
9798
});
9899
}

0 commit comments

Comments
 (0)