codekingpro/portable-devtools
114k
1import { Minipass } from 'minipass';
2import { normalizeWindowsPath } from './normalize-windows-path.js';
3export class ReadEntry extends Minipass {
4 extended;
5 globalExtended;
6 header;
7 startBlockSize;
8 blockRemain;
9 remain;
10 type;
11 meta = false;
12 ignore = false;
13 path;
14 mode;
15 uid;
16 gid;
17 uname;
18 gname;
19 size = 0;
20 mtime;
21 atime;
22 ctime;
23 linkpath;
24 dev;
25 ino;
26 nlink;
27 invalid = false;
28 absolute;
29 unsupported = false;
30 constructor(header, ex, gex) {
31 super({});
32 // read entries always start life paused. this is to avoid the
33 // situation where Minipass's auto-ending empty streams results
34 // in an entry ending before we're ready for it.
35 this.pause();
36 this.extended = ex;
37 this.globalExtended = gex;
38 this.header = header;
39 /* c8 ignore start */
40 this.remain = header.size ?? 0;
41 /* c8 ignore stop */
42 this.startBlockSize = 512 * Math.ceil(this.remain / 512);
43 this.blockRemain = this.startBlockSize;
44 this.type = header.type;
45 switch (this.type) {
46 case 'File':
47 case 'OldFile':
48 case 'Link':
49 case 'SymbolicLink':
50 case 'CharacterDevice':
51 case 'BlockDevice':
52 case 'Directory':
53 case 'FIFO':
54 case 'ContiguousFile':
55 case 'GNUDumpDir':
56 break;
57 case 'NextFileHasLongLinkpath':
58 case 'NextFileHasLongPath':
59 case 'OldGnuLongPath':
60 case 'GlobalExtendedHeader':
61 case 'ExtendedHeader':
62 case 'OldExtendedHeader':
63 this.meta = true;
64 break;
65 // NOTE: gnutar and bsdtar treat unrecognized types as 'File'
66 // it may be worth doing the same, but with a warning.
67 default:
68 this.ignore = true;
69 }
70 /* c8 ignore start */
71 if (!header.path) {
72 throw new Error('no path provided for tar.ReadEntry');
73 }
74 /* c8 ignore stop */
75 this.path = normalizeWindowsPath(header.path);
76 this.mode = header.mode;
77 if (this.mode) {
78 this.mode = this.mode & 0o7777;
79 }
80 this.uid = header.uid;
81 this.gid = header.gid;
82 this.uname = header.uname;
83 this.gname = header.gname;
84 this.size = this.remain;
85 this.mtime = header.mtime;
86 this.atime = header.atime;
87 this.ctime = header.ctime;
88 /* c8 ignore start */
89 this.linkpath =
90 header.linkpath ? normalizeWindowsPath(header.linkpath) : undefined;
91 /* c8 ignore stop */
92 this.uname = header.uname;
93 this.gname = header.gname;
94 if (ex) {
95 this.#slurp(ex);
96 }
97 if (gex) {
98 this.#slurp(gex, true);
99 }
100 }
101 write(data) {
102 const writeLen = data.length;
103 if (writeLen > this.blockRemain) {
104 throw new Error('writing more to entry than is appropriate');
105 }
106 const r = this.remain;
107 const br = this.blockRemain;
108 this.remain = Math.max(0, r - writeLen);
109 this.blockRemain = Math.max(0, br - writeLen);
110 if (this.ignore) {
111 return true;
112 }
113 if (r >= writeLen) {
114 return super.write(data);
115 }
116 // r < writeLen
117 return super.write(data.subarray(0, r));
118 }
119 #slurp(ex, gex = false) {
120 if (ex.path)
121 ex.path = normalizeWindowsPath(ex.path);
122 if (ex.linkpath)
123 ex.linkpath = normalizeWindowsPath(ex.linkpath);
124 Object.assign(this, Object.fromEntries(Object.entries(ex).filter(([k, v]) => {
125 // we slurp in everything except for the path attribute in
126 // a global extended header, because that's weird. Also, any
127 // null/undefined values are ignored.
128 return !(v === null || v === undefined || (k === 'path' && gex));
129 })));
130 }
131}
132//# sourceMappingURL=read-entry.js.map