async-try-catch
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -31,6 +31,2 @@ /* | ||
var Events = require( 'events' ) ; | ||
function AsyncTryCatch() { throw new Error( "Use AsyncTryCatch.try() instead." ) ; } | ||
@@ -42,3 +38,21 @@ module.exports = AsyncTryCatch ; | ||
// First, backup everything | ||
// First, get Node.js core Events module if possible and check if NextGen Events is part of this project | ||
if ( ! process.browser ) | ||
{ | ||
AsyncTryCatch.NodeEvents = require( 'events' ) ; | ||
try { | ||
AsyncTryCatch.NextGenEvents = require( 'nextgen-events' ) ; | ||
} catch ( error ) {} | ||
} | ||
else | ||
{ | ||
if ( ! global.setImmediate ) | ||
{ | ||
global.setImmediate = function setImmediate( fn ) { return setTimeout( fn , 0 ) ; } ; | ||
global.clearImmediate = function clearImmediate( timer ) { return clearTimeout( timer ) ; } ; | ||
} | ||
} | ||
if ( ! global.Vanilla ) | ||
@@ -51,15 +65,5 @@ { | ||
if ( ! global.Vanilla.nextTick ) { global.Vanilla.nextTick = process.nextTick ; } | ||
if ( ! global.Vanilla.eventsOn ) { global.Vanilla.eventsOn = Events.prototype.on ; } | ||
//if ( ! global.Vanilla.Error ) { global.Vanilla.Error = Error ; } | ||
} | ||
// Check if NextGen Events is part of this project | ||
try { | ||
AsyncTryCatch.NextGenEvents = require( 'nextgen-events' ) ; | ||
} catch ( error ) {} | ||
AsyncTryCatch.stack = [] ; | ||
@@ -126,18 +130,17 @@ AsyncTryCatch.substituted = false ; | ||
AsyncTryCatch.registerWrapper = function registerWrapper( originalMethod , callbackIndex , callbackKey ) | ||
AsyncTryCatch.addListenerWrapper = function addListenerWrapper( originalMethod , listenerIndex , listenerKey ) | ||
{ | ||
var fn , context , | ||
var fn , context , wrapperFn , | ||
args = Array.prototype.slice.call( arguments , 3 ) ; | ||
if ( callbackIndex < 0 ) { callbackIndex = args.length - callbackIndex ; } | ||
if ( listenerIndex < 0 ) { listenerIndex = args.length - listenerIndex ; } | ||
fn = args[ callbackIndex ] ; | ||
fn = args[ listenerIndex ] ; | ||
// NextGen event compatibility | ||
if ( typeof fn !== 'function' && callbackKey ) { fn = fn[ callbackKey ] ; } | ||
if ( typeof fn !== 'function' && listenerKey ) { fn = fn[ listenerKey ] ; } | ||
if ( typeof fn !== 'function' || ! AsyncTryCatch.stack.length ) | ||
{ | ||
originalMethod.apply( this , args ) ; | ||
return ; | ||
return originalMethod.apply( this , args ) ; | ||
} | ||
@@ -147,16 +150,37 @@ | ||
// Replace the the callback by this one: | ||
args[ callbackIndex ] = function() { | ||
try { | ||
AsyncTryCatch.stack.push( context ) ; | ||
fn.apply( this , arguments ) ; | ||
AsyncTryCatch.stack.pop() ; | ||
// This method cover setTimeout(), setImmediate() and process.nextTick() | ||
if ( this ) | ||
{ | ||
// Assume that the function is only wrapped once per eventEmitter | ||
if ( this.__fnToWrapperMap ) | ||
{ | ||
wrapperFn = this.__fnToWrapperMap.get( fn ) ; | ||
} | ||
catch ( error ) { | ||
AsyncTryCatch.stack.pop() ; | ||
context.callCatchFn( error ) ; | ||
else | ||
{ | ||
// Create the map, make it non-enumerable | ||
Object.defineProperty( this , '__fnToWrapperMap', { value: new WeakMap() } ) ; | ||
} | ||
} ; | ||
} | ||
originalMethod.apply( this , args ) ; | ||
if ( ! wrapperFn ) | ||
{ | ||
wrapperFn = function() { | ||
try { | ||
AsyncTryCatch.stack.push( context ) ; | ||
fn.apply( this , arguments ) ; | ||
AsyncTryCatch.stack.pop() ; | ||
} | ||
catch ( error ) { | ||
AsyncTryCatch.stack.pop() ; | ||
context.callCatchFn( error ) ; | ||
} | ||
} ; | ||
if ( this ) { this.__fnToWrapperMap.set( fn , wrapperFn ) ; } | ||
} | ||
args[ listenerIndex ] = wrapperFn ; | ||
return originalMethod.apply( this , args ) ; | ||
} ; | ||
@@ -166,24 +190,77 @@ | ||
AsyncTryCatch.setTimeout = AsyncTryCatch.registerWrapper.bind( undefined , global.Vanilla.setTimeout , 0 , null ) ; | ||
AsyncTryCatch.setImmediate = AsyncTryCatch.registerWrapper.bind( undefined , global.Vanilla.setImmediate , 0 , null ) ; | ||
AsyncTryCatch.nextTick = AsyncTryCatch.registerWrapper.bind( process , global.Vanilla.nextTick , 0 , null ) ; | ||
AsyncTryCatch.removeListenerWrapper = function removeListenerWrapper( originalMethod , listenerIndex , listenerKey ) | ||
{ | ||
var fn , context , wrapperFn , | ||
args = Array.prototype.slice.call( arguments , 3 ) ; | ||
if ( listenerIndex < 0 ) { listenerIndex = args.length - listenerIndex ; } | ||
fn = args[ listenerIndex ] ; | ||
//console.log( 'fn:' , fn ) ; | ||
// NextGen event compatibility | ||
//if ( typeof fn !== 'function' && listenerKey ) { fn = fn[ listenerKey ] ; } | ||
// 'this' is always defined here | ||
if ( typeof fn === 'function' && this.__fnToWrapperMap ) | ||
{ | ||
args[ listenerIndex ] = this.__fnToWrapperMap.get( fn ) || fn ; | ||
} | ||
return originalMethod.apply( this , args ) ; | ||
} ; | ||
// Core Events addListener() replacement | ||
AsyncTryCatch.on = function on() | ||
AsyncTryCatch.setTimeout = AsyncTryCatch.addListenerWrapper.bind( undefined , global.Vanilla.setTimeout , 0 , null ) ; | ||
AsyncTryCatch.setImmediate = AsyncTryCatch.addListenerWrapper.bind( undefined , global.Vanilla.setImmediate , 0 , null ) ; | ||
// DO NOT BIND process as 'this', it is not mandatory for nextTick() to have a 'this' context, | ||
// and furthermore the lib would assume that we are in a real addListener() use-case | ||
AsyncTryCatch.nextTick = AsyncTryCatch.addListenerWrapper.bind( undefined , global.Vanilla.nextTick , 0 , null ) ; | ||
// NodeEvents on()/addListener() replacement | ||
AsyncTryCatch.addListener = function addListener() | ||
{ | ||
var args = [ global.Vanilla.eventsOn , 1 , null ].concat( Array.from( arguments ) ) ; | ||
AsyncTryCatch.registerWrapper.apply( this , [ global.Vanilla.eventsOn , 1 , null ].concat( Array.from( arguments ) ) ) ; | ||
AsyncTryCatch.addListenerWrapper.apply( this , [ AsyncTryCatch.NodeEvents.__addListener , 1 , null ].concat( Array.from( arguments ) ) ) ; | ||
} ; | ||
// NextGen Events addListener() replacement | ||
AsyncTryCatch.ngevOn = function ngevOn() | ||
// NodeEvents once() replacement | ||
AsyncTryCatch.addListenerOnce = function addListenerOnce() | ||
{ | ||
AsyncTryCatch.registerWrapper.apply( this , [ AsyncTryCatch.NextGenEvents.addListener , 1 , 'fn' ].concat( Array.from( arguments ) ) ) ; | ||
AsyncTryCatch.addListenerWrapper.apply( this , [ AsyncTryCatch.NodeEvents.__addListenerOnce , 1 , null ].concat( Array.from( arguments ) ) ) ; | ||
} ; | ||
// NodeEvents removeListener() replacement | ||
AsyncTryCatch.removeListener = function removeListener() | ||
{ | ||
AsyncTryCatch.removeListenerWrapper.apply( this , [ AsyncTryCatch.NodeEvents.__removeListener , 1 , null ].concat( Array.from( arguments ) ) ) ; | ||
} ; | ||
// NextGen Events on()/addListener() replacement | ||
AsyncTryCatch.ngevAddListener = function ngevAddListener() | ||
{ | ||
AsyncTryCatch.addListenerWrapper.apply( this , [ AsyncTryCatch.NextGenEvents.on , 1 , 'fn' ].concat( Array.from( arguments ) ) ) ; | ||
} ; | ||
// NextGen Events once() replacement | ||
AsyncTryCatch.ngevAddListenerOnce = function ngevAddListenerOnce() | ||
{ | ||
AsyncTryCatch.addListenerWrapper.apply( this , [ AsyncTryCatch.NextGenEvents.once , 1 , 'fn' ].concat( Array.from( arguments ) ) ) ; | ||
} ; | ||
// NextGen Events off()/removeListener() replacement | ||
AsyncTryCatch.ngevRemoveListener = function ngevRemoveListener() | ||
{ | ||
AsyncTryCatch.removeListenerWrapper.apply( this , [ AsyncTryCatch.NextGenEvents.off , 1 , 'fn' ].concat( Array.from( arguments ) ) ) ; | ||
} ; | ||
AsyncTryCatch.substitute = function substitute() | ||
{ | ||
if ( AsyncTryCatch.substituted ) { return ; } | ||
// This test should be done by the caller, because substitution could be incomplete | ||
// E.g. browser case: Node Events or NextGen Events are not loaded/accessible at time | ||
//if ( AsyncTryCatch.substituted ) { return ; } | ||
AsyncTryCatch.substituted = true ; | ||
@@ -195,4 +272,24 @@ | ||
Events.prototype.on = AsyncTryCatch.on ; | ||
Events.prototype.addListener = AsyncTryCatch.on ; | ||
if ( AsyncTryCatch.NodeEvents ) | ||
{ | ||
if ( ! AsyncTryCatch.NodeEvents.__addListener ) | ||
{ | ||
AsyncTryCatch.NodeEvents.__addListener = AsyncTryCatch.NodeEvents.prototype.on ; | ||
} | ||
if ( ! AsyncTryCatch.NodeEvents.__addListenerOnce ) | ||
{ | ||
AsyncTryCatch.NodeEvents.__addListenerOnce = AsyncTryCatch.NodeEvents.prototype.addListenerOnce ; | ||
} | ||
if ( ! AsyncTryCatch.NodeEvents.__removeListener ) | ||
{ | ||
AsyncTryCatch.NodeEvents.__removeListener = AsyncTryCatch.NodeEvents.prototype.removeListener ; | ||
} | ||
AsyncTryCatch.NodeEvents.prototype.on = AsyncTryCatch.addListener ; | ||
AsyncTryCatch.NodeEvents.prototype.addListener = AsyncTryCatch.addListener ; | ||
AsyncTryCatch.NodeEvents.prototype.once = AsyncTryCatch.addListenerOnce ; | ||
AsyncTryCatch.NodeEvents.prototype.removeListener = AsyncTryCatch.removeListener ; | ||
} | ||
@@ -204,4 +301,7 @@ //global.Error = AsyncTryCatch.Error ; | ||
{ | ||
AsyncTryCatch.NextGenEvents.prototype.on = AsyncTryCatch.ngevOn ; | ||
AsyncTryCatch.NextGenEvents.prototype.addListener = AsyncTryCatch.ngevOn ; | ||
AsyncTryCatch.NextGenEvents.prototype.on = AsyncTryCatch.ngevAddListener ; | ||
AsyncTryCatch.NextGenEvents.prototype.addListener = AsyncTryCatch.ngevAddListener ; | ||
AsyncTryCatch.NextGenEvents.prototype.once = AsyncTryCatch.ngevAddListenerOnce ; | ||
AsyncTryCatch.NextGenEvents.prototype.off = AsyncTryCatch.ngevRemoveListener ; | ||
AsyncTryCatch.NextGenEvents.prototype.removeListener = AsyncTryCatch.ngevRemoveListener ; | ||
} | ||
@@ -214,3 +314,6 @@ } ; | ||
{ | ||
if ( ! AsyncTryCatch.substituted ) { return ; } | ||
// This test should be done by the caller, because substitution could be incomplete | ||
// E.g. browser case: Node Events or NextGen Events are not loaded/accessible at time | ||
//if ( ! AsyncTryCatch.substituted ) { return ; } | ||
AsyncTryCatch.substituted = false ; | ||
@@ -222,4 +325,20 @@ | ||
Events.prototype.on = global.Vanilla.eventsOn ; | ||
Events.prototype.addListener = global.Vanilla.eventsOn ; | ||
if ( AsyncTryCatch.NodeEvents ) | ||
{ | ||
if ( AsyncTryCatch.NodeEvents.__addListener ) | ||
{ | ||
AsyncTryCatch.NodeEvents.prototype.on = AsyncTryCatch.NodeEvents.__addListener ; | ||
AsyncTryCatch.NodeEvents.prototype.addListener = AsyncTryCatch.NodeEvents.__addListener ; | ||
} | ||
if ( AsyncTryCatch.NodeEvents.__addListenerOnce ) | ||
{ | ||
AsyncTryCatch.NodeEvents.prototype.once = AsyncTryCatch.NodeEvents.__addListenerOnce ; | ||
} | ||
if ( AsyncTryCatch.NodeEvents.__removeListener ) | ||
{ | ||
AsyncTryCatch.NodeEvents.prototype.removeListener = AsyncTryCatch.NodeEvents.__removeListener ; | ||
} | ||
} | ||
@@ -230,4 +349,7 @@ //global.Error = global.Vanilla.Error ; | ||
{ | ||
AsyncTryCatch.NextGenEvents.prototype.on = AsyncTryCatch.NextGenEvents.addListener ; | ||
AsyncTryCatch.NextGenEvents.prototype.addListener = AsyncTryCatch.NextGenEvents.addListener ; | ||
AsyncTryCatch.NextGenEvents.prototype.on = AsyncTryCatch.NextGenEvents.on ; | ||
AsyncTryCatch.NextGenEvents.prototype.addListener = AsyncTryCatch.NextGenEvents.on ; | ||
AsyncTryCatch.NextGenEvents.prototype.once = AsyncTryCatch.NextGenEvents.once ; | ||
AsyncTryCatch.NextGenEvents.prototype.off = AsyncTryCatch.NextGenEvents.off ; | ||
AsyncTryCatch.NextGenEvents.prototype.removeListener = AsyncTryCatch.NextGenEvents.removeListener ; | ||
} | ||
@@ -234,0 +356,0 @@ } ; |
{ | ||
"name": "async-try-catch", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Async try catch", | ||
@@ -11,6 +11,8 @@ "main": "lib/AsyncTryCatch.js", | ||
"devDependencies": { | ||
"browserify": "^13.0.1", | ||
"expect.js": "^0.3.1", | ||
"jshint": "^2.9.2", | ||
"mocha": "^2.5.3", | ||
"nextgen-events": "^0.5.14" | ||
"nextgen-events": "^0.5.16", | ||
"uglify-js": "^2.6.2" | ||
}, | ||
@@ -17,0 +19,0 @@ "scripts": { |
@@ -38,2 +38,8 @@ /* | ||
if ( process.browser ) | ||
{ | ||
AsyncTryCatch.NodeEvents = Events ; | ||
AsyncTryCatch.NextGenEvents = NextGenEvents ; | ||
} | ||
var expect = require( 'expect.js' ) ; | ||
@@ -186,3 +192,3 @@ | ||
describe( "Events" , function() { | ||
describe( "Node Events" , function() { | ||
@@ -223,3 +229,35 @@ it( "an exception thrown synchronously from a listener within an async-try closure should be catched" , function( done ) { | ||
it( "should resume the event emitting" ) ; | ||
it( "once() should works as expected" , function() { | ||
var emitter = Object.create( Events.prototype ) ; | ||
var onDamage = function onDamage() { throw new Error( 'argh!' ) ; } ; | ||
var count = 0 ; | ||
asyncTry( function() { | ||
emitter.once( 'damage' , onDamage ) ; | ||
emitter.emit( 'damage' ) ; | ||
emitter.emit( 'damage' ) ; | ||
} ) | ||
.catch( function( error ) { | ||
if ( count ++ ) { throw error ; } | ||
} ) ; | ||
} ) ; | ||
it( "removeListener() should works as expected" , function() { | ||
var emitter = Object.create( Events.prototype ) ; | ||
var onDamage = function onDamage() { throw new Error( 'argh!' ) ; } ; | ||
asyncTry( function() { | ||
emitter.on( 'damage' , onDamage ) ; | ||
emitter.removeListener( 'damage' , onDamage ) ; | ||
emitter.emit( 'damage' ) ; | ||
} ) | ||
.catch( function( error ) { | ||
throw error ; | ||
} ) ; | ||
} ) ; | ||
//it( "isolate listener? (a throwing listener should not affect others listeners)" ) ; | ||
} ) ; | ||
@@ -318,4 +356,36 @@ | ||
} ) ; | ||
it( "once() should works as expected" , function() { | ||
var emitter = Object.create( NextGenEvents.prototype ) ; | ||
var onDamage = function onDamage() { throw new Error( 'argh!' ) ; } ; | ||
var count = 0 ; | ||
asyncTry( function() { | ||
emitter.once( 'damage' , onDamage ) ; | ||
emitter.emit( 'damage' ) ; | ||
emitter.emit( 'damage' ) ; | ||
} ) | ||
.catch( function( error ) { | ||
if ( count ++ ) { throw error ; } | ||
} ) ; | ||
} ) ; | ||
it( "removeListener() should works as expected" , function() { | ||
var emitter = Object.create( NextGenEvents.prototype ) ; | ||
var onDamage = function onDamage() { throw new Error( 'argh!' ) ; } ; | ||
asyncTry( function() { | ||
emitter.on( 'damage' , onDamage ) ; | ||
emitter.removeListener( 'damage' , onDamage ) ; | ||
emitter.emit( 'damage' ) ; | ||
} ) | ||
.catch( function( error ) { | ||
throw error ; | ||
} ) ; | ||
} ) ; | ||
} ) ; | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
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
338844
17
6085
6
4
10
2