Skip to content

Commit

Permalink
scope-reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
coditva committed Jan 24, 2024
1 parent 3d1294f commit aac8010
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/sandbox/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ module.exports = function (bridge, glob) {
},
dispatchAssertions,
options.requireFileResolver,
scope,
new PostmanCookieStore(id, bridge, timers),
{
disabledAPIs: initializationOptions.disabledAPIs
Expand Down
26 changes: 18 additions & 8 deletions lib/sandbox/pmapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const _ = require('lodash'),
* @param {Object} [options] - options
* @param {Array.<String>} [options.disabledAPIs] - list of disabled APIs
*/
function Postman (execution, onRequest, onSkipRequest, onAssertion, requireFileResolver, cookieStore, options = {}) {
function Postman (execution, onRequest, onSkipRequest, onAssertion, requireFileResolver, scope, cookieStore, options = {}) {
// @todo - ensure runtime passes data in a scope format
let iterationData = new VariableScope();

Expand Down Expand Up @@ -299,15 +299,25 @@ function Postman (execution, onRequest, onSkipRequest, onAssertion, requireFileR
exports: {}
};

const fn = new Function('module', 'exports', file);
const pm = this;
const globalScope = {
pm
}; // how to access this?
const codeToExecute = [
'(function (module, exports) {',
file,
'})(moduleObj, moduleObj.exports);'
].join('\n');

fn.call(globalScope, module, module.exports);
const moduleObj = {
exports: {}
};

return module.exports;
scope.import({
moduleObj: moduleObj
});

scope.exec(codeToExecute, (err) => {});

// TODO: clean up moduleObj

return moduleObj.exports;
}
}, options.disabledAPIs);

Expand Down
23 changes: 23 additions & 0 deletions lib/sandbox/postman-require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Scope = require('uniscope'),
executeContext = require('./execute-context');

function createPostmanRequire (requireFileResolver, scope, execution, pmApi, pmConsole, timers, dispatchAssertions) {
return function postmanRequire (path) {
var file = requireFileResolver[path],
module = {
exports: {}
};

// const scope = Scope.create({
// eval: true,
// ignore: ['require'],
// block: ['bridge']
// });

scope.exec(file, () => {});

return module.exports;
};
}

module.exports = createPostmanRequire;

0 comments on commit aac8010

Please sign in to comment.