From d743b849feb476c79f5f53ce87db91b4e6842622 Mon Sep 17 00:00:00 2001 From: FoxxMD Date: Thu, 9 Apr 2026 13:04:47 +0000 Subject: [PATCH] refactor: replace awaitjs/express with express usage --- src/backend/server/api.ts | 42 +++++++++---------- src/backend/server/auth.ts | 12 +++--- src/backend/server/deezerRoutes.ts | 6 +-- src/backend/server/endpointLastfmRoutes.ts | 6 +-- .../server/endpointListenbrainzRoutes.ts | 10 ++--- src/backend/server/index.ts | 5 +-- src/backend/server/webscrobblerRoutes.ts | 6 +-- 7 files changed, 43 insertions(+), 44 deletions(-) diff --git a/src/backend/server/api.ts b/src/backend/server/api.ts index 5247295e..32ed9f06 100644 --- a/src/backend/server/api.ts +++ b/src/backend/server/api.ts @@ -1,5 +1,5 @@ -import { ExpressWithAsync } from "@awaitjs/express"; import { LogDataPretty, Logger, LogLevel } from "@foxxmd/logging"; +import { Express } from 'express'; import bsseDef from 'better-sse'; import bodyParser from "body-parser"; import { FixedSizeList } from 'fixed-size-list'; @@ -55,7 +55,7 @@ const getLogs = (minLevel: number, limit: number = maxBufferSize, sort: 'asc' | return allLogs.flat(1).sort((a, b) => a.time - b.time).slice(0, limit); } -export const setupApi = (app: ExpressWithAsync, logger: Logger, appLoggerStream: PassThrough, initialLogOutput: LogDataPretty[] = [], scrobbleSources: ScrobbleSources, scrobbleClients: ScrobbleClients) => { +export const setupApi = (app: Express, logger: Logger, appLoggerStream: PassThrough, initialLogOutput: LogDataPretty[] = [], scrobbleSources: ScrobbleSources, scrobbleClients: ScrobbleClients) => { for(const level of Object.keys(logger.levels.labels)) { output[level] = new FixedSizeList(maxBufferSize); } @@ -155,17 +155,17 @@ export const setupApi = (app: ExpressWithAsync, logger: Logger, appLoggerStream: setupLastfmEndpointRoutes(app, logger, scrobbleSources); setupAuthRoutes(app, logger, sourceRequiredMiddle, clientRequiredMiddle, scrobbleSources, scrobbleClients); - app.putAsync('/api/webscrobbler', bodyParser.json({type: ['text/*', 'application/json']}), async (req, res) => { + app.put('/api/webscrobbler', bodyParser.json({type: ['text/*', 'application/json']}), async (req, res) => { logger.info(req.body); res.sendStatus(200); }); - app.getAsync('/api/webscrobbler', bodyParser.json({type: ['text/*', 'application/json']}), async (req, res) => { + app.get('/api/webscrobbler', bodyParser.json({type: ['text/*', 'application/json']}), async (req, res) => { logger.info(req.body); res.sendStatus(200); }); - app.getAsync('/api/status', async (req, res, next) => { + app.get('/api/status', async (req, res, next) => { const sourceData = scrobbleSources.sources.map((x) => { const { @@ -257,7 +257,7 @@ export const setupApi = (app: ExpressWithAsync, logger: Logger, appLoggerStream: return res.json({sources: sourceData, clients: clientData}); }); - app.getAsync('/api/recent', sourceMiddleFunc(false), async (req, res, next) => { + app.get('/api/recent', sourceMiddleFunc(false), async (req, res, next) => { const { // @ts-expect-error TS(2339): Property 'scrobbleSource' does not exist on type '... Remove this comment to see the full error message scrobbleSource: source, @@ -285,7 +285,7 @@ export const setupApi = (app: ExpressWithAsync, logger: Logger, appLoggerStream: return res.json(result); }); - app.getAsync('/api/source/art', sourceMiddleFunc(false), async (req, res, next) => { + app.get('/api/source/art', sourceMiddleFunc(false), async (req, res, next) => { const { // @ts-expect-error TS(2339): Property 'scrobbleSource' does not exist on type '... Remove this comment to see the full error message scrobbleSource, @@ -313,7 +313,7 @@ export const setupApi = (app: ExpressWithAsync, logger: Logger, appLoggerStream: } }); - app.getAsync('/api/dead', clientMiddleFunc(true), async (req, res, next) => { + app.get('/api/dead', clientMiddleFunc(true), async (req, res, next) => { const { // @ts-expect-error TS(2339): Property 'scrobbleSource' does not exist on type '... Remove this comment to see the full error message scrobbleClient: client, @@ -324,7 +324,7 @@ export const setupApi = (app: ExpressWithAsync, logger: Logger, appLoggerStream: return res.json(result); }); - app.putAsync('/api/dead', clientMiddleFunc(true), async (req, res, next) => { + app.put('/api/dead', clientMiddleFunc(true), async (req, res, next) => { const { // @ts-expect-error TS(2339): Property 'scrobbleSource' does not exist on type '... Remove this comment to see the full error message scrobbleClient: client, @@ -337,7 +337,7 @@ export const setupApi = (app: ExpressWithAsync, logger: Logger, appLoggerStream: await ((client as AbstractScrobbleClient).processDeadLetterQueue(1000)); }); - app.putAsync('/api/dead/:id', clientMiddleFunc(true), async (req, res, next) => { + app.put('/api/dead/:id', clientMiddleFunc(true), async (req, res, next) => { const { // @ts-expect-error TS(2339): Property 'scrobbleSource' does not exist on type '... Remove this comment to see the full error message scrobbleClient: client, @@ -366,7 +366,7 @@ export const setupApi = (app: ExpressWithAsync, logger: Logger, appLoggerStream: return res.json(dead); }); - app.deleteAsync('/api/dead', clientMiddleFunc(true), async (req, res, next) => { + app.delete('/api/dead', clientMiddleFunc(true), async (req, res, next) => { const { // @ts-expect-error TS(2339): Property 'scrobbleSource' does not exist on type '... Remove this comment to see the full error message scrobbleClient: client, @@ -379,7 +379,7 @@ export const setupApi = (app: ExpressWithAsync, logger: Logger, appLoggerStream: return res.json([]); }); - app.deleteAsync('/api/dead/:id', clientMiddleFunc(true), async (req, res, next) => { + app.delete('/api/dead/:id', clientMiddleFunc(true), async (req, res, next) => { const { // @ts-expect-error TS(2339): Property 'scrobbleSource' does not exist on type '... Remove this comment to see the full error message scrobbleClient: client, @@ -403,7 +403,7 @@ export const setupApi = (app: ExpressWithAsync, logger: Logger, appLoggerStream: return res.status(200).send(); }); - app.getAsync('/api/scrobbled', clientMiddleFunc(false), async (req, res, next) => { + app.get('/api/scrobbled', clientMiddleFunc(false), async (req, res, next) => { const { // @ts-expect-error scrobbleClient not part of req scrobbleClient: client, @@ -418,7 +418,7 @@ export const setupApi = (app: ExpressWithAsync, logger: Logger, appLoggerStream: }); app.use('/api/source/init', sourceRequiredMiddle); - app.postAsync('/api/source/init', async (req, res) => { + app.post('/api/source/init', async (req, res) => { // @ts-expect-error TS(2339): Property 'scrobbleSource' does not exist on type '... Remove this comment to see the full error message const source = req.scrobbleSource as AbstractSource; @@ -446,7 +446,7 @@ export const setupApi = (app: ExpressWithAsync, logger: Logger, appLoggerStream: }); app.use('/api/source/listen', sourceRequiredMiddle); - app.postAsync('/api/source/listen', async (req, res) => { + app.post('/api/source/listen', async (req, res) => { // @ts-expect-error TS(2339): Property 'scrobbleSource' does not exist on type '... Remove this comment to see the full error message const source = req.scrobbleSource as AbstractSource; @@ -474,7 +474,7 @@ export const setupApi = (app: ExpressWithAsync, logger: Logger, appLoggerStream: }); app.use('/api/client/init', clientRequiredMiddle); - app.postAsync('/api/client/init', async (req, res) => { + app.post('/api/client/init', async (req, res) => { // @ts-expect-error TS(2339): Property 'scrobbleSource' does not exist on type '... Remove this comment to see the full error message const client = req.scrobbleClient as AbstractScrobbleClient; @@ -498,8 +498,8 @@ export const setupApi = (app: ExpressWithAsync, logger: Logger, appLoggerStream: res.status(200).send('OK'); }); - app.getAsync('/health', async (req, res) => res.redirect(307, `/api/${req.url.slice(1)}`)); - app.getAsync('/api/health', async (req, res) => { + app.get('/health', async (req, res) => res.redirect(307, `/api/${req.url.slice(1)}`)); + app.get('/api/health', async (req, res) => { const { type, name @@ -549,7 +549,7 @@ export const setupApi = (app: ExpressWithAsync, logger: Logger, appLoggerStream: prom.collectDefaultMetrics(); } - app.getAsync('/api/metrics', async (req, res) => { + app.get('/api/metrics', async (req, res) => { const metricsString = await prom.register.metrics(); return res @@ -559,11 +559,11 @@ export const setupApi = (app: ExpressWithAsync, logger: Logger, appLoggerStream: }); - app.getAsync('/api/version', async (req, res) => { + app.get('/api/version', async (req, res) => { return res.json({version: root.get('version')}); }); - app.useAsync('/api/*', async (req, res) => { + app.use('/api/*', async (req, res) => { const remote = req.connection.remoteAddress; const proxyRemote = req.headers["x-forwarded-for"]; const ua = req.headers["user-agent"]; diff --git a/src/backend/server/auth.ts b/src/backend/server/auth.ts index 659db8aa..0a8c80e8 100644 --- a/src/backend/server/auth.ts +++ b/src/backend/server/auth.ts @@ -1,4 +1,4 @@ -import { ExpressWithAsync } from "@awaitjs/express"; +import { Express } from 'express'; import { Logger } from "@foxxmd/logging"; import passport from "passport"; import { ExpressHandler } from "../common/infrastructure/Atomic.js"; @@ -18,9 +18,9 @@ import LibrefmSource from "../sources/LibrefmSource.js"; import e from "express"; import AbstractSource from "../sources/AbstractSource.js"; -export const setupAuthRoutes = (app: ExpressWithAsync, logger: Logger, sourceMiddle: ExpressHandler, clientMiddle: ExpressHandler, scrobbleSources: ScrobbleSources, scrobbleClients: ScrobbleClients) => { +export const setupAuthRoutes = (app: Express, logger: Logger, sourceMiddle: ExpressHandler, clientMiddle: ExpressHandler, scrobbleSources: ScrobbleSources, scrobbleClients: ScrobbleClients) => { app.use('/api/client/auth', clientMiddle); - app.getAsync('/api/client/auth', async (req, res) => { + app.get('/api/client/auth', async (req, res) => { const { scrobbleClient, } = req as any; @@ -40,7 +40,7 @@ export const setupAuthRoutes = (app: ExpressWithAsync, logger: Logger, sourceMid }); app.use('/api/source/auth', sourceMiddle); - app.getAsync('/api/source/auth', async (req, res, next) => { + app.get('/api/source/auth', async (req, res, next) => { const { // @ts-expect-error TS(2339): Property 'scrobbleSource' does not exist on type '... Remove this comment to see the full error message scrobbleSource: source, @@ -74,7 +74,7 @@ export const setupAuthRoutes = (app: ExpressWithAsync, logger: Logger, sourceMid } }); - app.getAsync(/.*callback$/, async (req, res, next) => { + app.get(/.*callback$/, async (req, res, next) => { if(req.url.indexOf('/api') !== 0) { return res.redirect(307, `/api${req.url}`); } @@ -155,7 +155,7 @@ export const setupAuthRoutes = (app: ExpressWithAsync, logger: Logger, sourceMid } }); - app.getAsync(/(\/api\/tealfm\/.*)/, async function (req, res) { + app.get(/(\/api\/tealfm\/.*)/, async function (req, res) { const clients = scrobbleClients.getByType('tealfm') as TealScrobbler[]; if (clients.length === 0) { diff --git a/src/backend/server/deezerRoutes.ts b/src/backend/server/deezerRoutes.ts index bbe3b633..a5c3d452 100644 --- a/src/backend/server/deezerRoutes.ts +++ b/src/backend/server/deezerRoutes.ts @@ -1,11 +1,11 @@ -import { ExpressWithAsync } from "@awaitjs/express"; +import { Express } from 'express'; import { Logger } from "@foxxmd/logging"; import passport from "passport"; import DeezerSource from "../sources/DeezerSource.js"; import ScrobbleSources from "../sources/ScrobbleSources.js"; import { sleep } from "../utils.js"; -export const setupDeezerRoutes = (app: ExpressWithAsync, logger: Logger, scrobbleSources: ScrobbleSources) => { +export const setupDeezerRoutes = (app: Express, logger: Logger, scrobbleSources: ScrobbleSources) => { // initialize deezer strategies // const deezerSources = scrobbleSources.getByType('deezer') as DeezerSource[]; @@ -15,7 +15,7 @@ export const setupDeezerRoutes = (app: ExpressWithAsync, logger: Logger, scrobbl // something about the deezer passport strategy makes express continue with the response even though it should wait for accesstoken callback and userprofile fetching // so to get around this add an additional middleware that loops/sleeps until we should have fetched everything ¯\_(ツ)_/¯ - app.getAsync(/.*deezer\/callback*$/, (req, res, next) => { + app.get(/.*deezer\/callback*$/, (req, res, next) => { if(req.url.indexOf('/api') !== 0) { return res.redirect(307, `/api${req.url}`); } diff --git a/src/backend/server/endpointLastfmRoutes.ts b/src/backend/server/endpointLastfmRoutes.ts index 34a72c84..a13b1bb5 100644 --- a/src/backend/server/endpointLastfmRoutes.ts +++ b/src/backend/server/endpointLastfmRoutes.ts @@ -1,5 +1,5 @@ /* eslint-disable prefer-arrow-functions/prefer-arrow-functions */ -import { ExpressWithAsync } from "@awaitjs/express"; +import { Express } from 'express'; import { childLogger, Logger } from "@foxxmd/logging"; import bodyParser from "body-parser"; import ScrobbleSources from "../sources/ScrobbleSources.js"; @@ -8,14 +8,14 @@ import { LFMEndpointNotifier } from "../sources/ingressNotifiers/LFMEndpointNoti import { EndpointLastfmSource, playStateFromRequest, parseDisplayIdentifiersFromRequest } from "../sources/EndpointLastfmSource.js"; import { LastFMScrobbleRequestPayload } from "../common/vendor/LastfmApiClient.js"; -export const setupLastfmEndpointRoutes = (app: ExpressWithAsync, parentLogger: Logger, scrobbleSources: ScrobbleSources) => { +export const setupLastfmEndpointRoutes = (app: Express, parentLogger: Logger, scrobbleSources: ScrobbleSources) => { const logger = childLogger(parentLogger, ['Ingress', 'LFM']); const nonEmptyCheck = nonEmptyBody(logger, 'LFM Endpoint'); const webhookIngress = new LFMEndpointNotifier(logger); - app.useAsync(/(\/api\/lastfm(?!\/callback))|(\/2.0\/?)$/, + app.use(/(\/api\/lastfm(?!\/callback))|(\/2.0\/?)$/, async function (req, res, next) { // track request before parsing body to ensure we at least log that something is happening // (in the event body parsing does not work or request is not POST/PATCH) diff --git a/src/backend/server/endpointListenbrainzRoutes.ts b/src/backend/server/endpointListenbrainzRoutes.ts index d6c97bfe..a9a6094b 100644 --- a/src/backend/server/endpointListenbrainzRoutes.ts +++ b/src/backend/server/endpointListenbrainzRoutes.ts @@ -1,5 +1,5 @@ /* eslint-disable prefer-arrow-functions/prefer-arrow-functions */ -import { ExpressWithAsync } from "@awaitjs/express"; +import { Express } from 'express'; import { childLogger, Logger } from "@foxxmd/logging"; import bodyParser from "body-parser"; import { EndpointListenbrainzSource, playStateFromRequest, parseDisplayIdentifiersFromRequest } from "../sources/EndpointListenbrainzSource.js"; @@ -10,7 +10,7 @@ import { isDebugMode } from "../utils.js"; const TEXT_WILDCARD_REGEX = new RegExp(/text\/.+/); -export const setupLZEndpointRoutes = (app: ExpressWithAsync, parentLogger: Logger, scrobbleSources: ScrobbleSources) => { +export const setupLZEndpointRoutes = (app: Express, parentLogger: Logger, scrobbleSources: ScrobbleSources) => { const logger = childLogger(parentLogger, ['Ingress', 'Listenbrainz']); @@ -30,7 +30,7 @@ export const setupLZEndpointRoutes = (app: ExpressWithAsync, parentLogger: Logge const nonEmptyCheck = nonEmptyBody(logger, 'LZ Endpoint'); const webhookIngress = new LZEndpointNotifier(logger); - app.useAsync(/(\/api\/listenbrainz.*)|(\/1\/submit-listens\/?$)/, + app.use(/(\/api\/listenbrainz.*)|(\/1\/submit-listens\/?$)/, async function (req, res, next) { // track request before parsing body to ensure we at least log that something is happening // (in the event body parsing does not work or request is not POST/PATCH) @@ -67,7 +67,7 @@ export const setupLZEndpointRoutes = (app: ExpressWithAsync, parentLogger: Logge await source.handle(playerState); } }); - app.getAsync('/1/validate-token', async function (req, res) { + app.get('/1/validate-token', async function (req, res) { //https://listenbrainz.readthedocs.io/en/latest/users/api/core.html#get--1-validate-token logger.info('Validated token'); return res.status(200).json({ @@ -77,7 +77,7 @@ export const setupLZEndpointRoutes = (app: ExpressWithAsync, parentLogger: Logge user_name: "Multi-Scrobbler" }) }); - app.useAsync(/\/1\/.*/, async function (req, res) { + app.use(/\/1\/.*/, async function (req, res) { logger.warn(`Received what looks like a Listenbrainz Endpoint request but it was to an invalid URL route: ${req.originalUrl}\nMake sure base URL path to MS endpoint is correct.`); res.status(404); }); diff --git a/src/backend/server/index.ts b/src/backend/server/index.ts index 0bf9c424..68c4372a 100644 --- a/src/backend/server/index.ts +++ b/src/backend/server/index.ts @@ -1,8 +1,7 @@ -import { addAsync, Router } from '@awaitjs/express'; +import express, { Router } from 'express'; import { childLogger, LogDataPretty, Logger } from "@foxxmd/logging"; import bodyParser from 'body-parser'; import { stripIndents } from "common-tags"; -import express from 'express'; import session from 'express-session'; import { PassThrough } from "node:stream"; import passport from 'passport'; @@ -16,7 +15,7 @@ import { setupApi } from "./api.js"; import ScrobbleSources from '../sources/ScrobbleSources.js'; import ScrobbleClients from '../scrobblers/ScrobbleClients.js'; -const app = addAsync(express()); +const app = express(); const router = Router(); export const initServer = async (parentLogger: Logger, appLoggerStream: PassThrough, initialOutput: LogDataPretty[] = [], sources: ScrobbleSources, clients: ScrobbleClients) => { diff --git a/src/backend/server/webscrobblerRoutes.ts b/src/backend/server/webscrobblerRoutes.ts index 7d143370..1377ba23 100644 --- a/src/backend/server/webscrobblerRoutes.ts +++ b/src/backend/server/webscrobblerRoutes.ts @@ -1,4 +1,4 @@ -import { ExpressWithAsync } from "@awaitjs/express"; +import { Express } from 'express'; import { childLogger, Logger } from "@foxxmd/logging"; import bodyParser from "body-parser"; import cors from 'cors'; @@ -12,7 +12,7 @@ const corsOpts: cors.CorsOptions = { methods: ['POST'] } -export const setupWebscrobblerRoutes = (app: ExpressWithAsync, parentLogger: Logger, scrobbleSources: ScrobbleSources) => { +export const setupWebscrobblerRoutes = (app: Express, parentLogger: Logger, scrobbleSources: ScrobbleSources) => { const logger = childLogger(parentLogger, ['Ingress', 'WebScrobbler']); @@ -31,7 +31,7 @@ export const setupWebscrobblerRoutes = (app: ExpressWithAsync, parentLogger: Log }, cors(corsOpts)); - app.postAsync('/api/webscrobbler*', + app.post('/api/webscrobbler*', async (req, res, next) => { webhookIngress.trackIngress(req, true); next(); -- 2.51.2