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

Function readFileHandle

lib/internal/fs/promises.js:1214–1307  ·  view source on GitHub ↗
(filehandle, options)

Source from the content-addressed store, hash-verified

1212}
1213
1214async function readFileHandle(filehandle, options) {
1215 const signal = options?.signal;
1216 const encoding = options?.encoding;
1217 const decoder = encoding && new StringDecoder(encoding);
1218
1219 checkAborted(signal);
1220
1221 const statFields = await PromisePrototypeThen(
1222 binding.fstat(filehandle.fd, false, kUsePromises),
1223 undefined,
1224 handleErrorFromBinding,
1225 );
1226
1227 checkAborted(signal);
1228
1229 let size = 0;
1230 let length = 0;
1231 if ((statFields[1/* mode */] & S_IFMT) === S_IFREG) {
1232 size = statFields[8/* size */];
1233 length = encoding ? MathMin(size, kReadFileBufferLength) : size;
1234 }
1235 if (length === 0) {
1236 length = kReadFileUnknownBufferLength;
1237 }
1238
1239 if (size > kIoMaxLength)
1240 throw new ERR_FS_FILE_TOO_LARGE(size);
1241
1242 if (options.buffer !== undefined) {
1243 return readFileHandleWithUserBuffer(filehandle, options, size);
1244 }
1245
1246 let totalRead = 0;
1247 const noSize = size === 0;
1248 let buffer = Buffer.allocUnsafeSlow(length);
1249 let result = '';
1250 let offset = 0;
1251 let buffers;
1252 const chunkedRead = length > kReadFileBufferLength;
1253
1254 while (true) {
1255 checkAborted(signal);
1256
1257 if (chunkedRead) {
1258 length = MathMin(size - totalRead, kReadFileBufferLength);
1259 }
1260
1261 const bytesRead = (await PromisePrototypeThen(
1262 binding.read(filehandle.fd, buffer, offset, length, -1, kUsePromises),
1263 undefined,
1264 handleErrorFromBinding,
1265 )) ?? 0;
1266 totalRead += bytesRead;
1267
1268 if (bytesRead === 0 ||
1269 totalRead === size ||
1270 (bytesRead !== buffer.length && !chunkedRead && !noSize)) {
1271 const singleRead = bytesRead === totalRead;

Callers 1

readFileFunction · 0.85

Calls 8

fstatMethod · 0.80
concatMethod · 0.80
checkAbortedFunction · 0.70
readMethod · 0.45
toStringMethod · 0.45
endMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…