@vulcan-sql/extension-store-canner
Version:
Canner persistence store for Vulcan SQL
65 lines • 3.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createStorageService = void 0;
const tslib_1 = require("tslib");
const canner_storage_1 = require("@canner/canner-storage");
const identity_1 = require("@azure/identity");
const googleapis_1 = require("googleapis");
const aws_sdk_1 = require("aws-sdk");
// Create a factory to get storage service by environment variables.
const createStorageService = (options) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const { provider } = options;
if (!provider)
throw new Error('Provider options is required!');
// check and throw error to exclude all not value of cloud provider
if (!(provider in canner_storage_1.CloudProvider))
throw new Error(`Provider should be ${Object.keys(canner_storage_1.CloudProvider).join(',')}`);
// other options will be checked when creating storage service by "getStorageService" function
const storageService = yield getStorageServiceMapper[provider](options);
return storageService;
});
exports.createStorageService = createStorageService;
// define a mapper to get storage service by config provider value
const getStorageServiceMapper = {
[canner_storage_1.CloudProvider.MINIO.toString()]: (options) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
return (0, canner_storage_1.getStorageService)({
cloudProvider: canner_storage_1.CloudProvider.MINIO,
endPoint: options.minioUrl,
port: options.minioPort,
useSSL: options.minioSSL,
accessKey: options.minioAccessKey,
secretKey: options.minioSecretKey,
bucket: options.minioBucket,
});
}),
[canner_storage_1.CloudProvider.AWS.toString()]: (options) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const credentialProviderChain = new aws_sdk_1.CredentialProviderChain();
const masterCredentials = yield credentialProviderChain.resolvePromise();
return (0, canner_storage_1.getStorageService)({
cloudProvider: canner_storage_1.CloudProvider.AWS,
credentials: masterCredentials,
bucket: options.awsBucket,
});
}),
[canner_storage_1.CloudProvider.GCP.toString()]: (options) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const auth = yield new googleapis_1.google.auth.GoogleAuth({
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
});
return (0, canner_storage_1.getStorageService)({
cloudProvider: canner_storage_1.CloudProvider.GCP,
bucket: options.gcpBucket,
// The reason to use any type for auth, reference: https://github.com/googleapis/nodejs-storage/issues/1284#issuecomment-768004492
auth: auth,
});
}),
[canner_storage_1.CloudProvider.AZURE.toString()]: (options) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const defaultAzureCredential = new identity_1.DefaultAzureCredential();
return (0, canner_storage_1.getStorageService)({
cloudProvider: canner_storage_1.CloudProvider.AZURE,
credential: defaultAzureCredential,
accountName: options.azureAccountName,
bucket: options.azureBucket,
});
}),
};
//# sourceMappingURL=storageService.js.map