From 98d3324db3b1c345fc30e5dbcef3b1e11169867e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 24 Jul 2018 14:35:11 +0200 Subject: Fix nodeinfo endpoint --- server/controllers/activitypub/client.ts | 4 +- server/controllers/feeds.ts | 6 +-- server/controllers/static.ts | 12 +++--- server/helpers/utils.ts | 5 +-- server/middlewares/cache.ts | 73 ++++++++++++++++---------------- 5 files changed, 50 insertions(+), 50 deletions(-) diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts index ebb2c06a2..c90c3f931 100644 --- a/server/controllers/activitypub/client.ts +++ b/server/controllers/activitypub/client.ts @@ -16,7 +16,7 @@ import { VideoModel } from '../../models/video/video' import { VideoChannelModel } from '../../models/video/video-channel' import { VideoCommentModel } from '../../models/video/video-comment' import { VideoShareModel } from '../../models/video/video-share' -import { cache } from '../../middlewares/cache' +import { cacheRoute } from '../../middlewares/cache' import { activityPubResponse } from './utils' import { AccountVideoRateModel } from '../../models/account/account-video-rate' import { @@ -43,7 +43,7 @@ activityPubClientRouter.get('/accounts?/:name/following', ) activityPubClientRouter.get('/videos/watch/:id', - executeIfActivityPub(asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))), + executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))), executeIfActivityPub(asyncMiddleware(videosGetValidator)), executeIfActivityPub(asyncMiddleware(videoController)) ) diff --git a/server/controllers/feeds.ts b/server/controllers/feeds.ts index 6cbe42a2a..682f4abda 100644 --- a/server/controllers/feeds.ts +++ b/server/controllers/feeds.ts @@ -5,7 +5,7 @@ import { asyncMiddleware, setDefaultSort, videoCommentsFeedsValidator, videoFeed import { VideoModel } from '../models/video/video' import * as Feed from 'pfeed' import { AccountModel } from '../models/account/account' -import { cache } from '../middlewares/cache' +import { cacheRoute } from '../middlewares/cache' import { VideoChannelModel } from '../models/video/video-channel' import { VideoCommentModel } from '../models/video/video-comment' import { buildNSFWFilter } from '../helpers/express-utils' @@ -13,7 +13,7 @@ import { buildNSFWFilter } from '../helpers/express-utils' const feedsRouter = express.Router() feedsRouter.get('/feeds/video-comments.:format', - asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.FEEDS)), + asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)), asyncMiddleware(videoCommentsFeedsValidator), asyncMiddleware(generateVideoCommentsFeed) ) @@ -21,7 +21,7 @@ feedsRouter.get('/feeds/video-comments.:format', feedsRouter.get('/feeds/videos.:format', videosSortValidator, setDefaultSort, - asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.FEEDS)), + asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)), asyncMiddleware(videoFeedsValidator), asyncMiddleware(generateVideoFeed) ) diff --git a/server/controllers/static.ts b/server/controllers/static.ts index ce5d0c5fa..f10427f3e 100644 --- a/server/controllers/static.ts +++ b/server/controllers/static.ts @@ -2,7 +2,7 @@ import * as cors from 'cors' import * as express from 'express' import { CONFIG, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS, ROUTE_CACHE_LIFETIME } from '../initializers' import { VideosPreviewCache } from '../lib/cache' -import { cache } from '../middlewares/cache' +import { cacheRoute } from '../middlewares/cache' import { asyncMiddleware, videosGetValidator } from '../middlewares' import { VideoModel } from '../models/video/video' import { VideosCaptionCache } from '../lib/cache/videos-caption-cache' @@ -71,7 +71,7 @@ staticRouter.use( // robots.txt service staticRouter.get('/robots.txt', - asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.ROBOTS)), + asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS)), (_, res: express.Response) => { res.type('text/plain') return res.send(CONFIG.INSTANCE.ROBOTS) @@ -80,7 +80,7 @@ staticRouter.get('/robots.txt', // nodeinfo service staticRouter.use('/.well-known/nodeinfo', - asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.NODEINFO)), + asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)), (_, res: express.Response) => { return res.json({ links: [ @@ -93,7 +93,7 @@ staticRouter.use('/.well-known/nodeinfo', } ) staticRouter.use('/nodeinfo/:version.json', - asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.NODEINFO)), + // asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)), asyncMiddleware(generateNodeinfo) ) @@ -161,13 +161,13 @@ async function generateNodeinfo (req: express.Request, res: express.Response, ne nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION } } as HttpNodeinfoDiasporaSoftwareNsSchema20 - res.set('Content-Type', 'application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8') + res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"') } else { json = { error: 'Nodeinfo schema version not handled' } res.status(404) } - return res.end(JSON.stringify(json)) + return res.send(json).end() } async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) { diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts index 834d788c8..9efc89d92 100644 --- a/server/helpers/utils.ts +++ b/server/helpers/utils.ts @@ -113,7 +113,7 @@ const timeTable = { week: 3600000 * 24 * 7, month: 3600000 * 24 * 30 } -export function parseDuration (duration: number | string, defaultDuration: number): number { +export function parseDuration (duration: number | string): number { if (typeof duration === 'number') return duration if (typeof duration === 'string') { @@ -130,8 +130,7 @@ export function parseDuration (duration: number | string, defaultDuration: numbe } } - logger.error('Duration could not be properly parsed, defaulting to ' + defaultDuration) - return defaultDuration + throw new Error('Duration could not be properly parsed') } function resetSequelizeInstance (instance: Model, savedFields: object) { diff --git a/server/middlewares/cache.ts b/server/middlewares/cache.ts index 1e5a13b2e..c671b88c9 100644 --- a/server/middlewares/cache.ts +++ b/server/middlewares/cache.ts @@ -6,59 +6,60 @@ import { logger } from '../helpers/logger' const lock = new AsyncLock({ timeout: 5000 }) -function cacheRoute (lifetime: number) { +function cacheRoute (lifetimeArg: string | number) { return async function (req: express.Request, res: express.Response, next: express.NextFunction) { const redisKey = Redis.Instance.buildCachedRouteKey(req) - await lock.acquire(redisKey, async (done) => { - const cached = await Redis.Instance.getCachedRoute(req) + try { + await lock.acquire(redisKey, async (done) => { + const cached = await Redis.Instance.getCachedRoute(req) - // Not cached - if (!cached) { - logger.debug('No cached results for route %s.', req.originalUrl) + // Not cached + if (!cached) { + logger.debug('No cached results for route %s.', req.originalUrl) - const sendSave = res.send.bind(res) + const sendSave = res.send.bind(res) - res.send = (body) => { - if (res.statusCode >= 200 && res.statusCode < 400) { - const contentType = res.get('content-type') - Redis.Instance.setCachedRoute(req, body, lifetime, contentType, res.statusCode) - .then(() => done()) - .catch(err => { - logger.error('Cannot cache route.', { err }) - return done(err) - }) + res.send = (body) => { + if (res.statusCode >= 200 && res.statusCode < 400) { + const contentType = res.get('content-type') + const lifetime = parseDuration(lifetimeArg) + + Redis.Instance.setCachedRoute(req, body, lifetime, contentType, res.statusCode) + .then(() => done()) + .catch(err => { + logger.error('Cannot cache route.', { err }) + return done(err) + }) + } + + return sendSave(body) } - return sendSave(body) + return next() } - return next() - } - - if (cached.contentType) res.set('content-type', cached.contentType) + if (cached.contentType) res.set('content-type', cached.contentType) - if (cached.statusCode) { - const statusCode = parseInt(cached.statusCode, 10) - if (!isNaN(statusCode)) res.status(statusCode) - } + if (cached.statusCode) { + const statusCode = parseInt(cached.statusCode, 10) + if (!isNaN(statusCode)) res.status(statusCode) + } - logger.debug('Use cached result for %s.', req.originalUrl) - res.send(cached.body).end() + logger.debug('Use cached result for %s.', req.originalUrl) + res.send(cached.body).end() - return done() - }) + return done() + }) + } catch (err) { + logger.error('Cannot serve cached route.', err) + return next() + } } } -const cache = (duration: number | string) => { - const _lifetime = parseDuration(duration, 3600000) - return cacheRoute(_lifetime) -} - // --------------------------------------------------------------------------- export { - cacheRoute, - cache + cacheRoute } -- cgit v1.2.3