amogus-sdk-internal-utils 0.35.2
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/dist/_virtual/rolldown_runtime.cjs +43 -0
- package/dist/_virtual/rolldown_runtime.js +18 -0
- package/dist/bridgeIndexer/bridgeIndexerHttpClient/apis.cjs +10 -0
- package/dist/bridgeIndexer/bridgeIndexerHttpClient/apis.d.cts +6 -0
- package/dist/bridgeIndexer/bridgeIndexerHttpClient/apis.d.ts +6 -0
- package/dist/bridgeIndexer/bridgeIndexerHttpClient/apis.js +10 -0
- package/dist/bridgeIndexer/bridgeIndexerHttpClient/index.cjs +17 -0
- package/dist/bridgeIndexer/bridgeIndexerHttpClient/index.d.cts +9 -0
- package/dist/bridgeIndexer/bridgeIndexerHttpClient/index.d.ts +9 -0
- package/dist/bridgeIndexer/bridgeIndexerHttpClient/index.js +12 -0
- package/dist/bridgeIndexer/bridgeIndexerHttpClient/runtime.cjs +20 -0
- package/dist/bridgeIndexer/bridgeIndexerHttpClient/runtime.js +20 -0
- package/dist/bridgeIndexer/bridgeIndexerHttpClient/types.cjs +25 -0
- package/dist/bridgeIndexer/bridgeIndexerHttpClient/types.d.cts +31 -0
- package/dist/bridgeIndexer/bridgeIndexerHttpClient/types.d.ts +31 -0
- package/dist/bridgeIndexer/bridgeIndexerHttpClient/types.js +23 -0
- package/dist/bridgeIndexer/index.cjs +13 -0
- package/dist/bridgeIndexer/index.d.cts +8 -0
- package/dist/bridgeIndexer/index.d.ts +8 -0
- package/dist/bridgeIndexer/index.js +8 -0
- package/dist/config.cjs +86 -0
- package/dist/config.d.cts +50 -0
- package/dist/config.d.ts +50 -0
- package/dist/config.js +76 -0
- package/dist/errors/assert.cjs +11 -0
- package/dist/errors/assert.d.cts +11 -0
- package/dist/errors/assert.d.ts +11 -0
- package/dist/errors/assert.js +11 -0
- package/dist/errors/base.cjs +34 -0
- package/dist/errors/base.d.cts +21 -0
- package/dist/errors/base.d.ts +21 -0
- package/dist/errors/base.js +33 -0
- package/dist/errors/index.cjs +23 -0
- package/dist/errors/index.d.cts +11 -0
- package/dist/errors/index.d.ts +11 -0
- package/dist/errors/index.js +18 -0
- package/dist/errors/request.cjs +48 -0
- package/dist/errors/request.d.cts +61 -0
- package/dist/errors/request.d.ts +61 -0
- package/dist/errors/request.js +46 -0
- package/dist/errors/utils/isNetworkError.cjs +23 -0
- package/dist/errors/utils/isNetworkError.js +22 -0
- package/dist/errors/utils/toError.cjs +8 -0
- package/dist/errors/utils/toError.d.cts +4 -0
- package/dist/errors/utils/toError.d.ts +4 -0
- package/dist/errors/utils/toError.js +7 -0
- package/dist/index.cjs +144 -0
- package/dist/index.d.cts +33 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +33 -0
- package/dist/logger.d.cts +11 -0
- package/dist/logger.d.ts +11 -0
- package/dist/nearClient.cjs +18 -0
- package/dist/nearClient.d.cts +8 -0
- package/dist/nearClient.d.ts +8 -0
- package/dist/nearClient.js +18 -0
- package/dist/poaBridge/constants/blockchains.cjs +62 -0
- package/dist/poaBridge/constants/blockchains.d.cts +95 -0
- package/dist/poaBridge/constants/blockchains.d.ts +95 -0
- package/dist/poaBridge/constants/blockchains.js +59 -0
- package/dist/poaBridge/errors/withdrawal.cjs +52 -0
- package/dist/poaBridge/errors/withdrawal.d.cts +34 -0
- package/dist/poaBridge/errors/withdrawal.d.ts +34 -0
- package/dist/poaBridge/errors/withdrawal.js +50 -0
- package/dist/poaBridge/getPendingDeposits.cjs +31 -0
- package/dist/poaBridge/getPendingDeposits.d.cts +14 -0
- package/dist/poaBridge/getPendingDeposits.d.ts +15 -0
- package/dist/poaBridge/getPendingDeposits.js +31 -0
- package/dist/poaBridge/index.cjs +23 -0
- package/dist/poaBridge/index.d.cts +11 -0
- package/dist/poaBridge/index.d.ts +11 -0
- package/dist/poaBridge/index.js +18 -0
- package/dist/poaBridge/poaBridgeHttpClient/apis.cjs +38 -0
- package/dist/poaBridge/poaBridgeHttpClient/apis.d.cts +12 -0
- package/dist/poaBridge/poaBridgeHttpClient/apis.d.ts +12 -0
- package/dist/poaBridge/poaBridgeHttpClient/apis.js +32 -0
- package/dist/poaBridge/poaBridgeHttpClient/index.cjs +21 -0
- package/dist/poaBridge/poaBridgeHttpClient/index.d.cts +9 -0
- package/dist/poaBridge/poaBridgeHttpClient/index.d.ts +9 -0
- package/dist/poaBridge/poaBridgeHttpClient/index.js +16 -0
- package/dist/poaBridge/poaBridgeHttpClient/runtime.cjs +49 -0
- package/dist/poaBridge/poaBridgeHttpClient/runtime.js +47 -0
- package/dist/poaBridge/poaBridgeHttpClient/types.d.cts +172 -0
- package/dist/poaBridge/poaBridgeHttpClient/types.d.ts +172 -0
- package/dist/poaBridge/waitForWithdrawalCompletion.cjs +57 -0
- package/dist/poaBridge/waitForWithdrawalCompletion.d.cts +31 -0
- package/dist/poaBridge/waitForWithdrawalCompletion.d.ts +31 -0
- package/dist/poaBridge/waitForWithdrawalCompletion.js +56 -0
- package/dist/services/blockchainBalanceService.cjs +40 -0
- package/dist/services/blockchainBalanceService.d.cts +21 -0
- package/dist/services/blockchainBalanceService.d.ts +21 -0
- package/dist/services/blockchainBalanceService.js +37 -0
- package/dist/solverRelay/errors/intentSettlement.cjs +20 -0
- package/dist/solverRelay/errors/intentSettlement.d.cts +13 -0
- package/dist/solverRelay/errors/intentSettlement.d.ts +13 -0
- package/dist/solverRelay/errors/intentSettlement.js +20 -0
- package/dist/solverRelay/errors/quote.cjs +18 -0
- package/dist/solverRelay/errors/quote.d.cts +21 -0
- package/dist/solverRelay/errors/quote.d.ts +22 -0
- package/dist/solverRelay/errors/quote.js +18 -0
- package/dist/solverRelay/getQuote.cjs +70 -0
- package/dist/solverRelay/getQuote.d.cts +14 -0
- package/dist/solverRelay/getQuote.d.ts +14 -0
- package/dist/solverRelay/getQuote.js +70 -0
- package/dist/solverRelay/getStatus.cjs +26 -0
- package/dist/solverRelay/getStatus.d.cts +17 -0
- package/dist/solverRelay/getStatus.d.ts +18 -0
- package/dist/solverRelay/getStatus.js +26 -0
- package/dist/solverRelay/index.cjs +27 -0
- package/dist/solverRelay/index.d.cts +16 -0
- package/dist/solverRelay/index.d.ts +16 -0
- package/dist/solverRelay/index.js +22 -0
- package/dist/solverRelay/publishIntent.cjs +20 -0
- package/dist/solverRelay/publishIntent.d.cts +15 -0
- package/dist/solverRelay/publishIntent.d.ts +16 -0
- package/dist/solverRelay/publishIntent.js +20 -0
- package/dist/solverRelay/publishIntents.cjs +42 -0
- package/dist/solverRelay/publishIntents.d.cts +10 -0
- package/dist/solverRelay/publishIntents.d.ts +11 -0
- package/dist/solverRelay/publishIntents.js +41 -0
- package/dist/solverRelay/solverRelayHttpClient/apis.cjs +21 -0
- package/dist/solverRelay/solverRelayHttpClient/apis.d.cts +9 -0
- package/dist/solverRelay/solverRelayHttpClient/apis.d.ts +9 -0
- package/dist/solverRelay/solverRelayHttpClient/apis.js +18 -0
- package/dist/solverRelay/solverRelayHttpClient/index.cjs +18 -0
- package/dist/solverRelay/solverRelayHttpClient/index.d.cts +9 -0
- package/dist/solverRelay/solverRelayHttpClient/index.d.ts +9 -0
- package/dist/solverRelay/solverRelayHttpClient/index.js +13 -0
- package/dist/solverRelay/solverRelayHttpClient/runtime.cjs +60 -0
- package/dist/solverRelay/solverRelayHttpClient/runtime.js +58 -0
- package/dist/solverRelay/solverRelayHttpClient/types.d.cts +117 -0
- package/dist/solverRelay/solverRelayHttpClient/types.d.ts +117 -0
- package/dist/solverRelay/utils/parseFailedPublishError.cjs +64 -0
- package/dist/solverRelay/utils/parseFailedPublishError.d.cts +56 -0
- package/dist/solverRelay/utils/parseFailedPublishError.d.ts +57 -0
- package/dist/solverRelay/utils/parseFailedPublishError.js +61 -0
- package/dist/solverRelay/utils/quoteWithLog.cjs +21 -0
- package/dist/solverRelay/utils/quoteWithLog.d.cts +12 -0
- package/dist/solverRelay/utils/quoteWithLog.d.ts +14 -0
- package/dist/solverRelay/utils/quoteWithLog.js +21 -0
- package/dist/solverRelay/waitForIntentSettlement.cjs +52 -0
- package/dist/solverRelay/waitForIntentSettlement.d.cts +30 -0
- package/dist/solverRelay/waitForIntentSettlement.d.ts +30 -0
- package/dist/solverRelay/waitForIntentSettlement.js +52 -0
- package/dist/types/authHandle.cjs +22 -0
- package/dist/types/authHandle.d.cts +25 -0
- package/dist/types/authHandle.d.ts +25 -0
- package/dist/types/authHandle.js +16 -0
- package/dist/types/base.d.cts +45 -0
- package/dist/types/base.d.ts +45 -0
- package/dist/types/intentsUserId.d.cts +10 -0
- package/dist/types/intentsUserId.d.ts +10 -0
- package/dist/types/walletMessage.cjs +11 -0
- package/dist/types/walletMessage.d.cts +106 -0
- package/dist/types/walletMessage.d.ts +106 -0
- package/dist/types/walletMessage.js +5 -0
- package/dist/utils/abortSignal.cjs +19 -0
- package/dist/utils/abortSignal.js +18 -0
- package/dist/utils/appFee.cjs +20 -0
- package/dist/utils/appFee.d.cts +9 -0
- package/dist/utils/appFee.d.ts +9 -0
- package/dist/utils/appFee.js +15 -0
- package/dist/utils/assert.cjs +9 -0
- package/dist/utils/assert.d.cts +7 -0
- package/dist/utils/assert.d.ts +7 -0
- package/dist/utils/assert.js +9 -0
- package/dist/utils/authIdentity.cjs +56 -0
- package/dist/utils/authIdentity.d.cts +34 -0
- package/dist/utils/authIdentity.d.ts +34 -0
- package/dist/utils/authIdentity.js +50 -0
- package/dist/utils/failover.cjs +36 -0
- package/dist/utils/failover.d.cts +24 -0
- package/dist/utils/failover.d.ts +24 -0
- package/dist/utils/failover.js +34 -0
- package/dist/utils/handleRPCResponse.cjs +30 -0
- package/dist/utils/handleRPCResponse.js +28 -0
- package/dist/utils/handleResponse.cjs +31 -0
- package/dist/utils/handleResponse.js +29 -0
- package/dist/utils/index.cjs +55 -0
- package/dist/utils/index.d.cts +13 -0
- package/dist/utils/index.d.ts +13 -0
- package/dist/utils/index.js +50 -0
- package/dist/utils/messageFactory.cjs +137 -0
- package/dist/utils/messageFactory.d.cts +70 -0
- package/dist/utils/messageFactory.d.ts +70 -0
- package/dist/utils/messageFactory.js +132 -0
- package/dist/utils/multiPayload/webauthn.cjs +27 -0
- package/dist/utils/multiPayload/webauthn.js +26 -0
- package/dist/utils/near.cjs +58 -0
- package/dist/utils/near.d.cts +35 -0
- package/dist/utils/near.d.ts +35 -0
- package/dist/utils/near.js +53 -0
- package/dist/utils/poll.cjs +87 -0
- package/dist/utils/poll.d.cts +54 -0
- package/dist/utils/poll.d.ts +54 -0
- package/dist/utils/poll.js +85 -0
- package/dist/utils/prepareBroadcastRequest.cjs +98 -0
- package/dist/utils/prepareBroadcastRequest.d.cts +16 -0
- package/dist/utils/prepareBroadcastRequest.d.ts +16 -0
- package/dist/utils/prepareBroadcastRequest.js +90 -0
- package/dist/utils/promise/withTimeout.cjs +34 -0
- package/dist/utils/promise/withTimeout.d.cts +16 -0
- package/dist/utils/promise/withTimeout.d.ts +16 -0
- package/dist/utils/promise/withTimeout.js +34 -0
- package/dist/utils/request.cjs +100 -0
- package/dist/utils/request.d.cts +27 -0
- package/dist/utils/request.d.ts +27 -0
- package/dist/utils/request.js +99 -0
- package/dist/utils/requestShouldRetry.cjs +20 -0
- package/dist/utils/requestShouldRetry.js +20 -0
- package/dist/utils/retry.cjs +66 -0
- package/dist/utils/retry.d.cts +56 -0
- package/dist/utils/retry.d.ts +56 -0
- package/dist/utils/retry.js +64 -0
- package/dist/utils/rpc-endpoint.cjs +56 -0
- package/dist/utils/rpc-endpoint.d.cts +20 -0
- package/dist/utils/rpc-endpoint.d.ts +20 -0
- package/dist/utils/rpc-endpoint.js +54 -0
- package/dist/utils/serialize.cjs +82 -0
- package/dist/utils/serialize.d.cts +17 -0
- package/dist/utils/serialize.d.ts +17 -0
- package/dist/utils/serialize.js +81 -0
- package/dist/utils/stellarAddressToBytes.cjs +295 -0
- package/dist/utils/stellarAddressToBytes.js +294 -0
- package/dist/utils/token.cjs +8 -0
- package/dist/utils/token.js +7 -0
- package/dist/utils/tokenUtils.cjs +263 -0
- package/dist/utils/tokenUtils.d.cts +88 -0
- package/dist/utils/tokenUtils.d.ts +88 -0
- package/dist/utils/tokenUtils.js +238 -0
- package/dist/utils/tronAddressToHex.cjs +18 -0
- package/dist/utils/tronAddressToHex.js +17 -0
- package/dist/utils/uint8Array.cjs +15 -0
- package/dist/utils/uint8Array.d.cts +4 -0
- package/dist/utils/uint8Array.d.ts +4 -0
- package/dist/utils/uint8Array.js +14 -0
- package/dist/utils/webAuthn.cjs +58 -0
- package/dist/utils/webAuthn.js +56 -0
- package/dist/xrpl/index.cjs +13 -0
- package/dist/xrpl/index.d.cts +8 -0
- package/dist/xrpl/index.d.ts +8 -0
- package/dist/xrpl/index.js +8 -0
- package/dist/xrpl/xrplHttpClient/apis.cjs +28 -0
- package/dist/xrpl/xrplHttpClient/apis.d.cts +7 -0
- package/dist/xrpl/xrplHttpClient/apis.d.ts +7 -0
- package/dist/xrpl/xrplHttpClient/apis.js +27 -0
- package/dist/xrpl/xrplHttpClient/errors.cjs +27 -0
- package/dist/xrpl/xrplHttpClient/errors.d.cts +24 -0
- package/dist/xrpl/xrplHttpClient/errors.d.ts +24 -0
- package/dist/xrpl/xrplHttpClient/errors.js +26 -0
- package/dist/xrpl/xrplHttpClient/index.cjs +19 -0
- package/dist/xrpl/xrplHttpClient/index.d.cts +10 -0
- package/dist/xrpl/xrplHttpClient/index.d.ts +10 -0
- package/dist/xrpl/xrplHttpClient/index.js +14 -0
- package/dist/xrpl/xrplHttpClient/runtime.cjs +57 -0
- package/dist/xrpl/xrplHttpClient/runtime.js +55 -0
- package/dist/xrpl/xrplHttpClient/types.cjs +39 -0
- package/dist/xrpl/xrplHttpClient/types.d.cts +64 -0
- package/dist/xrpl/xrplHttpClient/types.d.ts +64 -0
- package/dist/xrpl/xrplHttpClient/types.js +35 -0
- package/package.json +59 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_request = require('../errors/request.cjs');
|
|
3
|
+
const require_isNetworkError = require('../errors/utils/isNetworkError.cjs');
|
|
4
|
+
const require_toError = require('../errors/utils/toError.cjs');
|
|
5
|
+
const require_abortSignal = require('./abortSignal.cjs');
|
|
6
|
+
const require_withTimeout = require('./promise/withTimeout.cjs');
|
|
7
|
+
const require_requestShouldRetry = require('./requestShouldRetry.cjs');
|
|
8
|
+
let _lifeomic_attempt = require("@lifeomic/attempt");
|
|
9
|
+
|
|
10
|
+
//#region src/utils/request.ts
|
|
11
|
+
async function request({ retryOptions = { maxAttempts: 1 }, logger, ...params }) {
|
|
12
|
+
const overallStartTime = performance.now();
|
|
13
|
+
let totalNetworkTime = 0;
|
|
14
|
+
let attemptCount = 0;
|
|
15
|
+
const response = await (0, _lifeomic_attempt.retry)(async (ctx) => {
|
|
16
|
+
attemptCount = ctx.attemptNum + 1;
|
|
17
|
+
const attemptStartTime = performance.now();
|
|
18
|
+
logger?.debug("Sending HTTP request", {
|
|
19
|
+
url: params.url.toString(),
|
|
20
|
+
method: params.fetchOptions?.method || "GET",
|
|
21
|
+
body: JSON.stringify(params.body),
|
|
22
|
+
attempt: ctx.attemptNum
|
|
23
|
+
});
|
|
24
|
+
const response$1 = await request_(params);
|
|
25
|
+
const attemptDuration = performance.now() - attemptStartTime;
|
|
26
|
+
totalNetworkTime += attemptDuration;
|
|
27
|
+
logger?.debug("HTTP request attempt completed", {
|
|
28
|
+
attempt: ctx.attemptNum,
|
|
29
|
+
attemptDuration: Math.floor(attemptDuration),
|
|
30
|
+
statusCode: response$1.status,
|
|
31
|
+
success: response$1.ok,
|
|
32
|
+
responseSize: response$1.headers.get("content-length")
|
|
33
|
+
});
|
|
34
|
+
return response$1;
|
|
35
|
+
}, {
|
|
36
|
+
...retryOptions,
|
|
37
|
+
handleError: (err, context) => {
|
|
38
|
+
logger?.error("HTTP request attempt failed", {
|
|
39
|
+
attempt: context.attemptNum,
|
|
40
|
+
errorMessage: require_toError.toError(err).message
|
|
41
|
+
});
|
|
42
|
+
if (!require_requestShouldRetry.requestShouldRetry(err) || params.fetchOptions?.signal?.reason === err) context.abort();
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
const overallDuration = performance.now() - overallStartTime;
|
|
46
|
+
const retryOverhead = overallDuration - totalNetworkTime;
|
|
47
|
+
logger?.[attemptCount > 1 ? "info" : "debug"]("HTTP request completed", {
|
|
48
|
+
statusCode: response.status,
|
|
49
|
+
success: response.ok,
|
|
50
|
+
totalDuration: Math.floor(overallDuration),
|
|
51
|
+
networkTime: Math.floor(totalNetworkTime),
|
|
52
|
+
retryOverhead: Math.floor(retryOverhead),
|
|
53
|
+
attempts: attemptCount,
|
|
54
|
+
responseSize: response.headers.get("content-length")
|
|
55
|
+
});
|
|
56
|
+
return response;
|
|
57
|
+
}
|
|
58
|
+
async function request_({ url, body, timeout = 1e4, fetchOptions }) {
|
|
59
|
+
const { headers: customHeaders, method, signal: signal_ } = fetchOptions ?? {};
|
|
60
|
+
try {
|
|
61
|
+
const headers = new Headers(customHeaders);
|
|
62
|
+
headers.set("Content-Type", "application/json");
|
|
63
|
+
const response = await require_withTimeout.withTimeout(({ signal }) => {
|
|
64
|
+
return fetch(url, {
|
|
65
|
+
...fetchOptions,
|
|
66
|
+
method,
|
|
67
|
+
headers,
|
|
68
|
+
body: JSON.stringify(body),
|
|
69
|
+
signal: require_abortSignal.mergeAbortSignals([signal_, timeout > 0 ? signal : null].filter(Boolean))
|
|
70
|
+
});
|
|
71
|
+
}, {
|
|
72
|
+
errorInstance: new require_request.TimeoutError({
|
|
73
|
+
body,
|
|
74
|
+
url: url.toString()
|
|
75
|
+
}),
|
|
76
|
+
timeout,
|
|
77
|
+
signal: true
|
|
78
|
+
});
|
|
79
|
+
if (!response.ok) throw new require_request.HttpRequestError({
|
|
80
|
+
body,
|
|
81
|
+
details: response.statusText,
|
|
82
|
+
headers: response.headers,
|
|
83
|
+
status: response.status,
|
|
84
|
+
url: url.toString()
|
|
85
|
+
});
|
|
86
|
+
return response;
|
|
87
|
+
} catch (err) {
|
|
88
|
+
if (err instanceof require_request.HttpRequestError) throw err;
|
|
89
|
+
if (err instanceof require_request.TimeoutError) throw err;
|
|
90
|
+
if (require_isNetworkError.isNetworkError(err)) throw new require_request.HttpRequestError({
|
|
91
|
+
body,
|
|
92
|
+
cause: err,
|
|
93
|
+
url: url.toString()
|
|
94
|
+
});
|
|
95
|
+
throw err;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
//#endregion
|
|
100
|
+
exports.request = request;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { HttpRequestError, TimeoutError } from "../errors/request.cjs";
|
|
2
|
+
import { ILogger } from "../logger.cjs";
|
|
3
|
+
import { RetryOptions } from "./retry.cjs";
|
|
4
|
+
|
|
5
|
+
//#region src/utils/request.d.ts
|
|
6
|
+
type RequestErrorType = HttpRequestError | TimeoutError;
|
|
7
|
+
declare function request({
|
|
8
|
+
retryOptions,
|
|
9
|
+
logger,
|
|
10
|
+
...params
|
|
11
|
+
}: Parameters<typeof request_>[0] & {
|
|
12
|
+
retryOptions?: RetryOptions;
|
|
13
|
+
logger?: ILogger;
|
|
14
|
+
}): Promise<Response>;
|
|
15
|
+
declare function request_({
|
|
16
|
+
url,
|
|
17
|
+
body,
|
|
18
|
+
timeout,
|
|
19
|
+
fetchOptions
|
|
20
|
+
}: {
|
|
21
|
+
url: string | URL;
|
|
22
|
+
body?: unknown | undefined;
|
|
23
|
+
timeout?: number | undefined;
|
|
24
|
+
fetchOptions?: Omit<RequestInit, "body"> | undefined;
|
|
25
|
+
}): Promise<Response>;
|
|
26
|
+
//#endregion
|
|
27
|
+
export { RequestErrorType, request };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { HttpRequestError, TimeoutError } from "../errors/request.js";
|
|
2
|
+
import { ILogger } from "../logger.js";
|
|
3
|
+
import { RetryOptions } from "./retry.js";
|
|
4
|
+
|
|
5
|
+
//#region src/utils/request.d.ts
|
|
6
|
+
type RequestErrorType = HttpRequestError | TimeoutError;
|
|
7
|
+
declare function request({
|
|
8
|
+
retryOptions,
|
|
9
|
+
logger,
|
|
10
|
+
...params
|
|
11
|
+
}: Parameters<typeof request_>[0] & {
|
|
12
|
+
retryOptions?: RetryOptions;
|
|
13
|
+
logger?: ILogger;
|
|
14
|
+
}): Promise<Response>;
|
|
15
|
+
declare function request_({
|
|
16
|
+
url,
|
|
17
|
+
body,
|
|
18
|
+
timeout,
|
|
19
|
+
fetchOptions
|
|
20
|
+
}: {
|
|
21
|
+
url: string | URL;
|
|
22
|
+
body?: unknown | undefined;
|
|
23
|
+
timeout?: number | undefined;
|
|
24
|
+
fetchOptions?: Omit<RequestInit, "body"> | undefined;
|
|
25
|
+
}): Promise<Response>;
|
|
26
|
+
//#endregion
|
|
27
|
+
export { RequestErrorType, request };
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { HttpRequestError, TimeoutError } from "../errors/request.js";
|
|
2
|
+
import { isNetworkError } from "../errors/utils/isNetworkError.js";
|
|
3
|
+
import { toError } from "../errors/utils/toError.js";
|
|
4
|
+
import { mergeAbortSignals } from "./abortSignal.js";
|
|
5
|
+
import { withTimeout } from "./promise/withTimeout.js";
|
|
6
|
+
import { requestShouldRetry } from "./requestShouldRetry.js";
|
|
7
|
+
import { retry } from "@lifeomic/attempt";
|
|
8
|
+
|
|
9
|
+
//#region src/utils/request.ts
|
|
10
|
+
async function request({ retryOptions = { maxAttempts: 1 }, logger, ...params }) {
|
|
11
|
+
const overallStartTime = performance.now();
|
|
12
|
+
let totalNetworkTime = 0;
|
|
13
|
+
let attemptCount = 0;
|
|
14
|
+
const response = await retry(async (ctx) => {
|
|
15
|
+
attemptCount = ctx.attemptNum + 1;
|
|
16
|
+
const attemptStartTime = performance.now();
|
|
17
|
+
logger?.debug("Sending HTTP request", {
|
|
18
|
+
url: params.url.toString(),
|
|
19
|
+
method: params.fetchOptions?.method || "GET",
|
|
20
|
+
body: JSON.stringify(params.body),
|
|
21
|
+
attempt: ctx.attemptNum
|
|
22
|
+
});
|
|
23
|
+
const response$1 = await request_(params);
|
|
24
|
+
const attemptDuration = performance.now() - attemptStartTime;
|
|
25
|
+
totalNetworkTime += attemptDuration;
|
|
26
|
+
logger?.debug("HTTP request attempt completed", {
|
|
27
|
+
attempt: ctx.attemptNum,
|
|
28
|
+
attemptDuration: Math.floor(attemptDuration),
|
|
29
|
+
statusCode: response$1.status,
|
|
30
|
+
success: response$1.ok,
|
|
31
|
+
responseSize: response$1.headers.get("content-length")
|
|
32
|
+
});
|
|
33
|
+
return response$1;
|
|
34
|
+
}, {
|
|
35
|
+
...retryOptions,
|
|
36
|
+
handleError: (err, context) => {
|
|
37
|
+
logger?.error("HTTP request attempt failed", {
|
|
38
|
+
attempt: context.attemptNum,
|
|
39
|
+
errorMessage: toError(err).message
|
|
40
|
+
});
|
|
41
|
+
if (!requestShouldRetry(err) || params.fetchOptions?.signal?.reason === err) context.abort();
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
const overallDuration = performance.now() - overallStartTime;
|
|
45
|
+
const retryOverhead = overallDuration - totalNetworkTime;
|
|
46
|
+
logger?.[attemptCount > 1 ? "info" : "debug"]("HTTP request completed", {
|
|
47
|
+
statusCode: response.status,
|
|
48
|
+
success: response.ok,
|
|
49
|
+
totalDuration: Math.floor(overallDuration),
|
|
50
|
+
networkTime: Math.floor(totalNetworkTime),
|
|
51
|
+
retryOverhead: Math.floor(retryOverhead),
|
|
52
|
+
attempts: attemptCount,
|
|
53
|
+
responseSize: response.headers.get("content-length")
|
|
54
|
+
});
|
|
55
|
+
return response;
|
|
56
|
+
}
|
|
57
|
+
async function request_({ url, body, timeout = 1e4, fetchOptions }) {
|
|
58
|
+
const { headers: customHeaders, method, signal: signal_ } = fetchOptions ?? {};
|
|
59
|
+
try {
|
|
60
|
+
const headers = new Headers(customHeaders);
|
|
61
|
+
headers.set("Content-Type", "application/json");
|
|
62
|
+
const response = await withTimeout(({ signal }) => {
|
|
63
|
+
return fetch(url, {
|
|
64
|
+
...fetchOptions,
|
|
65
|
+
method,
|
|
66
|
+
headers,
|
|
67
|
+
body: JSON.stringify(body),
|
|
68
|
+
signal: mergeAbortSignals([signal_, timeout > 0 ? signal : null].filter(Boolean))
|
|
69
|
+
});
|
|
70
|
+
}, {
|
|
71
|
+
errorInstance: new TimeoutError({
|
|
72
|
+
body,
|
|
73
|
+
url: url.toString()
|
|
74
|
+
}),
|
|
75
|
+
timeout,
|
|
76
|
+
signal: true
|
|
77
|
+
});
|
|
78
|
+
if (!response.ok) throw new HttpRequestError({
|
|
79
|
+
body,
|
|
80
|
+
details: response.statusText,
|
|
81
|
+
headers: response.headers,
|
|
82
|
+
status: response.status,
|
|
83
|
+
url: url.toString()
|
|
84
|
+
});
|
|
85
|
+
return response;
|
|
86
|
+
} catch (err) {
|
|
87
|
+
if (err instanceof HttpRequestError) throw err;
|
|
88
|
+
if (err instanceof TimeoutError) throw err;
|
|
89
|
+
if (isNetworkError(err)) throw new HttpRequestError({
|
|
90
|
+
body,
|
|
91
|
+
cause: err,
|
|
92
|
+
url: url.toString()
|
|
93
|
+
});
|
|
94
|
+
throw err;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
//#endregion
|
|
99
|
+
export { request };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const require_request = require('../errors/request.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/utils/requestShouldRetry.ts
|
|
4
|
+
function requestShouldRetry(error) {
|
|
5
|
+
if (error instanceof require_request.HttpRequestError && error.status != null) {
|
|
6
|
+
if (error.status === 403) return true;
|
|
7
|
+
if (error.status === 408) return true;
|
|
8
|
+
if (error.status === 413) return true;
|
|
9
|
+
if (error.status === 429) return true;
|
|
10
|
+
if (error.status === 500) return true;
|
|
11
|
+
if (error.status === 502) return true;
|
|
12
|
+
if (error.status === 503) return true;
|
|
13
|
+
if (error.status === 504) return true;
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
exports.requestShouldRetry = requestShouldRetry;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { HttpRequestError } from "../errors/request.js";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/requestShouldRetry.ts
|
|
4
|
+
function requestShouldRetry(error) {
|
|
5
|
+
if (error instanceof HttpRequestError && error.status != null) {
|
|
6
|
+
if (error.status === 403) return true;
|
|
7
|
+
if (error.status === 408) return true;
|
|
8
|
+
if (error.status === 413) return true;
|
|
9
|
+
if (error.status === 429) return true;
|
|
10
|
+
if (error.status === 500) return true;
|
|
11
|
+
if (error.status === 502) return true;
|
|
12
|
+
if (error.status === 503) return true;
|
|
13
|
+
if (error.status === 504) return true;
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
export { requestShouldRetry };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/utils/retry.ts
|
|
3
|
+
const RETRY_CONFIGS = {
|
|
4
|
+
THIRTY_SECS_AGGRESSIVE: {
|
|
5
|
+
delay: 500,
|
|
6
|
+
minDelay: 500,
|
|
7
|
+
factor: 2,
|
|
8
|
+
maxAttempts: 6,
|
|
9
|
+
jitter: true
|
|
10
|
+
},
|
|
11
|
+
ONE_MIN_MODERATE: {
|
|
12
|
+
delay: 1e3,
|
|
13
|
+
minDelay: 1e3,
|
|
14
|
+
factor: 1.5,
|
|
15
|
+
maxAttempts: 8,
|
|
16
|
+
jitter: true
|
|
17
|
+
},
|
|
18
|
+
TWO_MINS_GRADUAL: {
|
|
19
|
+
delay: 500,
|
|
20
|
+
minDelay: 500,
|
|
21
|
+
factor: 1.5,
|
|
22
|
+
maxAttempts: 12,
|
|
23
|
+
jitter: true
|
|
24
|
+
},
|
|
25
|
+
FIVE_MINS_STEADY: {
|
|
26
|
+
delay: 2e3,
|
|
27
|
+
minDelay: 2e3,
|
|
28
|
+
factor: 1.4,
|
|
29
|
+
maxAttempts: 12,
|
|
30
|
+
jitter: true
|
|
31
|
+
},
|
|
32
|
+
THIRTY_MINS_PATIENT: {
|
|
33
|
+
delay: 5e3,
|
|
34
|
+
minDelay: 5e3,
|
|
35
|
+
factor: 1.3,
|
|
36
|
+
maxAttempts: 18,
|
|
37
|
+
jitter: true
|
|
38
|
+
},
|
|
39
|
+
TWO_HOURS_PERSISTENT: {
|
|
40
|
+
delay: 1e4,
|
|
41
|
+
minDelay: 1e4,
|
|
42
|
+
factor: 1.2,
|
|
43
|
+
maxAttempts: 27,
|
|
44
|
+
jitter: true
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Calculates how many attempts fit within a timeout budget.
|
|
49
|
+
*
|
|
50
|
+
* @returns number of attempts that fit within the timeout
|
|
51
|
+
*/
|
|
52
|
+
function calculateMaxAttempts(timeout, delay, factor) {
|
|
53
|
+
let totalDelay = 0;
|
|
54
|
+
let currentDelay = delay;
|
|
55
|
+
let attempts = 1;
|
|
56
|
+
while (totalDelay + currentDelay <= timeout) {
|
|
57
|
+
totalDelay += currentDelay;
|
|
58
|
+
attempts++;
|
|
59
|
+
currentDelay *= factor;
|
|
60
|
+
}
|
|
61
|
+
return Math.max(1, attempts);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
//#endregion
|
|
65
|
+
exports.RETRY_CONFIGS = RETRY_CONFIGS;
|
|
66
|
+
exports.calculateMaxAttempts = calculateMaxAttempts;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { AttemptOptions } from "@lifeomic/attempt";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/retry.d.ts
|
|
4
|
+
type RetryOptions = Partial<Pick<AttemptOptions<unknown>, "delay" | "initialDelay" | "minDelay" | "maxDelay" | "factor" | "maxAttempts" | "timeout" | "jitter" | "initialJitter">>;
|
|
5
|
+
declare const RETRY_CONFIGS: {
|
|
6
|
+
THIRTY_SECS_AGGRESSIVE: {
|
|
7
|
+
delay: number;
|
|
8
|
+
minDelay: number;
|
|
9
|
+
factor: number;
|
|
10
|
+
maxAttempts: number;
|
|
11
|
+
jitter: boolean;
|
|
12
|
+
};
|
|
13
|
+
ONE_MIN_MODERATE: {
|
|
14
|
+
delay: number;
|
|
15
|
+
minDelay: number;
|
|
16
|
+
factor: number;
|
|
17
|
+
maxAttempts: number;
|
|
18
|
+
jitter: boolean;
|
|
19
|
+
};
|
|
20
|
+
TWO_MINS_GRADUAL: {
|
|
21
|
+
delay: number;
|
|
22
|
+
minDelay: number;
|
|
23
|
+
factor: number;
|
|
24
|
+
maxAttempts: number;
|
|
25
|
+
jitter: boolean;
|
|
26
|
+
};
|
|
27
|
+
FIVE_MINS_STEADY: {
|
|
28
|
+
delay: number;
|
|
29
|
+
minDelay: number;
|
|
30
|
+
factor: number;
|
|
31
|
+
maxAttempts: number;
|
|
32
|
+
jitter: boolean;
|
|
33
|
+
};
|
|
34
|
+
THIRTY_MINS_PATIENT: {
|
|
35
|
+
delay: number;
|
|
36
|
+
minDelay: number;
|
|
37
|
+
factor: number;
|
|
38
|
+
maxAttempts: number;
|
|
39
|
+
jitter: boolean;
|
|
40
|
+
};
|
|
41
|
+
TWO_HOURS_PERSISTENT: {
|
|
42
|
+
delay: number;
|
|
43
|
+
minDelay: number;
|
|
44
|
+
factor: number;
|
|
45
|
+
maxAttempts: number;
|
|
46
|
+
jitter: boolean;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Calculates how many attempts fit within a timeout budget.
|
|
51
|
+
*
|
|
52
|
+
* @returns number of attempts that fit within the timeout
|
|
53
|
+
*/
|
|
54
|
+
declare function calculateMaxAttempts(timeout: number, delay: number, factor: number): number;
|
|
55
|
+
//#endregion
|
|
56
|
+
export { RETRY_CONFIGS, RetryOptions, calculateMaxAttempts };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { AttemptOptions } from "@lifeomic/attempt";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/retry.d.ts
|
|
4
|
+
type RetryOptions = Partial<Pick<AttemptOptions<unknown>, "delay" | "initialDelay" | "minDelay" | "maxDelay" | "factor" | "maxAttempts" | "timeout" | "jitter" | "initialJitter">>;
|
|
5
|
+
declare const RETRY_CONFIGS: {
|
|
6
|
+
THIRTY_SECS_AGGRESSIVE: {
|
|
7
|
+
delay: number;
|
|
8
|
+
minDelay: number;
|
|
9
|
+
factor: number;
|
|
10
|
+
maxAttempts: number;
|
|
11
|
+
jitter: boolean;
|
|
12
|
+
};
|
|
13
|
+
ONE_MIN_MODERATE: {
|
|
14
|
+
delay: number;
|
|
15
|
+
minDelay: number;
|
|
16
|
+
factor: number;
|
|
17
|
+
maxAttempts: number;
|
|
18
|
+
jitter: boolean;
|
|
19
|
+
};
|
|
20
|
+
TWO_MINS_GRADUAL: {
|
|
21
|
+
delay: number;
|
|
22
|
+
minDelay: number;
|
|
23
|
+
factor: number;
|
|
24
|
+
maxAttempts: number;
|
|
25
|
+
jitter: boolean;
|
|
26
|
+
};
|
|
27
|
+
FIVE_MINS_STEADY: {
|
|
28
|
+
delay: number;
|
|
29
|
+
minDelay: number;
|
|
30
|
+
factor: number;
|
|
31
|
+
maxAttempts: number;
|
|
32
|
+
jitter: boolean;
|
|
33
|
+
};
|
|
34
|
+
THIRTY_MINS_PATIENT: {
|
|
35
|
+
delay: number;
|
|
36
|
+
minDelay: number;
|
|
37
|
+
factor: number;
|
|
38
|
+
maxAttempts: number;
|
|
39
|
+
jitter: boolean;
|
|
40
|
+
};
|
|
41
|
+
TWO_HOURS_PERSISTENT: {
|
|
42
|
+
delay: number;
|
|
43
|
+
minDelay: number;
|
|
44
|
+
factor: number;
|
|
45
|
+
maxAttempts: number;
|
|
46
|
+
jitter: boolean;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Calculates how many attempts fit within a timeout budget.
|
|
51
|
+
*
|
|
52
|
+
* @returns number of attempts that fit within the timeout
|
|
53
|
+
*/
|
|
54
|
+
declare function calculateMaxAttempts(timeout: number, delay: number, factor: number): number;
|
|
55
|
+
//#endregion
|
|
56
|
+
export { RETRY_CONFIGS, RetryOptions, calculateMaxAttempts };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
//#region src/utils/retry.ts
|
|
2
|
+
const RETRY_CONFIGS = {
|
|
3
|
+
THIRTY_SECS_AGGRESSIVE: {
|
|
4
|
+
delay: 500,
|
|
5
|
+
minDelay: 500,
|
|
6
|
+
factor: 2,
|
|
7
|
+
maxAttempts: 6,
|
|
8
|
+
jitter: true
|
|
9
|
+
},
|
|
10
|
+
ONE_MIN_MODERATE: {
|
|
11
|
+
delay: 1e3,
|
|
12
|
+
minDelay: 1e3,
|
|
13
|
+
factor: 1.5,
|
|
14
|
+
maxAttempts: 8,
|
|
15
|
+
jitter: true
|
|
16
|
+
},
|
|
17
|
+
TWO_MINS_GRADUAL: {
|
|
18
|
+
delay: 500,
|
|
19
|
+
minDelay: 500,
|
|
20
|
+
factor: 1.5,
|
|
21
|
+
maxAttempts: 12,
|
|
22
|
+
jitter: true
|
|
23
|
+
},
|
|
24
|
+
FIVE_MINS_STEADY: {
|
|
25
|
+
delay: 2e3,
|
|
26
|
+
minDelay: 2e3,
|
|
27
|
+
factor: 1.4,
|
|
28
|
+
maxAttempts: 12,
|
|
29
|
+
jitter: true
|
|
30
|
+
},
|
|
31
|
+
THIRTY_MINS_PATIENT: {
|
|
32
|
+
delay: 5e3,
|
|
33
|
+
minDelay: 5e3,
|
|
34
|
+
factor: 1.3,
|
|
35
|
+
maxAttempts: 18,
|
|
36
|
+
jitter: true
|
|
37
|
+
},
|
|
38
|
+
TWO_HOURS_PERSISTENT: {
|
|
39
|
+
delay: 1e4,
|
|
40
|
+
minDelay: 1e4,
|
|
41
|
+
factor: 1.2,
|
|
42
|
+
maxAttempts: 27,
|
|
43
|
+
jitter: true
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Calculates how many attempts fit within a timeout budget.
|
|
48
|
+
*
|
|
49
|
+
* @returns number of attempts that fit within the timeout
|
|
50
|
+
*/
|
|
51
|
+
function calculateMaxAttempts(timeout, delay, factor) {
|
|
52
|
+
let totalDelay = 0;
|
|
53
|
+
let currentDelay = delay;
|
|
54
|
+
let attempts = 1;
|
|
55
|
+
while (totalDelay + currentDelay <= timeout) {
|
|
56
|
+
totalDelay += currentDelay;
|
|
57
|
+
attempts++;
|
|
58
|
+
currentDelay *= factor;
|
|
59
|
+
}
|
|
60
|
+
return Math.max(1, attempts);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
export { RETRY_CONFIGS, calculateMaxAttempts };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let _scure_base = require("@scure/base");
|
|
3
|
+
|
|
4
|
+
//#region src/utils/rpc-endpoint.ts
|
|
5
|
+
/**
|
|
6
|
+
* Parses URL with embedded credentials (http://user:pass@host) and extracts
|
|
7
|
+
* Authorization header. Returns cleaned URL without credentials.
|
|
8
|
+
*/
|
|
9
|
+
function parseUrlCredentials(urlString) {
|
|
10
|
+
const url = new URL(urlString);
|
|
11
|
+
const headers = {};
|
|
12
|
+
if (url.username || url.password) {
|
|
13
|
+
const credentials = `${url.username}:${url.password}`;
|
|
14
|
+
headers.Authorization = `Basic ${_scure_base.base64.encode(new TextEncoder().encode(credentials))}`;
|
|
15
|
+
url.username = "";
|
|
16
|
+
url.password = "";
|
|
17
|
+
return {
|
|
18
|
+
url: url.toString(),
|
|
19
|
+
headers
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
url: urlString,
|
|
24
|
+
headers
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Normalizes RpcEndpoint to RpcEndpointConfig, parsing credentials from URL if present.
|
|
29
|
+
*/
|
|
30
|
+
function normalizeRpcEndpoint(endpoint) {
|
|
31
|
+
const config = typeof endpoint === "string" ? { url: endpoint } : endpoint;
|
|
32
|
+
const { url, headers: parsedHeaders } = parseUrlCredentials(config.url);
|
|
33
|
+
return {
|
|
34
|
+
url,
|
|
35
|
+
headers: {
|
|
36
|
+
...parsedHeaders,
|
|
37
|
+
...config.headers
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Extracts plain URLs from RpcEndpoint array.
|
|
43
|
+
* Use this when passing RPC endpoints to external SDKs that only support URL strings.
|
|
44
|
+
*
|
|
45
|
+
* @note This strips credentials from URLs. For credential-containing URLs,
|
|
46
|
+
* use normalizeRpcEndpoint() to properly convert them to auth headers.
|
|
47
|
+
*/
|
|
48
|
+
function extractRpcUrls(endpoints) {
|
|
49
|
+
return endpoints.map((endpoint) => {
|
|
50
|
+
return parseUrlCredentials(typeof endpoint === "string" ? endpoint : endpoint.url).url;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
//#endregion
|
|
55
|
+
exports.extractRpcUrls = extractRpcUrls;
|
|
56
|
+
exports.normalizeRpcEndpoint = normalizeRpcEndpoint;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/utils/rpc-endpoint.d.ts
|
|
2
|
+
interface RpcEndpointConfig {
|
|
3
|
+
url: string;
|
|
4
|
+
headers?: Record<string, string>;
|
|
5
|
+
}
|
|
6
|
+
type RpcEndpoint = string | RpcEndpointConfig;
|
|
7
|
+
/**
|
|
8
|
+
* Normalizes RpcEndpoint to RpcEndpointConfig, parsing credentials from URL if present.
|
|
9
|
+
*/
|
|
10
|
+
declare function normalizeRpcEndpoint(endpoint: RpcEndpoint): RpcEndpointConfig;
|
|
11
|
+
/**
|
|
12
|
+
* Extracts plain URLs from RpcEndpoint array.
|
|
13
|
+
* Use this when passing RPC endpoints to external SDKs that only support URL strings.
|
|
14
|
+
*
|
|
15
|
+
* @note This strips credentials from URLs. For credential-containing URLs,
|
|
16
|
+
* use normalizeRpcEndpoint() to properly convert them to auth headers.
|
|
17
|
+
*/
|
|
18
|
+
declare function extractRpcUrls(endpoints: RpcEndpoint[]): string[];
|
|
19
|
+
//#endregion
|
|
20
|
+
export { RpcEndpoint, RpcEndpointConfig, extractRpcUrls, normalizeRpcEndpoint };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/utils/rpc-endpoint.d.ts
|
|
2
|
+
interface RpcEndpointConfig {
|
|
3
|
+
url: string;
|
|
4
|
+
headers?: Record<string, string>;
|
|
5
|
+
}
|
|
6
|
+
type RpcEndpoint = string | RpcEndpointConfig;
|
|
7
|
+
/**
|
|
8
|
+
* Normalizes RpcEndpoint to RpcEndpointConfig, parsing credentials from URL if present.
|
|
9
|
+
*/
|
|
10
|
+
declare function normalizeRpcEndpoint(endpoint: RpcEndpoint): RpcEndpointConfig;
|
|
11
|
+
/**
|
|
12
|
+
* Extracts plain URLs from RpcEndpoint array.
|
|
13
|
+
* Use this when passing RPC endpoints to external SDKs that only support URL strings.
|
|
14
|
+
*
|
|
15
|
+
* @note This strips credentials from URLs. For credential-containing URLs,
|
|
16
|
+
* use normalizeRpcEndpoint() to properly convert them to auth headers.
|
|
17
|
+
*/
|
|
18
|
+
declare function extractRpcUrls(endpoints: RpcEndpoint[]): string[];
|
|
19
|
+
//#endregion
|
|
20
|
+
export { RpcEndpoint, RpcEndpointConfig, extractRpcUrls, normalizeRpcEndpoint };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { base64 } from "@scure/base";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/rpc-endpoint.ts
|
|
4
|
+
/**
|
|
5
|
+
* Parses URL with embedded credentials (http://user:pass@host) and extracts
|
|
6
|
+
* Authorization header. Returns cleaned URL without credentials.
|
|
7
|
+
*/
|
|
8
|
+
function parseUrlCredentials(urlString) {
|
|
9
|
+
const url = new URL(urlString);
|
|
10
|
+
const headers = {};
|
|
11
|
+
if (url.username || url.password) {
|
|
12
|
+
const credentials = `${url.username}:${url.password}`;
|
|
13
|
+
headers.Authorization = `Basic ${base64.encode(new TextEncoder().encode(credentials))}`;
|
|
14
|
+
url.username = "";
|
|
15
|
+
url.password = "";
|
|
16
|
+
return {
|
|
17
|
+
url: url.toString(),
|
|
18
|
+
headers
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
url: urlString,
|
|
23
|
+
headers
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Normalizes RpcEndpoint to RpcEndpointConfig, parsing credentials from URL if present.
|
|
28
|
+
*/
|
|
29
|
+
function normalizeRpcEndpoint(endpoint) {
|
|
30
|
+
const config = typeof endpoint === "string" ? { url: endpoint } : endpoint;
|
|
31
|
+
const { url, headers: parsedHeaders } = parseUrlCredentials(config.url);
|
|
32
|
+
return {
|
|
33
|
+
url,
|
|
34
|
+
headers: {
|
|
35
|
+
...parsedHeaders,
|
|
36
|
+
...config.headers
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Extracts plain URLs from RpcEndpoint array.
|
|
42
|
+
* Use this when passing RPC endpoints to external SDKs that only support URL strings.
|
|
43
|
+
*
|
|
44
|
+
* @note This strips credentials from URLs. For credential-containing URLs,
|
|
45
|
+
* use normalizeRpcEndpoint() to properly convert them to auth headers.
|
|
46
|
+
*/
|
|
47
|
+
function extractRpcUrls(endpoints) {
|
|
48
|
+
return endpoints.map((endpoint) => {
|
|
49
|
+
return parseUrlCredentials(typeof endpoint === "string" ? endpoint : endpoint.url).url;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
//#endregion
|
|
54
|
+
export { extractRpcUrls, normalizeRpcEndpoint };
|