xrpl 2.10.0 → 2.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/xrpl-latest-min.js +1 -1
- package/build/xrpl-latest-min.js.map +1 -1
- package/build/xrpl-latest.js +728 -28
- package/build/xrpl-latest.js.map +1 -1
- package/dist/npm/Wallet/walletFromSecretNumbers.d.ts +7 -0
- package/dist/npm/Wallet/walletFromSecretNumbers.d.ts.map +1 -0
- package/dist/npm/Wallet/walletFromSecretNumbers.js +27 -0
- package/dist/npm/Wallet/walletFromSecretNumbers.js.map +1 -0
- package/dist/npm/client/index.d.ts +2 -1
- package/dist/npm/client/index.d.ts.map +1 -1
- package/dist/npm/client/index.js.map +1 -1
- package/dist/npm/index.d.ts +1 -0
- package/dist/npm/index.d.ts.map +1 -1
- package/dist/npm/index.js +3 -1
- package/dist/npm/index.js.map +1 -1
- package/dist/npm/models/common/index.d.ts +5 -0
- package/dist/npm/models/common/index.d.ts.map +1 -1
- package/dist/npm/models/ledger/AMM.d.ts +27 -0
- package/dist/npm/models/ledger/AMM.d.ts.map +1 -0
- package/dist/npm/models/ledger/AMM.js +3 -0
- package/dist/npm/models/ledger/AMM.js.map +1 -0
- package/dist/npm/models/ledger/AccountRoot.d.ts +2 -0
- package/dist/npm/models/ledger/AccountRoot.d.ts.map +1 -1
- package/dist/npm/models/ledger/AccountRoot.js +1 -0
- package/dist/npm/models/ledger/AccountRoot.js.map +1 -1
- package/dist/npm/models/ledger/LedgerEntry.d.ts +2 -1
- package/dist/npm/models/ledger/LedgerEntry.d.ts.map +1 -1
- package/dist/npm/models/methods/ammInfo.d.ts +39 -0
- package/dist/npm/models/methods/ammInfo.d.ts.map +1 -0
- package/dist/npm/models/methods/ammInfo.js +3 -0
- package/dist/npm/models/methods/ammInfo.js.map +1 -0
- package/dist/npm/models/methods/index.d.ts +4 -3
- package/dist/npm/models/methods/index.d.ts.map +1 -1
- package/dist/npm/models/methods/ledgerEntry.d.ts +10 -0
- package/dist/npm/models/methods/ledgerEntry.d.ts.map +1 -1
- package/dist/npm/models/transactions/AMMBid.d.ts +12 -0
- package/dist/npm/models/transactions/AMMBid.d.ts.map +1 -0
- package/dist/npm/models/transactions/AMMBid.js +56 -0
- package/dist/npm/models/transactions/AMMBid.js.map +1 -0
- package/dist/npm/models/transactions/AMMCreate.d.ts +11 -0
- package/dist/npm/models/transactions/AMMCreate.d.ts.map +1 -0
- package/dist/npm/models/transactions/AMMCreate.js +32 -0
- package/dist/npm/models/transactions/AMMCreate.js.map +1 -0
- package/dist/npm/models/transactions/AMMDelete.d.ts +9 -0
- package/dist/npm/models/transactions/AMMDelete.d.ts.map +1 -0
- package/dist/npm/models/transactions/AMMDelete.js +22 -0
- package/dist/npm/models/transactions/AMMDelete.js.map +1 -0
- package/dist/npm/models/transactions/AMMDeposit.d.ts +27 -0
- package/dist/npm/models/transactions/AMMDeposit.d.ts.map +1 -0
- package/dist/npm/models/transactions/AMMDeposit.js +51 -0
- package/dist/npm/models/transactions/AMMDeposit.js.map +1 -0
- package/dist/npm/models/transactions/AMMVote.d.ts +10 -0
- package/dist/npm/models/transactions/AMMVote.d.ts.map +1 -0
- package/dist/npm/models/transactions/AMMVote.js +32 -0
- package/dist/npm/models/transactions/AMMVote.js.map +1 -0
- package/dist/npm/models/transactions/AMMWithdraw.d.ts +31 -0
- package/dist/npm/models/transactions/AMMWithdraw.d.ts.map +1 -0
- package/dist/npm/models/transactions/AMMWithdraw.js +50 -0
- package/dist/npm/models/transactions/AMMWithdraw.js.map +1 -0
- package/dist/npm/models/transactions/common.d.ts +2 -1
- package/dist/npm/models/transactions/common.d.ts.map +1 -1
- package/dist/npm/models/transactions/common.js +12 -1
- package/dist/npm/models/transactions/common.js.map +1 -1
- package/dist/npm/models/transactions/index.d.ts +6 -0
- package/dist/npm/models/transactions/index.d.ts.map +1 -1
- package/dist/npm/models/transactions/index.js +5 -1
- package/dist/npm/models/transactions/index.js.map +1 -1
- package/dist/npm/models/transactions/transaction.d.ts +7 -1
- package/dist/npm/models/transactions/transaction.d.ts.map +1 -1
- package/dist/npm/models/transactions/transaction.js +24 -0
- package/dist/npm/models/transactions/transaction.js.map +1 -1
- package/dist/npm/models/utils/flags.d.ts.map +1 -1
- package/dist/npm/models/utils/flags.js +14 -21
- package/dist/npm/models/utils/flags.js.map +1 -1
- package/dist/npm/snippets/tsconfig.tsbuildinfo +1 -1
- package/dist/npm/src/Wallet/walletFromSecretNumbers.d.ts +7 -0
- package/dist/npm/src/Wallet/walletFromSecretNumbers.d.ts.map +1 -0
- package/dist/npm/src/Wallet/walletFromSecretNumbers.js +27 -0
- package/dist/npm/src/Wallet/walletFromSecretNumbers.js.map +1 -0
- package/dist/npm/src/client/index.d.ts +2 -1
- package/dist/npm/src/client/index.d.ts.map +1 -1
- package/dist/npm/src/client/index.js.map +1 -1
- package/dist/npm/src/index.d.ts +1 -0
- package/dist/npm/src/index.d.ts.map +1 -1
- package/dist/npm/src/index.js +3 -1
- package/dist/npm/src/index.js.map +1 -1
- package/dist/npm/src/models/common/index.d.ts +5 -0
- package/dist/npm/src/models/common/index.d.ts.map +1 -1
- package/dist/npm/src/models/ledger/AMM.d.ts +27 -0
- package/dist/npm/src/models/ledger/AMM.d.ts.map +1 -0
- package/dist/npm/src/models/ledger/AMM.js +3 -0
- package/dist/npm/src/models/ledger/AMM.js.map +1 -0
- package/dist/npm/src/models/ledger/AccountRoot.d.ts +2 -0
- package/dist/npm/src/models/ledger/AccountRoot.d.ts.map +1 -1
- package/dist/npm/src/models/ledger/AccountRoot.js +1 -0
- package/dist/npm/src/models/ledger/AccountRoot.js.map +1 -1
- package/dist/npm/src/models/ledger/LedgerEntry.d.ts +2 -1
- package/dist/npm/src/models/ledger/LedgerEntry.d.ts.map +1 -1
- package/dist/npm/src/models/methods/ammInfo.d.ts +39 -0
- package/dist/npm/src/models/methods/ammInfo.d.ts.map +1 -0
- package/dist/npm/src/models/methods/ammInfo.js +3 -0
- package/dist/npm/src/models/methods/ammInfo.js.map +1 -0
- package/dist/npm/src/models/methods/index.d.ts +4 -3
- package/dist/npm/src/models/methods/index.d.ts.map +1 -1
- package/dist/npm/src/models/methods/ledgerEntry.d.ts +10 -0
- package/dist/npm/src/models/methods/ledgerEntry.d.ts.map +1 -1
- package/dist/npm/src/models/transactions/AMMBid.d.ts +12 -0
- package/dist/npm/src/models/transactions/AMMBid.d.ts.map +1 -0
- package/dist/npm/src/models/transactions/AMMBid.js +56 -0
- package/dist/npm/src/models/transactions/AMMBid.js.map +1 -0
- package/dist/npm/src/models/transactions/AMMCreate.d.ts +11 -0
- package/dist/npm/src/models/transactions/AMMCreate.d.ts.map +1 -0
- package/dist/npm/src/models/transactions/AMMCreate.js +32 -0
- package/dist/npm/src/models/transactions/AMMCreate.js.map +1 -0
- package/dist/npm/src/models/transactions/AMMDelete.d.ts +9 -0
- package/dist/npm/src/models/transactions/AMMDelete.d.ts.map +1 -0
- package/dist/npm/src/models/transactions/AMMDelete.js +22 -0
- package/dist/npm/src/models/transactions/AMMDelete.js.map +1 -0
- package/dist/npm/src/models/transactions/AMMDeposit.d.ts +27 -0
- package/dist/npm/src/models/transactions/AMMDeposit.d.ts.map +1 -0
- package/dist/npm/src/models/transactions/AMMDeposit.js +51 -0
- package/dist/npm/src/models/transactions/AMMDeposit.js.map +1 -0
- package/dist/npm/src/models/transactions/AMMVote.d.ts +10 -0
- package/dist/npm/src/models/transactions/AMMVote.d.ts.map +1 -0
- package/dist/npm/src/models/transactions/AMMVote.js +32 -0
- package/dist/npm/src/models/transactions/AMMVote.js.map +1 -0
- package/dist/npm/src/models/transactions/AMMWithdraw.d.ts +31 -0
- package/dist/npm/src/models/transactions/AMMWithdraw.d.ts.map +1 -0
- package/dist/npm/src/models/transactions/AMMWithdraw.js +50 -0
- package/dist/npm/src/models/transactions/AMMWithdraw.js.map +1 -0
- package/dist/npm/src/models/transactions/common.d.ts +2 -1
- package/dist/npm/src/models/transactions/common.d.ts.map +1 -1
- package/dist/npm/src/models/transactions/common.js +12 -1
- package/dist/npm/src/models/transactions/common.js.map +1 -1
- package/dist/npm/src/models/transactions/index.d.ts +6 -0
- package/dist/npm/src/models/transactions/index.d.ts.map +1 -1
- package/dist/npm/src/models/transactions/index.js +5 -1
- package/dist/npm/src/models/transactions/index.js.map +1 -1
- package/dist/npm/src/models/transactions/transaction.d.ts +7 -1
- package/dist/npm/src/models/transactions/transaction.d.ts.map +1 -1
- package/dist/npm/src/models/transactions/transaction.js +24 -0
- package/dist/npm/src/models/transactions/transaction.js.map +1 -1
- package/dist/npm/src/models/utils/flags.d.ts.map +1 -1
- package/dist/npm/src/models/utils/flags.js +14 -21
- package/dist/npm/src/models/utils/flags.js.map +1 -1
- package/dist/npm/src/sugar/autofill.d.ts.map +1 -1
- package/dist/npm/src/sugar/autofill.js +4 -3
- package/dist/npm/src/sugar/autofill.js.map +1 -1
- package/dist/npm/sugar/autofill.d.ts.map +1 -1
- package/dist/npm/sugar/autofill.js +4 -3
- package/dist/npm/sugar/autofill.js.map +1 -1
- package/package.json +8 -6
- package/src/ECDSA.ts +6 -0
- package/src/Wallet/defaultFaucets.ts +82 -0
- package/src/Wallet/fundWallet.ts +344 -0
- package/src/Wallet/index.ts +504 -0
- package/src/Wallet/rfc1751.ts +190 -0
- package/src/Wallet/rfc1751Words.json +243 -0
- package/src/Wallet/signer.ts +173 -0
- package/src/Wallet/walletFromSecretNumbers.ts +37 -0
- package/src/client/BroadcastClient.ts +84 -0
- package/src/client/ConnectionManager.ts +40 -0
- package/src/client/ExponentialBackoff.ts +71 -0
- package/src/client/RequestManager.ts +194 -0
- package/src/client/WSWrapper.ts +106 -0
- package/src/client/connection.ts +593 -0
- package/src/client/index.ts +712 -0
- package/src/client/partialPayment.ts +153 -0
- package/src/errors.ts +161 -0
- package/src/index.ts +20 -0
- package/src/models/common/index.ts +149 -0
- package/src/models/index.ts +16 -0
- package/src/models/ledger/AMM.ts +78 -0
- package/src/models/ledger/AccountRoot.ts +217 -0
- package/src/models/ledger/Amendments.ts +45 -0
- package/src/models/ledger/BaseLedgerEntry.ts +3 -0
- package/src/models/ledger/Check.ts +70 -0
- package/src/models/ledger/DepositPreauth.ts +35 -0
- package/src/models/ledger/DirectoryNode.ts +46 -0
- package/src/models/ledger/Escrow.ts +74 -0
- package/src/models/ledger/FeeSettings.ts +52 -0
- package/src/models/ledger/Ledger.ts +65 -0
- package/src/models/ledger/LedgerEntry.ts +34 -0
- package/src/models/ledger/LedgerHashes.ts +24 -0
- package/src/models/ledger/NFTokenOffer.ts +16 -0
- package/src/models/ledger/NFTokenPage.ts +20 -0
- package/src/models/ledger/NegativeUNL.ts +34 -0
- package/src/models/ledger/Offer.ts +52 -0
- package/src/models/ledger/PayChannel.ts +107 -0
- package/src/models/ledger/RippleState.ts +88 -0
- package/src/models/ledger/SignerList.ts +56 -0
- package/src/models/ledger/Ticket.ts +36 -0
- package/src/models/ledger/index.ts +58 -0
- package/src/models/methods/accountChannels.ts +93 -0
- package/src/models/methods/accountCurrencies.ts +45 -0
- package/src/models/methods/accountInfo.ts +182 -0
- package/src/models/methods/accountLines.ts +137 -0
- package/src/models/methods/accountNFTs.ts +72 -0
- package/src/models/methods/accountObjects.ts +120 -0
- package/src/models/methods/accountOffers.ts +100 -0
- package/src/models/methods/accountTx.ts +109 -0
- package/src/models/methods/ammInfo.ts +145 -0
- package/src/models/methods/baseMethod.ts +58 -0
- package/src/models/methods/bookOffers.ts +96 -0
- package/src/models/methods/channelVerify.ts +41 -0
- package/src/models/methods/depositAuthorized.ts +56 -0
- package/src/models/methods/fee.ts +91 -0
- package/src/models/methods/gatewayBalances.ts +85 -0
- package/src/models/methods/index.ts +391 -0
- package/src/models/methods/ledger.ts +125 -0
- package/src/models/methods/ledgerClosed.ts +32 -0
- package/src/models/methods/ledgerCurrent.ts +31 -0
- package/src/models/methods/ledgerData.ts +78 -0
- package/src/models/methods/ledgerEntry.ts +177 -0
- package/src/models/methods/manifest.ts +54 -0
- package/src/models/methods/nftBuyOffers.ts +37 -0
- package/src/models/methods/nftHistory.ts +113 -0
- package/src/models/methods/nftInfo.ts +25 -0
- package/src/models/methods/nftSellOffers.ts +37 -0
- package/src/models/methods/norippleCheck.ts +82 -0
- package/src/models/methods/pathFind.ts +116 -0
- package/src/models/methods/ping.ts +21 -0
- package/src/models/methods/random.ts +23 -0
- package/src/models/methods/ripplePathFind.ts +81 -0
- package/src/models/methods/serverInfo.ts +257 -0
- package/src/models/methods/serverState.ts +77 -0
- package/src/models/methods/submit.ts +94 -0
- package/src/models/methods/submitMultisigned.ts +51 -0
- package/src/models/methods/subscribe.ts +435 -0
- package/src/models/methods/transactionEntry.ts +47 -0
- package/src/models/methods/tx.ts +69 -0
- package/src/models/methods/unsubscribe.ts +49 -0
- package/src/models/transactions/AMMBid.ts +140 -0
- package/src/models/transactions/AMMCreate.ts +80 -0
- package/src/models/transactions/AMMDelete.ts +55 -0
- package/src/models/transactions/AMMDeposit.ts +130 -0
- package/src/models/transactions/AMMVote.ts +71 -0
- package/src/models/transactions/AMMWithdraw.ts +126 -0
- package/src/models/transactions/NFTokenAcceptOffer.ts +104 -0
- package/src/models/transactions/NFTokenBurn.ts +48 -0
- package/src/models/transactions/NFTokenCancelOffer.ts +45 -0
- package/src/models/transactions/NFTokenCreateOffer.ts +145 -0
- package/src/models/transactions/NFTokenMint.ts +123 -0
- package/src/models/transactions/UNLModify.ts +20 -0
- package/src/models/transactions/accountDelete.ts +50 -0
- package/src/models/transactions/accountSet.ts +228 -0
- package/src/models/transactions/checkCancel.ts +34 -0
- package/src/models/transactions/checkCash.ts +73 -0
- package/src/models/transactions/checkCreate.ts +90 -0
- package/src/models/transactions/clawback.ts +49 -0
- package/src/models/transactions/common.ts +295 -0
- package/src/models/transactions/depositPreauth.ts +68 -0
- package/src/models/transactions/enableAmendment.ts +26 -0
- package/src/models/transactions/escrowCancel.ts +45 -0
- package/src/models/transactions/escrowCreate.ts +100 -0
- package/src/models/transactions/escrowFinish.ts +63 -0
- package/src/models/transactions/index.ts +66 -0
- package/src/models/transactions/metadata.ts +69 -0
- package/src/models/transactions/offerCancel.ts +37 -0
- package/src/models/transactions/offerCreate.ts +144 -0
- package/src/models/transactions/payment.ts +278 -0
- package/src/models/transactions/paymentChannelClaim.ts +165 -0
- package/src/models/transactions/paymentChannelCreate.ts +116 -0
- package/src/models/transactions/paymentChannelFund.ts +65 -0
- package/src/models/transactions/setFee.ts +48 -0
- package/src/models/transactions/setRegularKey.ts +33 -0
- package/src/models/transactions/signerListSet.ts +89 -0
- package/src/models/transactions/ticketCreate.ts +50 -0
- package/src/models/transactions/transaction.ts +303 -0
- package/src/models/transactions/trustSet.ts +146 -0
- package/src/models/utils/flags.ts +98 -0
- package/src/models/utils/index.ts +37 -0
- package/src/sugar/autofill.ts +373 -0
- package/src/sugar/balances.ts +123 -0
- package/src/sugar/getFeeXrp.ts +45 -0
- package/src/sugar/getLedgerIndex.ts +15 -0
- package/src/sugar/getOrderbook.ts +152 -0
- package/src/sugar/index.ts +11 -0
- package/src/sugar/submit.ts +305 -0
- package/src/sugar/utils.ts +29 -0
- package/src/utils/derive.ts +23 -0
- package/src/utils/getBalanceChanges.ts +186 -0
- package/src/utils/getNFTokenID.ts +97 -0
- package/src/utils/hashes/HashPrefix.ts +40 -0
- package/src/utils/hashes/README.md +65 -0
- package/src/utils/hashes/SHAMap/InnerNode.ts +124 -0
- package/src/utils/hashes/SHAMap/LeafNode.ts +69 -0
- package/src/utils/hashes/SHAMap/index.ts +41 -0
- package/src/utils/hashes/SHAMap/node.ts +14 -0
- package/src/utils/hashes/hashLedger.ts +236 -0
- package/src/utils/hashes/index.ts +187 -0
- package/src/utils/hashes/ledgerSpaces.ts +34 -0
- package/src/utils/hashes/sha512Half.ts +19 -0
- package/src/utils/index.ts +223 -0
- package/src/utils/parseNFTokenID.ts +84 -0
- package/src/utils/quality.ts +169 -0
- package/src/utils/signPaymentChannelClaim.ts +27 -0
- package/src/utils/stringConversion.ts +27 -0
- package/src/utils/timeConversion.ts +53 -0
- package/src/utils/verifyPaymentChannelClaim.ts +30 -0
- package/src/utils/xrpConversion.ts +104 -0
@@ -0,0 +1,71 @@
|
|
1
|
+
/*
|
2
|
+
* Original code based on "backo" - https://github.com/segmentio/backo
|
3
|
+
* MIT License - Copyright 2014 Segment.io
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
|
5
|
+
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
|
6
|
+
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
|
7
|
+
* is furnished to do so, subject to the following conditions:
|
8
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
9
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
10
|
+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
11
|
+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
12
|
+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
13
|
+
*/
|
14
|
+
|
15
|
+
interface ExponentialBackoffOptions {
|
16
|
+
// The min backoff duration.
|
17
|
+
min?: number
|
18
|
+
// The max backoff duration.
|
19
|
+
max?: number
|
20
|
+
}
|
21
|
+
|
22
|
+
const DEFAULT_MIN = 100
|
23
|
+
const DEFAULT_MAX = 1000
|
24
|
+
|
25
|
+
/**
|
26
|
+
* A Back off strategy that increases exponentially. Useful with repeated
|
27
|
+
* setTimeout calls over a network (where the destination may be down).
|
28
|
+
*/
|
29
|
+
export default class ExponentialBackoff {
|
30
|
+
private readonly ms: number
|
31
|
+
private readonly max: number
|
32
|
+
private readonly factor: number = 2
|
33
|
+
private numAttempts = 0
|
34
|
+
|
35
|
+
/**
|
36
|
+
* Constructs an ExponentialBackoff object.
|
37
|
+
*
|
38
|
+
* @param opts - The options for the object.
|
39
|
+
*/
|
40
|
+
public constructor(opts: ExponentialBackoffOptions = {}) {
|
41
|
+
this.ms = opts.min ?? DEFAULT_MIN
|
42
|
+
this.max = opts.max ?? DEFAULT_MAX
|
43
|
+
}
|
44
|
+
|
45
|
+
/**
|
46
|
+
* Number of attempts for backoff so far.
|
47
|
+
*
|
48
|
+
* @returns Number of attempts.
|
49
|
+
*/
|
50
|
+
public get attempts(): number {
|
51
|
+
return this.numAttempts
|
52
|
+
}
|
53
|
+
|
54
|
+
/**
|
55
|
+
* Return the backoff duration.
|
56
|
+
*
|
57
|
+
* @returns The backoff duration in milliseconds.
|
58
|
+
*/
|
59
|
+
public duration(): number {
|
60
|
+
const ms = this.ms * this.factor ** this.numAttempts
|
61
|
+
this.numAttempts += 1
|
62
|
+
return Math.floor(Math.min(ms, this.max))
|
63
|
+
}
|
64
|
+
|
65
|
+
/**
|
66
|
+
* Reset the number of attempts.
|
67
|
+
*/
|
68
|
+
public reset(): void {
|
69
|
+
this.numAttempts = 0
|
70
|
+
}
|
71
|
+
}
|
@@ -0,0 +1,194 @@
|
|
1
|
+
import {
|
2
|
+
ResponseFormatError,
|
3
|
+
RippledError,
|
4
|
+
TimeoutError,
|
5
|
+
XrplError,
|
6
|
+
} from '../errors'
|
7
|
+
import { Response } from '../models/methods'
|
8
|
+
import { BaseRequest, ErrorResponse } from '../models/methods/baseMethod'
|
9
|
+
|
10
|
+
/**
|
11
|
+
* Manage all the requests made to the websocket, and their async responses
|
12
|
+
* that come in from the WebSocket. Responses come in over the WS connection
|
13
|
+
* after-the-fact, so this manager will tie that response to resolve the
|
14
|
+
* original request.
|
15
|
+
*/
|
16
|
+
export default class RequestManager {
|
17
|
+
private nextId = 0
|
18
|
+
private readonly promisesAwaitingResponse = new Map<
|
19
|
+
string | number,
|
20
|
+
{
|
21
|
+
resolve: (value: Response | PromiseLike<Response>) => void
|
22
|
+
reject: (value: Error) => void
|
23
|
+
timer: ReturnType<typeof setTimeout>
|
24
|
+
}
|
25
|
+
>()
|
26
|
+
|
27
|
+
/**
|
28
|
+
* Successfully resolves a request.
|
29
|
+
*
|
30
|
+
* @param id - ID of the request.
|
31
|
+
* @param response - Response to return.
|
32
|
+
* @throws Error if no existing promise with the given ID.
|
33
|
+
*/
|
34
|
+
public resolve(id: string | number, response: Response): void {
|
35
|
+
const promise = this.promisesAwaitingResponse.get(id)
|
36
|
+
if (promise == null) {
|
37
|
+
throw new XrplError(`No existing promise with id ${id}`, {
|
38
|
+
type: 'resolve',
|
39
|
+
response,
|
40
|
+
})
|
41
|
+
}
|
42
|
+
clearTimeout(promise.timer)
|
43
|
+
promise.resolve(response)
|
44
|
+
this.deletePromise(id)
|
45
|
+
}
|
46
|
+
|
47
|
+
/**
|
48
|
+
* Rejects a request.
|
49
|
+
*
|
50
|
+
* @param id - ID of the request.
|
51
|
+
* @param error - Error to throw with the reject.
|
52
|
+
* @throws Error if no existing promise with the given ID.
|
53
|
+
*/
|
54
|
+
public reject(id: string | number, error: Error): void {
|
55
|
+
const promise = this.promisesAwaitingResponse.get(id)
|
56
|
+
if (promise == null) {
|
57
|
+
throw new XrplError(`No existing promise with id ${id}`, {
|
58
|
+
type: 'reject',
|
59
|
+
error,
|
60
|
+
})
|
61
|
+
}
|
62
|
+
clearTimeout(promise.timer)
|
63
|
+
// TODO: figure out how to have a better stack trace for an error
|
64
|
+
promise.reject(error)
|
65
|
+
this.deletePromise(id)
|
66
|
+
}
|
67
|
+
|
68
|
+
/**
|
69
|
+
* Reject all pending requests.
|
70
|
+
*
|
71
|
+
* @param error - Error to throw with the reject.
|
72
|
+
*/
|
73
|
+
public rejectAll(error: Error): void {
|
74
|
+
this.promisesAwaitingResponse.forEach((_promise, id, _map) => {
|
75
|
+
this.reject(id, error)
|
76
|
+
this.deletePromise(id)
|
77
|
+
})
|
78
|
+
}
|
79
|
+
|
80
|
+
/**
|
81
|
+
* Creates a new WebSocket request. This sets up a timeout timer to catch
|
82
|
+
* hung responses, and a promise that will resolve with the response once
|
83
|
+
* the response is seen & handled.
|
84
|
+
*
|
85
|
+
* @param request - Request to create.
|
86
|
+
* @param timeout - Timeout length to catch hung responses.
|
87
|
+
* @returns Request ID, new request form, and the promise for resolving the request.
|
88
|
+
* @throws XrplError if request with the same ID is already pending.
|
89
|
+
*/
|
90
|
+
public createRequest<T extends BaseRequest>(
|
91
|
+
request: T,
|
92
|
+
timeout: number,
|
93
|
+
): [string | number, string, Promise<Response>] {
|
94
|
+
let newId: string | number
|
95
|
+
if (request.id == null) {
|
96
|
+
newId = this.nextId
|
97
|
+
this.nextId += 1
|
98
|
+
} else {
|
99
|
+
newId = request.id
|
100
|
+
}
|
101
|
+
const newRequest = JSON.stringify({ ...request, id: newId })
|
102
|
+
// Typing required for Jest running in browser
|
103
|
+
const timer: ReturnType<typeof setTimeout> = setTimeout(() => {
|
104
|
+
this.reject(
|
105
|
+
newId,
|
106
|
+
new TimeoutError(
|
107
|
+
`Timeout for request: ${JSON.stringify(request)} with id ${newId}`,
|
108
|
+
request,
|
109
|
+
),
|
110
|
+
)
|
111
|
+
}, timeout)
|
112
|
+
/*
|
113
|
+
* Node.js won't exit if a timer is still running, so we tell Node to ignore.
|
114
|
+
* (Node will still wait for the request to complete).
|
115
|
+
*/
|
116
|
+
// The following type assertions are required to get this code to pass in browser environments
|
117
|
+
// where setTimeout has a different type
|
118
|
+
// eslint-disable-next-line max-len -- Necessary to disable both rules.
|
119
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access -- Reason above.
|
120
|
+
if ((timer as unknown as any).unref) {
|
121
|
+
// eslint-disable-next-line max-len -- Necessary to disable both rules.
|
122
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call -- Reason above.
|
123
|
+
;(timer as unknown as any).unref()
|
124
|
+
}
|
125
|
+
if (this.promisesAwaitingResponse.has(newId)) {
|
126
|
+
clearTimeout(timer)
|
127
|
+
throw new XrplError(
|
128
|
+
`Response with id '${newId}' is already pending`,
|
129
|
+
request,
|
130
|
+
)
|
131
|
+
}
|
132
|
+
const newPromise = new Promise<Response>(
|
133
|
+
(resolve: (value: Response | PromiseLike<Response>) => void, reject) => {
|
134
|
+
this.promisesAwaitingResponse.set(newId, { resolve, reject, timer })
|
135
|
+
},
|
136
|
+
)
|
137
|
+
|
138
|
+
return [newId, newRequest, newPromise]
|
139
|
+
}
|
140
|
+
|
141
|
+
/**
|
142
|
+
* Handle a "response". Responses match to the earlier request handlers,
|
143
|
+
* and resolve/reject based on the data received.
|
144
|
+
*
|
145
|
+
* @param response - The response to handle.
|
146
|
+
* @throws ResponseFormatError if the response format is invalid, RippledError if rippled returns an error.
|
147
|
+
*/
|
148
|
+
public handleResponse(response: Partial<Response | ErrorResponse>): void {
|
149
|
+
if (
|
150
|
+
response.id == null ||
|
151
|
+
!(typeof response.id === 'string' || typeof response.id === 'number')
|
152
|
+
) {
|
153
|
+
throw new ResponseFormatError('valid id not found in response', response)
|
154
|
+
}
|
155
|
+
if (!this.promisesAwaitingResponse.has(response.id)) {
|
156
|
+
return
|
157
|
+
}
|
158
|
+
if (response.status == null) {
|
159
|
+
const error = new ResponseFormatError('Response has no status')
|
160
|
+
this.reject(response.id, error)
|
161
|
+
}
|
162
|
+
if (response.status === 'error') {
|
163
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- We know this must be true
|
164
|
+
const errorResponse = response as Partial<ErrorResponse>
|
165
|
+
const error = new RippledError(
|
166
|
+
errorResponse.error_message ?? errorResponse.error,
|
167
|
+
errorResponse,
|
168
|
+
)
|
169
|
+
this.reject(response.id, error)
|
170
|
+
return
|
171
|
+
}
|
172
|
+
if (response.status !== 'success') {
|
173
|
+
const error = new ResponseFormatError(
|
174
|
+
`unrecognized response.status: ${response.status ?? ''}`,
|
175
|
+
response,
|
176
|
+
)
|
177
|
+
this.reject(response.id, error)
|
178
|
+
return
|
179
|
+
}
|
180
|
+
// status no longer needed because error is thrown if status is not "success"
|
181
|
+
delete response.status
|
182
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Must be a valid Response here
|
183
|
+
this.resolve(response.id, response as unknown as Response)
|
184
|
+
}
|
185
|
+
|
186
|
+
/**
|
187
|
+
* Delete a promise after it has been returned.
|
188
|
+
*
|
189
|
+
* @param id - ID of the request.
|
190
|
+
*/
|
191
|
+
private deletePromise(id: string | number): void {
|
192
|
+
this.promisesAwaitingResponse.delete(id)
|
193
|
+
}
|
194
|
+
}
|
@@ -0,0 +1,106 @@
|
|
1
|
+
/* eslint-disable max-classes-per-file -- Needs to be a wrapper for ws */
|
2
|
+
import { EventEmitter } from 'events'
|
3
|
+
|
4
|
+
// Define the global WebSocket class found on the native browser
|
5
|
+
declare class WebSocket {
|
6
|
+
public onclose?: (closeEvent: CloseEvent) => void
|
7
|
+
public onopen?: (openEvent: Event) => void
|
8
|
+
public onerror?: (error: Error) => void
|
9
|
+
public onmessage?: (message: MessageEvent) => void
|
10
|
+
public readyState: number
|
11
|
+
public constructor(url: string)
|
12
|
+
public close(code?: number, reason?: Buffer): void
|
13
|
+
public send(message: string): void
|
14
|
+
}
|
15
|
+
|
16
|
+
interface WSWrapperOptions {
|
17
|
+
perMessageDeflate: boolean
|
18
|
+
handshakeTimeout: number
|
19
|
+
protocolVersion: number
|
20
|
+
origin: string
|
21
|
+
maxPayload: number
|
22
|
+
followRedirects: boolean
|
23
|
+
maxRedirects: number
|
24
|
+
}
|
25
|
+
|
26
|
+
/**
|
27
|
+
* Provides `EventEmitter` interface for native browser `WebSocket`,
|
28
|
+
* same, as `ws` package provides.
|
29
|
+
*/
|
30
|
+
export default class WSWrapper extends EventEmitter {
|
31
|
+
public static CONNECTING = 0
|
32
|
+
public static OPEN = 1
|
33
|
+
public static CLOSING = 2
|
34
|
+
// eslint-disable-next-line @typescript-eslint/no-magic-numbers -- magic number is being defined here
|
35
|
+
public static CLOSED = 3
|
36
|
+
private readonly ws: WebSocket
|
37
|
+
|
38
|
+
/**
|
39
|
+
* Constructs a browser-safe websocket.
|
40
|
+
*
|
41
|
+
* @param url - URL to connect to.
|
42
|
+
* @param _protocols - Not used.
|
43
|
+
* @param _websocketOptions - Not used.
|
44
|
+
*/
|
45
|
+
public constructor(
|
46
|
+
url: string,
|
47
|
+
_protocols: string | string[] | WSWrapperOptions | undefined,
|
48
|
+
_websocketOptions: WSWrapperOptions,
|
49
|
+
) {
|
50
|
+
super()
|
51
|
+
this.setMaxListeners(Infinity)
|
52
|
+
|
53
|
+
this.ws = new WebSocket(url)
|
54
|
+
|
55
|
+
this.ws.onclose = (closeEvent: CloseEvent): void => {
|
56
|
+
let reason: Uint8Array | undefined
|
57
|
+
if (closeEvent.reason) {
|
58
|
+
const enc = new TextEncoder()
|
59
|
+
reason = enc.encode(closeEvent.reason)
|
60
|
+
}
|
61
|
+
this.emit('close', closeEvent.code, reason)
|
62
|
+
}
|
63
|
+
|
64
|
+
this.ws.onopen = (): void => {
|
65
|
+
this.emit('open')
|
66
|
+
}
|
67
|
+
|
68
|
+
this.ws.onerror = (error): void => {
|
69
|
+
this.emit('error', error)
|
70
|
+
}
|
71
|
+
|
72
|
+
this.ws.onmessage = (message: MessageEvent): void => {
|
73
|
+
this.emit('message', message.data)
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
/**
|
78
|
+
* Get the ready state of the websocket.
|
79
|
+
*
|
80
|
+
* @returns The Websocket's ready state.
|
81
|
+
*/
|
82
|
+
public get readyState(): number {
|
83
|
+
return this.ws.readyState
|
84
|
+
}
|
85
|
+
|
86
|
+
/**
|
87
|
+
* Closes the websocket.
|
88
|
+
*
|
89
|
+
* @param code - Close code.
|
90
|
+
* @param reason - Close reason.
|
91
|
+
*/
|
92
|
+
public close(code?: number, reason?: Buffer): void {
|
93
|
+
if (this.readyState === 1) {
|
94
|
+
this.ws.close(code, reason)
|
95
|
+
}
|
96
|
+
}
|
97
|
+
|
98
|
+
/**
|
99
|
+
* Sends a message over the Websocket connection.
|
100
|
+
*
|
101
|
+
* @param message - Message to send.
|
102
|
+
*/
|
103
|
+
public send(message: string): void {
|
104
|
+
this.ws.send(message)
|
105
|
+
}
|
106
|
+
}
|