@vltpkg/cache 1.1.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -26,6 +26,9 @@ const cache = new Cache({
26
26
  // on-disk folder will just keep everything
27
27
  // see @vltpkg/cache-manager for handling that
28
28
  max: 10_000,
29
+ // optional global store root: deleting an entry from disk with its
30
+ // integrity also removes <store>/<integrity-hex>{,.json,.copied}
31
+ store: '/path/to/store/v1',
29
32
  })
30
33
 
31
34
  // reading is always async, because it has to go to disk maybe
package/dist/index.d.ts CHANGED
@@ -15,6 +15,12 @@ export type CacheOptions = {
15
15
  * folder where items should be stored to disk
16
16
  */
17
17
  path: string;
18
+ /**
19
+ * global store root. Deleting an entry from disk with its integrity
20
+ * also removes the store entry, sidecar index and copied marker for
21
+ * that integrity.
22
+ */
23
+ store?: string;
18
24
  /**
19
25
  * called whenever an item is written to disk.
20
26
  */
@@ -31,6 +37,7 @@ export declare class Cache extends LRUCache<string, Buffer, CacheFetchContext> {
31
37
  [Symbol.toStringTag]: string;
32
38
  onDiskWrite?: CacheOptions['onDiskWrite'];
33
39
  onDiskDelete?: CacheOptions['onDiskDelete'];
40
+ store?: string;
34
41
  /**
35
42
  * A list of the actions currently happening in the background
36
43
  */
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { error } from '@vltpkg/error-cause';
2
+ import { integrityHex, integrityRE } from '@vltpkg/types';
2
3
  import { createHash, randomBytes } from 'node:crypto';
3
4
  import { opendirSync, readFileSync } from 'node:fs';
4
5
  import { link, mkdir, opendir, readFile, rename, stat, writeFile, } from 'node:fs/promises';
@@ -16,6 +17,7 @@ export class Cache extends LRUCache {
16
17
  #pending = new Set();
17
18
  onDiskWrite;
18
19
  onDiskDelete;
20
+ store;
19
21
  /**
20
22
  * A list of the actions currently happening in the background
21
23
  */
@@ -30,7 +32,7 @@ export class Cache extends LRUCache {
30
32
  return 10_000;
31
33
  }
32
34
  constructor(options) {
33
- const { onDiskWrite, onDiskDelete, path, fetchMethod: _, sizeCalculation = options.maxSize || options.maxEntrySize ?
35
+ const { onDiskWrite, onDiskDelete, path, store, fetchMethod: _, sizeCalculation = options.maxSize || options.maxEntrySize ?
34
36
  (v, k) => v.length + k.length
35
37
  : undefined, ...lruOpts } = options;
36
38
  super({
@@ -53,6 +55,7 @@ export class Cache extends LRUCache {
53
55
  });
54
56
  this.onDiskWrite = onDiskWrite;
55
57
  this.onDiskDelete = onDiskDelete;
58
+ this.store = store;
56
59
  this.#path = path;
57
60
  }
58
61
  /**
@@ -171,16 +174,14 @@ export class Cache extends LRUCache {
171
174
  integrityPath(integrity) {
172
175
  if (!integrity)
173
176
  return undefined;
174
- const m = /^sha512-([a-zA-Z0-9/+]{86}==)$/.exec(integrity);
175
- const hash = m?.[1];
176
- if (!hash) {
177
+ const hex = integrityHex(integrity);
178
+ if (!hex) {
177
179
  throw error('invalid integrity value', {
178
180
  found: integrity,
179
- wanted: /^sha512-([a-zA-Z0-9/+]{86}==)$/,
181
+ wanted: integrityRE,
180
182
  });
181
183
  }
182
- const base = Buffer.from(hash, 'base64').toString('hex');
183
- return resolve(this.#path, base);
184
+ return resolve(this.#path, hex);
184
185
  }
185
186
  /**
186
187
  * Read synchronously from the fs cache storage if not already
@@ -212,14 +213,23 @@ export class Cache extends LRUCache {
212
213
  }
213
214
  /* c8 ignore stop */
214
215
  /**
215
- * Delete path and path + '.key'
216
+ * Delete path and path + '.key', and with an integrity, its
217
+ * integrity link and global store entry
216
218
  */
217
219
  async #diskDelete(path, integrity) {
218
- const intPath = this.#maybeIntegrityPath(integrity);
219
220
  const paths = [path, path + '.key'];
220
- if (intPath)
221
- paths.push(intPath);
222
- return await rimraf(paths);
221
+ const hex = integrityHex(integrity);
222
+ if (hex)
223
+ paths.push(resolve(this.#path, hex));
224
+ const deleted = await rimraf(paths);
225
+ if (hex && this.store) {
226
+ // dir first: a sidecar without its dir is a plain store miss,
227
+ // a dir without its sidecar is never re-exploded
228
+ const entry = resolve(this.store, hex);
229
+ await rimraf(entry);
230
+ await rimraf([entry + '.json', entry + '.copied']);
231
+ }
232
+ return deleted;
223
233
  }
224
234
  #maybeIntegrityPath(i) {
225
235
  try {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vltpkg/cache",
3
3
  "description": "The filesystem cache for `@vlt/registry-client`",
4
- "version": "1.1.1",
4
+ "version": "1.3.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",
@@ -13,8 +13,8 @@
13
13
  "url": "http://vlt.sh"
14
14
  },
15
15
  "dependencies": {
16
- "@vltpkg/error-cause": "1.1.1",
17
- "@vltpkg/types": "1.1.1",
16
+ "@vltpkg/error-cause": "1.3.0",
17
+ "@vltpkg/types": "1.3.0",
18
18
  "lru-cache": "^11.2.4",
19
19
  "rimraf": "^6.1.2"
20
20
  },
@@ -23,7 +23,7 @@
23
23
  "@types/node": "^22.19.2",
24
24
  "eslint": "^9.39.1",
25
25
  "prettier": "^3.7.4",
26
- "tap": "^21.5.0",
26
+ "tap": "^21.8.0",
27
27
  "typedoc": "~0.27.9",
28
28
  "typescript": "5.7.3",
29
29
  "typescript-eslint": "^8.49.0"