UNPKG

tar

Version:
287 lines • 8.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ReadEntrySync = exports.ReadEntry = exports.EntryData = void 0; const concat_js_1 = require("./concat.js"); // The types that are an object of the file system. const fsTypes = new Set([ 'File', 'OldFile', 'Link', 'SymbolicLink', 'CharacterDevice', 'BlockDevice', 'Directory', 'FIFO', 'ContiguousFile', 'GNUDumpDir', ]); const winPath = (path) => path.replaceAll('\\', '/'); /** * The metadata of one archive entry: the header fields, with the * values of the PAX and GNU extended headers applied. */ class EntryData { /** The header block of the entry. */ header; /** The PAX header that applies to this entry only. */ extended; /** The global PAX header in effect. */ globalExtended; type; path; /** The permission bits only. */ mode; uid; gid; uname; gname; /** The number of bytes in the body. */ size; mtime; atime; ctime; linkpath; devmaj; devmin; dev; ino; nlink; /** The absolute path of the entry, when a consumer resolves it. */ absolute; /** * The body is skipped. Set for a type that is not an object of the * file system, and for an entry that the filter refuses. */ ignore; /** A consumer sets this when it refuses the entry. */ invalid = false; /** A consumer sets this when it does not support the type. */ unsupported = false; /** * @param win32 change each `\` in `path` and `linkpath` to `/` */ constructor(header, ex, gex, win32 = false) { if (!header.path) { throw new Error('no path provided for tar.ReadEntry'); } this.header = header; this.extended = ex; this.globalExtended = gex; this.type = header.type; this.ignore = !fsTypes.has(this.type); this.path = win32 ? winPath(header.path) : header.path; this.mode = header.mode && header.mode & 0o7777; this.uid = header.uid; this.gid = header.gid; this.uname = header.uname; this.gname = header.gname; this.size = header.size ?? 0; this.mtime = header.mtime; this.atime = header.atime; this.ctime = header.ctime; this.linkpath = header.linkpath ? win32 ? winPath(header.linkpath) : header.linkpath : undefined; this.devmaj = header.devmaj; this.devmin = header.devmin; // Apply the global values first, so that the local values win. // The header already has the size. if (gex) this.#apply(gex); if (ex) { this.#apply(ex); // A global header does not set a path or a linkpath. if (ex.path != null) this.path = win32 ? winPath(ex.path) : ex.path; if (ex.linkpath != null) { this.linkpath = win32 ? winPath(ex.linkpath) : ex.linkpath; } } } #apply(ex) { if (ex.uid != null) this.uid = ex.uid; if (ex.gid != null) this.gid = ex.gid; if (ex.uname != null) this.uname = ex.uname; if (ex.gname != null) this.gname = ex.gname; if (ex.mtime != null) this.mtime = ex.mtime; if (ex.atime != null) this.atime = ex.atime; if (ex.ctime != null) this.ctime = ex.ctime; if (ex.dev != null) this.dev = ex.dev; if (ex.ino != null) this.ino = ex.ino; if (ex.nlink != null) this.nlink = ex.nlink; } } exports.EntryData = EntryData; // As `Response.text()` does, this decoder removes a byte order mark // at the start of the body. const decoder = new TextDecoder(); const decode = (parts) => { let text = ''; for (const part of parts) text += decoder.decode(part, { stream: true }); return text + decoder.decode(); }; const usedError = () => new TypeError('Body has already been consumed'); /** * An archive entry with its body, in the shape of a `Response`. The * body can be consumed one time, in one way. It must be consumed * before the next entry is read: after that, the rest of it is gone. */ class ReadEntry extends EntryData { #source; #used = false; #stream; constructor(header, ex, gex, win32, source) { super(header, ex, gex, win32); this.#source = source; } /** True when the body started to be consumed. */ get bodyUsed() { return this.#used; } /** * The body as a stream. Each chunk is a view into the input, not a * copy. Input is read only when the stream is read. */ get body() { if (this.#stream) return this.#stream; let started = false; return (this.#stream = new ReadableStream({ pull: async (controller) => { if (!started) { this.#claim(); started = true; } const [part] = await this.#read(1); if (part) controller.enqueue(part); else controller.close(); }, }, { highWaterMark: 0 })); } #claim() { if (this.#used) throw usedError(); this.#used = true; } // Add the parts of the body that are in memory to `parts`, up to // `max` parts. False if the body has more parts that need input. #collect(parts, max = Infinity) { const source = this.#source; while (source && parts.length < max) { const part = source.readBody(this); if (part === null) break; if (part === undefined) return false; parts.push(part); } return true; } // Read all the parts of the body that are left, or `max` of them. async #read(max) { const parts = []; while (!this.#collect(parts, max)) await this.#source?.fill(); return parts; } /** The body in a new `Uint8Array`. */ async bytes() { this.#claim(); const parts = []; while (!this.#collect(parts)) await this.#source?.fill(); return (0, concat_js_1.concat)(parts); } /** The body in a new `ArrayBuffer`. */ async arrayBuffer() { return (await this.bytes()).buffer; } /** * The body, decoded as UTF-8. As with `Response.text()`, a byte * order mark at the start is not in the text. */ async text() { this.#claim(); return decode(await this.#read()); } /** The body in a new `Blob`. */ async blob() { this.#claim(); return new Blob((await this.#read())); } } exports.ReadEntry = ReadEntry; /** * An archive entry with its body, for sync code. The body can be * consumed one time, in one way. It must be consumed before the next * entry is read: after that, the rest of it is gone. */ class ReadEntrySync extends EntryData { #source; #used = false; #body; constructor(header, ex, gex, win32, source) { super(header, ex, gex, win32); this.#source = source; } /** True when the body started to be consumed. */ get bodyUsed() { return this.#used; } /** * The parts of the body. Each one is a view into the input, not a * copy. Input is read only when the iterator is read. */ get body() { return (this.#body ??= this.#iterate()); } *#iterate() { this.#claim(); for (let part; (part = this.#source?.readBody(this));) yield part; } #claim() { if (this.#used) throw usedError(); this.#used = true; } #read() { this.#claim(); const parts = []; for (let part; (part = this.#source?.readBody(this));) parts.push(part); return parts; } /** The body in a new `Uint8Array`. */ bytes() { return (0, concat_js_1.concat)(this.#read()); } /** The body in a new `ArrayBuffer`. */ arrayBuffer() { return this.bytes().buffer; } /** * The body, decoded as UTF-8. A byte order mark at the start is not * in the text. */ text() { return decode(this.#read()); } } exports.ReadEntrySync = ReadEntrySync; //# sourceMappingURL=read-entry.js.map