@rsksmart/rif-storage-pinning
Version:
Application for providing your storage space to other to use in exchange of RIF Tokens
52 lines (51 loc) • 2.06 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProviderManager = void 0;
const ipfs_1 = require("./ipfs");
/**
* Provider which redirects the pin/unpin requests to the correct provider
* based on the structure of the hash.
*/
class ProviderManager {
register(provider) {
if (provider instanceof ipfs_1.IpfsProvider) {
this.ipfs = provider;
}
}
pin(hash, expectedSize, agreementReference) {
return __awaiter(this, void 0, void 0, function* () {
if (hash.startsWith('/ipfs/')) {
if (!this.ipfs) {
throw new Error('IPFS provider was not registered!');
}
yield this.ipfs.pin(hash, expectedSize, agreementReference);
}
else {
throw new Error(`Unknown type of hash ${hash}`);
}
});
}
unpin(hash) {
return __awaiter(this, void 0, void 0, function* () {
if (hash.startsWith('/ipfs/')) {
if (!this.ipfs) {
throw new Error('IPFS provider was not registered!');
}
yield this.ipfs.unpin(hash);
}
else {
throw new Error(`Unknown type of hash ${hash}`);
}
});
}
}
exports.ProviderManager = ProviderManager;