* Writes data to the file synchronously. * Replaces content in 'w' mode, appends in 'a' mode. * @param {Buffer|string} data The data to write * @param {object} [options] Options
(data, options)
| 607 | * @param {object} [options] Options |
| 608 | */ |
| 609 | writeFileSync(data, options) { |
| 610 | this.#checkClosed('write'); |
| 611 | this.#checkWritable(); |
| 612 | |
| 613 | const buffer = typeof data === 'string' ? Buffer.from(data, options?.encoding) : data; |
| 614 | |
| 615 | // In append mode, append to existing content |
| 616 | if (this.#isAppend()) { |
| 617 | const neededSize = this.#size + buffer.length; |
| 618 | if (neededSize > this.#content.length) { |
| 619 | const newCapacity = MathMax(neededSize, this.#content.length * 2); |
| 620 | const newContent = Buffer.alloc(newCapacity); |
| 621 | this.#content.copy(newContent, 0, 0, this.#size); |
| 622 | this.#content = newContent; |
| 623 | } |
| 624 | buffer.copy(this.#content, this.#size); |
| 625 | this.#size = neededSize; |
| 626 | } else { |
| 627 | this.#content = Buffer.from(buffer); |
| 628 | this.#size = buffer.length; |
| 629 | } |
| 630 | |
| 631 | // Update the entry's content, mtime, and ctime |
| 632 | if (this.#entry) { |
| 633 | const now = DateNow(); |
| 634 | this.#entry.content = this.#content.subarray(0, this.#size); |
| 635 | this.#entry.mtime = now; |
| 636 | this.#entry.ctime = now; |
| 637 | } |
| 638 | |
| 639 | this.position = this.#size; |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Writes data to the file (replacing content). |
no test coverage detected