path.js 737 B

1234567891011121314151617181920212223242526272829
  1. 'use strict'
  2. const contentVer = require('../../package.json')['cache-version'].content
  3. const hashToSegments = require('../util/hash-to-segments')
  4. const path = require('path')
  5. const ssri = require('ssri')
  6. // Current format of content file path:
  7. //
  8. // sha512-BaSE64Hex= ->
  9. // ~/.my-cache/content-v2/sha512/ba/da/55deadbeefc0ffee
  10. //
  11. module.exports = contentPath
  12. function contentPath (cache, integrity) {
  13. const sri = ssri.parse(integrity, { single: true })
  14. // contentPath is the *strongest* algo given
  15. return path.join(
  16. contentDir(cache),
  17. sri.algorithm,
  18. ...hashToSegments(sri.hexDigest())
  19. )
  20. }
  21. module.exports.contentDir = contentDir
  22. function contentDir (cache) {
  23. return path.join(cache, `content-v${contentVer}`)
  24. }