morphit/node_modules/@beblurt/dblurt/lib/client.js
Morphit Team 24240cef4c
Some checks failed
morphit-release / Build + publish release tarball (push) Has been cancelled
Reputation flags clearable across all four signals; names and avatars stop vanishing
2026-07-23 14:28:04 -07:00

164 lines
7.9 KiB
JavaScript

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Client = exports.DEFAULT_ADDRESS_PREFIX = exports.DEFAULT_CHAIN_ID = exports.VERSION = void 0;
/**
* @file Blurt RPC client implementation.
* @author BeBlurt <https://beblurt.com/@beblurt>
* @description adaptation from Johan Nordberg <code@johan-nordberg.com> RPC client implementation.
* @license
* Copyright (c) 2017 Johan Nordberg. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistribution of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* You acknowledge that this software is not designed, licensed or intended for use
* in the design, construction, operation or maintenance of any military facility.
*/
const assert = __importStar(require("assert"));
const version_1 = __importDefault(require("./version"));
const account_history_1 = require("./helpers/account_history");
const blockchain_1 = require("./helpers/blockchain");
const broadcast_1 = require("./helpers/broadcast");
const index_browser_1 = require("./index-browser");
const database_1 = require("./helpers/database");
const nexus_1 = require("./helpers/nexus");
const read_models_1 = require("./helpers/read_models");
const tools_1 = require("./helpers/tools");
const transaction_status_1 = require("./helpers/transaction_status");
const core_rpc_transport_1 = require("./transports/core_rpc_transport");
const legacy_rpc_transport_1 = require("./transports/legacy_rpc_transport");
/** Library version. */
exports.VERSION = version_1.default;
/** Main Blurt network chain id */
exports.DEFAULT_CHAIN_ID = Buffer.from('cd8d90f29ae273abec3eaa7731e25934c63eb654d55080caff2ebb7f5df6381f', 'hex');
/** Main Blurt network address prefix */
exports.DEFAULT_ADDRESS_PREFIX = 'BLT';
/** Default backoff function. ```min(tries*10^2, 10 seconds)``` */
const defaultBackoff = (tries) => Math.min(Math.pow(tries * 10, 2), 10 * 1000);
/**
* Main SDK client for Blurt Layer 1 RPC, Nexus/Bridge social views and helper APIs.
*
* A client owns endpoint failover, timeout/backoff settings and helper namespaces.
* Use `condenser`, `database`, `accountHistory` and `blockchain` for Layer 1
* reads, `nexus` for indexed Layer 2 social views, `read` for composed read
* models, and `broadcast` only for explicit signing/broadcast workflows.
*
* Can be used in Node.js >=18 and modern browsers with native `fetch` and
* `AbortController`. Also see {@link ClientOptions}.
*/
class Client {
/**
* @param address The address to the Blurt RPC server,
* e.g. `https://rpc.blurt.blog`. or [`https://rpc.blurt.blog`, `https://another.api.com`]
* @param options Client options.
*/
constructor(address, options = {}) {
this.currentAddress = Array.isArray(address) ? address[0] ? address[0] : 'https://rpc.blurt.blog' : address;
this.address = address;
this.options = options;
this.chainId = options.chainId ? Buffer.from(options.chainId, 'hex') : exports.DEFAULT_CHAIN_ID;
assert.strictEqual(this.chainId.length, 32, 'invalid chain id');
this.addressPrefix = options.addressPrefix || exports.DEFAULT_ADDRESS_PREFIX;
this.timeout = options.timeout !== undefined ? options.timeout : 60 * 1000;
this.backoff = options.backoff || defaultBackoff;
this.failoverThreshold = options.failoverThreshold !== undefined ? options.failoverThreshold : 3;
this.consoleOnFailover = options.consoleOnFailover || false;
this.accountHistory = new account_history_1.AccountHistoryAPI(this);
this.blockchain = new blockchain_1.Blockchain(this);
this.broadcast = new broadcast_1.BroadcastAPI(this);
this.condenser = new index_browser_1.CondenserAPI(this);
this.database = new database_1.DatabaseAPI(this);
this.nexus = new nexus_1.Nexus(this);
this.read = new read_models_1.ReadModels(this);
this.tools = new tools_1.Tools(this);
this.transactionStatus = new transaction_status_1.TransactionStatusAPI(this);
this.transport = this.createTransport();
}
/**
* Make a RPC call to the server.
*
* @param api The API to call, e.g. `database_api`.
* @param method The API method, e.g. `get_dynamic_global_properties`.
* @param params Array of parameters to pass to the method, optional.
*
*/
async call(api, method, params = []) {
const result = await this.transport.call(api, method, params);
const currentAddress = this.transport.getCurrentAddress();
if (currentAddress !== this.currentAddress) {
this.currentAddress = currentAddress;
}
return result;
}
createTransport() {
const base = {
address: this.address,
backoff: this.backoff,
currentAddress: this.currentAddress,
failoverThreshold: this.failoverThreshold,
options: this.options,
timeout: this.timeout
};
if (this.options.rpcTransport === 'core') {
return new core_rpc_transport_1.CoreRpcTransport({
...base,
coreClient: this.options.coreClient,
coreModule: this.options.coreModule,
nodeSelectionStrategy: this.options.nodeSelectionStrategy
});
}
return new legacy_rpc_transport_1.LegacyRpcTransport({
...base,
consoleOnFailover: this.consoleOnFailover
});
}
}
exports.Client = Client;