Skip to content

Commit

Permalink
Merge branch 'develop' into feature/modify-mainModule
Browse files Browse the repository at this point in the history
  • Loading branch information
codenirvana authored Apr 2, 2024
2 parents 9b6b74e + 213923c commit 9f7a99a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
unreleased:
fixed bugs:
- GH-994 Modified process.mainModule to prevent access to the Module object
chores:
- GH-993 Moved deprecation warnings to be on the first usage of API

4.7.0:
date: 2024-04-01
Expand Down
4 changes: 2 additions & 2 deletions lib/sandbox/execute-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = function (scope, code, execution, console, timers, pmapi, onAss
}
else {
// prepare legacy environment, which adds a tonne of global variables
legacy.setup(scope, execution);
legacy.setup(scope, execution, console);
}

// prepare the scope's environment variables
Expand Down Expand Up @@ -54,7 +54,7 @@ module.exports = function (scope, code, execution, console, timers, pmapi, onAss
execution.return.async = (timers.queueLength() > 0);

// call this hook to perform any post script execution tasks
legacy.finish(scope, pmapi, console, onAssertion);
legacy.finish(scope, pmapi, onAssertion);

function complete () {
// if timers are running, we do not need to proceed with any logic of completing execution. instead we wait
Expand Down
46 changes: 28 additions & 18 deletions lib/sandbox/postman-legacy-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,22 @@ function raiseAssertionEvent (scope, pmapi, onAssertion) {
onAssertion(assertions);
}

function logDeprecationWarning (key, legacyUsageSet, console) {
// we've already logged warning once. ignore all next usages
if (legacyUsageSet.has(key)) {
return;
}

legacyUsageSet.add(key);

if (LEGACY_GLOBS_ALTERNATIVES[key]) {
console.warn(`Using "${key}" is deprecated. Use "${LEGACY_GLOBS_ALTERNATIVES[key]}" instead.`);
}
else if (LEGACY_GLOBS.includes(key)) {
console.warn(`Using "${key}" is deprecated.`);
}
}

class PostmanLegacyInterface {
/**
* @param {Object} execution -
Expand Down Expand Up @@ -279,10 +295,11 @@ module.exports = {
*
* @param {Uniscope} scope -
* @param {Execution} execution -
* @param {Object} console -
*
* @note ensure that globalvars variables added here are added as part of the LEGACY_GLOBS array
*/
setup (scope, execution) {
setup (scope, execution, console) {
/**
* @name SandboxGlobals
* @type {Object}
Expand Down Expand Up @@ -403,7 +420,9 @@ module.exports = {
globalvars.postman = new (execution.target === TARGET_TEST ?
PostmanLegacyTestInterface : PostmanLegacyInterface)(execution, globalvars);

scope.__postman_legacy_api_usage = new Set();
const legacyAPIUsage = new Set();

scope.__is_deprecation_warning_enabled = true;

// wrap all globals to ensure we track their usage to show warnings
// on access.
Expand All @@ -414,7 +433,7 @@ module.exports = {

globalvars[key] = new Proxy(globalvars[key], {
set (target, prop, value) {
scope.__postman_legacy_api_usage.add(key);
scope.__is_deprecation_warning_enabled && logDeprecationWarning(key, legacyAPIUsage, console);

target[prop] = value;
},
Expand All @@ -423,17 +442,18 @@ module.exports = {
// special handling for postman because setNextRequest is
// used a lot.
if (key === 'postman') {
scope.__postman_legacy_api_usage.add('postman.setNextRequest');
scope.__is_deprecation_warning_enabled &&
logDeprecationWarning('postman.setNextRequest', legacyAPIUsage, console);
}
else {
scope.__postman_legacy_api_usage.add(key);
scope.__is_deprecation_warning_enabled && logDeprecationWarning(key, legacyAPIUsage, console);
}

return target[prop];
},

apply (target, thisArg, args) {
scope.__postman_legacy_api_usage.add(key);
scope.__is_deprecation_warning_enabled && logDeprecationWarning(key, legacyAPIUsage, console);

return target.apply(thisArg, args);
}
Expand Down Expand Up @@ -469,24 +489,14 @@ module.exports = {
*
* @param {Uniscope} scope -
* @param {Object} pmapi -
* @param {Object} console -
* @param {Function} onAssertion -
*/
finish (scope, pmapi, console, onAssertion) {
finish (scope, pmapi, onAssertion) {
if (!scope.__postman_legacy_setup) {
return;
}

if (scope.__postman_legacy_api_usage) {
scope.__postman_legacy_api_usage.forEach((key) => {
if (LEGACY_GLOBS_ALTERNATIVES[key]) {
console.warn(`Using "${key}" is deprecated. Use "${LEGACY_GLOBS_ALTERNATIVES[key]}" instead.`);
}
else if (LEGACY_GLOBS.includes(key)) {
console.warn(`Using "${key}" is deprecated.`);
}
});
}
scope.__is_deprecation_warning_enabled = false;

raiseAssertionEvent(scope, pmapi, onAssertion);
},
Expand Down

0 comments on commit 9f7a99a

Please sign in to comment.