Skip to content

Commit

Permalink
fix: fix etag problem for http cache if not show full telemetry numbe…
Browse files Browse the repository at this point in the history
…r in badge
  • Loading branch information
moonrailgun committed Mar 24, 2024
1 parent 11fd1a6 commit bd4e6b5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
9 changes: 8 additions & 1 deletion 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();
Expand All @@ -11,6 +11,9 @@ const blankGifBuffer = Buffer.from(
'base64'
);

/**
* @deprecated please use route with telemetry id
*/
telemetryRouter.get(
'/:workspaceId/blank.gif',
validate(
Expand All @@ -24,6 +27,9 @@ telemetryRouter.get(
}
);

/**
* @deprecated please use route with telemetry id
*/
telemetryRouter.get(
'/:workspaceId/badge.svg',
validate(
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 8 additions & 0 deletions 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');
});
});
6 changes: 4 additions & 2 deletions 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');
Expand All @@ -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);
});
});
});
12 changes: 12 additions & 0 deletions src/server/utils/common.ts
Expand Up @@ -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();
}
Expand Down Expand Up @@ -240,3 +248,7 @@ export function numify(num: number): string {

return result;
}

export function generateETag(data: string) {
return `"${md5(data)}"`;
}

0 comments on commit bd4e6b5

Please sign in to comment.