Socket
Socket
Sign inDemoInstall

async-try-catch

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-try-catch - npm Package Compare versions

Comparing version 0.1.3 to 0.1.4

browser/AsyncTryCatch.js

228

lib/AsyncTryCatch.js

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc