isolated-function 0.1.6 → 0.1.7

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.
package/README.md CHANGED
@@ -30,6 +30,7 @@
30
30
  - [memory](#memory)
31
31
  - [throwError](#throwerror)
32
32
  - [timeout](#timeout)
33
+ - [tmpdir](#tmpdir)
33
34
  - [=\> (fn(\[...args\]), teardown())](#-fnargs-teardown)
34
35
  - [fn](#fn)
35
36
  - [teardown](#teardown)
@@ -232,6 +233,13 @@ Default: `Infinity`
232
233
 
233
234
  Timeout after a specified amount of time, in milliseconds.
234
235
 
236
+ ##### tmpdir
237
+
238
+ Type: `function`<br>
239
+ Default: `fs.mkdtemp(path.join(require('os').tmpdir(), 'compile-'))`
240
+
241
+ The temporal folder to use for installing code dependencies.
242
+
235
243
  ### => (fn([...args]), teardown())
236
244
 
237
245
  #### fn
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.6",
5
+ "version": "0.1.7",
6
6
  "main": "src/index.js",
7
7
  "exports": {
8
8
  ".": "./src/index.js"
@@ -3,7 +3,6 @@
3
3
  const { execSync } = require('child_process')
4
4
  const esbuild = require('esbuild')
5
5
  const fs = require('fs/promises')
6
- const { tmpdir } = require('os')
7
6
  const $ = require('tinyspawn')
8
7
  const path = require('path')
9
8
 
@@ -22,8 +21,10 @@ const packageManager = (() => {
22
21
  }
23
22
  })()
24
23
 
25
- const getTmp = async content => {
26
- const cwd = await fs.mkdtemp(path.join(tmpdir(), 'compile-'))
24
+ const tmpdirDefault = () => fs.mkdtemp(path.join(require('os').tmpdir(), 'compile-'))
25
+
26
+ const getTmp = async (content, tmpdir) => {
27
+ const cwd = await tmpdir()
27
28
  await fs.mkdir(cwd, { recursive: true })
28
29
 
29
30
  const filepath = path.join(cwd, 'index.js')
@@ -33,10 +34,10 @@ const getTmp = async content => {
33
34
  return { filepath, cwd, content, cleanup }
34
35
  }
35
36
 
36
- module.exports = async snippet => {
37
+ module.exports = async (snippet, tmpdir = tmpdirDefault) => {
37
38
  const compiledTemplate = generateTemplate(snippet)
38
39
  const dependencies = detectDependencies(compiledTemplate)
39
- const tmp = await getTmp(transformDependencies(compiledTemplate))
40
+ const tmp = await getTmp(transformDependencies(compiledTemplate), tmpdir)
40
41
 
41
42
  await $(packageManager.init, { cwd: tmp.cwd })
42
43
  await $(`${packageManager.install} ${dependencies.join(' ')}`, {
@@ -52,7 +53,7 @@ module.exports = async snippet => {
52
53
  })
53
54
 
54
55
  await tmp.cleanup()
55
- return getTmp(result.outputFiles[0].text)
56
+ return getTmp(result.outputFiles[0].text, tmpdir)
56
57
  }
57
58
 
58
59
  module.exports.detectDependencies = detectDependencies
package/src/index.js CHANGED
@@ -20,9 +20,9 @@ const flags = ({ filename, memory }) => {
20
20
  return flags.join(' ')
21
21
  }
22
22
 
23
- module.exports = (snippet, { timeout, memory, throwError = true } = {}) => {
23
+ module.exports = (snippet, { tmpdir, timeout, memory, throwError = true } = {}) => {
24
24
  if (!['function', 'string'].includes(typeof snippet)) throw new TypeError('Expected a function')
25
- const compilePromise = compile(snippet)
25
+ const compilePromise = compile(snippet, tmpdir)
26
26
 
27
27
  const fn = async (...args) => {
28
28
  let duration