fix: fix etag problem for http cache if not show full telemetry number in badge

This commit is contained in:
moonrailgun 2024-03-24 15:37:17 +08:00
parent 11fd1a616b
commit bd4e6b5b05
4 changed files with 32 additions and 3 deletions

View File

@ -1,7 +1,7 @@
import { Router } from 'express';
import { query, validate } from '../middleware/validate';
import { recordTelemetryEvent, sumTelemetryEvent } from '../model/telemetry';
import { numify } from '../utils/common';
import { generateETag, numify } from '../utils/common';
import { makeBadge } from 'badge-maker';
export const telemetryRouter = Router();
@ -11,6 +11,9 @@ const blankGifBuffer = Buffer.from(
'base64'
);
/**
* @deprecated please use route with telemetry id
*/
telemetryRouter.get(
'/:workspaceId/blank.gif',
validate(
@ -24,6 +27,9 @@ telemetryRouter.get(
}
);
/**
* @deprecated please use route with telemetry id
*/
telemetryRouter.get(
'/:workspaceId/badge.svg',
validate(
@ -97,6 +103,7 @@ telemetryRouter.get(
'Cache-Control',
'no-cache,max-age=0,no-store,s-maxage=0,proxy-revalidate'
)
.header('etag', generateETag(`${title}|${count}`))
.status(200)
.send(svg);
}

View File

@ -0,0 +1,8 @@
import { describe, expect, test } from 'vitest';
import { md5 } from '../common';
describe('md5', () => {
test('normal test', async () => {
expect(md5('Hello world')).toBe('3e25960a79dbc69b674cd4ec67a72c62');
});
});

View File

@ -1,7 +1,9 @@
import { describe, expect, test } from 'vitest';
import { getLocation } from '../detect';
import fs from 'fs-extra';
import { libraryPath } from '../lib';
describe('detect', () => {
describe.runIf(fs.existsSync(libraryPath.geoPath))('detect', () => {
describe('getLocation', () => {
test('should detect local ip', async () => {
const location = await getLocation('127.0.0.1');
@ -18,7 +20,7 @@ describe('detect', () => {
expect(location).toHaveProperty('city', 'Walnut');
expect(location).toHaveProperty('longitude', -117.8512);
expect(location).toHaveProperty('latitude', 34.0233);
expect(location).toHaveProperty('accuracy_radius', 20);
expect(location).toHaveProperty('accuracyRadius', 20);
});
});
});

View File

@ -48,6 +48,14 @@ export function hashUuid(...args: string[]) {
return v5(hash(...args), v5.DNS);
}
/**
* generate hash with md5
* which use in unimportant scene
*/
export function md5(input: string) {
return crypto.createHash('md5').update(input).digest('hex');
}
export function isValidDate(input: any) {
return dayjs(input).isValid();
}
@ -240,3 +248,7 @@ export function numify(num: number): string {
return result;
}
export function generateETag(data: string) {
return `"${md5(data)}"`;
}