index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var callBound = require('call-bind/callBound');
  4. var inspect = require('object-inspect');
  5. var $TypeError = require('es-errors/type');
  6. var $WeakMap = GetIntrinsic('%WeakMap%', true);
  7. var $Map = GetIntrinsic('%Map%', true);
  8. var $weakMapGet = callBound('WeakMap.prototype.get', true);
  9. var $weakMapSet = callBound('WeakMap.prototype.set', true);
  10. var $weakMapHas = callBound('WeakMap.prototype.has', true);
  11. var $mapGet = callBound('Map.prototype.get', true);
  12. var $mapSet = callBound('Map.prototype.set', true);
  13. var $mapHas = callBound('Map.prototype.has', true);
  14. /*
  15. * This function traverses the list returning the node corresponding to the given key.
  16. *
  17. * That node is also moved to the head of the list, so that if it's accessed again we don't need to traverse the whole list. By doing so, all the recently used nodes can be accessed relatively quickly.
  18. */
  19. var listGetNode = function (list, key) { // eslint-disable-line consistent-return
  20. for (var prev = list, curr; (curr = prev.next) !== null; prev = curr) {
  21. if (curr.key === key) {
  22. prev.next = curr.next;
  23. curr.next = list.next;
  24. list.next = curr; // eslint-disable-line no-param-reassign
  25. return curr;
  26. }
  27. }
  28. };
  29. var listGet = function (objects, key) {
  30. var node = listGetNode(objects, key);
  31. return node && node.value;
  32. };
  33. var listSet = function (objects, key, value) {
  34. var node = listGetNode(objects, key);
  35. if (node) {
  36. node.value = value;
  37. } else {
  38. // Prepend the new node to the beginning of the list
  39. objects.next = { // eslint-disable-line no-param-reassign
  40. key: key,
  41. next: objects.next,
  42. value: value
  43. };
  44. }
  45. };
  46. var listHas = function (objects, key) {
  47. return !!listGetNode(objects, key);
  48. };
  49. module.exports = function getSideChannel() {
  50. var $wm;
  51. var $m;
  52. var $o;
  53. var channel = {
  54. assert: function (key) {
  55. if (!channel.has(key)) {
  56. throw new $TypeError('Side channel does not contain ' + inspect(key));
  57. }
  58. },
  59. get: function (key) { // eslint-disable-line consistent-return
  60. if ($WeakMap && key && (typeof key === 'object' || typeof key === 'function')) {
  61. if ($wm) {
  62. return $weakMapGet($wm, key);
  63. }
  64. } else if ($Map) {
  65. if ($m) {
  66. return $mapGet($m, key);
  67. }
  68. } else {
  69. if ($o) { // eslint-disable-line no-lonely-if
  70. return listGet($o, key);
  71. }
  72. }
  73. },
  74. has: function (key) {
  75. if ($WeakMap && key && (typeof key === 'object' || typeof key === 'function')) {
  76. if ($wm) {
  77. return $weakMapHas($wm, key);
  78. }
  79. } else if ($Map) {
  80. if ($m) {
  81. return $mapHas($m, key);
  82. }
  83. } else {
  84. if ($o) { // eslint-disable-line no-lonely-if
  85. return listHas($o, key);
  86. }
  87. }
  88. return false;
  89. },
  90. set: function (key, value) {
  91. if ($WeakMap && key && (typeof key === 'object' || typeof key === 'function')) {
  92. if (!$wm) {
  93. $wm = new $WeakMap();
  94. }
  95. $weakMapSet($wm, key, value);
  96. } else if ($Map) {
  97. if (!$m) {
  98. $m = new $Map();
  99. }
  100. $mapSet($m, key, value);
  101. } else {
  102. if (!$o) {
  103. // Initialize the linked list as an empty node, so that we don't have to special-case handling of the first node: we can always refer to it as (previous node).next, instead of something like (list).head
  104. $o = { key: {}, next: null };
  105. }
  106. listSet($o, key, value);
  107. }
  108. }
  109. };
  110. return channel;
  111. };