Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
header.js325 linesDownload Raw Back to commonjs
1"use strict";
2// parse a 512-byte header block to a data object, or vice-versa
3// encode returns `true` if a pax extended header is needed, because
4// the data could not be faithfully encoded in a simple header.
5// (Also, check header.needPax to see if it needs a pax header.)
6var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7    if (k2 === undefined) k2 = k;
8    var desc = Object.getOwnPropertyDescriptor(m, k);
9    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10      desc = { enumerable: true, get: function() { return m[k]; } };
11    }
12    Object.defineProperty(o, k2, desc);
13}) : (function(o, m, k, k2) {
14    if (k2 === undefined) k2 = k;
15    o[k2] = m[k];
16}));
17var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
18    Object.defineProperty(o, "default", { enumerable: true, value: v });
19}) : function(o, v) {
20    o["default"] = v;
21});
22var __importStar = (this && this.__importStar) || (function () {
23    var ownKeys = function(o) {
24        ownKeys = Object.getOwnPropertyNames || function (o) {
25            var ar = [];
26            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
27            return ar;
28        };
29        return ownKeys(o);
30    };
31    return function (mod) {
32        if (mod && mod.__esModule) return mod;
33        var result = {};
34        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
35        __setModuleDefault(result, mod);
36        return result;
37    };
38})();
39Object.defineProperty(exports, "__esModule", { value: true });
40exports.Header = void 0;
41const node_path_1 = require("node:path");
42const large = __importStar(require("./large-numbers.js"));
43const types = __importStar(require("./types.js"));
44class Header {
45    cksumValid = false;
46    needPax = false;
47    nullBlock = false;
48    block;
49    path;
50    mode;
51    uid;
52    gid;
53    size;
54    cksum;
55    #type = 'Unsupported';
56    linkpath;
57    uname;
58    gname;
59    devmaj = 0;
60    devmin = 0;
61    atime;
62    ctime;
63    mtime;
64    charset;
65    comment;
66    constructor(data, off = 0, ex, gex) {
67        if (Buffer.isBuffer(data)) {
68            this.decode(data, off || 0, ex, gex);
69        }
70        else if (data) {
71            this.#slurp(data);
72        }
73    }
74    decode(buf, off, ex, gex) {
75        if (!off) {
76            off = 0;
77        }
78        if (!buf || !(buf.length >= off + 512)) {
79            throw new Error('need 512 bytes for header');
80        }
81        this.path = ex?.path ?? decString(buf, off, 100);
82        this.mode = ex?.mode ?? gex?.mode ?? decNumber(buf, off + 100, 8);
83        this.uid = ex?.uid ?? gex?.uid ?? decNumber(buf, off + 108, 8);
84        this.gid = ex?.gid ?? gex?.gid ?? decNumber(buf, off + 116, 8);
85        this.size = ex?.size ?? gex?.size ?? decNumber(buf, off + 124, 12);
86        this.mtime =
87            ex?.mtime ?? gex?.mtime ?? decDate(buf, off + 136, 12);
88        this.cksum = decNumber(buf, off + 148, 12);
89        // if we have extended or global extended headers, apply them now
90        // See https://github.com/npm/node-tar/pull/187
91        // Apply global before local, so it overrides
92        if (gex)
93            this.#slurp(gex, true);
94        if (ex)
95            this.#slurp(ex);
96        // old tar versions marked dirs as a file with a trailing /
97        const t = decString(buf, off + 156, 1);
98        if (types.isCode(t)) {
99            this.#type = t || '0';
100        }
101        if (this.#type === '0' && this.path.slice(-1) === '/') {
102            this.#type = '5';
103        }
104        // tar implementations sometimes incorrectly put the stat(dir).size
105        // as the size in the tarball, even though Directory entries are
106        // not able to have any body at all.  In the very rare chance that
107        // it actually DOES have a body, we weren't going to do anything with
108        // it anyway, and it'll just be a warning about an invalid header.
109        if (this.#type === '5') {
110            this.size = 0;
111        }
112        this.linkpath = decString(buf, off + 157, 100);
113        if (buf.subarray(off + 257, off + 265).toString() ===
114            'ustar\u000000') {
115            /* c8 ignore start */
116            this.uname =
117                ex?.uname ?? gex?.uname ?? decString(buf, off + 265, 32);
118            this.gname =
119                ex?.gname ?? gex?.gname ?? decString(buf, off + 297, 32);
120            this.devmaj =
121                ex?.devmaj ?? gex?.devmaj ?? decNumber(buf, off + 329, 8) ?? 0;
122            this.devmin =
123                ex?.devmin ?? gex?.devmin ?? decNumber(buf, off + 337, 8) ?? 0;
124            /* c8 ignore stop */
125            if (buf[off + 475] !== 0) {
126                // definitely a prefix, definitely >130 chars.
127                const prefix = decString(buf, off + 345, 155);
128                this.path = prefix + '/' + this.path;
129            }
130            else {
131                const prefix = decString(buf, off + 345, 130);
132                if (prefix) {
133                    this.path = prefix + '/' + this.path;
134                }
135                /* c8 ignore start */
136                this.atime =
137                    ex?.atime ?? gex?.atime ?? decDate(buf, off + 476, 12);
138                this.ctime =
139                    ex?.ctime ?? gex?.ctime ?? decDate(buf, off + 488, 12);
140                /* c8 ignore stop */
141            }
142        }
143        let sum = 8 * 0x20;
144        for (let i = off; i < off + 148; i++) {
145            sum += buf[i];
146        }
147        for (let i = off + 156; i < off + 512; i++) {
148            sum += buf[i];
149        }
150        this.cksumValid = sum === this.cksum;
151        if (this.cksum === undefined && sum === 8 * 0x20) {
152            this.nullBlock = true;
153        }
154    }
155    #slurp(ex, gex = false) {
156        Object.assign(this, Object.fromEntries(Object.entries(ex).filter(([k, v]) => {
157            // we slurp in everything except for the path attribute in
158            // a global extended header, because that's weird. Also, any
159            // null/undefined values are ignored.
160            return !(v === null ||
161                v === undefined ||
162                (k === 'path' && gex) ||
163                (k === 'linkpath' && gex) ||
164                k === 'global');
165        })));
166    }
167    encode(buf, off = 0) {
168        if (!buf) {
169            buf = this.block = Buffer.alloc(512);
170        }
171        if (this.#type === 'Unsupported') {
172            this.#type = '0';
173        }
174        if (!(buf.length >= off + 512)) {
175            throw new Error('need 512 bytes for header');
176        }
177        const prefixSize = this.ctime || this.atime ? 130 : 155;
178        const split = splitPrefix(this.path || '', prefixSize);
179        const path = split[0];
180        const prefix = split[1];
181        this.needPax = !!split[2];
182        this.needPax = encString(buf, off, 100, path) || this.needPax;
183        this.needPax =
184            encNumber(buf, off + 100, 8, this.mode) || this.needPax;
185        this.needPax =
186            encNumber(buf, off + 108, 8, this.uid) || this.needPax;
187        this.needPax =
188            encNumber(buf, off + 116, 8, this.gid) || this.needPax;
189        this.needPax =
190            encNumber(buf, off + 124, 12, this.size) || this.needPax;
191        this.needPax =
192            encDate(buf, off + 136, 12, this.mtime) || this.needPax;
193        buf[off + 156] = this.#type.charCodeAt(0);
194        this.needPax =
195            encString(buf, off + 157, 100, this.linkpath) || this.needPax;
196        buf.write('ustar\u000000', off + 257, 8);
197        this.needPax =
198            encString(buf, off + 265, 32, this.uname) || this.needPax;
199        this.needPax =
200            encString(buf, off + 297, 32, this.gname) || this.needPax;
201        this.needPax =
202            encNumber(buf, off + 329, 8, this.devmaj) || this.needPax;
203        this.needPax =
204            encNumber(buf, off + 337, 8, this.devmin) || this.needPax;
205        this.needPax =
206            encString(buf, off + 345, prefixSize, prefix) || this.needPax;
207        if (buf[off + 475] !== 0) {
208            this.needPax =
209                encString(buf, off + 345, 155, prefix) || this.needPax;
210        }
211        else {
212            this.needPax =
213                encString(buf, off + 345, 130, prefix) || this.needPax;
214            this.needPax =
215                encDate(buf, off + 476, 12, this.atime) || this.needPax;
216            this.needPax =
217                encDate(buf, off + 488, 12, this.ctime) || this.needPax;
218        }
219        let sum = 8 * 0x20;
220        for (let i = off; i < off + 148; i++) {
221            sum += buf[i];
222        }
223        for (let i = off + 156; i < off + 512; i++) {
224            sum += buf[i];
225        }
226        this.cksum = sum;
227        encNumber(buf, off + 148, 8, this.cksum);
228        this.cksumValid = true;
229        return this.needPax;
230    }
231    get type() {
232        return (this.#type === 'Unsupported' ?
233            this.#type
234            : types.name.get(this.#type));
235    }
236    get typeKey() {
237        return this.#type;
238    }
239    set type(type) {
240        const c = String(types.code.get(type));
241        if (types.isCode(c) || c === 'Unsupported') {
242            this.#type = c;
243        }
244        else if (types.isCode(type)) {
245            this.#type = type;
246        }
247        else {
248            throw new TypeError('invalid entry type: ' + type);
249        }
250    }
251}
252exports.Header = Header;
253const splitPrefix = (p, prefixSize) => {
254    const pathSize = 100;
255    let pp = p;
256    let prefix = '';
257    let ret = undefined;
258    const root = node_path_1.posix.parse(p).root || '.';
259    if (Buffer.byteLength(pp) < pathSize) {
260        ret = [pp, prefix, false];
261    }
262    else {
263        // first set prefix to the dir, and path to the base
264        prefix = node_path_1.posix.dirname(pp);
265        pp = node_path_1.posix.basename(pp);
266        do {
267            if (Buffer.byteLength(pp) <= pathSize &&
268                Buffer.byteLength(prefix) <= prefixSize) {
269                // both fit!
270                ret = [pp, prefix, false];
271            }
272            else if (Buffer.byteLength(pp) > pathSize &&
273                Buffer.byteLength(prefix) <= prefixSize) {
274                // prefix fits in prefix, but path doesn't fit in path
275                ret = [pp.slice(0, pathSize - 1), prefix, true];
276            }
277            else {
278                // make path take a bit from prefix
279                pp = node_path_1.posix.join(node_path_1.posix.basename(prefix), pp);
280                prefix = node_path_1.posix.dirname(prefix);
281            }
282        } while (prefix !== root && ret === undefined);
283        // at this point, found no resolution, just truncate
284        if (!ret) {
285            ret = [p.slice(0, pathSize - 1), '', true];
286        }
287    }
288    return ret;
289};
290const decString = (buf, off, size) => buf
291    .subarray(off, off + size)
292    .toString('utf8')
293    .replace(/\0.*/, '');
294const decDate = (buf, off, size) => numToDate(decNumber(buf, off, size));
295const numToDate = (num) => num === undefined ? undefined : new Date(num * 1000);
296const decNumber = (buf, off, size) => Number(buf[off]) & 0x80 ?
297    large.parse(buf.subarray(off, off + size))
298    : decSmallNumber(buf, off, size);
299const nanUndef = (value) => (isNaN(value) ? undefined : value);
300const decSmallNumber = (buf, off, size) => nanUndef(parseInt(buf
301    .subarray(off, off + size)
302    .toString('utf8')
303    .replace(/\0.*$/, '')
304    .trim(), 8));
305// the maximum encodable as a null-terminated octal, by field size
306const MAXNUM = {
307    12: 0o77777777777,
308    8: 0o7777777,
309};
310const encNumber = (buf, off, size, num) => num === undefined ? false
311    : num > MAXNUM[size] || num < 0 ?
312        (large.encode(num, buf.subarray(off, off + size)), true)
313        : (encSmallNumber(buf, off, size, num), false);
314const encSmallNumber = (buf, off, size, num) => buf.write(octalString(num, size), off, size, 'ascii');
315const octalString = (num, size) => padOctal(Math.floor(num).toString(8), size);
316const padOctal = (str, size) => (str.length === size - 1 ?
317    str
318    : new Array(size - str.length - 1).join('0') + str + ' ') + '\0';
319const encDate = (buf, off, size, date) => date === undefined ? false : (encNumber(buf, off, size, date.getTime() / 1000));
320// enough to fill the longest string we've got
321const NULLS = new Array(156).join('\0');
322// pad with nulls, return true if it's longer or non-ascii
323const encString = (buf, off, size, str) => str === undefined ? false : ((buf.write(str + NULLS, off, size, 'utf8'),
324    str.length !== Buffer.byteLength(str) || str.length > size));
325//# sourceMappingURL=header.js.map
codekingpro/portable-devtools · Team Ai