extract-base-iterator
Version:
Base iterator for extract iterators like tar-iterator and zip-iterator
59 lines • 2.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, // Backward compatible: waitForAccess(path, callback) or waitForAccess(path, noFollow, callback)
"default", {
enumerable: true,
get: function() {
return waitForAccess;
}
});
var _gracefulfs = /*#__PURE__*/ _interop_require_default(require("graceful-fs"));
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function waitForAccess(fullPath, noFollow, callback) {
callback = typeof noFollow === 'function' ? noFollow : callback;
noFollow = typeof noFollow === 'function' ? false : noFollow;
// Exponential backoff: 5, 10, 20, 40, 80, 160, 320, 640, 1280, 2560ms
// Total max wait: ~5 seconds
function waitSymlink(attempts, cb) {
_gracefulfs.default.lstat(fullPath, function(err) {
if (err) {
if (err.code === 'ENOENT' && attempts < 10) {
var delay = Math.min(5 * Math.pow(2, attempts), 2560);
return setTimeout(function() {
return waitSymlink(attempts + 1, cb);
}, delay);
}
return cb(err);
}
cb();
});
}
function waitOpen(attempts, cb) {
_gracefulfs.default.open(fullPath, 'r', function(err, fd) {
if (err) {
if (err.code === 'ENOENT' && attempts < 10) {
var delay = Math.min(5 * Math.pow(2, attempts), 2560);
return setTimeout(function() {
return waitOpen(attempts + 1, cb);
}, delay);
}
return cb(err);
}
_gracefulfs.default.close(fd, function() {
return cb();
});
});
}
// Windows: NTFS metadata may not be committed yet, verify accessibility
// Node 0.10: the write stream's finish/close events may fire before the file is fully flushed to disk
// For symlinks (noFollow=true), use lstat to check the link itself exists
// For files/dirs/hardlinks, use open to verify the file is accessible
noFollow ? waitSymlink(0, callback) : waitOpen(0, callback);
}
/* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }