UNPKG

@stokr/utils

Version:

STOKR - Utils

24 lines (22 loc) 653 B
import fs from "fs"; import util from "util"; import log from "./log.js"; export default { // Get a secret from its name get(secret, fallback) { log.info(`Loading secret ${secret}.`); try { // Swarm secret are accessible within tmpfs /run/secrets dir // the call to trim() is really important as it removes the trailing "\n" return fs .readFileSync(util.format("/run/secrets/%s", secret), "utf8") .trim(); } catch (error) { if (fallback !== undefined) { log.warn(`Secret ${secret} not found, using fallback value.`); return fallback; } log.error(error); } }, };