mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-17 21:15:32 +09:00
build(api): bundle the api instead of transpiling at boot (#2227)
This commit is contained in:
1 parent
ba54b61dcf
commit
5f6f9428ac
11 files changed
+197
-30
No files matched your search
@@ -1,14 +1,13 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
import {fileURLToPath} from 'node:url';
|
||||
import type {Hono} from 'hono';
|
||||
import {RateLimitMiddleware} from '../middleware/RateLimitMiddleware';
|
||||
import {RateLimitConfigs} from '../RateLimitConfig';
|
||||
import type {HonoEnv} from '../types/HonoEnv';
|
||||
import {resolveAssetPath} from '../utils/AssetPaths';
|
||||
|
||||
const SPEC_PATH = path.join(path.dirname(fileURLToPath(import.meta.url)), 'openapi.json');
|
||||
const SPEC_PATH = resolveAssetPath('openapi', 'openapi.json');
|
||||
const SPEC_BODY = fs.readFileSync(SPEC_PATH, 'utf-8');
|
||||
|
||||
export function OpenAPIController(app: Hono<HonoEnv>): void {
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {existsSync} from 'node:fs';
|
||||
import {dirname, join} from 'node:path';
|
||||
import {fileURLToPath} from 'node:url';
|
||||
|
||||
function findPackageRoot(startDir: string): string {
|
||||
let current = startDir;
|
||||
while (!existsSync(join(current, 'package.json'))) {
|
||||
const parent = dirname(current);
|
||||
if (parent === current) {
|
||||
throw new Error(`Unable to locate the fluxer_api package root from ${startDir}`);
|
||||
}
|
||||
current = parent;
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
const PACKAGE_ROOT = findPackageRoot(dirname(fileURLToPath(import.meta.url)));
|
||||
|
||||
export function resolveAssetPath(...segments: Array<string>): string {
|
||||
return join(PACKAGE_ROOT, 'src', 'api', ...segments);
|
||||
}
|
||||
@@ -2,13 +2,11 @@
|
||||
|
||||
import {randomInt} from 'node:crypto';
|
||||
import {readFileSync} from 'node:fs';
|
||||
import {dirname, resolve} from 'node:path';
|
||||
import {fileURLToPath} from 'node:url';
|
||||
import {UsernameType} from '@fluxer/schema/src/primitives/UserValidators';
|
||||
import {resolveAssetPath} from './AssetPaths';
|
||||
|
||||
const WORDS_DIR = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'words');
|
||||
const scales = readFileSync(resolve(WORDS_DIR, 'scales.txt'), 'utf-8').trim().split('\n').filter(Boolean);
|
||||
const tails = readFileSync(resolve(WORDS_DIR, 'tails.txt'), 'utf-8').trim().split('\n').filter(Boolean);
|
||||
const scales = readFileSync(resolveAssetPath('words', 'scales.txt'), 'utf-8').trim().split('\n').filter(Boolean);
|
||||
const tails = readFileSync(resolveAssetPath('words', 'tails.txt'), 'utf-8').trim().split('\n').filter(Boolean);
|
||||
|
||||
function capitalize(word: string): string {
|
||||
return word.charAt(0).toUpperCase() + word.slice(1);
|
||||
|
||||
@@ -42,6 +42,7 @@ import type {RequestCache} from '../middleware/RequestCacheMiddleware';
|
||||
import type {Channel} from '../models/Channel';
|
||||
import type {Message} from '../models/Message';
|
||||
import type {Webhook} from '../models/Webhook';
|
||||
import {resolveAssetPath} from '../utils/AssetPaths';
|
||||
import * as RandomUtils from '../utils/RandomUtils';
|
||||
import type {IWebhookRepository} from './IWebhookRepository';
|
||||
import {transform as GitHubTransform} from './transformers/GitHubTransformer';
|
||||
@@ -620,7 +621,7 @@ export class WebhookService {
|
||||
const cacheKey = `webhook:${webhookId}:avatar:${provider}`;
|
||||
const avatarCache = await this.cacheService.get<string | null>(cacheKey);
|
||||
if (avatarCache) return avatarCache;
|
||||
const avatarFile = await fs.readFile(new URL(`../assets/${provider}.webp`, import.meta.url));
|
||||
const avatarFile = await fs.readFile(resolveAssetPath('assets', `${provider}.webp`));
|
||||
const avatar = await this.avatarService.uploadAvatar({
|
||||
prefix: 'avatars',
|
||||
entityId: webhookId,
|
||||
|
||||
@@ -2,11 +2,7 @@
|
||||
|
||||
import {randomInt} from 'node:crypto';
|
||||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
import {fileURLToPath} from 'node:url';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
import {resolveAssetPath} from '../utils/AssetPaths';
|
||||
|
||||
let tails: Array<string> | undefined;
|
||||
let scales: Array<string> | undefined;
|
||||
@@ -34,9 +30,8 @@ export function generateConnectionId(): string {
|
||||
}
|
||||
|
||||
function initWords(): void {
|
||||
const wordsDir = path.join(__dirname);
|
||||
tails = parseWordsFile(path.join(wordsDir, 'tails.txt'));
|
||||
scales = parseWordsFile(path.join(wordsDir, 'scales.txt'));
|
||||
tails = parseWordsFile(resolveAssetPath('words', 'tails.txt'));
|
||||
scales = parseWordsFile(resolveAssetPath('words', 'scales.txt'));
|
||||
}
|
||||
|
||||
function parseWordsFile(filePath: string): Array<string> {
|
||||
|
||||
Reference in new issue
Block a user