Refactor WEBMENTION_TOKEN handling: remove file-based token retrieval and use global variable
All checks were successful
Deploy / deploy (push) Successful in 1m17s
All checks were successful
Deploy / deploy (push) Successful in 1m17s
This commit is contained in:
parent
85f58ae557
commit
7de0a815f4
3 changed files with 19 additions and 30 deletions
|
|
@ -1,32 +1,13 @@
|
|||
---
|
||||
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');
|
||||
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;
|
||||
declare global {
|
||||
// eslint-disable-next-line no-var
|
||||
var __WEBMENTION_TOKEN__: string;
|
||||
}
|
||||
|
||||
const FILE_TOKEN = readTokenFromFile();
|
||||
const WEBMENTION_TOKEN = FILE_TOKEN ?? (import.meta.env as Record<string, string | undefined>).WEBMENTION_TOKEN;
|
||||
const tokenRaw = (globalThis as unknown as { __WEBMENTION_TOKEN__?: string }).__WEBMENTION_TOKEN__;
|
||||
const WEBMENTION_TOKEN = typeof tokenRaw === 'string' ? tokenRaw : '';
|
||||
|
||||
interface WMAuthor {
|
||||
name?: string;
|
||||
|
|
@ -58,8 +39,7 @@ interface FetchResult {
|
|||
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},probe=${fileDebug})` };
|
||||
if (!token) return { mentions: [], debug: `no-token(len=${tokenLen})` };
|
||||
const withSlash = target.endsWith('/') ? target : `${target}/`;
|
||||
const withoutSlash = target.replace(/\/+$/, '');
|
||||
const fetchOne = async (t: string) => {
|
||||
|
|
@ -83,7 +63,7 @@ async function fetchMentions(target: string): Promise<FetchResult> {
|
|||
}
|
||||
return {
|
||||
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}`,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue