utilium 1.2.0 → 1.2.1

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,10 @@ export interface ResourceCacheOptions {
13
13
  */
14
14
  regionGapThreshold?: number;
15
15
  }
16
+ type CacheRange = {
17
+ start: number;
18
+ end: number;
19
+ };
16
20
  export type RequestError = {
17
21
  tag: 'status';
18
22
  response: Response;
@@ -43,3 +47,12 @@ export interface RequestOptions extends ResourceCacheOptions {
43
47
  * @throws RequestError
44
48
  */
45
49
  export declare function GET(url: string, options: RequestOptions, init?: RequestInit): Promise<Uint8Array>;
50
+ /**
51
+ * Synchronously gets a cached resource
52
+ * Assumes you pass valid start and end when using ranges
53
+ */
54
+ export declare function getCached(url: string, options: RequestOptions): {
55
+ data: Uint8Array;
56
+ missing: CacheRange[];
57
+ };
58
+ export {};
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.1",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",
package/src/requests.ts CHANGED
@@ -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
+ }