codekingpro/portable-devtools
114k
1// tar -x
2import * as fsm from '@isaacs/fs-minipass';
3import fs from 'node:fs';
4import { filesFilter } from './list.js';
5import { makeCommand } from './make-command.js';
6import { Unpack, UnpackSync } from './unpack.js';
7const 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};
20const 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};
45export 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