cache.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. const cacache = require('cacache')
  2. const { promisify } = require('util')
  3. const log = require('npmlog')
  4. const pacote = require('pacote')
  5. const path = require('path')
  6. const rimraf = promisify(require('rimraf'))
  7. const semver = require('semver')
  8. const BaseCommand = require('./base-command.js')
  9. const npa = require('npm-package-arg')
  10. const jsonParse = require('json-parse-even-better-errors')
  11. const localeCompare = require('@isaacs/string-locale-compare')('en')
  12. const searchCachePackage = async (path, spec, cacheKeys) => {
  13. const parsed = npa(spec)
  14. if (parsed.rawSpec !== '' && parsed.type === 'tag')
  15. throw new Error(`Cannot list cache keys for a tagged package.`)
  16. const searchMFH = new RegExp(`^make-fetch-happen:request-cache:.*(?<!/[@a-zA-Z]+)/${parsed.name}/-/(${parsed.name}[^/]+.tgz)$`)
  17. const searchPack = new RegExp(`^make-fetch-happen:request-cache:.*/${parsed.escapedName}$`)
  18. const results = new Set()
  19. cacheKeys = new Set(cacheKeys)
  20. for (const key of cacheKeys) {
  21. // match on the public key registry url format
  22. if (searchMFH.test(key)) {
  23. // extract the version from the filename
  24. const filename = key.match(searchMFH)[1]
  25. const noExt = filename.slice(0, -4)
  26. const noScope = `${parsed.name.split('/').pop()}-`
  27. const ver = noExt.slice(noScope.length)
  28. if (semver.satisfies(ver, parsed.rawSpec))
  29. results.add(key)
  30. continue
  31. }
  32. // is this key a packument?
  33. if (!searchPack.test(key))
  34. continue
  35. results.add(key)
  36. let packument, details
  37. try {
  38. details = await cacache.get(path, key)
  39. packument = jsonParse(details.data)
  40. } catch (_) {
  41. // if we couldn't parse the packument, abort
  42. continue
  43. }
  44. if (!packument.versions || typeof packument.versions !== 'object')
  45. continue
  46. // assuming this is a packument
  47. for (const ver of Object.keys(packument.versions)) {
  48. if (semver.satisfies(ver, parsed.rawSpec)) {
  49. if (packument.versions[ver].dist
  50. && typeof packument.versions[ver].dist === 'object'
  51. && packument.versions[ver].dist.tarball !== undefined
  52. && cacheKeys.has(`make-fetch-happen:request-cache:${packument.versions[ver].dist.tarball}`))
  53. results.add(`make-fetch-happen:request-cache:${packument.versions[ver].dist.tarball}`)
  54. }
  55. }
  56. }
  57. return results
  58. }
  59. class Cache extends BaseCommand {
  60. static get description () {
  61. return 'Manipulates packages cache'
  62. }
  63. /* istanbul ignore next - see test/lib/load-all-commands.js */
  64. static get name () {
  65. return 'cache'
  66. }
  67. /* istanbul ignore next - see test/lib/load-all-commands.js */
  68. static get params () {
  69. return ['cache']
  70. }
  71. /* istanbul ignore next - see test/lib/load-all-commands.js */
  72. static get usage () {
  73. return [
  74. 'add <tarball file>',
  75. 'add <folder>',
  76. 'add <tarball url>',
  77. 'add <git url>',
  78. 'add <name>@<version>',
  79. 'clean [<key>]',
  80. 'ls [<name>@<version>]',
  81. 'verify',
  82. ]
  83. }
  84. async completion (opts) {
  85. const argv = opts.conf.argv.remain
  86. if (argv.length === 2)
  87. return ['add', 'clean', 'verify', 'ls', 'delete']
  88. // TODO - eventually...
  89. switch (argv[2]) {
  90. case 'verify':
  91. case 'clean':
  92. case 'add':
  93. case 'ls':
  94. case 'delete':
  95. return []
  96. }
  97. }
  98. exec (args, cb) {
  99. this.cache(args).then(() => cb()).catch(cb)
  100. }
  101. async cache (args) {
  102. const cmd = args.shift()
  103. switch (cmd) {
  104. case 'rm': case 'clear': case 'clean':
  105. return await this.clean(args)
  106. case 'add':
  107. return await this.add(args)
  108. case 'verify': case 'check':
  109. return await this.verify()
  110. case 'ls':
  111. return await this.ls(args)
  112. default:
  113. throw Object.assign(new Error(this.usage), { code: 'EUSAGE' })
  114. }
  115. }
  116. // npm cache clean [pkg]*
  117. async clean (args) {
  118. const cachePath = path.join(this.npm.cache, '_cacache')
  119. if (args.length === 0) {
  120. if (!this.npm.config.get('force')) {
  121. throw new Error(`As of npm@5, the npm cache self-heals from corruption issues
  122. by treating integrity mismatches as cache misses. As a result,
  123. data extracted from the cache is guaranteed to be valid. If you
  124. want to make sure everything is consistent, use \`npm cache verify\`
  125. instead. Deleting the cache can only make npm go slower, and is
  126. not likely to correct any problems you may be encountering!
  127. On the other hand, if you're debugging an issue with the installer,
  128. or race conditions that depend on the timing of writing to an empty
  129. cache, you can use \`npm install --cache /tmp/empty-cache\` to use a
  130. temporary cache instead of nuking the actual one.
  131. If you're sure you want to delete the entire cache, rerun this command
  132. with --force.`)
  133. }
  134. return rimraf(cachePath)
  135. }
  136. for (const key of args) {
  137. let entry
  138. try {
  139. entry = await cacache.get(cachePath, key)
  140. } catch (err) {
  141. this.npm.log.warn(`Not Found: ${key}`)
  142. break
  143. }
  144. this.npm.output(`Deleted: ${key}`)
  145. await cacache.rm.entry(cachePath, key)
  146. await cacache.rm.content(cachePath, entry.integrity)
  147. }
  148. }
  149. // npm cache add <tarball-url>...
  150. // npm cache add <pkg> <ver>...
  151. // npm cache add <tarball>...
  152. // npm cache add <folder>...
  153. async add (args) {
  154. const usage = 'Usage:\n' +
  155. ' npm cache add <tarball-url>...\n' +
  156. ' npm cache add <pkg>@<ver>...\n' +
  157. ' npm cache add <tarball>...\n' +
  158. ' npm cache add <folder>...\n'
  159. log.silly('cache add', 'args', args)
  160. if (args.length === 0)
  161. throw Object.assign(new Error(usage), { code: 'EUSAGE' })
  162. return Promise.all(args.map(spec => {
  163. log.silly('cache add', 'spec', spec)
  164. // we ask pacote for the thing, and then just throw the data
  165. // away so that it tee-pipes it into the cache like it does
  166. // for a normal request.
  167. return pacote.tarball.stream(spec, stream => {
  168. stream.resume()
  169. return stream.promise()
  170. }, this.npm.flatOptions)
  171. }))
  172. }
  173. async verify () {
  174. const cache = path.join(this.npm.cache, '_cacache')
  175. const prefix = cache.indexOf(process.env.HOME) === 0
  176. ? `~${cache.substr(process.env.HOME.length)}`
  177. : cache
  178. const stats = await cacache.verify(cache)
  179. this.npm.output(`Cache verified and compressed (${prefix})`)
  180. this.npm.output(`Content verified: ${stats.verifiedContent} (${stats.keptSize} bytes)`)
  181. stats.badContentCount && this.npm.output(`Corrupted content removed: ${stats.badContentCount}`)
  182. stats.reclaimedCount && this.npm.output(`Content garbage-collected: ${stats.reclaimedCount} (${stats.reclaimedSize} bytes)`)
  183. stats.missingContent && this.npm.output(`Missing content: ${stats.missingContent}`)
  184. this.npm.output(`Index entries: ${stats.totalEntries}`)
  185. this.npm.output(`Finished in ${stats.runTime.total / 1000}s`)
  186. }
  187. // npm cache ls [--package <spec> ...]
  188. async ls (specs) {
  189. const cachePath = path.join(this.npm.cache, '_cacache')
  190. const cacheKeys = Object.keys(await cacache.ls(cachePath))
  191. if (specs.length > 0) {
  192. // get results for each package spec specified
  193. const results = new Set()
  194. for (const spec of specs) {
  195. const keySet = await searchCachePackage(cachePath, spec, cacheKeys)
  196. for (const key of keySet)
  197. results.add(key)
  198. }
  199. [...results].sort(localeCompare).forEach(key => this.npm.output(key))
  200. return
  201. }
  202. cacheKeys.sort(localeCompare).forEach(key => this.npm.output(key))
  203. }
  204. }
  205. module.exports = Cache