UNPKG

azurite

Version:

An open source Azure Storage API compatible server

97 lines 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const constants_1 = require("../common/utils/constants"); const constants_2 = require("./utils/constants"); var Status; (function (Status) { Status[Status["Initializing"] = 0] = "Initializing"; Status[Status["Initialized"] = 1] = "Initialized"; Status[Status["Closing"] = 2] = "Closing"; Status[Status["Closed"] = 3] = "Closed"; })(Status || (Status = {})); const DEFAULT_EMULATOR_ACCOUNTS = { [constants_1.EMULATOR_ACCOUNT_NAME]: { name: constants_1.EMULATOR_ACCOUNT_NAME, key1: constants_1.EMULATOR_ACCOUNT_KEY } }; class AccountDataStore { constructor(logger) { this.logger = logger; this.status = Status.Closed; this.accounts = DEFAULT_EMULATOR_ACCOUNTS; } getAccount(name) { if (this.accounts[name] !== undefined) { return this.accounts[name]; } else { return undefined; } } async init() { this.refresh(); this.timer = setInterval(() => { this.refresh(); }, constants_2.DEFAULT_ACCOUNTS_REFRESH_INTERVAL); this.timer.unref(); this.status = Status.Initialized; } isInitialized() { return this.status === Status.Initialized; } async close() { clearInterval(this.timer); this.status = Status.Closed; } isClosed() { return this.status === Status.Closed; } async clean() { /* NOOP */ } refresh() { // TODO: Parse environment variable from environment class const env = process.env[constants_2.AZURITE_ACCOUNTS_ENV]; this.logger.info(`AccountDataStore:init() Refresh accounts from environment variable ${constants_2.AZURITE_ACCOUNTS_ENV} with value ${env ? "*****" : undefined}`); if (env) { try { this.accounts = this.parserAccountsEnvironmentString(env); } catch (err) { this.logger.error(`AccountDataStore:init() Fallback to default emulator account ${constants_1.EMULATOR_ACCOUNT_NAME}. Refresh accounts from environment variable ${constants_2.AZURITE_ACCOUNTS_ENV} failed. ${JSON.stringify(err)}`); this.accounts = DEFAULT_EMULATOR_ACCOUNTS; } } else { this.logger.info(`AccountDataStore:init() Fallback to default emulator account ${constants_1.EMULATOR_ACCOUNT_NAME}.`); this.accounts = DEFAULT_EMULATOR_ACCOUNTS; } } parserAccountsEnvironmentString(accounts) { // account1:key1 // account1:key1:key2 // account1:key1:key2;account2:key2; const results = {}; const accountsArray = accounts.trim().split(";"); accountsArray.forEach(accountAndKeys => { if (accountAndKeys.length > 0) { const parts = accountAndKeys.split(":"); if (parts.length < 2 || parts.length > 3) { throw RangeError(`AccountDataStore:parserAccountsEnvironmentString() Invalid environment string format for ${accounts}`); } const account = parts[0]; const key1 = parts[1]; const key2 = parts.length > 2 ? parts[2] : undefined; results[account] = { name: account, key1: Buffer.from(key1, "base64"), key2: key2 ? Buffer.from(key2, "base64") : undefined }; } }); return results; } } exports.default = AccountDataStore; //# sourceMappingURL=AccountDataStore.js.map