NodeFileSystemSyncAccessHandle.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.NodeFileSystemSyncAccessHandle = void 0;
  4. const util_1 = require("./util");
  5. const buffer_1 = require("@jsonjoy.com/fs-node-builtins/lib/internal/buffer");
  6. /**
  7. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle
  8. */
  9. class NodeFileSystemSyncAccessHandle {
  10. constructor(fs, path, ctx) {
  11. this.fs = fs;
  12. this.path = path;
  13. this.ctx = ctx;
  14. this.fd = fs.openSync(path, 'r+');
  15. this.ctx.locks.acquireLock(this.path);
  16. }
  17. /**
  18. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/close
  19. */
  20. async close() {
  21. (0, util_1.assertCanWrite)(this.ctx.mode);
  22. this.fs.closeSync(this.fd);
  23. this.ctx.locks.releaseLock(this.path);
  24. }
  25. /**
  26. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/flush
  27. */
  28. async flush() {
  29. (0, util_1.assertCanWrite)(this.ctx.mode);
  30. this.fs.fsyncSync(this.fd);
  31. }
  32. /**
  33. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/getSize
  34. */
  35. async getSize() {
  36. return this.fs.statSync(this.path).size;
  37. }
  38. /**
  39. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/read
  40. */
  41. async read(buffer, options = {}) {
  42. const buf = buffer instanceof ArrayBuffer ? buffer_1.Buffer.from(buffer) : buffer;
  43. try {
  44. const size = this.fs.readSync(this.fd, buf, 0, buffer.byteLength, options.at ?? 0);
  45. return size;
  46. }
  47. catch (error) {
  48. if (error instanceof DOMException)
  49. throw error;
  50. if (error && typeof error === 'object') {
  51. switch (error.code) {
  52. case 'EBADF': {
  53. throw new DOMException('File handle already closed.', 'InvalidStateError');
  54. }
  55. }
  56. }
  57. throw error;
  58. }
  59. }
  60. /**
  61. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/truncate
  62. * @param newSize The number of bytes to resize the file to.
  63. */
  64. async truncate(newSize) {
  65. (0, util_1.assertCanWrite)(this.ctx.mode);
  66. this.fs.truncateSync(this.fd, newSize);
  67. }
  68. /**
  69. * Writes the content of a specified buffer to the file associated with the
  70. * handle, optionally at a given offset.
  71. *
  72. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/write
  73. * @param buffer
  74. * @param options
  75. */
  76. async write(buffer, options = {}) {
  77. (0, util_1.assertCanWrite)(this.ctx.mode);
  78. const buf = buffer instanceof ArrayBuffer ? buffer_1.Buffer.from(buffer) : buffer;
  79. try {
  80. return this.fs.writeSync(this.fd, buf, 0, buffer.byteLength, options.at ?? 0);
  81. }
  82. catch (error) {
  83. if (error instanceof DOMException)
  84. throw error;
  85. if (error && typeof error === 'object') {
  86. switch (error.code) {
  87. case 'EBADF': {
  88. throw new DOMException('File handle already closed.', 'InvalidStateError');
  89. }
  90. }
  91. }
  92. throw error;
  93. }
  94. }
  95. }
  96. exports.NodeFileSystemSyncAccessHandle = NodeFileSystemSyncAccessHandle;
  97. //# sourceMappingURL=NodeFileSystemSyncAccessHandle.js.map