WIP: initial prototype
- Added error parameter to request and response event - Dropped error event - Updated typescript indentation
This commit is contained in:
9
index.d.ts
vendored
9
index.d.ts
vendored
@ -8,7 +8,7 @@ import ConnectionPool, { nodeSelectorFn, nodeFilterFn } from './lib/ConnectionPo
|
||||
import Serializer from './lib/Serializer';
|
||||
|
||||
declare type anyObject = {
|
||||
[key: string]: any;
|
||||
[key: string]: any;
|
||||
};
|
||||
declare type callbackFn = (err: Error | null, result: ApiResponse) => void;
|
||||
declare type apiMethod = (params?: anyObject | callbackFn, callback?: callbackFn) => any;
|
||||
@ -464,10 +464,9 @@ declare class Client extends EventEmitter {
|
||||
}
|
||||
|
||||
declare const events: {
|
||||
RESPONSE: string;
|
||||
REQUEST: string;
|
||||
ERROR: string;
|
||||
SNIFF: string;
|
||||
RESPONSE: string;
|
||||
REQUEST: string;
|
||||
SNIFF: string;
|
||||
};
|
||||
|
||||
export {
|
||||
|
||||
7
index.js
7
index.js
@ -23,12 +23,6 @@ class Client extends EventEmitter {
|
||||
this.on('error', console.log)
|
||||
}
|
||||
|
||||
// The logging is exposed via events, which the user can
|
||||
// listen to and log the message its preferred way
|
||||
// we add a fake listener to the error event to avoid
|
||||
// the "unhandled error event" error.
|
||||
this.on('error', () => {})
|
||||
|
||||
const options = Object.assign({}, {
|
||||
Connection,
|
||||
ConnectionPool,
|
||||
@ -95,7 +89,6 @@ class Client extends EventEmitter {
|
||||
const events = {
|
||||
RESPONSE: 'response',
|
||||
REQUEST: 'request',
|
||||
ERROR: 'error',
|
||||
SNIFF: 'sniff'
|
||||
}
|
||||
|
||||
|
||||
80
lib/Connection.d.ts
vendored
80
lib/Connection.d.ts
vendored
@ -5,52 +5,52 @@ 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;
|
||||
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;
|
||||
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;
|
||||
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 {};
|
||||
|
||||
218
lib/ConnectionPool.d.ts
vendored
218
lib/ConnectionPool.d.ts
vendored
@ -4,124 +4,124 @@ import { SecureContextOptions } from 'tls';
|
||||
import Connection, { AgentOptions } from './Connection';
|
||||
|
||||
export interface nodeSelectorFn {
|
||||
(connections: Connection[]): Connection;
|
||||
(connections: Connection[]): Connection;
|
||||
}
|
||||
|
||||
export interface nodeFilterFn {
|
||||
(connection: Connection): boolean;
|
||||
(connection: Connection): boolean;
|
||||
}
|
||||
|
||||
interface ConnectionPoolOptions {
|
||||
ssl?: SecureContextOptions;
|
||||
agent?: AgentOptions;
|
||||
pingTimeout?: number;
|
||||
randomizeHost?: boolean;
|
||||
Connection: typeof Connection;
|
||||
resurrectStrategy?: string;
|
||||
nodeFilter?: nodeFilterFn;
|
||||
nodeSelector?: string | nodeSelectorFn;
|
||||
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;
|
||||
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;
|
||||
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;
|
||||
@ -129,9 +129,9 @@ 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;
|
||||
defaultNodeFilter: typeof defaultNodeFilter;
|
||||
roundRobinSelector: typeof roundRobinSelector;
|
||||
randomSelector: typeof randomSelector;
|
||||
};
|
||||
|
||||
export {};
|
||||
|
||||
8
lib/Serializer.d.ts
vendored
8
lib/Serializer.d.ts
vendored
@ -1,6 +1,6 @@
|
||||
export default class Serializer {
|
||||
serialize(object: any): string;
|
||||
deserialize(json: string): any;
|
||||
ndserialize(array: any[]): string;
|
||||
qserialize(object: any): string;
|
||||
serialize(object: any): string;
|
||||
deserialize(json: string): any;
|
||||
ndserialize(array: any[]): string;
|
||||
qserialize(object: any): string;
|
||||
}
|
||||
|
||||
8
lib/Transport.d.ts
vendored
8
lib/Transport.d.ts
vendored
@ -40,10 +40,10 @@ export interface SniffMeta {
|
||||
|
||||
export default class Transport {
|
||||
static sniffReasons: {
|
||||
SNIFF_ON_START: string;
|
||||
SNIFF_INTERVAL: string;
|
||||
SNIFF_ON_CONNECTION_FAULT: string;
|
||||
DEFAULT: string;
|
||||
SNIFF_ON_START: string;
|
||||
SNIFF_INTERVAL: string;
|
||||
SNIFF_ON_CONNECTION_FAULT: string;
|
||||
DEFAULT: string;
|
||||
};
|
||||
emit: emitFn & noopFn;
|
||||
connectionPool: ConnectionPool;
|
||||
|
||||
@ -99,7 +99,7 @@ class Transport {
|
||||
params.timeout = toMs(params.requestTimeout || this.requestTimeout)
|
||||
|
||||
meta.request = params
|
||||
this.emit('request', meta)
|
||||
this.emit('request', null, meta)
|
||||
|
||||
// perform the actual http request
|
||||
return meta.connection.request(params, onResponse)
|
||||
@ -127,7 +127,7 @@ class Transport {
|
||||
? err
|
||||
: new ConnectionError(err.message, params)
|
||||
|
||||
this.emit('error', error, meta)
|
||||
this.emit('response', error, meta)
|
||||
return callback(error, result)
|
||||
}
|
||||
|
||||
@ -142,7 +142,7 @@ class Transport {
|
||||
if (params.asStream === true) {
|
||||
result.body = response
|
||||
meta.response = result
|
||||
this.emit('response', meta)
|
||||
this.emit('response', null, meta)
|
||||
callback(null, result)
|
||||
return
|
||||
}
|
||||
@ -151,7 +151,11 @@ class Transport {
|
||||
// collect the payload
|
||||
response.setEncoding('utf8')
|
||||
response.on('data', chunk => { payload += chunk })
|
||||
response.on('error', err => callback(new ConnectionError(err.message, params), result))
|
||||
response.on('error', err => {
|
||||
const error = new ConnectionError(err.message, params)
|
||||
this.emit('response', error, meta)
|
||||
callback(error, result)
|
||||
})
|
||||
response.on('end', () => {
|
||||
const isHead = params.method === 'HEAD'
|
||||
// we should attempt the payload deserialization only if:
|
||||
@ -166,7 +170,7 @@ class Transport {
|
||||
try {
|
||||
result.body = this.serializer.deserialize(payload)
|
||||
} catch (err) {
|
||||
this.emit('error', err, meta)
|
||||
this.emit('response', err, meta)
|
||||
return callback(err, result)
|
||||
}
|
||||
} else {
|
||||
@ -198,14 +202,16 @@ class Transport {
|
||||
}
|
||||
|
||||
meta.response = result
|
||||
this.emit('response', meta)
|
||||
if (ignoreStatusCode === false && statusCode >= 400) {
|
||||
callback(new ResponseError(result), result)
|
||||
const error = new ResponseError(result)
|
||||
this.emit('response', error, meta)
|
||||
callback(error, result)
|
||||
} else {
|
||||
// cast to boolean if the request method was HEAD
|
||||
if (isHead === true && statusCode === 404) {
|
||||
result.body = false
|
||||
}
|
||||
this.emit('response', null, meta)
|
||||
callback(null, result)
|
||||
}
|
||||
})
|
||||
|
||||
56
lib/errors.d.ts
vendored
56
lib/errors.d.ts
vendored
@ -1,48 +1,48 @@
|
||||
export declare class TimeoutError extends Error {
|
||||
name: string;
|
||||
message: string;
|
||||
request: any;
|
||||
constructor(message: string, request: any);
|
||||
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);
|
||||
name: string;
|
||||
message: string;
|
||||
request: any;
|
||||
constructor(message: string, request: any);
|
||||
}
|
||||
|
||||
export declare class NoLivingConnectionsError extends Error {
|
||||
name: string;
|
||||
message: string;
|
||||
constructor(message: string);
|
||||
name: string;
|
||||
message: string;
|
||||
constructor(message: string);
|
||||
}
|
||||
|
||||
export declare class SerializationError extends Error {
|
||||
name: string;
|
||||
message: string;
|
||||
constructor(message: string);
|
||||
name: string;
|
||||
message: string;
|
||||
constructor(message: string);
|
||||
}
|
||||
|
||||
export declare class DeserializationError extends Error {
|
||||
name: string;
|
||||
message: string;
|
||||
constructor(message: string);
|
||||
name: string;
|
||||
message: string;
|
||||
constructor(message: string);
|
||||
}
|
||||
|
||||
export declare class ConfigurationError extends Error {
|
||||
name: string;
|
||||
message: string;
|
||||
constructor(message: string);
|
||||
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;
|
||||
});
|
||||
name: string;
|
||||
message: string;
|
||||
body: any;
|
||||
statusCode: number;
|
||||
headers: any;
|
||||
constructor({ body, statusCode, headers }: {
|
||||
[key: string]: any;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user