Skip to content

Commit

Permalink
chore: move some utils from blink to serve
Browse files Browse the repository at this point in the history
  • Loading branch information
MKRhere committed Jan 3, 2025
1 parent 0dc65a1 commit 8c53390
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/serve/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { stat } from "node:fs/promises";

export const days = (n: number) => n * 60 * 60 * 24;
export const hours = (n: number) => n * 60 * 60;
export const minutes = (n: number) => n * 60;
export const seconds = (n: number) => n;

export async function generateETagFromFile(filePath: string): Promise<string> {
const stats = await stat(filePath);
const mtime = stats.mtime.getTime().toString();
const size = stats.size.toString();
const rawETag = `${mtime}-${size}`;

const hashBuffer = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(rawETag));
const hashArray = new Uint8Array(hashBuffer);
let hashHex = "";
for (let i = 0; i < hashArray.length; i++) {
hashHex += hashArray[i].toString(16).padStart(2, "0");
}
return `"${hashHex}"`;
}

export const redirect = (url: string, status: 301 | 302 = 302, headers: Record<string, string> = {}) =>
new Response(null, { status, headers: { Location: url, ...headers } });

export const json = (body: any, status = 200, headers: Record<string, string> = {}) =>
new Response(JSON.stringify(body), { status, headers: { "Content-Type": "application/json", ...headers } });

export const html = (content: string, status = 200, headers: Record<string, string> = {}) =>
new Response("<!DOCTYPE html>" + content, { status, headers: { "Content-Type": "text/html", ...headers } });

0 comments on commit 8c53390

Please sign in to comment.