pierrec / node-eval

Evaluate node require() module content directly
MIT License
93 stars 20 forks source link

Buffer is undefined with includeGlobals: true #24

Closed MatisLepik closed 2 years ago

MatisLepik commented 3 years ago

Example (node v14.16.1):

const nodeEval = require('eval');
const test = nodeEval(`module.exports = () => console.log(typeof Buffer);`, 'test.js', {}, true);
test(); // > undefined

It seems this is because Buffer is non-enumerable, and we use Object.keys to merge global into sandbox, so Buffer gets left behind.

A few things like URL are being merged manually, perhaps the same could be done for Buffer?

Alternatively, we could use Object.getOwnPropertyNames(global) instead of Object.keys to avoid missing non-enumerable properties, though I don't know if this has any unintended side-effects. For now, I fixed it on my side by passing those as the scope (third argument).

Thanks!