JAVASCRIPT   16

module.js

Guest on 15th May 2021 10:21:47 AM

  1. 'use strict';
  2.  
  3. // Invoke with makeRequireFunction(module) where |module| is the Module object
  4. // to use as the context for the require() function.
  5. function makeRequireFunction(mod) {
  6.   const Module = mod.constructor;
  7.  
  8.   function require(path) {
  9.     try {
  10.       exports.requireDepth += 1;
  11.       return mod.require(path);
  12.     } finally {
  13.       exports.requireDepth -= 1;
  14.     }
  15.   }
  16.  
  17.   function resolve(request, options) {
  18.     return Module._resolveFilename(request, mod, false, options);
  19.   }
  20.  
  21.   require.resolve = resolve;
  22.  
  23.   function paths(request) {
  24.     return Module._resolveLookupPaths(request, mod, true);
  25.   }
  26.  
  27.   resolve.paths = paths;
  28.  
  29.   require.main = process.mainModule;
  30.  
  31.   // Enable support to add extra extension types.
  32.   require.extensions = Module._extensions;
  33.  
  34.   require.cache = Module._cache;
  35.  
  36.   return require;
  37. }
  38.  
  39. /**
  40.  * Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
  41.  * because the buffer-to-string conversion in `fs.readFileSync()`
  42.  * translates it to FEFF, the UTF-16 BOM.
  43.  */
  44. function stripBOM(content) {
  45.   if (content.charCodeAt(0) === 0xFEFF) {
  46.     content = content.slice(1);
  47.   }
  48.   return content;
  49. }
  50.  
  51. /**
  52.  * Find end of shebang line and slice it off
  53.  */
  54. function stripShebang(content) {
  55.   // Remove shebang
  56.   var contLen = content.length;
  57.   if (contLen >= 2) {
  58.     if (content.charCodeAt(0) === 35/*#*/ &&
  59.         content.charCodeAt(1) === 33/*!*/) {
  60.       if (contLen === 2) {
  61.         // Exact match
  62.         content = '';
  63.       } else {
  64.         // Find end of shebang line and slice it off
  65.         var i = 2;
  66.         for (; i < contLen; ++i) {
  67.           var code = content.charCodeAt(i);
  68.           if (code === 10/*\n*/ || code === 13/*\r*/)
  69.             break;
  70.         }
  71.         if (i === contLen)
  72.           content = '';
  73.         else {
  74.           // Note that this actually includes the newline character(s) in the
  75.           // new output. This duplicates the behavior of the regular expression
  76.           // that was previously used to replace the shebang line
  77.           content = content.slice(i);
  78.         }
  79.       }
  80.     }
  81.   }
  82.   return content;
  83. }
  84.  
  85. const builtinLibs = [
  86.   'assert', 'async_hooks', 'buffer', 'child_process', 'cluster', 'crypto',
  87.   'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'https', 'net',
  88.   'os', 'path', 'perf_hooks', 'punycode', 'querystring', 'readline', 'repl',
  89.   'stream', 'string_decoder', 'tls', 'tty', 'url', 'util', 'v8', 'vm', 'zlib'
  90. ];
  91.  
  92. const { exposeHTTP2 } = process.binding('config');
  93. if (exposeHTTP2)
  94.   builtinLibs.push('http2');
  95.  
  96. if (typeof process.binding('inspector').connect === 'function') {
  97.   builtinLibs.push('inspector');
  98.   builtinLibs.sort();
  99. }
  100.  
  101. function addBuiltinLibsToObject(object) {
  102.   // Make built-in modules available directly (loaded lazily).
  103.   builtinLibs.forEach((name) => {
  104.     // Goals of this mechanism are:
  105.     // - Lazy loading of built-in modules
  106.     // - Having all built-in modules available as non-enumerable properties
  107.     // - Allowing the user to re-assign these variables as if there were no
  108.     //   pre-existing globals with the same name.
  109.  
  110.     const setReal = (val) => {
  111.       // Deleting the property before re-assigning it disables the
  112.       // getter/setter mechanism.
  113.       delete object[name];
  114.       object[name] = val;
  115.     };
  116.  
  117.     Object.defineProperty(object, name, {
  118.       get: () => {
  119.         const lib = require(name);
  120.  
  121.         // Disable the current getter/setter and set up a new
  122.         // non-enumerable property.
  123.         delete object[name];
  124.         Object.defineProperty(object, name, {
  125.           get: () => lib,
  126.           set: setReal,
  127.           configurable: true,
  128.           enumerable: false
  129.         });
  130.  
  131.         return lib;
  132.       },
  133.       set: setReal,
  134.       configurable: true,
  135.       enumerable: false
  136.     });
  137.   });
  138. }
  139.  
  140. module.exports = exports = {
  141.   addBuiltinLibsToObject,
  142.   builtinLibs,
  143.   makeRequireFunction,
  144.   requireDepth: 0,
  145.   stripBOM,
  146.   stripShebang
  147. };

Raw Paste


Login or Register to edit or fork this paste. It's free.