Skip to content

Commit b100108

Browse files
authored
🤖 Merge PR DefinitelyTyped#72986 cacache: update type definition to match v19.0.1 by @hexchain
1 parent a633aaf commit b100108

3 files changed

Lines changed: 75 additions & 5 deletions

File tree

‎types/cacache/cacache-tests.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ cacache.ls.stream(cachePath).on("data", data => {
1111

1212
cacache.get(cachePath, "my-thing", { memoize: true }).then(() => {});
1313

14+
cacache.get(cachePath, "my-thing", { memoize: new Map() }).then(() => {});
15+
1416
cacache.get.byDigest(cachePath, "sha512-BaSe64HaSh").then(() => {});
1517

1618
cacache.get
@@ -110,3 +112,11 @@ cacache.verify(cachePath).then(() => {
110112
console.log("cacache.verify was last called on" + lastTime);
111113
});
112114
});
115+
116+
cacache.index.insert(cachePath, "registry.npmjs.org|cacache@1.0.0", "sha512-BaSe64HaSh").then((entry) => {
117+
entry; // $ExpectType CacheObject
118+
});
119+
120+
cacache.index.compact(cachePath, "registry.npmjs.org|cacache@1.0.0", (_a, _b) => true).then((entries) => {
121+
entries; // $ExpectType CacheObject[]
122+
});

‎types/cacache/index.d.ts‎

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1618
export 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

2330
export 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+
323383
export function clearMemoized(): Record<string, CacheObject>;
324384

325385
/**

‎types/cacache/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "@types/cacache",
4-
"version": "17.0.9999",
4+
"version": "19.0.9999",
55
"projects": [
66
"https://github.com/npm/cacache#readme"
77
],

0 commit comments

Comments
 (0)