source: imaps-frontend/node_modules/side-channel-weakmap/index.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.6 KB
Line 
1'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4var callBound = require('call-bound');
5var inspect = require('object-inspect');
6var getSideChannelMap = require('side-channel-map');
7
8var $TypeError = require('es-errors/type');
9var $WeakMap = GetIntrinsic('%WeakMap%', true);
10
11/** @type {<K extends object, V>(thisArg: WeakMap<K, V>, key: K) => V} */
12var $weakMapGet = callBound('WeakMap.prototype.get', true);
13/** @type {<K extends object, V>(thisArg: WeakMap<K, V>, key: K, value: V) => void} */
14var $weakMapSet = callBound('WeakMap.prototype.set', true);
15/** @type {<K extends object, V>(thisArg: WeakMap<K, V>, key: K) => boolean} */
16var $weakMapHas = callBound('WeakMap.prototype.has', true);
17/** @type {<K extends object, V>(thisArg: WeakMap<K, V>, key: K) => boolean} */
18var $weakMapDelete = callBound('WeakMap.prototype.delete', true);
19
20/** @type {import('.')} */
21module.exports = $WeakMap
22 ? /** @type {Exclude<import('.'), false>} */ function getSideChannelWeakMap() {
23 /** @typedef {ReturnType<typeof getSideChannelWeakMap>} Channel */
24 /** @typedef {Parameters<Channel['get']>[0]} K */
25 /** @typedef {Parameters<Channel['set']>[1]} V */
26
27 /** @type {WeakMap<K & object, V> | undefined} */ var $wm;
28 /** @type {Channel | undefined} */ var $m;
29
30 /** @type {Channel} */
31 var channel = {
32 assert: function (key) {
33 if (!channel.has(key)) {
34 throw new $TypeError('Side channel does not contain ' + inspect(key));
35 }
36 },
37 'delete': function (key) {
38 if ($WeakMap && key && (typeof key === 'object' || typeof key === 'function')) {
39 if ($wm) {
40 return $weakMapDelete($wm, key);
41 }
42 } else if (getSideChannelMap) {
43 if ($m) {
44 return $m['delete'](key);
45 }
46 }
47 return false;
48 },
49 get: function (key) {
50 if ($WeakMap && key && (typeof key === 'object' || typeof key === 'function')) {
51 if ($wm) {
52 return $weakMapGet($wm, key);
53 }
54 }
55 return $m && $m.get(key);
56 },
57 has: function (key) {
58 if ($WeakMap && key && (typeof key === 'object' || typeof key === 'function')) {
59 if ($wm) {
60 return $weakMapHas($wm, key);
61 }
62 }
63 return !!$m && $m.has(key);
64 },
65 set: function (key, value) {
66 if ($WeakMap && key && (typeof key === 'object' || typeof key === 'function')) {
67 if (!$wm) {
68 $wm = new $WeakMap();
69 }
70 $weakMapSet($wm, key, value);
71 } else if (getSideChannelMap) {
72 if (!$m) {
73 $m = getSideChannelMap();
74 }
75 // eslint-disable-next-line no-extra-parens
76 /** @type {NonNullable<typeof $m>} */ ($m).set(key, value);
77 }
78 }
79 };
80
81 // @ts-expect-error TODO: figure out why this is erroring
82 return channel;
83 }
84 : getSideChannelMap;
Note: See TracBrowser for help on using the repository browser.