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