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.

list.js 3.1KB

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