Comparing version 0.1.1 to 0.1.2
57
index.js
/** | ||
* Shims built-in async functions and automatically wraps callbacks with "wrap" | ||
* Shims built-in async functions and automatically wraps callbacks with 'wrap' | ||
* @param {function} wrap The function to return the new callback | ||
*/ | ||
module.exports = process.hookit = 'function' === typeof process.hookit ? process.hookit : hookit | ||
var alreadyRequired | ||
function hookit(wrap) { | ||
var nextTick | ||
, fs | ||
, on | ||
, EventEmitter | ||
, on | ||
, removeListener | ||
@@ -25,3 +28,3 @@ , addListener | ||
arguments[0] = wrap(callback, name) | ||
return original.apply(this, arguments) | ||
return callInsteadOfApply(original, this, arguments) | ||
} | ||
@@ -31,6 +34,7 @@ } | ||
// Wrap process.nextTick | ||
// // Wrap process.nextTick | ||
nextTick = process.nextTick | ||
process.nextTick = function wrappedNextTick(callback) { | ||
return nextTick.call(this, wrap(callback, 'process.nextTick')) | ||
arguments[0] = wrap(callback, 'process.nextTick') | ||
return callInsteadOfApply(nextTick, this, arguments) | ||
} | ||
@@ -42,3 +46,3 @@ | ||
// If it has a *Sync counterpart, it's probably async | ||
if (!fs.hasOwnProperty(name + "Sync")) return | ||
if (!fs.hasOwnProperty(name + 'Sync')) return | ||
var original = fs[name] | ||
@@ -50,3 +54,3 @@ fs[name] = function () { | ||
} | ||
return original.apply(this, arguments) | ||
return callInsteadOfApply(original, this, arguments) | ||
} | ||
@@ -82,2 +86,39 @@ }) | ||
var alreadyRequired | ||
function callInsteadOfApply(fn, that, args) { | ||
// Avoid slow apply for common use | ||
switch(args.length) { | ||
case 0: | ||
fn.call(that) | ||
break | ||
case 1: | ||
fn.call(that, args[0]) | ||
break | ||
case 2: | ||
fn.call(that, args[0], args[1]) | ||
break | ||
case 3: | ||
fn.call(that, args[0], args[1], args[2]) | ||
break | ||
case 4: | ||
fn.call(that, args[0], args[1], args[2], args[3]) | ||
break | ||
case 5: | ||
fn.call(that, args[0], args[1], args[2], args[3], args[4]) | ||
break | ||
case 6: | ||
fn.call(that, args[0], args[1], args[2], args[3], args[4], args[5]) | ||
break | ||
case 7: | ||
fn.call(that, args[0], args[1], args[2], args[3], args[4], args[5], args[6]) | ||
break | ||
case 8: | ||
fn.call(that, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]) | ||
break | ||
case 9: | ||
fn.call(that, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]) | ||
break | ||
default: | ||
fn.apply(that, args) | ||
} | ||
} |
{ | ||
"name": "hookit", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "A function for shimming all node.js core IO calls allowing callback wraping.", | ||
@@ -19,6 +19,15 @@ "main": "index.js", | ||
], | ||
"author": "Adam Crabtree", | ||
"author": { | ||
"name": "Adam Crabtree" | ||
}, | ||
"license": "MIT", | ||
"readmeFilename": "README.md", | ||
"gitHead": "10a8993ee901c0fc9d1f0fb5543177e205caabaf" | ||
"gitHead": "10a8993ee901c0fc9d1f0fb5543177e205caabaf", | ||
"readme": "hook\n====\n\nShim all core IO calls and allow callback shimming via wrap function.\n\n###Installation\n```\n$ npm install hookit\n```\n###Usage:\n```javascript\nhookit(function generateShim(callback, fnName) {\n return function() {\n try {\n callback.apply(this, arguments);\n } finally {\n console.log('bummer.')\n }\n }\n})\n\nprocess.nextTick(function() {\n throw new Error('here') // Will log \"bummer\" before we crash on error\n})\n```\n", | ||
"bugs": { | ||
"url": "https://github.com/CrabDude/hookit/issues" | ||
}, | ||
"homepage": "https://github.com/CrabDude/hookit", | ||
"_id": "hookit@0.1.1", | ||
"_from": "hookit@~0.1.1" | ||
} |
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
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
5920
6
104
1
2