Comparing version 13.0.0-15 to 13.0.0-16
@@ -29,7 +29,7 @@ /** | ||
* @param {String} hash? - the hash string representing this particular input configuration (if the cache is not in use, this is always `undefined`) | ||
* @param {Dictionary} machine - the live machine instance | ||
* @param {Dictionary} liveMachine - the live machine instance | ||
* | ||
* @return {Dictionary} of new callbacks which intercept the configured callback functions | ||
*/ | ||
module.exports = function interceptExitCallbacks (callbacks, _cache, hash, machine){ | ||
module.exports = function interceptExitCallbacks (callbacks, _cache, hash, liveMachine){ | ||
@@ -50,12 +50,19 @@ var interceptedCallbacks = _.reduce(callbacks, function (memo,fn,exitName){ | ||
var thisStack = (new Error()).stack.split('\n'); | ||
// `thisStack` is only used if the `_doAlterStack` flag is enabled on the live machine instance. | ||
// (stack alteration is **experimental** as of machine@v13.x) | ||
var thisStack; | ||
// =================================================================================================================== | ||
if (liveMachine._doAlterStack) { | ||
thisStack = (new Error()).stack.split('\n'); | ||
// Remove the first line (the error message) from the stack track. | ||
thisStack = thisStack.slice(1); | ||
// Remove the first line (the error message) from the stack track. | ||
thisStack = thisStack.slice(1); | ||
// Remove all non-userland calls (i.e. calls from the machine runner or internal to Node). | ||
thisStack = removeNonUserlandCallsFromStack(thisStack); | ||
// Remove all non-userland calls (i.e. calls from the machine runner or internal to Node). | ||
thisStack = removeNonUserlandCallsFromStack(thisStack); | ||
}//>- | ||
// =================================================================================================================== | ||
// If the machine has already timed out, then we should bail. | ||
if (machine._timedOut) { | ||
if (liveMachine._timedOut) { | ||
return; | ||
@@ -66,4 +73,4 @@ }// --• | ||
// If it has, log a warning and bail. | ||
if (machine._exited) { | ||
console.warn('In machine `'+_.camelCase(machine.identity)+'`: attempted to call exit `'+exitName+'` after exit `'+machine._exited+'` was already triggered. If you are the maintainer of this machine, make sure your machine only ever calls one exit, exactly once!'); | ||
if (liveMachine._exited) { | ||
console.warn('In machine `'+_.camelCase(liveMachine.identity)+'`: attempted to call exit `'+exitName+'` after exit `'+liveMachine._exited+'` was already triggered. If you are the maintainer of this machine, make sure your machine only ever calls one exit, exactly once!'); | ||
return; | ||
@@ -92,7 +99,7 @@ }// --• | ||
// continue with sending back the output. | ||
machine.warn(err); | ||
liveMachine.warn(err); | ||
}// >- | ||
// Look up appropriate exit definition. | ||
var exitDef = machine.exits && machine.exits[exitName]; | ||
var exitDef = liveMachine.exits && liveMachine.exits[exitName]; | ||
@@ -106,3 +113,3 @@ // Determine if the exit has been explicitly "voided". | ||
// Coerce exit's output value (if `_exitCoercion` flag enabled) | ||
if(machine._exitCoercion && !voided) { | ||
if(liveMachine._exitCoercion && !voided) { | ||
@@ -113,3 +120,3 @@ // Get exit's example if possible | ||
try { | ||
example = determineEffectiveOutputExample(exitDef, exitName, machine._configuredInputs, machine); | ||
example = determineEffectiveOutputExample(exitDef, exitName, liveMachine._configuredInputs, liveMachine); | ||
} | ||
@@ -119,3 +126,3 @@ catch (e) { | ||
// emit a warning, but then continue on sending back the output below. | ||
machine.warn('Encountered issue when attempting to determine example of '+exitName+' exit in machine ('+machine.identity+'): ' + e.stack); | ||
liveMachine.warn('Encountered issue when attempting to determine example of '+exitName+' exit in machine ('+liveMachine.identity+'): ' + e.stack); | ||
} | ||
@@ -156,3 +163,3 @@ // >- | ||
default: | ||
throw new Error('Consistency violation: Encountered unexpected internal error in machine runner when attempting to coerce output for exit (`'+exitName+'`) of machine (`'+machine.identity+'`). Details: '+e.stack); | ||
throw new Error('Consistency violation: Encountered unexpected internal error in machine runner when attempting to coerce output for exit (`'+exitName+'`) of machine (`'+liveMachine.identity+'`). Details: '+e.stack); | ||
@@ -168,3 +175,3 @@ }//</switch> | ||
// In order to allow for synchronous usage, `sync` must be explicitly `true`. | ||
if (machine._runningSynchronously) { | ||
if (liveMachine._runningSynchronously) { | ||
return proceed(); | ||
@@ -191,27 +198,27 @@ } | ||
var DEBUG_LOG_LINE_LEN = 45; | ||
var identity = machine.identity; | ||
var identity = liveMachine.identity; | ||
var paddedIdentity = _.padRight(_.trunc('machine-log:'+identity, {length: DEBUG_LOG_LINE_LEN, omission: ''}), DEBUG_LOG_LINE_LEN); | ||
Debug('machine:'+machine.identity+':exec')('%s',machine._exited); | ||
Debug(paddedIdentity)(' \\_ %s',machine._exited); | ||
Debug('machine:'+liveMachine.identity+':exec')('%s',liveMachine._exited); | ||
Debug(paddedIdentity)(' \\_ %s',liveMachine._exited); | ||
var msElapsed; | ||
if (machine._doTrackDuration){ | ||
machine._execFinishTimestamp = new Date(); | ||
if (liveMachine._doTrackDuration){ | ||
liveMachine._execFinishTimestamp = new Date(); | ||
try { | ||
msElapsed = machine._execFinishTimestamp.getTime() - machine._execBeginTimestamp.getTime(); | ||
machine._msElapsed = msElapsed; | ||
msElapsed = liveMachine._execFinishTimestamp.getTime() - liveMachine._execBeginTimestamp.getTime(); | ||
liveMachine._msElapsed = msElapsed; | ||
} | ||
catch (e) { | ||
machine.warn('Error calculating duration of machine execution:\n',e); | ||
liveMachine.warn('Error calculating duration of liveMachine execution:\n',e); | ||
} | ||
} | ||
if (machine._isLogEnabled) { | ||
if (liveMachine._isLogEnabled) { | ||
try { | ||
machine._output = value; | ||
machine._onInvoke(machine); | ||
liveMachine._output = value; | ||
liveMachine._onInvoke(liveMachine); | ||
} | ||
catch (e) { | ||
machine.warn('Error logging machine info\n:',e); | ||
liveMachine.warn('Error logging liveMachine info\n:',e); | ||
} | ||
@@ -221,7 +228,7 @@ } | ||
// Clear timeout alarm so the error exit callback isn't fired after we're done. | ||
clearTimeout(machine._timeoutAlarm); | ||
clearTimeout(liveMachine._timeoutAlarm); | ||
// Set the `._exited` property to indicate that the machine instance's `fn` | ||
// Set the `._exited` property to indicate that the liveMachine instance's `fn` | ||
// has attempted to trigger an exit callback. | ||
machine._exited = exitName; | ||
liveMachine._exited = exitName; | ||
@@ -237,31 +244,46 @@ // ┌─┐┌┐┌┌─┐┬ ┬┬─┐┌─┐ ┌─┐┬─┐┬─┐┌─┐┬─┐ ┌─┐─┐ ┬┬┌┬┐ ┌─┐┬ ┬┌┬┐┌─┐┬ ┬┌┬┐ ┬┌─┐ ┌─┐┌┐┌ | ||
if (exitName === 'error') { | ||
// If the value is not an error instance, make it one, copying the original value | ||
// to the `output` property, and copy the stack from `thisStack` created at the | ||
// beginning of this function _before_ the setTimeout(). | ||
// to the `output` property | ||
if (!_.isError(value)) { | ||
var oldValue = value; | ||
// Create a new, descriptive error. | ||
value = new Error('Machine `' + machine.identity + '` called its `error` exit with:\n\n' + util.inspect(oldValue, {depth: null})); | ||
value = new Error('Machine `' + liveMachine.identity + '` called its `error` exit with:\n\n' + util.inspect(oldValue, {depth: null})); | ||
// Set the original value as the `output` property of the error. | ||
value.output = oldValue; | ||
// Set the new error's stack to that of `thisStack`, with the error message prepended. | ||
// We prepend the message because the code below that modifies the stack expects | ||
// it to be there. | ||
value.stack = value.message + '\n' + thisStack.join('\n'); | ||
} | ||
// ┌─┐─┐ ┬┌─┐┌─┐┌┐┌┌┬┐ ┌─┐┌┬┐┌─┐┌─┐┬┌─ ┌─┐┌─┐┬─┐ ┌─┐┬─┐┬─┐┌─┐┬─┐ ┌─┐┬ ┬┌┬┐┌─┐┬ ┬┌┬┐ | ||
// ├┤ ┌┴┬┘├─┘├─┤│││ ││ └─┐ │ ├─┤│ ├┴┐ ├┤ │ │├┬┘ ├┤ ├┬┘├┬┘│ │├┬┘ │ ││ │ │ ├─┘│ │ │ | ||
// └─┘┴ └─┴ ┴ ┴┘└┘─┴┘ └─┘ ┴ ┴ ┴└─┘┴ ┴ └ └─┘┴└─ └─┘┴└─┴└─└─┘┴└─ └─┘└─┘ ┴ ┴ └─┘ ┴ | ||
// | ||
// At this point, the value is guaranteed to be an error instance. | ||
// But it may or may not have already had its stack combined with | ||
// the stacks in the environment. If not, we'll do it now. | ||
if (!value.stackModified) { | ||
value.stack = value.name + ': ' + value.message + '\n' + getCombinedErrorStack(getStackTraceWithoutInitialMessage(value).split('\n'), machine._configuredEnvironment.debug.stack); | ||
// Indicate that we've already massaged this stack, so we don't do it again. | ||
Object.defineProperty(value, 'stackModified', {value: true}); | ||
} | ||
} | ||
// =================================================================================================================== | ||
if (liveMachine._doAlterStack) { | ||
// Finally, copy the stack from `thisStack` created at the | ||
// beginning of this function _before_ the setTimeout(). | ||
// Set the new error's stack to that of `thisStack`, with the error message prepended. | ||
// We prepend the message because the code below that modifies the stack expects | ||
// it to be there. | ||
value.stack = value.message + '\n' + thisStack.join('\n'); | ||
}//>- | ||
// =================================================================================================================== | ||
}//</if output is NOT an error instance> | ||
// =================================================================================================================== | ||
if (liveMachine._doAlterStack) { | ||
// ┌─┐─┐ ┬┌─┐┌─┐┌┐┌┌┬┐ ┌─┐┌┬┐┌─┐┌─┐┬┌─ ┌─┐┌─┐┬─┐ ┌─┐┬─┐┬─┐┌─┐┬─┐ ┌─┐┬ ┬┌┬┐┌─┐┬ ┬┌┬┐ | ||
// ├┤ ┌┴┬┘├─┘├─┤│││ ││ └─┐ │ ├─┤│ ├┴┐ ├┤ │ │├┬┘ ├┤ ├┬┘├┬┘│ │├┬┘ │ ││ │ │ ├─┘│ │ │ | ||
// └─┘┴ └─┴ ┴ ┴┘└┘─┴┘ └─┘ ┴ ┴ ┴└─┘┴ ┴ └ └─┘┴└─ └─┘┴└─┴└─└─┘┴└─ └─┘└─┘ ┴ ┴ └─┘ ┴ | ||
// | ||
// At this point, the value is guaranteed to be an error instance. | ||
// But it may or may not have already had its stack combined with | ||
// the stacks in the environment. If not, we'll do it now. | ||
if (!value.stackModified) { | ||
value.stack = value.name + ': ' + value.message + '\n' + getCombinedErrorStack(getStackTraceWithoutInitialMessage(value).split('\n'), liveMachine._configuredEnvironment.debug.stack); | ||
// Indicate that we've already massaged this stack, so we don't do it again. | ||
Object.defineProperty(value, 'stackModified', {value: true}); | ||
} | ||
}//>- | ||
// =================================================================================================================== | ||
}//</if this is the error exit> | ||
//‡ | ||
// ╔═╗┌┬┐┌┬┐ ┌─┐┬ ┬┌┬┐┌─┐┬ ┬┌┬┐ ┌─┐┌─┐┬─┐ ┬ ┬┌─┐┬┌┬┐ ┌┬┐┬┌─┐┌─┐ ┌─┐─┐ ┬┬┌┬┐┌─┐ | ||
@@ -274,9 +296,15 @@ // ╠═╣ ││ ││ │ ││ │ │ ├─┘│ │ │ ├┤ │ │├┬┘ └┐┌┘│ ││ ││ ││││└─┐│ ├┤ ┌┴┬┘│ │ └─┐ | ||
// of that exit if available. | ||
value = new Error('Machine `' + machine.identity + '` called its `' + exitName + '` exit' + (exitDef.description ? (': ' + exitDef.description) : '.')); | ||
// Make sure the error stack reaches all the way back to the beginning of the run, following asynchronous hops. | ||
value.stack = value.name + ': ' + value.message + '\n' + getCombinedErrorStack(getStackTraceWithoutInitialMessage(value).split('\n'), machine._configuredEnvironment.debug.stack); | ||
// Indicate that we've already massaged this stack, so we don't do it again. | ||
Object.defineProperty(value, 'stackModified', {value: true}); | ||
} | ||
value = new Error('Machine `' + liveMachine.identity + '` called its `' + exitName + '` exit' + (exitDef.description ? (': ' + exitDef.description) : '.')); | ||
// =================================================================================================================== | ||
if (liveMachine._doAlterStack) { | ||
// Make sure the error stack reaches all the way back to the beginning of the run, following asynchronous hops. | ||
value.stack = value.name + ': ' + value.message + '\n' + getCombinedErrorStack(getStackTraceWithoutInitialMessage(value).split('\n'), liveMachine._configuredEnvironment.debug.stack); | ||
// Indicate that we've already massaged this stack, so we don't do it again. | ||
Object.defineProperty(value, 'stackModified', {value: true}); | ||
}//>- | ||
// =================================================================================================================== | ||
}//</else if not success exit, there is no runtime output, and that's what the exit was expcting (i.e. was expecting void)> | ||
//‡ | ||
// If the void exit is called with non-error output (this is weird but permissable) | ||
@@ -286,9 +314,17 @@ else if (exitName !== 'success' && _.isUndefined(example) && !_.isError(value)) { | ||
// of that exit if available. | ||
value = new Error('Machine `' + machine.identity + '` called its `' + exitName + '` exit with:\n\n' + util.inspect(value, {depth: null})); | ||
// Make sure the error stack reaches all the way back to the beginning of the run, following asynchronous hops. | ||
value.stack = value.name + ': ' + value.message + '\n' + getCombinedErrorStack(getStackTraceWithoutInitialMessage(value).split('\n'), machine._configuredEnvironment.debug.stack); | ||
// Indicate that we've already massaged this stack, so we don't do it again. | ||
Object.defineProperty(value, 'stackModified', {value: true}); | ||
} | ||
value = new Error('Machine `' + liveMachine.identity + '` called its `' + exitName + '` exit with:\n\n' + util.inspect(value, {depth: null})); | ||
// =================================================================================================================== | ||
if (liveMachine._doAlterStack) { | ||
// Make sure the error stack reaches all the way back to the beginning of the run, following asynchronous hops. | ||
value.stack = value.name + ': ' + value.message + '\n' + getCombinedErrorStack(getStackTraceWithoutInitialMessage(value).split('\n'), liveMachine._configuredEnvironment.debug.stack); | ||
// Indicate that we've already massaged this stack, so we don't do it again. | ||
Object.defineProperty(value, 'stackModified', {value: true}); | ||
}//>- | ||
// =================================================================================================================== | ||
}//</else if not success exit, and there IS runtime output other than an Error instance, but that's NOT what the exit was expecting (i.e. it was expecting void)> | ||
//>- | ||
// TODO -- Handle the case of a "void" exit that nevertheless is called with an Error instance | ||
@@ -299,16 +335,20 @@ // as output, using some code like the blocks above. The trick is to make sure any metadata | ||
// ┬ ┬┌─┐┌┬┐┌─┐┌┬┐┌─┐ ┌─┐┌┐┌┬ ┬ ┌─┐┌┬┐┌─┐┌─┐┬┌─ | ||
// │ │├─┘ ││├─┤ │ ├┤ ├┤ │││└┐┌┘ └─┐ │ ├─┤│ ├┴┐ | ||
// └─┘┴ ─┴┘┴ ┴ ┴ └─┘ └─┘┘└┘ └┘ └─┘ ┴ ┴ ┴└─┘┴ ┴ | ||
// Leaving through an exit means popping something off the stack | ||
// =================================================================================================================== | ||
if (liveMachine._doAlterStack) { | ||
// ┬ ┬┌─┐┌┬┐┌─┐┌┬┐┌─┐ ┌─┐┌┐┌┬ ┬ ┌─┐┌┬┐┌─┐┌─┐┬┌─ | ||
// │ │├─┘ ││├─┤ │ ├┤ ├┤ │││└┐┌┘ └─┐ │ ├─┤│ ├┴┐ | ||
// └─┘┴ ─┴┘┴ ┴ ┴ └─┘ └─┘┘└┘ └┘ └─┘ ┴ ┴ ┴└─┘┴ ┴ | ||
// Leaving through an exit means popping something off the stack | ||
// Pop the last entry off our stack | ||
var lastStackEntry = machine._configuredEnvironment.debug.stack.pop(); | ||
// Pop the last entry off our stack | ||
var lastStackEntry = liveMachine._configuredEnvironment.debug.stack.pop(); | ||
// console.log('`' + machine.identity + '`.exit ('+exitName+') stacks:'); | ||
// console.log(machine._configuredEnvironment.debug.stack); | ||
// console.log('====='); | ||
// console.log('`' + liveMachine.identity + '`.exit ('+exitName+') stacks:'); | ||
// console.log(liveMachine._configuredEnvironment.debug.stack); | ||
// console.log('====='); | ||
}//>- | ||
// =================================================================================================================== | ||
// Call the configured callback for this exit | ||
return fn.call(machine._configuredEnvironment, value); | ||
return fn.call(liveMachine._configuredEnvironment, value); | ||
@@ -315,0 +355,0 @@ |
{ | ||
"name": "machine", | ||
"version": "13.0.0-15", | ||
"version": "13.0.0-16", | ||
"description": "Configure and execute machines", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
225934
3569