util.js 2.2 KB

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