Refactor WEBMENTION_TOKEN handling: remove file-based token retrieval and use global variable
All checks were successful
Deploy / deploy (push) Successful in 1m17s

This commit is contained in:
Adrian Altner 2026-04-22 03:07:20 +02:00
parent 85f58ae557
commit 7de0a815f4
3 changed files with 19 additions and 30 deletions

View file

@ -11,9 +11,6 @@ RUN npm ci
COPY . . COPY . .
RUN printf '%s' "${WEBMENTION_TOKEN}" > /app/.webmention-token
RUN echo "TOKEN FILE:" && ls -la /app/.webmention-token && wc -c /app/.webmention-token
RUN npm run build RUN npm run build

View file

@ -3,13 +3,25 @@
import mdx from '@astrojs/mdx'; import mdx from '@astrojs/mdx';
import sitemap from '@astrojs/sitemap'; import sitemap from '@astrojs/sitemap';
import { defineConfig, fontProviders } from 'astro/config'; import { defineConfig, fontProviders } from 'astro/config';
import { loadEnv } from 'vite';
import node from '@astrojs/node'; import node from '@astrojs/node';
const envMode = process.env.NODE_ENV === 'production' ? 'production' : 'development';
const envVars = loadEnv(envMode, process.cwd(), '');
const WEBMENTION_TOKEN = envVars.WEBMENTION_TOKEN || process.env.WEBMENTION_TOKEN || '';
console.log(`[astro.config] loadEnv mode=${envMode} token-len=${WEBMENTION_TOKEN.length}`);
// https://astro.build/config // https://astro.build/config
export default defineConfig({ export default defineConfig({
site: 'https://adrian-altner.de', site: 'https://adrian-altner.de',
vite: {
define: {
'globalThis.__WEBMENTION_TOKEN__': JSON.stringify(WEBMENTION_TOKEN),
},
},
devToolbar: { devToolbar: {
enabled: false, enabled: false,
}, },

View file

@ -1,32 +1,13 @@
--- ---
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 = ''; declare global {
function readTokenFromFile(): string | undefined { // eslint-disable-next-line no-var
const paths = ['/app/.webmention-token', '.webmention-token']; var __WEBMENTION_TOKEN__: string;
const logs: string[] = [];
logs.push(`cwd=${process.cwd()}`);
for (const p of paths) {
try {
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) { const tokenRaw = (globalThis as unknown as { __WEBMENTION_TOKEN__?: string }).__WEBMENTION_TOKEN__;
logs.push(`${p}:err(${(err as Error).code ?? 'unknown'})`); const WEBMENTION_TOKEN = typeof tokenRaw === 'string' ? tokenRaw : '';
}
}
fileDebug = logs.join(';');
return undefined;
}
const FILE_TOKEN = readTokenFromFile();
const WEBMENTION_TOKEN = FILE_TOKEN ?? (import.meta.env as Record<string, string | undefined>).WEBMENTION_TOKEN;
interface WMAuthor { interface WMAuthor {
name?: string; name?: string;
@ -58,8 +39,7 @@ interface FetchResult {
async function fetchMentions(target: string): Promise<FetchResult> { 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'; if (!token) return { mentions: [], debug: `no-token(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) => {
@ -83,7 +63,7 @@ async function fetchMentions(target: string): Promise<FetchResult> {
} }
return { return {
mentions: merged, mentions: merged,
debug: `ok(src=${source},len=${tokenLen},probe=${fileDebug}) slash=${a.status}:${a.entries.length} noslash=${b.status}:${b.entries.length}`, debug: `ok(len=${tokenLen}) slash=${a.status}:${a.entries.length} noslash=${b.status}:${b.entries.length}`,
}; };
} }