UNPKG

hardhat

Version:

Hardhat is an extensible developer tool that helps smart contract developers increase productivity by reliably bringing together the tools they want.

99 lines 3.64 kB
import { assertHardhatInvariant, HardhatError, } from "@nomicfoundation/hardhat-errors"; import { normalizeHexString } from "@nomicfoundation/hardhat-utils/hex"; import { AsyncMutex } from "@nomicfoundation/hardhat-utils/synchronization"; export const CONFIGURATION_VARIABLE_MARKER = "{variable}"; export function resolveConfigurationVariable(hooks, variable) { if (typeof variable === "string") { return new FixedValueConfigurationVariable(variable); } return new LazyResolvedConfigurationVariable(hooks, variable); } class BaseResolvedConfigurationVariable { format; _type = "ResolvedConfigurationVariable"; #cachedValue; constructor(format) { this.format = format; assertHardhatInvariant(this.format.includes(CONFIGURATION_VARIABLE_MARKER), "The format must include the variable marker"); } async get() { if (this.#cachedValue === undefined) { this.#cachedValue = await this._getRawValue(); } return this.format.replaceAll(CONFIGURATION_VARIABLE_MARKER, this.#cachedValue); } async getUrl() { const value = await this.get(); try { new URL(value); return value; } catch (_error) { throw new HardhatError(HardhatError.ERRORS.CORE.GENERAL.INVALID_URL, { url: value, }); } } async getBigInt() { const value = await this.get(); try { return BigInt(value); } catch (_error) { throw new HardhatError(HardhatError.ERRORS.CORE.GENERAL.INVALID_BIGINT, { value, }); } } async getHexString() { const value = await this.get(); try { return normalizeHexString(value); } catch { throw new HardhatError(HardhatError.ERRORS.CORE.GENERAL.INVALID_HEX_STRING, { value, }); } } } export class LazyResolvedConfigurationVariable extends BaseResolvedConfigurationVariable { // We want to serialize the calls to the configurationVariables#fetchValue // hook for each HRE. We don't have the HRE here, so we create a mutex per // HookManager, which is equivalent. static #mutexes = new WeakMap(); #hooks; #variable; name; constructor(hooks, variable) { super(variable.format ?? CONFIGURATION_VARIABLE_MARKER); this.name = variable.name; this.#hooks = hooks; this.#variable = variable; if (!LazyResolvedConfigurationVariable.#mutexes.has(hooks)) { LazyResolvedConfigurationVariable.#mutexes.set(hooks, new AsyncMutex()); } } async _getRawValue() { const mutex = LazyResolvedConfigurationVariable.#mutexes.get(this.#hooks); assertHardhatInvariant(mutex !== undefined, "Mutex must be defined"); return mutex.exclusiveRun(async () => this.#hooks.runHandlerChain("configurationVariables", "fetchValue", [this.#variable], async (_context, v) => { const value = process.env[v.name]; if (typeof value !== "string") { throw new HardhatError(HardhatError.ERRORS.CORE.GENERAL.ENV_VAR_NOT_FOUND, { name: v.name }); } return value; })); } } export class FixedValueConfigurationVariable extends BaseResolvedConfigurationVariable { #value; constructor(value) { super(CONFIGURATION_VARIABLE_MARKER); this.#value = value; } async _getRawValue() { return this.#value; } } //# sourceMappingURL=configuration-variables.js.map