MCPcopy
hub / github.com/patriksimek/vm2 / requireImpl

Function requireImpl

lib/setup-node-sandbox.js:86–138  ·  view source on GitHub ↗
(mod, id, direct)

Source from the content-addressed store, hash-verified

84const cacheBuiltins = {__proto__: null};
85
86function requireImpl(mod, id, direct) {
87 if (direct && mod.require !== originalRequire) {
88 return mod.require(id);
89 }
90 const filename = resolve0(mod, id, undefined, Module._extensions, direct);
91 if (localStringPrototypeStartsWith(filename, 'node:')) {
92 id = localStringPrototypeSlice(filename, 5);
93 let nmod = cacheBuiltins[id];
94 if (!nmod) {
95 nmod = loadBuiltinModule(id);
96 if (!nmod) throw new VMError(`Cannot find module '${filename}'`, 'ENOTFOUND');
97 cacheBuiltins[id] = nmod;
98 }
99 return nmod;
100 }
101
102 const cachedModule = Module._cache[filename];
103 if (cachedModule !== undefined) {
104 mod._updateChildren(cachedModule, false);
105 return cachedModule.exports;
106 }
107
108 let nmod = cacheBuiltins[id];
109 if (nmod) return nmod;
110 nmod = loadBuiltinModule(id);
111 if (nmod) {
112 cacheBuiltins[id] = nmod;
113 return nmod;
114 }
115
116 const path = dirname(filename);
117 const module = new Module(filename, path, mod);
118 registerModule(module, filename, path, mod, direct);
119 mod._updateChildren(module, true);
120 try {
121 Module._cache[filename] = module;
122 const handler = findBestExtensionHandler(filename);
123 handler(module, filename);
124 module.loaded = true;
125 } catch (e) {
126 delete Module._cache[filename];
127 const children = mod.children;
128 if (localArrayIsArray(children)) {
129 const index = localArrayPrototypeIndexOf(children, module);
130 if (index !== -1) {
131 localArrayPrototypeSplice(children, index, 1);
132 }
133 }
134 throw e;
135 }
136
137 return module.exports;
138}
139
140Module.builtinModules = ensureSandboxArray(builtinModules);
141Module.globalPaths = ensureSandboxArray(globalPaths);

Callers 2

requireMethod · 0.85
requireFunction · 0.85

Calls 4

findBestExtensionHandlerFunction · 0.85
handlerFunction · 0.85
_updateChildrenMethod · 0.80
requireMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…