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

call-hook

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

call-hook - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

2

package.json
{
"name": "call-hook",
"version": "1.0.0",
"version": "1.1.0",
"description": "Hook function calls with other functions",

@@ -5,0 +5,0 @@ "main": "index.js",

module.exports = function post (callee, preCall) {
var aborted
return function callHook () {
var result = preCall.apply(undefined, arguments)
var result = preCall.apply({ abort: abort }, arguments)
if (aborted) return aborted.returnValue
return callee.apply(undefined, Array.isArray(result) ? result : arguments)
}
function abort (returnValue) {
aborted = { returnValue: returnValue }
}
}

@@ -9,6 +9,6 @@ # call-hook

Prehooks execute before the hooked function executes and may alter the arguments
sent to the hooked function. Posthooks execute after the hooked function,
receive the same arguments as the hooked function, and may also access it's
return value.
Prehooks execute before the callee (aka target) function executes and may
alter the arguments sent to the callee or abort callee execution, while
posthooks execute after the callee function, receive the same arguments
as the callee, and may also access it's return value.

@@ -59,4 +59,8 @@ ## example

and `callee` functions will receive the arguments of the `hookedFunc` function
call. Both functions are executed in an `undefined` context. The return value of
the `hookedFunc` function call will be the return value of `callee`.
call. The callee is executed in an `undefined` context, while the `preCall`
function is executed in the context of an object that offers the `abort` function.
Calling `abort` will prevent `callee` from being called. The return value of
the `hookedFunc` function call will be the return value of `callee`, unless
`abort` was called, in which case the returnValue of `hookedFunc` will be the
1st argument to `abort`.

@@ -79,2 +83,20 @@ Example of altering arguments being sent to `callee`:

Example of aborting:
```javascript
var pre = require('call-hook/pre')
function roll (sides) {
return Math.ceil(Math.random() * sides)
}
// hijack roll, if a 20 sided die is requested, always return 20
var roll = pre(roll, function loadedD20 (sides) {
if (sides === 20) return this.abort(20)
})
console.log('10-sided die roll result: ' + roll(10)) // 1 - 10
console.log('20-sided die roll result: ' + roll(20)) // always 20
```
### hookedFunc = post(callee, postCall)

@@ -81,0 +103,0 @@

@@ -63,1 +63,16 @@ var test = require('tape'),

})
test('pre abort prohibits call to callee', function (t) {
t.plan(1)
var returnValue = pre(callee, function () {
this.abort('interrupted!')
})()
t.equal(returnValue, 'interrupted!', 'returnValue = interrupted!')
function callee () {
t.fail('callee should not be called')
return true
}
})
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