UNPKG

tar

Version:
168 lines • 5.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.packFilesSync = exports.packFiles = exports.writeChunksSync = exports.writeChunks = void 0; exports.create = create; exports.createSync = createSync; const dealias_js_1 = require("./dealias.js"); const entries_js_1 = require("./entries.js"); const pack_js_1 = require("./pack.js"); const read_file_js_1 = require("./read-file.js"); const to_stream_js_1 = require("./to-stream.js"); // Chunks that are smaller than this are put together for one write. const SMALL = 64 * 1024; const BATCH = 4 * SMALL; const noop = () => { }; const check = (opt, files) => { if (!files?.length) { throw new TypeError('no paths specified to add to archive'); } if (opt.file && !opt.fs) { throw new TypeError('the file option needs the fs option'); } }; const writeAll = async (handle, buf, length, position) => { for (let off = 0; off < length;) { off += (await handle.write(buf, off, length - off, position + off)) .bytesWritten; } }; /** * Write the chunks to the file, from `position`. Chunks that are * smaller than 64 KiB are put together for one write. */ const writeChunks = async (handle, chunks, position) => { const batch = new Uint8Array(BATCH); let used = 0; const flush = async () => { await writeAll(handle, batch, used, position); position += used; used = 0; }; for await (const chunk of chunks) { if (used + chunk.length > BATCH || chunk.length >= SMALL) await flush(); if (chunk.length >= SMALL) { await writeAll(handle, chunk, chunk.length, position); position += chunk.length; } else { batch.set(chunk, used); used += chunk.length; } } await flush(); }; exports.writeChunks = writeChunks; /** The sync form of `writeChunks`. */ const writeChunksSync = (fs, fd, chunks, position) => { const batch = new Uint8Array(BATCH); let used = 0; const write = (buf, length) => { for (let off = 0; off < length;) { off += fs.writeSync(fd, buf, off, length - off, position + off); } position += length; }; for (const chunk of chunks) { if (used + chunk.length > BATCH || chunk.length >= SMALL) { write(batch, used); used = 0; } if (chunk.length >= SMALL) write(chunk, chunk.length); else { batch.set(chunk, used); used += chunk.length; } } write(batch, used); }; exports.writeChunksSync = writeChunksSync; const createFile = async (pack, fs, file, mode) => { const handle = await fs.open(file, 'w', mode); let ok = false; try { await (0, exports.writeChunks)(handle, pack, 0); ok = true; } finally { const closed = handle.close(); await (ok ? closed : closed.catch(noop)); } }; /** * Make the pack for the files of a command. A name that starts with * `@` is the path of an archive: the pack gets its entries. */ const packFiles = (opt, files) => { const { fs, strict, onwarn, codecs, platform } = opt; const pack = new pack_js_1.Pack(opt); for (const item of files) { if (typeof item !== 'string' || item.charAt(0) !== '@') pack.add(item); else { const archive = pack.resolve(item.slice(1)); pack.add(new entries_js_1.TarReader((0, read_file_js_1.readFile)(fs, archive, opt.maxReadSize), { strict, onwarn, codecs, platform, file: archive, })); } } return pack.end(); }; exports.packFiles = packFiles; /** The sync form of `packFiles`. */ const packFilesSync = (opt, files) => { const { strict, onwarn, codecs, platform } = opt; const pack = new pack_js_1.PackSync(opt); for (const item of files) { if (typeof item !== 'string' || item.charAt(0) !== '@') pack.add(item); else { const archive = pack.resolve(item.slice(1)); pack.add(new entries_js_1.TarReaderSync((0, read_file_js_1.readFileSync)(opt.fs, archive, opt.maxReadSize), { strict, onwarn, codecs, platform, file: archive })); } } return pack; }; exports.packFilesSync = packFilesSync; function create(options, files) { const opt = (0, dealias_js_1.dealias)(options); if (!opt.file) { check(opt, files); return (0, to_stream_js_1.toStream)((0, exports.packFiles)(opt, files)); } // The file form returns a promise, so a bad argument rejects it. try { check(opt, files); return createFile((0, exports.packFiles)(opt, files), opt.fs, opt.file, opt.mode || 0o666); } catch (er) { return Promise.reject(er); } } function createSync(options, files) { const opt = (0, dealias_js_1.dealias)(options); check(opt, files); const { file } = opt; const fs = opt.fs; const pack = (0, exports.packFilesSync)(opt, files); if (!file) return pack; const fd = fs.openSync(file, 'w', opt.mode || 0o666); try { (0, exports.writeChunksSync)(fs, fd, pack, 0); } catch (er) { try { fs.closeSync(fd); } catch { } throw er; } fs.closeSync(fd); } //# sourceMappingURL=create.js.map