
Security News
npm ‘is’ Package Hijacked in Expanding Supply Chain Attack
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Observe the object property getter, setter or method calls and add custom behavior.
Observe the object property getter, setter or method calls and add custom behavior.
ghostrap can intercept object on any timing (e.g. property value change, assignment, function invocation, etc).
<script src="ghostrap.js"></script>
or
<script src="ghostrap.min.js"></script>
Function object ghostrap will defined in the global scope.
npm install ghostrap
var ghostrap = require('ghostrap');
bower install ghostrap
ghostrap
is a handy constructor.
new
operator is not needed.
In this example, outputs a log when the object message has change.
var myModel = {
id: 1,
message: 'Hello'
};
var ghost = ghostrap(myModel);
ghost.on('change:message', function() {
console.log('message changed!');
});
myModel.message = 'Good evening';
// message changed!
myModel.message = 'Good night';
// message changed!
Add new handler.
The first argument type separates by a colon.
'when:propName'
e.g. 'get:myPropName'
The second argument func is a handler function. On callback arguments are following.
function(target, key, value, args) { ... }
Example:
var myData = {
data: '',
maxLength: 20
};
var ghost = ghostrap(myData);
ghost.on('set:data', function(target, key, value) {
// Truncate data value to the maximum length when set new data.
if (value.length > myData.maxLength) {
value = value.substr(0, myData.maxLength - 3) + '...';
}
return value;
});
myData.data = 'Lorem ipsum dolor sit amet';
console.log(myData.data); // 'Lorem ipsum dolor...'
Add a new handler. Just like on, but handler is called only once.
Remove a handler. If argument type is specified, same types handlers are removed. If argument func is specified, same handlers are removed. If arguments is omitted, all handlers are removed.
Example:
var myModel = {
id: 1,
message: 'Hello'
};
function onMessageChange(target, key, value) {
console.log('message changed!', value);
}
var ghost = ghostrap(myModel);
ghost.on('change:message', onMessageChange);
myModel.message = 'Good evening';
// message changed!
myModel.message = 'Good night';
// message changed!
ghost.off('change:message', onMessageChange);
myModel.message = 'Good morning';
// no console logs.
Clear all handlers and release the target object reference.
MIT
FAQs
Observe the object property getter, setter or method calls and add custom behavior.
The npm package ghostrap receives a total of 0 weekly downloads. As such, ghostrap popularity was classified as not popular.
We found that ghostrap demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Security News
A critical flaw in the popular npm form-data package could allow HTTP parameter pollution, affecting millions of projects until patched versions are adopted.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.