tar
Version:
tar for JavaScript
114 lines • 4.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.listSync = exports.list = exports.filesFilter = void 0;
const strip_trailing_slashes_js_1 = require("./strip-trailing-slashes.js");
const dealias_js_1 = require("./dealias.js");
const entries_js_1 = require("./entries.js");
const read_file_js_1 = require("./read-file.js");
const warn_js_1 = require("./warn.js");
/**
* Make a filter that passes the entries that `files` lists. An entry
* passes if `filter` accepts it, and it is in the list or one of its
* parent directories is. Trailing slashes are ignored.
*
* The second item of the result reports each listed path that no
* entry of the archive matched, as a `TAR_ENTRY_ERROR` warning. An
* entry that `filter` refuses also matches. Call it when the archive
* ends.
*/
const filesFilter = (files, filter) => {
const set = new Set(files.map(f => (0, strip_trailing_slashes_js_1.stripTrailingSlashes)(f)));
// The listed paths that no entry matched yet.
const missing = new Set(set);
let max = 0;
for (const f of set)
max = Math.max(max, f.length);
const listed = (path) => {
const p = (0, strip_trailing_slashes_js_1.stripTrailingSlashes)(path);
const root = p.charAt(0) === '/' ? '/' : '.';
let found = false;
// Look at the path, and then at each parent directory. One that
// is longer than each listed path is not looked up, so the work
// for a very long path stays small.
for (let end = p.length; end > 0; end = p.lastIndexOf('/', end - 1)) {
if (end > max)
continue;
const dir = p.slice(0, end);
if (dir === root || !set.has(dir))
continue;
found = true;
missing.delete(dir);
// A parent directory can also be in the list.
if (!missing.size)
break;
}
return found;
};
return [
(path, entry) => {
// The filter of the caller gets each entry.
const ok = !filter || filter(path, entry);
return listed(path) && ok;
},
opt => {
for (const path of missing) {
(0, warn_js_1.warn)(opt, 'TAR_ENTRY_ERROR', `${path}: not found in archive`, {
path,
});
}
},
];
};
exports.filesFilter = filesFilter;
const noInput = () => new TypeError('list needs input, or file and fs');
/**
* Call `opt.onEntry` with each entry of an archive. If `files` is
* set, only with the entries that it lists, and the entries in the
* directories that it lists. A listed path that no entry matches is
* a warning when the archive ends.
*/
const list = async (options, files) => {
const opt = (0, dealias_js_1.dealias)(options);
const { input, file, fs, onEntry } = opt;
const source = input ?? (file && fs ? (0, read_file_js_1.readFile)(fs, file, opt.maxReadSize) : undefined);
if (!source)
throw noInput();
const [filter, report] = files?.length ? (0, exports.filesFilter)(files, opt.filter) : [];
if (filter)
opt.filter = filter;
const reader = new entries_js_1.TarReader(source, opt);
try {
for (;;) {
const entry = reader.poll();
if (entry === null)
break;
if (entry === undefined)
await reader.fill();
else if (onEntry)
await onEntry(entry);
}
}
finally {
await reader.return();
}
report?.(opt);
};
exports.list = list;
/** The sync form of `list`. */
const listSync = (options, files) => {
const opt = (0, dealias_js_1.dealias)(options);
const { input, file, fs, onEntry } = opt;
const source = input ??
(file && fs ? (0, read_file_js_1.readFileSync)(fs, file, opt.maxReadSize) : undefined);
if (!source)
throw noInput();
const [filter, report] = files?.length ? (0, exports.filesFilter)(files, opt.filter) : [];
if (filter)
opt.filter = filter;
const reader = new entries_js_1.TarReaderSync(source, opt);
for (const entry of reader)
onEntry?.(entry);
report?.(opt);
};
exports.listSync = listSync;
//# sourceMappingURL=list.js.map