NodeFileSystemFileHandle.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 () => new NodeFileSystemSyncAccessHandle_1.NodeFileSystemSyncAccessHandle(this.fs, this.__path, this.ctx);
  52. }
  53. /**
  54. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/createWritable
  55. */
  56. async createWritable({ keepExistingData = false } = { keepExistingData: false }) {
  57. (0, util_1.assertCanWrite)(this.ctx.mode);
  58. return new NodeFileSystemWritableFileStream_1.NodeFileSystemWritableFileStream(this.fs, this.__path, keepExistingData);
  59. }
  60. }
  61. exports.NodeFileSystemFileHandle = NodeFileSystemFileHandle;
  62. //# sourceMappingURL=NodeFileSystemFileHandle.js.map