Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
@akeating-redhat/mongodb-lock
Advanced tools
A really light-weight way to get distributed locks with a nice API if you're already using MongoDB.
Create a connection to your MongoDB database, and use it to create a lock object:
var mongodb = require('mongodb')
var mongoDbLock = require('mongodb-lock')
var con = 'mongodb://localhost:27017/test'
mongodb.MongoClient.connect(con, function(err, db) {
// supply the database, the collection to use and the lock name
var lock = mongoDbLock(db, 'locks', 'database-backup')
})
Now, acquire the lock:
lock.acquire(function(err, code) {
if (err) {
return console.error(code)
}
if ( code ) {
// lock was acquired
console.log('code=' + code)
}
else {
// lock was not acquired
}
})
Once you have a lock, you have a 30 second timeout until the lock is released. You can release it earlier by supplying the code:
lock.release(code, function(err, ok) {
if (err) {
return console.error(err)
}
if (ok) {
console.log('Lock released ok')
}
else {
console.log("Lock was not released, perhaps it's already been released or timed out")
}
})
You should make sure any indexes have been added to the collection to make the queries faster:
lock.ensureIndexes(function(err) {
if (err) {
return console.error(err)
}
// all ok
})
Multiple locks can use the same collection and operate quite independently:
var dbBackupLock = mongoDbLock(db, 'locks', 'database-backup')
var hourlyStats = mongoDbLock(db, 'locks', 'hourly-stats')
var sendInvoices = mongoDbLock(db, 'locks', 'send-invoices')
Currently there is only the option of the timeout. Currently the default is 30 seconds, but you can change it (in milliseconds):
// lock for 60 seconds
var uploadFiles = mongoDbLock(db, 'locks', 'upload-files', { timeout : 60 * 1000})
uploadFiles.lock(function(err, code) {
// locked for 60s
})
Written by Andrew Chilton - Twitter.
MIT - http://chilts.mit-license.org/2014/
(Ends)
FAQs
Locks which uses MongoDB's atomic operations.
The npm package @akeating-redhat/mongodb-lock receives a total of 2 weekly downloads. As such, @akeating-redhat/mongodb-lock popularity was classified as not popular.
We found that @akeating-redhat/mongodb-lock 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
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.