Sin descripción
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.pruneUndefiends = exports.formatServiceAccount = exports.getInvokerMembers = exports.fieldMasks = exports.renameIfPresent = exports.convertIfPresent = exports.copyIfPresent = exports.assertOneOf = exports.durationFromSeconds = exports.secondsFromDuration = void 0;
  4. const error_1 = require("../error");
  5. function secondsFromDuration(d) {
  6. return +d.slice(0, d.length - 1);
  7. }
  8. exports.secondsFromDuration = secondsFromDuration;
  9. function durationFromSeconds(s) {
  10. return `${s}s`;
  11. }
  12. exports.durationFromSeconds = durationFromSeconds;
  13. function assertOneOf(typename, obj, oneof, ...fields) {
  14. const defined = [];
  15. for (const key of fields) {
  16. const value = obj[key];
  17. if (typeof value !== "undefined" && value != null) {
  18. defined.push(key);
  19. }
  20. }
  21. if (defined.length > 1) {
  22. throw new error_1.FirebaseError(`Invalid ${typename} definition. ${oneof} can only have one field defined, but found ${defined.join(",")}`);
  23. }
  24. }
  25. exports.assertOneOf = assertOneOf;
  26. function copyIfPresent(dest, src, ...fields) {
  27. for (const field of fields) {
  28. if (!Object.prototype.hasOwnProperty.call(src, field)) {
  29. continue;
  30. }
  31. dest[field] = src[field];
  32. }
  33. }
  34. exports.copyIfPresent = copyIfPresent;
  35. function convertIfPresent(...args) {
  36. if (args.length === 4) {
  37. const [dest, src, key, converter] = args;
  38. if (Object.prototype.hasOwnProperty.call(src, key)) {
  39. dest[key] = converter(src[key]);
  40. }
  41. return;
  42. }
  43. const [dest, src, destKey, srcKey, converter] = args;
  44. if (Object.prototype.hasOwnProperty.call(src, srcKey)) {
  45. dest[destKey] = converter(src[srcKey]);
  46. }
  47. }
  48. exports.convertIfPresent = convertIfPresent;
  49. function renameIfPresent(dest, src, destKey, srcKey) {
  50. if (!Object.prototype.hasOwnProperty.call(src, srcKey)) {
  51. return;
  52. }
  53. dest[destKey] = src[srcKey];
  54. }
  55. exports.renameIfPresent = renameIfPresent;
  56. function fieldMasks(object, ...doNotRecurseIn) {
  57. const masks = [];
  58. fieldMasksHelper([], object, doNotRecurseIn, masks);
  59. return masks;
  60. }
  61. exports.fieldMasks = fieldMasks;
  62. function fieldMasksHelper(prefixes, cursor, doNotRecurseIn, masks) {
  63. if (Array.isArray(cursor) && !cursor.length) {
  64. return;
  65. }
  66. if (typeof cursor !== "object" || (Array.isArray(cursor) && cursor.length) || cursor === null) {
  67. masks.push(prefixes.join("."));
  68. return;
  69. }
  70. const entries = Object.entries(cursor);
  71. if (entries.length === 0) {
  72. masks.push(prefixes.join("."));
  73. return;
  74. }
  75. for (const [key, value] of entries) {
  76. const newPrefixes = [...prefixes, key];
  77. if (doNotRecurseIn.includes(newPrefixes.join("."))) {
  78. masks.push(newPrefixes.join("."));
  79. continue;
  80. }
  81. fieldMasksHelper(newPrefixes, value, doNotRecurseIn, masks);
  82. }
  83. }
  84. function getInvokerMembers(invoker, projectId) {
  85. if (invoker.includes("private")) {
  86. return [];
  87. }
  88. if (invoker.includes("public")) {
  89. return ["allUsers"];
  90. }
  91. return invoker.map((inv) => formatServiceAccount(inv, projectId));
  92. }
  93. exports.getInvokerMembers = getInvokerMembers;
  94. function formatServiceAccount(serviceAccount, projectId) {
  95. if (serviceAccount.length === 0) {
  96. throw new error_1.FirebaseError("Service account cannot be an empty string");
  97. }
  98. if (!serviceAccount.includes("@")) {
  99. throw new error_1.FirebaseError("Service account must be of the form 'service-account@' or 'service-account@{project-id}.iam.gserviceaccount.com'");
  100. }
  101. if (serviceAccount.endsWith("@")) {
  102. const suffix = `${projectId}.iam.gserviceaccount.com`;
  103. return `serviceAccount:${serviceAccount}${suffix}`;
  104. }
  105. return `serviceAccount:${serviceAccount}`;
  106. }
  107. exports.formatServiceAccount = formatServiceAccount;
  108. function pruneUndefiends(obj) {
  109. if (typeof obj !== "object" || obj === null) {
  110. return;
  111. }
  112. const keyable = obj;
  113. for (const key of Object.keys(keyable)) {
  114. if (keyable[key] === undefined) {
  115. delete keyable[key];
  116. }
  117. else if (typeof keyable[key] === "object") {
  118. if (Array.isArray(keyable[key])) {
  119. for (const sub of keyable[key]) {
  120. pruneUndefiends(sub);
  121. }
  122. keyable[key] = keyable[key].filter((e) => e !== undefined);
  123. }
  124. else {
  125. pruneUndefiends(keyable[key]);
  126. }
  127. }
  128. }
  129. }
  130. exports.pruneUndefiends = pruneUndefiends;