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