UNPKG

@softchef/cdk-iot-device-management

Version:

IoT device management is composed of things, thing types, thing groups, jobs, files API services. The constructs can be used independently, that are based on full-managed service to create an API Gateway & Lambda function.

85 lines (84 loc) 3.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getHomeDir = exports.loadSharedConfigFiles = exports.ENV_CONFIG_PATH = exports.ENV_CREDENTIALS_PATH = void 0; const fs_1 = require("fs"); const os_1 = require("os"); const path_1 = require("path"); exports.ENV_CREDENTIALS_PATH = "AWS_SHARED_CREDENTIALS_FILE"; exports.ENV_CONFIG_PATH = "AWS_CONFIG_FILE"; const swallowError = () => ({}); const loadSharedConfigFiles = (init = {}) => { const { filepath = process.env[exports.ENV_CREDENTIALS_PATH] || path_1.join(exports.getHomeDir(), ".aws", "credentials"), configFilepath = process.env[exports.ENV_CONFIG_PATH] || path_1.join(exports.getHomeDir(), ".aws", "config"), } = init; return Promise.all([ slurpFile(configFilepath).then(parseIni).then(normalizeConfigFile).catch(swallowError), slurpFile(filepath).then(parseIni).catch(swallowError), ]).then((parsedFiles) => { const [configFile, credentialsFile] = parsedFiles; return { configFile, credentialsFile, }; }); }; exports.loadSharedConfigFiles = loadSharedConfigFiles; const profileKeyRegex = /^profile\s(["'])?([^\1]+)\1$/; const normalizeConfigFile = (data) => { const map = {}; for (const key of Object.keys(data)) { let matches; if (key === "default") { map.default = data.default; } else if ((matches = profileKeyRegex.exec(key))) { const [_1, _2, normalizedKey] = matches; if (normalizedKey) { map[normalizedKey] = data[key]; } } } return map; }; const profileNameBlockList = ["__proto__", "profile __proto__"]; const parseIni = (iniData) => { const map = {}; let currentSection; for (let line of iniData.split(/\r?\n/)) { line = line.split(/(^|\s)[;#]/)[0]; const section = line.match(/^\s*\[([^\[\]]+)]\s*$/); if (section) { currentSection = section[1]; if (profileNameBlockList.includes(currentSection)) { throw new Error(`Found invalid profile name "${currentSection}"`); } } else if (currentSection) { const item = line.match(/^\s*(.+?)\s*=\s*(.+?)\s*$/); if (item) { map[currentSection] = map[currentSection] || {}; map[currentSection][item[1]] = item[2]; } } } return map; }; const slurpFile = (path) => new Promise((resolve, reject) => { fs_1.readFile(path, "utf8", (err, data) => { if (err) { reject(err); } else { resolve(data); } }); }); const getHomeDir = () => { const { HOME, USERPROFILE, HOMEPATH, HOMEDRIVE = `C:${path_1.sep}` } = process.env; if (HOME) return HOME; if (USERPROFILE) return USERPROFILE; if (HOMEPATH) return `${HOMEDRIVE}${HOMEPATH}`; return os_1.homedir(); }; exports.getHomeDir = getHomeDir;