Blame view

天文台pc/tianwentai-ui/node_modules/tar/dist/esm/list.js 3.27 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  // tar -t
  import * as fsm from '@isaacs/fs-minipass';
  import fs from 'node:fs';
  import { dirname, parse } from 'path';
  import { makeCommand } from './make-command.js';
  import { Parser } from './parse.js';
  import { stripTrailingSlashes } from './strip-trailing-slashes.js';
  const onReadEntryFunction = (opt) => {
      const onReadEntry = opt.onReadEntry;
      opt.onReadEntry =
          onReadEntry ?
              e => {
                  onReadEntry(e);
                  e.resume();
              }
              : e => e.resume();
  };
  // construct a filter that limits the file entries listed
  // include child entries if a dir is included
  export const filesFilter = (opt, files) => {
      const map = new Map(files.map(f => [stripTrailingSlashes(f), true]));
      const filter = opt.filter;
      const mapHas = (file, r = '') => {
          const root = r || parse(file).root || '.';
          let ret;
          if (file === root)
              ret = false;
          else {
              const m = map.get(file);
              ret = m !== undefined ? m : mapHas(dirname(file), root);
          }
          map.set(file, ret);
          return ret;
      };
      opt.filter =
          filter ?
              (file, entry) => filter(file, entry) && mapHas(stripTrailingSlashes(file))
              : file => mapHas(stripTrailingSlashes(file));
  };
  const listFileSync = (opt) => {
      const p = new Parser(opt);
      const file = opt.file;
      let fd;
      try {
          fd = fs.openSync(file, 'r');
          const stat = fs.fstatSync(fd);
          const readSize = opt.maxReadSize || 16 * 1024 * 1024;
          if (stat.size < readSize) {
              const buf = Buffer.allocUnsafe(stat.size);
              const read = fs.readSync(fd, buf, 0, stat.size, 0);
              p.end(read === buf.byteLength ? buf : buf.subarray(0, read));
          }
          else {
              let pos = 0;
              const buf = Buffer.allocUnsafe(readSize);
              while (pos < stat.size) {
                  const bytesRead = fs.readSync(fd, buf, 0, readSize, pos);
                  if (bytesRead === 0)
                      break;
                  pos += bytesRead;
                  p.write(buf.subarray(0, bytesRead));
              }
              p.end();
          }
      }
      finally {
          if (typeof fd === 'number') {
              try {
                  fs.closeSync(fd);
                  /* c8 ignore next */
              }
              catch { }
          }
      }
  };
  const listFile = (opt, _files) => {
      const parse = new Parser(opt);
      const readSize = opt.maxReadSize || 16 * 1024 * 1024;
      const file = opt.file;
      const p = new Promise((resolve, reject) => {
          parse.on('error', reject);
          parse.on('end', resolve);
          fs.stat(file, (er, stat) => {
              if (er) {
                  reject(er);
              }
              else {
                  const stream = new fsm.ReadStream(file, {
                      readSize: readSize,
                      size: stat.size,
                  });
                  stream.on('error', reject);
                  stream.pipe(parse);
              }
          });
      });
      return p;
  };
  export const list = makeCommand(listFileSync, listFile, opt => new Parser(opt), opt => new Parser(opt), (opt, files) => {
      if (files?.length)
          filesFilter(opt, files);
      if (!opt.noResume)
          onReadEntryFunction(opt);
  });
  //# sourceMappingURL=list.js.map