All files / src/node-to-fsa util.ts

96.42% Statements 27/28
87.5% Branches 7/8
85.71% Functions 6/7
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46          46x 6917x               46x 6946x 6946x 6946x     46x   46x 9742x 9742x     46x 2486x 8x           46x 873x         46x 750x   46x  
import type { NodeFsaContext } from './types';
 
/**
 * Creates a new {@link NodeFsaContext}.
 */
export const ctx = (partial: Partial<NodeFsaContext> = {}): NodeFsaContext => {
  return {
    separator: '/',
    syncHandleAllowed: false,
    mode: 'read',
    ...partial,
  };
};
 
export const basename = (path: string, separator: string) => {
  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: string, method: string, klass: string) => {
  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: 'read' | 'readwrite') => {
  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');