list.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. 'use strict'
  2. // XXX: This shares a lot in common with extract.js
  3. // maybe some DRY opportunity here?
  4. // tar -t
  5. const hlo = require('./high-level-opt.js')
  6. const Parser = require('./parse.js')
  7. const fs = require('fs')
  8. const fsm = require('fs-minipass')
  9. const path = require('path')
  10. const stripSlash = require('./strip-trailing-slashes.js')
  11. module.exports = (opt_, files, cb) => {
  12. if (typeof opt_ === 'function')
  13. cb = opt_, files = null, opt_ = {}
  14. else if (Array.isArray(opt_))
  15. files = opt_, opt_ = {}
  16. if (typeof files === 'function')
  17. cb = files, files = null
  18. if (!files)
  19. files = []
  20. else
  21. files = Array.from(files)
  22. const opt = hlo(opt_)
  23. if (opt.sync && typeof cb === 'function')
  24. throw new TypeError('callback not supported for sync tar functions')
  25. if (!opt.file && typeof cb === 'function')
  26. throw new TypeError('callback only supported with file option')
  27. if (files.length)
  28. filesFilter(opt, files)
  29. if (!opt.noResume)
  30. onentryFunction(opt)
  31. return opt.file && opt.sync ? listFileSync(opt)
  32. : opt.file ? listFile(opt, cb)
  33. : list(opt)
  34. }
  35. const onentryFunction = opt => {
  36. const onentry = opt.onentry
  37. opt.onentry = onentry ? e => {
  38. onentry(e)
  39. e.resume()
  40. } : e => e.resume()
  41. }
  42. // construct a filter that limits the file entries listed
  43. // include child entries if a dir is included
  44. const filesFilter = (opt, files) => {
  45. const map = new Map(files.map(f => [stripSlash(f), true]))
  46. const filter = opt.filter
  47. const mapHas = (file, r) => {
  48. const root = r || path.parse(file).root || '.'
  49. const ret = file === root ? false
  50. : map.has(file) ? map.get(file)
  51. : mapHas(path.dirname(file), root)
  52. map.set(file, ret)
  53. return ret
  54. }
  55. opt.filter = filter
  56. ? (file, entry) => filter(file, entry) && mapHas(stripSlash(file))
  57. : file => mapHas(stripSlash(file))
  58. }
  59. const listFileSync = opt => {
  60. const p = list(opt)
  61. const file = opt.file
  62. let threw = true
  63. let fd
  64. try {
  65. const stat = fs.statSync(file)
  66. const readSize = opt.maxReadSize || 16 * 1024 * 1024
  67. if (stat.size < readSize)
  68. p.end(fs.readFileSync(file))
  69. else {
  70. let pos = 0
  71. const buf = Buffer.allocUnsafe(readSize)
  72. fd = fs.openSync(file, 'r')
  73. while (pos < stat.size) {
  74. const bytesRead = fs.readSync(fd, buf, 0, readSize, pos)
  75. pos += bytesRead
  76. p.write(buf.slice(0, bytesRead))
  77. }
  78. p.end()
  79. }
  80. threw = false
  81. } finally {
  82. if (threw && fd) {
  83. try {
  84. fs.closeSync(fd)
  85. } catch (er) {}
  86. }
  87. }
  88. }
  89. const listFile = (opt, cb) => {
  90. const parse = new Parser(opt)
  91. const readSize = opt.maxReadSize || 16 * 1024 * 1024
  92. const file = opt.file
  93. const p = new Promise((resolve, reject) => {
  94. parse.on('error', reject)
  95. parse.on('end', resolve)
  96. fs.stat(file, (er, stat) => {
  97. if (er)
  98. reject(er)
  99. else {
  100. const stream = new fsm.ReadStream(file, {
  101. readSize: readSize,
  102. size: stat.size,
  103. })
  104. stream.on('error', reject)
  105. stream.pipe(parse)
  106. }
  107. })
  108. })
  109. return cb ? p.then(cb, cb) : p
  110. }
  111. const list = opt => new Parser(opt)