Skip to content

Commit

Permalink
feature: use generateEtag function (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
levivilet authored Dec 27, 2024
1 parent cc770bc commit c42f960
Showing 1 changed file with 2 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { createHash } from 'node:crypto'
import type { HandlerOptions } from '../HandlerOptions/HandlerOptions.ts'
import type { RequestOptions } from '../RequestOptions/RequestOptions.ts'
import * as CrossOriginEmbedderPolicy from '../CrossOriginEmbedderPolicy/CrossOriginEmbedderPolicy.ts'
import * as CrossOriginResourcePolicy from '../CrossOriginResourcePolicy/CrossOriginResourcePolicy.ts'
import * as GenerateEtag from '../GenerateEtag/GenerateEtag.ts'
import * as GetContentSecurityPolicyDocument from '../GetContentSecurityPolicyDocument/GetContentSecurityPolicyDocument.ts'
import * as GetContentType from '../GetContentType/GetContentType.ts'
import * as HttpHeader from '../HttpHeader/HttpHeader.ts'
import * as MatchesEtag from '../MatchesEtag/MatchesEtag.ts'
import { NotModifiedResponse } from '../Responses/NotModifiedResponse.ts'
import { ServerErrorResponse } from '../Responses/ServerErrorResponse.ts'

const generateEtag = (content: string): string => {
const hash = createHash('sha1')
hash.update(content)
return `W/"${hash.digest('hex')}"`
}

export const handleIndexHtml = async (request: RequestOptions, options: HandlerOptions): Promise<Response> => {
try {
if (!options.iframeContent) {
Expand All @@ -32,7 +26,7 @@ export const handleIndexHtml = async (request: RequestOptions, options: HandlerO
}

if (options.etag) {
const etag = generateEtag(options.iframeContent)
const etag = GenerateEtag.generateEtag(options.iframeContent)
if (MatchesEtag.matchesEtag(request, etag)) {
return new NotModifiedResponse(etag, headers)
}
Expand Down

0 comments on commit c42f960

Please sign in to comment.