a9aeaf93dcff7335010e076c3259d3422eb40e730a2e25f50df8ebb5d7944d5f961b6f2a9bf6267a1ce8ca3f97058d3235ae5d6f96ffb0461eabfdcd04e97f 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // tar -x
  2. import * as fsm from '@isaacs/fs-minipass';
  3. import fs from 'node:fs';
  4. import { filesFilter } from './list.js';
  5. import { makeCommand } from './make-command.js';
  6. import { Unpack, UnpackSync } from './unpack.js';
  7. const extractFileSync = (opt) => {
  8. const u = new UnpackSync(opt);
  9. const file = opt.file;
  10. const stat = fs.statSync(file);
  11. // This trades a zero-byte read() syscall for a stat
  12. // However, it will usually result in less memory allocation
  13. const readSize = opt.maxReadSize || 16 * 1024 * 1024;
  14. const stream = new fsm.ReadStreamSync(file, {
  15. readSize: readSize,
  16. size: stat.size,
  17. });
  18. stream.pipe(u);
  19. };
  20. const extractFile = (opt, _) => {
  21. const u = new Unpack(opt);
  22. const readSize = opt.maxReadSize || 16 * 1024 * 1024;
  23. const file = opt.file;
  24. const p = new Promise((resolve, reject) => {
  25. u.on('error', reject);
  26. u.on('close', resolve);
  27. // This trades a zero-byte read() syscall for a stat
  28. // However, it will usually result in less memory allocation
  29. fs.stat(file, (er, stat) => {
  30. if (er) {
  31. reject(er);
  32. }
  33. else {
  34. const stream = new fsm.ReadStream(file, {
  35. readSize: readSize,
  36. size: stat.size,
  37. });
  38. stream.on('error', reject);
  39. stream.pipe(u);
  40. }
  41. });
  42. });
  43. return p;
  44. };
  45. export const extract = makeCommand(extractFileSync, extractFile, opt => new UnpackSync(opt), opt => new Unpack(opt), (opt, files) => {
  46. if (files?.length)
  47. filesFilter(opt, files);
  48. });
  49. //# sourceMappingURL=extract.js.map