feat: implement staticRenderRegeneration (#221)

* feat: implement staticRenderRegeneration

* chore: remove the check in create

* chore: ignore read permission throw

* chore: safe read file

* fix: stream save

* chore: change file folder
This commit is contained in:
Alexandro Castro 2023-09-26 14:59:20 +01:00 • committed by GitHub
parent 33b4105d8a
commit 22e46c69e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 148 additions and 9 deletions

View file

@ -23,7 +23,7 @@ GITHUB_API=https://github.example.com/api/graphql
Run local server. Run local server.
```sh ```sh
deno run --allow-net --allow-read --allow-env debug.ts deno run --allow-net --allow-read --allow-env --allow-write debug.ts
``` ```
Open localhost from your browser. Open localhost from your browser.

View file

@ -4,11 +4,25 @@ import { CONSTANTS, parseParams } from "./src/utils.ts";
import { COLORS, Theme } from "./src/theme.ts"; import { COLORS, Theme } from "./src/theme.ts";
import { Error400, Error404 } from "./src/error_page.ts"; import { Error400, Error404 } from "./src/error_page.ts";
import "https://deno.land/x/dotenv@v0.5.0/load.ts"; import "https://deno.land/x/dotenv@v0.5.0/load.ts";
import {staticRenderRegeneration} from "./src/StaticRenderRegeneration/index.ts";
const apiEndpoint = Deno.env.get("GITHUB_API") || CONSTANTS.DEFAULT_GITHUB_API; const apiEndpoint = Deno.env.get("GITHUB_API") || CONSTANTS.DEFAULT_GITHUB_API;
const client = new GithubAPIClient(apiEndpoint); const client = new GithubAPIClient(apiEndpoint);
export default async (req: Request) => { const defaultHeaders = new Headers(
{
"Content-Type": "image/svg+xml",
"Cache-Control": `public, max-age=${CONSTANTS.CACHE_MAX_AGE}`,
},
)
export default (request: Request) => staticRenderRegeneration(request, {
revalidate: CONSTANTS.REVALIDATE_TIME,
headers: defaultHeaders
}, function (req: Request) {
return app(req);
});
async function app (req: Request): Promise<Response>{
const params = parseParams(req); const params = parseParams(req);
const username = params.get("username"); const username = params.get("username");
const row = params.getNumberValue("row", CONSTANTS.DEFAULT_MAX_ROW); const row = params.getNumberValue("row", CONSTANTS.DEFAULT_MAX_ROW);
@ -69,6 +83,7 @@ export default async (req: Request) => {
}, },
); );
} }
// Success Response // Success Response
return new Response( return new Response(
new Card( new Card(
@ -83,12 +98,7 @@ export default async (req: Request) => {
noFrame, noFrame,
).render(userInfo, theme), ).render(userInfo, theme),
{ {
headers: new Headers( headers: defaultHeaders,
{
"Content-Type": "image/svg+xml",
"Cache-Control": `public, max-age=${CONSTANTS.CACHE_MAX_AGE}`,
},
),
}, },
); );
}; };

View file

@ -0,0 +1,49 @@
import { existsSync } from './utils.ts'
export class CacheManager {
constructor(private revalidateTime: number, private cacheFile: string) {}
// Reason to use /tmp/:
// https://github.com/orgs/vercel/discussions/314
get cacheFilePath(): string {
return `/tmp/${this.cacheFile}`;
}
get cacheFileExists(): boolean {
return existsSync(this.cacheFilePath);
}
get cacheFileLastModified(): Date | null {
if (!this.cacheFileExists) {
return null;
}
const fileInfo = Deno.statSync(this.cacheFilePath);
return fileInfo.mtime ?? null;
}
get cacheFileLastModifiedGetTime(): number | null {
const lastModified = this.cacheFileLastModified;
if (lastModified === null) {
return null;
}
return lastModified.getTime();
}
get isCacheValid(): boolean {
if (this.cacheFileLastModifiedGetTime === null) {
return false;
}
const currentTime = new Date().getTime();
return currentTime - this.cacheFileLastModifiedGetTime < this.revalidateTime;
}
async save (response: Response): Promise<void> {
if(response === null) return
// Prevent TypeError: ReadableStream is locked
const text = await response.clone().text()
const data = new TextEncoder().encode(text)
Deno.writeFile(this.cacheFilePath, data, { create: true }).catch(() => {
console.warn("Failed to save cache file")
});
}
}

View file

@ -0,0 +1,29 @@
import { CacheManager } from "./cache_manager.ts";
import { StaticRegenerationOptions } from "./types.ts";
import { getUrl, readCache, generateUUID } from "./utils.ts";
export async function staticRenderRegeneration(request: Request, options: StaticRegenerationOptions, render: (request: Request) => Promise<Response>) {
// avoid TypeError: Invalid URL at deno:core
const url = getUrl(request)
// if more conditions are added, make sure to create a variable to skipCache
if (url.pathname === "/favicon.ico") {
return await render(request);
}
const cacheFile = await generateUUID(url.pathname + (url.search ?? ""));
const cacheManager = new CacheManager(options.revalidate ?? 0, cacheFile);
if (cacheManager.isCacheValid) {
const cache = readCache(cacheManager.cacheFilePath)
if(cache !== null) {
return new Response(cache, {
headers: options.headers ?? new Headers({}),
});
}
}
const response = await render(request)
cacheManager.save(response)
return response
}

View file

@ -0,0 +1,6 @@
export interface StaticRegenerationOptions {
// The number of milliseconds before the page should be revalidated
revalidate?: number
// The headers to be sent with the response
headers?: Headers
}

View file

@ -0,0 +1,42 @@
export function getUrl(request: Request) {
try {
return new URL(request.url)
} catch {
return {
pathname: request.url,
search: request.url
}
}
}
export function readCache(cacheFilePath: string): Uint8Array | null {
try {
return Deno.readFileSync(cacheFilePath)
} catch {
return null
}
}
// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
export async function generateUUID(message: string): Promise<string> {
const encoder = new TextEncoder();
const data = encoder.encode(message);
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
return hashHex;
}
export const existsSync = (filename: string): boolean => {
try {
Deno.statSync(filename);
// successful, file or directory must exist
return true;
} catch {
return false;
}
};

View file

@ -53,6 +53,8 @@ export function abridgeScore(score: number): string {
return (Math.sign(score) * Math.abs(score)).toString() + "pt"; return (Math.sign(score) * Math.abs(score)).toString() + "pt";
} }
const HOUR_IN_MILLISECONDS = 60 * 60 * 1000;
export const CONSTANTS = { export const CONSTANTS = {
CACHE_MAX_AGE: 7200, CACHE_MAX_AGE: 7200,
DEFAULT_PANEL_SIZE: 110, DEFAULT_PANEL_SIZE: 110,
@ -63,6 +65,7 @@ export const CONSTANTS = {
DEFAULT_NO_BACKGROUND: false, DEFAULT_NO_BACKGROUND: false,
DEFAULT_NO_FRAME: false, DEFAULT_NO_FRAME: false,
DEFAULT_GITHUB_API: "https://api.github.com/graphql", DEFAULT_GITHUB_API: "https://api.github.com/graphql",
REVALIDATE_TIME: HOUR_IN_MILLISECONDS,
}; };
export enum RANK { export enum RANK {