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("../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. }
  16. /**
  17. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/close
  18. */
  19. async close() {
  20. (0, util_1.assertCanWrite)(this.ctx.mode);
  21. this.fs.closeSync(this.fd);
  22. }
  23. /**
  24. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/flush
  25. */
  26. async flush() {
  27. (0, util_1.assertCanWrite)(this.ctx.mode);
  28. this.fs.fsyncSync(this.fd);
  29. }
  30. /**
  31. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/getSize
  32. */
  33. async getSize() {
  34. return this.fs.statSync(this.path).size;
  35. }
  36. /**
  37. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/read
  38. */
  39. async read(buffer, options = {}) {
  40. var _a;
  41. const buf = buffer instanceof ArrayBuffer ? buffer_1.Buffer.from(buffer) : buffer;
  42. try {
  43. const size = this.fs.readSync(this.fd, buf, 0, buffer.byteLength, (_a = options.at) !== null && _a !== void 0 ? _a : 0);
  44. return size;
  45. }
  46. catch (error) {
  47. if (error instanceof DOMException)
  48. throw error;
  49. if (error && typeof error === 'object') {
  50. switch (error.code) {
  51. case 'EBADF': {
  52. throw new DOMException('File handle already closed.', 'InvalidStateError');
  53. }
  54. }
  55. }
  56. throw error;
  57. }
  58. }
  59. /**
  60. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/truncate
  61. * @param newSize The number of bytes to resize the file to.
  62. */
  63. async truncate(newSize) {
  64. (0, util_1.assertCanWrite)(this.ctx.mode);
  65. this.fs.truncateSync(this.fd, newSize);
  66. }
  67. /**
  68. * Writes the content of a specified buffer to the file associated with the
  69. * handle, optionally at a given offset.
  70. *
  71. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/write
  72. * @param buffer
  73. * @param options
  74. */
  75. async write(buffer, options = {}) {
  76. var _a;
  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, (_a = options.at) !== null && _a !== void 0 ? _a : 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