mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-16 11:02:11 +08:00
Merge pull request #14235 from plantain-00/node-zookeeper-client
add types of node-zookeeper-client
This commit is contained in:
169
node-zookeeper-client/index.d.ts
vendored
Normal file
169
node-zookeeper-client/index.d.ts
vendored
Normal file
@@ -0,0 +1,169 @@
|
||||
// Type definitions for node-zookeeper-client 0.2
|
||||
// Project: https://github.com/alexguan/node-zookeeper-client
|
||||
// Definitions by: York Yao <https://github.com/plantain-00/>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.1
|
||||
|
||||
/// <reference types="node" />
|
||||
|
||||
import * as EventEmitter from "events";
|
||||
|
||||
export class Id {
|
||||
scheme: string;
|
||||
id: string;
|
||||
constructor(scheme: string, id: string);
|
||||
}
|
||||
|
||||
export class ACL {
|
||||
perms: number;
|
||||
id: Id;
|
||||
constructor(perms: number, id: Id);
|
||||
}
|
||||
|
||||
export const Permission: {
|
||||
READ: number,
|
||||
WRITE: number,
|
||||
CREATE: number,
|
||||
DELETE: number,
|
||||
ADMIN: number,
|
||||
ALL: number,
|
||||
};
|
||||
|
||||
export interface Stat {
|
||||
czxid: number;
|
||||
mzxid: number;
|
||||
ctime: number;
|
||||
mtime: number;
|
||||
version: number;
|
||||
cversion: number;
|
||||
aversion: number;
|
||||
ephemeralOwner: number;
|
||||
dataLength: number;
|
||||
numChildren: number;
|
||||
pzxid: number;
|
||||
}
|
||||
|
||||
export class State {
|
||||
static DISCONNECTED: State;
|
||||
static SYNC_CONNECTED: State;
|
||||
static AUTH_FAILED: State;
|
||||
static CONNECTED_READ_ONLY: State;
|
||||
static SASL_AUTHENTICATED: State;
|
||||
static EXPIRED: State;
|
||||
|
||||
name: string;
|
||||
code: number;
|
||||
constructor(name: string, code: number);
|
||||
toString(): string;
|
||||
}
|
||||
|
||||
export class Event {
|
||||
static NODE_CREATED: number;
|
||||
static NODE_DELETED: number;
|
||||
static NODE_DATA_CHANGED: number;
|
||||
static NODE_CHILDREN_CHANGED: number;
|
||||
type: string;
|
||||
name: string;
|
||||
path: string;
|
||||
constructor(type: string, name: string, path: string);
|
||||
toString(): string;
|
||||
getType(): string;
|
||||
getName(): string;
|
||||
getPath(): string;
|
||||
}
|
||||
|
||||
interface Transaction {
|
||||
create(path: string, dataOrAclsOrmode1?: Buffer | ACL[] | number, dataOrAclsOrmode2?: Buffer | ACL[] | number, dataOrAclsOrmode3?: Buffer | ACL[] | number): this;
|
||||
setData(path: string, data: Buffer | null, version?: number): this;
|
||||
check(path: string, version?: number): this;
|
||||
remove(path: string, version?: number): this;
|
||||
commit(callback: (error: Error | Exception, results: any) => void): void;
|
||||
}
|
||||
|
||||
interface Client extends EventEmitter {
|
||||
connect(): void;
|
||||
close(): void;
|
||||
create(path: string, callback: (error: Error | Exception, path: string) => void): void;
|
||||
create(path: string, dataOrAclsOrmode1: Buffer | ACL[] | number, callback: (error: Error | Exception, path: string) => void): void;
|
||||
create(path: string, dataOrAclsOrmode1: Buffer | ACL[] | number, dataOrAclsOrmode2: Buffer | ACL[] | number, callback: (error: Error | Exception, path: string) => void): void;
|
||||
create(path: string, dataOrAclsOrmode1: Buffer | ACL[] | number, dataOrAclsOrmode2: Buffer | ACL[] | number, dataOrAclsOrmode3: Buffer | ACL[] | number, callback: (error: Error | Exception, path: string) => void): void;
|
||||
remove(path: string, callback: (error: Error | Exception) => void): void;
|
||||
remove(path: string, version: number, callback: (error: Error | Exception) => void): void;
|
||||
exists(path: string, callback: (error: Error | Exception, stat: Stat) => void): void;
|
||||
exists(path: string, watcher: (event: Event) => void, callback: (error: Error | Exception, stat: Stat) => void): void;
|
||||
getChildren(path: string, callback: (error: Error | Exception, children: string[], stat: Stat) => void): void;
|
||||
getChildren(path: string, watcher: (event: Event) => void, callback: (error: Error | Exception, children: string[], stat: Stat) => void): void;
|
||||
getData(path: string, callback: (error: Error | Exception, data: Buffer, stat: Stat) => void): void;
|
||||
getData(path: string, watcher: (event: Event) => void, callback: (error: Error | Exception, data: Buffer, stat: Stat) => void): void;
|
||||
setData(path: string, data: Buffer | null, callback: (error: Error | Exception, stat: Stat) => void): void;
|
||||
setData(path: string, data: Buffer | null, version: number, callback: (error: Error | Exception, stat: Stat) => void): void;
|
||||
getACL(path: string, callback: (error: Error | Exception, acls: ACL[], stat: Stat) => void): void;
|
||||
setACL(path: string, acls: ACL[], callback: (error: Error | Exception, stat: Stat) => void): void;
|
||||
setACL(path: string, acls: ACL[], version: number, callback: (error: Error | Exception, stat: Stat) => void): void;
|
||||
transaction(): Transaction;
|
||||
mkdirp(path: string, callback: (error: Error | Exception, path: string) => void): void;
|
||||
mkdirp(path: string, dataOrAclsOrmode1: Buffer | ACL[] | number, callback: (error: Error | Exception, path: string) => void): void;
|
||||
mkdirp(path: string, dataOrAclsOrmode1: Buffer | ACL[] | number, dataOrAclsOrmode2: Buffer | ACL[] | number, callback: (error: Error | Exception, path: string) => void): void;
|
||||
mkdirp(path: string, dataOrAclsOrmode1: Buffer | ACL[] | number, dataOrAclsOrmode2: Buffer | ACL[] | number, dataOrAclsOrmode3: Buffer | ACL[] | number, callback: (error: Error | Exception, path: string) => void): void;
|
||||
addAuthInfo(scheme: string, auth: Buffer): void;
|
||||
getState(): State;
|
||||
getSessionId(): Buffer;
|
||||
getSessionPassword(): Buffer;
|
||||
getSessionTimeout(): number;
|
||||
|
||||
on(event: "state", cb: (state: State) => void): this;
|
||||
on(event: "connected" | "connectedReadOnly" | "disconnected" | "expired" | "authenticationFailed" | string, cb: () => void): this;
|
||||
|
||||
once(event: "state", cb: (state: State) => void): this;
|
||||
once(event: "connected" | "connectedReadOnly" | "disconnected" | "expired" | "authenticationFailed" | string, cb: () => void): this;
|
||||
|
||||
addListener(event: "state", cb: (state: State) => void): this;
|
||||
addListener(event: "connected" | "connectedReadOnly" | "disconnected" | "expired" | "authenticationFailed" | string, cb: () => void): this;
|
||||
}
|
||||
|
||||
export interface Option {
|
||||
sessionTimeout: number;
|
||||
spinDelay: number;
|
||||
retries: number;
|
||||
}
|
||||
|
||||
export function createClient(connectionString: string, options?: Partial<Option>): Client;
|
||||
|
||||
export const CreateMode: {
|
||||
PERSISTENT: number,
|
||||
PERSISTENT_SEQUENTIAL: number,
|
||||
EPHEMERAL: number,
|
||||
EPHEMERAL_SEQUENTIAL: number,
|
||||
};
|
||||
|
||||
export class Exception {
|
||||
static OK: number;
|
||||
static SYSTEM_ERROR: number;
|
||||
static RUNTIME_INCONSISTENCY: number;
|
||||
static DATA_INCONSISTENCY: number;
|
||||
static CONNECTION_LOSS: number;
|
||||
static MARSHALLING_ERROR: number;
|
||||
static UNIMPLEMENTED: number;
|
||||
static OPERATION_TIMEOUT: number;
|
||||
static BAD_ARGUMENTS: number;
|
||||
static API_ERROR: number;
|
||||
static NO_NODE: number;
|
||||
static NO_AUTH: number;
|
||||
static BAD_VERSION: number;
|
||||
static NO_CHILDREN_FOR_EPHEMERALS: number;
|
||||
static NODE_EXISTS: number;
|
||||
static NOT_EMPTY: number;
|
||||
static SESSION_EXPIRED: number;
|
||||
static INVALID_CALLBACK: number;
|
||||
static INVALID_ACL: number;
|
||||
static AUTH_FAILED: number;
|
||||
|
||||
code: number;
|
||||
name: string;
|
||||
path: number;
|
||||
constructor(code: number, name: string, path: number);
|
||||
toString(): string;
|
||||
getCode(): number;
|
||||
getName(): string;
|
||||
getPath(): string;
|
||||
}
|
||||
253
node-zookeeper-client/node-zookeeper-client-tests.ts
Normal file
253
node-zookeeper-client/node-zookeeper-client-tests.ts
Normal file
@@ -0,0 +1,253 @@
|
||||
import * as zookeeper from "node-zookeeper-client";
|
||||
|
||||
{
|
||||
const client = zookeeper.createClient('localhost:2181');
|
||||
const path = process.argv[2];
|
||||
|
||||
client.once('connected', function () {
|
||||
console.log('Connected to the server.');
|
||||
|
||||
client.create(path, function (error) {
|
||||
if (error) {
|
||||
console.log('Failed to create node: %s due to: %s.', path, error);
|
||||
} else {
|
||||
console.log('Node: %s is successfully created.', path);
|
||||
}
|
||||
|
||||
client.close();
|
||||
});
|
||||
});
|
||||
|
||||
client.connect();
|
||||
}
|
||||
|
||||
function listChildren(client: zookeeper.Client, path: string) {
|
||||
client.getChildren(
|
||||
path,
|
||||
function (event) {
|
||||
console.log('Got watcher event: %s', event);
|
||||
listChildren(client, path);
|
||||
},
|
||||
function (error, children, stat) {
|
||||
if (error) {
|
||||
console.log(
|
||||
'Failed to list children of %s due to: %s.',
|
||||
path,
|
||||
error
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Children of %s are: %j.', path, children);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
const client = zookeeper.createClient('localhost:2181');
|
||||
const path = process.argv[2];
|
||||
|
||||
client.once('connected', function () {
|
||||
console.log('Connected to ZooKeeper.');
|
||||
listChildren(client, path);
|
||||
});
|
||||
|
||||
client.connect();
|
||||
}
|
||||
|
||||
const client = zookeeper.createClient(
|
||||
'localhost:2181/test',
|
||||
{ sessionTimeout: 10000 }
|
||||
);
|
||||
|
||||
{
|
||||
client.create(
|
||||
'/test/demo',
|
||||
new Buffer('data'),
|
||||
zookeeper.CreateMode.EPHEMERAL,
|
||||
function (error: Error, path) {
|
||||
if (error) {
|
||||
console.log(error.stack);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Node: %s is created.', path);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
client.remove('/test/demo', -1, function (error: Error) {
|
||||
if (error) {
|
||||
console.log(error.stack);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Node is deleted.');
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
client.exists('/test/demo', function (error: Error, stat) {
|
||||
if (error) {
|
||||
console.log(error.stack);
|
||||
return;
|
||||
}
|
||||
|
||||
if (stat) {
|
||||
console.log('Node exists.');
|
||||
} else {
|
||||
console.log('Node does not exist.');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
client.getChildren('/test/demo', function (error: Error, children, stats) {
|
||||
if (error) {
|
||||
console.log(error.stack);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Children are: %j.', children);
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
client.getData(
|
||||
'/test/demo',
|
||||
function (event) {
|
||||
console.log('Got event: %s.', event);
|
||||
},
|
||||
function (error: Error, data, stat) {
|
||||
if (error) {
|
||||
console.log(error.stack);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Got data: %s', data.toString('utf8'));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
client.setData('/test/demo', null, 2, function (error: Error, stat) {
|
||||
if (error) {
|
||||
console.log(error.stack);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Data is set.');
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
client.getACL('/test/demo', function (error: Error, acls, stat) {
|
||||
if (error) {
|
||||
console.log(error.stack);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('ACL(s) are: %j', acls);
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
client.setACL(
|
||||
'/test/demo',
|
||||
[
|
||||
new zookeeper.ACL(
|
||||
zookeeper.Permission.ADMIN,
|
||||
new zookeeper.Id('ip', '127.0.0.1')
|
||||
)
|
||||
],
|
||||
function (error: Error, stat) {
|
||||
if (error) {
|
||||
console.log(error.stack);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('New ACL is set.');
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
client.mkdirp('/test/demo/1/2/3', function (error: Error, path) {
|
||||
if (error) {
|
||||
console.log(error.stack);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Node: %s is created.', path);
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
client.addAuthInfo('ip', new Buffer('127.0.0.1'));
|
||||
}
|
||||
|
||||
{
|
||||
const state = client.getState();
|
||||
console.log('Current state is: %s', state);
|
||||
}
|
||||
|
||||
{
|
||||
const id = client.getSessionId();
|
||||
console.log('Session id is: %s', id.toString('hex'));
|
||||
}
|
||||
|
||||
{
|
||||
client.getSessionPassword();
|
||||
client.getSessionTimeout();
|
||||
|
||||
client.on('connected', function () {
|
||||
console.log('Client state is changed to connected.');
|
||||
});
|
||||
client.on('state', function (state) {
|
||||
if (state === zookeeper.State.SYNC_CONNECTED) {
|
||||
console.log('Client state is changed to connected.');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
client.once('connected', function () {
|
||||
client.transaction().
|
||||
create('/txn').
|
||||
create('/txn/1', new Buffer('transaction')).
|
||||
setData('/txn/1', new Buffer('test'), -1).
|
||||
check('/txn/1').
|
||||
remove('/txn/1', -1).
|
||||
remove('/txn').
|
||||
commit(function (error, results) {
|
||||
if (error) {
|
||||
console.log(
|
||||
'Failed to execute the transaction: %s, results: %j',
|
||||
error,
|
||||
results
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Transaction completed.');
|
||||
client.close();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
client.create('/test/demo', function (error, path) {
|
||||
if (error) {
|
||||
if ((error as zookeeper.Exception).getCode() == zookeeper.Exception.NODE_EXISTS) {
|
||||
console.log('Node exists.');
|
||||
} else {
|
||||
console.log((error as Error).stack);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Node: %s is created.', path);
|
||||
});
|
||||
}
|
||||
22
node-zookeeper-client/tsconfig.json
Normal file
22
node-zookeeper-client/tsconfig.json
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es5"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"node-zookeeper-client-tests.ts"
|
||||
]
|
||||
}
|
||||
1
node-zookeeper-client/tslint.json
Normal file
1
node-zookeeper-client/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "../tslint.json" }
|
||||
Reference in New Issue
Block a user