whitespace fix

This commit is contained in:
yawhn 2022-11-03 02:15:40 +02:00
parent dba4d2240e
commit 58eed45187

View file

@ -2,51 +2,51 @@
* Web Client Server * Web Client Server
*/ */
import { dirname } from 'node:path'; import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import { readFileSync } from 'node:fs'; import { readFileSync } from 'node:fs';
import Koa from 'koa'; import Koa from 'koa';
import Router from '@koa/router'; import Router from '@koa/router';
import send from 'koa-send'; import send from 'koa-send';
import favicon from 'koa-favicon'; import favicon from 'koa-favicon';
import views from 'koa-views'; import views from 'koa-views';
import sharp from 'sharp'; import sharp from 'sharp';
import { createBullBoard } from '@bull-board/api'; import { createBullBoard } from '@bull-board/api';
import { BullAdapter } from '@bull-board/api/bullAdapter.js'; import { BullAdapter } from '@bull-board/api/bullAdapter.js';
import { KoaAdapter } from '@bull-board/koa'; import { KoaAdapter } from '@bull-board/koa';
import { In, IsNull } from 'typeorm'; import { In, IsNull } from 'typeorm';
import { fetchMeta } from '@/misc/fetch-meta.js'; import { fetchMeta } from '@/misc/fetch-meta.js';
import config from '@/config/index.js'; import config from '@/config/index.js';
import { Users, Notes, UserProfiles, Pages, Channels, Clips, GalleryPosts } from '@/models/index.js'; import { Users, Notes, UserProfiles, Pages, Channels, Clips, GalleryPosts } from '@/models/index.js';
import * as Acct from '@/misc/acct.js'; import * as Acct from '@/misc/acct.js';
import { getNoteSummary } from '@/misc/get-note-summary.js'; import { getNoteSummary } from '@/misc/get-note-summary.js';
import { queues } from '@/queue/queues.js'; import { queues } from '@/queue/queues.js';
import { genOpenapiSpec } from '../api/openapi/gen-spec.js'; import { genOpenapiSpec } from '../api/openapi/gen-spec.js';
import { urlPreviewHandler } from './url-preview.js'; import { urlPreviewHandler } from './url-preview.js';
import { manifestHandler } from './manifest.js'; import { manifestHandler } from './manifest.js';
import packFeed from './feed.js'; import packFeed from './feed.js';
import { MINUTE, DAY } from '@/const.js'; import { MINUTE, DAY } from '@/const.js';
const _filename = fileURLToPath(import.meta.url); const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename); const _dirname = dirname(_filename);
const staticAssets = `${_dirname}/../../../assets/`; const staticAssets = `${_dirname}/../../../assets/`;
const clientAssets = `${_dirname}/../../../../client/assets/`; const clientAssets = `${_dirname}/../../../../client/assets/`;
const assets = `${_dirname}/../../../../../built/_client_dist_/`; const assets = `${_dirname}/../../../../../built/_client_dist_/`;
const swAssets = `${_dirname}/../../../../../built/_sw_dist_/`; const swAssets = `${_dirname}/../../../../../built/_sw_dist_/`;
// Init app // Init app
const app = new Koa(); const app = new Koa();
//#region Bull Dashboard //#region Bull Dashboard
const bullBoardPath = '/queue'; const bullBoardPath = '/queue';
// used as a url param to prevent caching css and images // used as a url param to prevent caching css and images
const nowDateMs = Date.now(); const nowDateMs = Date.now();
// Authenticate // Authenticate
app.use(async (ctx, next) => { app.use(async (ctx, next) => {
if (ctx.path === bullBoardPath || ctx.path.startsWith(bullBoardPath + '/')) { if (ctx.path === bullBoardPath || ctx.path.startsWith(bullBoardPath + '/')) {
const token = ctx.cookies.get('token'); const token = ctx.cookies.get('token');
if (token == null) { if (token == null) {
@ -60,21 +60,21 @@
} }
} }
await next(); await next();
}); });
const serverAdapter = new KoaAdapter(); const serverAdapter = new KoaAdapter();
createBullBoard({ createBullBoard({
queues: queues.map(q => new BullAdapter(q)), queues: queues.map(q => new BullAdapter(q)),
serverAdapter, serverAdapter,
}); });
serverAdapter.setBasePath(bullBoardPath); serverAdapter.setBasePath(bullBoardPath);
app.use(serverAdapter.registerPlugin()); app.use(serverAdapter.registerPlugin());
//#endregion //#endregion
// Init renderer // Init renderer
app.use(views(_dirname + '/views', { app.use(views(_dirname + '/views', {
extension: 'pug', extension: 'pug',
options: { options: {
version: config.version, version: config.version,
@ -83,52 +83,52 @@
JSON.parse(readFileSync(`${_dirname}/../../../../../built/_client_dist_/manifest.json`, 'utf-8'))['src/init.ts'], JSON.parse(readFileSync(`${_dirname}/../../../../../built/_client_dist_/manifest.json`, 'utf-8'))['src/init.ts'],
config, config,
}, },
})); }));
// Serve favicon // Serve favicon
app.use(favicon(`${_dirname}/../../../assets/favicon.ico`)); app.use(favicon(`${_dirname}/../../../assets/favicon.ico`));
// Common request handler // Common request handler
app.use(async (ctx, next) => { app.use(async (ctx, next) => {
// IFrameの中に入れられないようにする // IFrameの中に入れられないようにする
ctx.set('X-Frame-Options', 'DENY'); ctx.set('X-Frame-Options', 'DENY');
await next(); await next();
}); });
// Init router // Init router
const router = new Router(); const router = new Router();
//#region static assets //#region static assets
router.get('/static-assets/(.*)', async ctx => { router.get('/static-assets/(.*)', async ctx => {
await send(ctx as any, ctx.path.replace('/static-assets/', ''), { await send(ctx as any, ctx.path.replace('/static-assets/', ''), {
root: staticAssets, root: staticAssets,
maxage: 7 * DAY, maxage: 7 * DAY,
}); });
}); });
router.get('/client-assets/(.*)', async ctx => { router.get('/client-assets/(.*)', async ctx => {
await send(ctx as any, ctx.path.replace('/client-assets/', ''), { await send(ctx as any, ctx.path.replace('/client-assets/', ''), {
root: clientAssets, root: clientAssets,
maxage: 7 * DAY, maxage: 7 * DAY,
}); });
}); });
router.get('/assets/(.*)', async ctx => { router.get('/assets/(.*)', async ctx => {
await send(ctx as any, ctx.path.replace('/assets/', ''), { await send(ctx as any, ctx.path.replace('/assets/', ''), {
root: assets, root: assets,
maxage: 7 * DAY, maxage: 7 * DAY,
}); });
}); });
// Apple touch icon // Apple touch icon
router.get('/apple-touch-icon.png', async ctx => { router.get('/apple-touch-icon.png', async ctx => {
await send(ctx as any, '/apple-touch-icon.png', { await send(ctx as any, '/apple-touch-icon.png', {
root: staticAssets, root: staticAssets,
}); });
}); });
router.get('/twemoji/(.*)', async ctx => { router.get('/twemoji/(.*)', async ctx => {
const path = ctx.path.replace('/twemoji/', ''); const path = ctx.path.replace('/twemoji/', '');
if (!path.match(/^[0-9a-f-]+\.svg$/)) { if (!path.match(/^[0-9a-f-]+\.svg$/)) {
@ -142,9 +142,9 @@
root: `${_dirname}/../../../node_modules/@discordapp/twemoji/dist/svg/`, root: `${_dirname}/../../../node_modules/@discordapp/twemoji/dist/svg/`,
maxage: 30 * DAY, maxage: 30 * DAY,
}); });
}); });
router.get('/twemoji-badge/(.*)', async ctx => { router.get('/twemoji-badge/(.*)', async ctx => {
const path = ctx.path.replace('/twemoji-badge/', ''); const path = ctx.path.replace('/twemoji-badge/', '');
if (!path.match(/^[0-9a-f-]+\.png$/)) { if (!path.match(/^[0-9a-f-]+\.png$/)) {
@ -185,42 +185,42 @@
ctx.set('Cache-Control', 'max-age=2592000'); ctx.set('Cache-Control', 'max-age=2592000');
ctx.set('Content-Type', 'image/png'); ctx.set('Content-Type', 'image/png');
ctx.body = buffer; ctx.body = buffer;
}); });
// ServiceWorker // ServiceWorker
router.get(`/sw.js`, async ctx => { router.get(`/sw.js`, async ctx => {
await send(ctx as any, `/sw.js`, { await send(ctx as any, `/sw.js`, {
root: swAssets, root: swAssets,
maxage: 10 * MINUTE, maxage: 10 * MINUTE,
}); });
}); });
// Manifest // Manifest
router.get('/manifest.json', manifestHandler); router.get('/manifest.json', manifestHandler);
router.get('/robots.txt', async ctx => { router.get('/robots.txt', async ctx => {
await send(ctx as any, '/robots.txt', { await send(ctx as any, '/robots.txt', {
root: staticAssets, root: staticAssets,
}); });
}); });
//#endregion //#endregion
// Docs // Docs
router.get('/api-doc', async ctx => { router.get('/api-doc', async ctx => {
await send(ctx as any, '/redoc.html', { await send(ctx as any, '/redoc.html', {
root: staticAssets, root: staticAssets,
}); });
}); });
// URL preview endpoint // URL preview endpoint
router.get('/url', urlPreviewHandler); router.get('/url', urlPreviewHandler);
router.get('/api.json', async ctx => { router.get('/api.json', async ctx => {
ctx.body = genOpenapiSpec(); ctx.body = genOpenapiSpec();
}); });
const getFeed = async (acct: string) => { const getFeed = async (acct: string) => {
const meta = await fetchMeta(); const meta = await fetchMeta();
if (meta.privateMode) { if (meta.privateMode) {
return; return;
@ -233,10 +233,10 @@
}); });
return user && await packFeed(user); return user && await packFeed(user);
}; };
// Atom // Atom
router.get('/@:user.atom', async ctx => { router.get('/@:user.atom', async ctx => {
const feed = await getFeed(ctx.params.user); const feed = await getFeed(ctx.params.user);
if (feed) { if (feed) {
@ -245,10 +245,10 @@
} else { } else {
ctx.status = 404; ctx.status = 404;
} }
}); });
// RSS // RSS
router.get('/@:user.rss', async ctx => { router.get('/@:user.rss', async ctx => {
const feed = await getFeed(ctx.params.user); const feed = await getFeed(ctx.params.user);
if (feed) { if (feed) {
@ -257,10 +257,10 @@
} else { } else {
ctx.status = 404; ctx.status = 404;
} }
}); });
// JSON // JSON
router.get('/@:user.json', async ctx => { router.get('/@:user.json', async ctx => {
const feed = await getFeed(ctx.params.user); const feed = await getFeed(ctx.params.user);
if (feed) { if (feed) {
@ -269,11 +269,11 @@
} else { } else {
ctx.status = 404; ctx.status = 404;
} }
}); });
//#region SSR (for crawlers) //#region SSR (for crawlers)
// User // User
router.get(['/@:user', '/@:user/:sub'], async (ctx, next) => { router.get(['/@:user', '/@:user/:sub'], async (ctx, next) => {
const { username, host } = Acct.parse(ctx.params.user); const { username, host } = Acct.parse(ctx.params.user);
const user = await Users.findOneBy({ const user = await Users.findOneBy({
usernameLower: username.toLowerCase(), usernameLower: username.toLowerCase(),
@ -306,9 +306,9 @@
// モデレータがAPI経由で参照可能にするために404にはしない // モデレータがAPI経由で参照可能にするために404にはしない
await next(); await next();
} }
}); });
router.get('/users/:user', async ctx => { router.get('/users/:user', async ctx => {
const user = await Users.findOneBy({ const user = await Users.findOneBy({
id: ctx.params.user, id: ctx.params.user,
host: IsNull(), host: IsNull(),
@ -321,10 +321,10 @@
} }
ctx.redirect(`/@${user.username}${ user.host == null ? '' : '@' + user.host}`); ctx.redirect(`/@${user.username}${ user.host == null ? '' : '@' + user.host}`);
}); });
// Note // Note
router.get('/notes/:note', async (ctx, next) => { router.get('/notes/:note', async (ctx, next) => {
const note = await Notes.findOneBy({ const note = await Notes.findOneBy({
id: ctx.params.note, id: ctx.params.note,
visibility: In(['public', 'home']), visibility: In(['public', 'home']),
@ -353,10 +353,10 @@
} }
await next(); await next();
}); });
// Page // Page
router.get('/@:user/pages/:page', async (ctx, next) => { router.get('/@:user/pages/:page', async (ctx, next) => {
const { username, host } = Acct.parse(ctx.params.user); const { username, host } = Acct.parse(ctx.params.user);
const user = await Users.findOneBy({ const user = await Users.findOneBy({
usernameLower: username.toLowerCase(), usernameLower: username.toLowerCase(),
@ -395,11 +395,11 @@
} }
await next(); await next();
}); });
// Clip // Clip
// TODO: 非publicなclipのハンドリング // TODO: 非publicなclipのハンドリング
router.get('/clips/:clip', async (ctx, next) => { router.get('/clips/:clip', async (ctx, next) => {
const clip = await Clips.findOneBy({ const clip = await Clips.findOneBy({
id: ctx.params.clip, id: ctx.params.clip,
}); });
@ -425,10 +425,10 @@
} }
await next(); await next();
}); });
// Gallery post // Gallery post
router.get('/gallery/:post', async (ctx, next) => { router.get('/gallery/:post', async (ctx, next) => {
const post = await GalleryPosts.findOneBy({ id: ctx.params.post }); const post = await GalleryPosts.findOneBy({ id: ctx.params.post });
if (post) { if (post) {
@ -452,10 +452,10 @@
} }
await next(); await next();
}); });
// Channel // Channel
router.get('/channels/:channel', async (ctx, next) => { router.get('/channels/:channel', async (ctx, next) => {
const channel = await Channels.findOneBy({ const channel = await Channels.findOneBy({
id: ctx.params.channel, id: ctx.params.channel,
}); });
@ -478,10 +478,10 @@
} }
await next(); await next();
}); });
//#endregion //#endregion
router.get('/_info_card_', async ctx => { router.get('/_info_card_', async ctx => {
const meta = await fetchMeta(true); const meta = await fetchMeta(true);
if (meta.privateMode) { if (meta.privateMode) {
ctx.status = 403; ctx.status = 403;
@ -497,35 +497,35 @@
originalUsersCount: await Users.countBy({ host: IsNull() }), originalUsersCount: await Users.countBy({ host: IsNull() }),
originalNotesCount: await Notes.countBy({ userHost: IsNull() }), originalNotesCount: await Notes.countBy({ userHost: IsNull() }),
}); });
}); });
router.get('/bios', async ctx => { router.get('/bios', async ctx => {
await ctx.render('bios', { await ctx.render('bios', {
version: config.version, version: config.version,
}); });
}); });
router.get('/cli', async ctx => { router.get('/cli', async ctx => {
await ctx.render('cli', { await ctx.render('cli', {
version: config.version, version: config.version,
}); });
}); });
const override = (source: string, target: string, depth = 0) => const override = (source: string, target: string, depth = 0) =>
[, ...target.split('/').filter(x => x), ...source.split('/').filter(x => x).splice(depth)].join('/'); [, ...target.split('/').filter(x => x), ...source.split('/').filter(x => x).splice(depth)].join('/');
router.get('/flush', async ctx => { router.get('/flush', async ctx => {
await ctx.render('flush'); await ctx.render('flush');
}); });
// streamingに非WebSocketリクエストが来た場合にbase htmlをキャシュ付きで返すと、Proxy等でそのパスがキャッシュされておかしくなる // streamingに非WebSocketリクエストが来た場合にbase htmlをキャシュ付きで返すと、Proxy等でそのパスがキャッシュされておかしくなる
router.get('/streaming', async ctx => { router.get('/streaming', async ctx => {
ctx.status = 503; ctx.status = 503;
ctx.set('Cache-Control', 'private, max-age=0'); ctx.set('Cache-Control', 'private, max-age=0');
}); });
// Render base html for all requests // Render base html for all requests
router.get('(.*)', async ctx => { router.get('(.*)', async ctx => {
const meta = await fetchMeta(); const meta = await fetchMeta();
let motd = ['Loading...']; let motd = ['Loading...'];
if (meta.customMOTD.length > 0) { if (meta.customMOTD.length > 0) {
@ -548,10 +548,9 @@
nowDateMs: nowDateMs, nowDateMs: nowDateMs,
}); });
ctx.set('Cache-Control', 'public, max-age=3'); ctx.set('Cache-Control', 'public, max-age=3');
}); });
// Register router // Register router
app.use(router.routes()); app.use(router.routes());
export default app;
export default app;