|
import { sha256 } from './js/sha256.js'; |
|
|
|
|
|
export default async function middleware(request) { |
|
|
|
const url = new URL(request.url); |
|
|
|
|
|
const isHtmlPage = url.pathname.endsWith('.html') || url.pathname.endsWith('/'); |
|
if (!isHtmlPage) { |
|
return; |
|
} |
|
|
|
|
|
const response = await fetch(request); |
|
|
|
|
|
const contentType = response.headers.get('content-type') || ''; |
|
if (!contentType.includes('text/html')) { |
|
return response; |
|
} |
|
|
|
|
|
const originalHtml = await response.text(); |
|
|
|
|
|
|
|
const password = process.env.PASSWORD || ''; |
|
let passwordHash = ''; |
|
if (password) { |
|
passwordHash = await sha256(password); |
|
} |
|
const modifiedHtml = originalHtml.replace( |
|
'window.__ENV__.PASSWORD = "{{PASSWORD}}";', |
|
`window.__ENV__.PASSWORD = "${passwordHash}"; // SHA-256 hash` |
|
); |
|
|
|
|
|
return new Response(modifiedHtml, { |
|
status: response.status, |
|
statusText: response.statusText, |
|
headers: response.headers |
|
}); |
|
} |
|
|
|
export const config = { |
|
matcher: ['/', '/((?!api|_next/static|_vercel|favicon.ico).*)'], |
|
}; |