UNPKG

tar

Version:
159 lines • 4.92 kB
import { dealias, } from './dealias.js'; import { TarReader, TarReaderSync } from './entries.js'; import { Pack, PackSync, } from './pack.js'; import { readFile, readFileSync } from './read-file.js'; import { toStream } from './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. */ export 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(); }; /** The sync form of `writeChunks`. */ export 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); }; const createFile = async (pack, fs, file, mode) => { const handle = await fs.open(file, 'w', mode); let ok = false; try { await 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. */ export const packFiles = (opt, files) => { const { fs, strict, onwarn, codecs, platform } = opt; const pack = new 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 TarReader(readFile(fs, archive, opt.maxReadSize), { strict, onwarn, codecs, platform, file: archive, })); } } return pack.end(); }; /** The sync form of `packFiles`. */ export const packFilesSync = (opt, files) => { const { strict, onwarn, codecs, platform } = opt; const pack = new 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 TarReaderSync(readFileSync(opt.fs, archive, opt.maxReadSize), { strict, onwarn, codecs, platform, file: archive })); } } return pack; }; export function create(options, files) { const opt = dealias(options); if (!opt.file) { check(opt, files); return toStream(packFiles(opt, files)); } // The file form returns a promise, so a bad argument rejects it. try { check(opt, files); return createFile(packFiles(opt, files), opt.fs, opt.file, opt.mode || 0o666); } catch (er) { return Promise.reject(er); } } export function createSync(options, files) { const opt = dealias(options); check(opt, files); const { file } = opt; const fs = opt.fs; const pack = packFilesSync(opt, files); if (!file) return pack; const fd = fs.openSync(file, 'w', opt.mode || 0o666); try { writeChunksSync(fs, fd, pack, 0); } catch (er) { try { fs.closeSync(fd); } catch { } throw er; } fs.closeSync(fd); } //# sourceMappingURL=create.js.map