extract.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict'
  2. // tar -x
  3. const hlo = require('./high-level-opt.js')
  4. const Unpack = require('./unpack.js')
  5. const fs = require('fs')
  6. const fsm = require('fs-minipass')
  7. const path = require('path')
  8. const stripSlash = require('./strip-trailing-slashes.js')
  9. module.exports = (opt_, files, cb) => {
  10. if (typeof opt_ === 'function')
  11. cb = opt_, files = null, opt_ = {}
  12. else if (Array.isArray(opt_))
  13. files = opt_, opt_ = {}
  14. if (typeof files === 'function')
  15. cb = files, files = null
  16. if (!files)
  17. files = []
  18. else
  19. files = Array.from(files)
  20. const opt = hlo(opt_)
  21. if (opt.sync && typeof cb === 'function')
  22. throw new TypeError('callback not supported for sync tar functions')
  23. if (!opt.file && typeof cb === 'function')
  24. throw new TypeError('callback only supported with file option')
  25. if (files.length)
  26. filesFilter(opt, files)
  27. return opt.file && opt.sync ? extractFileSync(opt)
  28. : opt.file ? extractFile(opt, cb)
  29. : opt.sync ? extractSync(opt)
  30. : extract(opt)
  31. }
  32. // construct a filter that limits the file entries listed
  33. // include child entries if a dir is included
  34. const filesFilter = (opt, files) => {
  35. const map = new Map(files.map(f => [stripSlash(f), true]))
  36. const filter = opt.filter
  37. const mapHas = (file, r) => {
  38. const root = r || path.parse(file).root || '.'
  39. const ret = file === root ? false
  40. : map.has(file) ? map.get(file)
  41. : mapHas(path.dirname(file), root)
  42. map.set(file, ret)
  43. return ret
  44. }
  45. opt.filter = filter
  46. ? (file, entry) => filter(file, entry) && mapHas(stripSlash(file))
  47. : file => mapHas(stripSlash(file))
  48. }
  49. const extractFileSync = opt => {
  50. const u = new Unpack.Sync(opt)
  51. const file = opt.file
  52. const stat = fs.statSync(file)
  53. // This trades a zero-byte read() syscall for a stat
  54. // However, it will usually result in less memory allocation
  55. const readSize = opt.maxReadSize || 16 * 1024 * 1024
  56. const stream = new fsm.ReadStreamSync(file, {
  57. readSize: readSize,
  58. size: stat.size,
  59. })
  60. stream.pipe(u)
  61. }
  62. const extractFile = (opt, cb) => {
  63. const u = new Unpack(opt)
  64. const readSize = opt.maxReadSize || 16 * 1024 * 1024
  65. const file = opt.file
  66. const p = new Promise((resolve, reject) => {
  67. u.on('error', reject)
  68. u.on('close', resolve)
  69. // This trades a zero-byte read() syscall for a stat
  70. // However, it will usually result in less memory allocation
  71. fs.stat(file, (er, stat) => {
  72. if (er)
  73. reject(er)
  74. else {
  75. const stream = new fsm.ReadStream(file, {
  76. readSize: readSize,
  77. size: stat.size,
  78. })
  79. stream.on('error', reject)
  80. stream.pipe(u)
  81. }
  82. })
  83. })
  84. return cb ? p.then(cb, cb) : p
  85. }
  86. const extractSync = opt => new Unpack.Sync(opt)
  87. const extract = opt => new Unpack(opt)