@@ -696,11 +696,17 @@ close the `FileHandle` automatically. User code must still call the
696696
697697<!-- YAML
698698added: v10.0.0
699+ changes:
700+ - version: REPLACEME
701+ pr-url: https://github.com/nodejs/node/pull/63634
702+ description: Added support for the ` buffer` option.
699703-->
700704
701705* ` options` {Object|string}
702706 * ` encoding` {string|null} **Default:** ` null `
703707 * ` signal` {AbortSignal} allows aborting an in-progress readFile
708+ * ` buffer` {Buffer|TypedArray|DataView|Function} A buffer to read into, or a
709+ function called with the file size that returns the buffer.
704710* Returns: {Promise} Fulfills upon a successful read with the contents of the
705711 file. If no encoding is specified (using ` options .encoding ` ), the data is
706712 returned as a {Buffer} object. Otherwise, the data will be a string.
@@ -709,13 +715,51 @@ Asynchronously reads the entire contents of a file.
709715
710716If ` options` is a string, then it specifies the ` encoding` .
711717
718+ If ` buffer` is provided and no encoding is specified, the returned {Buffer} is
719+ a view over the supplied buffer containing only the bytes read. If the
720+ supplied buffer is too small to contain the entire file, the operation will
721+ fail.
722+
712723The {FileHandle} has to support reading.
713724
714725If one or more ` filehandle .read ()` calls are made on a file handle and then a
715726` filehandle .readFile ()` call is made, the data will be read from the current
716727position till the end of the file. It doesn't always read from the beginning
717728of the file.
718729
730+ An example using the ` buffer` option with a pre-allocated buffer:
731+
732+ ` ` ` mjs
733+ import { Buffer } from ' node:buffer' ;
734+ import { open } from ' node:fs/promises' ;
735+
736+ const file = await open (' ./some/file/to/read' );
737+ try {
738+ const buf = Buffer .alloc (16384 );
739+ const contents = await file .readFile ({ buffer: buf });
740+ console .log (contents); // A view over `buf` containing only the bytes read
741+ } finally {
742+ await file .close ();
743+ }
744+ ` ` `
745+
746+ An example using the ` buffer` option with a function returning a buffer:
747+
748+ ` ` ` mjs
749+ import { Buffer } from ' node:buffer' ;
750+ import { open } from ' node:fs/promises' ;
751+
752+ const file = await open (' ./some/file/to/read' );
753+ try {
754+ const contents = await file .readFile ({
755+ buffer : (size ) => Buffer .alloc (size),
756+ });
757+ console .log (contents);
758+ } finally {
759+ await file .close ();
760+ }
761+ ` ` `
762+
719763#### ` filehandle .readLines ([options])`
720764
721765<!-- YAML
@@ -1765,6 +1809,9 @@ try {
17651809<!-- YAML
17661810added: v10.0.0
17671811changes:
1812+ - version: REPLACEME
1813+ pr-url: https://github.com/nodejs/node/pull/63634
1814+ description: Added support for the `buffer` option.
17681815 - version:
17691816 - v15.2.0
17701817 - v14.17.0
@@ -1778,6 +1825,8 @@ changes:
17781825 * `encoding` {string|null} **Default:** `null`
17791826 * `flag` {string} See [support of file system `flags`][]. **Default:** `'r'`.
17801827 * `signal` {AbortSignal} allows aborting an in-progress readFile
1828+ * `buffer` {Buffer|TypedArray|DataView|Function} A buffer to read into, or a
1829+ function called with the file size that returns the buffer.
17811830* Returns: {Promise} Fulfills with the contents of the file.
17821831
17831832Asynchronously reads the entire contents of a file.
@@ -1787,6 +1836,11 @@ as a {Buffer} object. Otherwise, the data will be a string.
17871836
17881837If `options` is a string, then it specifies the encoding.
17891838
1839+ If `buffer` is provided and no encoding is specified, the returned {Buffer} is
1840+ a view over the supplied buffer containing only the bytes read. If the
1841+ supplied buffer is too small to contain the entire file, the promise will be
1842+ rejected.
1843+
17901844When the `path` is a directory, the behavior of `fsPromises.readFile()` is
17911845platform-specific. On macOS, Linux, and Windows, the promise will be rejected
17921846with an error. On FreeBSD, a representation of the directory's contents will be
@@ -1847,6 +1901,29 @@ system requests but rather the internal buffering `fs.readFile` performs.
18471901
18481902Any specified {FileHandle} has to support reading.
18491903
1904+ An example using the `buffer` option with a pre-allocated buffer:
1905+
1906+ ```mjs
1907+ import { Buffer } from 'node:buffer';
1908+ import { readFile } from 'node:fs/promises';
1909+
1910+ const buf = Buffer.alloc(16384);
1911+ const contents = await readFile('/path/to/file', { buffer: buf });
1912+ console.log(contents); // A view over `buf` containing only the bytes read
1913+ ```
1914+
1915+ An example using the `buffer` option with a function returning a buffer:
1916+
1917+ ```mjs
1918+ import { Buffer } from 'node:buffer';
1919+ import { readFile } from 'node:fs/promises';
1920+
1921+ const contents = await readFile('/path/to/file', {
1922+ buffer: (size) => Buffer.alloc(size),
1923+ });
1924+ console.log(contents);
1925+ ```
1926+
18501927### `fsPromises.readlink(path[, options])`
18511928
18521929<!-- YAML
@@ -4225,6 +4302,9 @@ If `options.withFileTypes` is set to `true`, the `files` array will contain
42254302<!-- YAML
42264303added: v0.1.29
42274304changes:
4305+ - version: REPLACEME
4306+ pr-url: https://github.com/nodejs/node/pull/63634
4307+ description: Added support for the ` buffer` option.
42284308 - version: v18.0.0
42294309 pr-url: https://github.com/nodejs/node/pull/41678
42304310 description: Passing an invalid callback to the ` callback` argument
@@ -4266,6 +4346,8 @@ changes:
42664346 * ` encoding` {string|null} **Default:** ` null `
42674347 * ` flag` {string} See [support of file system ` flags` ][]. **Default:** ` ' r' ` .
42684348 * ` signal` {AbortSignal} allows aborting an in-progress readFile
4349+ * ` buffer` {Buffer|TypedArray|DataView|Function} A buffer to read into, or a
4350+ function called with the file size that returns the buffer.
42694351* ` callback` {Function}
42704352 * ` err` {Error|AggregateError}
42714353 * ` data` {string|Buffer}
@@ -4286,6 +4368,11 @@ contents of the file.
42864368
42874369If no encoding is specified, then the raw buffer is returned.
42884370
4371+ If ` buffer` is provided and no encoding is specified, the returned {Buffer} is
4372+ a view over the supplied buffer containing only the bytes read. If the
4373+ supplied buffer is too small to contain the entire file, the callback is
4374+ called with an error.
4375+
42894376If ` options` is a string, then it specifies the encoding:
42904377
42914378` ` ` mjs
@@ -4334,6 +4421,33 @@ when possible prefer streaming via `fs.createReadStream()`.
43344421Aborting an ongoing request does not abort individual operating
43354422system requests but rather the internal buffering ` fs .readFile ` performs.
43364423
4424+ An example using the ` buffer` option with a pre-allocated buffer:
4425+
4426+ ` ` ` mjs
4427+ import { Buffer } from ' node:buffer' ;
4428+ import { readFile } from ' node:fs' ;
4429+
4430+ const buf = Buffer .alloc (16384 );
4431+ readFile (' /path/to/file' , { buffer: buf }, (err , data ) => {
4432+ if (err) throw err;
4433+ console .log (data); // A view over `buf` containing only the bytes read
4434+ });
4435+ ` ` `
4436+
4437+ An example using the ` buffer` option with a function returning a buffer:
4438+
4439+ ` ` ` mjs
4440+ import { Buffer } from ' node:buffer' ;
4441+ import { readFile } from ' node:fs' ;
4442+
4443+ readFile (' /path/to/file' , {
4444+ buffer : (size ) => Buffer .alloc (size),
4445+ }, (err , data ) => {
4446+ if (err) throw err;
4447+ console .log (data);
4448+ });
4449+ ` ` `
4450+
43374451#### File descriptors
43384452
433944531. Any specified file descriptor has to support reading.
@@ -6428,6 +6542,9 @@ If `options.withFileTypes` is set to `true`, the result will contain
64286542<!-- YAML
64296543added: v0.1.8
64306544changes:
6545+ - version: REPLACEME
6546+ pr-url: https://github.com/nodejs/node/pull/63634
6547+ description: Added support for the ` buffer` option.
64316548 - version: v7.6.0
64326549 pr-url: https://github.com/nodejs/node/pull/10739
64336550 description: The ` path` parameter can be a WHATWG ` URL ` object using ` file: `
@@ -6441,6 +6558,8 @@ changes:
64416558* ` options` {Object|string}
64426559 * ` encoding` {string|null} **Default:** ` null `
64436560 * ` flag` {string} See [support of file system ` flags` ][]. **Default:** ` ' r' ` .
6561+ * ` buffer` {Buffer|TypedArray|DataView|Function} A buffer to read into, or a
6562+ function called with the file size that returns the buffer.
64446563* Returns: {string|Buffer}
64456564
64466565Returns the contents of the ` path` .
@@ -6451,6 +6570,11 @@ this API: [`fs.readFile()`][].
64516570If the ` encoding` option is specified then this function returns a
64526571string. Otherwise it returns a buffer.
64536572
6573+ If ` buffer` is provided and no encoding is specified, the returned {Buffer} is
6574+ a view over the supplied buffer containing only the bytes read. If the
6575+ supplied buffer is too small to contain the entire file, an error will be
6576+ thrown.
6577+
64546578Similar to [` fs .readFile ()` ][], when the path is a directory, the behavior of
64556579` fs .readFileSync ()` is platform-specific.
64566580
0 commit comments