@@ -22,11 +22,11 @@ var _ = require('lodash'),
2222 EXECUTION_ERROR_EVENT_BASE = 'execution.error.' ,
2323 EXECUTION_COOKIES_EVENT_BASE = 'execution.cookies.' ,
2424 EXECUTION_SKIP_REQUEST_EVENT_BASE = 'execution.skipRequest.' ,
25- EXECUTION_GET_VARIABLES_FROM_THIS_EXECUTION = 'execution.get_variables_from_this_execution.' ,
26- EXECUTION_REPLY_TO_VARIABLES_REQUEST = 'execution.response_to_variables_request.' ,
2725
2826 EXECUTION_VAULT_BASE = 'execution.vault.' ,
2927
28+ CONTEXT_VARIABLE_SCOPES = [ '_variables' , 'environment' , 'globals' ] ,
29+
3030 COOKIES_EVENT_STORE_ACTION = 'store' ,
3131 COOKIE_STORE_PUT_METHOD = 'putCookie' ,
3232 COOKIE_STORE_UPDATE_METHOD = 'updateCookie' ,
@@ -515,7 +515,7 @@ module.exports = {
515515
516516 // Should fetch the request from the enclosing Postman App & resolve scripts and variables
517517 containerRequestPassedOptions . requestResolverBridge ( requestToRunId ,
518- async function ( err , collectionWithReqToRun ) {
518+ function ( err , collectionWithReqToRun ) {
519519 if ( err ) {
520520 return dispatchErrorToListener ( err ) ;
521521 }
@@ -531,39 +531,31 @@ module.exports = {
531531 // variables set by the parent using pm.<variable-form>.set do not reflect
532532 // in postman-runtime's scope immediately. They are present inside postman-sandbox
533533 // till the parent request's script execution ends.
534- const variablesRequestUniqueId = uuid . v4 ( ) ,
534+ const variableValues = nestedRequestOptions . currentScopeVariableValues || { } ,
535535 globals = { values : [ ] } ,
536536 localVariables = { values : [ ] } ,
537537 environment = { values : [ ] } ,
538538 collectionVariables = { values : [ ] } ;
539539
540- await new Promise ( ( resolve ) => {
541- self . host . once ( EXECUTION_REPLY_TO_VARIABLES_REQUEST + variablesRequestUniqueId ,
542- function ( variableValues ) {
543- if ( variableValues . globals && variableValues . globals . length ) {
544- globals . values = variableValues . globals ;
545- }
546- if ( variableValues . environment && variableValues . environment . length ) {
547- environment . values = variableValues . environment ;
548- }
549- if ( variableValues . _variables && variableValues . _variables . length ) {
550- localVariables . values = variableValues . _variables ;
551- }
552- // Merge the root request's collection variables as globals
553- // for nested requests. For all further nested requests, globals
554- // will continue to have these values useful for resolution
555- // unless overridden.
556- if ( ! self . state . isNestedRequest &&
557- variableValues . collectionVariables &&
558- variableValues . collectionVariables . length ) {
559- collectionVariables . values = variableValues . collectionVariables ;
560- }
561- resolve ( ) ;
562- } ) ;
540+ if ( variableValues . globals && variableValues . globals . length ) {
541+ globals . values = variableValues . globals ;
542+ }
543+ if ( variableValues . environment && variableValues . environment . length ) {
544+ environment . values = variableValues . environment ;
545+ }
546+ if ( variableValues . _variables && variableValues . _variables . length ) {
547+ localVariables . values = variableValues . _variables ;
548+ }
563549
564- self . host . dispatch ( EXECUTION_GET_VARIABLES_FROM_THIS_EXECUTION + executionId ,
565- variablesRequestUniqueId ) ;
566- } ) ;
550+ // Merge the root request's collection variables as globals
551+ // for nested requests. For all further nested requests, globals
552+ // will continue to have these values useful for resolution
553+ // unless overridden.
554+ if ( ! self . state . isNestedRequest &&
555+ variableValues . collectionVariables &&
556+ variableValues . collectionVariables . length ) {
557+ collectionVariables . values = variableValues . collectionVariables ;
558+ }
567559
568560 // Merge provided local variables with the variables provided in reference request opts
569561 const collectionRequestRunner = require ( '../../runner' ) ,
@@ -613,15 +605,15 @@ module.exports = {
613605 function ( err , run ) {
614606 let exceptionForThisRequest = null ,
615607 responseForThisRequest = null ,
616- variableMutationsFromThisScriptExecution = { } ;
608+ variableMutationsFromThisExecution = { } ;
617609
618610 if ( err ) {
619611 return self . host
620612 . dispatch ( EXECUTION_RUN_REQUEST_RESPONSE_EVENT_BASE + eventId ,
621613 requestId , err ) ;
622614 }
623615 run . start ( {
624- script ( _err , _cursor , result , _script , event ) {
616+ script ( _err , _cursor , result ) {
625617 // This is to sync changes to pm.variables, pm.environment & pm.globals
626618 // that happened inside the nested request's script
627619 // back to parent request's scripts still currently executing.
@@ -631,11 +623,15 @@ module.exports = {
631623 // All other global variables defined by syntax like 'a=1'
632624 // are anyway synced as the sandbox's common scope is shared across runs
633625 if ( result ) {
634- variableMutationsFromThisScriptExecution [ event . listen ] = {
635- _variables : result . _variables . mutations ,
636- environment : result . environment . mutations ,
637- globals : result . globals . mutations
638- } ;
626+ CONTEXT_VARIABLE_SCOPES . forEach ( function ( type ) {
627+ variableMutationsFromThisExecution [ type ] =
628+ ! variableMutationsFromThisExecution [ type ] ?
629+ [ result [ type ] . mutations ] :
630+ [
631+ ...variableMutationsFromThisExecution [ type ] ,
632+ result [ type ] . mutations
633+ ] ;
634+ } ) ;
639635 }
640636 } ,
641637 exception ( _ , err ) {
@@ -672,8 +668,7 @@ module.exports = {
672668 return self . host . dispatch ( runRequestRespEvent ,
673669 requestId , error || null ,
674670 responseForThisRequest ,
675- { variableMutationStages :
676- variableMutationsFromThisScriptExecution } ) ;
671+ { variableMutations : variableMutationsFromThisExecution } ) ;
677672 }
678673 } ) ;
679674 } ) ;
0 commit comments