UNPKG

14.7 kBTypeScriptView Raw
1// Type definitions for cacache 15.0
2// Project: https://github.com/npm/cacache#readme
3// Definitions by: Florian Imdahl <https://github.com/ffflorian>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5// TypeScript Version: 2.1
6
7/// <reference types="node" />
8
9export interface CacheObject {
10 /** Subresource Integrity hash for the content this entry refers to. */
11 integrity: string;
12 /** Key the entry was looked up under. Matches the key argument. */
13 key: string;
14 /** User-assigned metadata associated with the entry/content. */
15 metadata?: any;
16 /** Filesystem path where content is stored, joined with cache argument. */
17 path: string;
18 /** Timestamp the entry was first added on. */
19 time: number;
20}
21
22export interface GetCacheObject {
23 metadata?: any;
24 integrity: string;
25 data: Buffer;
26 size: number;
27}
28
29export namespace get {
30 interface HasContentObject {
31 size: number;
32 sri: {
33 algorithm: string;
34 digest: string;
35 options: any[];
36 source: string;
37 };
38 }
39
40 interface Options {
41 /**
42 * If present, the pre-calculated digest for the inserted content. If
43 * this option is provided and does not match the post-insertion digest,
44 * insertion will fail with an `EINTEGRITY` error.
45 *
46 * `algorithms` has no effect if this option is present.
47 */
48 integrity?: string | undefined;
49
50 /**
51 * Default: `null`
52 *
53 * If provided, cacache will memoize the given cache insertion in
54 * memory, bypassing any filesystem checks for that key or digest in
55 * future cache fetches. Nothing will be written to the in-memory cache
56 * unless this option is explicitly truthy.
57 *
58 * If `opts.memoize` is an object or a `Map`-like (that is, an object
59 * with `get` and `set` methods), it will be written to instead of the
60 * global memoization cache.
61 *
62 * Reading from disk data can be forced by explicitly passing
63 * `memoize: false` to the reader functions, but their default will be
64 * to read from memory.
65 */
66 memoize?: null | boolean | undefined;
67
68 /**
69 * If provided, the data stream will be verified to check that enough
70 * data was passed through. If there's more or less data than expected,
71 * insertion will fail with an `EBADSIZE` error.
72 */
73 size?: number | undefined;
74 }
75
76 namespace copy {
77 function byDigest(cachePath: string, hash: string, dest: string, opts?: Options): Promise<string>;
78 }
79
80 namespace stream {
81 function byDigest(cachePath: string, hash: string, opts?: Options): NodeJS.ReadableStream;
82 }
83
84 function byDigest(cachePath: string, hash: string, opts?: Options): Promise<string>;
85 function copy(cachePath: string, key: string, dest: string, opts?: Options): Promise<CacheObject>;
86
87 /**
88 * Looks up a Subresource Integrity hash in the cache. If content exists
89 * for this `integrity`, it will return an object, with the specific single
90 * integrity hash that was found in sri key, and the size of the found
91 * content as size. If no content exists for this integrity, it will return
92 * `false`.
93 */
94 function hasContent(cachePath: string, hash: string): Promise<HasContentObject | false>;
95 function hasContentnc(cachePath: string, hash: string): HasContentObject | false;
96
97 /**
98 * Looks up `key` in the cache index, returning information about the entry
99 * if one exists.
100 */
101 function info(cachePath: string, key: string): Promise<CacheObject>;
102
103 /**
104 * Returns a Readable Stream of the cached data identified by `key`.
105 *
106 * If there is no content identified by `key`, or if the locally-stored data
107 * does not pass the validity checksum, an error will be emitted.
108 *
109 * `metadata` and `integrity` events will be emitted before the stream
110 * closes, if you need to collect that extra data about the cached entry.
111 *
112 * A sub-function, `get.stream.byDigest` may be used for identical behavior,
113 * except lookup will happen by integrity hash, bypassing the index
114 * entirely. This version does not emit the `metadata` and `integrity`
115 * events at all.
116 */
117 function stream(cachePath: string, key: string, opts?: Options): NodeJS.ReadableStream;
118 function sync(cachePath: string, key: string, opts?: Options): CacheObject;
119 function syncDigest(cachePath: string, key: string, opts?: Options): CacheObject;
120}
121
122export namespace ls {
123 type Cache = Record<string, CacheObject & { size: number }>;
124
125 /**
126 * Lists info for all entries currently in the cache as a single large
127 * object.
128 *
129 * This works just like `ls`, except `get.info` entries are returned as
130 * `'data'` events on the returned stream.
131 */
132 function stream(cachePath: string): NodeJS.ReadableStream;
133}
134
135export namespace put {
136 interface Options {
137 /**
138 * Default: `['sha512']`
139 *
140 * Hashing algorithms to use when calculating the subresource integrity
141 * digest for inserted data. Can use any algorithm listed in
142 * `crypto.getHashes()` or `'omakase'`/`'お任せします'` to pick a random
143 * hash algorithm on each insertion. You may also use any anagram of
144 * `'modnar'` to use this feature.
145 *
146 * Currently only supports one algorithm at a time (i.e., an array
147 * length of exactly `1`). Has no effect if `opts.integrity` is present.
148 */
149 algorithms?: string[] | undefined;
150
151 /**
152 * If present, the pre-calculated digest for the inserted content. If
153 * this option is provided and does not match the post-insertion digest,
154 * insertion will fail with an `EINTEGRITY` error.
155 *
156 * `algorithms` has no effect if this option is present.
157 */
158 integrity?: string | undefined;
159
160 /** Arbitrary metadata to be attached to the inserted key. */
161 metadata?: any;
162
163 /**
164 * Default: `null`
165 *
166 * If provided, cacache will memoize the given cache insertion in
167 * memory, bypassing any filesystem checks for that key or digest in
168 * future cache fetches. Nothing will be written to the in-memory cache
169 * unless this option is explicitly truthy.
170 *
171 * If `opts.memoize` is an object or a `Map`-like (that is, an object
172 * with `get` and `set` methods), it will be written to instead of the
173 * global memoization cache.
174 *
175 * Reading from disk data can be forced by explicitly passing
176 * `memoize: false` to the reader functions, but their default will be
177 * to read from memory.
178 */
179 memoize?: null | boolean | undefined;
180
181 /**
182 * If provided, the data stream will be verified to check that enough
183 * data was passed through. If there's more or less data than expected,
184 * insertion will fail with an `EBADSIZE` error.
185 */
186 size?: number | undefined;
187
188 /**
189 * Default: `null`
190 *
191 * Prefix to append on the temporary directory name inside the cache's tmp dir.
192 */
193 tmpPrefix?: null | string | undefined;
194 }
195
196 /**
197 * Returns a Writable Stream that inserts data written to it into the cache.
198 * Emits an `integrity` event with the digest of written contents when it
199 * succeeds.
200 */
201 function stream(cachePath: string, key: string, opts?: Options): NodeJS.WritableStream;
202}
203
204export namespace rm {
205 /**
206 * Clears the entire cache. Mainly by blowing away the cache directory
207 * itself.
208 */
209 function all(cachePath: string): Promise<void>;
210
211 /**
212 * Removes the index entry for `key`. Content will still be accessible if
213 * requested directly by content address (`get.stream.byDigest`).
214 *
215 * To remove the content itself (which might still be used by other
216 * entries), use `rm.content`. Or, to safely vacuum any unused content,
217 * use `verify`.
218 */
219 function entry(cachePath: string, key: string): Promise<CacheObject>;
220
221 /**
222 * Removes the content identified by `integrity`. Any index entries
223 * referring to it will not be usable again until the content is re-added
224 * to the cache with an identical digest.
225 */
226 function content(cachePath: string, hash: string): Promise<boolean>;
227}
228
229export namespace tmp {
230 type Callback = (dir: string) => void;
231
232 interface Options {
233 /**
234 * Default: 20
235 *
236 * Number of concurrently read files in the filesystem while doing clean up.
237 */
238 concurrency?: number | undefined;
239
240 /**
241 * Receives a formatted entry. Return `false` to remove it.
242 *
243 * Note: might be called more than once on the same entry.
244 */
245 filter?: string | false | undefined;
246
247 /**
248 * Custom logger function:
249 * ```
250 * log: { silly () {} }
251 * log.silly('verify', 'verifying cache at', cache)
252 * ```
253 */
254 log?: Record<string, (...args: any[]) => any> | undefined;
255
256 /**
257 * Default: `null`
258 *
259 * Prefix to append on the temporary directory name inside the cache's tmp dir.
260 */
261 tmpPrefix?: null | string | undefined;
262 }
263
264 /**
265 * Sets the `uid` and `gid` properties on all files and folders within the
266 * tmp folder to match the rest of the cache.
267 *
268 * Use this after manually writing files into `tmp.mkdir` or `tmp.withTmp`.
269 */
270 function fix(cachePath: string): Promise<void>;
271
272 /**
273 * Returns a unique temporary directory inside the cache's `tmp` dir. This
274 * directory will use the same safe user assignment that all the other stuff
275 * use.
276 *
277 * Once the directory is made, it's the user's responsibility that all files
278 * within are given the appropriate `gid`/`uid` ownership settings to match
279 * the rest of the cache. If not, you can ask cacache to do it for you by
280 * calling `tmp.fix()`, which will fix all tmp directory permissions.
281 *
282 * If you want automatic cleanup of this directory, use `tmp.withTmp()`
283 */
284 function mkdir(cachePath: string, opts?: Options): Promise<string>;
285
286 /**
287 * Creates a temporary directory with `tmp.mkdir()` and calls `cb` with it.
288 * The created temporary directory will be removed when the return value of
289 * `cb()` resolves, the tmp directory will be automatically deleted once that
290 * promise completes.
291 *
292 * The same caveats apply when it comes to managing permissions for the tmp dir's contents.
293 */
294 function withTmp(cachePath: string, opts: Options, cb: Callback): void;
295 function withTmp(cachePath: string, cb: Callback): void;
296}
297
298export namespace verify {
299 interface Options {
300 /**
301 * Default: 20
302 *
303 * Number of concurrently read files in the filesystem while doing clean up.
304 */
305 concurrency?: number | undefined;
306
307 /**
308 * Receives a formatted entry. Return `false` to remove it.
309 *
310 * Note: might be called more than once on the same entry.
311 */
312 filter?: string | false | undefined;
313
314 /**
315 * Custom logger function:
316 * ```
317 * log: { silly () {} }
318 * log.silly('verify', 'verifying cache at', cache)
319 * ```
320 */
321 log?: Record<string, (...args: any[]) => any> | undefined;
322 }
323
324 /**
325 * Returns a Date representing the last time `cacache.verify` was run on
326 * `cache`.
327 */
328 function lastRun(cachePath: string): Promise<Date>;
329}
330
331export function clearMemoized(): Record<string, CacheObject>;
332
333/**
334 * Returns an object with the cached data, digest, and metadata identified by
335 * `key`. The `data` property of this object will be a Buffer instance that
336 * presumably holds some data that means something to you. I'm sure you know
337 * what to do with it! cacache just won't care.
338 *
339 * `integrity` is a Subresource Integrity string. That is, a string that can be
340 * used to verify `data`, which looks like
341 * `<hash-algorithm>-<base64-integrity-hash>`.
342 *
343 * If there is no content identified by key, or if the locally-stored data does
344 * not pass the validity checksum, the promise will be rejected.
345 *
346 * A sub-function, `get.byDigest` may be used for identical behavior, except
347 * lookup will happen by integrity hash, bypassing the index entirely. This
348 * version of the function only returns data itself, without any wrapper.
349 *
350 * **Note**
351 *
352 * This function loads the entire cache entry into memory before returning it.
353 * If you're dealing with Very Large data, consider using `get.stream` instead.
354 */
355export function get(cachePath: string, key: string, options?: get.Options): Promise<GetCacheObject>;
356
357/**
358 * Lists info for all entries currently in the cache as a single large object.
359 * Each entry in the object will be keyed by the unique index key, with
360 * corresponding `get.info` objects as the values.
361 */
362export function ls(cachePath: string): Promise<ls.Cache>;
363
364/**
365 * Inserts data passed to it into the cache. The returned Promise resolves with
366 * a digest (generated according to `opts.algorithms`) after the cache entry has
367 * been successfully written.
368 */
369export function put(cachePath: string, key: string, data: any, opts?: put.Options): Promise<string>;
370
371/**
372 * Removes the index entry for `key`. Content will still be accessible if
373 * requested directly by content address (`get.stream.byDigest`).
374 *
375 * To remove the content itself (which might still be used by other
376 * entries), use `rm.content`. Or, to safely vacuum any unused content,
377 * use `verify`.
378 */
379export function rm(cachePath: string, key: string): Promise<any>;
380
381/**
382 * Checks out and fixes up your cache:
383 *
384 * - Cleans up corrupted or invalid index entries
385 * - Custom entry filtering options
386 * - Garbage collects any content entries not referenced by the index
387 * - Checks integrity for all content entries and removes invalid content
388 * - Fixes cache ownership
389 * - Removes the `tmp` directory in the cache and all its contents.
390 *
391 * When it's done, it'll return an object with various stats about the
392 * verification process, including amount of storage reclaimed, number of valid
393 * entries, number of entries removed, etc.
394 */
395export function verify(cachePath: string, opts?: verify.Options): Promise<any>;