read.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. 'use strict'
  2. const fs = require('fs/promises')
  3. const fsm = require('fs-minipass')
  4. const ssri = require('ssri')
  5. const contentPath = require('./path')
  6. const Pipeline = require('minipass-pipeline')
  7. module.exports = read
  8. const MAX_SINGLE_READ_SIZE = 64 * 1024 * 1024
  9. async function read (cache, integrity, opts = {}) {
  10. const { size } = opts
  11. const { stat, cpath, sri } = await withContentSri(cache, integrity, async (cpath, sri) => {
  12. // get size
  13. const stat = await fs.stat(cpath)
  14. return { stat, cpath, sri }
  15. })
  16. if (typeof size === 'number' && stat.size !== size) {
  17. throw sizeError(size, stat.size)
  18. }
  19. if (stat.size > MAX_SINGLE_READ_SIZE) {
  20. return readPipeline(cpath, stat.size, sri, new Pipeline()).concat()
  21. }
  22. const data = await fs.readFile(cpath, { encoding: null })
  23. if (!ssri.checkData(data, sri)) {
  24. throw integrityError(sri, cpath)
  25. }
  26. return data
  27. }
  28. const readPipeline = (cpath, size, sri, stream) => {
  29. stream.push(
  30. new fsm.ReadStream(cpath, {
  31. size,
  32. readSize: MAX_SINGLE_READ_SIZE,
  33. }),
  34. ssri.integrityStream({
  35. integrity: sri,
  36. size,
  37. })
  38. )
  39. return stream
  40. }
  41. module.exports.stream = readStream
  42. module.exports.readStream = readStream
  43. function readStream (cache, integrity, opts = {}) {
  44. const { size } = opts
  45. const stream = new Pipeline()
  46. // Set all this up to run on the stream and then just return the stream
  47. Promise.resolve().then(async () => {
  48. const { stat, cpath, sri } = await withContentSri(cache, integrity, async (cpath, sri) => {
  49. // just stat to ensure it exists
  50. const stat = await fs.stat(cpath)
  51. return { stat, cpath, sri }
  52. })
  53. if (typeof size === 'number' && size !== stat.size) {
  54. return stream.emit('error', sizeError(size, stat.size))
  55. }
  56. return readPipeline(cpath, stat.size, sri, stream)
  57. }).catch(err => stream.emit('error', err))
  58. return stream
  59. }
  60. module.exports.copy = copy
  61. function copy (cache, integrity, dest) {
  62. return withContentSri(cache, integrity, (cpath, sri) => {
  63. return fs.copyFile(cpath, dest)
  64. })
  65. }
  66. module.exports.hasContent = hasContent
  67. async function hasContent (cache, integrity) {
  68. if (!integrity) {
  69. return false
  70. }
  71. try {
  72. return await withContentSri(cache, integrity, async (cpath, sri) => {
  73. const stat = await fs.stat(cpath)
  74. return { size: stat.size, sri, stat }
  75. })
  76. } catch (err) {
  77. if (err.code === 'ENOENT') {
  78. return false
  79. }
  80. if (err.code === 'EPERM') {
  81. /* istanbul ignore else */
  82. if (process.platform !== 'win32') {
  83. throw err
  84. } else {
  85. return false
  86. }
  87. }
  88. }
  89. }
  90. async function withContentSri (cache, integrity, fn) {
  91. const sri = ssri.parse(integrity)
  92. // If `integrity` has multiple entries, pick the first digest
  93. // with available local data.
  94. const algo = sri.pickAlgorithm()
  95. const digests = sri[algo]
  96. if (digests.length <= 1) {
  97. const cpath = contentPath(cache, digests[0])
  98. return fn(cpath, digests[0])
  99. } else {
  100. // Can't use race here because a generic error can happen before
  101. // a ENOENT error, and can happen before a valid result
  102. const results = await Promise.all(digests.map(async (meta) => {
  103. try {
  104. return await withContentSri(cache, meta, fn)
  105. } catch (err) {
  106. if (err.code === 'ENOENT') {
  107. return Object.assign(
  108. new Error('No matching content found for ' + sri.toString()),
  109. { code: 'ENOENT' }
  110. )
  111. }
  112. return err
  113. }
  114. }))
  115. // Return the first non error if it is found
  116. const result = results.find((r) => !(r instanceof Error))
  117. if (result) {
  118. return result
  119. }
  120. // Throw the No matching content found error
  121. const enoentError = results.find((r) => r.code === 'ENOENT')
  122. if (enoentError) {
  123. throw enoentError
  124. }
  125. // Throw generic error
  126. throw results.find((r) => r instanceof Error)
  127. }
  128. }
  129. function sizeError (expected, found) {
  130. /* eslint-disable-next-line max-len */
  131. const err = new Error(`Bad data size: expected inserted data to be ${expected} bytes, but got ${found} instead`)
  132. err.expected = expected
  133. err.found = found
  134. err.code = 'EBADSIZE'
  135. return err
  136. }
  137. function integrityError (sri, path) {
  138. const err = new Error(`Integrity verification failed for ${sri} (${path})`)
  139. err.code = 'EINTEGRITY'
  140. err.sri = sri
  141. err.path = path
  142. return err
  143. }