isolated-function 0.1.7 → 0.1.8

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
@@ -22,6 +22,7 @@
22
22
  - [Auto install dependencies](#auto-install-dependencies)
23
23
  - [Execution profiling](#execution-profiling)
24
24
  - [Resource limits](#resource-limits)
25
+ - [Logging](#logging)
25
26
  - [Error handling](#error-handling)
26
27
  - [API](#api)
27
28
  - [isolatedFunction(code, \[options\])](#isolatedfunctioncode-options)
@@ -167,6 +168,32 @@ await fn(100)
167
168
  // => TimeoutError: Execution timed out
168
169
  ```
169
170
 
171
+ ### Logging
172
+
173
+ The logs are collected into a `logging` object returned after the execution:
174
+
175
+ ```js
176
+ const [fn, teardown] = isolatedFunction(() => {
177
+ console.log('console.log')
178
+ console.info('console.info')
179
+ console.debug('console.debug')
180
+ console.warn('console.warn')
181
+ console.error('console.error')
182
+ return 'done'
183
+ })
184
+
185
+ const { logging } await fn()
186
+
187
+ console.log(logging)
188
+ // {
189
+ // log: ['console.log'],
190
+ // info: ['console.info'],
191
+ // debug: ['console.debug'],
192
+ // warn: ['console.warn'],
193
+ // error: ['console.error']
194
+ // }
195
+ ```
196
+
170
197
  ### Error handling
171
198
 
172
199
  Any error during **isolated-function** execution will be propagated:
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.7",
5
+ "version": "0.1.8",
6
6
  "main": "src/index.js",
7
7
  "exports": {
8
8
  ".": "./src/index.js"
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
- const acorn = require('acorn')
4
3
  const walk = require('acorn-walk')
4
+ const acorn = require('acorn')
5
5
 
6
6
  module.exports = code => {
7
7
  const ast = acorn.parse(code, { ecmaVersion: 2020, sourceType: 'module' })
package/src/index.js CHANGED
@@ -15,7 +15,11 @@ const createError = ({ name, message, ...props }) => {
15
15
  }
16
16
 
17
17
  const flags = ({ filename, memory }) => {
18
- const flags = ['--experimental-permission', `--allow-fs-read=${filename}`]
18
+ const flags = [
19
+ '--disable-warning=ExperimentalWarning',
20
+ '--experimental-permission',
21
+ `--allow-fs-read=${filename}`
22
+ ]
19
23
  if (memory) flags.push(`--max-old-space-size=${memory}`)
20
24
  return flags.join(' ')
21
25
  }
@@ -39,11 +43,15 @@ module.exports = (snippet, { tmpdir, timeout, memory, throwError = true } = {})
39
43
  timeout,
40
44
  killSignal: 'SIGKILL'
41
45
  })
42
- const { isFulfilled, value, profiling } = JSON.parse(stdout)
46
+ const { isFulfilled, value, profiling, logging } = JSON.parse(stdout)
43
47
  profiling.duration = duration()
44
- if (isFulfilled) return { isFulfilled, value, profiling }
45
- if (throwError) throw deserializeError(value)
46
- return { isFulfilled: false, value: deserializeError(value), profiling }
48
+ return isFulfilled
49
+ ? { isFulfilled, value, profiling, logging }
50
+ : throwError
51
+ ? (() => {
52
+ throw deserializeError(value)
53
+ })()
54
+ : { isFulfilled: false, value: deserializeError(value), profiling, logging }
47
55
  } catch (error) {
48
56
  if (error.signalCode === 'SIGTRAP') {
49
57
  throw createError({
@@ -2,10 +2,20 @@
2
2
 
3
3
  const SERIALIZE_ERROR = require('./serialize-error')
4
4
 
5
- const generateTemplate = snippet => `
5
+ module.exports = snippet => `
6
6
  const args = JSON.parse(process.argv[2])
7
7
 
8
- ;(async () => {
8
+ const logging = Object.create(null)
9
+
10
+ for (const method of ['log', 'info', 'debug', 'warn', 'error']) {
11
+ console[method] = function (...args) {
12
+ const input = args.join(' ')
13
+ logging[method] === undefined ? logging[method] = [input] : logging[method].push(input)
14
+ }
15
+ }
16
+
17
+ ;(async (send) => {
18
+ process.stdout.write = function () {}
9
19
  let value
10
20
  let isFulfilled
11
21
 
@@ -16,12 +26,11 @@ const generateTemplate = snippet => `
16
26
  value = ${SERIALIZE_ERROR}(error)
17
27
  isFulfilled = false
18
28
  } finally {
19
- console.log(JSON.stringify({
29
+ send(JSON.stringify({
20
30
  isFulfilled,
31
+ logging,
21
32
  value,
22
33
  profiling: { memory: process.memoryUsage().rss }
23
34
  }))
24
35
  }
25
- })()`
26
-
27
- module.exports = generateTemplate
36
+ })(process.stdout.write.bind(process.stdout))`