项目原始demo,不改动
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
Questo repository è archiviato. Puoi vedere i file e clonarli, ma non puoi effettuare richieste di pushj o aprire problemi/richieste di pull.
 
 
 
 

126 righe
3.3 KiB

  1. 'use strict'
  2. const BB = require('bluebird')
  3. const contentPath = require('./path')
  4. const fs = require('graceful-fs')
  5. const PassThrough = require('stream').PassThrough
  6. const pipe = BB.promisify(require('mississippi').pipe)
  7. const ssri = require('ssri')
  8. const Y = require('../util/y.js')
  9. BB.promisifyAll(fs)
  10. module.exports = read
  11. function read (cache, integrity, opts) {
  12. opts = opts || {}
  13. return pickContentSri(cache, integrity).then(content => {
  14. const sri = content.sri
  15. const cpath = contentPath(cache, sri)
  16. return fs.readFileAsync(cpath, null).then(data => {
  17. if (typeof opts.size === 'number' && opts.size !== data.length) {
  18. throw sizeError(opts.size, data.length)
  19. } else if (ssri.checkData(data, sri)) {
  20. return data
  21. } else {
  22. throw integrityError(sri, cpath)
  23. }
  24. })
  25. })
  26. }
  27. module.exports.stream = readStream
  28. module.exports.readStream = readStream
  29. function readStream (cache, integrity, opts) {
  30. opts = opts || {}
  31. const stream = new PassThrough()
  32. pickContentSri(
  33. cache, integrity
  34. ).then(content => {
  35. const sri = content.sri
  36. return pipe(
  37. fs.createReadStream(contentPath(cache, sri)),
  38. ssri.integrityStream({
  39. integrity: sri,
  40. size: opts.size
  41. }),
  42. stream
  43. )
  44. }).catch(err => {
  45. stream.emit('error', err)
  46. })
  47. return stream
  48. }
  49. if (fs.copyFile) {
  50. module.exports.copy = copy
  51. }
  52. function copy (cache, integrity, dest, opts) {
  53. opts = opts || {}
  54. return pickContentSri(cache, integrity).then(content => {
  55. const sri = content.sri
  56. const cpath = contentPath(cache, sri)
  57. return fs.copyFileAsync(cpath, dest).then(() => content.size)
  58. })
  59. }
  60. module.exports.hasContent = hasContent
  61. function hasContent (cache, integrity) {
  62. if (!integrity) { return BB.resolve(false) }
  63. return pickContentSri(cache, integrity)
  64. .catch({code: 'ENOENT'}, () => false)
  65. .catch({code: 'EPERM'}, err => {
  66. if (process.platform !== 'win32') {
  67. throw err
  68. } else {
  69. return false
  70. }
  71. }).then(content => {
  72. if (!content.sri) return false
  73. return ({ sri: content.sri, size: content.stat.size })
  74. })
  75. }
  76. module.exports._pickContentSri = pickContentSri
  77. function pickContentSri (cache, integrity) {
  78. const sri = ssri.parse(integrity)
  79. // If `integrity` has multiple entries, pick the first digest
  80. // with available local data.
  81. const algo = sri.pickAlgorithm()
  82. const digests = sri[algo]
  83. if (digests.length <= 1) {
  84. const cpath = contentPath(cache, digests[0])
  85. return fs.lstatAsync(cpath).then(stat => ({ sri: digests[0], stat }))
  86. } else {
  87. return BB.any(sri[sri.pickAlgorithm()].map(meta => {
  88. return pickContentSri(cache, meta)
  89. }))
  90. .catch(err => {
  91. if ([].some.call(err, e => e.code === 'ENOENT')) {
  92. throw Object.assign(
  93. new Error('No matching content found for ' + sri.toString()),
  94. {code: 'ENOENT'}
  95. )
  96. } else {
  97. throw err[0]
  98. }
  99. })
  100. }
  101. }
  102. function sizeError (expected, found) {
  103. var err = new Error(Y`Bad data size: expected inserted data to be ${expected} bytes, but got ${found} instead`)
  104. err.expected = expected
  105. err.found = found
  106. err.code = 'EBADSIZE'
  107. return err
  108. }
  109. function integrityError (sri, path) {
  110. var err = new Error(Y`Integrity verification failed for ${sri} (${path})`)
  111. err.code = 'EINTEGRITY'
  112. err.sri = sri
  113. err.path = path
  114. return err
  115. }