Skip to content

Commit 5921e9f

Browse files
committed
Move serializeLogs option to context-level
1 parent 9ec9ea6 commit 5921e9f

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

CHANGELOG.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
unreleased:
22
fixed bugs:
33
- Fixed memory leak due to console listeners
4+
chores:
5+
- Moved `serializeLogs` option to context-level instead of per-execute
46

57
4.2.3:
68
date: 2023-02-22

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ The following section outlines the API available inside sandbox scripts
6262
- execution.abort.*
6363
- execution.response.*
6464
- execution.cookies.*
65-
- execution.console.*
6665

6766
## Contributing
6867

lib/postman-sandbox.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const _ = require('lodash'),
55
teleportJS = require('teleport-javascript'),
66

77
TO_WAIT_BUFFER = 500, // time to wait for sandbox to declare timeout
8+
CONSOLE_EVENT_NAME = 'execution.console',
89
EXECUTION_TIMEOUT_ERROR_MESSAGE = 'sandbox not responding',
910
BRIDGE_DISCONNECTING_ERROR_MESSAGE = 'sandbox: execution interrupted, bridge disconnecting.';
1011

@@ -31,6 +32,14 @@ class PostmanSandbox extends UniversalVM {
3132
if (err) { return callback(err); }
3233

3334
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+
3443
// eslint-disable-next-line callback-return
3544
callback(err, context);
3645
context = null;
@@ -81,7 +90,6 @@ class PostmanSandbox extends UniversalVM {
8190
}
8291

8392
const id = _.isString(options.id) ? options.id : uuid(),
84-
consoleEventName = 'execution.console',
8593
executionEventName = 'execution.result.' + id,
8694
executionTimeout = _.get(options, 'timeout', this.executionTimeout),
8795
cursor = _.clone(_.get(options, 'cursor', {})), // clone the cursor as it travels through IPC for mutation
@@ -109,19 +117,10 @@ class PostmanSandbox extends UniversalVM {
109117
delete this._executing[id];
110118
}
111119

112-
this.removeAllListeners(consoleEventName);
113120
this.emit('execution', err, id, result);
114121
callback(err, result);
115122
});
116123

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-
125124
// send the code to the sandbox to be intercepted and executed
126125
this.dispatch('execute', id, target, _.get(options, 'context', {}), {
127126
cursor: cursor,
@@ -143,6 +142,7 @@ class PostmanSandbox extends UniversalVM {
143142
this.emit('execution.result.' + id, new Error(BRIDGE_DISCONNECTING_ERROR_MESSAGE));
144143
});
145144

145+
this.removeAllListeners(CONSOLE_EVENT_NAME);
146146
this.disconnect();
147147
}
148148
}

test/unit/sandbox-console.test.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ describe('console inside sandbox', function () {
238238
});
239239

240240
it('should allow sending serialized logs', function (done) {
241-
Sandbox.createContext({}, function (err, ctx) {
241+
Sandbox.createContext({ serializeLogs: true }, function (err, ctx) {
242242
var logsData = {
243243
undef: undefined,
244244
str: 'string'
@@ -255,9 +255,7 @@ describe('console inside sandbox', function () {
255255
consoleEventArgs = arguments;
256256
});
257257

258-
ctx.execute('console.log({ undef: undefined, str: "string" });', {
259-
serializeLogs: true
260-
}, function (err) {
258+
ctx.execute('console.log({ undef: undefined, str: "string" });', {}, function (err) {
261259
if (err) {
262260
return done(err);
263261
}
@@ -300,7 +298,7 @@ describe('console inside sandbox', function () {
300298
});
301299

302300
it('should allow calling console.log without arguments with serializeLogs options set', function (done) {
303-
Sandbox.createContext({}, function (err, ctx) {
301+
Sandbox.createContext({ serializeLogs: true }, function (err, ctx) {
304302
var consoleEventArgs;
305303

306304
if (err) {
@@ -312,9 +310,7 @@ describe('console inside sandbox', function () {
312310
consoleEventArgs = arguments;
313311
});
314312

315-
ctx.execute('console.log();', {
316-
serializeLogs: true
317-
}, function (err) {
313+
ctx.execute('console.log();', {}, function (err) {
318314
if (err) {
319315
return done(err);
320316
}

0 commit comments

Comments
 (0)