UNPKG

ssm-config-loader

Version:

Load configuration from SSM Parameter Store with local fallback

78 lines 2.96 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadSsmConfig = void 0; const promises_1 = require("fs/promises"); const path = __importStar(require("path")); const loadConfigFromSsm = async (ssm, prefix) => { const config = {}; let nextToken = undefined; do { const result = await ssm.getParametersByPath({ Path: prefix, Recursive: true, NextToken: nextToken, WithDecryption: true, }); if (!result.Parameters) { break; } for (const parameter of result.Parameters) { if (parameter.Name === undefined || parameter.Value === undefined) { continue; } const branchNames = parameter.Name.substring(prefix.length + 1).split('/'); const leafName = branchNames.pop(); if (!leafName) { continue; } let branch = config; for (const branchName of branchNames) { if (typeof branch[branchName] === 'string') { throw new Error(`Parameter "${parameter.Name}" collides with leaf node parameter`); } if (!branch[branchName]) { branch[branchName] = {}; } branch = branch[branchName]; } branch[leafName] = parameter.Value; } nextToken = result.NextToken; } while (nextToken); return config; }; const loadConfigFromFile = async () => { const json = await (0, promises_1.readFile)(path.join(process.cwd(), 'ssm-config.json'), { encoding: 'utf8' }); return JSON.parse(json); }; const loadSsmConfig = async (ssm, prefix) => { if (prefix) { return loadConfigFromSsm(ssm, prefix); } return loadConfigFromFile(); }; exports.loadSsmConfig = loadSsmConfig; //# sourceMappingURL=index.js.map