node-redis-warlock
Advanced tools
Comparing version 0.2.0 to 1.0.0
@@ -1,4 +0,13 @@ | ||
Changelog | ||
--- | ||
# [1.0.0](https://github.com/TheDeveloper/warlock/compare/v0.2.0...v1.0.0) (2021-04-02) | ||
**This is a major release and contain breaking changes. Please read this changelog before upgrading.** | ||
### BREAKING CHANGES | ||
* Drop support for node 6. | ||
### Bug Fixes | ||
* Switch deprecated [node-redis-scripty](https://github.com/TheDeveloper/scripty) to [node-redis-script](https://github.com/TheDeveloper/node-redis-script). | ||
* Updated deps. | ||
# v0.2.0 | ||
@@ -5,0 +14,0 @@ |
@@ -1,12 +0,11 @@ | ||
var crypto = require('crypto'); | ||
var Scripty = require('node-redis-scripty'); | ||
var UUID = require('uuid'); | ||
const UUID = require('uuid'); | ||
const { createScript } = require('./del'); | ||
module.exports = function(redis){ | ||
var warlock = {}; | ||
module.exports = function (redis) { | ||
const warlock = {}; | ||
var scripty = new Scripty(redis); | ||
const parityDel = createScript(redis); | ||
warlock.makeKey = function(key) { | ||
return key + ':lock'; | ||
warlock.makeKey = function (key) { | ||
return `${key}:lock`; | ||
}; | ||
@@ -20,4 +19,4 @@ | ||
*/ | ||
warlock.lock = function(key, ttl, cb) { | ||
cb = cb || function(){}; | ||
warlock.lock = function (key, ttl, cb) { | ||
cb = cb || function () {}; | ||
@@ -28,3 +27,3 @@ if (typeof key !== 'string') { | ||
var id; | ||
let id; | ||
UUID.v1(null, (id = new Buffer(16))); | ||
@@ -35,10 +34,10 @@ id = id.toString('base64'); | ||
'PX', ttl, 'NX', | ||
function(err, lockSet) { | ||
(err, lockSet) => { | ||
if (err) return cb(err); | ||
var unlock = warlock.unlock.bind(warlock, key, id); | ||
let unlock = warlock.unlock.bind(warlock, key, id); | ||
if (!lockSet) unlock = false; | ||
return cb(err, unlock, id); | ||
} | ||
}, | ||
); | ||
@@ -49,4 +48,4 @@ | ||
warlock.unlock = function(key, id, cb) { | ||
cb = cb || function(){}; | ||
warlock.unlock = async (key, id, cb) => { | ||
cb = cb || function () {}; | ||
@@ -57,11 +56,10 @@ if (typeof key !== 'string') { | ||
scripty.loadScriptFile( | ||
'parityDel', | ||
__dirname + '/lua/parityDel.lua', | ||
function(err, parityDel){ | ||
if (err) return cb(err); | ||
return parityDel.run(1, warlock.makeKey(key), id, cb); | ||
} | ||
); | ||
const numKeys = 1; | ||
const _key = warlock.makeKey(key); | ||
try { | ||
const result = await parityDel(numKeys, _key, id); | ||
cb(null, result); | ||
} catch (e) { | ||
cb(e); | ||
} | ||
}; | ||
@@ -72,8 +70,8 @@ | ||
*/ | ||
warlock.optimistic = function(key, ttl, maxAttempts, wait, cb) { | ||
var attempts = 0; | ||
warlock.optimistic = function (key, ttl, maxAttempts, wait, cb) { | ||
let attempts = 0; | ||
var tryLock = function() { | ||
var tryLock = function () { | ||
attempts += 1; | ||
warlock.lock(key, ttl, function(err, unlock) { | ||
warlock.lock(key, ttl, (err, unlock) => { | ||
if (err) return cb(err); | ||
@@ -83,3 +81,3 @@ | ||
if (attempts >= maxAttempts) { | ||
var e = new Error('unable to obtain lock'); | ||
const e = new Error('unable to obtain lock'); | ||
e.maxAttempts = maxAttempts; | ||
@@ -86,0 +84,0 @@ e.key = key; |
{ | ||
"name": "node-redis-warlock", | ||
"version": "0.2.0", | ||
"version": "1.0.0", | ||
"description": "Battle-hardened distributed locking using redis", | ||
"main": "lib/warlock.js", | ||
"scripts": { | ||
"test": "mocha -R list ./test/warlock", | ||
"bench": "mocha -R list ./test/bench" | ||
"pretest": "npm run start-redis", | ||
"posttest": "npm run cleanup", | ||
"start-redis": "docker-compose up -d redis", | ||
"test": "mocha --exit ./test/warlock", | ||
"cleanup": "docker-compose stop && docker-compose rm -f" | ||
}, | ||
@@ -26,11 +29,13 @@ "repository": { | ||
"dependencies": { | ||
"node-redis-scripty": "0.0.5", | ||
"uuid": "^2.0.1" | ||
"node-redis-script": "^2.0.1", | ||
"uuid": "^8.3.2" | ||
}, | ||
"devDependencies": { | ||
"async": "^1.5.1", | ||
"mocha": "^2.3.4", | ||
"redis": "^2.4.2", | ||
"should": "^8.0.2" | ||
"eslint": "^7.23.0", | ||
"eslint-config-airbnb-base": "^14.2.1", | ||
"eslint-plugin-import": "^2.22.1", | ||
"mocha": "^8.3.2", | ||
"redis": "^3.1.0", | ||
"should": "^13.2.3" | ||
} | ||
} |
warlock | ||
======= | ||
[![Travis](https://travis-ci.org/TheDeveloper/warlock.svg?branch=master)](https://travis-ci.org/TheDeveloper/warlock) | ||
[![Dependency Status](https://david-dm.org/thedeveloper/warlock.svg)](https://david-dm.org/thedeveloper/warlock) | ||
[![Join the chat at https://gitter.im/TheDeveloper/warlock](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/TheDeveloper/warlock?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
@@ -22,15 +20,14 @@ | ||
```javascript | ||
const Warlock = require('node-redis-warlock'); | ||
const Redis = require('redis'); | ||
var Warlock = require('node-redis-warlock'); | ||
var redis = require('redis'); | ||
// Establish a redis client and pass it to warlock | ||
var redis = redis.createClient(); | ||
var warlock = Warlock(redis); | ||
const redis = Redis.createClient(); | ||
const warlock = Warlock(redis); | ||
// Set a lock | ||
var key = 'test-lock'; | ||
var ttl = 10000; // Lifetime of the lock | ||
const key = 'test-lock'; | ||
const ttl = 10000; // Lifetime of the lock | ||
warlock.lock(key, ttl, function(err, unlock){ | ||
warlock.lock(key, ttl, (err, unlock) => { | ||
if (err) { | ||
@@ -55,7 +52,7 @@ // Something went wrong and we weren't able to set a lock | ||
// set a lock optimistically | ||
var key = 'opt-lock'; | ||
var ttl = 10000; | ||
var maxAttempts = 4; // Max number of times to try setting the lock before erroring | ||
var wait = 1000; // Time to wait before another attempt if lock already in place | ||
warlock.optimistic(key, ttl, maxAttempts, wait, function(err, unlock) {}); | ||
const key = 'opt-lock'; | ||
const ttl = 10000; | ||
const maxAttempts = 4; // Max number of times to try setting the lock before erroring | ||
const wait = 1000; // Time to wait before another attempt if lock already in place | ||
warlock.optimistic(key, ttl, maxAttempts, wait, (err, unlock) => {}); | ||
@@ -65,5 +62,5 @@ // unlock using the lock id | ||
var ttl = 10000; | ||
var lockId; | ||
let lockId; | ||
warlock.lock(key, ttl, function(err, _, id) { | ||
warlock.lock(key, ttl, (err, _, id) => { | ||
lockId = id; | ||
@@ -73,8 +70,7 @@ }); | ||
// each client who knows the lockId can release the lock | ||
warlock.unlock(key, lockId, function(err, result) { | ||
if(result == 1) { | ||
warlock.unlock(key, lockId, (err, result) => { | ||
if (result == 1) { | ||
// unlocked successfully | ||
} | ||
}); | ||
``` | ||
@@ -81,0 +77,0 @@ |
@@ -1,9 +0,10 @@ | ||
var should = require('should'); | ||
var redis = require('./setup/redisConnection'); | ||
var warlock = require('../lib/warlock')(redis); | ||
require('./setup/redisFlush'); | ||
const should = require('should'); | ||
const Redis = require('redis'); | ||
describe('locking', function() { | ||
it('sets lock', function (done) { | ||
warlock.lock('testLock', 1000, function(err, unlock) { | ||
const redis = Redis.createClient({ port: 6386 }); | ||
const warlock = require('../lib/warlock')(redis); | ||
describe('locking', () => { | ||
it('sets lock', (done) => { | ||
warlock.lock('testLock', 1000, (err, unlock) => { | ||
should.not.exist(err); | ||
@@ -16,4 +17,4 @@ (typeof unlock).should.equal('function'); | ||
it('does not set lock if it already exists', function(done) { | ||
warlock.lock('testLock', 1000, function(err, unlock) { | ||
it('does not set lock if it already exists', (done) => { | ||
warlock.lock('testLock', 1000, (err, unlock) => { | ||
should.not.exist(err); | ||
@@ -26,9 +27,9 @@ unlock.should.equal(false); | ||
it('does not alter expiry of lock if it already exists', function(done) { | ||
redis.pttl(warlock.makeKey('testLock'), function(err, ttl) { | ||
warlock.lock('testLock', 1000, function(err, unlock) { | ||
it('does not alter expiry of lock if it already exists', (done) => { | ||
redis.pttl(warlock.makeKey('testLock'), (err, ttl) => { | ||
warlock.lock('testLock', 1000, (err, unlock) => { | ||
should.not.exist(err); | ||
unlock.should.equal(false); | ||
redis.pttl(warlock.makeKey('testLock'), function(err, ttl2) { | ||
redis.pttl(warlock.makeKey('testLock'), (err, ttl2) => { | ||
(ttl2 <= ttl).should.equal(true); | ||
@@ -42,4 +43,4 @@ | ||
it('unlocks', function(done) { | ||
warlock.lock('unlock', 1000, function(err, unlock) { | ||
it('unlocks', (done) => { | ||
warlock.lock('unlock', 1000, (err, unlock) => { | ||
should.not.exist(err); | ||
@@ -51,9 +52,9 @@ unlock(done); | ||
describe('unlocking with id', function() { | ||
var lockId; | ||
describe('unlocking with id', () => { | ||
let lockId; | ||
it('sets lock and gets lock id', function(done) { | ||
warlock.lock('customlock', 20000, function(err, unlock, id) { | ||
it('sets lock and gets lock id', (done) => { | ||
warlock.lock('customlock', 20000, (err, unlock, id) => { | ||
should.not.exists(err); | ||
id.should.type("string"); | ||
id.should.type('string'); | ||
lockId = id; | ||
@@ -64,4 +65,4 @@ done(); | ||
it('does not unlock with wrong id', function(done) { | ||
warlock.unlock('customlock', "wrongid", function(err, result) { | ||
it('does not unlock with wrong id', (done) => { | ||
warlock.unlock('customlock', 'wrongid', (err, result) => { | ||
should.not.exists(err); | ||
@@ -73,4 +74,4 @@ result.should.equal(0); | ||
it('unlocks', function(done) { | ||
warlock.unlock('customlock', lockId, function(err, result) { | ||
it('unlocks', (done) => { | ||
warlock.unlock('customlock', lockId, (err, result) => { | ||
should.not.exists(err); | ||
@@ -77,0 +78,0 @@ result.should.equal(1); |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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 v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
10531
0
6
11
146
77
1
+ Addednode-redis-script@^2.0.1
+ Addednode-redis-script@2.0.1(transitive)
+ Addeduuid@8.3.2(transitive)
- Removednode-redis-scripty@0.0.5
- Removedextend@1.3.0(transitive)
- Removedlru-cache@2.7.3(transitive)
- Removednode-redis-scripty@0.0.5(transitive)
- Removeduuid@2.0.3(transitive)
Updateduuid@^8.3.2