No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functionsDeployHelper.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.isEndpointFiltered = exports.isCodebaseFiltered = exports.groupEndpointsByCodebase = exports.targetCodebases = exports.getFunctionLabel = exports.getEndpointFilters = exports.parseFunctionSelector = exports.endpointMatchesFilter = exports.endpointMatchesAnyFilter = void 0;
  4. const backend = require("./backend");
  5. const projectConfig_1 = require("../../functions/projectConfig");
  6. function endpointMatchesAnyFilter(endpoint, filters) {
  7. if (!filters) {
  8. return true;
  9. }
  10. return filters.some((filter) => endpointMatchesFilter(endpoint, filter));
  11. }
  12. exports.endpointMatchesAnyFilter = endpointMatchesAnyFilter;
  13. function endpointMatchesFilter(endpoint, filter) {
  14. if (endpoint.codebase && filter.codebase) {
  15. if (endpoint.codebase !== filter.codebase) {
  16. return false;
  17. }
  18. }
  19. if (!filter.idChunks) {
  20. return true;
  21. }
  22. const idChunks = endpoint.id.split("-");
  23. if (idChunks.length < filter.idChunks.length) {
  24. return false;
  25. }
  26. for (let i = 0; i < filter.idChunks.length; i += 1) {
  27. if (idChunks[i] !== filter.idChunks[i]) {
  28. return false;
  29. }
  30. }
  31. return true;
  32. }
  33. exports.endpointMatchesFilter = endpointMatchesFilter;
  34. function parseFunctionSelector(selector) {
  35. const fragments = selector.split(":");
  36. if (fragments.length < 2) {
  37. return [
  38. { codebase: fragments[0] },
  39. { codebase: projectConfig_1.DEFAULT_CODEBASE, idChunks: fragments[0].split(/[-.]/) },
  40. ];
  41. }
  42. return [
  43. {
  44. codebase: fragments[0],
  45. idChunks: fragments[1].split(/[-.]/),
  46. },
  47. ];
  48. }
  49. exports.parseFunctionSelector = parseFunctionSelector;
  50. function getEndpointFilters(options) {
  51. if (!options.only) {
  52. return undefined;
  53. }
  54. const selectors = options.only.split(",");
  55. const filters = [];
  56. for (let selector of selectors) {
  57. if (selector.startsWith("functions:")) {
  58. selector = selector.replace("functions:", "");
  59. if (selector.length > 0) {
  60. filters.push(...parseFunctionSelector(selector));
  61. }
  62. }
  63. }
  64. if (filters.length === 0) {
  65. return undefined;
  66. }
  67. return filters;
  68. }
  69. exports.getEndpointFilters = getEndpointFilters;
  70. function getFunctionLabel(fn) {
  71. let id = `${fn.id}(${fn.region})`;
  72. if (fn.codebase && fn.codebase !== projectConfig_1.DEFAULT_CODEBASE) {
  73. id = `${fn.codebase}:${id}`;
  74. }
  75. return id;
  76. }
  77. exports.getFunctionLabel = getFunctionLabel;
  78. function targetCodebases(config, filters) {
  79. const codebasesFromConfig = [...new Set(Object.values(config).map((c) => c.codebase))];
  80. if (!filters) {
  81. return [...codebasesFromConfig];
  82. }
  83. const codebasesFromFilters = [
  84. ...new Set(filters.map((f) => f.codebase).filter((c) => c !== undefined)),
  85. ];
  86. if (codebasesFromFilters.length === 0) {
  87. return [...codebasesFromConfig];
  88. }
  89. const intersections = [];
  90. for (const codebase of codebasesFromConfig) {
  91. if (codebasesFromFilters.includes(codebase)) {
  92. intersections.push(codebase);
  93. }
  94. }
  95. return intersections;
  96. }
  97. exports.targetCodebases = targetCodebases;
  98. function groupEndpointsByCodebase(wantBackends, haveEndpoints) {
  99. const grouped = {};
  100. let endpointsToAssign = haveEndpoints;
  101. for (const codebase of Object.keys(wantBackends)) {
  102. const names = backend.allEndpoints(wantBackends[codebase]).map((e) => backend.functionName(e));
  103. grouped[codebase] = backend.of(...endpointsToAssign.filter((e) => names.includes(backend.functionName(e))));
  104. endpointsToAssign = endpointsToAssign.filter((e) => !names.includes(backend.functionName(e)));
  105. }
  106. for (const codebase of Object.keys(wantBackends)) {
  107. const matchedEndpoints = endpointsToAssign.filter((e) => e.codebase === codebase);
  108. grouped[codebase] = backend.merge(grouped[codebase], backend.of(...matchedEndpoints));
  109. const matchedNames = matchedEndpoints.map((e) => backend.functionName(e));
  110. endpointsToAssign = endpointsToAssign.filter((e) => {
  111. return !matchedNames.includes(backend.functionName(e));
  112. });
  113. }
  114. return grouped;
  115. }
  116. exports.groupEndpointsByCodebase = groupEndpointsByCodebase;
  117. function isCodebaseFiltered(codebase, filters) {
  118. return filters.some((filter) => {
  119. const noIdChunks = (filter.idChunks || []).length === 0;
  120. return noIdChunks && filter.codebase === codebase;
  121. });
  122. }
  123. exports.isCodebaseFiltered = isCodebaseFiltered;
  124. function isEndpointFiltered(endpoint, filters) {
  125. return filters.some((filter) => endpointMatchesFilter(endpoint, filter));
  126. }
  127. exports.isEndpointFiltered = isEndpointFiltered;