Node-Red configuration
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.

index.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. /** @type {import('.').listGetNode} */
  20. var listGetNode = function (list, key) { // eslint-disable-line consistent-return
  21. /** @type {typeof list | NonNullable<(typeof list)['next']>} */
  22. var prev = list;
  23. /** @type {(typeof list)['next']} */
  24. var curr;
  25. for (; (curr = prev.next) !== null; prev = curr) {
  26. if (curr.key === key) {
  27. prev.next = curr.next;
  28. // eslint-disable-next-line no-extra-parens
  29. curr.next = /** @type {NonNullable<typeof list.next>} */ (list.next);
  30. list.next = curr; // eslint-disable-line no-param-reassign
  31. return curr;
  32. }
  33. }
  34. };
  35. /** @type {import('.').listGet} */
  36. var listGet = function (objects, key) {
  37. var node = listGetNode(objects, key);
  38. return node && node.value;
  39. };
  40. /** @type {import('.').listSet} */
  41. var listSet = function (objects, key, value) {
  42. var node = listGetNode(objects, key);
  43. if (node) {
  44. node.value = value;
  45. } else {
  46. // Prepend the new node to the beginning of the list
  47. objects.next = /** @type {import('.').ListNode<typeof value>} */ ({ // eslint-disable-line no-param-reassign, no-extra-parens
  48. key: key,
  49. next: objects.next,
  50. value: value
  51. });
  52. }
  53. };
  54. /** @type {import('.').listHas} */
  55. var listHas = function (objects, key) {
  56. return !!listGetNode(objects, key);
  57. };
  58. /** @type {import('.')} */
  59. module.exports = function getSideChannel() {
  60. /** @type {WeakMap<object, unknown>} */ var $wm;
  61. /** @type {Map<object, unknown>} */ var $m;
  62. /** @type {import('.').RootNode<unknown>} */ var $o;
  63. /** @type {import('.').Channel} */
  64. var channel = {
  65. assert: function (key) {
  66. if (!channel.has(key)) {
  67. throw new $TypeError('Side channel does not contain ' + inspect(key));
  68. }
  69. },
  70. get: function (key) { // eslint-disable-line consistent-return
  71. if ($WeakMap && key && (typeof key === 'object' || typeof key === 'function')) {
  72. if ($wm) {
  73. return $weakMapGet($wm, key);
  74. }
  75. } else if ($Map) {
  76. if ($m) {
  77. return $mapGet($m, key);
  78. }
  79. } else {
  80. if ($o) { // eslint-disable-line no-lonely-if
  81. return listGet($o, key);
  82. }
  83. }
  84. },
  85. has: function (key) {
  86. if ($WeakMap && key && (typeof key === 'object' || typeof key === 'function')) {
  87. if ($wm) {
  88. return $weakMapHas($wm, key);
  89. }
  90. } else if ($Map) {
  91. if ($m) {
  92. return $mapHas($m, key);
  93. }
  94. } else {
  95. if ($o) { // eslint-disable-line no-lonely-if
  96. return listHas($o, key);
  97. }
  98. }
  99. return false;
  100. },
  101. set: function (key, value) {
  102. if ($WeakMap && key && (typeof key === 'object' || typeof key === 'function')) {
  103. if (!$wm) {
  104. $wm = new $WeakMap();
  105. }
  106. $weakMapSet($wm, key, value);
  107. } else if ($Map) {
  108. if (!$m) {
  109. $m = new $Map();
  110. }
  111. $mapSet($m, key, value);
  112. } else {
  113. if (!$o) {
  114. // 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
  115. $o = { key: {}, next: null };
  116. }
  117. listSet($o, key, value);
  118. }
  119. }
  120. };
  121. return channel;
  122. };