@ezdevlol/memfs
Version:
In-memory file-system with Node's fs API.
31 lines (30 loc) • 1.38 kB
JavaScript
/**
* Creates a new {@link NodeFsaContext}.
*/
export const ctx = (partial = {}) => {
return {
separator: '/',
syncHandleAllowed: false,
mode: 'read',
...partial,
};
};
export const basename = (path, separator) => {
if (path[path.length - 1] === separator)
path = path.slice(0, -1);
const lastSlashIndex = path.lastIndexOf(separator);
return lastSlashIndex === -1 ? path : path.slice(lastSlashIndex + 1);
};
const nameRegex = /^(\.{1,2})$|^(.*([\/\\]).*)$/;
export const assertName = (name, method, klass) => {
const isInvalid = !name || nameRegex.test(name);
if (isInvalid)
throw new TypeError(`Failed to execute '${method}' on '${klass}': Name is not allowed.`);
};
export const assertCanWrite = (mode) => {
if (mode !== 'readwrite')
throw new DOMException('The request is not allowed by the user agent or the platform in the current context.', 'NotAllowedError');
};
export const newNotFoundError = () => new DOMException('A requested file or directory could not be found at the time an operation was processed.', 'NotFoundError');
export const newTypeMismatchError = () => new DOMException('The path supplied exists, but was not an entry of requested type.', 'TypeMismatchError');
export const newNotAllowedError = () => new DOMException('Permission not granted.', 'NotAllowedError');