isolated-function 0.1.35 → 0.1.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +13 -6
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "isolated-function",
3
3
  "description": "Runs untrusted code in a Node.js v8 sandbox.",
4
4
  "homepage": "https://github.com/Kikobeats/isolated-function",
5
- "version": "0.1.35",
5
+ "version": "0.1.36",
6
6
  "main": "src/index.js",
7
7
  "exports": {
8
8
  ".": "./src/index.js"
package/src/index.js CHANGED
@@ -15,17 +15,18 @@ const createError = ({ name, message, ...props }) => {
15
15
  return error
16
16
  }
17
17
 
18
- const [major] = process.version.slice(1).split('.').map(Number)
18
+ const [nodeMajor] = process.version.slice(1).split('.').map(Number)
19
19
 
20
- const PERMISSION_FLAG = major >= 24 ? '--permission' : '--experimental-permission'
20
+ const PERMISSION_FLAG = nodeMajor >= 24 ? '--permission' : '--experimental-permission'
21
21
 
22
- const flags = ({ memory }) => {
22
+ const flags = ({ memory, allow }) => {
23
23
  const flags = ['--disable-warning=ExperimentalWarning', PERMISSION_FLAG]
24
24
  if (memory) flags.push(`--max-old-space-size=${memory}`)
25
+ allow.forEach(resource => flags.push(`--allow-${resource}`))
25
26
  return flags.join(' ')
26
27
  }
27
28
 
28
- module.exports = (snippet, { tmpdir, timeout, memory, throwError = true } = {}) => {
29
+ module.exports = (snippet, { tmpdir, timeout, memory, throwError = true, allow = [] } = {}) => {
29
30
  if (!['function', 'string'].includes(typeof snippet)) throw new TypeError('Expected a function')
30
31
  const compilePromise = compile(snippet, tmpdir)
31
32
 
@@ -38,7 +39,7 @@ module.exports = (snippet, { tmpdir, timeout, memory, throwError = true } = {})
38
39
  const subprocess = $('node', ['-', JSON.stringify(args)], {
39
40
  env: {
40
41
  ...process.env,
41
- NODE_OPTIONS: flags({ memory })
42
+ NODE_OPTIONS: flags({ memory, allow })
42
43
  },
43
44
  timeout,
44
45
  killSignal: 'SIGKILL'
@@ -77,9 +78,15 @@ module.exports = (snippet, { tmpdir, timeout, memory, throwError = true } = {})
77
78
  }
78
79
 
79
80
  if (error.code === 'ERR_ACCESS_DENIED') {
81
+ const permission = error.permission
82
+ ? error.permission
83
+ : error.message.includes('getaddrinfo')
84
+ ? 'network'
85
+ : undefined
86
+
80
87
  throw createError({
81
88
  name: 'PermissionError',
82
- message: `Access to '${error.permission}' has been restricted`,
89
+ message: `Access to '${permission}' has been restricted`,
83
90
  profiling: { duration: duration() }
84
91
  })
85
92
  }