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