* 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)
| 399 | * @returns {void} |
| 400 | */ |
| 401 | function 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 | |
| 438 | function tryStatSync(fd, isUserFd) { |
| 439 | const stats = binding.fstat(fd, false, undefined, true /* shouldNotThrow */); |
no test coverage detected
searching dependent graphs…