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

Function read

lib/internal/fs/promises.js:1367–1424  ·  view source on GitHub ↗
(handle, bufferOrParams, offset, length, position)

Source from the content-addressed store, hash-verified

1365}
1366
1367async function read(handle, bufferOrParams, offset, length, position) {
1368 let buffer = bufferOrParams;
1369 if (!isArrayBufferView(buffer)) {
1370 // This is fh.read(params)
1371 if (bufferOrParams !== undefined) {
1372 validateObject(bufferOrParams, 'options', kValidateObjectAllowNullable);
1373 }
1374 ({
1375 buffer = Buffer.alloc(16384),
1376 offset = 0,
1377 length = buffer.byteLength - offset,
1378 position = null,
1379 } = bufferOrParams ?? kEmptyObject);
1380
1381 validateBuffer(buffer);
1382 }
1383
1384 if (offset !== null && typeof offset === 'object') {
1385 // This is fh.read(buffer, options)
1386 ({
1387 offset = 0,
1388 length = buffer.byteLength - offset,
1389 position = null,
1390 } = offset);
1391 }
1392
1393 if (offset == null) {
1394 offset = 0;
1395 } else {
1396 validateInteger(offset, 'offset', 0);
1397 }
1398
1399 length ??= buffer.byteLength - offset;
1400
1401 if (position == null) {
1402 position = -1;
1403 } else {
1404 validatePosition(position, 'position', length);
1405 }
1406
1407 if (length === 0)
1408 return { __proto__: null, bytesRead: length, buffer };
1409
1410 if (buffer.byteLength === 0) {
1411 throw new ERR_INVALID_ARG_VALUE('buffer', buffer,
1412 'is empty and cannot be written');
1413 }
1414
1415 validateOffsetLengthRead(offset, length, buffer.byteLength);
1416
1417 const bytesRead = (await PromisePrototypeThen(
1418 binding.read(handle.fd, buffer, offset, length, position, kUsePromises),
1419 undefined,
1420 handleErrorFromBinding,
1421 )) || 0;
1422
1423 return { __proto__: null, bytesRead, buffer };
1424}

Callers 1

readMethod · 0.50

Calls 2

allocMethod · 0.80
readMethod · 0.45

Tested by

no test coverage detected