extract-base-iterator
Version:
Base iterator for extract iterators like tar-iterator and zip-iterator
49 lines (48 loc) • 2.1 kB
JavaScript
import mkdirp from 'mkdirp-classic';
import path from 'path';
import Queue from 'queue-cb';
import chmod from './fs/chmod.js';
import chown from './fs/chown.js';
import utimes from './fs/utimes.js';
import { objectAssign } from './shared/index.js';
import safeJoinPath from './shared/safeJoinPath.js';
import stripPath from './shared/stripPath.js';
import validateAttributes from './validateAttributes.js';
import waitForAccess from './waitForAccess.js';
const MANDATORY_ATTRIBUTES = [
'mode',
'mtime',
'path'
];
let DirectoryEntry = class DirectoryEntry {
create(dest, options, callback) {
callback = typeof options === 'function' ? options : callback;
options = typeof options === 'function' ? {} : options || {};
if (typeof callback === 'function') {
try {
const normalizedPath = path.normalize(this.path);
const fullPath = safeJoinPath(dest, stripPath(normalizedPath, options));
// do not check for the existence of the directory but allow out-of-order calling
const queue = new Queue(1);
queue.defer((cb)=>mkdirp(fullPath, (err)=>cb(err)));
queue.defer((cb)=>waitForAccess(fullPath, cb));
queue.defer((cb)=>chmod(fullPath, this, options, (err)=>cb(err)));
queue.defer((cb)=>chown(fullPath, this, options, (err)=>cb(err)));
queue.defer((cb)=>utimes(fullPath, this, options, (err)=>cb(err)));
queue.await(callback);
} catch (err) {
callback(err);
}
return;
}
return new Promise((resolve, reject)=>this.create(dest, options, (err)=>err ? reject(err) : resolve(true)));
}
destroy() {}
constructor(attributes){
validateAttributes(attributes, MANDATORY_ATTRIBUTES);
objectAssign(this, attributes);
if (this.type === undefined) this.type = 'directory';
if (this.basename === undefined) this.basename = path.basename(this.path);
}
};
export { DirectoryEntry as default };