Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
rorschach
Advanced tools
Changelog
Readme
Rorschach is a Node.js module for high-level interaction with ZooKeeper. Currently it provides all the basic ZooKeeper operations. Refer to the Rorschach
API doc below. And in a mean time more recipes will be added.
Rorschach is meant to provide functionality similar to Apache Curator. It was started as implementation of distributed locking recipe and most of algorithms are taken from Curator sources.
While Curator is a ZooKeeper Keeper, Rorschach is a ZooKeeper Watchman.
npm install rorschach --save
'use strict';
var Rorschach = require('rorschach');
var zookeeper = require('node-zookeeper-client');
var ACL = Rorschach.ACL;
var CreateMode = Rorschach.CreateMode;
var Event = Rorschach.Event;
var Lock = Rorschach.Lock;
var ReadWriteLock = Rorschach.ReadWriteLock;
var client = new Rorschach('127.0.0.1:2181');
client.on('connected', doTheWork);
client.on('error', onerror);
function doTheWork() {
client
.create()
.withMode(CreateMode.EPHEMERAL)
.withACL(ACL.READ_ACL_UNSAFE)
.forPath('/a', /*afterCreate(err, path)*/);
client
.create()
.withProtection()
.creatingParentsIfNeeded()
.forPath('/my/cool/znode/foo/bar', /*afterCreate(err, path)*/);
client
.delete()
.deleteChildrenIfNeeded()
.guaranteed()
.forPath('/my/cool/znode', /*afterDelete(err)*/);
client
.getData()
.usingWatcher(function watcher(event) {
if (event.getType() === Event.NODE_DELETED) {
// handle deleted
}
else if (event.getType() === Event.NODE_DATA_CHANGED) {
// handle data change
}
else {
console.warn('Wow, really?');
}
})
.forPath('/some/path', /*afterGetData(err, data, stat)*/);
var lock = new Lock(client, '/my/znodes/locks/myResource');
lock.acquire(500, function afterAcquire(err) {
// handle error
// do the work with `myResource`
lock.release(/*callback(err)*/);
});
var rwLock = new ReadWriteLock(client, '/my/znodes/locks/anotherResource');
rwLock.writeLock().acquire(1000, function afterWriteAcquire(err) {
// handle error
// update `anotherResource`
rwLock.writeLock().release(/*callback(err)*/);
});
}
function onerror(err) {
console.warn('[Error: %d]: %s', err.getCode(), err.stack);
}
Main class and, better to add, namespace.
For convenience, following node-zookeeper-client classes & constants were referenced by Rorschach
:
ACL
CreateMode
Event
Exception
Id
Permission
State
So you can use them like following:
function watcher(event) {
if (event.getType() === Rorschach.Event.NODE_DELETED) {
console.log('At last...');
}
}
Create instance.
Arguments
String
ZooKeeper connection stringObject
Options:
Object|RetryPolicy
RetryPolicy instance or optionsObject
ZooKeeper client optionsconnected
function onConnected() { }
Emitted when connection to ZooKeeper server is established.
connectionStateChanged
function onStateChanged(state) {
if (state === Rorschach.ConnectionState.CONNECTED || state === Rorschach.ConnectionState.RECONNECTED) {
console.info('Let\'s rock!');
}
/* else if ... */
}
Emitted whenever ZooKeeper client connection state changes. The only argument is state which is one of ConnectionState
s.
error
function onerror(err) {
/* whatTheFussIsGoingOn(err); */
}
Currently, this event is emitted only when some operation fails in retry loop. It is emitted only if error
event listener is added to Rorschach
instance - to save user from Unhandled 'error' event
.
Close connection to ZooKeeper.
Arguments
function
Callback functionCreateBuilder
create()Instantiate create operation builder.
Returns
CreateBuilder
Builder instanceDeleteBuilder
delete()Instantiate delete operation builder.
Returns
DeleteBuilder
Builder instanceExistsBuilder
exists()Instantiate exists operation builder.
Returns
ExistsBuilder
Builder instanceGetACLBuilder
getACL()Instantiate get ACL builder.
Returns
GetACLBuilder
Builder instanceGetChildrenBuilder
getChildren()Instantiate get children builder.
Returns
GetChildrenBuilder
Builder instanceGetDataBuilder
getData()Instantiate get data builder.
Returns
GetDataBuilder
Builder instanceSetACLBuilder
setACL()Instantiate set ACL builder.
Returns
SetACLBuilder
Builder instanceSetDataBuilder
setData()Instantiate set data builder.
Returns
SetDataBuilder
Builder instanceRepresents high-level connection state.
ConnectionState
CONNECTEDConnected state. Emitted only once for each Rorschach instance.
ConnectionState
READ_ONLYConnected in read-only mode.
ConnectionState
RECONNECTEDConnection was re-established.
ConnectionState
SUSPENDEDConnection was lost, but we're waiting to re-connect.
ConnectionState
LOSTConnection was lost. Bye-bye, white pony.
Return "connected" state.
Utility functions.
Delete children for given path and maybe given znode.
Arguments
Client
ZooKeeper clientString
Node pathBoolean
Delete node itself Default: false
function
Callback function: (err)
String
join(args...)Join paths.
Arguments
String
PathsReturns
String
Create path and parent nodes if needed.
Arguments
Client
ZooKeeper clientString
Boolean
Array.<ACL>
function
Callback function: (err)
Retry policy controls behavior of Rorschach in case of operational errors.
Instantiate policy.
Arguments
Object
Options:
Number
Max number of attemptsArray.<String>|function
Error codes or error validation functionNumber
DEFAULT_MAX_ATTEMPTSDefault number of operation attempts.
Array.<Number>
DEFAULT_RETRYABLE_ERRORSDefault codes of errors allowing re-try in case of no. of attempts < maxRetries.
Boolean
isRetryable(err)Check if error is retryable.
Arguments
Error|Exception
ZooKeeper client errorReturns
Boolean
Create request builder.
CreateBuilder
creatingParentsIfNeeded()If path create operation will receive NO_NODE
error then builder will make an attempt to create parent nodes.
Returns
CreateBuilder
Execute create op.
Arguments
String
Path to znodeBuffer
ZNode data to set Default: null
function
Callback function: (executionError, path)
CreateBuilder
withACL(acls)Set ACLs.
Arguments
Array.<ACL>
Returns
CreateBuilder
CreateBuilder
withMode(mode)Set create mode.
Arguments
Number
CreateModeReturns
CreateBuilder
CreateBuilder
withProtection()See this page for explanation.
Returns
CreateBuilder
Delete request builder.
DeleteBuilder
deleteChildrenIfNeeded()If delete operation receives NOT_EMPTY
error then make an attempt to delete child nodes.
Returns
DeleteBuilder
Execute delete.
Arguments
String
Node pathfunction
Callback function: (err)
DeleteBuilder
guaranteed()Mark delete op. as guaranteed.
Returns
DeleteBuilder
DeleteBuilder
withVersion(version)Set node version to delete.
Arguments
Number
Returns
DeleteBuilder
Exists request builder.
Execute exists().
Arguments
String
Node pathfunction
Callback function: (executionError, exists, stat)
ExistsBuilder
usingWatcher(watcher)Add watcher to operation request.
Arguments
function
Watch function: (event)
Returns
ExistsBuilder
Get ACL request builder.
Execute getACL().
Arguments
String
Node pathfunction
Callback function: (executionError, acls, stat)
Get children request builder.
Execute getChildren().
Arguments
String
Node pathfunction
Callback function: (executionError, data, stat)
GetChildrenBuilder
usingWatcher(watcher)Add watcher to operation request.
Arguments
function
Watch function: (event)
Returns
GetChildrenBuilder
Get data request builder.
Execute getData().
Arguments
String
Node pathfunction
Callback function: (executionError, data, stat)
GetDataBuilder
usingWatcher(watcher)Add watcher to operation request.
Arguments
function
Watch function: (event)
Returns
GetDataBuilder
Set ACL request builder.
Execute setACL().
Arguments
String
Node pathArray.<ACL>
ACLsfunction
Callback function: (executionError, stat)
SetACLBuilder
withVersion(version)Set node ACL version (number of changes of ACL).
It's not the same as node version (number of data changes).
Arguments
Number
Returns
SetACLBuilder
Set data request builder.
Execute setData().
Arguments
String
Node pathBuffer
Data to setfunction
Callback function: (executionError, stat)
SetDataBuilder
withVersion(version)Set node version.
Arguments
Number
Returns
SetDataBuilder
Leader election recipe implementation.
Leader election participant.
Arguments
Rorschach
Rorschach instanceString
Election pathString
Participant iderror
function onerror(err) {
// do smth. with error
}
Emitted when some background operation fails. You must always set listener for this event.
isLeader
function leaderListener() {
// start tasks for which leader is responsible
}
Emitted when leadership is obtained.
notLeader
function notLeaderListener() {
// stop tasks for which leader is responsible
}
Emitted when leadership is lost.
Rorschach
clientRef. to client.
String
pathZooKeeper path where participants' nodes exist.
String
idId of participant. It's kept in node.
Boolean
hasLeadership()Check if our node is leader.
Returns
Boolean
Start taking part in election process.
Arguments
function
Callback function: (err)
Leave the game for youngz.
Arguments
function
Callback function: (err)
Distributed re-entrant lock.
Distributed lock implementation
Arguments
Rorschach
Rorschach instanceString
Base lock pathString
Ephemeral node nameLockDriver
Lock utilities Default: new LockDriver()
String
LOCK_NAMEDefault lock node name.
Rorschach
clientKeep ref to client as all the low-level operations are done through it.
String
basePathBase path should be valid ZooKeeper path.
String
lockNameNode name.
LockDriver
driverLock driver.
String
lockPathSequence node name (set when acquired).
Number
maxLeasesNumber of max leases.
Number
acquiresNumber of acquires.
Acquire a lock.
Arguments
Number
Time to wait for (milliseconds).function
Callback function: (err)
Destroy lock i.e. remove node and set acquires
counter to zero.
Arguments
function
Optional callback functionBoolean
isOwner()Check lock is owned by process.
Returns
Boolean
Release the lock.
Arguments
function
Callback function: (err)
Lock
setMaxLeases(maxLeases)Set max leases.
Arguments
Number
Max # of participants holding the lock at one timeReturns
Lock
Instance is returned for chainingReaders-writer lock implementation.
Initialize read and write mutexes.
Arguments
Rorschach
Rorschach instanceString
Base lock pathString
READ_LOCK_NAMERead lock node name.
string
WRITE_LOCK_NAMEWrite lock node name.
Lock
writeMutexWrite mutex.
Lock
readMutexRead mutex.
Lock
readLock()Return read mutex.
Returns
Lock
Read lockLock
writeLock()Return write mutex.
Returns
Lock
Write lockError which serves a wrapper to actual ZooKeeper client error. It is returned by operation builders in case of failure.
Error
originalOriginal error instance.
String
operationOperation type: create
, remove
, etc.
Array
operationArgsArguments passed to original ZK client method.
Number
getCode()Defined only if original error is instance of Exception
. Returns error code of
original error.
See CHANGELOG.md.
InterProcessSemaphoreV2
;InterProcessSemaphoreMutex
;InterProcessMultiLock
;See LICENSE.md.
FAQs
Curator-like framework for ZooKeeper
The npm package rorschach receives a total of 16 weekly downloads. As such, rorschach popularity was classified as not popular.
We found that rorschach 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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.