diff --git a/CHANGELOG.yaml b/CHANGELOG.yaml index 617c6a39..938d8de1 100644 --- a/CHANGELOG.yaml +++ b/CHANGELOG.yaml @@ -1,3 +1,6 @@ +unreleased: + fixed bugs: + - GH-1037 Reduced memory footprint by dereferencing bootCode 5.1.1: date: 2024-08-01 fixed bugs: diff --git a/lib/index.js b/lib/index.js index e35924e0..9f7d0b6d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,5 +1,4 @@ const _ = require('lodash'), - bootcode = require('./bootcode'), PostmanSandbox = require('./postman-sandbox'), PostmanSandboxFleet = require('./postman-sandbox-fleet'); @@ -17,14 +16,7 @@ module.exports = { } options = _.clone(options); - bootcode((err, code) => { - if (err) { return callback(err); } - if (!code) { return callback(new Error('sandbox: bootcode missing!')); } - - options.bootCode = code; // assign the code in options - - new PostmanSandbox().initialize({}, options, callback); - }); + new PostmanSandbox().initialize({}, options, callback); }, /** @@ -60,20 +52,13 @@ module.exports = { connectOptions = _.clone(connectOptions); - bootcode((err, code) => { - if (err) { return callback(err); } - if (!code) { return callback(new Error('sandbox: bootcode missing!')); } - - connectOptions.bootCode = code; + try { + const sandboxFleet = new PostmanSandboxFleet(registry, initOptions, connectOptions); - try { - const sandboxFleet = new PostmanSandboxFleet(registry, initOptions, connectOptions); - - return callback(null, sandboxFleet); - } - catch (err) { - return callback(err); - } - }); + return callback(null, sandboxFleet); + } + catch (err) { + return callback(err); + } } }; diff --git a/lib/postman-sandbox.js b/lib/postman-sandbox.js index bb4ed609..5c73b296 100644 --- a/lib/postman-sandbox.js +++ b/lib/postman-sandbox.js @@ -3,12 +3,23 @@ const _ = require('lodash'), UniversalVM = require('uvm'), PostmanEvent = require('postman-collection').Event, teleportJS = require('teleport-javascript'), + bootcode = require('./bootcode'), TO_WAIT_BUFFER = 500, // time to wait for sandbox to declare timeout CONSOLE_EVENT_NAME = 'execution.console', EXECUTION_TIMEOUT_ERROR_MESSAGE = 'sandbox not responding', BRIDGE_DISCONNECTING_ERROR_MESSAGE = 'sandbox: execution interrupted, bridge disconnecting.'; + +function getBootCode (callback) { + bootcode((err, code) => { + if (err) { return callback(err); } + if (!code) { return callback(new Error('sandbox: bootcode missing!')); } + + callback(null, code); + }); +} + class PostmanSandbox extends UniversalVM { constructor () { super(); @@ -28,27 +39,35 @@ class PostmanSandbox extends UniversalVM { // set the dispatch timeout of UVM based on what is set in options unless original options sends the same _.isFinite(this.connectOptions.timeout) && (this.executionTimeout = this.connectOptions.timeout); - super.connect(this.connectOptions, (err, context) => { + getBootCode((err, bootCode) => { if (err) { return callback(err); } - this.once('initialize', (err) => { - this.isReady = true; - this.hasTimedOut = false; + this.connectOptions.bootCode = bootCode; + + super.connect(this.connectOptions, (err, context) => { + delete this.connectOptions.bootCode; - this.on(CONSOLE_EVENT_NAME, (cursor, level, args) => { - if (this.connectOptions.serializeLogs) { - return this.emit('console', cursor, level, args); - } + if (err) { return callback(err); } - this.emit('console', cursor, level, ...teleportJS.parse(args)); + this.once('initialize', (err) => { + this.isReady = true; + this.hasTimedOut = false; + + this.on(CONSOLE_EVENT_NAME, (cursor, level, args) => { + if (this.connectOptions.serializeLogs) { + return this.emit('console', cursor, level, args); + } + + this.emit('console', cursor, level, ...teleportJS.parse(args)); + }); + + // eslint-disable-next-line n/callback-return + callback(err, context); + context = null; }); - // eslint-disable-next-line n/callback-return - callback(err, context); - context = null; + this.dispatch('initialize', this.initOptions); }); - - this.dispatch('initialize', this.initOptions); }); } diff --git a/test/unit/sandbox-sanity.test.js b/test/unit/sandbox-sanity.test.js index ec60aaa8..767690c3 100644 --- a/test/unit/sandbox-sanity.test.js +++ b/test/unit/sandbox-sanity.test.js @@ -292,4 +292,15 @@ describe('sandbox', function () { }); }); }); + + it('should not hold reference to bootCode after initialization', function (done) { + // TODO: Add a memory footprint test to ensure bootCode is not retained elsewhere + Sandbox.createContext({ debug: true }, function (err, ctx) { + if (err) { return done(err); } + ctx.on('error', done); + + expect(ctx.connectOptions.bootCode).to.be.undefined; + done(); + }); + }); });