isolated-function 0.1.7 → 0.1.9

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)
@@ -57,7 +58,6 @@ const [sum, teardown] = isolatedFunction((y, z) => y + z, {
57
58
 
58
59
  /* interact with the isolated-function */
59
60
  const { value, profiling } = await sum(3, 2)
60
- console.log({ value, profiling })
61
61
 
62
62
  /* close resources associated with the isolated-function initialization */
63
63
  await teardown()
@@ -167,6 +167,32 @@ await fn(100)
167
167
  // => TimeoutError: Execution timed out
168
168
  ```
169
169
 
170
+ ### Logging
171
+
172
+ The logs are collected into a `logging` object returned after the execution:
173
+
174
+ ```js
175
+ const [fn, teardown] = isolatedFunction(() => {
176
+ console.log('console.log')
177
+ console.info('console.info')
178
+ console.debug('console.debug')
179
+ console.warn('console.warn')
180
+ console.error('console.error')
181
+ return 'done'
182
+ })
183
+
184
+ const { logging } await fn()
185
+
186
+ console.log(logging)
187
+ // {
188
+ // log: ['console.log'],
189
+ // info: ['console.info'],
190
+ // debug: ['console.debug'],
191
+ // warn: ['console.warn'],
192
+ // error: ['console.error']
193
+ // }
194
+ ```
195
+
170
196
  ### Error handling
171
197
 
172
198
  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.9",
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,19 @@
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
+ logging[method] === undefined ? logging[method] = [args] : logging[method].push(args)
13
+ }
14
+ }
15
+
16
+ ;(async (send) => {
17
+ process.stdout.write = function () {}
9
18
  let value
10
19
  let isFulfilled
11
20
 
@@ -16,12 +25,11 @@ const generateTemplate = snippet => `
16
25
  value = ${SERIALIZE_ERROR}(error)
17
26
  isFulfilled = false
18
27
  } finally {
19
- console.log(JSON.stringify({
28
+ send(JSON.stringify({
20
29
  isFulfilled,
30
+ logging,
21
31
  value,
22
32
  profiling: { memory: process.memoryUsage().rss }
23
33
  }))
24
34
  }
25
- })()`
26
-
27
- module.exports = generateTemplate
35
+ })(process.stdout.write.bind(process.stdout))`