Skip to content

Commit 02eef1d

Browse files
committed
don't cache asyncBuffer if any error
(should only occur when fetching bytelength)
1 parent e155a20 commit 02eef1d

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/lib/utils.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@ export function cn(...names: (string | undefined | false)[]): string {
1111
/**
1212
* Convert AsyncBufferFromUrl to AsyncBuffer.
1313
*/
14-
export function asyncBufferFrom(from: AsyncBufferFrom): Promise<AsyncBuffer> {
14+
export async function asyncBufferFrom(from: AsyncBufferFrom): Promise<AsyncBuffer> {
1515
if ('url' in from) {
1616
// Cached asyncBuffer for urls only
1717
const key = JSON.stringify(from)
1818
const cached = cache.get(key)
1919
if (cached) return cached
20-
const asyncBuffer = asyncBufferFromUrl(from).then(cachedAsyncBuffer)
21-
cache.set(key, asyncBuffer)
22-
return asyncBuffer
20+
const buffer = await asyncBufferFromUrl(from)
21+
const newCached = cachedAsyncBuffer(buffer)
22+
// only cache if no error, otherwise let the error bubble up
23+
cache.set(key, newCached)
24+
return newCached
2325
} else {
2426
return from.file.arrayBuffer()
2527
}
2628
}
27-
const cache = new Map<string, Promise<AsyncBuffer>>()
29+
const cache = new Map<string, AsyncBuffer>()
2830
// TODO(SL): do we really want a singleton?
2931

3032
export function getFileDateShort(file?: { lastModified?: string }): string {

0 commit comments

Comments
 (0)