1 | 'use strict';
|
---|
2 |
|
---|
3 | var GetIntrinsic = require('get-intrinsic');
|
---|
4 | var callBound = require('call-bound');
|
---|
5 | var inspect = require('object-inspect');
|
---|
6 |
|
---|
7 | var $TypeError = require('es-errors/type');
|
---|
8 | var $Map = GetIntrinsic('%Map%', true);
|
---|
9 |
|
---|
10 | /** @type {<K, V>(thisArg: Map<K, V>, key: K) => V} */
|
---|
11 | var $mapGet = callBound('Map.prototype.get', true);
|
---|
12 | /** @type {<K, V>(thisArg: Map<K, V>, key: K, value: V) => void} */
|
---|
13 | var $mapSet = callBound('Map.prototype.set', true);
|
---|
14 | /** @type {<K, V>(thisArg: Map<K, V>, key: K) => boolean} */
|
---|
15 | var $mapHas = callBound('Map.prototype.has', true);
|
---|
16 | /** @type {<K, V>(thisArg: Map<K, V>, key: K) => boolean} */
|
---|
17 | var $mapDelete = callBound('Map.prototype.delete', true);
|
---|
18 | /** @type {<K, V>(thisArg: Map<K, V>) => number} */
|
---|
19 | var $mapSize = callBound('Map.prototype.size', true);
|
---|
20 |
|
---|
21 | /** @type {import('.')} */
|
---|
22 | module.exports = !!$Map && /** @type {Exclude<import('.'), false>} */ function getSideChannelMap() {
|
---|
23 | /** @typedef {ReturnType<typeof getSideChannelMap>} Channel */
|
---|
24 | /** @typedef {Parameters<Channel['get']>[0]} K */
|
---|
25 | /** @typedef {Parameters<Channel['set']>[1]} V */
|
---|
26 |
|
---|
27 | /** @type {Map<K, V> | undefined} */ var $m;
|
---|
28 |
|
---|
29 | /** @type {Channel} */
|
---|
30 | var channel = {
|
---|
31 | assert: function (key) {
|
---|
32 | if (!channel.has(key)) {
|
---|
33 | throw new $TypeError('Side channel does not contain ' + inspect(key));
|
---|
34 | }
|
---|
35 | },
|
---|
36 | 'delete': function (key) {
|
---|
37 | if ($m) {
|
---|
38 | var result = $mapDelete($m, key);
|
---|
39 | if ($mapSize($m) === 0) {
|
---|
40 | $m = void undefined;
|
---|
41 | }
|
---|
42 | return result;
|
---|
43 | }
|
---|
44 | return false;
|
---|
45 | },
|
---|
46 | get: function (key) { // eslint-disable-line consistent-return
|
---|
47 | if ($m) {
|
---|
48 | return $mapGet($m, key);
|
---|
49 | }
|
---|
50 | },
|
---|
51 | has: function (key) {
|
---|
52 | if ($m) {
|
---|
53 | return $mapHas($m, key);
|
---|
54 | }
|
---|
55 | return false;
|
---|
56 | },
|
---|
57 | set: function (key, value) {
|
---|
58 | if (!$m) {
|
---|
59 | // @ts-expect-error TS can't handle narrowing a variable inside a closure
|
---|
60 | $m = new $Map();
|
---|
61 | }
|
---|
62 | $mapSet($m, key, value);
|
---|
63 | }
|
---|
64 | };
|
---|
65 |
|
---|
66 | // @ts-expect-error TODO: figure out why TS is erroring here
|
---|
67 | return channel;
|
---|
68 | };
|
---|