@@ -11,13 +11,20 @@ export interface CacheObject {
1111 path : string ;
1212 /** Timestamp the entry was first added on. */
1313 time : number ;
14+ /** Size of the data */
15+ size ?: number ;
1416}
1517
1618export interface GetCacheObject {
1719 metadata ?: any ;
1820 integrity : string ;
1921 data : Buffer ;
20- size : number ;
22+ size ?: number ;
23+ }
24+
25+ export interface Memoizer {
26+ get : ( key : string ) => object | undefined ;
27+ set : ( key : string , value : object ) => void ;
2128}
2229
2330export namespace get {
@@ -57,7 +64,7 @@ export namespace get {
5764 * `memoize: false` to the reader functions, but their default will be
5865 * to read from memory.
5966 */
60- memoize ?: null | boolean | undefined ;
67+ memoize ?: null | boolean | Memoizer | undefined ;
6168
6269 /**
6370 * If provided, the data stream will be verified to check that enough
@@ -86,7 +93,6 @@ export namespace get {
8693 * `false`.
8794 */
8895 function hasContent ( cachePath : string , hash : string ) : Promise < HasContentObject | false > ;
89- function hasContentnc ( cachePath : string , hash : string ) : HasContentObject | false ;
9096
9197 /**
9298 * Looks up `key` in the cache index, returning information about the entry
@@ -168,7 +174,7 @@ export namespace put {
168174 * `memoize: false` to the reader functions, but their default will be
169175 * to read from memory.
170176 */
171- memoize ?: null | boolean | undefined ;
177+ memoize ?: null | boolean | Memoizer | undefined ;
172178
173179 /**
174180 * If provided, the data stream will be verified to check that enough
@@ -320,6 +326,60 @@ export namespace verify {
320326 function lastRun ( cachePath : string ) : Promise < Date > ;
321327}
322328
329+ export namespace index {
330+ interface InsertOptions {
331+ /** Arbitrary metadata to be attached to the inserted key. */
332+ metadata ?: any ;
333+
334+ size ?: number ;
335+ }
336+
337+ /**
338+ * Writes an index entry to the cache for the given key without writing content.
339+ *
340+ * It is assumed if you are using this method, you have already stored the
341+ * content some other way and you only wish to add a new index to that content.
342+ * The metadata and size properties are read from opts and used as part of the
343+ * index entry.
344+ *
345+ * Returns a Promise resolving to the newly added entry.
346+ */
347+ function insert (
348+ cache : string ,
349+ key : string ,
350+ integrity : string ,
351+ opts ?: InsertOptions ,
352+ ) : Promise < CacheObject > ;
353+
354+ interface CompactOptions {
355+ validateEntry ?: ( entry : CacheObject ) => boolean ;
356+ }
357+
358+ /**
359+ * Uses matchFn, which must be a synchronous function that accepts two entries
360+ * and returns a boolean indicating whether or not the two entries match, to
361+ * deduplicate all entries in the cache for the given key.
362+
363+ * If opts.validateEntry is provided, it will be called as a function with the
364+ * only parameter being a single index entry. The function must return a
365+ * Boolean, if it returns true the entry is considered valid and will be kept
366+ * in the index, if it returns false the entry will be removed from the index.
367+
368+ * If opts.validateEntry is not provided, however, every entry in the index
369+ * will be deduplicated and kept until the first null integrity is reached,
370+ * removing all entries that were written before the null.
371+
372+ * The deduplicated list of entries is both written to the index, replacing
373+ * the existing content, and returned in the Promise.
374+ */
375+ function compact (
376+ cache : string ,
377+ key : string ,
378+ matchFn : ( obj1 : CacheObject , obj2 : CacheObject ) => boolean ,
379+ opts ?: CompactOptions ,
380+ ) : Promise < CacheObject [ ] > ;
381+ }
382+
323383export function clearMemoized ( ) : Record < string , CacheObject > ;
324384
325385/**
0 commit comments