Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function pathToArray(path) {
path = normalize(path);
var nix = /^\//.test(path);
if(!nix) {
if(!/^[A-Za-z]:/.test(path)) {
throw new MemoryFileSystemError(errors.code.EINVAL, path);
}
path = path.replace(/[\\\/]+/g, "\\"); // multi slashs
path = path.split(/[\\\/]/);
path[0] = path[0].toUpperCase();
} else {
path = path.replace(/\/+/g, "/"); // multi slashs
path = path.substr(1).split("/");
}
if(!path[path.length-1]) path.pop();
return path;
}
function pathToArray(path) {
path = normalize(path);
var nix = /^\//.test(path);
if(!nix) {
if(!/^[A-Za-z]:/.test(path)) {
throw new MemoryFileSystemError(errors.code.EINVAL, path);
}
path = path.replace(/[\\\/]+/g, "\\"); // multi slashs
path = path.split(/[\\\/]/);
path[0] = path[0].toUpperCase();
} else {
path = path.replace(/\/+/g, "/"); // multi slashs
path = path.substr(1).split("/");
}
if(!path[path.length-1]) path.pop();
return path;
}
function pathToArray(path) {
path = normalize(path);
var nix = /^\//.test(path);
if(!nix) {
if(!/^[A-Za-z]:/.test(path)) {
throw new MemoryFileSystemError(errors.code.EINVAL, path);
}
path = path.replace(/[\\\/]+/g, "\\"); // multi slashs
path = path.split(/[\\\/]/);
path[0] = path[0].toUpperCase();
} else {
path = path.replace(/\/+/g, "/"); // multi slashs
path = path.substr(1).split("/");
}
if(!path[path.length-1]) path.pop();
return path;
}
function pathToArray(path) {
path = normalize(path);
const nix = /^\//.test(path);
if(!nix) {
if(!/^[A-Za-z]:/.test(path)) {
throw new MemoryFileSystemError(errors.code.EINVAL, path);
}
path = path.replace(/[\\\/]+/g, "\\"); // multi slashs
path = path.split(/[\\\/]/);
path[0] = path[0].toUpperCase();
} else {
path = path.replace(/\/+/g, "/"); // multi slashs
path = path.substr(1).split("/");
}
if(!path[path.length-1]) path.pop();
return path;
}
function pathToArray(path) {
path = normalize(path);
var nix = /^\//.test(path);
if(!nix) {
if(!/^[A-Za-z]:/.test(path)) {
throw new MemoryFileSystemError(errors.code.EINVAL, path);
}
path = path.replace(/[\\\/]+/g, "\\"); // multi slashs
path = path.split(/[\\\/]/);
path[0] = path[0].toUpperCase();
} else {
path = path.replace(/\/+/g, "/"); // multi slashs
path = path.substr(1).split("/");
}
if(!path[path.length-1]) path.pop();
return path;
}
_remove(_path, name, testFn) {
const path = pathToArray(_path);
const operation = name === "File" ? "unlink" : "rmdir";
if(path.length === 0) {
throw new MemoryFileSystemError(errors.code.EPERM, _path, operation);
}
let current = this.data;
let i = 0;
for(; i < path.length - 1; i++) {
if(!isDir(current[path[i]]))
throw new MemoryFileSystemError(errors.code.ENOENT, _path, operation);
current = current[path[i]];
}
if(!testFn(current[path[i]]))
throw new MemoryFileSystemError(errors.code.ENOENT, _path, operation);
delete current[path[i]];
return;
}
MemoryFileSystem.prototype.mkdirSync = function(_path) {
var path = pathToArray(_path);
if(path.length === 0) return;
var current = this.data;
for(var i = 0; i < path.length - 1; i++) {
if(!isDir(current[path[i]]))
throw new MemoryFileSystemError(errors.code.ENOENT, _path);
current = current[path[i]];
}
if(isDir(current[path[i]]))
throw new MemoryFileSystemError(errors.code.EEXIST, _path);
else if(isFile(current[path[i]]))
throw new MemoryFileSystemError(errors.code.ENOTDIR, _path);
current[path[i]] = {"":true};
return;
};
isSymbolicLink: falseFn,
isFIFO: falseFn,
isSocket: falseFn
};
} else if(isFile(current)) {
return {
isFile: trueFn,
isDirectory: falseFn,
isBlockDevice: falseFn,
isCharacterDevice: falseFn,
isSymbolicLink: falseFn,
isFIFO: falseFn,
isSocket: falseFn
};
} else {
throw new MemoryFileSystemError(errors.code.ENOENT, _path, "stat");
}
}
MemoryFileSystem.prototype.readlinkSync = function(_path) {
throw new MemoryFileSystemError(errors.code.ENOSYS, _path);
};
function fsError(code, path) {
var errorType = errno.code[code];
var message = errorType.code + ', ' + errorType.description + ' ' + path;
var error = new Error(message);
error.errno = errorType.errno;
error.code = errorType.code;
error.path = path;
return error;
}