list.js 3.0 KB

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