CoreFileSystemSyncAccessHandle.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.CoreFileSystemSyncAccessHandle = void 0;
  4. const buffer_1 = require("../internal/buffer");
  5. const util_1 = require("./util");
  6. const constants_1 = require("../node/constants");
  7. /**
  8. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle
  9. */
  10. class CoreFileSystemSyncAccessHandle {
  11. constructor(_core, _path, _ctx) {
  12. this._core = _core;
  13. this._path = _path;
  14. this._ctx = _ctx;
  15. this._fd = null;
  16. this._closed = false;
  17. }
  18. _ensureOpen() {
  19. if (this._closed) {
  20. throw new DOMException('The file handle is closed.', 'InvalidStateError');
  21. }
  22. if (this._fd === null) {
  23. // Open file for read/write
  24. const flags = this._ctx.mode === 'readwrite' ? constants_1.FLAGS['r+'] : constants_1.FLAGS.r;
  25. this._fd = this._core.open(this._path, flags, 0o644);
  26. }
  27. return this._fd;
  28. }
  29. /**
  30. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/close
  31. */
  32. async close() {
  33. if (this._fd !== null) {
  34. this._core.close(this._fd);
  35. this._fd = null;
  36. }
  37. this._closed = true;
  38. }
  39. /**
  40. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/flush
  41. */
  42. async flush() {
  43. const fd = this._ensureOpen();
  44. // Core doesn't have an explicit flush method, but we can try to sync if available
  45. // For now, this is a no-op as the core writes are synchronous
  46. }
  47. /**
  48. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/getSize
  49. */
  50. async getSize() {
  51. try {
  52. const link = this._core.getResolvedLinkOrThrow(this._path);
  53. const node = link.getNode();
  54. return node.getSize();
  55. }
  56. catch (error) {
  57. if (error && typeof error === 'object' && error.code === "EACCES" /* ERROR_CODE.EACCES */) {
  58. throw (0, util_1.newNotAllowedError)();
  59. }
  60. throw error;
  61. }
  62. }
  63. /**
  64. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/read
  65. */
  66. async read(buffer, options = {}) {
  67. const fd = this._ensureOpen();
  68. const { at: position = 0 } = options;
  69. const buf = buffer_1.Buffer.from(buffer);
  70. try {
  71. return this._core.read(fd, buf, 0, buf.length, position);
  72. }
  73. catch (error) {
  74. if (error && typeof error === 'object' && error.code === "EACCES" /* ERROR_CODE.EACCES */) {
  75. throw (0, util_1.newNotAllowedError)();
  76. }
  77. throw error;
  78. }
  79. }
  80. /**
  81. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/truncate
  82. */
  83. async truncate(newSize) {
  84. if (this._ctx.mode !== 'readwrite') {
  85. throw (0, util_1.newNotAllowedError)();
  86. }
  87. try {
  88. const link = this._core.getResolvedLinkOrThrow(this._path);
  89. const node = link.getNode();
  90. node.truncate(newSize);
  91. }
  92. catch (error) {
  93. if (error && typeof error === 'object' && error.code === "EACCES" /* ERROR_CODE.EACCES */) {
  94. throw (0, util_1.newNotAllowedError)();
  95. }
  96. throw error;
  97. }
  98. }
  99. /**
  100. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/write
  101. */
  102. async write(buffer, options = {}) {
  103. if (this._ctx.mode !== 'readwrite') {
  104. throw (0, util_1.newNotAllowedError)();
  105. }
  106. const fd = this._ensureOpen();
  107. const { at: position = 0 } = options;
  108. const buf = buffer_1.Buffer.from(buffer);
  109. try {
  110. return this._core.write(fd, buf, 0, buf.length, position);
  111. }
  112. catch (error) {
  113. if (error && typeof error === 'object' && error.code === "EACCES" /* ERROR_CODE.EACCES */) {
  114. throw (0, util_1.newNotAllowedError)();
  115. }
  116. throw error;
  117. }
  118. }
  119. }
  120. exports.CoreFileSystemSyncAccessHandle = CoreFileSystemSyncAccessHandle;
  121. //# sourceMappingURL=CoreFileSystemSyncAccessHandle.js.map