dwnpm
Version:
Decentralized Registry Package Manager (DRPM) helps developers publish, install, find and manage Decentralized Packages (DPKs) published to Decentralized Web Nodes (DWNs). DRPM does this by looking up a Decentralized Identifier (DID) to find its DID docum
71 lines • 2.61 kB
JavaScript
import { Logger } from '../utils/logger.js';
import { secureProfileContext } from '../utils/misc.js';
import { DhtProfile } from './utils/dht.js';
import { Profile } from './profile.js';
import { WebProfile } from './utils/web.js';
import { DRegistryPackageManagerError } from '../cli/commands/error.js';
export class DWeb5 {
static connection;
static isConnected() {
return this.connection !== undefined;
}
static async connect({ name, verbose = false }) {
try {
if (this.isConnected()) {
return this.connection;
}
const profile = new Profile(name);
const { web, dht } = await profile.load();
const data = name === 'web' ? web : dht;
if (verbose) {
Logger.info(`Connecting with ${name} profile: ${secureProfileContext(data)}`);
}
if (name === 'web') {
this.connection = await WebProfile.connect(data);
}
if (name === 'dht') {
this.connection = await DhtProfile.connect(data);
}
if (!this.isConnected()) {
throw new DRegistryPackageManagerError(`DWeb5: Current ${name} profile not found`);
}
if (verbose) {
Logger.info(`Connected ${this.connection.did} to Web5 with ${name} profile context`);
}
return this.connection;
}
catch (error) {
Logger.error(error);
process.exit(1);
}
}
static connectSync(verbose = false) {
try {
if (this.isConnected()) {
return this.connection;
}
const { name, web, dht } = Profile.loadStaticSync();
const data = name === 'web' ? web : dht;
if (verbose) {
Logger.info(`Connecting with ${name} profile: ${secureProfileContext(data)}`);
}
if (name === 'web') {
WebProfile.connect(data)
.then(connection => this.connection = connection);
}
if (name === 'dht') {
DhtProfile.connect(data)
.then(connection => this.connection = connection);
}
if (!this.isConnected()) {
throw new DRegistryPackageManagerError(`DWeb5: Current ${name} profile not found`);
}
return this.connection;
}
catch (error) {
Logger.error(error);
process.exit(1);
}
}
}
//# sourceMappingURL=dweb5.js.map