{"_id":"@bestest/node-module-sandbox","_rev":"3-3938e9a2922350e985e5bb7dda0ffaf0","name":"@bestest/node-module-sandbox","dist-tags":{"latest":"1.1.1"},"versions":{"1.0.0":{"name":"@bestest/node-module-sandbox","version":"1.0.0","description":"Sandbox Node.js modules along with require function","publishConfig":{"access":"public"},"main":"./index","types":"./index.d.ts","scripts":{"clean":"rimraf lib/**/*.{js,d.ts} index.{js,d.ts}","build":"npm run clean && tsc -p ./tsconfig.json","lint":"tslint -p ./tsconfig.json -c ../../tslint.json","ts-check":"tsc -p ./tsconfig.json --noEmit","test":"npm run ts-check && npm run lint","prepublishOnly":"npm run build"},"repository":{"type":"git","url":"git+https://github.com/rangoo94/bestest.git"},"bugs":{"url":"https://github.com/rangoo94/bestest/issues"},"keywords":["bestest","sandbox","module","modules","fs","files","monkey","vm","eval","function","execution","patch","resolve","resolution","run","mock"],"author":{"name":"Dawid Rusnak","email":"dawid@drcode.pl"},"license":"MIT","dependencies":{"@bestest/utils":"^1.0.0","strip-bom":"^4.0.0"},"devDependencies":{"@types/node":"^12.7.0","rimraf":"^2.6.3","ts-node":"^8.3.0","tslint":"^5.18.0","tslint-config-standard":"^8.0.1","typescript":"^3.5.3"},"homepage":"https://github.com/rangoo94/bestest#readme","_id":"@bestest/node-module-sandbox@1.0.0","_nodeVersion":"12.7.0","_npmVersion":"6.10.3","dist":{"integrity":"sha512-4ckby0BP96WEs+vvDL5Bnx0UaZS0htBDdbMrHO+nqU7Wnv3r0nKd+OTXRzwy5JOaXwVYo7wxN/liVOTIpysWGQ==","shasum":"6eb6eb6b720028723aaf4a2103a6e0c38efb9955","tarball":"https://registry.npmjs.org/@bestest/node-module-sandbox/-/node-module-sandbox-1.0.0.tgz","fileCount":30,"unpackedSize":34030,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdUrkWCRA9TVsSAnZWagAA7VEP/i6rjVqgv77GlwDjFvV/\nm9z31rlcwSuiJjNkKkncdwpE6D3Ntw3hwE+UfmQwNDplrtrJxzSQ0KtnMwP5\ncbXxeIgz0rxj34nfUFYbMIEHwiKUmoErjUKlEyDTgqXmmnQX7MVRtrMbhNp1\n2hQ6fzuXEivACk+N7x9MzdyBK/NPilvG3b3YZ71y7Z1uWvzvY87ePwlB8foL\nxf/l1wCwHgULusHBQXgR8ecqZ2J/Rtoh7Aqss+yX6wNiqvCdLsTb5vOpxcD5\nb5Cih1m4YTEI9+yG2EfraDPV4FzDNFAhpAS8iZCeGFxzbB3NtxvQrcKIU2qd\n5xgZ2Z5d7XbNxks4hXyANTkEbmbXCtLRJJexP8JnAWLqhxDv1yn++m9HW6qF\nAuGvJsf+sSDwxxd/a4vrtjfaDdx9YmKN6EoymgQESlKhi1bmbZ0+Iq/KBUZK\nY9RPf884l4DJ4vA9oqKVh+fExH+PLLH00fUe5vV095PudqVfK90HhiPB2KK4\nqgRfCXpguR9148HfBBgAgEKRaYmerYRg8Y5fTY/gCMyWrt0INzMD9V2H/Fno\n6Pud6+8aeKGMjktOz90zDyNsL66C8h3yEry32ChMUhVGW0lC1DJKRiDiBFdo\nMHBzw0p+hQvz7RuaahqDE/w8H9Ssod1sEu7LIKxZzdZxYzBcomc2ng7Di3eG\nVLXU\r\n=OwWZ\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCuTRKpvnwa3kop8VNXtg6GvGOalz/43oS704YHDcv3dAIhAO9dsFjoDm7hBFlpgz1j+9cPAztq2462y+pgsA/tyeMi"}]},"maintainers":[{"name":"rangoo","email":"dawid@drcode.pl"}],"_npmUser":{"name":"rangoo","email":"dawid@drcode.pl"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/node-module-sandbox_1.0.0_1565702422132_0.013487948581076159"},"_hasShrinkwrap":false},"1.1.0":{"name":"@bestest/node-module-sandbox","version":"1.1.0","description":"Sandbox Node.js modules along with require function","publishConfig":{"access":"public"},"main":"./index","types":"./index.d.ts","scripts":{"clean":"rimraf lib/**/*.{js,d.ts} index.{js,d.ts}","build":"npm run clean && tsc -p ./tsconfig.json","lint":"tslint -p ./tsconfig.json -c ../../tslint.json","ts-check":"tsc -p ./tsconfig.json --noEmit","test":"npm run ts-check && npm run lint","prepublishOnly":"npm run build"},"repository":{"type":"git","url":"git+https://github.com/rangoo94/bestest.git"},"bugs":{"url":"https://github.com/rangoo94/bestest/issues"},"keywords":["bestest","sandbox","module","modules","fs","files","monkey","vm","eval","function","execution","patch","resolve","resolution","run","mock"],"author":{"name":"Dawid Rusnak","email":"dawid@drcode.pl"},"license":"MIT","dependencies":{"@bestest/utils":"^1.0.0","strip-bom":"^4.0.0"},"devDependencies":{"@types/node":"^12.7.0","rimraf":"^2.6.3","ts-node":"^8.3.0","tslint":"^5.18.0","tslint-config-standard":"^8.0.1","typescript":"^3.5.3"},"homepage":"https://github.com/rangoo94/bestest#readme","_id":"@bestest/node-module-sandbox@1.1.0","_nodeVersion":"12.7.0","_npmVersion":"6.10.3","dist":{"integrity":"sha512-S1Y4cU8FD7oNYqkOKgdVQwH4TGxSqjfqLQtkcyRbgWgtqVcU+HhkaCmwuotClYTO1xoLKMMJiIg+nodn8MV3kQ==","shasum":"9534b14465954b06612e4da44039545e9ce3d678","tarball":"https://registry.npmjs.org/@bestest/node-module-sandbox/-/node-module-sandbox-1.1.0.tgz","fileCount":30,"unpackedSize":36387,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdUtpCCRA9TVsSAnZWagAAbA4P/jFlbXjqX7aRXYo4CIAP\ndN0VX+nCHT5pdt0zUje8zdHQdEbHPWN6v1u8mdzLUTvBA4u9HsMP4GOIvj4H\n4aCzO6eveKg2LofUwdAkeLxO6CHDrckni2XF9OYFEd+mB12ZbLPf7iqv6pOW\nqu9DNS0OK8wJHL/MhhN+cM16F3GrI/hJMzVUR7V2vRW8dRBZXtwL07s1WCtm\nV+Glh0y8nmutq1IR5sj9lhZPEMF1ObXkjWq5i5kEVTT1rxGEL3g+YU/RRUYB\nZEFtse7QWWXaxAXvcLXlNZEcdWsoh2G4Sc6Wdo8fYS8nxJqFCaqEgY44/mQz\nWieObsMkp51kGlMc7Ej6fjfVzz61zZJqNqCUQRK8dwSdn2ErDsYjrbtusrVb\nUJRuPD/PlobS4o/scAYz3z5HulffgB8B9G3Y7E1Aq9GuWAe7/yGsYvHh0mEr\nCoPfgI+gpUvIkTkJW9sKuqm2goIiwdWlj9Hv1e14YwYO5My+b5WPQjxOOzCE\nipeanf1yfeH+SOXWZIlTzWKZShhrAc6FUq4U3kkHoXPZ9VA/V2wYp9sPU7Nm\nYv6b4f+JUhHSA/VWoWHuPwtAv0WZo3HLDWqVeXBzoD5cYnES+6ACCkjX1iOL\nafwfIgJ2KrkYmAxmATNLQIHfofggab6eS1m/Y7qwelVBgQ0uUkkH7+Eo33rD\nmh4y\r\n=1xMm\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIATg+ccltLSvWtII/fHSqGqWvicn5eUSKIlKZzbJuQ24AiBMlD4sg6+UUKd3m2Xh9IRxydc50ISfGmeQtBEBJwNW/A=="}]},"maintainers":[{"name":"rangoo","email":"dawid@drcode.pl"}],"_npmUser":{"name":"rangoo","email":"dawid@drcode.pl"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/node-module-sandbox_1.1.0_1565710913490_0.3991045056049165"},"_hasShrinkwrap":false},"1.1.1":{"name":"@bestest/node-module-sandbox","version":"1.1.1","description":"Sandbox Node.js modules along with require function","publishConfig":{"access":"public"},"main":"./index","types":"./index.d.ts","scripts":{"clean":"rimraf lib/**/*.{js,d.ts} index.{js,d.ts}","build":"npm run clean && tsc -p ./tsconfig.json","lint":"tslint -p ./tsconfig.json -c ../../tslint.json","ts-check":"tsc -p ./tsconfig.json --noEmit","test":"npm run ts-check && npm run lint","prepublishOnly":"npm run build"},"repository":{"type":"git","url":"git+https://github.com/rangoo94/bestest.git"},"bugs":{"url":"https://github.com/rangoo94/bestest/issues"},"keywords":["bestest","sandbox","module","modules","fs","files","monkey","vm","eval","function","execution","patch","resolve","resolution","run","mock"],"author":{"name":"Dawid Rusnak","email":"dawid@drcode.pl"},"license":"MIT","dependencies":{"@bestest/utils":"^1.0.0","strip-bom":"^4.0.0"},"devDependencies":{"@types/node":"^12.7.0","rimraf":"^2.6.3","ts-node":"^8.3.0","tslint":"^5.18.0","tslint-config-standard":"^8.0.1","typescript":"^3.5.3"},"homepage":"https://github.com/rangoo94/bestest#readme","_id":"@bestest/node-module-sandbox@1.1.1","_nodeVersion":"12.7.0","_npmVersion":"6.10.3","dist":{"integrity":"sha512-YopkEBN8Q0FTXF8zbLxk3N0wCmTWCCP4oMYfbj+0cc5bUINbk+ictUHit1/SCHW4hGV9O/5JP71OtddDERcbYw==","shasum":"c51e2e5d842fd1c7c59de0049e4f044e3ec2a0fc","tarball":"https://registry.npmjs.org/@bestest/node-module-sandbox/-/node-module-sandbox-1.1.1.tgz","fileCount":33,"unpackedSize":39040,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdUxbvCRA9TVsSAnZWagAArckP/jytoQaWNVhUOfpY24w9\nMjpm49h+OXi/ZnwPEgCk+/faDtbvMOrvZhdy64n0h/vxo88uzBMfPvXcJv8a\nMmkItNSXAdV1K308KvcW2pK3N2J0ccjo6bUF325N4F8iY6rtn5XAKrufWYQx\nVM1IfD4AIl+ZgWC39Cb2Y4xkf/2bOTqfs8CN3OfoR5wsfzqR24j506+JnDI5\nk/UHU2BFGpHTc61clFgsTA5H+xCziJVpCulT5uTBfnkG9jgHXVuSuKYU88PN\nbrOzqUqiH9UgVyJaUMjR3MS3+cnMfi2dxcaqzVqqhQFzwvNMDHKKSfOTsrjr\nkU8zeQjhlSJGYA3vd5XeDLBgqw3OA8SATdByuucEhKMjvWtzV8HcmBVGeTFX\nFWjQdTJWFwYY+fQGC6iJneabZKsgRIwV+PyGRWjRBmEwukKqciuE+tFS/f80\nq7t6o/orHXX75M5yNZQGiJ9dyfOJxkdPqcLGh3ERnr37YkhXaQhL4IznIFY+\ntzTnddqBJY7Fg+LTbgTZC2WP/+OMWAo3ny2Xkgglfj9MSsPtlmDc2whdYgx0\nyFdoTKCzJ+fgK1IlNq03VNCwZN6OFnAhOVRNf9jis6YF5ef4F0NuHuQsZT9c\nNV2xyBQnS06GzJ0StCQrYFl/6ayL/AObcD0Rc73WGrbzXiLavvzla0bbGPhB\nJbOp\r\n=hpJP\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIHILj/I/2ewB28vNkXMDv0spaP+VG0+v2+x1rIwsOM88AiEAvSq/aIigOVdK6ANvMLpR+IaS1Txf4bvZUR4T9PUZdHI="}]},"maintainers":[{"name":"rangoo","email":"dawid@drcode.pl"}],"_npmUser":{"name":"rangoo","email":"dawid@drcode.pl"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/node-module-sandbox_1.1.1_1565726446716_0.7478047274822435"},"_hasShrinkwrap":false}},"time":{"created":"2019-08-13T13:20:21.920Z","1.0.0":"2019-08-13T13:20:22.352Z","modified":"2022-04-04T18:33:37.708Z","1.1.0":"2019-08-13T15:41:53.656Z","1.1.1":"2019-08-13T20:00:46.842Z"},"maintainers":[{"name":"rangoo","email":"dawid@drcode.pl"}],"description":"Sandbox Node.js modules along with require function","homepage":"https://github.com/rangoo94/bestest#readme","keywords":["bestest","sandbox","module","modules","fs","files","monkey","vm","eval","function","execution","patch","resolve","resolution","run","mock"],"repository":{"type":"git","url":"git+https://github.com/rangoo94/bestest.git"},"author":{"name":"Dawid Rusnak","email":"dawid@drcode.pl"},"bugs":{"url":"https://github.com/rangoo94/bestest/issues"},"license":"MIT","readme":"# @bestest/node-module-sandbox\n\nThis module is trying to separate node modules creation in selected context, without touching i.e. regular `require` cache.\n\n> \n> **Warning:**\n> Remember, you should not treat this as a safe sandbox solution!\n> It is also changing some default behaviors, so you may treat is as a buggy as well.\n> \n\n## How to use it\n\n### Execute code (with sandboxed modules cache)\n\n```js\n// fn.js\nexports.fn = () => 3\n\n// main.js\nconst { NodeModuleSandbox } = require('@bestest/node-module-sandbox')\n\nconst sandbox1 = new NodeModuleSandbox()\nconst sandbox2 = new NodeModuleSandbox()\n\n// Modify fn.js module in \"sandbox1\"\nsandbox1.executeScript('require(\"./fn\").fn = () => 5')\n\n// Verify that './fn' has been modified in \"sandbox1\"\nconsole.log(sandbox1.executeScript('require(\"./fn\").fn()')) // 5\n\n// Verify that './fn' has been modified in \"sandbox2\"\nconsole.log(sandbox2.executeScript('require(\"./fn\").fn()')) // 3\n\n// Verify that './fn' has not been modified locally\nconsole.log(require('./fn').fn()) // 3\n```\n\n### Passing arguments (or callback)\n\n```js\nconst { NodeModuleSandbox } = require('@bestest/node-module-sandbox')\n\nconst sandbox = new NodeModuleSandbox()\n\n// Remember that this function will be serialized,\n// so you should not use anything from outside of its context.\nconst fn = (callback) => {\n  setTimeout(() => {\n    callback(10)    \n  }, 1000)\n}\n\n// Remember that this function will be serialized,\n// so you should not use anything from outside of its context.\nconst fn2 = (x, y, z) => {\n    console.log('Sum:', x + y + z)\n}\n\nsandbox.executeScriptWithArguments(fn, value => {\n  console.log('The value is', value)\n})\n\nsandbox.executeScriptWithArguments(fn2, 1, 2, 3)\n```\n\n### Passing custom file system\n\n```js\nconst { FileSystem } = require('@bestest/fs')\nconst { NodeModuleSandbox } = require('@bestest/node-module-sandbox')\n\nconst fileSystem = new FileSystem()\n\nfileSystem.setLocalFile('./fixture.json', '\"text\"')\n\nfileSystem.setLocalFile('./example.js', `\n  module.exports = {\n    root: \"/root/\"\n  }\n`)\n\nfileSystem.setLocalFile('./a.js', `\n  // File system can be mocked as well\n  const fs = require('fs')\n  const fixture = JSON.parse(fs.readFileSync('./fixture.json'))\n\n  console.log(require('./example').root + fixture) // \"/root/text\"\n`)\n\nconst sandbox = new NodeModuleSandbox({\n  modules: { fs: fileSystem.fs }\n})\n\nsandbox.requireModule('./a.js', module) // logs \"/root/text\"\nsandbox.requireModule('./a.js', module) // does nothing - this module is already resolved\nsandbox.executeScript('require(\"./a.js\")') // does nothing - this module is already resolved\n```\n\n## Other options\n\n* If you would like to run code in better sandbox:\n  * You may use native [vm](https://nodejs.org/api/vm.html) module, which will create new V8 context (but i.e. without access to Node.js modules).\n* If you would like to run code in separate thread:\n  * You may think about [worker threads](https://nodejs.org/api/worker_threads.html), available since Node.js 10\n  * You can run it as a separate process through [child_process](https://nodejs.org/api/child_process.html) module\n  * You may also want to take a look at [cluster](https://nodejs.org/api/cluster.html) module\n  \n## Problems\n\n### Extensions\n\nDue to the way it is resolved, there are some differences in behavior:\n\n* `.js` and `.json` files will be loaded without any registered extension handlers (i.e. `ts-node` or `@babel/register`)\n* Native modules may be replaced by selected implementation\n* All other files will be loaded regularly, outside of sandbox\n\n### Global context is still available\n\nModule context (`module`, `require`, `__dirname`, `__filename`) will be replaced, but other [global objects](https://nodejs.org/api/globals.html) will still be the same.\n\n### ESM imports are not modified\n\nUnfortunately, there is no API yet available to access and modify ESMLoader.\n\n## Changelog\n\n- **1.1.1** (on 13.08.2019): improve list of script extensions for file resolution\n- **1.1.0** (on 13.08.2019): add option to pass arguments for executed scripts\n- **1.0.0** (on 13.08.2019): initial version\n","readmeFilename":"README.md"}