@celo/network-utils
Version:
Utilities for fetching static information about the Celo network
50 lines • 2.71 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GoogleStorageUtils = void 0;
const cross_fetch_1 = __importDefault(require("cross-fetch"));
const debug_1 = __importDefault(require("debug"));
const debug = (0, debug_1.default)('network-utils:google-storage-utils');
const TAG = 'google-storage-utils';
class GoogleStorageUtils {
static fetchFileFromGoogleStorage(bucketName, fileName) {
return __awaiter(this, void 0, void 0, function* () {
debug(`google-storage-utils@fetchFileFromGoogleStorage`, `Downloading file ${fileName} from ${bucketName}...`);
const url = GoogleStorageUtils.constructGoogleStorageUrl(bucketName, fileName);
let response = null;
try {
response = yield (0, cross_fetch_1.default)(url);
}
catch (error) {
debug(`google-storage-utils@fetchFileFromGoogleStorage`, `${TAG} Fetch failed with error "${error}"`);
throw error;
}
debug(`google-storage-utils@fetchFileFromGoogleStorage`, `${TAG} response for ${url} is ${JSON.stringify(response)}`);
if (response.status >= 400) {
debug(`google-storage-utils@fetchFileFromGoogleStorage`, `Failed to fetch data`);
throw new Error(yield response.text());
}
else {
debug(`google-storage-utils@fetchFileFromGoogleStorage`, `Successfully fetched data`);
return response.text();
}
});
}
// Source: https://cloud.google.com/storage/docs/json_api/
static constructGoogleStorageUrl(bucketName, fileName) {
return `https://www.googleapis.com/storage/v1/b/${bucketName}/o/${fileName}?alt=media`;
}
}
exports.GoogleStorageUtils = GoogleStorageUtils;
//# sourceMappingURL=google-storage-utils.js.map