@inkwell.ar/sdk
Version:
SDK for interacting with the Inkwell Blog CRUD AO process using aoconnect for deployment and interactions
112 lines • 3.93 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TRANSACTION_QUERY = exports.arweave = void 0;
exports.sleep = sleep;
exports.getArweave = getArweave;
exports.retryWithDelay = retryWithDelay;
exports.pollForProcessSpawn = pollForProcessSpawn;
const arweave_1 = __importDefault(require("arweave"));
/**
* Initializes a default Arweave instance.
*/
exports.arweave = arweave_1.default.init({
host: 'arweave.net',
port: 443,
protocol: 'https',
});
async function sleep(delay = 3000) {
return new Promise((resolve) => setTimeout(resolve, delay));
}
/**
* Parses a gateway URL and returns an object containing the host, port, and protocol.
*
* @param url - The gateway URL to be parsed.
* @returns An object with the host, port, and protocol of the URL.
*/
function parseGatewayUrl(url) {
const parsedUrl = new URL(url);
return {
host: parsedUrl.hostname,
port: parsedUrl.port ? Number.parseInt(parsedUrl.port, 10) : 443,
protocol: parsedUrl.protocol.replace(':', ''),
};
}
/**
* Initializes an Arweave instance with a custom gateway.
*
* @param gateway - The gateway URL to connect to.
* @returns An Arweave instance configured with the provided gateway.
*/
function getArweave(gateway) {
try {
if (!gateway)
return exports.arweave;
const { host, port, protocol } = parseGatewayUrl(gateway);
return arweave_1.default.init({ host, port, protocol });
}
catch {
return exports.arweave;
}
}
/**
* Retries a given function up to a maximum number of attempts.
* @param fn - The asynchronous function to retry, which should return a Promise.
* @param maxAttempts - The maximum number of attempts to make.
* @param initialDelay - The delay between attempts in milliseconds.
* @param getDelay - A function that returns the delay for a given attempt.
* @return A Promise that resolves with the result of the function or rejects after all attempts fail.
*/
async function retryWithDelay(fn, maxAttempts = 3, initialDelay = 1000, getDelay = () => initialDelay) {
let attempts = 0;
const attempt = async () => {
try {
return await fn();
}
catch (error) {
attempts += 1;
if (attempts < maxAttempts) {
const currentDelay = getDelay(attempts);
// console.log(`Attempt ${attempts} failed, retrying...`)
return new Promise((resolve) => setTimeout(() => resolve(attempt()), currentDelay));
}
else {
throw error;
}
}
};
return attempt();
}
exports.TRANSACTION_QUERY = `query ($ids: [ID!]!) {
transactions(ids: $ids) {
edges {
node {
id
}
}
}
}`;
async function pollForProcessSpawn({ processId, gatewayUrl, options = {}, }) {
const { maxAttempts = 10, initialDelayMs = 3000, backoffFactor = 1.5, } = options;
const arweave = getArweave(gatewayUrl);
const queryTransaction = async () => {
const response = await arweave.api.post('/graphql', {
query: exports.TRANSACTION_QUERY,
variables: { ids: [processId] },
});
const transaction = response?.data?.data?.transactions?.edges?.[0]?.node;
if (!transaction) {
throw new Error('Transaction not found');
}
return transaction;
};
try {
await retryWithDelay(queryTransaction, maxAttempts, initialDelayMs, (attempt) => initialDelayMs * Math.pow(backoffFactor, attempt - 1));
}
catch {
throw new Error(`Failed to find process ${processId} after ${maxAttempts} attempts. The process may still be spawning.`);
}
}
//# sourceMappingURL=utils.js.map