1 | # cacache [![npm version](https://img.shields.io/npm/v/cacache.svg)](https://npm.im/cacache) [![license](https://img.shields.io/npm/l/cacache.svg)](https://npm.im/cacache) [![Travis](https://img.shields.io/travis/npm/cacache.svg)](https://travis-ci.org/npm/cacache) [![AppVeyor](https://ci.appveyor.com/api/projects/status/github/npm/cacache?svg=true)](https://ci.appveyor.com/project/npm/cacache) [![Coverage Status](https://coveralls.io/repos/github/npm/cacache/badge.svg?branch=latest)](https://coveralls.io/github/npm/cacache?branch=latest)
|
---|
2 |
|
---|
3 | [`cacache`](https://github.com/npm/cacache) is a Node.js library for managing
|
---|
4 | local key and content address caches. It's really fast, really good at
|
---|
5 | concurrency, and it will never give you corrupted data, even if cache files
|
---|
6 | get corrupted or manipulated.
|
---|
7 |
|
---|
8 | On systems that support user and group settings on files, cacache will
|
---|
9 | match the `uid` and `gid` values to the folder where the cache lives, even
|
---|
10 | when running as `root`.
|
---|
11 |
|
---|
12 | It was written to be used as [npm](https://npm.im)'s local cache, but can
|
---|
13 | just as easily be used on its own.
|
---|
14 |
|
---|
15 | ## Install
|
---|
16 |
|
---|
17 | `$ npm install --save cacache`
|
---|
18 |
|
---|
19 | ## Table of Contents
|
---|
20 |
|
---|
21 | * [Example](#example)
|
---|
22 | * [Features](#features)
|
---|
23 | * [Contributing](#contributing)
|
---|
24 | * [API](#api)
|
---|
25 | * [Using localized APIs](#localized-api)
|
---|
26 | * Reading
|
---|
27 | * [`ls`](#ls)
|
---|
28 | * [`ls.stream`](#ls-stream)
|
---|
29 | * [`get`](#get-data)
|
---|
30 | * [`get.stream`](#get-stream)
|
---|
31 | * [`get.info`](#get-info)
|
---|
32 | * [`get.hasContent`](#get-hasContent)
|
---|
33 | * Writing
|
---|
34 | * [`put`](#put-data)
|
---|
35 | * [`put.stream`](#put-stream)
|
---|
36 | * [`rm.all`](#rm-all)
|
---|
37 | * [`rm.entry`](#rm-entry)
|
---|
38 | * [`rm.content`](#rm-content)
|
---|
39 | * [`index.compact`](#index-compact)
|
---|
40 | * [`index.insert`](#index-insert)
|
---|
41 | * Utilities
|
---|
42 | * [`clearMemoized`](#clear-memoized)
|
---|
43 | * [`tmp.mkdir`](#tmp-mkdir)
|
---|
44 | * [`tmp.withTmp`](#with-tmp)
|
---|
45 | * Integrity
|
---|
46 | * [Subresource Integrity](#integrity)
|
---|
47 | * [`verify`](#verify)
|
---|
48 | * [`verify.lastRun`](#verify-last-run)
|
---|
49 |
|
---|
50 | ### Example
|
---|
51 |
|
---|
52 | ```javascript
|
---|
53 | const cacache = require('cacache')
|
---|
54 | const fs = require('fs')
|
---|
55 |
|
---|
56 | const tarball = '/path/to/mytar.tgz'
|
---|
57 | const cachePath = '/tmp/my-toy-cache'
|
---|
58 | const key = 'my-unique-key-1234'
|
---|
59 |
|
---|
60 | // Cache it! Use `cachePath` as the root of the content cache
|
---|
61 | cacache.put(cachePath, key, '10293801983029384').then(integrity => {
|
---|
62 | console.log(`Saved content to ${cachePath}.`)
|
---|
63 | })
|
---|
64 |
|
---|
65 | const destination = '/tmp/mytar.tgz'
|
---|
66 |
|
---|
67 | // Copy the contents out of the cache and into their destination!
|
---|
68 | // But this time, use stream instead!
|
---|
69 | cacache.get.stream(
|
---|
70 | cachePath, key
|
---|
71 | ).pipe(
|
---|
72 | fs.createWriteStream(destination)
|
---|
73 | ).on('finish', () => {
|
---|
74 | console.log('done extracting!')
|
---|
75 | })
|
---|
76 |
|
---|
77 | // The same thing, but skip the key index.
|
---|
78 | cacache.get.byDigest(cachePath, integrityHash).then(data => {
|
---|
79 | fs.writeFile(destination, data, err => {
|
---|
80 | console.log('tarball data fetched based on its sha512sum and written out!')
|
---|
81 | })
|
---|
82 | })
|
---|
83 | ```
|
---|
84 |
|
---|
85 | ### Features
|
---|
86 |
|
---|
87 | * Extraction by key or by content address (shasum, etc)
|
---|
88 | * [Subresource Integrity](#integrity) web standard support
|
---|
89 | * Multi-hash support - safely host sha1, sha512, etc, in a single cache
|
---|
90 | * Automatic content deduplication
|
---|
91 | * Fault tolerance (immune to corruption, partial writes, process races, etc)
|
---|
92 | * Consistency guarantees on read and write (full data verification)
|
---|
93 | * Lockless, high-concurrency cache access
|
---|
94 | * Streaming support
|
---|
95 | * Promise support
|
---|
96 | * Fast -- sub-millisecond reads and writes including verification
|
---|
97 | * Arbitrary metadata storage
|
---|
98 | * Garbage collection and additional offline verification
|
---|
99 | * Thorough test coverage
|
---|
100 | * There's probably a bloom filter in there somewhere. Those are cool, right? π€
|
---|
101 |
|
---|
102 | ### Contributing
|
---|
103 |
|
---|
104 | The cacache team enthusiastically welcomes contributions and project participation! There's a bunch of things you can do if you want to contribute! The [Contributor Guide](CONTRIBUTING.md) has all the information you need for everything from reporting bugs to contributing entire new features. Please don't hesitate to jump in if you'd like to, or even ask us questions if something isn't clear.
|
---|
105 |
|
---|
106 | All participants and maintainers in this project are expected to follow [Code of Conduct](CODE_OF_CONDUCT.md), and just generally be excellent to each other.
|
---|
107 |
|
---|
108 | Please refer to the [Changelog](CHANGELOG.md) for project history details, too.
|
---|
109 |
|
---|
110 | Happy hacking!
|
---|
111 |
|
---|
112 | ### API
|
---|
113 |
|
---|
114 | #### <a name="ls"></a> `> cacache.ls(cache) -> Promise<Object>`
|
---|
115 |
|
---|
116 | Lists info for all entries currently in the cache as a single large object. Each
|
---|
117 | entry in the object will be keyed by the unique index key, with corresponding
|
---|
118 | [`get.info`](#get-info) objects as the values.
|
---|
119 |
|
---|
120 | ##### Example
|
---|
121 |
|
---|
122 | ```javascript
|
---|
123 | cacache.ls(cachePath).then(console.log)
|
---|
124 | // Output
|
---|
125 | {
|
---|
126 | 'my-thing': {
|
---|
127 | key: 'my-thing',
|
---|
128 | integrity: 'sha512-BaSe64/EnCoDED+HAsh=='
|
---|
129 | path: '.testcache/content/deadbeef', // joined with `cachePath`
|
---|
130 | time: 12345698490,
|
---|
131 | size: 4023948,
|
---|
132 | metadata: {
|
---|
133 | name: 'blah',
|
---|
134 | version: '1.2.3',
|
---|
135 | description: 'this was once a package but now it is my-thing'
|
---|
136 | }
|
---|
137 | },
|
---|
138 | 'other-thing': {
|
---|
139 | key: 'other-thing',
|
---|
140 | integrity: 'sha1-ANothER+hasH=',
|
---|
141 | path: '.testcache/content/bada55',
|
---|
142 | time: 11992309289,
|
---|
143 | size: 111112
|
---|
144 | }
|
---|
145 | }
|
---|
146 | ```
|
---|
147 |
|
---|
148 | #### <a name="ls-stream"></a> `> cacache.ls.stream(cache) -> Readable`
|
---|
149 |
|
---|
150 | Lists info for all entries currently in the cache as a single large object.
|
---|
151 |
|
---|
152 | This works just like [`ls`](#ls), except [`get.info`](#get-info) entries are
|
---|
153 | returned as `'data'` events on the returned stream.
|
---|
154 |
|
---|
155 | ##### Example
|
---|
156 |
|
---|
157 | ```javascript
|
---|
158 | cacache.ls.stream(cachePath).on('data', console.log)
|
---|
159 | // Output
|
---|
160 | {
|
---|
161 | key: 'my-thing',
|
---|
162 | integrity: 'sha512-BaSe64HaSh',
|
---|
163 | path: '.testcache/content/deadbeef', // joined with `cachePath`
|
---|
164 | time: 12345698490,
|
---|
165 | size: 13423,
|
---|
166 | metadata: {
|
---|
167 | name: 'blah',
|
---|
168 | version: '1.2.3',
|
---|
169 | description: 'this was once a package but now it is my-thing'
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | {
|
---|
174 | key: 'other-thing',
|
---|
175 | integrity: 'whirlpool-WoWSoMuchSupport',
|
---|
176 | path: '.testcache/content/bada55',
|
---|
177 | time: 11992309289,
|
---|
178 | size: 498023984029
|
---|
179 | }
|
---|
180 |
|
---|
181 | {
|
---|
182 | ...
|
---|
183 | }
|
---|
184 | ```
|
---|
185 |
|
---|
186 | #### <a name="get-data"></a> `> cacache.get(cache, key, [opts]) -> Promise({data, metadata, integrity})`
|
---|
187 |
|
---|
188 | Returns an object with the cached data, digest, and metadata identified by
|
---|
189 | `key`. The `data` property of this object will be a `Buffer` instance that
|
---|
190 | presumably holds some data that means something to you. I'm sure you know what
|
---|
191 | to do with it! cacache just won't care.
|
---|
192 |
|
---|
193 | `integrity` is a [Subresource
|
---|
194 | Integrity](#integrity)
|
---|
195 | string. That is, a string that can be used to verify `data`, which looks like
|
---|
196 | `<hash-algorithm>-<base64-integrity-hash>`.
|
---|
197 |
|
---|
198 | If there is no content identified by `key`, or if the locally-stored data does
|
---|
199 | not pass the validity checksum, the promise will be rejected.
|
---|
200 |
|
---|
201 | A sub-function, `get.byDigest` may be used for identical behavior, except lookup
|
---|
202 | will happen by integrity hash, bypassing the index entirely. This version of the
|
---|
203 | function *only* returns `data` itself, without any wrapper.
|
---|
204 |
|
---|
205 | See: [options](#get-options)
|
---|
206 |
|
---|
207 | ##### Note
|
---|
208 |
|
---|
209 | This function loads the entire cache entry into memory before returning it. If
|
---|
210 | you're dealing with Very Large data, consider using [`get.stream`](#get-stream)
|
---|
211 | instead.
|
---|
212 |
|
---|
213 | ##### Example
|
---|
214 |
|
---|
215 | ```javascript
|
---|
216 | // Look up by key
|
---|
217 | cache.get(cachePath, 'my-thing').then(console.log)
|
---|
218 | // Output:
|
---|
219 | {
|
---|
220 | metadata: {
|
---|
221 | thingName: 'my'
|
---|
222 | },
|
---|
223 | integrity: 'sha512-BaSe64HaSh',
|
---|
224 | data: Buffer#<deadbeef>,
|
---|
225 | size: 9320
|
---|
226 | }
|
---|
227 |
|
---|
228 | // Look up by digest
|
---|
229 | cache.get.byDigest(cachePath, 'sha512-BaSe64HaSh').then(console.log)
|
---|
230 | // Output:
|
---|
231 | Buffer#<deadbeef>
|
---|
232 | ```
|
---|
233 |
|
---|
234 | #### <a name="get-stream"></a> `> cacache.get.stream(cache, key, [opts]) -> Readable`
|
---|
235 |
|
---|
236 | Returns a [Readable Stream](https://nodejs.org/api/stream.html#stream_readable_streams) of the cached data identified by `key`.
|
---|
237 |
|
---|
238 | If there is no content identified by `key`, or if the locally-stored data does
|
---|
239 | not pass the validity checksum, an error will be emitted.
|
---|
240 |
|
---|
241 | `metadata` and `integrity` events will be emitted before the stream closes, if
|
---|
242 | you need to collect that extra data about the cached entry.
|
---|
243 |
|
---|
244 | A sub-function, `get.stream.byDigest` may be used for identical behavior,
|
---|
245 | except lookup will happen by integrity hash, bypassing the index entirely. This
|
---|
246 | version does not emit the `metadata` and `integrity` events at all.
|
---|
247 |
|
---|
248 | See: [options](#get-options)
|
---|
249 |
|
---|
250 | ##### Example
|
---|
251 |
|
---|
252 | ```javascript
|
---|
253 | // Look up by key
|
---|
254 | cache.get.stream(
|
---|
255 | cachePath, 'my-thing'
|
---|
256 | ).on('metadata', metadata => {
|
---|
257 | console.log('metadata:', metadata)
|
---|
258 | }).on('integrity', integrity => {
|
---|
259 | console.log('integrity:', integrity)
|
---|
260 | }).pipe(
|
---|
261 | fs.createWriteStream('./x.tgz')
|
---|
262 | )
|
---|
263 | // Outputs:
|
---|
264 | metadata: { ... }
|
---|
265 | integrity: 'sha512-SoMeDIGest+64=='
|
---|
266 |
|
---|
267 | // Look up by digest
|
---|
268 | cache.get.stream.byDigest(
|
---|
269 | cachePath, 'sha512-SoMeDIGest+64=='
|
---|
270 | ).pipe(
|
---|
271 | fs.createWriteStream('./x.tgz')
|
---|
272 | )
|
---|
273 | ```
|
---|
274 |
|
---|
275 | #### <a name="get-info"></a> `> cacache.get.info(cache, key) -> Promise`
|
---|
276 |
|
---|
277 | Looks up `key` in the cache index, returning information about the entry if
|
---|
278 | one exists.
|
---|
279 |
|
---|
280 | ##### Fields
|
---|
281 |
|
---|
282 | * `key` - Key the entry was looked up under. Matches the `key` argument.
|
---|
283 | * `integrity` - [Subresource Integrity hash](#integrity) for the content this entry refers to.
|
---|
284 | * `path` - Filesystem path where content is stored, joined with `cache` argument.
|
---|
285 | * `time` - Timestamp the entry was first added on.
|
---|
286 | * `metadata` - User-assigned metadata associated with the entry/content.
|
---|
287 |
|
---|
288 | ##### Example
|
---|
289 |
|
---|
290 | ```javascript
|
---|
291 | cacache.get.info(cachePath, 'my-thing').then(console.log)
|
---|
292 |
|
---|
293 | // Output
|
---|
294 | {
|
---|
295 | key: 'my-thing',
|
---|
296 | integrity: 'sha256-MUSTVERIFY+ALL/THINGS=='
|
---|
297 | path: '.testcache/content/deadbeef',
|
---|
298 | time: 12345698490,
|
---|
299 | size: 849234,
|
---|
300 | metadata: {
|
---|
301 | name: 'blah',
|
---|
302 | version: '1.2.3',
|
---|
303 | description: 'this was once a package but now it is my-thing'
|
---|
304 | }
|
---|
305 | }
|
---|
306 | ```
|
---|
307 |
|
---|
308 | #### <a name="get-hasContent"></a> `> cacache.get.hasContent(cache, integrity) -> Promise`
|
---|
309 |
|
---|
310 | Looks up a [Subresource Integrity hash](#integrity) in the cache. If content
|
---|
311 | exists for this `integrity`, it will return an object, with the specific single integrity hash
|
---|
312 | that was found in `sri` key, and the size of the found content as `size`. If no content exists for this integrity, it will return `false`.
|
---|
313 |
|
---|
314 | ##### Example
|
---|
315 |
|
---|
316 | ```javascript
|
---|
317 | cacache.get.hasContent(cachePath, 'sha256-MUSTVERIFY+ALL/THINGS==').then(console.log)
|
---|
318 |
|
---|
319 | // Output
|
---|
320 | {
|
---|
321 | sri: {
|
---|
322 | source: 'sha256-MUSTVERIFY+ALL/THINGS==',
|
---|
323 | algorithm: 'sha256',
|
---|
324 | digest: 'MUSTVERIFY+ALL/THINGS==',
|
---|
325 | options: []
|
---|
326 | },
|
---|
327 | size: 9001
|
---|
328 | }
|
---|
329 |
|
---|
330 | cacache.get.hasContent(cachePath, 'sha521-NOT+IN/CACHE==').then(console.log)
|
---|
331 |
|
---|
332 | // Output
|
---|
333 | false
|
---|
334 | ```
|
---|
335 |
|
---|
336 | ##### <a name="get-options"></a> Options
|
---|
337 |
|
---|
338 | ##### `opts.integrity`
|
---|
339 | If present, the pre-calculated digest for the inserted content. If this option
|
---|
340 | is provided and does not match the post-insertion digest, insertion will fail
|
---|
341 | with an `EINTEGRITY` error.
|
---|
342 |
|
---|
343 | ##### `opts.memoize`
|
---|
344 |
|
---|
345 | Default: null
|
---|
346 |
|
---|
347 | If explicitly truthy, cacache will read from memory and memoize data on bulk read. If `false`, cacache will read from disk data. Reader functions by default read from in-memory cache.
|
---|
348 |
|
---|
349 | ##### `opts.size`
|
---|
350 | If provided, the data stream will be verified to check that enough data was
|
---|
351 | passed through. If there's more or less data than expected, insertion will fail
|
---|
352 | with an `EBADSIZE` error.
|
---|
353 |
|
---|
354 |
|
---|
355 | #### <a name="put-data"></a> `> cacache.put(cache, key, data, [opts]) -> Promise`
|
---|
356 |
|
---|
357 | Inserts data passed to it into the cache. The returned Promise resolves with a
|
---|
358 | digest (generated according to [`opts.algorithms`](#optsalgorithms)) after the
|
---|
359 | cache entry has been successfully written.
|
---|
360 |
|
---|
361 | See: [options](#put-options)
|
---|
362 |
|
---|
363 | ##### Example
|
---|
364 |
|
---|
365 | ```javascript
|
---|
366 | fetch(
|
---|
367 | 'https://registry.npmjs.org/cacache/-/cacache-1.0.0.tgz'
|
---|
368 | ).then(data => {
|
---|
369 | return cacache.put(cachePath, 'registry.npmjs.org|cacache@1.0.0', data)
|
---|
370 | }).then(integrity => {
|
---|
371 | console.log('integrity hash is', integrity)
|
---|
372 | })
|
---|
373 | ```
|
---|
374 |
|
---|
375 | #### <a name="put-stream"></a> `> cacache.put.stream(cache, key, [opts]) -> Writable`
|
---|
376 |
|
---|
377 | Returns a [Writable
|
---|
378 | Stream](https://nodejs.org/api/stream.html#stream_writable_streams) that inserts
|
---|
379 | data written to it into the cache. Emits an `integrity` event with the digest of
|
---|
380 | written contents when it succeeds.
|
---|
381 |
|
---|
382 | See: [options](#put-options)
|
---|
383 |
|
---|
384 | ##### Example
|
---|
385 |
|
---|
386 | ```javascript
|
---|
387 | request.get(
|
---|
388 | 'https://registry.npmjs.org/cacache/-/cacache-1.0.0.tgz'
|
---|
389 | ).pipe(
|
---|
390 | cacache.put.stream(
|
---|
391 | cachePath, 'registry.npmjs.org|cacache@1.0.0'
|
---|
392 | ).on('integrity', d => console.log(`integrity digest is ${d}`))
|
---|
393 | )
|
---|
394 | ```
|
---|
395 |
|
---|
396 | ##### <a name="put-options"></a> Options
|
---|
397 |
|
---|
398 | ##### `opts.metadata`
|
---|
399 |
|
---|
400 | Arbitrary metadata to be attached to the inserted key.
|
---|
401 |
|
---|
402 | ##### `opts.size`
|
---|
403 |
|
---|
404 | If provided, the data stream will be verified to check that enough data was
|
---|
405 | passed through. If there's more or less data than expected, insertion will fail
|
---|
406 | with an `EBADSIZE` error.
|
---|
407 |
|
---|
408 | ##### `opts.integrity`
|
---|
409 |
|
---|
410 | If present, the pre-calculated digest for the inserted content. If this option
|
---|
411 | is provided and does not match the post-insertion digest, insertion will fail
|
---|
412 | with an `EINTEGRITY` error.
|
---|
413 |
|
---|
414 | `algorithms` has no effect if this option is present.
|
---|
415 |
|
---|
416 | ##### `opts.algorithms`
|
---|
417 |
|
---|
418 | Default: ['sha512']
|
---|
419 |
|
---|
420 | Hashing algorithms to use when calculating the [subresource integrity
|
---|
421 | digest](#integrity)
|
---|
422 | for inserted data. Can use any algorithm listed in `crypto.getHashes()` or
|
---|
423 | `'omakase'`/`'γδ»»γγγΎγ'` to pick a random hash algorithm on each insertion. You
|
---|
424 | may also use any anagram of `'modnar'` to use this feature.
|
---|
425 |
|
---|
426 | Currently only supports one algorithm at a time (i.e., an array length of
|
---|
427 | exactly `1`). Has no effect if `opts.integrity` is present.
|
---|
428 |
|
---|
429 | ##### `opts.memoize`
|
---|
430 |
|
---|
431 | Default: null
|
---|
432 |
|
---|
433 | If provided, cacache will memoize the given cache insertion in memory, bypassing
|
---|
434 | any filesystem checks for that key or digest in future cache fetches. Nothing
|
---|
435 | will be written to the in-memory cache unless this option is explicitly truthy.
|
---|
436 |
|
---|
437 | If `opts.memoize` is an object or a `Map`-like (that is, an object with `get`
|
---|
438 | and `set` methods), it will be written to instead of the global memoization
|
---|
439 | cache.
|
---|
440 |
|
---|
441 | Reading from disk data can be forced by explicitly passing `memoize: false` to
|
---|
442 | the reader functions, but their default will be to read from memory.
|
---|
443 |
|
---|
444 | ##### `opts.tmpPrefix`
|
---|
445 | Default: null
|
---|
446 |
|
---|
447 | Prefix to append on the temporary directory name inside the cache's tmp dir.
|
---|
448 |
|
---|
449 | #### <a name="rm-all"></a> `> cacache.rm.all(cache) -> Promise`
|
---|
450 |
|
---|
451 | Clears the entire cache. Mainly by blowing away the cache directory itself.
|
---|
452 |
|
---|
453 | ##### Example
|
---|
454 |
|
---|
455 | ```javascript
|
---|
456 | cacache.rm.all(cachePath).then(() => {
|
---|
457 | console.log('THE APOCALYPSE IS UPON US π±')
|
---|
458 | })
|
---|
459 | ```
|
---|
460 |
|
---|
461 | #### <a name="rm-entry"></a> `> cacache.rm.entry(cache, key, [opts]) -> Promise`
|
---|
462 |
|
---|
463 | Alias: `cacache.rm`
|
---|
464 |
|
---|
465 | Removes the index entry for `key`. Content will still be accessible if
|
---|
466 | requested directly by content address ([`get.stream.byDigest`](#get-stream)).
|
---|
467 |
|
---|
468 | By default, this appends a new entry to the index with an integrity of `null`.
|
---|
469 | If `opts.removeFully` is set to `true` then the index file itself will be
|
---|
470 | physically deleted rather than appending a `null`.
|
---|
471 |
|
---|
472 | To remove the content itself (which might still be used by other entries), use
|
---|
473 | [`rm.content`](#rm-content). Or, to safely vacuum any unused content, use
|
---|
474 | [`verify`](#verify).
|
---|
475 |
|
---|
476 | ##### Example
|
---|
477 |
|
---|
478 | ```javascript
|
---|
479 | cacache.rm.entry(cachePath, 'my-thing').then(() => {
|
---|
480 | console.log('I did not like it anyway')
|
---|
481 | })
|
---|
482 | ```
|
---|
483 |
|
---|
484 | #### <a name="rm-content"></a> `> cacache.rm.content(cache, integrity) -> Promise`
|
---|
485 |
|
---|
486 | Removes the content identified by `integrity`. Any index entries referring to it
|
---|
487 | will not be usable again until the content is re-added to the cache with an
|
---|
488 | identical digest.
|
---|
489 |
|
---|
490 | ##### Example
|
---|
491 |
|
---|
492 | ```javascript
|
---|
493 | cacache.rm.content(cachePath, 'sha512-SoMeDIGest/IN+BaSE64==').then(() => {
|
---|
494 | console.log('data for my-thing is gone!')
|
---|
495 | })
|
---|
496 | ```
|
---|
497 |
|
---|
498 | #### <a name="index-compact"></a> `> cacache.index.compact(cache, key, matchFn, [opts]) -> Promise`
|
---|
499 |
|
---|
500 | Uses `matchFn`, which must be a synchronous function that accepts two entries
|
---|
501 | and returns a boolean indicating whether or not the two entries match, to
|
---|
502 | deduplicate all entries in the cache for the given `key`.
|
---|
503 |
|
---|
504 | If `opts.validateEntry` is provided, it will be called as a function with the
|
---|
505 | only parameter being a single index entry. The function must return a Boolean,
|
---|
506 | if it returns `true` the entry is considered valid and will be kept in the index,
|
---|
507 | if it returns `false` the entry will be removed from the index.
|
---|
508 |
|
---|
509 | If `opts.validateEntry` is not provided, however, every entry in the index will
|
---|
510 | be deduplicated and kept until the first `null` integrity is reached, removing
|
---|
511 | all entries that were written before the `null`.
|
---|
512 |
|
---|
513 | The deduplicated list of entries is both written to the index, replacing the
|
---|
514 | existing content, and returned in the Promise.
|
---|
515 |
|
---|
516 | #### <a name="index-insert"></a> `> cacache.index.insert(cache, key, integrity, opts) -> Promise`
|
---|
517 |
|
---|
518 | Writes an index entry to the cache for the given `key` without writing content.
|
---|
519 |
|
---|
520 | It is assumed if you are using this method, you have already stored the content
|
---|
521 | some other way and you only wish to add a new index to that content. The `metadata`
|
---|
522 | and `size` properties are read from `opts` and used as part of the index entry.
|
---|
523 |
|
---|
524 | Returns a Promise resolving to the newly added entry.
|
---|
525 |
|
---|
526 | #### <a name="clear-memoized"></a> `> cacache.clearMemoized()`
|
---|
527 |
|
---|
528 | Completely resets the in-memory entry cache.
|
---|
529 |
|
---|
530 | #### <a name="tmp-mkdir"></a> `> tmp.mkdir(cache, opts) -> Promise<Path>`
|
---|
531 |
|
---|
532 | Returns a unique temporary directory inside the cache's `tmp` dir. This
|
---|
533 | directory will use the same safe user assignment that all the other stuff use.
|
---|
534 |
|
---|
535 | Once the directory is made, it's the user's responsibility that all files
|
---|
536 | within are given the appropriate `gid`/`uid` ownership settings to match
|
---|
537 | the rest of the cache. If not, you can ask cacache to do it for you by
|
---|
538 | calling [`tmp.fix()`](#tmp-fix), which will fix all tmp directory
|
---|
539 | permissions.
|
---|
540 |
|
---|
541 | If you want automatic cleanup of this directory, use
|
---|
542 | [`tmp.withTmp()`](#with-tpm)
|
---|
543 |
|
---|
544 | See: [options](#tmp-options)
|
---|
545 |
|
---|
546 | ##### Example
|
---|
547 |
|
---|
548 | ```javascript
|
---|
549 | cacache.tmp.mkdir(cache).then(dir => {
|
---|
550 | fs.writeFile(path.join(dir, 'blablabla'), Buffer#<1234>, ...)
|
---|
551 | })
|
---|
552 | ```
|
---|
553 |
|
---|
554 | #### <a name="tmp-fix"></a> `> tmp.fix(cache) -> Promise`
|
---|
555 |
|
---|
556 | Sets the `uid` and `gid` properties on all files and folders within the tmp
|
---|
557 | folder to match the rest of the cache.
|
---|
558 |
|
---|
559 | Use this after manually writing files into [`tmp.mkdir`](#tmp-mkdir) or
|
---|
560 | [`tmp.withTmp`](#with-tmp).
|
---|
561 |
|
---|
562 | ##### Example
|
---|
563 |
|
---|
564 | ```javascript
|
---|
565 | cacache.tmp.mkdir(cache).then(dir => {
|
---|
566 | writeFile(path.join(dir, 'file'), someData).then(() => {
|
---|
567 | // make sure we didn't just put a root-owned file in the cache
|
---|
568 | cacache.tmp.fix().then(() => {
|
---|
569 | // all uids and gids match now
|
---|
570 | })
|
---|
571 | })
|
---|
572 | })
|
---|
573 | ```
|
---|
574 |
|
---|
575 | #### <a name="with-tmp"></a> `> tmp.withTmp(cache, opts, cb) -> Promise`
|
---|
576 |
|
---|
577 | Creates a temporary directory with [`tmp.mkdir()`](#tmp-mkdir) and calls `cb`
|
---|
578 | with it. The created temporary directory will be removed when the return value
|
---|
579 | of `cb()` resolves, the tmp directory will be automatically deleted once that
|
---|
580 | promise completes.
|
---|
581 |
|
---|
582 | The same caveats apply when it comes to managing permissions for the tmp dir's
|
---|
583 | contents.
|
---|
584 |
|
---|
585 | See: [options](#tmp-options)
|
---|
586 |
|
---|
587 | ##### Example
|
---|
588 |
|
---|
589 | ```javascript
|
---|
590 | cacache.tmp.withTmp(cache, dir => {
|
---|
591 | return fs.writeFileAsync(path.join(dir, 'blablabla'), Buffer#<1234>, ...)
|
---|
592 | }).then(() => {
|
---|
593 | // `dir` no longer exists
|
---|
594 | })
|
---|
595 | ```
|
---|
596 |
|
---|
597 | ##### <a name="tmp-options"></a> Options
|
---|
598 |
|
---|
599 | ##### `opts.tmpPrefix`
|
---|
600 | Default: null
|
---|
601 |
|
---|
602 | Prefix to append on the temporary directory name inside the cache's tmp dir.
|
---|
603 |
|
---|
604 | #### <a name="integrity"></a> Subresource Integrity Digests
|
---|
605 |
|
---|
606 | For content verification and addressing, cacache uses strings following the
|
---|
607 | [Subresource
|
---|
608 | Integrity spec](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity).
|
---|
609 | That is, any time cacache expects an `integrity` argument or option, it
|
---|
610 | should be in the format `<hashAlgorithm>-<base64-hash>`.
|
---|
611 |
|
---|
612 | One deviation from the current spec is that cacache will support any hash
|
---|
613 | algorithms supported by the underlying Node.js process. You can use
|
---|
614 | `crypto.getHashes()` to see which ones you can use.
|
---|
615 |
|
---|
616 | ##### Generating Digests Yourself
|
---|
617 |
|
---|
618 | If you have an existing content shasum, they are generally formatted as a
|
---|
619 | hexadecimal string (that is, a sha1 would look like:
|
---|
620 | `5f5513f8822fdbe5145af33b64d8d970dcf95c6e`). In order to be compatible with
|
---|
621 | cacache, you'll need to convert this to an equivalent subresource integrity
|
---|
622 | string. For this example, the corresponding hash would be:
|
---|
623 | `sha1-X1UT+IIv2+UUWvM7ZNjZcNz5XG4=`.
|
---|
624 |
|
---|
625 | If you want to generate an integrity string yourself for existing data, you can
|
---|
626 | use something like this:
|
---|
627 |
|
---|
628 | ```javascript
|
---|
629 | const crypto = require('crypto')
|
---|
630 | const hashAlgorithm = 'sha512'
|
---|
631 | const data = 'foobarbaz'
|
---|
632 |
|
---|
633 | const integrity = (
|
---|
634 | hashAlgorithm +
|
---|
635 | '-' +
|
---|
636 | crypto.createHash(hashAlgorithm).update(data).digest('base64')
|
---|
637 | )
|
---|
638 | ```
|
---|
639 |
|
---|
640 | You can also use [`ssri`](https://npm.im/ssri) to have a richer set of functionality
|
---|
641 | around SRI strings, including generation, parsing, and translating from existing
|
---|
642 | hex-formatted strings.
|
---|
643 |
|
---|
644 | #### <a name="verify"></a> `> cacache.verify(cache, opts) -> Promise`
|
---|
645 |
|
---|
646 | Checks out and fixes up your cache:
|
---|
647 |
|
---|
648 | * Cleans up corrupted or invalid index entries.
|
---|
649 | * Custom entry filtering options.
|
---|
650 | * Garbage collects any content entries not referenced by the index.
|
---|
651 | * Checks integrity for all content entries and removes invalid content.
|
---|
652 | * Fixes cache ownership.
|
---|
653 | * Removes the `tmp` directory in the cache and all its contents.
|
---|
654 |
|
---|
655 | When it's done, it'll return an object with various stats about the verification
|
---|
656 | process, including amount of storage reclaimed, number of valid entries, number
|
---|
657 | of entries removed, etc.
|
---|
658 |
|
---|
659 | ##### <a name="verify-options"></a> Options
|
---|
660 |
|
---|
661 | ##### `opts.concurrency`
|
---|
662 |
|
---|
663 | Default: 20
|
---|
664 |
|
---|
665 | Number of concurrently read files in the filesystem while doing clean up.
|
---|
666 |
|
---|
667 | ##### `opts.filter`
|
---|
668 | Receives a formatted entry. Return false to remove it.
|
---|
669 | Note: might be called more than once on the same entry.
|
---|
670 |
|
---|
671 | ##### `opts.log`
|
---|
672 | Custom logger function:
|
---|
673 | ```
|
---|
674 | log: { silly () {} }
|
---|
675 | log.silly('verify', 'verifying cache at', cache)
|
---|
676 | ```
|
---|
677 |
|
---|
678 | ##### Example
|
---|
679 |
|
---|
680 | ```sh
|
---|
681 | echo somegarbage >> $CACHEPATH/content/deadbeef
|
---|
682 | ```
|
---|
683 |
|
---|
684 | ```javascript
|
---|
685 | cacache.verify(cachePath).then(stats => {
|
---|
686 | // deadbeef collected, because of invalid checksum.
|
---|
687 | console.log('cache is much nicer now! stats:', stats)
|
---|
688 | })
|
---|
689 | ```
|
---|
690 |
|
---|
691 | #### <a name="verify-last-run"></a> `> cacache.verify.lastRun(cache) -> Promise`
|
---|
692 |
|
---|
693 | Returns a `Date` representing the last time `cacache.verify` was run on `cache`.
|
---|
694 |
|
---|
695 | ##### Example
|
---|
696 |
|
---|
697 | ```javascript
|
---|
698 | cacache.verify(cachePath).then(() => {
|
---|
699 | cacache.verify.lastRun(cachePath).then(lastTime => {
|
---|
700 | console.log('cacache.verify was last called on' + lastTime)
|
---|
701 | })
|
---|
702 | })
|
---|
703 | ```
|
---|