util.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. "use strict";
  2. // Copyright 2023 Google LLC
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  16. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
  17. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
  18. return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
  19. };
  20. var _LRUCache_instances, _LRUCache_cache, _LRUCache_moveToEnd, _LRUCache_evict;
  21. Object.defineProperty(exports, "__esModule", { value: true });
  22. exports.LRUCache = void 0;
  23. exports.snakeToCamel = snakeToCamel;
  24. exports.originalOrCamelOptions = originalOrCamelOptions;
  25. /**
  26. * Returns the camel case of a provided string.
  27. *
  28. * @remarks
  29. *
  30. * Match any `_` and not `_` pair, then return the uppercase of the not `_`
  31. * character.
  32. *
  33. * @internal
  34. *
  35. * @param str the string to convert
  36. * @returns the camelCase'd string
  37. */
  38. function snakeToCamel(str) {
  39. return str.replace(/([_][^_])/g, match => match.slice(1).toUpperCase());
  40. }
  41. /**
  42. * Get the value of `obj[key]` or `obj[camelCaseKey]`, with a preference
  43. * for original, non-camelCase key.
  44. *
  45. * @param obj object to lookup a value in
  46. * @returns a `get` function for getting `obj[key || snakeKey]`, if available
  47. */
  48. function originalOrCamelOptions(obj) {
  49. /**
  50. *
  51. * @param key an index of object, preferably snake_case
  52. * @returns the value `obj[key || snakeKey]`, if available
  53. */
  54. function get(key) {
  55. var _a;
  56. const o = (obj || {});
  57. return (_a = o[key]) !== null && _a !== void 0 ? _a : o[snakeToCamel(key)];
  58. }
  59. return { get };
  60. }
  61. /**
  62. * A simple LRU cache utility.
  63. * Not meant for external usage.
  64. *
  65. * @experimental
  66. * @internal
  67. */
  68. class LRUCache {
  69. constructor(options) {
  70. _LRUCache_instances.add(this);
  71. /**
  72. * Maps are in order. Thus, the older item is the first item.
  73. *
  74. * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map}
  75. */
  76. _LRUCache_cache.set(this, new Map());
  77. this.capacity = options.capacity;
  78. this.maxAge = options.maxAge;
  79. }
  80. /**
  81. * Add an item to the cache.
  82. *
  83. * @param key the key to upsert
  84. * @param value the value of the key
  85. */
  86. set(key, value) {
  87. __classPrivateFieldGet(this, _LRUCache_instances, "m", _LRUCache_moveToEnd).call(this, key, value);
  88. __classPrivateFieldGet(this, _LRUCache_instances, "m", _LRUCache_evict).call(this);
  89. }
  90. /**
  91. * Get an item from the cache.
  92. *
  93. * @param key the key to retrieve
  94. */
  95. get(key) {
  96. const item = __classPrivateFieldGet(this, _LRUCache_cache, "f").get(key);
  97. if (!item)
  98. return;
  99. __classPrivateFieldGet(this, _LRUCache_instances, "m", _LRUCache_moveToEnd).call(this, key, item.value);
  100. __classPrivateFieldGet(this, _LRUCache_instances, "m", _LRUCache_evict).call(this);
  101. return item.value;
  102. }
  103. }
  104. exports.LRUCache = LRUCache;
  105. _LRUCache_cache = new WeakMap(), _LRUCache_instances = new WeakSet(), _LRUCache_moveToEnd = function _LRUCache_moveToEnd(key, value) {
  106. __classPrivateFieldGet(this, _LRUCache_cache, "f").delete(key);
  107. __classPrivateFieldGet(this, _LRUCache_cache, "f").set(key, {
  108. value,
  109. lastAccessed: Date.now(),
  110. });
  111. }, _LRUCache_evict = function _LRUCache_evict() {
  112. const cutoffDate = this.maxAge ? Date.now() - this.maxAge : 0;
  113. /**
  114. * Because we know Maps are in order, this item is both the
  115. * last item in the list (capacity) and oldest (maxAge).
  116. */
  117. let oldestItem = __classPrivateFieldGet(this, _LRUCache_cache, "f").entries().next();
  118. while (!oldestItem.done &&
  119. (__classPrivateFieldGet(this, _LRUCache_cache, "f").size > this.capacity || // too many
  120. oldestItem.value[1].lastAccessed < cutoffDate) // too old
  121. ) {
  122. __classPrivateFieldGet(this, _LRUCache_cache, "f").delete(oldestItem.value[0]);
  123. oldestItem = __classPrivateFieldGet(this, _LRUCache_cache, "f").entries().next();
  124. }
  125. };