MCPcopy Index your code
hub / github.com/nodejs/node / writeFileSync

Method writeFileSync

lib/internal/vfs/file_handle.js:609–640  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

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).

Callers 1

writeFileMethod · 0.95

Calls 6

#checkClosedMethod · 0.95
#checkWritableMethod · 0.95
#isAppendMethod · 0.95
allocMethod · 0.80
copyMethod · 0.65
fromMethod · 0.45

Tested by

no test coverage detected