@stoar/cli
Version:
CLI tool for STOAR - decentralized file storage on Arweave
63 lines • 2.31 kB
JavaScript
import { Bundle } from '@dha-team/arbundles';
/**
* Parse an ANS-104 bundle and extract data items
*/
export async function parseBundle(bundleData) {
try {
// Create bundle instance from binary data
const bundle = new Bundle(Buffer.from(bundleData));
const items = bundle.items.map((item) => {
// Convert tags to object format
const tags = {};
let fileName;
let contentType;
for (const tag of item.tags || []) {
const name = typeof tag.name === 'string' ? tag.name : new TextDecoder().decode(tag.name);
const value = typeof tag.value === 'string' ? tag.value : new TextDecoder().decode(tag.value);
tags[name] = value;
// Extract common fields
if (name === 'File-Name') {
fileName = value;
}
else if (name === 'Content-Type') {
contentType = value;
}
}
return {
id: item.id,
owner: item.owner,
size: item.data?.length || 0,
tags,
fileName,
contentType
};
});
return {
id: 'bundle', // Bundle ID is the transaction ID, not stored in the bundle itself
items,
totalItems: items.length
};
}
catch (error) {
throw new Error(`Failed to parse bundle: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Fetch and parse a bundle transaction from Arweave
*/
export async function fetchAndParseBundle(transactionId, gateway = 'https://arweave.net') {
try {
// Fetch bundle data
const response = await fetch(`${gateway}/${transactionId}`);
if (!response.ok) {
throw new Error(`Failed to fetch bundle: ${response.status} ${response.statusText}`);
}
const arrayBuffer = await response.arrayBuffer();
const bundleData = new Uint8Array(arrayBuffer);
return await parseBundle(bundleData);
}
catch (error) {
throw new Error(`Failed to fetch bundle: ${error instanceof Error ? error.message : String(error)}`);
}
}
//# sourceMappingURL=bundle.js.map