utilium 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,6 +13,18 @@ export interface ResourceCacheOptions {
13
13
  */
14
14
  regionGapThreshold?: number;
15
15
  }
16
+ export type CacheRange = {
17
+ start: number;
18
+ end: number;
19
+ };
20
+ export interface CacheRegion {
21
+ /** The region's offset from the start of the resource */
22
+ offset: number;
23
+ /** Ranges cached in this region. These are absolute! */
24
+ ranges: CacheRange[];
25
+ /** Data for this region */
26
+ data: Uint8Array;
27
+ }
16
28
  export type RequestError = {
17
29
  tag: 'status';
18
30
  response: Response;
@@ -43,3 +55,11 @@ export interface RequestOptions extends ResourceCacheOptions {
43
55
  * @throws RequestError
44
56
  */
45
57
  export declare function GET(url: string, options: RequestOptions, init?: RequestInit): Promise<Uint8Array>;
58
+ /**
59
+ * Synchronously gets a cached resource
60
+ * Assumes you pass valid start and end when using ranges
61
+ */
62
+ export declare function getCached(url: string, options: RequestOptions): {
63
+ data: Uint8Array;
64
+ missing: CacheRange[];
65
+ };
package/dist/requests.js CHANGED
@@ -170,3 +170,35 @@ export async function GET(url, options, init = {}) {
170
170
  const region = cache.regionAt(start);
171
171
  return region.data.subarray(start - region.offset, end - region.offset);
172
172
  }
173
+ /**
174
+ * Synchronously gets a cached resource
175
+ * Assumes you pass valid start and end when using ranges
176
+ */
177
+ export function getCached(url, options) {
178
+ const cache = requestsCache.get(url);
179
+ /**
180
+ * @todo Make sure we have a size?
181
+ */
182
+ if (!cache)
183
+ return { data: new Uint8Array(0), missing: [{ start: 0, end: options.size ?? 0 }] };
184
+ const { start = 0, end = cache.size } = options;
185
+ const data = new Uint8Array(end - start);
186
+ for (const region of cache.regions) {
187
+ if (region.offset + region.data.byteLength <= start)
188
+ continue;
189
+ if (region.offset >= end)
190
+ break;
191
+ for (const range of region.ranges) {
192
+ if (range.end <= start)
193
+ continue;
194
+ if (range.start >= end)
195
+ break;
196
+ const overlapStart = Math.max(range.start, start);
197
+ const overlapEnd = Math.min(range.end, end);
198
+ if (overlapStart >= overlapEnd)
199
+ continue;
200
+ data.set(region.data.subarray(overlapStart - region.offset, overlapEnd - region.offset), overlapStart - start);
201
+ }
202
+ }
203
+ return { data, missing: cache.missing(start, end) };
204
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",
package/src/requests.ts CHANGED
@@ -21,9 +21,9 @@ export interface ResourceCacheOptions {
21
21
  regionGapThreshold?: number;
22
22
  }
23
23
 
24
- type CacheRange = { start: number; end: number };
24
+ export type CacheRange = { start: number; end: number };
25
25
 
26
- interface CacheRegion {
26
+ export interface CacheRegion {
27
27
  /** The region's offset from the start of the resource */
28
28
  offset: number;
29
29
 
@@ -37,7 +37,7 @@ interface CacheRegion {
37
37
  /** The cache for a specific resource */
38
38
  class ResourceCache {
39
39
  /** Regions used to reduce unneeded allocations. Think of sparse arrays. */
40
- protected readonly regions: CacheRegion[] = [];
40
+ public readonly regions: CacheRegion[] = [];
41
41
 
42
42
  public constructor(
43
43
  /** The resource URL */
@@ -249,3 +249,39 @@ export async function GET(url: string, options: RequestOptions, init: RequestIni
249
249
  const region = cache.regionAt(start)!;
250
250
  return region.data.subarray(start - region.offset, end - region.offset);
251
251
  }
252
+
253
+ /**
254
+ * Synchronously gets a cached resource
255
+ * Assumes you pass valid start and end when using ranges
256
+ */
257
+ export function getCached(url: string, options: RequestOptions): { data: Uint8Array; missing: CacheRange[] } {
258
+ const cache = requestsCache.get(url);
259
+
260
+ /**
261
+ * @todo Make sure we have a size?
262
+ */
263
+ if (!cache) return { data: new Uint8Array(0), missing: [{ start: 0, end: options.size ?? 0 }] };
264
+
265
+ const { start = 0, end = cache.size } = options;
266
+
267
+ const data = new Uint8Array(end - start);
268
+
269
+ for (const region of cache.regions) {
270
+ if (region.offset + region.data.byteLength <= start) continue;
271
+ if (region.offset >= end) break;
272
+
273
+ for (const range of region.ranges) {
274
+ if (range.end <= start) continue;
275
+ if (range.start >= end) break;
276
+
277
+ const overlapStart = Math.max(range.start, start);
278
+ const overlapEnd = Math.min(range.end, end);
279
+
280
+ if (overlapStart >= overlapEnd) continue;
281
+
282
+ data.set(region.data.subarray(overlapStart - region.offset, overlapEnd - region.offset), overlapStart - start);
283
+ }
284
+ }
285
+
286
+ return { data, missing: cache.missing(start, end) };
287
+ }