From cfac231a5b2dbc94da42c747b039fe7a61a52a8f Mon Sep 17 00:00:00 2001 From: lei xia Date: Wed, 18 Oct 2017 08:35:39 -0700 Subject: [PATCH] add zookeeper definition (#20588) * add zookeeper definition * type defintion fixed * zookeeper --- types/zookeeper/index.d.ts | 104 +++++++++++++++++++++++++++++ types/zookeeper/tsconfig.json | 23 +++++++ types/zookeeper/tslint.json | 1 + types/zookeeper/zookeeper-tests.ts | 24 +++++++ 4 files changed, 152 insertions(+) create mode 100644 types/zookeeper/index.d.ts create mode 100644 types/zookeeper/tsconfig.json create mode 100644 types/zookeeper/tslint.json create mode 100644 types/zookeeper/zookeeper-tests.ts diff --git a/types/zookeeper/index.d.ts b/types/zookeeper/index.d.ts new file mode 100644 index 0000000000..92d1b06c96 --- /dev/null +++ b/types/zookeeper/index.d.ts @@ -0,0 +1,104 @@ +// Type definitions for zookeeper 3.4 +// Project: https://github.com/yfinkelstein/node-zookeeper#readme +// Definitions by: xialeistudio +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 +/// + +type ACL = number | { perms: number, scheme: string, auth: string }; + +interface Stat { + czxid: number; + mzxid: number; + ctime: number; + mtime: number; + version: number; + cversion: number; + aversion: number; + ephemeralOwner: string; + dataLength: number; + numChildren: number; + pzxid: number; +} + +interface ConnectionOptions { + connect?: string; + timeout?: number; + host_order_deterministic?: boolean; + data_as_buffer?: boolean; + debug_level?: number; +} + +type child2_callback = (rc: number, error: string, children: string[], stat: Stat) => void; +type watch_callback = (type: number, state: number, path: string) => void; +type acl_callback = (rc: number, error: string, acl: ACL[], stat: Stat) => void; + +type Callback = (rc: number, error: string, value: T) => void; +export = Zookeeper; + +declare class Zookeeper extends Client { + constructor(options: ConnectionOptions); + + connect(callback: (e: Error | null, client: Client) => void): void; + + close(): void; +} + +declare class Client { + client_id?: string; + client_password?: any; + state?: number; + timeout?: number; + is_unrecoverable?: boolean; + + a_create(path: string, data: string | Buffer, flags: number, callback: Callback): void; + + mkdirp(path: string, callback: (e?: Error) => void): void; + + a_exists(path: string, watch: boolean, callback: Callback): void; + + a_get(path: string, watch: boolean, callback: Callback): void; + + a_get_children(path: string, watch: boolean, callback: Callback): void; + + a_get_children2(path: string, watch: boolean, callback: child2_callback): void; + + a_set(path: string, data: Buffer | string, version: number, callback: Callback): void; + + a_delete_(path: string, version: number, callback: Callback): void; + + a_set_acl(path: string, version: number, acl: ACL[], callback: Callback): void; + + a_get_acl(path: string, callback: acl_callback): void; + + add_auth(schema: string, auth: string, callback: Callback): void; + + aw_exists(path: string, watch_callback: watch_callback, callback: Callback): void; + + aw_get(path: string, watch_callback: watch_callback, callback: Callback): void; + + aw_get_children(path: string, watch_callback: watch_callback, callback: Callback): void; + + aw_get_children2(path: string, watch_callback: watch_callback, callback: child2_callback): void; +} + +declare namespace Zookeeper { + // log levels + const ZOO_LOG_LEVEL_ERROR = 1; + const ZOO_LOG_LEVEL_WARN = 2; + const ZOO_LOG_LEVEL_INFO = 3; + const ZOO_LOG_LEVEL_DEBUG = 4; + // acl permissions + const ZOO_OPEN_ACL_UNSAFE: number; + const ZOO_READ_ACL_UNSAFE: number; + const ZOO_CREATOR_ALL_ACL: number; + const ZOO_PERM_READ = 1; + const ZOO_PERM_WRITE = 2; + const ZOO_PERM_CREATE = 4; + const ZOO_PERM_DELETE = 8; + const ZOO_PERM_ADMIN = 16; + const ZOO_PERM_ALL = 31; + // Dunno + const ZOO_EPHEMERAL = 1; + const ZOO_SEQUENCE = 2; +} diff --git a/types/zookeeper/tsconfig.json b/types/zookeeper/tsconfig.json new file mode 100644 index 0000000000..281f393e23 --- /dev/null +++ b/types/zookeeper/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "zookeeper-tests.ts" + ] +} diff --git a/types/zookeeper/tslint.json b/types/zookeeper/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/zookeeper/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/zookeeper/zookeeper-tests.ts b/types/zookeeper/zookeeper-tests.ts new file mode 100644 index 0000000000..63f4fe5d09 --- /dev/null +++ b/types/zookeeper/zookeeper-tests.ts @@ -0,0 +1,24 @@ +import ZooKeeper = require('zookeeper'); + +const zk = new ZooKeeper({ + connect: "localhost:2181", + timeout: 2000, + debug_level: ZooKeeper.ZOO_LOG_LEVEL_WARN, + host_order_deterministic: false, + data_as_buffer: true, +}); +// test1 +zk.connect((e) => { + if (e) throw e; + console.log("zk session established, id=%s", zk.client_id); + zk.a_create("/node.js1", "some value", ZooKeeper.ZOO_SEQUENCE | ZooKeeper.ZOO_EPHEMERAL, (rc, error, path) => { + if (rc !== 0) { + console.log("zk node create result: %d, error: '%s', path=%s", rc, error, path); + } else { + console.log("created zk node %s", path); + process.nextTick(() => { + zk.close(); + }); + } + }); +});