@haechi-labs/henesis-provider
Advanced tools
Comparing version 1.0.0-beta.1 to 1.0.0-beta.4
@@ -6,2 +6,14 @@ # Change Log | ||
# [1.0.0-beta.4](https://github.com/HAECHI-LABS/henesis-sdk-js/compare/v1.0.0-beta.3...v1.0.0-beta.4) (2019-10-04) | ||
### Features | ||
* add authentication token and bootstrap transaction tracker ([6fc4b20](https://github.com/HAECHI-LABS/henesis-sdk-js/commit/6fc4b20)) | ||
* add heartbeat logic at provider [#29](https://github.com/HAECHI-LABS/henesis-sdk-js/issues/29) ([6797ad1](https://github.com/HAECHI-LABS/henesis-sdk-js/commit/6797ad1)) | ||
# 1.0.0-beta.1 (2019-09-03) | ||
@@ -8,0 +20,0 @@ |
import ProvidersFactory from './factories/ProvidersFactory'; | ||
import JsonRpcMapper from './mappers/JsonRpcMapper'; | ||
import WebsocketProvider from './providers/WebsocketProvider'; | ||
import JsonRpcResponseValidator from './validators/JsonRpcResponseValidator'; | ||
import { Provider, JsonRpcRequest, SubscriptionResult, JsonRpcResponse, Subscription, WebsocketOption } from './types'; | ||
export { ProvidersFactory, JsonRpcMapper, WebsocketProvider, JsonRpcResponseValidator, Provider, JsonRpcRequest, SubscriptionResult, JsonRpcResponse, Subscription, WebsocketOption }; | ||
import { Provider, JsonRpcRequest, JsonRpcResponse, Subscription, WebsocketOption } from './types'; | ||
export { ProvidersFactory, Provider, JsonRpcRequest, JsonRpcResponse, Subscription, WebsocketOption, WebsocketProvider }; |
@@ -5,8 +5,4 @@ "use strict"; | ||
exports.ProvidersFactory = ProvidersFactory_1.default; | ||
const JsonRpcMapper_1 = require("./mappers/JsonRpcMapper"); | ||
exports.JsonRpcMapper = JsonRpcMapper_1.default; | ||
const WebsocketProvider_1 = require("./providers/WebsocketProvider"); | ||
exports.WebsocketProvider = WebsocketProvider_1.default; | ||
const JsonRpcResponseValidator_1 = require("./validators/JsonRpcResponseValidator"); | ||
exports.JsonRpcResponseValidator = JsonRpcResponseValidator_1.default; | ||
//# sourceMappingURL=index.js.map |
@@ -13,2 +13,5 @@ import { w3cwebsocket as W3CWebsocket } from "websocket"; | ||
private readonly subscriptions; | ||
private readonly HEARTBEAT; | ||
private readonly KEEP_ALIVE_INTERVAL; | ||
private keepAliveHandle; | ||
constructor(connection: W3CWebsocket, timeout: number); | ||
@@ -19,3 +22,5 @@ getUrl(): string; | ||
send(method: string, parameters: any[]): Promise<any>; | ||
sendPayload(payload: any): Promise<JsonRpcResponse>; | ||
sendAsync(method: string, parameters: any[]): Promise<void>; | ||
sendPayload(payload: any, waitResponse: boolean): Promise<JsonRpcResponse>; | ||
private _keepAlive; | ||
onConnect(e: any): Promise<void>; | ||
@@ -22,0 +27,0 @@ onMessage(e: any): void; |
@@ -14,2 +14,4 @@ "use strict"; | ||
this.isReconnecting = true; | ||
this.HEARTBEAT = 'HEARTBEAT'; | ||
this.KEEP_ALIVE_INTERVAL = 30 * 1000; | ||
this.connection = connection; | ||
@@ -29,8 +31,3 @@ this.timeout = timeout; | ||
parameters.unshift(subscriptionId, subscriptionMethod); | ||
try { | ||
await this.send(subscribeMethod, parameters); | ||
} | ||
catch (error) { | ||
throw new Error(`Provider error: ${error}`); | ||
} | ||
await this.send(subscribeMethod, parameters); | ||
this.subscriptions.set(subscriptionId, { | ||
@@ -44,3 +41,3 @@ id: subscriptionId, | ||
async send(method, parameters) { | ||
const response = await this.sendPayload(JsonRpcMapper_1.default.toPayload(method, parameters)); | ||
const response = await this.sendPayload(JsonRpcMapper_1.default.toPayload(method, parameters), true); | ||
const validationResult = JsonRpcResponseValidator_1.default.validate(response); | ||
@@ -52,7 +49,10 @@ if (validationResult instanceof Error) { | ||
} | ||
sendPayload(payload) { | ||
async sendAsync(method, parameters) { | ||
const payload = JsonRpcMapper_1.default.toPayload(method, parameters); | ||
await this.sendPayload(payload, false); | ||
} | ||
async sendPayload(payload, waitResponse) { | ||
return new Promise((resolve, reject) => { | ||
this.once('error', reject); | ||
if (!this.isConnecting()) { | ||
let timeout, id; | ||
if (this.connection.readyState !== this.connection.OPEN) { | ||
@@ -69,24 +69,32 @@ this.removeListener('error', reject); | ||
} | ||
if (this.timeout) { | ||
timeout = setTimeout(() => { | ||
reject(new Error('Connection error: Timeout exceeded')); | ||
}, this.timeout); | ||
if (waitResponse) { | ||
let timeout, id; | ||
if (this.timeout) { | ||
timeout = setTimeout(() => { | ||
this.removeListener('error', reject); | ||
this.removeAllListeners(id); | ||
reject(new Error('Connection error: Timeout exceeded')); | ||
}, this.timeout); | ||
} | ||
if (lodash_1.isArray(payload)) { | ||
id = payload[0].id; | ||
} | ||
else { | ||
id = payload.id; | ||
} | ||
this.once(id, (response) => { | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
} | ||
this.removeListener('error', reject); | ||
return resolve(response); | ||
}); | ||
} | ||
if (lodash_1.isArray(payload)) { | ||
id = payload[0].id; | ||
} | ||
else { | ||
id = payload.id; | ||
this.removeListener('error', reject); | ||
} | ||
this.once(id, (response) => { | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
} | ||
this.removeListener('error', reject); | ||
return resolve(response); | ||
}); | ||
return; | ||
} | ||
this.once('connect', () => { | ||
this.sendPayload(payload) | ||
this.sendPayload(payload, waitResponse) | ||
.then((response) => { | ||
@@ -103,2 +111,8 @@ this.removeListener('error', reject); | ||
} | ||
_keepAlive() { | ||
this.sendAsync(this.HEARTBEAT, []) | ||
.catch((err) => { | ||
this.emit('error', err); | ||
}); | ||
} | ||
async onConnect(e) { | ||
@@ -113,2 +127,7 @@ for (const key of this.subscriptions.keys()) { | ||
} | ||
if (this.keepAliveHandle) { | ||
clearInterval(this.keepAliveHandle); | ||
} | ||
this.keepAliveHandle = setInterval(() => this._keepAlive(), this.KEEP_ALIVE_INTERVAL); | ||
this.keepAliveHandle.unref(); | ||
this.emit(this.CONNECT, e); | ||
@@ -141,2 +160,4 @@ } | ||
onClose(e) { | ||
clearInterval(this.keepAliveHandle); | ||
this.keepAliveHandle = null; | ||
if (!this.isReconnecting) { | ||
@@ -143,0 +164,0 @@ this.emit(this.CLOSE, e); |
@@ -8,7 +8,2 @@ import { IClientConfig } from "websocket"; | ||
} | ||
export interface SubscriptionResult { | ||
ackId: string; | ||
messageId: string; | ||
payload: any; | ||
} | ||
export interface JsonRpcResponse { | ||
@@ -24,2 +19,3 @@ jsonrpc: string; | ||
send(method: string, parameters: any[]): Promise<any>; | ||
sendAsync(method: string, parameters: any[]): Promise<void>; | ||
subscribe(subscriptionId: string, subscribeMethod: string, subscriptionMethod: string, parameters: any[]): Promise<string>; | ||
@@ -31,2 +27,3 @@ unsubscribe(subscriptionId: string, unsubscribeMethod: string): Promise<any>; | ||
supportsSubscriptions(): boolean; | ||
once(type: string, callback: Function): void; | ||
on(type: string, callback: Function): void; | ||
@@ -33,0 +30,0 @@ on(type: 'connect', callback: Function): void; |
{ | ||
"name": "@haechi-labs/henesis-provider", | ||
"version": "1.0.0-beta.1", | ||
"version": "1.0.0-beta.4", | ||
"description": "henesis-provider", | ||
@@ -12,3 +12,3 @@ "main": "./lib/index.js", | ||
"author": "Haechi labs Team", | ||
"license": "ISC", | ||
"license": "LGPL3", | ||
"dependencies": { | ||
@@ -43,3 +43,3 @@ "eventemitter3": "^4.0.0", | ||
}, | ||
"gitHead": "0c272fd48333f581f5a815d8a854e6565d39c03a" | ||
"gitHead": "ac8ddda658ad548a086818bd8541d280f97a7179" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37049
21
2
70
411