Comparing version 1.2.0 to 1.2.1
@@ -16,2 +16,6 @@ export interface ResourceCacheOptions { | ||
} | ||
type CacheRange = { | ||
start: number; | ||
end: number; | ||
}; | ||
export type RequestError = { | ||
@@ -47,1 +51,10 @@ tag: 'status'; | ||
export declare function GET(url: string, options: RequestOptions, init?: RequestInit): Promise<Uint8Array>; | ||
/** | ||
* Synchronously gets a cached resource | ||
* Assumes you pass valid start and end when using ranges | ||
*/ | ||
export declare function getCached(url: string, options: RequestOptions): { | ||
data: Uint8Array; | ||
missing: CacheRange[]; | ||
}; | ||
export {}; |
@@ -173,1 +173,33 @@ /* Utilities for `fetch` when using range requests. It also allows you to handle errors easier */ | ||
} | ||
/** | ||
* Synchronously gets a cached resource | ||
* Assumes you pass valid start and end when using ranges | ||
*/ | ||
export function getCached(url, options) { | ||
const cache = requestsCache.get(url); | ||
/** | ||
* @todo Make sure we have a size? | ||
*/ | ||
if (!cache) | ||
return { data: new Uint8Array(0), missing: [{ start: 0, end: options.size ?? 0 }] }; | ||
const { start = 0, end = cache.size } = options; | ||
const data = new Uint8Array(end - start); | ||
for (const region of cache.regions) { | ||
if (region.offset + region.data.byteLength <= start) | ||
continue; | ||
if (region.offset >= end) | ||
break; | ||
for (const range of region.ranges) { | ||
if (range.end <= start) | ||
continue; | ||
if (range.start >= end) | ||
break; | ||
const overlapStart = Math.max(range.start, start); | ||
const overlapEnd = Math.min(range.end, end); | ||
if (overlapStart >= overlapEnd) | ||
continue; | ||
data.set(region.data.subarray(overlapStart - region.offset, overlapEnd - region.offset), overlapStart - start); | ||
} | ||
} | ||
return { data, missing: cache.missing(start, end) }; | ||
} |
{ | ||
"name": "utilium", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Typescript utilities", | ||
@@ -5,0 +5,0 @@ "funding": { |
@@ -40,3 +40,3 @@ /* Utilities for `fetch` when using range requests. It also allows you to handle errors easier */ | ||
/** Regions used to reduce unneeded allocations. Think of sparse arrays. */ | ||
protected readonly regions: CacheRegion[] = []; | ||
public readonly regions: CacheRegion[] = []; | ||
@@ -253,1 +253,37 @@ public constructor( | ||
} | ||
/** | ||
* Synchronously gets a cached resource | ||
* Assumes you pass valid start and end when using ranges | ||
*/ | ||
export function getCached(url: string, options: RequestOptions): { data: Uint8Array; missing: CacheRange[] } { | ||
const cache = requestsCache.get(url); | ||
/** | ||
* @todo Make sure we have a size? | ||
*/ | ||
if (!cache) return { data: new Uint8Array(0), missing: [{ start: 0, end: options.size ?? 0 }] }; | ||
const { start = 0, end = cache.size } = options; | ||
const data = new Uint8Array(end - start); | ||
for (const region of cache.regions) { | ||
if (region.offset + region.data.byteLength <= start) continue; | ||
if (region.offset >= end) break; | ||
for (const range of region.ranges) { | ||
if (range.end <= start) continue; | ||
if (range.start >= end) break; | ||
const overlapStart = Math.max(range.start, start); | ||
const overlapEnd = Math.min(range.end, end); | ||
if (overlapStart >= overlapEnd) continue; | ||
data.set(region.data.subarray(overlapStart - region.offset, overlapEnd - region.offset), overlapStart - start); | ||
} | ||
} | ||
return { data, missing: cache.missing(start, end) }; | ||
} |
116415
3273