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