@@ -5,6 +5,7 @@ const _ = require('lodash'),
5
5
teleportJS = require ( 'teleport-javascript' ) ,
6
6
7
7
TO_WAIT_BUFFER = 500 , // time to wait for sandbox to declare timeout
8
+ CONSOLE_EVENT_NAME = 'execution.console' ,
8
9
EXECUTION_TIMEOUT_ERROR_MESSAGE = 'sandbox not responding' ,
9
10
BRIDGE_DISCONNECTING_ERROR_MESSAGE = 'sandbox: execution interrupted, bridge disconnecting.' ;
10
11
@@ -31,6 +32,14 @@ class PostmanSandbox extends UniversalVM {
31
32
if ( err ) { return callback ( err ) ; }
32
33
33
34
this . once ( 'initialize' , ( err ) => {
35
+ this . on ( CONSOLE_EVENT_NAME , ( cursor , level , args ) => {
36
+ if ( connectOptions . serializeLogs ) {
37
+ return this . emit ( 'console' , cursor , level , args ) ;
38
+ }
39
+
40
+ this . emit ( 'console' , cursor , level , ...teleportJS . parse ( args ) ) ;
41
+ } ) ;
42
+
34
43
// eslint-disable-next-line callback-return
35
44
callback ( err , context ) ;
36
45
context = null ;
@@ -81,7 +90,6 @@ class PostmanSandbox extends UniversalVM {
81
90
}
82
91
83
92
const id = _ . isString ( options . id ) ? options . id : uuid ( ) ,
84
- consoleEventName = 'execution.console' ,
85
93
executionEventName = 'execution.result.' + id ,
86
94
executionTimeout = _ . get ( options , 'timeout' , this . executionTimeout ) ,
87
95
cursor = _ . clone ( _ . get ( options , 'cursor' , { } ) ) , // clone the cursor as it travels through IPC for mutation
@@ -109,19 +117,10 @@ class PostmanSandbox extends UniversalVM {
109
117
delete this . _executing [ id ] ;
110
118
}
111
119
112
- this . removeAllListeners ( consoleEventName ) ;
113
120
this . emit ( 'execution' , err , id , result ) ;
114
121
callback ( err , result ) ;
115
122
} ) ;
116
123
117
- this . on ( consoleEventName , ( cursor , level , args ) => {
118
- if ( _ . get ( options , 'serializeLogs' ) ) {
119
- return this . emit ( 'console' , cursor , level , args ) ;
120
- }
121
-
122
- this . emit ( 'console' , cursor , level , ...teleportJS . parse ( args ) ) ;
123
- } ) ;
124
-
125
124
// send the code to the sandbox to be intercepted and executed
126
125
this . dispatch ( 'execute' , id , target , _ . get ( options , 'context' , { } ) , {
127
126
cursor : cursor ,
@@ -143,6 +142,7 @@ class PostmanSandbox extends UniversalVM {
143
142
this . emit ( 'execution.result.' + id , new Error ( BRIDGE_DISCONNECTING_ERROR_MESSAGE ) ) ;
144
143
} ) ;
145
144
145
+ this . removeAllListeners ( CONSOLE_EVENT_NAME ) ;
146
146
this . disconnect ( ) ;
147
147
}
148
148
}
0 commit comments