Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
header.js312 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 = ex?.mtime ?? gex?.mtime ?? decDate(buf, off + 136, 12);
87        this.cksum = decNumber(buf, off + 148, 12);
88        // if we have extended or global extended headers, apply them now
89        // See https://github.com/npm/node-tar/pull/187
90        // Apply global before local, so it overrides
91        if (gex)
92            this.#slurp(gex, true);
93        if (ex)
94            this.#slurp(ex);
95        // old tar versions marked dirs as a file with a trailing /
96        const t = decString(buf, off + 156, 1);
97        if (types.isCode(t)) {
98            this.#type = t || '0';
99        }
100        if (this.#type === '0' && this.path.slice(-1) === '/') {
101            this.#type = '5';
102        }
103        // tar implementations sometimes incorrectly put the stat(dir).size
104        // as the size in the tarball, even though Directory entries are
105        // not able to have any body at all.  In the very rare chance that
106        // it actually DOES have a body, we weren't going to do anything with
107        // it anyway, and it'll just be a warning about an invalid header.
108        if (this.#type === '5') {
109            this.size = 0;
110        }
111        this.linkpath = decString(buf, off + 157, 100);
112        if (buf.subarray(off + 257, off + 265).toString() === 'ustar\u000000') {
113            /* c8 ignore start */
114            this.uname = ex?.uname ?? gex?.uname ?? decString(buf, off + 265, 32);
115            this.gname = ex?.gname ?? gex?.gname ?? decString(buf, off + 297, 32);
116            this.devmaj =
117                ex?.devmaj ?? gex?.devmaj ?? decNumber(buf, off + 329, 8) ?? 0;
118            this.devmin =
119                ex?.devmin ?? gex?.devmin ?? decNumber(buf, off + 337, 8) ?? 0;
120            /* c8 ignore stop */
121            if (buf[off + 475] !== 0) {
122                // definitely a prefix, definitely >130 chars.
123                const prefix = decString(buf, off + 345, 155);
124                this.path = prefix + '/' + this.path;
125            }
126            else {
127                const prefix = decString(buf, off + 345, 130);
128                if (prefix) {
129                    this.path = prefix + '/' + this.path;
130                }
131                /* c8 ignore start */
132                this.atime = ex?.atime ?? gex?.atime ?? decDate(buf, off + 476, 12);
133                this.ctime = ex?.ctime ?? gex?.ctime ?? decDate(buf, off + 488, 12);
134                /* c8 ignore stop */
135            }
136        }
137        let sum = 8 * 0x20;
138        for (let i = off; i < off + 148; i++) {
139            sum += buf[i];
140        }
141        for (let i = off + 156; i < off + 512; i++) {
142            sum += buf[i];
143        }
144        this.cksumValid = sum === this.cksum;
145        if (this.cksum === undefined && sum === 8 * 0x20) {
146            this.nullBlock = true;
147        }
148    }
149    #slurp(ex, gex = false) {
150        Object.assign(this, Object.fromEntries(Object.entries(ex).filter(([k, v]) => {
151            // we slurp in everything except for the path attribute in
152            // a global extended header, because that's weird. Also, any
153            // null/undefined values are ignored.
154            return !(v === null ||
155                v === undefined ||
156                (k === 'path' && gex) ||
157                (k === 'linkpath' && gex) ||
158                k === 'global');
159        })));
160    }
161    encode(buf, off = 0) {
162        if (!buf) {
163            buf = this.block = Buffer.alloc(512);
164        }
165        if (this.#type === 'Unsupported') {
166            this.#type = '0';
167        }
168        if (!(buf.length >= off + 512)) {
169            throw new Error('need 512 bytes for header');
170        }
171        const prefixSize = this.ctime || this.atime ? 130 : 155;
172        const split = splitPrefix(this.path || '', prefixSize);
173        const path = split[0];
174        const prefix = split[1];
175        this.needPax = !!split[2];
176        this.needPax = encString(buf, off, 100, path) || this.needPax;
177        this.needPax = encNumber(buf, off + 100, 8, this.mode) || this.needPax;
178        this.needPax = encNumber(buf, off + 108, 8, this.uid) || this.needPax;
179        this.needPax = encNumber(buf, off + 116, 8, this.gid) || this.needPax;
180        this.needPax = encNumber(buf, off + 124, 12, this.size) || this.needPax;
181        this.needPax = encDate(buf, off + 136, 12, this.mtime) || this.needPax;
182        buf[off + 156] = Number(this.#type.codePointAt(0));
183        this.needPax =
184            encString(buf, off + 157, 100, this.linkpath) || this.needPax;
185        buf.write('ustar\u000000', off + 257, 8);
186        this.needPax =
187            encString(buf, off + 265, 32, this.uname) || this.needPax;
188        this.needPax =
189            encString(buf, off + 297, 32, this.gname) || this.needPax;
190        this.needPax =
191            encNumber(buf, off + 329, 8, this.devmaj) || this.needPax;
192        this.needPax =
193            encNumber(buf, off + 337, 8, this.devmin) || this.needPax;
194        this.needPax =
195            encString(buf, off + 345, prefixSize, prefix) || this.needPax;
196        if (buf[off + 475] !== 0) {
197            this.needPax = encString(buf, off + 345, 155, prefix) || this.needPax;
198        }
199        else {
200            this.needPax = encString(buf, off + 345, 130, prefix) || this.needPax;
201            this.needPax =
202                encDate(buf, off + 476, 12, this.atime) || this.needPax;
203            this.needPax =
204                encDate(buf, off + 488, 12, this.ctime) || this.needPax;
205        }
206        let sum = 8 * 0x20;
207        for (let i = off; i < off + 148; i++) {
208            sum += buf[i];
209        }
210        for (let i = off + 156; i < off + 512; i++) {
211            sum += buf[i];
212        }
213        this.cksum = sum;
214        encNumber(buf, off + 148, 8, this.cksum);
215        this.cksumValid = true;
216        return this.needPax;
217    }
218    get type() {
219        return (this.#type === 'Unsupported' ?
220            this.#type
221            : types.name.get(this.#type));
222    }
223    get typeKey() {
224        return this.#type;
225    }
226    set type(type) {
227        const c = String(types.code.get(type));
228        if (types.isCode(c) || c === 'Unsupported') {
229            this.#type = c;
230        }
231        else if (types.isCode(type)) {
232            this.#type = type;
233        }
234        else {
235            throw new TypeError('invalid entry type: ' + type);
236        }
237    }
238}
239exports.Header = Header;
240const splitPrefix = (p, prefixSize) => {
241    const pathSize = 100;
242    let pp = p;
243    let prefix = '';
244    let ret = undefined;
245    const root = node_path_1.posix.parse(p).root || '.';
246    if (Buffer.byteLength(pp) < pathSize) {
247        ret = [pp, prefix, false];
248    }
249    else {
250        // first set prefix to the dir, and path to the base
251        prefix = node_path_1.posix.dirname(pp);
252        pp = node_path_1.posix.basename(pp);
253        do {
254            if (Buffer.byteLength(pp) <= pathSize &&
255                Buffer.byteLength(prefix) <= prefixSize) {
256                // both fit!
257                ret = [pp, prefix, false];
258            }
259            else if (Buffer.byteLength(pp) > pathSize &&
260                Buffer.byteLength(prefix) <= prefixSize) {
261                // prefix fits in prefix, but path doesn't fit in path
262                ret = [pp.slice(0, pathSize - 1), prefix, true];
263            }
264            else {
265                // make path take a bit from prefix
266                pp = node_path_1.posix.join(node_path_1.posix.basename(prefix), pp);
267                prefix = node_path_1.posix.dirname(prefix);
268            }
269        } while (prefix !== root && ret === undefined);
270        // at this point, found no resolution, just truncate
271        if (!ret) {
272            ret = [p.slice(0, pathSize - 1), '', true];
273        }
274    }
275    return ret;
276};
277const decString = (buf, off, size) => buf
278    .subarray(off, off + size)
279    .toString('utf8')
280    .replace(/\0.*/, '');
281const decDate = (buf, off, size) => numToDate(decNumber(buf, off, size));
282const numToDate = (num) => num === undefined ? undefined : new Date(num * 1000);
283const decNumber = (buf, off, size) => Number(buf[off]) & 0x80 ?
284    large.parse(buf.subarray(off, off + size))
285    : decSmallNumber(buf, off, size);
286const nanUndef = (value) => (isNaN(value) ? undefined : value);
287const decSmallNumber = (buf, off, size) => nanUndef(parseInt(buf
288    .subarray(off, off + size)
289    .toString('utf8')
290    .replace(/\0.*$/, '')
291    .trim(), 8));
292// the maximum encodable as a null-terminated octal, by field size
293const MAXNUM = {
294    12: 0o77777777777,
295    8: 0o7777777,
296};
297const encNumber = (buf, off, size, num) => num === undefined ? false
298    : num > MAXNUM[size] || num < 0 ?
299        (large.encode(num, buf.subarray(off, off + size)), true)
300        : (encSmallNumber(buf, off, size, num), false);
301const encSmallNumber = (buf, off, size, num) => buf.write(octalString(num, size), off, size, 'ascii');
302const octalString = (num, size) => padOctal(Math.floor(num).toString(8), size);
303const padOctal = (str, size) => (str.length === size - 1 ?
304    str
305    : new Array(size - str.length - 1).join('0') + str + ' ') + '\0';
306const encDate = (buf, off, size, date) => date === undefined ? false : (encNumber(buf, off, size, date.getTime() / 1000));
307// enough to fill the longest string we've got
308const NULLS = new Array(156).join('\0');
309// pad with nulls, return true if it's longer or non-ascii
310const encString = (buf, off, size, str) => str === undefined ? false : ((buf.write(str + NULLS, off, size, 'utf8'),
311    str.length !== Buffer.byteLength(str) || str.length > size));
312//# sourceMappingURL=header.js.map
codekingpro/portable-devtools · Team Ai