Skip to content

Commit

Permalink
fix(webscrobbler): Add preflight route #157
Browse files Browse the repository at this point in the history
  • Loading branch information
FoxxMD committed Jun 13, 2024
1 parent 9842ddc commit bed8314
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"common-tags": "^1.8.2",
"compare-versions": "^4.1.2",
"concat-stream": "^2.0.0",
"cors": "^2.8.5",
"dayjs": "^1.10.4",
"dbus-ts": "^0.0.7",
"dotenv": "^10.0.0",
Expand Down Expand Up @@ -119,6 +120,7 @@
"@types/clone": "^2.1.2",
"@types/common-tags": "^1.8.1",
"@types/concat-stream": "^2.0.0",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.13",
"@types/express-session": "^1.17.4",
"@types/formidable": "^2.0.5",
Expand Down
14 changes: 12 additions & 2 deletions src/backend/server/webscrobblerRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { ExpressWithAsync } from "@awaitjs/express";
import { childLogger, Logger } from "@foxxmd/logging";
import bodyParser from "body-parser";
import cors from 'cors';
import path from "path";
import { WebhookNotifier } from "../sources/ingressNotifiers/WebhookNotifier.js";
import ScrobbleSources from "../sources/ScrobbleSources.js";
import { WebScrobblerSource } from "../sources/WebScrobblerSource.js";
import { nonEmptyBody } from "./middleware.js";

const corsOpts: cors.CorsOptions = {
methods: ['POST']
}

export const setupWebscrobblerRoutes = (app: ExpressWithAsync, parentLogger: Logger, scrobbleSources: ScrobbleSources) => {

const logger = childLogger(parentLogger, ['Ingress', 'WebScrobbler']);
Expand All @@ -20,13 +25,18 @@ export const setupWebscrobblerRoutes = (app: ExpressWithAsync, parentLogger: Log
// }
});
const webhookIngress = new WebhookNotifier(logger);
app.options('/api/webscrobbler*', async (req, res, next) => {
webhookIngress.trackIngress(req, true);
next();
},
cors(corsOpts));

app.postAsync('/api/webscrobbler*',
async (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)
webhookIngress.trackIngress(req, true);
next();
},
cors(corsOpts),
webScrobblerJsonParser, nonEmptyBody(logger, 'WebScrobbler Extension'), async (req, res) => {
webhookIngress.trackIngress(req, false);

Expand Down
12 changes: 3 additions & 9 deletions src/backend/sources/ingressNotifiers/WebhookNotifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,21 @@ export class WebhookNotifier extends IngressNotifier {
notifyBySource(req: Request, isRaw: boolean): [boolean, (string | undefined)] {

if(!isRaw) {
// let cleanPath = req.path;
// if(cleanPath.charAt(cleanPath.length - 1) === '/') {
// cleanPath = cleanPath.slice(0, -1);
// }
// const splitPath = cleanPath.split('/');
// const slug = splitPath[cleanPath.length - 1];
const parts = path.parse(req.path);
const slug = parts.name;

if(this.seenSlugs[slug] === undefined) {
this.seenSlugs[slug] = true;
return [true, `Received valid data for API URL slug '${slug === 'webscrobbler' ? '(none)' : slug}' for the first time.`];
return [true, `Received data for API URL slug '${slug === 'webscrobbler' ? '(none)' : slug}' for the first time.`];
}
}

return [true, undefined];
}

notifyByRequest(req: Request, isRaw: boolean): string | undefined {
if(req.method !== 'POST') {
return `Expected POST request (webhook payload) but received ${req.method}`;
if(req.method !== 'POST' && req.method !== 'OPTIONS') {
return `Expected POST or OPTIONS request (webhook payload) but received ${req.method}`;
}
return;
}
Expand Down

0 comments on commit bed8314

Please sign in to comment.