fix(ETag): fixed an error when the file size is too large. (#461)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Made `utils/crypto` supports `ReadbleStream`. Fix #458
authored by Yusuke Wada · committed by GitHub · parent 0370cac · Browse files · 4 files·+22 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+22 added−8 removed
- other:+22 added−8 removed
Provenance
- Commit
7345b81- Tree
a8edaad- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
0370cac
4 files changed+22 added−8 removed
modified:deno_dist/middleware/etag/index.ts+1 added-3 removed
12334566 unmodified lines131415161819171819202112 unmodified linesimport 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 = { weak: boolean6 unmodified lines await next()
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}"`
if (ifNoneMatch && ifNoneMatch === etag) {12 unmodified linesmodified:deno_dist/utils/crypto.ts+10 added-1 removed
12345667891014 unmodified lines252627282930313233343536373839404119 unmodified linestype Algorithm = { name: string alias: string}
type Data = string | boolean | number | object | ArrayBufferView | ArrayBuffertype Data = string | boolean | number | object | ArrayBufferView | ArrayBuffer | ReadableStream
export const sha256 = async (data: Data) => { const algorithm: Algorithm = { name: 'SHA-256', alias: 'sha256' } const hash = await createHash(data, algorithm)14 unmodified lines
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 { if (typeof data === 'object') {19 unmodified linesmodified:src/middleware/etag/index.ts+1 added-3 removed
12334566 unmodified lines131415161819171819202112 unmodified linesimport type { Context } from '../../context'import type { Next } from '../../hono'import { parseBody } from '../../utils/body'import { sha1 } from '../../utils/crypto'
type ETagOptions = { weak: boolean6 unmodified lines await next()
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}"`
if (ifNoneMatch && ifNoneMatch === etag) {12 unmodified linesmodified:src/utils/crypto.ts+10 added-1 removed
12345667891014 unmodified lines252627282930313233343536373839404119 unmodified linestype Algorithm = { name: string alias: string}
type Data = string | boolean | number | object | ArrayBufferView | ArrayBuffertype Data = string | boolean | number | object | ArrayBufferView | ArrayBuffer | ReadableStream
export const sha256 = async (data: Data) => { const algorithm: Algorithm = { name: 'SHA-256', alias: 'sha256' } const hash = await createHash(data, algorithm)14 unmodified lines
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 { if (typeof data === 'object') {19 unmodified lines