Skip to content

Commit

Permalink
file-resolver-without-global-scope
Browse files Browse the repository at this point in the history
  • Loading branch information
coditva committed Jan 23, 2024
1 parent 903677b commit 3d1294f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
2 changes: 2 additions & 0 deletions lib/postman-sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class PostmanSandbox extends UniversalVM {
executionEventName = 'execution.result.' + id,
executionTimeout = _.get(options, 'timeout', this.executionTimeout),
cursor = _.clone(_.get(options, 'cursor', {})), // clone the cursor as it travels through IPC for mutation
requireFileResolver = _.get(options, 'requireFileResolver'),
debugMode = _.has(options, 'debug') ? options.debug : this.debug;

let waiting;
Expand Down Expand Up @@ -126,6 +127,7 @@ class PostmanSandbox extends UniversalVM {
cursor: cursor,
debug: debugMode,
timeout: executionTimeout,
requireFileResolver,
legacy: _.get(options, 'legacy')
});
}
Expand Down
25 changes: 15 additions & 10 deletions lib/sandbox/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,11 @@ module.exports = function (bridge, glob) {
timers.clearEvent(id, err, res);
});

// send control to the function that executes the context and prepares the scope
executeContext(scope, code, execution,
// if a console is sent, we use it. otherwise this also prevents erroneous referencing to any console
// inside this closure.
(new PostmanConsole(bridge, options.cursor, options.debug && glob.console)),
timers,
(
new PostmanAPI(execution,
const pmConsole = new PostmanConsole(bridge, options.cursor, options.debug && glob.console);

Check failure on line 208 in lib/sandbox/execute.js

View workflow job for this annotation

GitHub Actions / Lint

Combine this with the previous 'const' statement

let pmApi;

Check failure on line 210 in lib/sandbox/execute.js

View workflow job for this annotation

GitHub Actions / Lint

Combine this with the previous 'let' statement

pmApi = new PostmanAPI(execution,
function onRequest (request, callback) {

Check failure on line 213 in lib/sandbox/execute.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 12 spaces but found 20
var eventId = timers.setEvent(callback);

Check failure on line 214 in lib/sandbox/execute.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 16 spaces but found 24

Expand All @@ -227,11 +224,19 @@ module.exports = function (bridge, glob) {
timers.terminate(null);
},
dispatchAssertions,
options.requireFileResolver,
new PostmanCookieStore(id, bridge, timers),
{
disabledAPIs: initializationOptions.disabledAPIs
})
),
});

// send control to the function that executes the context and prepares the scope
executeContext(scope, code, execution,
// if a console is sent, we use it. otherwise this also prevents erroneous referencing to any console
// inside this closure.
pmConsole,
timers,
pmApi,
dispatchAssertions,
{ disableLegacyAPIs: initializationOptions.disableLegacyAPIs });
});
Expand Down
19 changes: 18 additions & 1 deletion 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, cookieStore, options = {}) {
function Postman (execution, onRequest, onSkipRequest, onAssertion, requireFileResolver, cookieStore, options = {}) {
// @todo - ensure runtime passes data in a scope format
let iterationData = new VariableScope();

Expand Down Expand Up @@ -291,6 +291,23 @@ function Postman (execution, onRequest, onSkipRequest, onAssertion, cookieStore,
*/
current: execution.legacy._eventItemName
})
},

require: function require (name) {
var file = requireFileResolver[name],
module = {
exports: {}
};

const fn = new Function('module', 'exports', file);
const pm = this;
const globalScope = {
pm
}; // how to access this?

fn.call(globalScope, module, module.exports);

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

Expand Down

0 comments on commit 3d1294f

Please sign in to comment.