You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

disk.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use strict'
  2. const fs = require('./wrapped-fs')
  3. const path = require('path')
  4. const pickle = require('chromium-pickle-js')
  5. const Filesystem = require('./filesystem')
  6. let filesystemCache = {}
  7. async function copyFile (dest, src, filename) {
  8. const srcFile = path.join(src, filename)
  9. const targetFile = path.join(dest, filename)
  10. const [content, stats] = await Promise.all([fs.readFile(srcFile), fs.stat(srcFile), fs.mkdirp(path.dirname(targetFile))])
  11. return fs.writeFile(targetFile, content, { mode: stats.mode })
  12. }
  13. async function streamTransformedFile (originalFilename, outStream, transformed) {
  14. return new Promise((resolve, reject) => {
  15. const stream = fs.createReadStream(transformed ? transformed.path : originalFilename)
  16. stream.pipe(outStream, { end: false })
  17. stream.on('error', reject)
  18. stream.on('end', () => resolve())
  19. })
  20. }
  21. const writeFileListToStream = async function (dest, filesystem, out, list, metadata) {
  22. for (const file of list) {
  23. if (file.unpack) { // the file should not be packed into archive
  24. const filename = path.relative(filesystem.src, file.filename)
  25. await copyFile(`${dest}.unpacked`, filesystem.src, filename)
  26. } else {
  27. await streamTransformedFile(file.filename, out, metadata[file.filename].transformed)
  28. }
  29. }
  30. return out.end()
  31. }
  32. module.exports.writeFilesystem = async function (dest, filesystem, files, metadata) {
  33. const headerPickle = pickle.createEmpty()
  34. headerPickle.writeString(JSON.stringify(filesystem.header))
  35. const headerBuf = headerPickle.toBuffer()
  36. const sizePickle = pickle.createEmpty()
  37. sizePickle.writeUInt32(headerBuf.length)
  38. const sizeBuf = sizePickle.toBuffer()
  39. const out = fs.createWriteStream(dest)
  40. await new Promise((resolve, reject) => {
  41. out.on('error', reject)
  42. out.write(sizeBuf)
  43. return out.write(headerBuf, () => resolve())
  44. })
  45. return writeFileListToStream(dest, filesystem, out, files, metadata)
  46. }
  47. module.exports.readArchiveHeaderSync = function (archive) {
  48. const fd = fs.openSync(archive, 'r')
  49. let size
  50. let headerBuf
  51. try {
  52. const sizeBuf = Buffer.alloc(8)
  53. if (fs.readSync(fd, sizeBuf, 0, 8, null) !== 8) {
  54. throw new Error('Unable to read header size')
  55. }
  56. const sizePickle = pickle.createFromBuffer(sizeBuf)
  57. size = sizePickle.createIterator().readUInt32()
  58. headerBuf = Buffer.alloc(size)
  59. if (fs.readSync(fd, headerBuf, 0, size, null) !== size) {
  60. throw new Error('Unable to read header')
  61. }
  62. } finally {
  63. fs.closeSync(fd)
  64. }
  65. const headerPickle = pickle.createFromBuffer(headerBuf)
  66. const header = headerPickle.createIterator().readString()
  67. return { headerString: header, header: JSON.parse(header), headerSize: size }
  68. }
  69. module.exports.readFilesystemSync = function (archive) {
  70. if (!filesystemCache[archive]) {
  71. const header = this.readArchiveHeaderSync(archive)
  72. const filesystem = new Filesystem(archive)
  73. filesystem.header = header.header
  74. filesystem.headerSize = header.headerSize
  75. filesystemCache[archive] = filesystem
  76. }
  77. return filesystemCache[archive]
  78. }
  79. module.exports.uncacheFilesystem = function (archive) {
  80. if (filesystemCache[archive]) {
  81. filesystemCache[archive] = undefined
  82. return true
  83. }
  84. return false
  85. }
  86. module.exports.uncacheAll = function () {
  87. filesystemCache = {}
  88. }
  89. module.exports.readFileSync = function (filesystem, filename, info) {
  90. let buffer = Buffer.alloc(info.size)
  91. if (info.size <= 0) { return buffer }
  92. if (info.unpacked) {
  93. // it's an unpacked file, copy it.
  94. buffer = fs.readFileSync(path.join(`${filesystem.src}.unpacked`, filename))
  95. } else {
  96. // Node throws an exception when reading 0 bytes into a 0-size buffer,
  97. // so we short-circuit the read in this case.
  98. const fd = fs.openSync(filesystem.src, 'r')
  99. try {
  100. const offset = 8 + filesystem.headerSize + parseInt(info.offset)
  101. fs.readSync(fd, buffer, 0, info.size, offset)
  102. } finally {
  103. fs.closeSync(fd)
  104. }
  105. }
  106. return buffer
  107. }