Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

async-listener

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-listener - npm Package Compare versions

Comparing version 0.0.3 to 0.1.1

test/core-asynclistener-error.simple.js

146

glue.js

@@ -1,56 +0,60 @@

'use strict';
var wrap = require('shimmer').wrap;
var listeners = [];
function runSetup(list, length) {
var data = new Array(length), i, listener;
for (i = 0; i < length; ++i) {
listener = list[i];
data[i] = listener.onAsync.call(this);
}
return data;
if (process._fatalException) {
wrap(process, '_fatalException', function (_fatalException) {
return function _asyncFatalException(er) {
var length = listeners.length;
for (var i = 0; i < length; ++i) {
var callbacks = listeners[i].callbacks;
// FIXME: find the actual domain element
var domain = {};
if (callbacks.error && callbacks.error(domain, er)) {
process._needTickCallback();
return true;
}
}
return _fatalException(er);
};
});
}
else {
process.on('uncaughtException', function _asyncUncaughtException(er) {
var length = listeners.length;
for (var i = 0; i < length; ++i) {
var callbacks = listeners[i].callbacks;
// FIXME: find the actual domain element
var domain = {};
if (callbacks.error && callbacks.error(domain, er)) {
process._needTickCallback();
return true;
}
}
function runBefore(data, list, length) {
var i, obj;
for (i = 0; i < length; ++i) {
obj = list[i].callbackObject;
if (obj && obj.before) obj.before(data[i]);
}
return false;
});
}
function runAfter(data, list, length) {
var i, obj;
for (i = 0; i < length; ++i) {
obj = list[i].callbackObject;
if (obj && obj.after) obj.after(data[i]);
function runSetup(list, length) {
var data = new Array(length);
for (var i = 0; i < length; ++i) {
data[i] = list[i].listener.call(this);
}
for (i = 0; i < length; ++i) {
obj = list[i].callbackObject;
if (obj && obj.done) obj.done(data[i]);
}
return data;
}
function runError(data, list, length) {
var i, obj;
for (i = 0; i < length; ++i) {
obj = list[i].callbackObject;
if (obj && obj.after) obj.after(data[i]);
function runBefore(data, list, length, context) {
for (var i = 0; i < length; ++i) {
var callbacks = list[i].callbacks;
if (callbacks && callbacks.before) callbacks.before(context, data[i]);
}
}
function catchyWrap(original, list, length) {
var data = runSetup(list, length);
return function () {
runBefore(data, list, length);
try {
return original.apply(this, arguments);
}
catch (err) {
runError(data, list, length);
}
finally {
runAfter(data, list, length);
}
};
function runAfter(data, list, length, context) {
for (var i = 0; i < length; ++i) {
var callbacks = list[i].callbacks;
if (callbacks && callbacks.after) callbacks.after(context, data[i]);
}
}

@@ -61,3 +65,3 @@

return function () {
runBefore(data, list, length);
runBefore(data, list, length, this);
try {

@@ -67,3 +71,3 @@ return original.apply(this, arguments);

finally {
runAfter(data, list, length);
runAfter(data, list, length, this);
}

@@ -74,3 +78,3 @@ };

function noWrap(original, list, length) {
for (var i = 0; i < length; ++i) list[i].onAsync();
for (var i = 0; i < length; ++i) list[i].listener();
return original;

@@ -80,42 +84,38 @@ }

function wrapCallback(original) {
var list = Array.prototype.slice.call(listeners);
var list = [].slice.call(listeners);
var length = list.length;
var hasAny = false, hasErr = false;
for (var i = 0; i < length; ++i) {
var obj = list[i].callbackObject;
if (obj) {
hasAny = true;
if (obj.error) hasErr = true;
}
if (list[i].callbacks) return normalWrap(original, list, length);
}
return hasAny ? hasErr ? catchyWrap(original, list, length)
: normalWrap(original, list, length)
: noWrap(original, list, length);
return noWrap(original, list, length);
}
function addAsyncListener(onAsync, callbackObject) {
var listener = {
onAsync : onAsync,
callbackObject : callbackObject
function createAsyncListener(listener, callbacks, domain) {
return {
listener : listener,
callbacks : callbacks,
domain : domain
};
listeners.push(listener);
return listener.onAsync;
}
function removeAsyncListener(onAsync) {
if (!onAsync) throw new Error("must pass listener to remove");
var index = -1;
for (var i = 0; i < listeners.length; i++) {
if (listeners[i].onAsync === onAsync) {
index = i;
break;
}
function addAsyncListener(listener, callbacks, domain) {
if (typeof listener === 'function') {
callbacks = createAsyncListener(listener, callbacks, domain);
}
if (index < 0) throw new Error("async listener not found");
else {
callbacks = listener;
}
listeners.splice(index, 1);
listeners.push(callbacks);
return callbacks;
}
function removeAsyncListener(listener) {
var index = listeners.indexOf(listener);
if (index >= 0) listeners.splice(index, 1);
}
process.createAsyncListener = createAsyncListener;
process.addAsyncListener = addAsyncListener;

@@ -122,0 +122,0 @@ process.removeAsyncListener = removeAsyncListener;

{
"name": "async-listener",
"version": "0.0.3",
"version": "0.1.1",
"description": "Polyfill exporting trevnorris's 0.11+ process.addAsynListener API.",

@@ -5,0 +5,0 @@ "author": "Forrest L Norvell <ogd@aoaioxxysz.net>",

@@ -6,3 +6,3 @@ 'use strict';

test("async listener lifecycle", function (t) {
t.plan(7);
t.plan(8);

@@ -16,10 +16,17 @@ if (process.addAsyncListener) {

t.ok(process.createAsyncListener, "can create async listeners");
var counted = 0;
var listener = process.createAsyncListener(
function () { counted++; },
{
before : function () {},
after : function () {},
error : function () {}
},
Object.create(null)
);
t.ok(process.addAsyncListener, "can add async listeners");
var listener;
t.doesNotThrow(function () {
listener = process.addAsyncListener(
function () {},
{before : function () {}, after : function () {}}
);
listener = process.addAsyncListener(listener);
}, "adding does not throw");

@@ -30,3 +37,2 @@

t.ok(process.removeAsyncListener, "can remove async listeners");
t.doesNotThrow(function () {

@@ -36,9 +42,7 @@ process.removeAsyncListener(listener);

t.throws(function () {
t.doesNotThrow(function () {
process.removeAsyncListener(listener);
}, "removing the same listener twice throws");
}, "failing remove does not throw");
t.throws(function () {
process.removeAsyncListener(null);
}, "removing a nonexistent listener throws");
t.equal(counted, 0, "didn't hit any async functions");
});
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