-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(skip-execution): add support to display system logs #957
base: feat/skip-request
Are you sure you want to change the base?
Changes from all commits
5d00855
323de69
1c1c2c4
50b3d00
d442bcf
ebb2342
d0a926f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Why Do we need this wrapper? | ||
* Because when user executes pm.request.stopExecution(), we need to stop the execution of the current request. | ||
* But, we don't stop the execution of the script. We just stop sending events to the bridge. | ||
* | ||
* @param {any} bridge - the bridge object | ||
* @param {{ shouldSkipExecution: boolean }} execution - the execution object | ||
* @param {string} event - the event name | ||
* @param {...any} args - the arguments to be passed to the event | ||
*/ | ||
module.exports = function dispatchEvent (bridge, execution, event, ...args) { | ||
// if the execution is skipped, do not dispatch the event | ||
if (execution && execution.shouldSkipExecution) { | ||
return; | ||
} | ||
|
||
bridge.dispatch(event, ...args); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ const _ = require('lodash'), | |
sdk = require('postman-collection'), | ||
PostmanEvent = sdk.Event, | ||
Execution = require('./execution'), | ||
PostmanConsole = require('./console'), | ||
{ default: PostmanConsole, dispatchSystemMessage } = require('./console'), | ||
PostmanTimers = require('./timers'), | ||
PostmanAPI = require('./pmapi'), | ||
PostmanCookieStore = require('./cookie-store'), | ||
|
@@ -20,7 +20,8 @@ const _ = require('lodash'), | |
EXECUTION_ASSERTION_EVENT = 'execution.assertion', | ||
EXECUTION_ASSERTION_EVENT_BASE = 'execution.assertion.', | ||
|
||
executeContext = require('./execute-context'); | ||
executeContext = require('./execute-context'), | ||
dispatchEvent = require('./dispatch-event'); | ||
|
||
module.exports = function (bridge, glob) { | ||
// @note we use a common scope for all executions. this causes issues when scripts are run inside the sandbox | ||
|
@@ -49,7 +50,7 @@ module.exports = function (bridge, glob) { | |
if (!template) { | ||
chai.use(require('chai-postman')(sdk, _, Ajv)); | ||
|
||
return bridge.dispatch('initialize'); | ||
return dispatchEvent(bridge, null, 'initialize'); | ||
} | ||
|
||
const _module = { exports: {} }, | ||
|
@@ -66,7 +67,7 @@ module.exports = function (bridge, glob) { | |
|
||
scope.exec(template, (err) => { | ||
if (err) { | ||
return bridge.dispatch('initialize', err); | ||
return dispatchEvent(bridge, null, 'initialize', err); | ||
} | ||
|
||
const { chaiPlugin, initializeExecution: setupExecution } = (_module && _module.exports) || {}; | ||
|
@@ -79,7 +80,7 @@ module.exports = function (bridge, glob) { | |
initializeExecution = setupExecution; | ||
} | ||
|
||
bridge.dispatch('initialize'); | ||
dispatchEvent(bridge, null, 'initialize'); | ||
}); | ||
}); | ||
|
||
|
@@ -97,7 +98,8 @@ module.exports = function (bridge, glob) { | |
*/ | ||
bridge.on('execute', function (id, event, context, options) { | ||
if (!(id && _.isString(id))) { | ||
return bridge.dispatch('error', new Error('sandbox: execution identifier parameter(s) missing')); | ||
return dispatchEvent(bridge, null, 'error', | ||
new Error('sandbox: execution identifier parameter(s) missing')); | ||
} | ||
|
||
!options && (options = {}); | ||
|
@@ -136,20 +138,21 @@ module.exports = function (bridge, glob) { | |
// For compatibility, dispatch the single assertion as an array. | ||
!Array.isArray(assertions) && (assertions = [assertions]); | ||
|
||
bridge.dispatch(assertionEventName, options.cursor, assertions); | ||
bridge.dispatch(EXECUTION_ASSERTION_EVENT, options.cursor, assertions); | ||
dispatchEvent(bridge, execution, assertionEventName, options.cursor, assertions); | ||
dispatchEvent(bridge, execution, EXECUTION_ASSERTION_EVENT, options.cursor, assertions); | ||
}; | ||
|
||
let waiting, | ||
timers; | ||
timers, | ||
postmanConsole; | ||
|
||
execution.return.async = false; | ||
|
||
// create the controlled timers | ||
timers = new PostmanTimers(null, function (err) { | ||
if (err) { // propagate the error out of sandbox | ||
bridge.dispatch(errorEventName, options.cursor, err); | ||
bridge.dispatch(EXECUTION_ERROR_EVENT, options.cursor, err); | ||
dispatchEvent(bridge, execution, errorEventName, options.cursor, err); | ||
dispatchEvent(bridge, execution, EXECUTION_ERROR_EVENT, options.cursor, err); | ||
} | ||
}, function () { | ||
execution.return.async = true; | ||
|
@@ -169,16 +172,22 @@ module.exports = function (bridge, glob) { | |
bridge.off(cookiesEventName); | ||
|
||
if (err) { // fire extra execution error event | ||
bridge.dispatch(errorEventName, options.cursor, err); | ||
bridge.dispatch(EXECUTION_ERROR_EVENT, options.cursor, err); | ||
dispatchEvent(bridge, execution, errorEventName, options.cursor, err); | ||
dispatchEvent(bridge, execution, EXECUTION_ERROR_EVENT, options.cursor, err); | ||
} | ||
|
||
// @note delete response from the execution object to avoid dispatching | ||
// the large response payload back due to performance reasons. | ||
execution.response && (delete execution.response); | ||
|
||
// fire the execution completion event | ||
(dnd !== true) && bridge.dispatch(executionEventName, err || null, execution); | ||
|
||
// note: We are sending shouldSkipExecution: false to dispatchEvent function | ||
// because this event should be fired even if shouldSkipExecution is true as this event is | ||
// used to complete the execution in the sandbox. All other events are fired only if | ||
// shouldSkipExecution is false. | ||
(dnd !== true) && dispatchEvent(bridge, { shouldSkipExecution: false }, | ||
executionEventName, err || null, execution); | ||
}); | ||
|
||
// if a timeout is set, we must ensure that all pending timers are cleared and an execution timeout event is | ||
|
@@ -203,18 +212,27 @@ module.exports = function (bridge, glob) { | |
timers.clearEvent(id, err, res); | ||
}); | ||
|
||
postmanConsole = new PostmanConsole(bridge, options.cursor, options.debug && glob.console, execution); | ||
|
||
// 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)), | ||
postmanConsole, | ||
timers, | ||
( | ||
new PostmanAPI(execution, function (request, callback) { | ||
var eventId = timers.setEvent(callback); | ||
|
||
bridge.dispatch(executionRequestEventName, options.cursor, id, eventId, request); | ||
}, dispatchAssertions, new PostmanCookieStore(id, bridge, timers), { | ||
dispatchEvent(bridge, execution, executionRequestEventName, options.cursor, id, eventId, request); | ||
}, /* onStopExecution = */ function () { | ||
// Dispatch event to display system message on console informing user that the request | ||
// execution was skipped from script | ||
dispatchSystemMessage(postmanConsole, 'skip_request', _.get(execution, 'legacy._itemName')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why dispatch a system type console event? Why not a "execution_stopped" or such event? Every client can then interpret it and use it however they want to. Also, "system" type log is not supported in Console interface either. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can also be a part of any existing events like |
||
execution.shouldSkipExecution = true; | ||
timers.terminate(null); | ||
}, | ||
dispatchAssertions, new PostmanCookieStore(id, bridge, timers, execution), { | ||
disabledAPIs: initializationOptions.disabledAPIs | ||
}) | ||
), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think if this should be part of this module. Maybe a helper?