@metatime/pinning-service
Version:
Pinning service that is built on Google Cloud Storage on top of IPFS.
57 lines (56 loc) • 2.19 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const bucket_helper_1 = __importDefault(require("./bucket-helper"));
const ipfs_http_client_1 = require("ipfs-http-client");
const operation_error_1 = __importDefault(require("../utils/operation-error"));
/**
* A class that pins asset to GCP storage and IPFS client
* @class Pin
* @implements {IPin} - Pin class interface
* @property {IPFSHTTPClient} client - Ipfs client options
*/
class Pin extends bucket_helper_1.default {
/** @private */
client;
/**
* @param {string} _credentials - Google Cloud Storage credentials
* @param {object} _clientOptions - IPFS client options
*/
constructor(_baseUri, _credentials, _clientOptions) {
super(_baseUri, _credentials);
this.client = (0, ipfs_http_client_1.create)(_clientOptions);
}
/**
* Pins asset to GCP storage on top of IPFS
* @param {string} bucketName - Google Cloud Storage bucket name
* @param {string} filePath - Path in the Google Cloud Storage bucket
* @param {string} data - Object or buffer data of file
* @returns {any} Result of pinned asset
*/
async pinAsset(bucketName, filePath, data) {
try {
const isOnline = this.client.isOnline();
if (!isOnline) {
const msg = `[pinAsset]: Error occured while trying to pin asset...\nDetails: IPFS client is not reachable!`;
throw new operation_error_1.default(msg, 1);
}
const fileUri = await super.uploadFileToBucket(bucketName, filePath, data);
if (!fileUri) {
return null;
}
const { cid } = await this.client.add(data);
return {
cid,
fileUri
};
}
catch (err) {
const msg = `[pinAsset]: Error occured while trying to pin asset...\nDetails: ${err}`;
throw new operation_error_1.default(msg, -1);
}
}
}
exports.default = Pin;