From 34208b11444a3f8763c019b5539b816c7ebdd5c6 Mon Sep 17 00:00:00 2001 From: FineFindus Date: Wed, 23 Apr 2025 16:39:13 +0200 Subject: [PATCH] feat(routes/videoLabel): add ETag to responses Adds the `ETag` header to the `/getVideoLabels` and the `/getVideoLabels:hash` routes. This allows clients to cache the responses reducing the amount of network traffic. Ref: https://github.com/ajayyy/SponsorBlockServer/issues/576 --- src/routes/getVideoLabel.ts | 5 +++++ src/routes/getVideoLabelByHash.ts | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/src/routes/getVideoLabel.ts b/src/routes/getVideoLabel.ts index a690ac28..4fc92589 100644 --- a/src/routes/getVideoLabel.ts +++ b/src/routes/getVideoLabel.ts @@ -5,6 +5,7 @@ import { SBRecord } from "../types/lib.model"; import { ActionType, Category, DBSegment, Service, VideoID, VideoIDHash } from "../types/segments.model"; import { Logger } from "../utils/logger"; import { QueryCacher } from "../utils/queryCacher"; +import { getEtag } from "../middleware/etag"; import { getService } from "../utils/getService"; interface FullVideoSegment { @@ -166,6 +167,10 @@ async function handleGetLabel(req: Request, res: Response): Promise res.set("ETag", etag)) + .catch(() => null); + if (hasStartSegment) { return segmentData; } else { diff --git a/src/routes/getVideoLabelByHash.ts b/src/routes/getVideoLabelByHash.ts index 076b7562..89425d88 100644 --- a/src/routes/getVideoLabelByHash.ts +++ b/src/routes/getVideoLabelByHash.ts @@ -2,6 +2,7 @@ import { hashPrefixTester } from "../utils/hashPrefixTester"; import { getLabelsByHash } from "./getVideoLabel"; import { Request, Response } from "express"; import { VideoIDHash, Service } from "../types/segments.model"; +import { getEtag } from "../middleware/etag"; import { getService } from "../utils/getService"; export async function getVideoLabelsByHash(req: Request, res: Response): Promise { @@ -25,5 +26,11 @@ export async function getVideoLabelsByHash(req: Request, res: Response): Promise segments: data.segments, hasStartSegment: data.hasStartSegment })); + + + const hashKey = hashPrefix.length === 4 ? "videoLabelHash" : "videoLabelsLargerHash"; + await getEtag(hashKey, hashPrefix, service) + .then(etag => res.set("ETag", etag)) + .catch(() => null); return res.status(output.length === 0 ? 404 : 200).json(output); }