Typings support (#737)

* Updated scripts

* Updated .gitignore

* Removed symbols

* Fixed typo

* Added typings

* Updated test

* Added typings test
This commit is contained in:
Tomas Della Vedova
2018-11-30 17:01:55 +01:00
committed by GitHub
parent db73802af9
commit ab1d7ba992
16 changed files with 869 additions and 45 deletions

56
lib/Connection.d.ts vendored Normal file
View File

@ -0,0 +1,56 @@
/// <reference types="node" />
import { URL } from 'url';
import * as http from 'http';
import { SecureContextOptions } from 'tls';
interface ConnectionOptions {
url: URL;
ssl?: SecureContextOptions;
id?: string;
headers?: any;
agent?: AgentOptions;
status?: string;
roles?: any;
}
export interface AgentOptions {
keepAlive: boolean;
keepAliveMsecs: number;
maxSockets: number;
maxFreeSockets: number;
}
export default class Connection {
static statuses: {
ALIVE: string;
DEAD: string;
};
static roles: {
MASTER: string;
DATA: string;
INGEST: string;
COORDINATING: string;
MACHINE_LEARNING: string;
};
url: URL;
ssl: SecureContextOptions | null;
id: string;
headers: any;
deadCount: number;
resurrectTimeout: number;
statuses: any;
roles: any;
makeRequest: any;
_openRequests: number;
_status: string;
_agent: http.Agent;
constructor(opts?: ConnectionOptions);
request(params: any, callback: (err: Error | null, response: http.IncomingMessage | null) => void): http.ClientRequest;
close(): Connection;
setRole(role: string, enabled: boolean): Connection;
status: string;
buildRequestObject(params: any): http.ClientRequestArgs;
}
export {};

137
lib/ConnectionPool.d.ts vendored Normal file
View File

@ -0,0 +1,137 @@
/// <reference types="node" />
import { SecureContextOptions } from 'tls';
import Connection, { AgentOptions } from './Connection';
export interface nodeSelectorFn {
(connections: Connection[]): Connection;
}
export interface nodeFilterFn {
(connection: Connection): boolean;
}
interface ConnectionPoolOptions {
ssl?: SecureContextOptions;
agent?: AgentOptions;
pingTimeout?: number;
randomizeHost?: boolean;
Connection: typeof Connection;
resurrectStrategy?: string;
nodeFilter?: nodeFilterFn;
nodeSelector?: string | nodeSelectorFn;
}
export interface getConnectionOptions {
filter?: nodeFilterFn;
selector?: nodeSelectorFn;
}
export default class ConnectionPool {
static resurrectStrategies: {
none: number;
ping: number;
optimistic: number;
};
connections: any;
dead: string[];
_ssl: SecureContextOptions | null;
_agent: AgentOptions | null;
resurrectTimeout: number;
resurrectTimeoutCutoff: number;
pingTimeout: number;
randomizeHost: boolean;
nodeFilter: nodeFilterFn;
nodeSelector: nodeSelectorFn;
Connection: typeof Connection;
resurrectStrategy: number;
constructor(opts?: ConnectionPoolOptions);
/**
* Marks a connection as 'alive'.
* If needed removes the connection from the dead list
* and then resets the `deadCount`.
*
* @param {object} connection
*/
markAlive(connection: Connection): void;
/**
* Marks a connection as 'dead'.
* If needed adds the connection to the dead list
* and then increments the `deadCount`.
*
* @param {object} connection
*/
markDead(connection: Connection): void;
/**
* If enabled, tries to resurrect a connection with the given
* resurrect strategy ('ping', 'optimistic', 'none').
*
* @param {number} epoch
* @param {function} callback (isAlive, connection)
*/
resurrect(now?: number, callback?: (isAlive: boolean | null, connection: Connection | null) => void): void;
/**
* Returns an alive connection if present,
* otherwise returns null.
* By default it filters the `master` only nodes.
* It uses the selector to choose which
* connection return.
*
* @param {object} options (filter and selector)
* @returns {object|null} connection
*/
getConnection(opts?: getConnectionOptions): Connection | null;
/**
* Adds a new connection to the pool.
*
* @param {object|string} host
* @returns {ConnectionPool}
*/
addConnection(opts: any): Connection | void;
/**
* Removes a new connection to the pool.
*
* @param {object} connection
* @returns {ConnectionPool}
*/
removeConnection(connection: Connection): ConnectionPool;
/**
* Empties the connection pool.
*
* @returns {ConnectionPool}
*/
empty(): ConnectionPool;
/**
* Update the ConnectionPool with new connections.
*
* @param {array} array of connections
* @returns {ConnectionPool}
*/
update(connections: Connection[]): ConnectionPool;
/**
* Transforms the nodes objects to a host object.
*
* @param {object} nodes
* @returns {array} hosts
*/
nodesToHost(nodes: any): any[];
/**
* Transforms an url string to a host object
*
* @param {string} url
* @returns {object} host
*/
urlToHost(url: string): any;
}
declare function defaultNodeFilter(node: Connection): boolean;
declare function roundRobinSelector(): (connections: Connection[]) => Connection;
declare function randomSelector(connections: Connection[]): Connection;
export declare const internals: {
defaultNodeFilter: typeof defaultNodeFilter;
roundRobinSelector: typeof roundRobinSelector;
randomSelector: typeof randomSelector;
};
export {};

View File

@ -330,9 +330,9 @@ ConnectionPool.resurrectStrategies = {
function defaultNodeFilter (node) {
// avoid master only nodes
if (!!node.master === true &&
!!node.data === false &&
!!node.ingest === false) {
if (!!node.roles.master === true &&
!!node.roles.data === false &&
!!node.roles.ingest === false) {
return false
}
return true

6
lib/Serializer.d.ts vendored Normal file
View File

@ -0,0 +1,6 @@
export default class Serializer {
serialize(object: any): string;
deserialize(json: string): any;
ndserialize(array: any[]): string;
qserialize(object: any): string;
}

47
lib/Transport.d.ts vendored Normal file
View File

@ -0,0 +1,47 @@
import ConnectionPool from './ConnectionPool';
import Connection from './Connection';
import Serializer from './Serializer';
declare type noopFn = (...args: any[]) => void;
declare type emitFn = (event: string | symbol, ...args: any[]) => boolean;
interface TransportOptions {
emit: emitFn & noopFn;
connectionPool: ConnectionPool;
serializer: Serializer;
maxRetries: number;
requestTimeout: number | string;
suggestCompression: boolean;
sniffInterval: number;
sniffOnConnectionFault: boolean;
sniffEndpoint: string;
sniffOnStart: boolean;
}
export interface ApiResponse {
body: any;
statusCode: number | null;
headers: any;
warnings: any[] | null;
}
export default class Transport {
emit: emitFn & noopFn;
connectionPool: ConnectionPool;
serializer: Serializer;
maxRetries: number;
requestTimeout: number;
suggestCompression: boolean;
sniffInterval: number;
sniffOnConnectionFault: boolean;
sniffEndpoint: string;
_sniffEnabled: boolean;
_nextSniff: number;
_isSniffing: boolean;
constructor(opts: TransportOptions);
request(params: any, callback: (err: Error | null, result: ApiResponse) => void): any;
getConnection(): Connection | null;
sniff(callback?: (...args: any[]) => void): void;
}
export {};

48
lib/errors.d.ts vendored Normal file
View File

@ -0,0 +1,48 @@
export declare class TimeoutError extends Error {
name: string;
message: string;
request: any;
constructor(message: string, request: any);
}
export declare class ConnectionError extends Error {
name: string;
message: string;
request: any;
constructor(message: string, request: any);
}
export declare class NoLivingConnectionsError extends Error {
name: string;
message: string;
constructor(message: string);
}
export declare class SerializationError extends Error {
name: string;
message: string;
constructor(message: string);
}
export declare class DeserializationError extends Error {
name: string;
message: string;
constructor(message: string);
}
export declare class ConfigurationError extends Error {
name: string;
message: string;
constructor(message: string);
}
export declare class ResponseError extends Error {
name: string;
message: string;
body: any;
statusCode: number;
headers: any;
constructor({ body, statusCode, headers }: {
[key: string]: any;
});
}

View File

@ -1,15 +0,0 @@
'use strict'
const kTransport = Symbol('elasticsearch-transport')
const kConnection = Symbol('elasticsearch-connection')
const kConnectionPool = Symbol('elasticsearch-connection-pool')
const kSerializer = Symbol('elasticsearch-serializer')
const kSelector = Symbol('elasticsearch-selector')
module.exports = {
kTransport,
kConnection,
kConnectionPool,
kSerializer,
kSelector
}