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

Function readFileHandleWithUserBuffer

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

Source from the content-addressed store, hash-verified

1162}
1163
1164async function readFileHandleWithUserBuffer(filehandle, options, size) {
1165 const signal = options?.signal;
1166 const encoding = options?.encoding;
1167 const buffer = getReadFileBuffer(options, size);
1168 const byteLengthName = getReadFileBufferByteLengthName(options);
1169 let totalRead = 0;
1170
1171 while (totalRead < buffer.byteLength) {
1172 checkAborted(signal);
1173
1174 const length = size === 0 ?
1175 buffer.byteLength - totalRead :
1176 MathMin(size - totalRead, kReadFileBufferLength);
1177
1178 const bytesRead = (await PromisePrototypeThen(
1179 binding.read(filehandle.fd, buffer, totalRead, length, -1, kUsePromises),
1180 undefined,
1181 handleErrorFromBinding,
1182 )) ?? 0;
1183
1184 totalRead += bytesRead;
1185
1186 if (bytesRead === 0 || totalRead === size) {
1187 const result = buffer.subarray(0, totalRead);
1188 return encoding ? result.toString(encoding) : result;
1189 }
1190 }
1191
1192 if (size === 0) {
1193 checkAborted(signal);
1194
1195 const extraBuffer = Buffer.allocUnsafeSlow(1);
1196 const bytesRead = (await PromisePrototypeThen(
1197 binding.read(filehandle.fd, extraBuffer, 0, 1, -1, kUsePromises),
1198 undefined,
1199 handleErrorFromBinding,
1200 )) ?? 0;
1201
1202 if (bytesRead !== 0) {
1203 throw new ERR_INVALID_ARG_VALUE(
1204 byteLengthName,
1205 buffer.byteLength,
1206 'is too small to contain the entire file',
1207 );
1208 }
1209 }
1210
1211 return encoding ? buffer.toString(encoding) : buffer.subarray(0, totalRead);
1212}
1213
1214async function readFileHandle(filehandle, options) {
1215 const signal = options?.signal;

Callers 1

readFileHandleFunction · 0.85

Calls 4

checkAbortedFunction · 0.70
readMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…