Skip to content

Commit

Permalink
Make pm.require synchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
coditva committed Dec 4, 2023
1 parent 9b1ca8b commit 9e87272
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .cache/bootcode.browser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .cache/bootcode.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion lib/postman-sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ class PostmanSandbox extends UniversalVM {
cursor: cursor,
debug: debugMode,
timeout: executionTimeout,
legacy: _.get(options, 'legacy')
legacy: _.get(options, 'legacy'),
fileCache: _.get(options, 'fileCache')
});
}

Expand Down
3 changes: 2 additions & 1 deletion lib/sandbox/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ module.exports = function (bridge, glob) {
dispatchAssertions,
new PostmanCookieStore(id, bridge, timers),
{
disabledAPIs: initializationOptions.disabledAPIs
disabledAPIs: initializationOptions.disabledAPIs,
fileCache: options.fileCache
})
),
dispatchAssertions,
Expand Down
4 changes: 3 additions & 1 deletion lib/sandbox/pmapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ function Postman (execution, onRequest, onSkipRequest, onAssertion, cookieStore,
* @param {String} path - path to the script file
*/
require: function (path) {
return postmanRequire(path);
return postmanRequire(path, null, {
fileCache: options.fileCache
});
}
}, options.disabledAPIs);

Expand Down
26 changes: 11 additions & 15 deletions lib/sandbox/require.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
const path = require('path');

function readFileSync (path) {
return `
const parent = require('.');
module.exports = {
name: "${path}",
module: module,
sum: function (a, b) {
return a + b;
}
};
`;
function readFileFromCache (path, options) {
const cache = options.fileCache;

if (!cache || !cache[path]) {
throw new Error('Count not find file in cache: ' + path);
}

return cache[path];
}

class Module {
Expand All @@ -35,13 +31,13 @@ class Module {
}
}

function postmanRequire (filePath, parentModule) {
function postmanRequire (filePath, parentModule, options) {
if (filePath in postmanRequire.cache) {
return postmanRequire.cache[filePath].exports;
}

const module = new Module(filePath, parentModule),
code = readFileSync(filePath),
code = readFileFromCache(filePath, options),

// TODO: is this safe??
wrapperFn = new Function('exports', 'require', 'module', '__filename',

Check failure on line 43 in lib/sandbox/require.js

View workflow job for this annotation

GitHub Actions / Lint

The Function constructor is eval
Expand All @@ -55,7 +51,7 @@ function postmanRequire (filePath, parentModule) {
require = function require (id) {
this.cache = postmanRequire.cache;

return postmanRequire(id, module);
return postmanRequire(id, module, options);
};

// cache it before evaluating because circular dependencies should not call
Expand Down

0 comments on commit 9e87272

Please sign in to comment.