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

Function readFile

lib/fs.js:401–436  ·  view source on GitHub ↗

* Asynchronously reads the entire contents of a file. * @param {string | Buffer | URL | number} path * @param {{ * encoding?: string | null; * flag?: string; * signal?: AbortSignal; * } | string} [options] * @param {( * err?: Error, * data?: string | Buffer * ) => any} callba

(path, options, callback)

Source from the content-addressed store, hash-verified

399 * @returns {void}
400 */
401function readFile(path, options, callback) {
402 callback ||= options;
403 validateFunction(callback, 'cb');
404
405 const h = vfsState.handlers;
406 if (h !== null) {
407 const opts = typeof options === 'function' ? undefined : options;
408 if (checkAborted(opts?.signal, callback)) return;
409 if (vfsResult(h.readFile(path, opts), callback)) return;
410 }
411
412 options = getOptions(options, { flag: 'r' });
413 validateReadFileBufferOptions(options);
414 ReadFileContext ??= require('internal/fs/read/context');
415 const context = new ReadFileContext(callback, options);
416 context.isUserFd = isFd(path); // File descriptor ownership
417
418 if (options.signal) {
419 context.signal = options.signal;
420 }
421 if (context.isUserFd) {
422 process.nextTick(function tick(context) {
423 FunctionPrototypeCall(readFileAfterOpen, { context }, null, path);
424 }, context);
425 return;
426 }
427
428 if (checkAborted(options.signal, callback))
429 return;
430
431 const flagsNumber = stringToFlags(options.flag, 'options.flag');
432 const req = new FSReqCallback();
433 req.context = context;
434 req.oncomplete = readFileAfterOpen;
435 binding.open(getValidatedPath(path), flagsNumber, 0o666, req);
436}
437
438function tryStatSync(fd, isUserFd) {
439 const stats = binding.fstat(fd, false, undefined, true /* shouldNotThrow */);

Callers 8

getServeCallbacksFunction · 0.50
readJsonFunction · 0.50
listMethod · 0.50
listWorkspacesMethod · 0.50
editMethod · 0.50
readFilesMethod · 0.50
tar.jsFile · 0.50
loadSnapshotsMethod · 0.50

Calls 7

vfsResultFunction · 0.85
stringToFlagsFunction · 0.85
checkAbortedFunction · 0.70
openMethod · 0.65
getOptionsFunction · 0.50
requireFunction · 0.50
readFileMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…