mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-04 21:15:47 +00:00
Add a 2-second timeout for connect()
This commit is contained in:
@@ -8,7 +8,7 @@ import {RippledError, DisconnectedError, NotConnectedError,
|
||||
RippledNotInitializedError} from './errors'
|
||||
|
||||
export interface ConnectionOptions {
|
||||
trace?: boolean,
|
||||
trace?: boolean
|
||||
proxy?: string
|
||||
proxyAuthorization?: string
|
||||
authorization?: string
|
||||
@@ -16,7 +16,8 @@ export interface ConnectionOptions {
|
||||
key?: string
|
||||
passphrase?: string
|
||||
certificate?: string
|
||||
timeout?: number
|
||||
timeout?: number,
|
||||
connectionTimeout?: number
|
||||
}
|
||||
|
||||
class Connection extends EventEmitter {
|
||||
@@ -43,6 +44,7 @@ class Connection extends EventEmitter {
|
||||
private _onUnexpectedCloseBound: null|((...args: any[]) => void) = null
|
||||
private _fee_base: null|number = null
|
||||
private _fee_ref: null|number = null
|
||||
private _connectionTimeout: number
|
||||
|
||||
constructor(url, options: ConnectionOptions = {}) {
|
||||
super()
|
||||
@@ -61,6 +63,7 @@ class Connection extends EventEmitter {
|
||||
this._passphrase = options.passphrase
|
||||
this._certificate = options.certificate
|
||||
this._timeout = options.timeout || (20 * 1000)
|
||||
this._connectionTimeout = options.connectionTimeout || 2000
|
||||
}
|
||||
|
||||
_updateLedgerVersions(data) {
|
||||
@@ -283,7 +286,14 @@ class Connection extends EventEmitter {
|
||||
|
||||
connect(): Promise<void> {
|
||||
this._clearReconnectTimer()
|
||||
return new Promise((resolve, reject) => {
|
||||
let connectFinished = false
|
||||
const promise = new Promise<void>((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
if (!connectFinished) {
|
||||
reject(`Error: connect() timed out after ${this._connectionTimeout} ms. ` +
|
||||
`If your internet connection is working, the rippled server may be blocked or inaccessible.`)
|
||||
}
|
||||
}, this._connectionTimeout)
|
||||
if (!this._url) {
|
||||
reject(new ConnectionError(
|
||||
'Cannot connect because no server was specified'))
|
||||
@@ -311,9 +321,22 @@ class Connection extends EventEmitter {
|
||||
this._onUnexpectedCloseBound = this._onUnexpectedClose.bind(this, true,
|
||||
resolve, reject)
|
||||
this._ws.once('close', this._onUnexpectedCloseBound)
|
||||
this._ws.once('open', () => this._onOpen().then(resolve, reject))
|
||||
this._ws.once('open', () => {
|
||||
if (connectFinished) {
|
||||
this._ws.close()
|
||||
} else {
|
||||
connectFinished = true
|
||||
return this._onOpen().then(resolve, reject)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
promise.then(() => {
|
||||
connectFinished = true
|
||||
}, () => {
|
||||
connectFinished = true
|
||||
})
|
||||
return promise
|
||||
}
|
||||
|
||||
disconnect(): Promise<void> {
|
||||
|
||||
@@ -4771,4 +4771,8 @@ describe('RippleAPI - offline', function () {
|
||||
assert.throws(() => new RippleAPI({ server: 'wss//s:1' }));
|
||||
});
|
||||
|
||||
xit('RippleAPI connect() times out after 2 seconds', function () {
|
||||
// TODO: Use a timer mock like https://jestjs.io/docs/en/timer-mocks
|
||||
// to test that connect() times out after 2 seconds.
|
||||
});
|
||||
});
|
||||
|
||||
10
yarn.lock
10
yarn.lock
@@ -4116,16 +4116,6 @@ ripple-binary-codec@^0.2.4:
|
||||
lodash "^4.17.15"
|
||||
ripple-address-codec "^3.0.4"
|
||||
|
||||
ripple-hashes@^0.3.4:
|
||||
version "0.3.4"
|
||||
resolved "https://registry.yarnpkg.com/ripple-hashes/-/ripple-hashes-0.3.4.tgz#d06f78fc39dde27749b33ad2461b39ba873c94ec"
|
||||
integrity sha512-u2kgg9Yu9D44HWnC9R2lNg+amVLllJkMQmXZEEM2DAMFXigr4+ph1O8LLxLv+k0fbdjAjos4aUyWwcw6cxzYMw==
|
||||
dependencies:
|
||||
bignumber.js "^4.1.0"
|
||||
create-hash "^1.1.2"
|
||||
ripple-address-codec "^3.0.4"
|
||||
ripple-binary-codec "^0.2.4"
|
||||
|
||||
ripple-keypairs@^0.10.1:
|
||||
version "0.10.2"
|
||||
resolved "https://registry.yarnpkg.com/ripple-keypairs/-/ripple-keypairs-0.10.2.tgz#b3a1d1e2fca85a11c5224b2a7e6da506c19720d0"
|
||||
|
||||
Reference in New Issue
Block a user