Repository: nodejs/node
File: lib/internal/modules/cjs/loader.js (JavaScript)

Code before:
    // Format from context is directly returned, because format detection should only be
    // done after the entire load chain is completed.
    return { source, format: context.format };
  };
}

/**
 * Load a specified builtin module, invoking load hooks if necessary.
 * @param {string} id The module ID (without the node: prefix)
 * @param {string} url The module URL (with the node: prefix)
 * @param {string} format Format from resolution.
 * @returns {{builtinExports?: any, resultFromHook?: any}} If there are no load hooks or the load hooks do not
 *   override the format of the builtin, load and return the exports of the builtin module.
 *   Otherwise, return the loadResult for the caller to continue loading.
 */
function loadBuiltinWithHooks(id, url, format) {
  let resultFromHook;
  if (loadHooks.length) {
    url ??= `node:${id}`;
    debug('loadBuiltinWithHooks ', loadHooks.length, id, url, format);
    // TODO(joyeecheung): do we really want to invoke the load hook for the builtins?
    resultFromHook = loadWithHooks(url, format || 'builtin', /* importAttributes */ undefined,
                                   getCjsConditionsArray(), getDefaultLoad(url, id), validateLoadStrict);
    if (resultFromHook.format && resultFromHook.format !== 'builtin') {
      debug('loadBuiltinWithHooks overriding module', id, url, resultFromHook);
      return { resultFromHook };  // Format has been overridden, return result for the caller to continue loading.
    }
  }

  // No hooks or the hooks have not overridden the format. Load it as a builtin module and return the
  // exports.
  const mod = loadBuiltinModule(id);
  return { builtinExports: mod.exports };
}

/**
 * Load a module from cache if it exists, otherwise create a new module instance.
 * 1. If a module already exists in the cache: return its exports object.
 * 2. If the module is native: call
 *    `BuiltinModule.prototype.compileForPublicLoader()` and return the exports.
 * 3. Otherwise, create a new module for the file and save it to the cache.
 *    Then have it load the file contents before returning its exports object.
 * @param {string} request Specifier of module to load via `require`
 * @param {Module} parent Absolute path of the module importing the child
 * @param {boolean} isMain Whether the module is the main entry point
 * @returns {object}
 */
Module._load = function(request, parent, isMain) {
  let relResolveCacheIdentifier;
  if (parent) {
    debug('Module._load REQUEST %s parent: %s', request, parent.id);

Review comment (suggestion):
```suggestion
      return { __proto__: null, resultFromHook };  // Format has been overridden, return result for the caller to continue loading.
```
or
```suggestion
      return { resultFromHook, builtinExports: undefined };  // Format has been overridden, return result for the caller to continue loading.
```
otherwise we'll be hitting potential user-defined getters

Code after:
    // Format from context is directly returned, because format detection should only be
    // done after the entire load chain is completed.
    return { source, format: context.format };
  };
}

/**
 * Load a specified builtin module, invoking load hooks if necessary.
 * @param {string} id The module ID (without the node: prefix)
 * @param {string} url The module URL (with the node: prefix)
 * @param {string} format Format from resolution.
 * @returns {{builtinExports: any, resultFromHook: undefined|ModuleLoadResult}} If there are no load
 *   hooks or the load hooks do not override the format of the builtin, load and return the exports
 *   of the builtin module. Otherwise, return the loadResult for the caller to continue loading.
 */
function loadBuiltinWithHooks(id, url, format) {
  let resultFromHook;
  if (loadHooks.length) {
    url ??= `node:${id}`;
    debug('loadBuiltinWithHooks ', loadHooks.length, id, url, format);
    // TODO(joyeecheung): do we really want to invoke the load hook for the builtins?
    resultFromHook = loadWithHooks(url, format || 'builtin', /* importAttributes */ undefined,
                                   getCjsConditionsArray(), getDefaultLoad(url, id), validateLoadStrict);
    if (resultFromHook.format && resultFromHook.format !== 'builtin') {
      debug('loadBuiltinWithHooks overriding module', id, url, resultFromHook);
      // Format has been overridden, return result for the caller to continue loading.
      return { builtinExports: undefined, resultFromHook };
    }
  }

  // No hooks or the hooks have not overridden the format. Load it as a builtin module and return the
  // exports.
  const mod = loadBuiltinModule(id);
  return { builtinExports: mod.exports, resultFromHook: undefined };
}

/**
 * Load a module from cache if it exists, otherwise create a new module instance.
 * 1. If a module already exists in the cache: return its exports object.
 * 2. If the module is native: call
 *    `BuiltinModule.prototype.compileForPublicLoader()` and return the exports.
 * 3. Otherwise, create a new module for the file and save it to the cache.
 *    Then have it load the file contents before returning its exports object.
 * @param {string} request Specifier of module to load via `require`
 * @param {Module} parent Absolute path of the module importing the child
 * @param {boolean} isMain Whether the module is the main entry point
 * @returns {object}
 */
Module._load = function(request, parent, isMain) {
  let relResolveCacheIdentifier;
  if (parent) {