entry-index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. 'use strict'
  2. const util = require('util')
  3. const crypto = require('crypto')
  4. const fs = require('fs')
  5. const Minipass = require('minipass')
  6. const path = require('path')
  7. const ssri = require('ssri')
  8. const uniqueFilename = require('unique-filename')
  9. const { disposer } = require('./util/disposer')
  10. const contentPath = require('./content/path')
  11. const fixOwner = require('./util/fix-owner')
  12. const hashToSegments = require('./util/hash-to-segments')
  13. const indexV = require('../package.json')['cache-version'].index
  14. const moveFile = require('@npmcli/move-file')
  15. const _rimraf = require('rimraf')
  16. const rimraf = util.promisify(_rimraf)
  17. rimraf.sync = _rimraf.sync
  18. const appendFile = util.promisify(fs.appendFile)
  19. const readFile = util.promisify(fs.readFile)
  20. const readdir = util.promisify(fs.readdir)
  21. const writeFile = util.promisify(fs.writeFile)
  22. module.exports.NotFoundError = class NotFoundError extends Error {
  23. constructor (cache, key) {
  24. super(`No cache entry for ${key} found in ${cache}`)
  25. this.code = 'ENOENT'
  26. this.cache = cache
  27. this.key = key
  28. }
  29. }
  30. module.exports.compact = compact
  31. async function compact (cache, key, matchFn, opts = {}) {
  32. const bucket = bucketPath(cache, key)
  33. const entries = await bucketEntries(bucket)
  34. const newEntries = []
  35. // we loop backwards because the bottom-most result is the newest
  36. // since we add new entries with appendFile
  37. for (let i = entries.length - 1; i >= 0; --i) {
  38. const entry = entries[i]
  39. // a null integrity could mean either a delete was appended
  40. // or the user has simply stored an index that does not map
  41. // to any content. we determine if the user wants to keep the
  42. // null integrity based on the validateEntry function passed in options.
  43. // if the integrity is null and no validateEntry is provided, we break
  44. // as we consider the null integrity to be a deletion of everything
  45. // that came before it.
  46. if (entry.integrity === null && !opts.validateEntry)
  47. break
  48. // if this entry is valid, and it is either the first entry or
  49. // the newEntries array doesn't already include an entry that
  50. // matches this one based on the provided matchFn, then we add
  51. // it to the beginning of our list
  52. if ((!opts.validateEntry || opts.validateEntry(entry) === true) &&
  53. (newEntries.length === 0 ||
  54. !newEntries.find((oldEntry) => matchFn(oldEntry, entry))))
  55. newEntries.unshift(entry)
  56. }
  57. const newIndex = '\n' + newEntries.map((entry) => {
  58. const stringified = JSON.stringify(entry)
  59. const hash = hashEntry(stringified)
  60. return `${hash}\t${stringified}`
  61. }).join('\n')
  62. const setup = async () => {
  63. const target = uniqueFilename(path.join(cache, 'tmp'), opts.tmpPrefix)
  64. await fixOwner.mkdirfix(cache, path.dirname(target))
  65. return {
  66. target,
  67. moved: false,
  68. }
  69. }
  70. const teardown = async (tmp) => {
  71. if (!tmp.moved)
  72. return rimraf(tmp.target)
  73. }
  74. const write = async (tmp) => {
  75. await writeFile(tmp.target, newIndex, { flag: 'wx' })
  76. await fixOwner.mkdirfix(cache, path.dirname(bucket))
  77. // we use @npmcli/move-file directly here because we
  78. // want to overwrite the existing file
  79. await moveFile(tmp.target, bucket)
  80. tmp.moved = true
  81. try {
  82. await fixOwner.chownr(cache, bucket)
  83. } catch (err) {
  84. if (err.code !== 'ENOENT')
  85. throw err
  86. }
  87. }
  88. // write the file atomically
  89. await disposer(setup(), teardown, write)
  90. // we reverse the list we generated such that the newest
  91. // entries come first in order to make looping through them easier
  92. // the true passed to formatEntry tells it to keep null
  93. // integrity values, if they made it this far it's because
  94. // validateEntry returned true, and as such we should return it
  95. return newEntries.reverse().map((entry) => formatEntry(cache, entry, true))
  96. }
  97. module.exports.insert = insert
  98. function insert (cache, key, integrity, opts = {}) {
  99. const { metadata, size } = opts
  100. const bucket = bucketPath(cache, key)
  101. const entry = {
  102. key,
  103. integrity: integrity && ssri.stringify(integrity),
  104. time: Date.now(),
  105. size,
  106. metadata,
  107. }
  108. return fixOwner
  109. .mkdirfix(cache, path.dirname(bucket))
  110. .then(() => {
  111. const stringified = JSON.stringify(entry)
  112. // NOTE - Cleverness ahoy!
  113. //
  114. // This works because it's tremendously unlikely for an entry to corrupt
  115. // another while still preserving the string length of the JSON in
  116. // question. So, we just slap the length in there and verify it on read.
  117. //
  118. // Thanks to @isaacs for the whiteboarding session that ended up with
  119. // this.
  120. return appendFile(bucket, `\n${hashEntry(stringified)}\t${stringified}`)
  121. })
  122. .then(() => fixOwner.chownr(cache, bucket))
  123. .catch((err) => {
  124. if (err.code === 'ENOENT')
  125. return undefined
  126. throw err
  127. // There's a class of race conditions that happen when things get deleted
  128. // during fixOwner, or between the two mkdirfix/chownr calls.
  129. //
  130. // It's perfectly fine to just not bother in those cases and lie
  131. // that the index entry was written. Because it's a cache.
  132. })
  133. .then(() => {
  134. return formatEntry(cache, entry)
  135. })
  136. }
  137. module.exports.insert.sync = insertSync
  138. function insertSync (cache, key, integrity, opts = {}) {
  139. const { metadata, size } = opts
  140. const bucket = bucketPath(cache, key)
  141. const entry = {
  142. key,
  143. integrity: integrity && ssri.stringify(integrity),
  144. time: Date.now(),
  145. size,
  146. metadata,
  147. }
  148. fixOwner.mkdirfix.sync(cache, path.dirname(bucket))
  149. const stringified = JSON.stringify(entry)
  150. fs.appendFileSync(bucket, `\n${hashEntry(stringified)}\t${stringified}`)
  151. try {
  152. fixOwner.chownr.sync(cache, bucket)
  153. } catch (err) {
  154. if (err.code !== 'ENOENT')
  155. throw err
  156. }
  157. return formatEntry(cache, entry)
  158. }
  159. module.exports.find = find
  160. function find (cache, key) {
  161. const bucket = bucketPath(cache, key)
  162. return bucketEntries(bucket)
  163. .then((entries) => {
  164. return entries.reduce((latest, next) => {
  165. if (next && next.key === key)
  166. return formatEntry(cache, next)
  167. else
  168. return latest
  169. }, null)
  170. })
  171. .catch((err) => {
  172. if (err.code === 'ENOENT')
  173. return null
  174. else
  175. throw err
  176. })
  177. }
  178. module.exports.find.sync = findSync
  179. function findSync (cache, key) {
  180. const bucket = bucketPath(cache, key)
  181. try {
  182. return bucketEntriesSync(bucket).reduce((latest, next) => {
  183. if (next && next.key === key)
  184. return formatEntry(cache, next)
  185. else
  186. return latest
  187. }, null)
  188. } catch (err) {
  189. if (err.code === 'ENOENT')
  190. return null
  191. else
  192. throw err
  193. }
  194. }
  195. module.exports.delete = del
  196. function del (cache, key, opts = {}) {
  197. if (!opts.removeFully)
  198. return insert(cache, key, null, opts)
  199. const bucket = bucketPath(cache, key)
  200. return rimraf(bucket)
  201. }
  202. module.exports.delete.sync = delSync
  203. function delSync (cache, key, opts = {}) {
  204. if (!opts.removeFully)
  205. return insertSync(cache, key, null, opts)
  206. const bucket = bucketPath(cache, key)
  207. return rimraf.sync(bucket)
  208. }
  209. module.exports.lsStream = lsStream
  210. function lsStream (cache) {
  211. const indexDir = bucketDir(cache)
  212. const stream = new Minipass({ objectMode: true })
  213. readdirOrEmpty(indexDir).then(buckets => Promise.all(
  214. buckets.map(bucket => {
  215. const bucketPath = path.join(indexDir, bucket)
  216. return readdirOrEmpty(bucketPath).then(subbuckets => Promise.all(
  217. subbuckets.map(subbucket => {
  218. const subbucketPath = path.join(bucketPath, subbucket)
  219. // "/cachename/<bucket 0xFF>/<bucket 0xFF>./*"
  220. return readdirOrEmpty(subbucketPath).then(entries => Promise.all(
  221. entries.map(entry => {
  222. const entryPath = path.join(subbucketPath, entry)
  223. return bucketEntries(entryPath).then(entries =>
  224. // using a Map here prevents duplicate keys from
  225. // showing up twice, I guess?
  226. entries.reduce((acc, entry) => {
  227. acc.set(entry.key, entry)
  228. return acc
  229. }, new Map())
  230. ).then(reduced => {
  231. // reduced is a map of key => entry
  232. for (const entry of reduced.values()) {
  233. const formatted = formatEntry(cache, entry)
  234. if (formatted)
  235. stream.write(formatted)
  236. }
  237. }).catch(err => {
  238. if (err.code === 'ENOENT')
  239. return undefined
  240. throw err
  241. })
  242. })
  243. ))
  244. })
  245. ))
  246. })
  247. ))
  248. .then(
  249. () => stream.end(),
  250. err => stream.emit('error', err)
  251. )
  252. return stream
  253. }
  254. module.exports.ls = ls
  255. function ls (cache) {
  256. return lsStream(cache).collect().then(entries =>
  257. entries.reduce((acc, xs) => {
  258. acc[xs.key] = xs
  259. return acc
  260. }, {})
  261. )
  262. }
  263. module.exports.bucketEntries = bucketEntries
  264. function bucketEntries (bucket, filter) {
  265. return readFile(bucket, 'utf8').then((data) => _bucketEntries(data, filter))
  266. }
  267. module.exports.bucketEntries.sync = bucketEntriesSync
  268. function bucketEntriesSync (bucket, filter) {
  269. const data = fs.readFileSync(bucket, 'utf8')
  270. return _bucketEntries(data, filter)
  271. }
  272. function _bucketEntries (data, filter) {
  273. const entries = []
  274. data.split('\n').forEach((entry) => {
  275. if (!entry)
  276. return
  277. const pieces = entry.split('\t')
  278. if (!pieces[1] || hashEntry(pieces[1]) !== pieces[0]) {
  279. // Hash is no good! Corruption or malice? Doesn't matter!
  280. // EJECT EJECT
  281. return
  282. }
  283. let obj
  284. try {
  285. obj = JSON.parse(pieces[1])
  286. } catch (e) {
  287. // Entry is corrupted!
  288. return
  289. }
  290. if (obj)
  291. entries.push(obj)
  292. })
  293. return entries
  294. }
  295. module.exports.bucketDir = bucketDir
  296. function bucketDir (cache) {
  297. return path.join(cache, `index-v${indexV}`)
  298. }
  299. module.exports.bucketPath = bucketPath
  300. function bucketPath (cache, key) {
  301. const hashed = hashKey(key)
  302. return path.join.apply(
  303. path,
  304. [bucketDir(cache)].concat(hashToSegments(hashed))
  305. )
  306. }
  307. module.exports.hashKey = hashKey
  308. function hashKey (key) {
  309. return hash(key, 'sha256')
  310. }
  311. module.exports.hashEntry = hashEntry
  312. function hashEntry (str) {
  313. return hash(str, 'sha1')
  314. }
  315. function hash (str, digest) {
  316. return crypto
  317. .createHash(digest)
  318. .update(str)
  319. .digest('hex')
  320. }
  321. function formatEntry (cache, entry, keepAll) {
  322. // Treat null digests as deletions. They'll shadow any previous entries.
  323. if (!entry.integrity && !keepAll)
  324. return null
  325. return {
  326. key: entry.key,
  327. integrity: entry.integrity,
  328. path: entry.integrity ? contentPath(cache, entry.integrity) : undefined,
  329. size: entry.size,
  330. time: entry.time,
  331. metadata: entry.metadata,
  332. }
  333. }
  334. function readdirOrEmpty (dir) {
  335. return readdir(dir).catch((err) => {
  336. if (err.code === 'ENOENT' || err.code === 'ENOTDIR')
  337. return []
  338. throw err
  339. })
  340. }