extract.js 2.7 KB

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