entry-index.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 contentPath = require('./content/path')
  9. const fixOwner = require('./util/fix-owner')
  10. const hashToSegments = require('./util/hash-to-segments')
  11. const indexV = require('../package.json')['cache-version'].index
  12. const appendFile = util.promisify(fs.appendFile)
  13. const readFile = util.promisify(fs.readFile)
  14. const readdir = util.promisify(fs.readdir)
  15. module.exports.NotFoundError = class NotFoundError extends Error {
  16. constructor (cache, key) {
  17. super(`No cache entry for ${key} found in ${cache}`)
  18. this.code = 'ENOENT'
  19. this.cache = cache
  20. this.key = key
  21. }
  22. }
  23. module.exports.insert = insert
  24. function insert (cache, key, integrity, opts = {}) {
  25. const { metadata, size } = opts
  26. const bucket = bucketPath(cache, key)
  27. const entry = {
  28. key,
  29. integrity: integrity && ssri.stringify(integrity),
  30. time: Date.now(),
  31. size,
  32. metadata
  33. }
  34. return fixOwner
  35. .mkdirfix(cache, path.dirname(bucket))
  36. .then(() => {
  37. const stringified = JSON.stringify(entry)
  38. // NOTE - Cleverness ahoy!
  39. //
  40. // This works because it's tremendously unlikely for an entry to corrupt
  41. // another while still preserving the string length of the JSON in
  42. // question. So, we just slap the length in there and verify it on read.
  43. //
  44. // Thanks to @isaacs for the whiteboarding session that ended up with this.
  45. return appendFile(bucket, `\n${hashEntry(stringified)}\t${stringified}`)
  46. })
  47. .then(() => fixOwner.chownr(cache, bucket))
  48. .catch((err) => {
  49. if (err.code === 'ENOENT') {
  50. return undefined
  51. }
  52. throw err
  53. // There's a class of race conditions that happen when things get deleted
  54. // during fixOwner, or between the two mkdirfix/chownr calls.
  55. //
  56. // It's perfectly fine to just not bother in those cases and lie
  57. // that the index entry was written. Because it's a cache.
  58. })
  59. .then(() => {
  60. return formatEntry(cache, entry)
  61. })
  62. }
  63. module.exports.insert.sync = insertSync
  64. function insertSync (cache, key, integrity, opts = {}) {
  65. const { metadata, size } = opts
  66. const bucket = bucketPath(cache, key)
  67. const entry = {
  68. key,
  69. integrity: integrity && ssri.stringify(integrity),
  70. time: Date.now(),
  71. size,
  72. metadata
  73. }
  74. fixOwner.mkdirfix.sync(cache, path.dirname(bucket))
  75. const stringified = JSON.stringify(entry)
  76. fs.appendFileSync(bucket, `\n${hashEntry(stringified)}\t${stringified}`)
  77. try {
  78. fixOwner.chownr.sync(cache, bucket)
  79. } catch (err) {
  80. if (err.code !== 'ENOENT') {
  81. throw err
  82. }
  83. }
  84. return formatEntry(cache, entry)
  85. }
  86. module.exports.find = find
  87. function find (cache, key) {
  88. const bucket = bucketPath(cache, key)
  89. return bucketEntries(bucket)
  90. .then((entries) => {
  91. return entries.reduce((latest, next) => {
  92. if (next && next.key === key) {
  93. return formatEntry(cache, next)
  94. } else {
  95. return latest
  96. }
  97. }, null)
  98. })
  99. .catch((err) => {
  100. if (err.code === 'ENOENT') {
  101. return null
  102. } else {
  103. throw err
  104. }
  105. })
  106. }
  107. module.exports.find.sync = findSync
  108. function findSync (cache, key) {
  109. const bucket = bucketPath(cache, key)
  110. try {
  111. return bucketEntriesSync(bucket).reduce((latest, next) => {
  112. if (next && next.key === key) {
  113. return formatEntry(cache, next)
  114. } else {
  115. return latest
  116. }
  117. }, null)
  118. } catch (err) {
  119. if (err.code === 'ENOENT') {
  120. return null
  121. } else {
  122. throw err
  123. }
  124. }
  125. }
  126. module.exports.delete = del
  127. function del (cache, key, opts) {
  128. return insert(cache, key, null, opts)
  129. }
  130. module.exports.delete.sync = delSync
  131. function delSync (cache, key, opts) {
  132. return insertSync(cache, key, null, opts)
  133. }
  134. module.exports.lsStream = lsStream
  135. function lsStream (cache) {
  136. const indexDir = bucketDir(cache)
  137. const stream = new Minipass({ objectMode: true })
  138. readdirOrEmpty(indexDir).then(buckets => Promise.all(
  139. buckets.map(bucket => {
  140. const bucketPath = path.join(indexDir, bucket)
  141. return readdirOrEmpty(bucketPath).then(subbuckets => Promise.all(
  142. subbuckets.map(subbucket => {
  143. const subbucketPath = path.join(bucketPath, subbucket)
  144. // "/cachename/<bucket 0xFF>/<bucket 0xFF>./*"
  145. return readdirOrEmpty(subbucketPath).then(entries => Promise.all(
  146. entries.map(entry => {
  147. const entryPath = path.join(subbucketPath, entry)
  148. return bucketEntries(entryPath).then(entries =>
  149. // using a Map here prevents duplicate keys from
  150. // showing up twice, I guess?
  151. entries.reduce((acc, entry) => {
  152. acc.set(entry.key, entry)
  153. return acc
  154. }, new Map())
  155. ).then(reduced => {
  156. // reduced is a map of key => entry
  157. for (const entry of reduced.values()) {
  158. const formatted = formatEntry(cache, entry)
  159. if (formatted) {
  160. stream.write(formatted)
  161. }
  162. }
  163. }).catch(err => {
  164. if (err.code === 'ENOENT') { return undefined }
  165. throw err
  166. })
  167. })
  168. ))
  169. })
  170. ))
  171. })
  172. ))
  173. .then(
  174. () => stream.end(),
  175. err => stream.emit('error', err)
  176. )
  177. return stream
  178. }
  179. module.exports.ls = ls
  180. function ls (cache) {
  181. return lsStream(cache).collect().then(entries =>
  182. entries.reduce((acc, xs) => {
  183. acc[xs.key] = xs
  184. return acc
  185. }, {})
  186. )
  187. }
  188. function bucketEntries (bucket, filter) {
  189. return readFile(bucket, 'utf8').then((data) => _bucketEntries(data, filter))
  190. }
  191. function bucketEntriesSync (bucket, filter) {
  192. const data = fs.readFileSync(bucket, 'utf8')
  193. return _bucketEntries(data, filter)
  194. }
  195. function _bucketEntries (data, filter) {
  196. const entries = []
  197. data.split('\n').forEach((entry) => {
  198. if (!entry) {
  199. return
  200. }
  201. const pieces = entry.split('\t')
  202. if (!pieces[1] || hashEntry(pieces[1]) !== pieces[0]) {
  203. // Hash is no good! Corruption or malice? Doesn't matter!
  204. // EJECT EJECT
  205. return
  206. }
  207. let obj
  208. try {
  209. obj = JSON.parse(pieces[1])
  210. } catch (e) {
  211. // Entry is corrupted!
  212. return
  213. }
  214. if (obj) {
  215. entries.push(obj)
  216. }
  217. })
  218. return entries
  219. }
  220. module.exports.bucketDir = bucketDir
  221. function bucketDir (cache) {
  222. return path.join(cache, `index-v${indexV}`)
  223. }
  224. module.exports.bucketPath = bucketPath
  225. function bucketPath (cache, key) {
  226. const hashed = hashKey(key)
  227. return path.join.apply(
  228. path,
  229. [bucketDir(cache)].concat(hashToSegments(hashed))
  230. )
  231. }
  232. module.exports.hashKey = hashKey
  233. function hashKey (key) {
  234. return hash(key, 'sha256')
  235. }
  236. module.exports.hashEntry = hashEntry
  237. function hashEntry (str) {
  238. return hash(str, 'sha1')
  239. }
  240. function hash (str, digest) {
  241. return crypto
  242. .createHash(digest)
  243. .update(str)
  244. .digest('hex')
  245. }
  246. function formatEntry (cache, entry) {
  247. // Treat null digests as deletions. They'll shadow any previous entries.
  248. if (!entry.integrity) {
  249. return null
  250. }
  251. return {
  252. key: entry.key,
  253. integrity: entry.integrity,
  254. path: contentPath(cache, entry.integrity),
  255. size: entry.size,
  256. time: entry.time,
  257. metadata: entry.metadata
  258. }
  259. }
  260. function readdirOrEmpty (dir) {
  261. return readdir(dir).catch((err) => {
  262. if (err.code === 'ENOENT' || err.code === 'ENOTDIR') {
  263. return []
  264. }
  265. throw err
  266. })
  267. }