diff --git a/src/server/router/telemetry.ts b/src/server/router/telemetry.ts index aabfe9b..a753c47 100644 --- a/src/server/router/telemetry.ts +++ b/src/server/router/telemetry.ts @@ -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); } diff --git a/src/server/utils/__tests__/common.test.ts b/src/server/utils/__tests__/common.test.ts new file mode 100644 index 0000000..9bea001 --- /dev/null +++ b/src/server/utils/__tests__/common.test.ts @@ -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'); + }); +}); diff --git a/src/server/utils/__tests__/detect.test.ts b/src/server/utils/__tests__/detect.test.ts index a723115..b5e9759 100644 --- a/src/server/utils/__tests__/detect.test.ts +++ b/src/server/utils/__tests__/detect.test.ts @@ -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); }); }); }); diff --git a/src/server/utils/common.ts b/src/server/utils/common.ts index 39c9bce..0e42802 100644 --- a/src/server/utils/common.ts +++ b/src/server/utils/common.ts @@ -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)}"`; +}