CoreFileSystemSyncAccessHandle.js 4.3 KB

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