@ethersphere/swarm-cli
Version:
CLI tool for Bee
55 lines (54 loc) • 1.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeBzzAddress = exports.BzzAddress = void 0;
const bee_js_1 = require("@ethersphere/bee-js");
const error_1 = require("./error");
class BzzAddress {
constructor(url) {
Object.defineProperty(this, "hash", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "path", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
if (url.startsWith('bzz://')) {
url = url.slice(6);
}
if (url.includes('//')) {
throw new error_1.CommandLineError('Invalid BZZ path: cannot contain multiple continuous slashes');
}
const parts = url.split('/');
this.hash = parts[0].toLowerCase();
if (this.hash.startsWith('0x')) {
this.hash = this.hash.slice(2);
}
if (!/[a-z0-9]{64,128}/.test(this.hash)) {
throw new error_1.CommandLineError('Invalid BZZ hash: expected 64 or 128 long hexadecimal hash');
}
const pathParts = parts.slice(1);
this.path = pathParts.length ? pathParts.join('/') : null;
}
}
exports.BzzAddress = BzzAddress;
async function makeBzzAddress(bee, url) {
const address = new BzzAddress(url);
try {
const manifest = await bee_js_1.MantarayNode.unmarshal(bee, address.hash);
await manifest.loadRecursively(bee);
const resolvedFeed = await manifest.resolveFeed(bee);
resolvedFeed.ifPresent(feed => {
address.hash = feed.payload.toHex();
});
return address;
}
catch {
return address;
}
}
exports.makeBzzAddress = makeBzzAddress;