-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow pm.require API to import packages in sandbox (#1377)
- Loading branch information
Showing
6 changed files
with
252 additions
and
103 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
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 |
---|---|---|
|
@@ -238,6 +238,8 @@ module.exports = { | |
abortOnFailure = payload.abortOnFailure, | ||
stopOnFailure = payload.stopOnFailure, | ||
|
||
packageResolver = _.get(this, 'options.script.packageResolver'), | ||
|
||
events; | ||
|
||
// @todo: find a better place to code this so that event is not aware of such options | ||
|
@@ -442,109 +444,135 @@ module.exports = { | |
shouldSkipExecution = true; | ||
}); | ||
|
||
const currentEventItem = event.parent && event.parent(); | ||
|
||
// finally execute the script | ||
this.host.execute(event, { | ||
id: executionId, | ||
// debug: true, | ||
timeout: payload.scriptTimeout, // @todo: Expose this as a property in Collection SDK's Script | ||
cursor: scriptCursor, | ||
context: _.pick(payload.context, SAFE_CONTEXT_VARIABLES), | ||
|
||
// legacy options | ||
legacy: { | ||
_itemId: item.id, | ||
_itemName: item.name, | ||
_itemPath: item.getPath && item.getPath(), | ||
_eventItemName: currentEventItem && currentEventItem.name | ||
} | ||
}, function (err, result) { | ||
this.host.removeAllListeners(EXECUTION_REQUEST_EVENT_BASE + executionId); | ||
this.host.removeAllListeners(EXECUTION_ASSERTION_EVENT_BASE + executionId); | ||
this.host.removeAllListeners(EXECUTION_RESPONSE_EVENT_BASE + executionId); | ||
this.host.removeAllListeners(EXECUTION_COOKIES_EVENT_BASE + executionId); | ||
this.host.removeAllListeners(EXECUTION_ERROR_EVENT_BASE + executionId); | ||
this.host.removeAllListeners(EXECUTION_SKIP_REQUEST_EVENT_BASE + executionId); | ||
|
||
// Handle async errors as well. | ||
// If there was an error running the script itself, that takes precedence | ||
if (!err && asyncScriptError) { | ||
err = asyncScriptError; | ||
} | ||
const currentEventItem = event.parent && event.parent(), | ||
|
||
executeScript = ({ resolvedPackages }, done) => { | ||
// finally execute the script | ||
this.host.execute(event, { | ||
id: executionId, | ||
// debug: true, | ||
|
||
// @todo: Expose this as a property in Collection SDK's Script | ||
timeout: payload.scriptTimeout, | ||
cursor: scriptCursor, | ||
context: _.pick(payload.context, SAFE_CONTEXT_VARIABLES), | ||
resolvedPackages: resolvedPackages, | ||
|
||
// legacy options | ||
legacy: { | ||
_itemId: item.id, | ||
_itemName: item.name, | ||
_itemPath: item.getPath && item.getPath(), | ||
_eventItemName: currentEventItem && currentEventItem.name | ||
} | ||
}, function (err, result) { | ||
this.host.removeAllListeners(EXECUTION_REQUEST_EVENT_BASE + executionId); | ||
this.host.removeAllListeners(EXECUTION_ASSERTION_EVENT_BASE + executionId); | ||
this.host.removeAllListeners(EXECUTION_RESPONSE_EVENT_BASE + executionId); | ||
this.host.removeAllListeners(EXECUTION_COOKIES_EVENT_BASE + executionId); | ||
this.host.removeAllListeners(EXECUTION_ERROR_EVENT_BASE + executionId); | ||
this.host.removeAllListeners(EXECUTION_SKIP_REQUEST_EVENT_BASE + executionId); | ||
|
||
// Handle async errors as well. | ||
// If there was an error running the script itself, that takes precedence | ||
if (!err && asyncScriptError) { | ||
err = asyncScriptError; | ||
} | ||
|
||
// electron IPC does not bubble errors to the browser process, so we serialize it here. | ||
err && (err = serialisedError(err, true)); | ||
// electron IPC does not bubble errors to the browser process, so we serialize it here. | ||
err && (err = serialisedError(err, true)); | ||
|
||
// if it is defined that certain variables are to be synced back to result, we do the same | ||
track && result && track.forEach(function (variable) { | ||
if (!(_.isObject(result[variable]) && payload.context[variable])) { return; } | ||
// if it is defined that certain variables are to be synced back to result, we do the same | ||
track && result && track.forEach(function (variable) { | ||
if (!(_.isObject(result[variable]) && payload.context[variable])) { return; } | ||
|
||
var contextVariable = payload.context[variable], | ||
mutations = result[variable].mutations; | ||
var contextVariable = payload.context[variable], | ||
mutations = result[variable].mutations; | ||
|
||
// bail out if there are no mutations | ||
if (!mutations) { | ||
return; | ||
} | ||
// bail out if there are no mutations | ||
if (!mutations) { | ||
return; | ||
} | ||
|
||
// ensure that variable scope is treated accordingly | ||
if (_.isFunction(contextVariable.applyMutation)) { | ||
mutations = new sdk.MutationTracker(result[variable].mutations); | ||
// ensure that variable scope is treated accordingly | ||
if (_.isFunction(contextVariable.applyMutation)) { | ||
mutations = new sdk.MutationTracker(result[variable].mutations); | ||
|
||
mutations.applyOn(contextVariable); | ||
} | ||
mutations.applyOn(contextVariable); | ||
} | ||
|
||
// @todo: unify the non variable scope flows and consume diff always | ||
// and drop sending the full variable scope from sandbox | ||
else { | ||
util.syncObject(contextVariable, result[variable]); | ||
} | ||
}); | ||
// @todo: unify the non variable scope flows and consume diff always | ||
// and drop sending the full variable scope from sandbox | ||
else { | ||
util.syncObject(contextVariable, result[variable]); | ||
} | ||
}); | ||
|
||
// Get the failures. If there was an error running the script itself, that takes precedence | ||
if (!err && (abortOnFailure || stopOnFailure)) { | ||
err = postProcessContext(result, assertionFailed); // also use async assertions | ||
} | ||
|
||
// Ensure that we have SDK instances, not serialized plain objects. | ||
// @todo - should this be handled by the sandbox? | ||
result && result._variables && | ||
(result._variables = new sdk.VariableScope(result._variables)); | ||
result && result.environment && | ||
(result.environment = new sdk.VariableScope(result.environment)); | ||
result && result.globals && (result.globals = new sdk.VariableScope(result.globals)); | ||
result && result.collectionVariables && | ||
(result.collectionVariables = new sdk.VariableScope(result.collectionVariables)); | ||
result && result.request && (result.request = new sdk.Request(result.request)); | ||
|
||
// @note Since [email protected], response object is not included in the execution | ||
// result. | ||
// Refer: https://github.com/postmanlabs/postman-sandbox/pull/512 | ||
// Adding back here to avoid breaking change in `script` callback. | ||
// @todo revisit script callback args in runtime v8. | ||
result && payload.context && payload.context.response && | ||
(result.response = new sdk.Response(payload.context.response)); | ||
|
||
// persist the pm.variables for the next script | ||
result && result._variables && | ||
(payload.context._variables = new sdk.VariableScope(result._variables)); | ||
|
||
// persist the pm.variables for the next request | ||
result && result._variables && | ||
(this.state._variables = new sdk.VariableScope(result._variables)); | ||
|
||
// persist the mutated request in payload context, | ||
// @note this will be used for the next prerequest script or | ||
// upcoming commands(request, httprequest). | ||
result && result.request && (payload.context.request = result.request); | ||
|
||
// now that this script is done executing, we trigger the event and move to the next script | ||
this.triggers.script(err || null, scriptCursor, result, script, event, item); | ||
|
||
// move to next script and pass on the results for accumulation | ||
done(((stopOnScriptError || abortOnError || stopOnFailure) && err) ? err : null, _.assign({ | ||
event, | ||
script, | ||
result | ||
}, err && { error: err })); // we use assign here to avoid needless error property | ||
}.bind(this)); | ||
}; | ||
|
||
if (!packageResolver) { | ||
// If packageResolver is not present, make sure that | ||
// resolvedPackages is undefined. This ensures that | ||
// pm.require command will not be present in the sandbox. | ||
return executeScript({}, next); | ||
} | ||
|
||
// Get the failures. If there was an error running the script itself, that takes precedence | ||
if (!err && (abortOnFailure || stopOnFailure)) { | ||
err = postProcessContext(result, assertionFailed); // also use async assertions | ||
return packageResolver({ packages: script.packages }, (err, resolvedPackages) => { | ||
if (err) { | ||
return next(err); | ||
} | ||
|
||
// Ensure that we have SDK instances, not serialized plain objects. | ||
// @todo - should this be handled by the sandbox? | ||
result && result._variables && (result._variables = new sdk.VariableScope(result._variables)); | ||
result && result.environment && (result.environment = new sdk.VariableScope(result.environment)); | ||
result && result.globals && (result.globals = new sdk.VariableScope(result.globals)); | ||
result && result.collectionVariables && | ||
(result.collectionVariables = new sdk.VariableScope(result.collectionVariables)); | ||
result && result.request && (result.request = new sdk.Request(result.request)); | ||
|
||
// @note Since [email protected], response object is not included in the execution result. | ||
// Refer: https://github.com/postmanlabs/postman-sandbox/pull/512 | ||
// Adding back here to avoid breaking change in `script` callback. | ||
// @todo revisit script callback args in runtime v8. | ||
result && payload.context && payload.context.response && | ||
(result.response = new sdk.Response(payload.context.response)); | ||
|
||
// persist the pm.variables for the next script | ||
result && result._variables && | ||
(payload.context._variables = new sdk.VariableScope(result._variables)); | ||
|
||
// persist the pm.variables for the next request | ||
result && result._variables && (this.state._variables = new sdk.VariableScope(result._variables)); | ||
|
||
// persist the mutated request in payload context, | ||
// @note this will be used for the next prerequest script or | ||
// upcoming commands(request, httprequest). | ||
result && result.request && (payload.context.request = result.request); | ||
|
||
// now that this script is done executing, we trigger the event and move to the next script | ||
this.triggers.script(err || null, scriptCursor, result, script, event, item); | ||
|
||
// move to next script and pass on the results for accumulation | ||
next(((stopOnScriptError || abortOnError || stopOnFailure) && err) ? err : null, _.assign({ | ||
event, | ||
script, | ||
result | ||
}, err && { error: err })); // we use assign here to avoid needless error property | ||
}.bind(this)); | ||
// Note that we don't check if resolvedPackages is valid | ||
// here but let sandbox handle it. | ||
return executeScript({ resolvedPackages }, next); | ||
}); | ||
}.bind(this), function (err, results) { | ||
// trigger the event completion callback | ||
this.triggers[eventName](null, cursor, results, item); | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.