util.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.newNotAllowedError = exports.newTypeMismatchError = exports.newNotFoundError = exports.assertCanWrite = exports.assertName = exports.basename = exports.ctx = void 0;
  4. /**
  5. * Creates a new {@link NodeFsaContext}.
  6. */
  7. const ctx = (partial = {}) => {
  8. return Object.assign({ separator: '/', syncHandleAllowed: false, mode: 'read' }, partial);
  9. };
  10. exports.ctx = ctx;
  11. const basename = (path, separator) => {
  12. if (path[path.length - 1] === separator)
  13. path = path.slice(0, -1);
  14. const lastSlashIndex = path.lastIndexOf(separator);
  15. return lastSlashIndex === -1 ? path : path.slice(lastSlashIndex + 1);
  16. };
  17. exports.basename = basename;
  18. const nameRegex = /^(\.{1,2})$|^(.*([\/\\]).*)$/;
  19. const assertName = (name, method, klass) => {
  20. const isInvalid = !name || nameRegex.test(name);
  21. if (isInvalid)
  22. throw new TypeError(`Failed to execute '${method}' on '${klass}': Name is not allowed.`);
  23. };
  24. exports.assertName = assertName;
  25. const assertCanWrite = (mode) => {
  26. if (mode !== 'readwrite')
  27. throw new DOMException('The request is not allowed by the user agent or the platform in the current context.', 'NotAllowedError');
  28. };
  29. exports.assertCanWrite = assertCanWrite;
  30. const newNotFoundError = () => new DOMException('A requested file or directory could not be found at the time an operation was processed.', 'NotFoundError');
  31. exports.newNotFoundError = newNotFoundError;
  32. const newTypeMismatchError = () => new DOMException('The path supplied exists, but was not an entry of requested type.', 'TypeMismatchError');
  33. exports.newTypeMismatchError = newTypeMismatchError;
  34. const newNotAllowedError = () => new DOMException('Permission not granted.', 'NotAllowedError');
  35. exports.newNotAllowedError = newNotAllowedError;
  36. //# sourceMappingURL=util.js.map