Skip to content

Commit

Permalink
2
Browse files Browse the repository at this point in the history
  • Loading branch information
EdamAme-x committed Feb 12, 2025
1 parent 604061b commit 0a078f6
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/middleware/etag/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,21 @@ function etagMatches(etag: string, ifNoneMatch: string | null) {
export const etag = (options?: ETagOptions): MiddlewareHandler => {
const retainedHeaders = options?.retainedHeaders ?? RETAINED_304_HEADERS
const weak = options?.weak ?? false
const generator =
options?.generateDigest ??
((body: Uint8Array) =>
crypto.subtle.digest(
{
name: 'SHA-1',
},
body
))
let generator = options?.generateDigest

if (!generator) {
if (crypto && crypto.subtle) {
generator = (body: Uint8Array) =>
crypto.subtle.digest(
{
name: 'SHA-1',
},
body
)
} else {
throw new Error('`crypto.subtle` is undefined. Etag middleware requires it.')

Check failure on line 72 in src/middleware/etag/index.ts

View workflow job for this annotation

GitHub Actions / Main

src/middleware/etag/index.test.ts > Etag Middleware > When crypto is not available > Should not generate etag

Error: `crypto.subtle` is undefined. Etag middleware requires it. ❯ etag src/middleware/etag/index.ts:72:13 ❯ src/middleware/etag/index.test.ts:290:26
}
}

return async function etag(c, next) {
const ifNoneMatch = c.req.header('If-None-Match') ?? null
Expand All @@ -76,7 +82,7 @@ export const etag = (options?: ETagOptions): MiddlewareHandler => {
let etag = res.headers.get('ETag')

if (!etag) {
const hash = await generateDigest(res.clone().body, generator)
const hash = await generateDigest(res.clone().body, generator!)
if (hash === null) {
return
}
Expand Down

0 comments on commit 0a078f6

Please sign in to comment.