theseus/bundled-addons/pithos/core/space.js

30 lines
1.2 KiB
JavaScript
Raw Normal View History

// Free space on the Sia account behind this s3d, as the indexer reports it.
// The key reading, BLAKE2b-256 and request signing live in one place
// (sia-account.js, blake2b.js); this keeps the names the space route and its
// tests use.
import { readConnection, signRequest, accountInfo } from './sia-account.js';
export { blake2b256 } from './blake2b.js';
export const signIndexerUrl = (appKey, method, endpointUrl, validUntilSec) => signRequest(appKey, method, endpointUrl, validUntilSec);
export async function readAppKey(dataDir) {
const c = await readConnection(dataDir).catch(() => null);
return c?.appKey && c.indexerUrl ? { appKey: c.appKey, indexerUrl: c.indexerUrl } : null;
}
// { maxPinnedData, pinnedData, remainingStorage, indexer } in bytes, or null
// when this s3d is not connected. Throws when the indexer does not answer.
export async function accountSpace(dataDir, { fetchImpl = fetch } = {}) {
const a = await accountInfo(dataDir, { fetchImpl, timeoutMs: 15_000 });
if (!a.connected) return null;
if (a.error) throw new Error(a.error);
return {
maxPinnedData: Number(a.maxPinnedData),
pinnedData: Number(a.pinnedData),
remainingStorage: Number(a.remainingStorage),
indexer: new URL(a.indexerUrl).host,
};
}