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.

functionsEmulatorUtils.js 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.isLocalHost = exports.compareVersionStrings = exports.parseVersionString = exports.parseRuntimeVersion = exports.removePathSegments = exports.trimSlashes = exports.isValidWildcardMatch = exports.extractParamsFromPath = void 0;
  4. const wildcardRegex = new RegExp("{[^/{}]*}");
  5. const wildcardKeyRegex = new RegExp("^{(.+)}$");
  6. function extractParamsFromPath(wildcardPath, snapshotPath) {
  7. if (!isValidWildcardMatch(wildcardPath, snapshotPath)) {
  8. return {};
  9. }
  10. const wildcardChunks = trimSlashes(wildcardPath).split("/");
  11. const snapshotChunks = trimSlashes(snapshotPath).split("/");
  12. return wildcardChunks
  13. .slice(-snapshotChunks.length)
  14. .reduce((params, chunk, index) => {
  15. const match = wildcardKeyRegex.exec(chunk);
  16. if (match) {
  17. const wildcardKey = match[1];
  18. const potentialWildcardValue = snapshotChunks[index];
  19. if (!wildcardKeyRegex.exec(potentialWildcardValue)) {
  20. params[wildcardKey] = potentialWildcardValue;
  21. }
  22. }
  23. return params;
  24. }, {});
  25. }
  26. exports.extractParamsFromPath = extractParamsFromPath;
  27. function isValidWildcardMatch(wildcardPath, snapshotPath) {
  28. const wildcardChunks = trimSlashes(wildcardPath).split("/");
  29. const snapshotChunks = trimSlashes(snapshotPath).split("/");
  30. if (snapshotChunks.length > wildcardChunks.length) {
  31. return false;
  32. }
  33. const mismatchedChunks = wildcardChunks.slice(-snapshotChunks.length).filter((chunk, index) => {
  34. return !(wildcardRegex.exec(chunk) || chunk === snapshotChunks[index]);
  35. });
  36. return mismatchedChunks.length === 0;
  37. }
  38. exports.isValidWildcardMatch = isValidWildcardMatch;
  39. function trimSlashes(str) {
  40. return str
  41. .split("/")
  42. .filter((c) => c)
  43. .join("/");
  44. }
  45. exports.trimSlashes = trimSlashes;
  46. function removePathSegments(path, count) {
  47. return trimSlashes(path).split("/").slice(count).join("/");
  48. }
  49. exports.removePathSegments = removePathSegments;
  50. function parseRuntimeVersion(runtime) {
  51. if (!runtime) {
  52. return undefined;
  53. }
  54. const runtimeRe = /(nodejs)?([0-9]+)/;
  55. const match = runtimeRe.exec(runtime);
  56. if (match) {
  57. return Number.parseInt(match[2]);
  58. }
  59. return undefined;
  60. }
  61. exports.parseRuntimeVersion = parseRuntimeVersion;
  62. function parseVersionString(version) {
  63. const parts = (version || "0").split(".");
  64. parts.push("0");
  65. parts.push("0");
  66. return {
  67. major: parseInt(parts[0], 10),
  68. minor: parseInt(parts[1], 10),
  69. patch: parseInt(parts[2], 10),
  70. };
  71. }
  72. exports.parseVersionString = parseVersionString;
  73. function compareVersionStrings(a, b) {
  74. const versionA = parseVersionString(a);
  75. const versionB = parseVersionString(b);
  76. if (versionA.major !== versionB.major) {
  77. return versionA.major - versionB.major;
  78. }
  79. if (versionA.minor !== versionB.minor) {
  80. return versionA.minor - versionB.minor;
  81. }
  82. if (versionA.patch !== versionB.patch) {
  83. return versionA.patch - versionB.patch;
  84. }
  85. return 0;
  86. }
  87. exports.compareVersionStrings = compareVersionStrings;
  88. function isLocalHost(href) {
  89. return !!href.match(/^(http(s)?:\/\/)?(localhost|127.0.0.1|\[::1])/);
  90. }
  91. exports.isLocalHost = isLocalHost;