UNPKG

serverless-vpc-discovery

Version:
68 lines (67 loc) 3.02 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.sleep = sleep; exports.getAWSPagedResults = getAWSPagedResults; exports.isObjectEmpty = isObjectEmpty; exports.wildcardMatches = wildcardMatches; exports.getValueFromTags = getValueFromTags; /** * Iterate through the pages of a AWS SDK response and collect them into a single array * * @param client - The AWS service instance to use to make the calls * @param resultsKey - The key name in the response that contains the items to return * @param nextTokenKey - The request key name to append to the request that has the paging token value * @param nextRequestTokenKey - The response key name that has the next paging token value * @param params - Parameters to send in the request */ function getAWSPagedResults(client, resultsKey, nextTokenKey, nextRequestTokenKey, params) { return __awaiter(this, void 0, void 0, function* () { let results = []; let response = yield client.send(params); results = results.concat(response[resultsKey] || results); while (nextRequestTokenKey in response && response[nextRequestTokenKey]) { params.input[nextTokenKey] = response[nextRequestTokenKey]; response = yield client.send(params); results = results.concat(response[resultsKey]); } return results; }); } /** * Stops event thread execution for given number of seconds. * @param seconds * @returns {Promise<void>} Resolves after given number of seconds. */ function sleep(seconds) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve) => setTimeout(resolve, 1000 * seconds)); }); } function isObjectEmpty(value) { return Object.keys(value).length === 0; } function replaceAll(input, search, replace) { return input.split(search).join(replace); } function wildcardMatches(inputArn, actualArn) { const noColon = "[^:]"; const inputArnRegexStr = replaceAll(replaceAll(inputArn, "?", noColon), "*", `${noColon}*`); const inputArnRegex = new RegExp(`^${inputArnRegexStr}$`); return inputArnRegex.test(actualArn); } function getValueFromTags(tags, tagKey) { const tagItem = tags.find((tag) => tag.Key === tagKey); if (tagItem) { return tagItem.Value; } return null; }