index.js 3.3 KB

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