util.js 2.1 KB

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