186 lines
6.6 KiB
JavaScript
186 lines
6.6 KiB
JavaScript
'use strict';
|
|
|
|
var rpcApi = require('@solana/rpc-api');
|
|
var rpcSpec = require('@solana/rpc-spec');
|
|
var errors = require('@solana/errors');
|
|
var functional = require('@solana/functional');
|
|
var rpcTransportHttp = require('@solana/rpc-transport-http');
|
|
var fastStableStringify = require('@solana/fast-stable-stringify');
|
|
|
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
|
|
var fastStableStringify__default = /*#__PURE__*/_interopDefault(fastStableStringify);
|
|
|
|
// src/index.ts
|
|
function createSolanaJsonRpcIntegerOverflowError(methodName, keyPath, value) {
|
|
let argumentLabel = "";
|
|
if (typeof keyPath[0] === "number") {
|
|
const argPosition = keyPath[0] + 1;
|
|
const lastDigit = argPosition % 10;
|
|
const lastTwoDigits = argPosition % 100;
|
|
if (lastDigit == 1 && lastTwoDigits != 11) {
|
|
argumentLabel = argPosition + "st";
|
|
} else if (lastDigit == 2 && lastTwoDigits != 12) {
|
|
argumentLabel = argPosition + "nd";
|
|
} else if (lastDigit == 3 && lastTwoDigits != 13) {
|
|
argumentLabel = argPosition + "rd";
|
|
} else {
|
|
argumentLabel = argPosition + "th";
|
|
}
|
|
} else {
|
|
argumentLabel = `\`${keyPath[0].toString()}\``;
|
|
}
|
|
const path = keyPath.length > 1 ? keyPath.slice(1).map((pathPart) => typeof pathPart === "number" ? `[${pathPart}]` : pathPart).join(".") : void 0;
|
|
const error = new errors.SolanaError(errors.SOLANA_ERROR__RPC__INTEGER_OVERFLOW, {
|
|
argumentLabel,
|
|
keyPath,
|
|
methodName,
|
|
optionalPathLabel: path ? ` at path \`${path}\`` : "",
|
|
value,
|
|
...path !== void 0 ? { path } : void 0
|
|
});
|
|
errors.safeCaptureStackTrace(error, createSolanaJsonRpcIntegerOverflowError);
|
|
return error;
|
|
}
|
|
|
|
// src/rpc-default-config.ts
|
|
var DEFAULT_RPC_CONFIG = {
|
|
defaultCommitment: "confirmed",
|
|
onIntegerOverflow(request, keyPath, value) {
|
|
throw createSolanaJsonRpcIntegerOverflowError(request.methodName, keyPath, value);
|
|
}
|
|
};
|
|
|
|
// src/rpc-request-coalescer.ts
|
|
var EXPLICIT_ABORT_TOKEN;
|
|
function createExplicitAbortToken() {
|
|
return process.env.NODE_ENV !== "production" ? {
|
|
EXPLICIT_ABORT_TOKEN: "This object is thrown from the request that underlies a series of coalesced requests when the last request in that series aborts"
|
|
} : {};
|
|
}
|
|
function getRpcTransportWithRequestCoalescing(transport, getDeduplicationKey) {
|
|
let coalescedRequestsByDeduplicationKey;
|
|
return async function makeCoalescedHttpRequest(request) {
|
|
const { payload, signal } = request;
|
|
const deduplicationKey = getDeduplicationKey(payload);
|
|
if (deduplicationKey === void 0) {
|
|
return await transport(request);
|
|
}
|
|
if (!coalescedRequestsByDeduplicationKey) {
|
|
queueMicrotask(() => {
|
|
coalescedRequestsByDeduplicationKey = void 0;
|
|
});
|
|
coalescedRequestsByDeduplicationKey = {};
|
|
}
|
|
if (coalescedRequestsByDeduplicationKey[deduplicationKey] == null) {
|
|
const abortController = new AbortController();
|
|
const responsePromise = (async () => {
|
|
try {
|
|
return await transport({
|
|
...request,
|
|
signal: abortController.signal
|
|
});
|
|
} catch (e) {
|
|
if (e === (EXPLICIT_ABORT_TOKEN ||= createExplicitAbortToken())) {
|
|
return;
|
|
}
|
|
throw e;
|
|
}
|
|
})();
|
|
coalescedRequestsByDeduplicationKey[deduplicationKey] = {
|
|
abortController,
|
|
numConsumers: 0,
|
|
responsePromise
|
|
};
|
|
}
|
|
const coalescedRequest = coalescedRequestsByDeduplicationKey[deduplicationKey];
|
|
coalescedRequest.numConsumers++;
|
|
if (signal) {
|
|
const responsePromise = coalescedRequest.responsePromise;
|
|
return await new Promise((resolve, reject) => {
|
|
const handleAbort = (e) => {
|
|
signal.removeEventListener("abort", handleAbort);
|
|
coalescedRequest.numConsumers -= 1;
|
|
queueMicrotask(() => {
|
|
if (coalescedRequest.numConsumers === 0) {
|
|
const abortController = coalescedRequest.abortController;
|
|
abortController.abort(EXPLICIT_ABORT_TOKEN ||= createExplicitAbortToken());
|
|
}
|
|
});
|
|
reject(e.target.reason);
|
|
};
|
|
signal.addEventListener("abort", handleAbort);
|
|
responsePromise.then(resolve).catch(reject).finally(() => {
|
|
signal.removeEventListener("abort", handleAbort);
|
|
});
|
|
});
|
|
} else {
|
|
return await coalescedRequest.responsePromise;
|
|
}
|
|
};
|
|
}
|
|
function getSolanaRpcPayloadDeduplicationKey(payload) {
|
|
return rpcSpec.isJsonRpcPayload(payload) ? fastStableStringify__default.default([payload.method, payload.params]) : void 0;
|
|
}
|
|
|
|
// src/rpc-transport.ts
|
|
function normalizeHeaders(headers) {
|
|
const out = {};
|
|
for (const headerName in headers) {
|
|
out[headerName.toLowerCase()] = headers[headerName];
|
|
}
|
|
return out;
|
|
}
|
|
function createDefaultRpcTransport(config) {
|
|
return functional.pipe(
|
|
rpcTransportHttp.createHttpTransportForSolanaRpc({
|
|
...config,
|
|
headers: {
|
|
...{
|
|
// Keep these headers lowercase so they will be overriden by any user-supplied headers below.
|
|
"accept-encoding": (
|
|
// Natively supported by Node LTS v20.18.0 and above.
|
|
"br,gzip,deflate"
|
|
)
|
|
// Brotli, gzip, and Deflate, in that order.
|
|
},
|
|
...config.headers ? normalizeHeaders(config.headers) : void 0,
|
|
...{
|
|
// Keep these headers lowercase so they will override any user-supplied headers above.
|
|
"solana-client": `js/${"2.0.0"}`
|
|
}
|
|
}
|
|
}),
|
|
(transport) => getRpcTransportWithRequestCoalescing(transport, getSolanaRpcPayloadDeduplicationKey)
|
|
);
|
|
}
|
|
|
|
// src/rpc.ts
|
|
function createSolanaRpc(clusterUrl, config) {
|
|
return createSolanaRpcFromTransport(createDefaultRpcTransport({ url: clusterUrl, ...config }));
|
|
}
|
|
function createSolanaRpcFromTransport(transport) {
|
|
return rpcSpec.createRpc({
|
|
api: rpcApi.createSolanaRpcApi(DEFAULT_RPC_CONFIG),
|
|
transport
|
|
});
|
|
}
|
|
|
|
exports.DEFAULT_RPC_CONFIG = DEFAULT_RPC_CONFIG;
|
|
exports.createDefaultRpcTransport = createDefaultRpcTransport;
|
|
exports.createSolanaRpc = createSolanaRpc;
|
|
exports.createSolanaRpcFromTransport = createSolanaRpcFromTransport;
|
|
Object.keys(rpcApi).forEach(function (k) {
|
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
enumerable: true,
|
|
get: function () { return rpcApi[k]; }
|
|
});
|
|
});
|
|
Object.keys(rpcSpec).forEach(function (k) {
|
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
enumerable: true,
|
|
get: function () { return rpcSpec[k]; }
|
|
});
|
|
});
|
|
//# sourceMappingURL=index.node.cjs.map
|
|
//# sourceMappingURL=index.node.cjs.map
|