@liskhq/lisk-api-client
Advanced tools
Comparing version 5.0.0 to 5.0.1
@@ -7,2 +7,3 @@ "use strict"; | ||
const events_1 = require("events"); | ||
const utils_1 = require("./utils"); | ||
const CONNECTION_TIME_OUT = 2000; | ||
@@ -94,7 +95,7 @@ const getSocketsPath = (dataPath) => { | ||
if (err) { | ||
reject(err); | ||
reject(utils_1.convertRPCError(err)); | ||
return; | ||
} | ||
if (data.error) { | ||
reject(err); | ||
reject(utils_1.convertRPCError(data.error)); | ||
return; | ||
@@ -101,0 +102,0 @@ } |
@@ -9,2 +9,7 @@ /// <reference types="node" /> | ||
} | ||
export interface JSONRPCError { | ||
code: number; | ||
message: string; | ||
data?: string | number | boolean | Record<string, unknown>; | ||
} | ||
export interface JSONRPCResponse<T> { | ||
@@ -15,7 +20,3 @@ readonly id: number; | ||
readonly params: never; | ||
readonly error?: { | ||
code: number; | ||
message: string; | ||
data?: string; | ||
}; | ||
readonly error?: JSONRPCError; | ||
readonly result?: T; | ||
@@ -22,0 +23,0 @@ } |
@@ -14,3 +14,5 @@ import { EventCallback } from './types'; | ||
subscribe<T = Record<string, unknown>>(eventName: string, cb: EventCallback<T>): void; | ||
private _handleClose; | ||
private _handlePing; | ||
private _handleMessage; | ||
} |
@@ -5,4 +5,4 @@ "use strict"; | ||
const events_1 = require("events"); | ||
const utils_1 = require("./utils"); | ||
const CONNECTION_TIMEOUT = 2000; | ||
const ACKNOWLEDGMENT_TIMEOUT = 2000; | ||
const RESPONSE_TIMEOUT = 3000; | ||
@@ -35,27 +35,36 @@ const timeout = async (ms, message) => new Promise((_, reject) => { | ||
this._ws = new WebSocket(this._url); | ||
const connect = new Promise(resolve => { | ||
this._ws.onclose = this._handleClose.bind(this); | ||
this._ws.onmessage = this._handleMessage.bind(this); | ||
this._ws.addEventListener('ping', this._handlePing.bind(this)); | ||
const connectHandler = new Promise(resolve => { | ||
var _a; | ||
(_a = this._ws) === null || _a === void 0 ? void 0 : _a.on('open', () => { | ||
const onOpen = () => { | ||
var _a; | ||
this.isAlive = true; | ||
(_a = this._ws) === null || _a === void 0 ? void 0 : _a.removeEventListener('open', onOpen); | ||
resolve(); | ||
}); | ||
}; | ||
(_a = this._ws) === null || _a === void 0 ? void 0 : _a.addEventListener('open', onOpen); | ||
}); | ||
const error = new Promise((_, reject) => { | ||
const errorHandler = new Promise((_, reject) => { | ||
var _a; | ||
(_a = this._ws) === null || _a === void 0 ? void 0 : _a.on('error', err => { | ||
const onError = (error) => { | ||
var _a; | ||
this.isAlive = false; | ||
reject(err); | ||
}); | ||
(_a = this._ws) === null || _a === void 0 ? void 0 : _a.removeEventListener('error', onError); | ||
reject(error.error); | ||
}; | ||
(_a = this._ws) === null || _a === void 0 ? void 0 : _a.addEventListener('error', onError); | ||
}); | ||
await Promise.race([ | ||
connect, | ||
error, | ||
timeout(CONNECTION_TIMEOUT, `Could not connect in ${CONNECTION_TIMEOUT}ms`), | ||
]); | ||
this._ws.on('ping', () => { | ||
this.isAlive = true; | ||
}); | ||
this._ws.on('message', data => { | ||
this._handleMessage(data); | ||
}); | ||
try { | ||
await Promise.race([ | ||
connectHandler, | ||
errorHandler, | ||
timeout(CONNECTION_TIMEOUT, `Could not connect in ${CONNECTION_TIMEOUT}ms`), | ||
]); | ||
} | ||
catch (err) { | ||
this._ws.close(); | ||
throw err; | ||
} | ||
} | ||
@@ -66,17 +75,30 @@ async disconnect() { | ||
if (!this._ws) { | ||
return Promise.resolve(); | ||
return; | ||
} | ||
return new Promise(resolve => { | ||
var _a, _b; | ||
(_a = this._ws) === null || _a === void 0 ? void 0 : _a.on('close', () => { | ||
if (this._ws.readyState === WebSocket.CLOSED) { | ||
this.isAlive = false; | ||
this._ws = undefined; | ||
return; | ||
} | ||
const closeHandler = new Promise(resolve => { | ||
var _a; | ||
const onClose = () => { | ||
var _a; | ||
(_a = this._ws) === null || _a === void 0 ? void 0 : _a.terminate(); | ||
this.isAlive = false; | ||
this._ws = undefined; | ||
(_a = this._ws) === null || _a === void 0 ? void 0 : _a.removeEventListener('close', onClose); | ||
resolve(); | ||
}); | ||
(_b = this._ws) === null || _b === void 0 ? void 0 : _b.close(); | ||
}; | ||
(_a = this._ws) === null || _a === void 0 ? void 0 : _a.addEventListener('close', onClose); | ||
}); | ||
this._ws.close(); | ||
await Promise.race([ | ||
closeHandler, | ||
timeout(CONNECTION_TIMEOUT, `Could not disconnect in ${CONNECTION_TIMEOUT}ms`), | ||
]); | ||
} | ||
async invoke(actionName, params) { | ||
var _a; | ||
if (!this.isAlive) { | ||
throw new Error('Websocket client is not connected.'); | ||
} | ||
const request = { | ||
@@ -88,15 +110,3 @@ jsonrpc: '2.0', | ||
}; | ||
const send = new Promise((resolve, reject) => { | ||
var _a; | ||
(_a = this._ws) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify(request), (err) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(); | ||
}); | ||
}); | ||
await Promise.race([ | ||
send, | ||
timeout(ACKNOWLEDGMENT_TIMEOUT, `Request is not acknowledged in ${ACKNOWLEDGMENT_TIMEOUT}ms`), | ||
]); | ||
(_a = this._ws) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify(request)); | ||
const response = defer(); | ||
@@ -113,5 +123,10 @@ this._pendingRequests[this._requestCounter] = response; | ||
} | ||
_handleMessage(message) { | ||
var _a; | ||
const res = JSON.parse(message); | ||
_handleClose() { | ||
this.isAlive = false; | ||
} | ||
_handlePing() { | ||
this.isAlive = true; | ||
} | ||
_handleMessage(event) { | ||
const res = JSON.parse(event.data); | ||
if (messageIsNotification(res)) { | ||
@@ -124,3 +139,3 @@ this._emitter.emit(res.method, res.params); | ||
if (res.error) { | ||
this._pendingRequests[id].reject(new Error((_a = res.error.data) !== null && _a !== void 0 ? _a : res.error.data)); | ||
this._pendingRequests[id].reject(utils_1.convertRPCError(res.error)); | ||
} | ||
@@ -127,0 +142,0 @@ else { |
{ | ||
"name": "@liskhq/lisk-api-client", | ||
"version": "5.0.0", | ||
"version": "5.0.1", | ||
"description": "An API client for the Lisk network", | ||
@@ -68,3 +68,4 @@ "author": "Lisk Foundation <admin@lisk.io>, lightcurve GmbH <admin@lightcurve.io>", | ||
"typescript": "3.8.3" | ||
} | ||
}, | ||
"gitHead": "1e2b3305d847ade159fa4eb8c5bdd26aec1fa09a" | ||
} |
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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
84009
41
1115
0