diff --git a/deno_dist/middleware/etag/index.ts b/deno_dist/middleware/etag/index.ts index 00702981..e618715f 100644 --- a/deno_dist/middleware/etag/index.ts +++ b/deno_dist/middleware/etag/index.ts @@ -1,6 +1,5 @@ import type { Context } from '../../context.ts' import type { Next } from '../../hono.ts' -import { parseBody } from '../../utils/body.ts' import { sha1 } from '../../utils/crypto.ts' type ETagOptions = { @@ -15,8 +14,7 @@ export const etag = (options: ETagOptions = { weak: false }) => { const res = c.res as Response const clone = res.clone() - const body = await parseBody(res) - const hash = await sha1(body) + const hash = await sha1(res.body || '') const etag = options.weak ? `W/"${hash}"` : `"${hash}"` diff --git a/deno_dist/utils/crypto.ts b/deno_dist/utils/crypto.ts index 506eac9a..1066f897 100644 --- a/deno_dist/utils/crypto.ts +++ b/deno_dist/utils/crypto.ts @@ -3,7 +3,7 @@ type Algorithm = { alias: string } -type Data = string | boolean | number | object | ArrayBufferView | ArrayBuffer +type Data = string | boolean | number | object | ArrayBufferView | ArrayBuffer | ReadableStream export const sha256 = async (data: Data) => { const algorithm: Algorithm = { name: 'SHA-256', alias: 'sha256' } @@ -26,6 +26,15 @@ export const md5 = async (data: Data) => { export const createHash = async (data: Data, algorithm: Algorithm): Promise => { let sourceBuffer: ArrayBufferView | ArrayBuffer + if (data instanceof ReadableStream) { + let body = '' + const reader = data.getReader() + await reader?.read().then(async (chuck) => { + const value = await createHash(chuck.value || '', algorithm) + body += value + }) + return body + } if (ArrayBuffer.isView(data) || data instanceof ArrayBuffer) { sourceBuffer = data } else { diff --git a/src/middleware/etag/index.ts b/src/middleware/etag/index.ts index 9945ca96..02a6dea9 100644 --- a/src/middleware/etag/index.ts +++ b/src/middleware/etag/index.ts @@ -1,6 +1,5 @@ import type { Context } from '../../context' import type { Next } from '../../hono' -import { parseBody } from '../../utils/body' import { sha1 } from '../../utils/crypto' type ETagOptions = { @@ -15,8 +14,7 @@ export const etag = (options: ETagOptions = { weak: false }) => { const res = c.res as Response const clone = res.clone() - const body = await parseBody(res) - const hash = await sha1(body) + const hash = await sha1(res.body || '') const etag = options.weak ? `W/"${hash}"` : `"${hash}"` diff --git a/src/utils/crypto.ts b/src/utils/crypto.ts index 506eac9a..1066f897 100644 --- a/src/utils/crypto.ts +++ b/src/utils/crypto.ts @@ -3,7 +3,7 @@ type Algorithm = { alias: string } -type Data = string | boolean | number | object | ArrayBufferView | ArrayBuffer +type Data = string | boolean | number | object | ArrayBufferView | ArrayBuffer | ReadableStream export const sha256 = async (data: Data) => { const algorithm: Algorithm = { name: 'SHA-256', alias: 'sha256' } @@ -26,6 +26,15 @@ export const md5 = async (data: Data) => { export const createHash = async (data: Data, algorithm: Algorithm): Promise => { let sourceBuffer: ArrayBufferView | ArrayBuffer + if (data instanceof ReadableStream) { + let body = '' + const reader = data.getReader() + await reader?.read().then(async (chuck) => { + const value = await createHash(chuck.value || '', algorithm) + body += value + }) + return body + } if (ArrayBuffer.isView(data) || data instanceof ArrayBuffer) { sourceBuffer = data } else {