Enhance debug output for token retrieval in readTokenFromFile function
All checks were successful
Deploy / deploy (push) Successful in 1m19s

This commit is contained in:
Adrian Altner 2026-04-22 03:00:05 +02:00
parent 8a8c160895
commit 85f58ae557

View file

@ -3,16 +3,25 @@ import { readFileSync } from 'node:fs';
import { DEFAULT_LOCALE, type Locale } from '~/consts';
import { getLocaleFromUrl, t } from '~/i18n/ui';
let fileDebug = '';
function readTokenFromFile(): string | undefined {
const paths = ['/app/.webmention-token', '.webmention-token'];
const logs: string[] = [];
logs.push(`cwd=${process.cwd()}`);
for (const p of paths) {
try {
const raw = readFileSync(p, 'utf-8').trim();
if (raw) return raw;
} catch {
// file not present, try next
const raw = readFileSync(p, 'utf-8');
logs.push(`${p}:ok(${raw.length})`);
const t = raw.trim();
if (t) {
fileDebug = logs.join(';');
return t;
}
} catch (err) {
logs.push(`${p}:err(${(err as Error).code ?? 'unknown'})`);
}
}
fileDebug = logs.join(';');
return undefined;
}
@ -50,7 +59,7 @@ async function fetchMentions(target: string): Promise<FetchResult> {
const token = WEBMENTION_TOKEN;
const tokenLen = typeof token === 'string' ? token.length : 0;
const source = FILE_TOKEN ? 'file' : 'env';
if (!token) return { mentions: [], debug: `no-token(src=${source},len=${tokenLen})` };
if (!token) return { mentions: [], debug: `no-token(src=${source},len=${tokenLen},probe=${fileDebug})` };
const withSlash = target.endsWith('/') ? target : `${target}/`;
const withoutSlash = target.replace(/\/+$/, '');
const fetchOne = async (t: string) => {
@ -74,7 +83,7 @@ async function fetchMentions(target: string): Promise<FetchResult> {
}
return {
mentions: merged,
debug: `ok(src=${source},len=${tokenLen}) slash=${a.status}:${a.entries.length} noslash=${b.status}:${b.entries.length}`,
debug: `ok(src=${source},len=${tokenLen},probe=${fileDebug}) slash=${a.status}:${a.entries.length} noslash=${b.status}:${b.entries.length}`,
};
}