Skip to content

Commit

Permalink
fix: crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
olros committed Nov 17, 2023
1 parent 6cd67b6 commit 3db33d2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {

return json({ ok: true }, { status: 202 });
} catch (e) {
console.error('[API-Internal - Pageview]', e);
console.error('[API-Internal - PageviewNext]', e);
return json({ ok: false }, { status: 400 });
}
};
21 changes: 14 additions & 7 deletions web/app/routes/api.$teamSlug.$projectSlug.pageview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ export const config = { runtime: 'edge' };
export const getPageViewUserIdHash = async (ip: string, userAgent: string, date: Date): Promise<PageviewRequestData['user_hash']> => {
invariant(process.env.SECRET_KEY, 'Expected environment variable "SECRET_KEY" to be set when tracking page visitors');
const day = format(date, 'yyyy-MM-dd');
const msgUint8 = new TextEncoder().encode(`${ip}_${userAgent}_${day}_${process.env.SECRET_KEY}`); // encode as (utf-8) Uint8Array
const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8); // hash the message
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join(''); // convert bytes to hex string
return hashHex;
const userId = `${ip}_${userAgent}_${day}_${process.env.SECRET_KEY}`;
try {
const msgUint8 = new TextEncoder().encode(userId); // encode as (utf-8) Uint8Array
const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8); // hash the message
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join(''); // convert bytes to hex string
return hashHex;
} catch (e) {
console.error('[API - UserIdHash]', e);
return userId;
}
};

export const getPageViewUserAgentData = (ua: UserAgentData): PageviewRequestData['userAgentData'] => {
Expand All @@ -40,7 +46,8 @@ const getPageViewNextRequest = async (request: Request): Promise<Request | undef

console.info('[API - PageViewNext]', { geo, ua, ip });

const geoData = geo.city && geo.country && geo.flag && geo.latitude && geo.longitude ? (geo as PageviewRequestData['geo']) : undefined;
const geoData: PageviewRequestData['geo'] | undefined =
geo.city && geo.country && geo.flag && geo.latitude && geo.longitude ? (geo as PageviewRequestData['geo']) : undefined;
if (!geoData || !ip || ua.isBot) {
throw new Error(
JSON.stringify({
Expand All @@ -54,7 +61,7 @@ const getPageViewNextRequest = async (request: Request): Promise<Request | undef
const userAgentData = getPageViewUserAgentData(ua);
const user_hash = await getPageViewUserIdHash(ip, ua.ua, date);

return new Request('/pageview-next', {
return new Request('https://stats.olafros.com/api/pageview-next', {
method: 'POST',
body: JSON.stringify({
data: (await request.json()) as PageviewInput,
Expand Down

1 comment on commit 3db33d2

@vercel
Copy link

@vercel vercel bot commented on 3db33d2 Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

stats – ./

stats.olafros.com
stats-git-main-olros.vercel.app
stats-olros.vercel.app
stats-nu-nine.vercel.app

Please sign in to comment.