Skip to content

Commit 035a06f

Browse files
authored
Merge pull request #291 from DavidVujic/boolean_path_exists
feat: add a Boolean "path exists" function
2 parents 866e8a0 + 7278e13 commit 035a06f

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ Have a look at the code in the [examples](./examples) folder: with __master__, _
103103
* `ttl` is optional. Must be positive if a TTL flag is used. See [Input parameters](#input-parameters)
104104
* `mkdirp(path, callback(Error))`
105105
* `stat = await exists(path, watch)`
106-
* rejects if node does not exist
106+
* rejects if node does not exist. There's also a `pathExists` as an alternative.
107+
* `trueOrFalseValue = await pathExists(path, watch)`
107108
* `data = await get(path, watch)`
108109
* `children = await get_children(path, watch)`
109110
* `[children, stat] = await get_children2( path, watch)`
@@ -123,7 +124,8 @@ Have a look at the code in the [examples](./examples) folder: with __master__, _
123124
*The watcher methods are forward-looking subscriptions that can recieve multiple callbacks whenever a matching event occurs.*
124125

125126
* `stat = await w_exists(path, watch_cb)`
126-
* rejects if node does not exist
127+
* rejects if node does not exist. There's also a `w_pathExists` as an alternative.
128+
* `trueOrFalseValue = await w_pathExists(path, watch)`
127129
* `data = await w_get(path, watch_cb)`
128130
* `children = await w_get_children(path, watch_cb)`
129131
* `[children, stat] = await w_get_children2 (path, watch_cb)`

examples/exists.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const { constants } = require('./wrapper');
2+
const { createNodes } = require('./setup');
3+
4+
const logger = require('./logger');
5+
6+
async function verifyNonExisting(client) {
7+
const tempNode = '/my-temporary-node';
8+
9+
const doesExist = await client.w_pathExists(tempNode, (data) => logger.log(`Node created with data: ${data}`));
10+
logger.log(`Does ${tempNode} exist? ${doesExist}`);
11+
12+
setTimeout(async () => {
13+
createNodes(client, [tempNode], constants.ZOO_EPHEMERAL);
14+
15+
const exists = await client.pathExists(tempNode, false);
16+
logger.log(`Does ${tempNode} exist now? ${exists}`);
17+
}, 3000);
18+
}
19+
20+
async function verifyTheNodeExistsFeature(client) {
21+
const doesStatusExist = await client.pathExists('/status', false);
22+
logger.log(`Does the /status node exist? ${doesStatusExist}`);
23+
24+
await verifyNonExisting(client);
25+
}
26+
27+
module.exports = {
28+
verifyTheNodeExistsFeature,
29+
};

examples/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { electLeader } = require('./electleader');
44
const { createWorker } = require('./createworker');
55
const { listen } = require('./addlistener');
66
const { addTask } = require('./addtask');
7+
const { verifyTheNodeExistsFeature } = require('./exists');
78

89
const logger = require('./logger');
910
const notifier = require('./notifier');
@@ -38,6 +39,8 @@ async function init() {
3839
});
3940

4041
await electLeader(client, '/master');
42+
43+
await verifyTheNodeExistsFeature(client);
4144
});
4245
}
4346

lib/typedeclarations.d.ts

+14
Original file line numberDiff line numberDiff line change
@@ -495,13 +495,27 @@ declare module "zookeeper" {
495495
* @returns {Promise.<stat>}
496496
*/
497497
exists(path: string, watch: boolean): Promise<stat>;
498+
/**
499+
* @param {string} path
500+
* @param {boolean} watch
501+
* @fulfill {boolean}
502+
* @returns {Promise.<boolean>}
503+
*/
504+
pathExists(path: string, watch: boolean): Promise<boolean>;
498505
/**
499506
* @param {string} path
500507
* @param {function} watchCb
501508
* @fulfill {stat}
502509
* @returns {Promise.<stat>}
503510
*/
504511
w_exists(path: string, watchCb: Function): Promise<stat>;
512+
/**
513+
* @param {string} path
514+
* @param {function} watchCb
515+
* @fulfill {boolean}
516+
* @returns {Promise.<boolean>}
517+
*/
518+
w_pathExists(path: string, watchCb: Function): Promise<boolean>;
505519
/**
506520
* @param {string} path
507521
* @param {boolean} watch

lib/zk_promise.js

+40
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ const ZkPromise = require('./promise');
44
const ZooKeeper = require('./zookeeper');
55
const zkConstants = require('./constants');
66

7+
function isTruthy(data) {
8+
if (data) {
9+
return true;
10+
}
11+
12+
return false;
13+
}
14+
715
/**
816
* A promisified version of the ZooKeeper class
917
* @class
@@ -46,6 +54,22 @@ class ZooKeeperPromise extends ZooKeeper {
4654
return this.promisify(super.a_exists, [path, watch]);
4755
}
4856

57+
/**
58+
* @param {string} path
59+
* @param {boolean} watch
60+
* @fulfill {boolean}
61+
* @returns {Promise.<boolean>}
62+
*/
63+
async pathExists(path, watch) {
64+
try {
65+
const stat = await this.exists(path, watch);
66+
67+
return isTruthy(stat);
68+
} catch (e) {
69+
return false;
70+
}
71+
}
72+
4973
/**
5074
* @param {string} path
5175
* @param {function} watchCb
@@ -56,6 +80,22 @@ class ZooKeeperPromise extends ZooKeeper {
5680
return this.promisify(super.aw_exists, [path, watchCb]);
5781
}
5882

83+
/**
84+
* @param {string} path
85+
* @param {function} watchCb
86+
* @fulfill {boolean}
87+
* @returns {Promise.<boolean>}
88+
*/
89+
async w_pathExists(path, watchCb) {
90+
try {
91+
const stat = await this.w_exists(path, watchCb);
92+
93+
return isTruthy(stat);
94+
} catch (e) {
95+
return false;
96+
}
97+
}
98+
5999
/**
60100
* @param {string} path
61101
* @param {boolean} watch

0 commit comments

Comments
 (0)