-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
221 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class PostmanRequireStore { | ||
constructor (fileCache) { | ||
this.fileCache = fileCache || {}; | ||
} | ||
|
||
getResolvedPath (path) { | ||
if (this.hasFile(path)) { | ||
return path; | ||
} | ||
} | ||
|
||
hasFile (path) { | ||
return Boolean(this.fileCache[path]); | ||
} | ||
|
||
getFileData (path) { | ||
return this.hasFile(path) && this.fileCache[path].data; | ||
} | ||
} | ||
|
||
module.exports = PostmanRequireStore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,12 @@ const _ = require('lodash'), | |
PostmanCookieList = sdk.CookieList, | ||
chai = require('chai'), | ||
|
||
MODULE_KEY = '__module_obj', // why not use `module`? | ||
MODULE_WRAPPER = [ | ||
'(function (exports, module) {\n', | ||
`\n})(${MODULE_KEY}.exports, ${MODULE_KEY});` | ||
], | ||
|
||
/** | ||
* Use this function to assign readonly properties to an object | ||
* | ||
|
@@ -50,7 +56,7 @@ const _ = require('lodash'), | |
* @param {Object} [options] - options | ||
Check failure on line 56 in lib/sandbox/pmapi.js GitHub Actions / Lint
|
||
* @param {Array.<String>} [options.disabledAPIs] - list of disabled APIs | ||
*/ | ||
function Postman (execution, onRequest, onSkipRequest, onAssertion, requireFileResolver, scope, cookieStore, options = {}) { | ||
function Postman (execution, onRequest, onSkipRequest, onAssertion, cookieStore, requireStore, scope, options = {}) { | ||
// @todo - ensure runtime passes data in a scope format | ||
let iterationData = new VariableScope(); | ||
|
||
|
@@ -294,39 +300,55 @@ function Postman (execution, onRequest, onSkipRequest, onAssertion, requireFileR | |
}, | ||
|
||
require: function require (name) { | ||
var file = (requireFileResolver || {})[name]; | ||
const path = requireStore.getResolvedPath(name); | ||
|
||
if (!file || !file.data) { | ||
throw new Error(`Cannot find module '${name}'`); | ||
this.cache = this.cache || {}; | ||
|
||
// Always use the resolved path as the ID of the module. This | ||
// ensures that relative paths are handled correctly. | ||
if (this.cache[path]) { | ||
return this.cache[path].exports; | ||
} | ||
|
||
const codeToExecute = [ | ||
'(function (module, exports) {', | ||
file.data, | ||
'})(__module_obj, __module_obj.exports);' | ||
].join('\n'); | ||
const file = path && requireStore.getFileData(path); | ||
|
||
if (!file) { | ||
// Error should contain the name exactly as the user specified. | ||
throw new Error(`Cannot find module '${name}'`); | ||
} | ||
|
||
const moduleObj = { | ||
id: path, | ||
exports: {} | ||
}; | ||
|
||
// when is this cache cleaned up? Memory leak??? | ||
this.__cache = this.__cache || {}; | ||
|
||
if (this.__cache[name]) { | ||
return this.__cache[name].exports; | ||
} | ||
// Add to cache before executing. This ensures that any dependency | ||
// that tries to import it's parent/ancestor gets the cached | ||
// version and not end up in infinite loop. | ||
this.cache[moduleObj.id] = moduleObj; | ||
|
||
this.__cache[name] = moduleObj; | ||
const wrappedModule = MODULE_WRAPPER[0] + file + MODULE_WRAPPER[1]; | ||
|
||
scope.import({ __module_obj: moduleObj }); | ||
scope.import({ | ||
[MODULE_KEY]: moduleObj | ||
}); | ||
|
||
scope.exec(codeToExecute, { async: true, resetLocals: false }, (err) => { | ||
scope.unset('__module_obj'); | ||
throw err; | ||
// Note: We're executing the code in the same scope as the one | ||
// which called the `pm.require` function. This is because we want | ||
// to share the global scope across all the required modules. Any | ||
// locals are available inside the required modules and any locals | ||
// created inside the required modules are available to the parent. | ||
// | ||
// Why `async` = true? | ||
// - We want to allow execution of async code like setTimeout etc. | ||
scope.exec(wrappedModule, true, (err) => { | ||
// Bubble up the error to be caught as execution error | ||
if (err) { | ||
throw err; | ||
} | ||
}); | ||
|
||
scope.unset('__module_obj'); | ||
scope.unset(MODULE_KEY); | ||
|
||
return moduleObj.exports; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters