HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-172-31-4-197 6.8.0-1036-aws #38~22.04.1-Ubuntu SMP Fri Aug 22 15:44:33 UTC 2025 x86_64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //lib/node_modules/ts-node/dist-raw/node-internalBinding-fs.js
const fs = require('fs');
const {versionGteLt} = require('../dist/util');

// In node's core, this is implemented in C
// https://github.com/nodejs/node/blob/v15.3.0/src/node_file.cc#L891-L985
/**
 * @param {string} path
 * @returns {[] | [string, boolean]}
 */
function internalModuleReadJSON(path) {
  let string
  try {
    string = fs.readFileSync(path, 'utf8')
  } catch (e) {
    if (e.code === 'ENOENT') return []
    throw e
  }
  // Node's implementation checks for the presence of relevant keys: main, name, type, exports, imports
  // Node does this for performance to skip unnecessary parsing.
  // This would slow us down and, based on our usage, we can skip it.
  const containsKeys = true
  return [string, containsKeys]
}

// In node's core, this is implemented in C
// https://github.com/nodejs/node/blob/63e7dc1e5c71b70c80ed9eda230991edb00811e2/src/node_file.cc#L987-L1005
/**
 * @param {string} path
 * @returns {number} 0 = file, 1 = dir, negative = error
 */
function internalModuleStat(path) {
  const stat = fs.statSync(path, { throwIfNoEntry: false });
  if(!stat) return -1;
  if(stat.isFile()) return 0;
  if(stat.isDirectory()) return 1;
}

/**
 * @param {string} path
 * @returns {number} 0 = file, 1 = dir, negative = error
 */
function internalModuleStatInefficient(path) {
  try {
    const stat = fs.statSync(path);
    if(stat.isFile()) return 0;
    if(stat.isDirectory()) return 1;
  } catch(e) {
    return -e.errno || -1;
  }
}

const statSupportsThrowIfNoEntry = versionGteLt(process.versions.node, '15.3.0') ||
  versionGteLt(process.versions.node, '14.17.0', '15.0.0');

module.exports = {
  internalModuleReadJSON,
  internalModuleStat: statSupportsThrowIfNoEntry ? internalModuleStat : internalModuleStatInefficient
};