-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
62 additions
and
51 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
/** | ||
* Returns a cached layer on top of an AsyncBuffer. For caching slices of a file | ||
* that are read multiple times, possibly over a network. | ||
* | ||
* @typedef {import('./types.js').AsyncBuffer} AsyncBuffer | ||
* @param {AsyncBuffer} file file-like object to cache | ||
* @returns {AsyncBuffer} cached file-like object | ||
*/ | ||
export function cachedAsyncBuffer({ byteLength, slice }) { | ||
const cache = new Map() | ||
return { | ||
byteLength, | ||
/** | ||
* @param {number} start | ||
* @param {number} [end] | ||
* @returns {import('./types.js').Awaitable<ArrayBuffer>} | ||
*/ | ||
slice(start, end) { | ||
const key = cacheKey(start, end, byteLength) | ||
const cached = cache.get(key) | ||
if (cached) return cached | ||
// cache miss, read from file | ||
const promise = slice(start, end) | ||
cache.set(key, promise) | ||
return promise | ||
}, | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Returns canonical cache key for a byte range 'start,end'. | ||
* Normalize int-range and suffix-range requests to the same key. | ||
* | ||
* @param {number} start start byte of range | ||
* @param {number} [end] end byte of range, or undefined for suffix range | ||
* @param {number} [size] size of file, or undefined for suffix range | ||
* @returns {string} | ||
*/ | ||
function cacheKey(start, end, size) { | ||
if (start < 0) { | ||
if (end !== undefined) throw new Error(`invalid suffix range [${start}, ${end}]`) | ||
if (size === undefined) return `${start},` | ||
return `${size + start},${size}` | ||
} else if (end !== undefined) { | ||
if (start > end) throw new Error(`invalid empty range [${start}, ${end}]`) | ||
return `${start},${end}` | ||
} else if (size === undefined) { | ||
return `${start},` | ||
} else { | ||
return `${start},${size}` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters