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

Method writeSync

lib/internal/vfs/file_handle.js:510–552  ·  view source on GitHub ↗

* Writes data to the file synchronously. * @param {Buffer} buffer The buffer to write from * @param {number} offset The offset in the buffer to start reading * @param {number} length The number of bytes to write * @param {number|null} position The position to write to (null uses current

(buffer, offset, length, position)

Source from the content-addressed store, hash-verified

508 * @returns {number} The number of bytes written
509 */
510 writeSync(buffer, offset, length, position) {
511 this.#checkClosed('write');
512 this.#checkWritable();
513
514 // In append mode, always write at the end
515 const writePos = this.#isAppend() ?
516 this.#size :
517 (position !== null && position !== undefined ?
518 Number(position) : this.position);
519 const data = buffer.subarray(offset, offset + length);
520
521 // Expand buffer if needed (geometric doubling for amortized O(1) appends)
522 const neededSize = writePos + length;
523 if (neededSize > this.#content.length) {
524 const newCapacity = MathMax(neededSize, this.#content.length * 2);
525 const newContent = Buffer.alloc(newCapacity);
526 this.#content.copy(newContent, 0, 0, this.#size);
527 this.#content = newContent;
528 }
529
530 // Write the data
531 data.copy(this.#content, writePos);
532
533 // Update actual content size
534 if (neededSize > this.#size) {
535 this.#size = neededSize;
536 }
537
538 // Update the entry's content, mtime, and ctime
539 if (this.#entry) {
540 const now = DateNow();
541 this.#entry.content = this.#content.subarray(0, this.#size);
542 this.#entry.mtime = now;
543 this.#entry.ctime = now;
544 }
545
546 // Update position if not using explicit position
547 if (position === null || position === undefined) {
548 this.position = writePos + length;
549 }
550
551 return length;
552 }
553
554 /**
555 * Writes data to the file.

Callers 4

writeMethod · 0.95
writeSyncFunction · 0.45
writevSyncFunction · 0.45
_writeMethod · 0.45

Calls 5

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

Tested by

no test coverage detected