source: node_modules/side-channel/index.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 3.3 KB
Line 
1'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4var callBound = require('call-bind/callBound');
5var inspect = require('object-inspect');
6
7var $TypeError = require('es-errors/type');
8var $WeakMap = GetIntrinsic('%WeakMap%', true);
9var $Map = GetIntrinsic('%Map%', true);
10
11var $weakMapGet = callBound('WeakMap.prototype.get', true);
12var $weakMapSet = callBound('WeakMap.prototype.set', true);
13var $weakMapHas = callBound('WeakMap.prototype.has', true);
14var $mapGet = callBound('Map.prototype.get', true);
15var $mapSet = callBound('Map.prototype.set', true);
16var $mapHas = callBound('Map.prototype.has', true);
17
18/*
19* This function traverses the list returning the node corresponding to the given key.
20*
21* 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.
22*/
23var listGetNode = function (list, key) { // eslint-disable-line consistent-return
24 for (var prev = list, curr; (curr = prev.next) !== null; prev = curr) {
25 if (curr.key === key) {
26 prev.next = curr.next;
27 curr.next = list.next;
28 list.next = curr; // eslint-disable-line no-param-reassign
29 return curr;
30 }
31 }
32};
33
34var listGet = function (objects, key) {
35 var node = listGetNode(objects, key);
36 return node && node.value;
37};
38var listSet = function (objects, key, value) {
39 var node = listGetNode(objects, key);
40 if (node) {
41 node.value = value;
42 } else {
43 // Prepend the new node to the beginning of the list
44 objects.next = { // eslint-disable-line no-param-reassign
45 key: key,
46 next: objects.next,
47 value: value
48 };
49 }
50};
51var listHas = function (objects, key) {
52 return !!listGetNode(objects, key);
53};
54
55module.exports = function getSideChannel() {
56 var $wm;
57 var $m;
58 var $o;
59 var channel = {
60 assert: function (key) {
61 if (!channel.has(key)) {
62 throw new $TypeError('Side channel does not contain ' + inspect(key));
63 }
64 },
65 get: function (key) { // eslint-disable-line consistent-return
66 if ($WeakMap && key && (typeof key === 'object' || typeof key === 'function')) {
67 if ($wm) {
68 return $weakMapGet($wm, key);
69 }
70 } else if ($Map) {
71 if ($m) {
72 return $mapGet($m, key);
73 }
74 } else {
75 if ($o) { // eslint-disable-line no-lonely-if
76 return listGet($o, key);
77 }
78 }
79 },
80 has: function (key) {
81 if ($WeakMap && key && (typeof key === 'object' || typeof key === 'function')) {
82 if ($wm) {
83 return $weakMapHas($wm, key);
84 }
85 } else if ($Map) {
86 if ($m) {
87 return $mapHas($m, key);
88 }
89 } else {
90 if ($o) { // eslint-disable-line no-lonely-if
91 return listHas($o, key);
92 }
93 }
94 return false;
95 },
96 set: function (key, value) {
97 if ($WeakMap && key && (typeof key === 'object' || typeof key === 'function')) {
98 if (!$wm) {
99 $wm = new $WeakMap();
100 }
101 $weakMapSet($wm, key, value);
102 } else if ($Map) {
103 if (!$m) {
104 $m = new $Map();
105 }
106 $mapSet($m, key, value);
107 } else {
108 if (!$o) {
109 // 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
110 $o = { key: {}, next: null };
111 }
112 listSet($o, key, value);
113 }
114 }
115 };
116 return channel;
117};
Note: See TracBrowser for help on using the repository browser.