
Research
/Security News
Toptal’s GitHub Organization Hijacked: 10 Malicious Packages Published
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
mongo-distributed-locks
Advanced tools
Node.js distributed locking based on Mongodb.
# Add to project
$ npm i mongo-distributed-locks
const DLocks = require('mongo-distributed-locks');
async function takeFee({ userId, fee }) {
let user = await User.findOne({ _id: userId });
user.balance -= fee;
return user.save();
}
function takeFeeWithLock(params) {
return DLocks.exec({
resource: 'createPayment',
id: params.userId,
fn: takeFee.bind(null, params)
});
}
function doWork() {
let userId = 'userId';
// current user balance is 60
return Promise.all([
takeFeeWithLock({ userId, fee: 10 }),
takeFeeWithLock({ userId, fee: 25 })
]);
// current user balance is 25
}
async function manageLockManually() {
let userId = 'userId';
let lock = await DLocks.lock({
resource: 'user',
id: userId
});
// current user balance is 60
try {
await takeFee({ userId, fee: 10 });
await takeFee({ userId, fee: 25 });
// current user balance is 25
}
finally {
await DLocks.unlock(lock);
}
}
static registerLoggerErrorFn(loggerErrorFn)
Registers logger error function (DLocks
can log a few error messages), if not provided console.error
is used`.
loggerErrorFn
- logger error function. Example of usage: DLocks.registerLoggerErrorFn(logger.error.bind(logger));
static exec({ resource, id, fn })
Creates a DLocks
instance and executes its exec
method. Shortcut for let instance = new DLocks(params); instance.exec();
.
resource
- operation: createPayment, or resource: user name.id
- id of the locking resource.fn
- function that does some operation on the locking resource.static lock({ resource, id, fn })
Locks resource and returns lock
object. This method can be helpful when you need to manage lock manually. Accepts the same parameters as static exec
.
unlock(lock)
Unlocks the previously locked resource.
lock
- previously generated lock
object.Alexander Mac
Licensed under the MIT license.
FAQs
Distributed locking based on Mongodb
The npm package mongo-distributed-locks receives a total of 3 weekly downloads. As such, mongo-distributed-locks popularity was classified as not popular.
We found that mongo-distributed-locks 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.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
Research
/Security News
Socket researchers investigate 4 malicious npm and PyPI packages with 56,000+ downloads that install surveillance malware.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.