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:
parent
0370cac372
commit
7345b8105d
@ -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}"`
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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}"`
|
||||
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user