UNPKG

lemon-core

Version:
134 lines 5.51 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.awsConfig = exports.asyncCredentials = exports.loadDataYml = exports.loadJsonSync = exports.onlyDefined = exports.isLambda = void 0; /** * `tools/tools.ts` * - additional helper. * * ex: * ```ts * const environ = require('lemon-core/dist/environ').default; * const $env = environ(process) * ``` * * @author Steve Jung <steve@lemoncloud.io> * @date 2019-08-09 initial typescript version. * @date 2018-05-23 initial version * @date 2019-11-26 cleanup and optimized for `lemon-core#v2` * * @copyright (C) 2019 LemonCloud Co Ltd. - All Rights Reserved. */ const fs_1 = __importDefault(require("fs")); const js_yaml_1 = __importDefault(require("js-yaml")); const credential_providers_1 = require("@aws-sdk/credential-providers"); /** * Check if the current environment is AWS Lambda * @returns {boolean} - true if running in AWS Lambda environment */ const isLambda = () => { var _a; return !!((_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a.AWS_LAMBDA_FUNCTION_NAME); }; exports.isLambda = isLambda; /** returns only defined */ const onlyDefined = (N, $def = null) => N && typeof N === 'object' ? Object.entries(N).reduce((N, [k, v]) => { if (v !== undefined) N[k] = v; return N; }, {}) : $def; exports.onlyDefined = onlyDefined; /** * load json in sync. */ const loadJsonSync = (name, def = {}) => { name = !name.startsWith('./') ? `./${name}` : name; try { const rawdata = fs_1.default.readFileSync(name); return JSON.parse(rawdata.toString()); } catch (e) { if (def && typeof def === 'object') def.error = `${e.message || e}`; return def; } }; exports.loadJsonSync = loadJsonSync; /** * load yml data via './data/<file>.yml' */ const loadDataYml = (file, folder) => { folder = folder || 'data'; const path = `./${folder}/` + file + (file.endsWith('.yml') ? '' : '.yml'); if (!fs_1.default.existsSync(path)) throw new Error('404 NOT FOUND - data-file:' + path); return js_yaml_1.default.load(fs_1.default.readFileSync(path, 'utf8')); }; exports.loadDataYml = loadDataYml; /** * dynamic loading credentials by profile. (search PROFILE -> NAME) * * @returns {Promise<CrendentialForAWS>} - AWS credentials */ const asyncCredentials = (profile) => __awaiter(void 0, void 0, void 0, function* () { const provider = (0, credential_providers_1.fromIni)({ profile }); const $res = yield provider(); return Object.assign(Object.assign({}, $res), { profile }); }); exports.asyncCredentials = asyncCredentials; /** * load config for aws sdk * - if `profile` is not defined, use `AWS_PROFILE` or `default` * - if `region` is not defined, use `AWS_REGION` or `ap-northeast-2` * * ``` * # run with default profile * $ npm run test:watch * * # run with `lemon` profile * #npm run test:watch.lemon */ const awsConfig = ($engine, /** params or region */ params) => { var _a; const errScope = `awsConfig(${(_a = $engine === null || $engine === void 0 ? void 0 : $engine.id) !== null && _a !== void 0 ? _a : ''})`; const region = params === undefined ? 'ap-northeast-2' : typeof params === 'string' ? params : typeof params === 'function' ? params() : params === null || params === void 0 ? void 0 : params.region; const _conf = typeof params === 'object' ? params : null; // If running in Lambda environment, set profile to 'none' const profile = (0, exports.isLambda)() ? 'none' : $engine === null || $engine === void 0 ? void 0 : $engine.environ('NAME', 'none'); if (typeof profile !== 'string') throw new Error(`@env.NAME[${typeof profile}] is invalid (check $engine) - ${errScope}`); //* build defualt config. const $conf = (0, exports.onlyDefined)(Object.assign({ profile: profile && profile !== 'none' ? profile : undefined, region }, _conf)); if (typeof $conf.region === 'string' && !$conf.region) throw new Error(`.region[${$conf.region}] is invalid (empty) - ${errScope}`); //! load the credentials provider(async function). const credentials = typeof $conf.credentials === 'function' ? $conf.credentials : $conf.profile ? (0, credential_providers_1.fromIni)({ profile: $conf.profile }) : undefined; return (0, exports.onlyDefined)(Object.assign(Object.assign({}, $conf), { credentials })); }; exports.awsConfig = awsConfig; //# sourceMappingURL=tools.js.map