NodeFileSystemFileHandle.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.NodeFileSystemFileHandle = void 0;
  4. const NodeFileSystemHandle_1 = require("./NodeFileSystemHandle");
  5. const NodeFileSystemSyncAccessHandle_1 = require("./NodeFileSystemSyncAccessHandle");
  6. const util_1 = require("./util");
  7. const NodeFileSystemWritableFileStream_1 = require("./NodeFileSystemWritableFileStream");
  8. class NodeFileSystemFileHandle extends NodeFileSystemHandle_1.NodeFileSystemHandle {
  9. constructor(fs, __path, ctx = {}) {
  10. ctx = (0, util_1.ctx)(ctx);
  11. super('file', (0, util_1.basename)(__path, ctx.separator));
  12. this.fs = fs;
  13. this.__path = __path;
  14. this.ctx = ctx;
  15. }
  16. /**
  17. * Returns a {@link Promise} which resolves to a {@link File} object
  18. * representing the state on disk of the entry represented by the handle.
  19. *
  20. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/getFile
  21. */
  22. async getFile() {
  23. try {
  24. const path = this.__path;
  25. const promises = this.fs.promises;
  26. const stats = await promises.stat(path);
  27. // TODO: Once implemented, use promises.readAsBlob() instead of promises.readFile().
  28. const data = await promises.readFile(path);
  29. const file = new File([data], this.name, { lastModified: stats.mtime.getTime() });
  30. return file;
  31. }
  32. catch (error) {
  33. if (error instanceof DOMException)
  34. throw error;
  35. if (error && typeof error === 'object') {
  36. switch (error.code) {
  37. case 'EPERM':
  38. case 'EACCES':
  39. throw (0, util_1.newNotAllowedError)();
  40. }
  41. }
  42. throw error;
  43. }
  44. }
  45. /**
  46. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/createSyncAccessHandle
  47. */
  48. get createSyncAccessHandle() {
  49. if (!this.ctx.syncHandleAllowed)
  50. return undefined;
  51. return async () => {
  52. if (this.ctx.locks.isLocked(this.__path)) {
  53. throw (0, util_1.newNoModificationAllowedError)();
  54. }
  55. return new NodeFileSystemSyncAccessHandle_1.NodeFileSystemSyncAccessHandle(this.fs, this.__path, this.ctx);
  56. };
  57. }
  58. /**
  59. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/createWritable
  60. */
  61. async createWritable({ keepExistingData = false } = { keepExistingData: false }) {
  62. (0, util_1.assertCanWrite)(this.ctx.mode);
  63. if (this.ctx.locks.isLocked(this.__path)) {
  64. throw (0, util_1.newNoModificationAllowedError)();
  65. }
  66. return new NodeFileSystemWritableFileStream_1.NodeFileSystemWritableFileStream(this.fs, this.__path, keepExistingData, this.ctx);
  67. }
  68. }
  69. exports.NodeFileSystemFileHandle = NodeFileSystemFileHandle;
  70. //# sourceMappingURL=NodeFileSystemFileHandle.js.map