Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a Boolean "path exists" function #291

Merged
merged 3 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ Have a look at the code in the [examples](./examples) folder: with __master__, _
* `ttl` is optional. Must be positive if a TTL flag is used. See [Input parameters](#input-parameters)
* `mkdirp(path, callback(Error))`
* `stat = await exists(path, watch)`
* rejects if node does not exist
* rejects if node does not exist. There's also a `pathExists` as an alternative.
* `trueOrFalseValue = await pathExists(path, watch)`
* `data = await get(path, watch)`
* `children = await get_children(path, watch)`
* `[children, stat] = await get_children2( path, watch)`
Expand All @@ -123,7 +124,8 @@ Have a look at the code in the [examples](./examples) folder: with __master__, _
*The watcher methods are forward-looking subscriptions that can recieve multiple callbacks whenever a matching event occurs.*

* `stat = await w_exists(path, watch_cb)`
* rejects if node does not exist
* rejects if node does not exist. There's also a `w_pathExists` as an alternative.
* `trueOrFalseValue = await w_pathExists(path, watch)`
* `data = await w_get(path, watch_cb)`
* `children = await w_get_children(path, watch_cb)`
* `[children, stat] = await w_get_children2 (path, watch_cb)`
Expand Down
29 changes: 29 additions & 0 deletions examples/exists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { constants } = require('./wrapper');
const { createNodes } = require('./setup');

const logger = require('./logger');

async function verifyNonExisting(client) {
const tempNode = '/my-temporary-node';

const doesExist = await client.w_pathExists(tempNode, (data) => logger.log(`Node created with data: ${data}`));
logger.log(`Does ${tempNode} exist? ${doesExist}`);

setTimeout(async () => {
createNodes(client, [tempNode], constants.ZOO_EPHEMERAL);

const exists = await client.pathExists(tempNode, false);
logger.log(`Does ${tempNode} exist now? ${exists}`);
}, 3000);
}

async function verifyTheNodeExistsFeature(client) {
const doesStatusExist = await client.pathExists('/status', false);
logger.log(`Does the /status node exist? ${doesStatusExist}`);

await verifyNonExisting(client);
}

module.exports = {
verifyTheNodeExistsFeature,
};
3 changes: 3 additions & 0 deletions examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { electLeader } = require('./electleader');
const { createWorker } = require('./createworker');
const { listen } = require('./addlistener');
const { addTask } = require('./addtask');
const { verifyTheNodeExistsFeature } = require('./exists');

const logger = require('./logger');
const notifier = require('./notifier');
Expand Down Expand Up @@ -38,6 +39,8 @@ async function init() {
});

await electLeader(client, '/master');

await verifyTheNodeExistsFeature(client);
});
}

Expand Down
14 changes: 14 additions & 0 deletions lib/typedeclarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,13 +495,27 @@ declare module "zookeeper" {
* @returns {Promise.<stat>}
*/
exists(path: string, watch: boolean): Promise<stat>;
/**
* @param {string} path
* @param {boolean} watch
* @fulfill {boolean}
* @returns {Promise.<boolean>}
*/
pathExists(path: string, watch: boolean): Promise<boolean>;
/**
* @param {string} path
* @param {function} watchCb
* @fulfill {stat}
* @returns {Promise.<stat>}
*/
w_exists(path: string, watchCb: Function): Promise<stat>;
/**
* @param {string} path
* @param {function} watchCb
* @fulfill {boolean}
* @returns {Promise.<boolean>}
*/
w_pathExists(path: string, watchCb: Function): Promise<boolean>;
/**
* @param {string} path
* @param {boolean} watch
Expand Down
40 changes: 40 additions & 0 deletions lib/zk_promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ const ZkPromise = require('./promise');
const ZooKeeper = require('./zookeeper');
const zkConstants = require('./constants');

function isTruthy(data) {
if (data) {
return true;
}

return false;
}

/**
* A promisified version of the ZooKeeper class
* @class
Expand Down Expand Up @@ -46,6 +54,22 @@ class ZooKeeperPromise extends ZooKeeper {
return this.promisify(super.a_exists, [path, watch]);
}

/**
* @param {string} path
* @param {boolean} watch
* @fulfill {boolean}
* @returns {Promise.<boolean>}
*/
async pathExists(path, watch) {
try {
const stat = await this.exists(path, watch);

return isTruthy(stat);
} catch (e) {
return false;
}
}

/**
* @param {string} path
* @param {function} watchCb
Expand All @@ -56,6 +80,22 @@ class ZooKeeperPromise extends ZooKeeper {
return this.promisify(super.aw_exists, [path, watchCb]);
}

/**
* @param {string} path
* @param {function} watchCb
* @fulfill {boolean}
* @returns {Promise.<boolean>}
*/
async w_pathExists(path, watchCb) {
try {
const stat = await this.w_exists(path, watchCb);

return isTruthy(stat);
} catch (e) {
return false;
}
}

/**
* @param {string} path
* @param {boolean} watch
Expand Down