diff --git a/types/hashring/hashring-tests.ts b/types/hashring/hashring-tests.ts new file mode 100644 index 0000000000..cbea3b5eaa --- /dev/null +++ b/types/hashring/hashring-tests.ts @@ -0,0 +1,16 @@ +import * as HashRing from "hashring"; + +const ring = new HashRing([ + '127.0.0.1', + '127.0.0.2', + '127.0.0.3', + '127.0.0.4' +], 'md5', { + 'max cache size': 10000 +}); + +ring.get('foo bar banana'); + +ring.range('foo bar banana', 2); + +ring.add('127.0.0.7').remove('127.0.0.1'); diff --git a/types/hashring/index.d.ts b/types/hashring/index.d.ts new file mode 100644 index 0000000000..96c7cd9dd9 --- /dev/null +++ b/types/hashring/index.d.ts @@ -0,0 +1,133 @@ +// Type definitions for hashring 3.2 +// Project: https://github.com/3rd-Eden/node-hashring +// Definitions by: superzheng +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +declare namespace HashRing { + interface HashRingOptions { + /** + * The amount of virtual nodes per server, + * defaults to 40 as this generates 160 points per server + * as used by ketama hashing. + */ + "vnode count": number; + + /** + * The default port number which will removed from the server address + * to provide ketama compatibility. + */ + "default port": number; + + /** + * Allows you to force a compatibility mode of the HashRing. + * It default to ketama hash rings but if you are coming from + * a python world you might want compatibility with the hash_ring module. + * + * There's a small diff between + * `hash_ring` and `ketama` and that's the amount of replica's of a server. + * `Ketama` uses 4 and `hash_ring` uses 3. + * + * Set this to `hash_ring` if you want to use 3. + */ + "compatibility": "hash_ring" | "ketama"; + + /** + * The amount of replicas per server. + * Defaults to 4. + */ + "replicas": number; + + /** + * We use a simple LRU cache + * inside the module to speed up frequent key lookups, + * you can customize the amount of keys that need to be cached. + * + * It defaults to 5000. + */ + "max cache size": number; + } + interface ServerConfig { + vnodes: number; + weight: number; + } + type Servers = string | + ReadonlyArray | + Record>; +} + +declare class HashRing { + constructor(servers: HashRing.Servers, + algorithm?: string, options?: Partial); + + /** + * Generates the continuum of server a.k.a as the Hash Ring + * based on their weights and virtual nodes assigned. + */ + continuum(): this; + + /** + * Find the correct node for which the key is closest to the point + * after what the given key hashes to. + * @param key Random key that needs to be searched in the hash ring + * @returns The matching server address + */ + get(key: string): string; + + /** + * Returns a range of servers. Could be useful for replication. + * @param key Random key that needs to be searched in the hash ring + * @param size Amount items to be returned (maximum). + * Defaults to the amount of servers that are in the hashring. + * @param unique Don't return duplicate servers. Defaults to true. + */ + range(key: string, size?: number, unique?: boolean): string[]; + + /** + * Hotswap identical servers with each other. + * This doesn't require the cache to be completely nuked and + * the hash ring distribution to be re-calculated. + * @param from The server that needs to be replaced + * @param to The server that replaces the server + */ + swap(from: string, to: string): this; + + /** + * Add a new server to ring without having to re-initialize the hashring. + * It accepts the same arguments as you can use in the constructor. + * @param servers Servers that need to be added to the ring. + */ + add(servers: HashRing.Servers): this; + + /** + * Remove a server from the hash ring. + * @param server Server that need to be removed from the ring. + */ + remove(server: HashRing.Servers): this; + + /** + * Checks if a given server exists in the hash ring. + * @param server Server for whose existence we're checking. + * @returns Indication if we have that server. + */ + has(server: string): boolean; + + /** + * Reset the HashRing and clean up it's references. + */ + reset(): this; + + /** + * Resets the HashRing and closes the ring. + */ + end(): this; + + /** + * Returns the points per server. + * @param servers server Optional server to filter down. + * @returns server -> Array(points). + */ + points(servers: HashRing.Servers): Array>; +} + +export = HashRing; diff --git a/types/hashring/tsconfig.json b/types/hashring/tsconfig.json new file mode 100644 index 0000000000..a11063db91 --- /dev/null +++ b/types/hashring/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", + "hashring-tests.ts" + ] +} diff --git a/types/hashring/tslint.json b/types/hashring/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/hashring/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }