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.

asar.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. 'use strict'
  2. const fs = require('./wrapped-fs')
  3. const path = require('path')
  4. const minimatch = require('minimatch')
  5. const Filesystem = require('./filesystem')
  6. const disk = require('./disk')
  7. const crawlFilesystem = require('./crawlfs')
  8. /**
  9. * Whether a directory should be excluded from packing due to the `--unpack-dir" option.
  10. *
  11. * @param {string} dirPath - directory path to check
  12. * @param {string} pattern - literal prefix [for backward compatibility] or glob pattern
  13. * @param {array} unpackDirs - Array of directory paths previously marked as unpacked
  14. */
  15. function isUnpackedDir (dirPath, pattern, unpackDirs) {
  16. if (dirPath.startsWith(pattern) || minimatch(dirPath, pattern)) {
  17. if (!unpackDirs.includes(dirPath)) {
  18. unpackDirs.push(dirPath)
  19. }
  20. return true
  21. } else {
  22. return unpackDirs.some(unpackDir => dirPath.startsWith(unpackDir))
  23. }
  24. }
  25. module.exports.createPackage = async function (src, dest) {
  26. return module.exports.createPackageWithOptions(src, dest, {})
  27. }
  28. module.exports.createPackageWithOptions = async function (src, dest, options) {
  29. const globOptions = options.globOptions ? options.globOptions : {}
  30. globOptions.dot = options.dot === undefined ? true : options.dot
  31. const pattern = src + (options.pattern ? options.pattern : '/**/*')
  32. const [filenames, metadata] = await crawlFilesystem(pattern, globOptions)
  33. return module.exports.createPackageFromFiles(src, dest, filenames, metadata, options)
  34. }
  35. /**
  36. * Create an ASAR archive from a list of filenames.
  37. *
  38. * @param {string} src: Base path. All files are relative to this.
  39. * @param {string} dest: Archive filename (& path).
  40. * @param {array} filenames: List of filenames relative to src.
  41. * @param {object} metadata: Object with filenames as keys and {type='directory|file|link', stat: fs.stat} as values. (Optional)
  42. * @param {object} options: Options passed to `createPackageWithOptions`.
  43. */
  44. module.exports.createPackageFromFiles = async function (src, dest, filenames, metadata, options) {
  45. if (typeof metadata === 'undefined' || metadata === null) { metadata = {} }
  46. if (typeof options === 'undefined' || options === null) { options = {} }
  47. src = path.normalize(src)
  48. dest = path.normalize(dest)
  49. filenames = filenames.map(function (filename) { return path.normalize(filename) })
  50. const filesystem = new Filesystem(src)
  51. const files = []
  52. const unpackDirs = []
  53. let filenamesSorted = []
  54. if (options.ordering) {
  55. const orderingFiles = (await fs.readFile(options.ordering)).toString().split('\n').map(line => {
  56. if (line.includes(':')) { line = line.split(':').pop() }
  57. line = line.trim()
  58. if (line.startsWith('/')) { line = line.slice(1) }
  59. return line
  60. })
  61. const ordering = []
  62. for (const file of orderingFiles) {
  63. const pathComponents = file.split(path.sep)
  64. let str = src
  65. for (const pathComponent of pathComponents) {
  66. str = path.join(str, pathComponent)
  67. ordering.push(str)
  68. }
  69. }
  70. let missing = 0
  71. const total = filenames.length
  72. for (const file of ordering) {
  73. if (!filenamesSorted.includes(file) && filenames.includes(file)) {
  74. filenamesSorted.push(file)
  75. }
  76. }
  77. for (const file of filenames) {
  78. if (!filenamesSorted.includes(file)) {
  79. filenamesSorted.push(file)
  80. missing += 1
  81. }
  82. }
  83. console.log(`Ordering file has ${((total - missing) / total) * 100}% coverage.`)
  84. } else {
  85. filenamesSorted = filenames
  86. }
  87. const handleFile = async function (filename) {
  88. if (!metadata[filename]) {
  89. metadata[filename] = await crawlFilesystem.determineFileType(filename)
  90. }
  91. const file = metadata[filename]
  92. let shouldUnpack
  93. switch (file.type) {
  94. case 'directory':
  95. if (options.unpackDir) {
  96. shouldUnpack = isUnpackedDir(path.relative(src, filename), options.unpackDir, unpackDirs)
  97. } else {
  98. shouldUnpack = false
  99. }
  100. filesystem.insertDirectory(filename, shouldUnpack)
  101. break
  102. case 'file':
  103. shouldUnpack = false
  104. if (options.unpack) {
  105. shouldUnpack = minimatch(filename, options.unpack, { matchBase: true })
  106. }
  107. if (!shouldUnpack && options.unpackDir) {
  108. const dirName = path.relative(src, path.dirname(filename))
  109. shouldUnpack = isUnpackedDir(dirName, options.unpackDir, unpackDirs)
  110. }
  111. files.push({ filename: filename, unpack: shouldUnpack })
  112. return filesystem.insertFile(filename, shouldUnpack, file, options)
  113. case 'link':
  114. filesystem.insertLink(filename)
  115. break
  116. }
  117. return Promise.resolve()
  118. }
  119. const insertsDone = async function () {
  120. await fs.mkdirp(path.dirname(dest))
  121. return disk.writeFilesystem(dest, filesystem, files, metadata)
  122. }
  123. const names = filenamesSorted.slice()
  124. const next = async function (name) {
  125. if (!name) { return insertsDone() }
  126. await handleFile(name)
  127. return next(names.shift())
  128. }
  129. return next(names.shift())
  130. }
  131. module.exports.statFile = function (archive, filename, followLinks) {
  132. const filesystem = disk.readFilesystemSync(archive)
  133. return filesystem.getFile(filename, followLinks)
  134. }
  135. module.exports.getRawHeader = function (archive) {
  136. return disk.readArchiveHeaderSync(archive)
  137. }
  138. module.exports.listPackage = function (archive, options) {
  139. return disk.readFilesystemSync(archive).listFiles(options)
  140. }
  141. module.exports.extractFile = function (archive, filename) {
  142. const filesystem = disk.readFilesystemSync(archive)
  143. return disk.readFileSync(filesystem, filename, filesystem.getFile(filename))
  144. }
  145. module.exports.extractAll = function (archive, dest) {
  146. const filesystem = disk.readFilesystemSync(archive)
  147. const filenames = filesystem.listFiles()
  148. // under windows just extract links as regular files
  149. const followLinks = process.platform === 'win32'
  150. // create destination directory
  151. fs.mkdirpSync(dest)
  152. for (const fullPath of filenames) {
  153. // Remove leading slash
  154. const filename = fullPath.substr(1)
  155. const destFilename = path.join(dest, filename)
  156. const file = filesystem.getFile(filename, followLinks)
  157. if (file.files) {
  158. // it's a directory, create it and continue with the next entry
  159. fs.mkdirpSync(destFilename)
  160. } else if (file.link) {
  161. // it's a symlink, create a symlink
  162. const linkSrcPath = path.dirname(path.join(dest, file.link))
  163. const linkDestPath = path.dirname(destFilename)
  164. const relativePath = path.relative(linkDestPath, linkSrcPath)
  165. // try to delete output file, because we can't overwrite a link
  166. try {
  167. fs.unlinkSync(destFilename)
  168. } catch {}
  169. const linkTo = path.join(relativePath, path.basename(file.link))
  170. fs.symlinkSync(linkTo, destFilename)
  171. } else {
  172. // it's a file, extract it
  173. const content = disk.readFileSync(filesystem, filename, file)
  174. fs.writeFileSync(destFilename, content)
  175. if (file.executable) {
  176. fs.chmodSync(destFilename, '755')
  177. }
  178. }
  179. }
  180. }
  181. module.exports.uncache = function (archive) {
  182. return disk.uncacheFilesystem(archive)
  183. }
  184. module.exports.uncacheAll = function () {
  185. disk.uncacheAll()
  186. }