FsaNodeWriteStream.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.FsaNodeWriteStream = void 0;
  4. const node_stream_1 = require("node:stream");
  5. const Defer_1 = require("thingies/lib/Defer");
  6. const concurrency_1 = require("thingies/lib/concurrency");
  7. const util_1 = require("../node/util");
  8. const queueMicrotask_1 = require("../queueMicrotask");
  9. /**
  10. * This WriteStream implementation does not build on top of the `fs` module,
  11. * but instead uses the lower-level `FileSystemFileHandle` interface. The reason
  12. * is the different semantics in `fs` and FSA (File System Access API) write streams.
  13. *
  14. * When data is written to an FSA file, a new FSA stream is created, it copies
  15. * the file to a temporary swap file. After each written chunk, that swap file
  16. * is closed and the original file is replaced with the swap file. This means,
  17. * if WriteStream was built on top of `fs`, each chunk write would result in
  18. * a file copy, write, close, rename operations, which is not what we want.
  19. *
  20. * Instead this implementation hooks into the lower-level and closes the swap
  21. * file only once the stream is closed. The downside is that the written data
  22. * is not immediately visible to other processes (because it is written to the
  23. * swap file), but that is the trade-off we have to make.
  24. *
  25. * @todo Could make this flush the data to the original file periodically, so that
  26. * the data is visible to other processes.
  27. * @todo This stream could work through `FileSystemSyncAccessHandle.write` in a
  28. * Worker thread instead.
  29. */
  30. class FsaNodeWriteStream extends node_stream_1.Writable {
  31. constructor(handle, path, options) {
  32. super();
  33. this.path = path;
  34. this.options = options;
  35. this.__pending__ = true;
  36. this.__closed__ = false;
  37. this.__bytes__ = 0;
  38. this.__mutex__ = (0, concurrency_1.concurrency)(1);
  39. if (options.start !== undefined) {
  40. if (typeof options.start !== 'number') {
  41. throw new TypeError('"start" option must be a Number');
  42. }
  43. if (options.start < 0) {
  44. throw new TypeError('"start" must be >= zero');
  45. }
  46. }
  47. const stream = new Defer_1.Defer();
  48. this.__stream__ = stream.promise;
  49. (async () => {
  50. const fsaHandle = await handle;
  51. const fileWasOpened = !options.fd;
  52. if (fileWasOpened)
  53. this.emit('open', fsaHandle.fd);
  54. const flags = (0, util_1.flagsToNumber)(options.flags ?? 'w');
  55. const keepExistingData = flags & 1024 /* FLAG_CON.O_APPEND */ ? true : false;
  56. const writable = await fsaHandle.file.createWritable({ keepExistingData });
  57. if (keepExistingData) {
  58. const start = Number(options.start ?? 0);
  59. if (start)
  60. await writable.seek(start);
  61. }
  62. this.__pending__ = false;
  63. stream.resolve(writable);
  64. })().catch(error => {
  65. stream.reject(error);
  66. });
  67. }
  68. async ___write___(buffers) {
  69. await this.__mutex__(async () => {
  70. if (this.__closed__)
  71. return;
  72. // if (this.__closed__) throw new Error('WriteStream is closed');
  73. const writable = await this.__stream__;
  74. for (const buffer of buffers) {
  75. await writable.write(buffer);
  76. this.__bytes__ += buffer.byteLength;
  77. }
  78. });
  79. }
  80. async __close__() {
  81. const emitClose = this.options.emitClose;
  82. await this.__mutex__(async () => {
  83. if (this.__closed__ && emitClose) {
  84. (0, queueMicrotask_1.default)(() => this.emit('close'));
  85. return;
  86. }
  87. try {
  88. const writable = await this.__stream__;
  89. this.__closed__ = true;
  90. await writable.close();
  91. if (emitClose)
  92. this.emit('close');
  93. }
  94. catch (error) {
  95. this.emit('error', error);
  96. if (emitClose)
  97. this.emit('close', error);
  98. }
  99. });
  100. }
  101. // ------------------------------------------------------------- IWriteStream
  102. get bytesWritten() {
  103. return this.__bytes__;
  104. }
  105. get pending() {
  106. return this.__pending__;
  107. }
  108. close(cb) {
  109. if (cb)
  110. this.once('close', cb);
  111. this.__close__().catch(() => { });
  112. }
  113. // ----------------------------------------------------------------- Writable
  114. _write(chunk, encoding, callback) {
  115. this.___write___([chunk])
  116. .then(() => {
  117. if (callback)
  118. callback(null);
  119. })
  120. .catch(error => {
  121. if (callback)
  122. callback(error);
  123. });
  124. }
  125. _writev(chunks, callback) {
  126. const buffers = chunks.map(({ chunk }) => chunk);
  127. this.___write___(buffers)
  128. .then(() => {
  129. if (callback)
  130. callback(null);
  131. })
  132. .catch(error => {
  133. if (callback)
  134. callback(error);
  135. });
  136. }
  137. _final(callback) {
  138. this.__close__()
  139. .then(() => {
  140. if (callback)
  141. callback(null);
  142. })
  143. .catch(error => {
  144. if (callback)
  145. callback(error);
  146. });
  147. }
  148. }
  149. exports.FsaNodeWriteStream = FsaNodeWriteStream;
  150. //# sourceMappingURL=FsaNodeWriteStream.js.map