0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-29 17:46:30 +01:00

fix(ETag): fixed an error when the file size is too large. (#461)

Made `utils/crypto` supports `ReadbleStream`.

Fix #458
This commit is contained in:
Yusuke Wada 2022-08-09 10:35:30 +09:00 committed by GitHub
parent 0370cac372
commit 7345b8105d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 8 deletions

View File

@ -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}"`

View File

@ -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<string | null> => {
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 {

View File

@ -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}"`

View File

@ -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<string | null> => {
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 {