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:
1.2 KB
|
Rev | Line | |
---|
[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var $TypeError = require('es-errors/type');
|
---|
[79a0317] | 4 | var inspect = require('object-inspect');
|
---|
| 5 | var getSideChannelList = require('side-channel-list');
|
---|
| 6 | var getSideChannelMap = require('side-channel-map');
|
---|
| 7 | var getSideChannelWeakMap = require('side-channel-weakmap');
|
---|
[d565449] | 8 |
|
---|
[79a0317] | 9 | var makeChannel = getSideChannelWeakMap || getSideChannelMap || getSideChannelList;
|
---|
[d565449] | 10 |
|
---|
| 11 | /** @type {import('.')} */
|
---|
| 12 | module.exports = function getSideChannel() {
|
---|
[79a0317] | 13 | /** @typedef {ReturnType<typeof getSideChannel>} Channel */
|
---|
[d565449] | 14 |
|
---|
[79a0317] | 15 | /** @type {Channel | undefined} */ var $channelData;
|
---|
| 16 |
|
---|
| 17 | /** @type {Channel} */
|
---|
[d565449] | 18 | var channel = {
|
---|
| 19 | assert: function (key) {
|
---|
| 20 | if (!channel.has(key)) {
|
---|
| 21 | throw new $TypeError('Side channel does not contain ' + inspect(key));
|
---|
| 22 | }
|
---|
| 23 | },
|
---|
[79a0317] | 24 | 'delete': function (key) {
|
---|
| 25 | return !!$channelData && $channelData['delete'](key);
|
---|
| 26 | },
|
---|
| 27 | get: function (key) {
|
---|
| 28 | return $channelData && $channelData.get(key);
|
---|
[d565449] | 29 | },
|
---|
| 30 | has: function (key) {
|
---|
[79a0317] | 31 | return !!$channelData && $channelData.has(key);
|
---|
[d565449] | 32 | },
|
---|
| 33 | set: function (key, value) {
|
---|
[79a0317] | 34 | if (!$channelData) {
|
---|
| 35 | $channelData = makeChannel();
|
---|
[d565449] | 36 | }
|
---|
[79a0317] | 37 |
|
---|
| 38 | $channelData.set(key, value);
|
---|
[d565449] | 39 | }
|
---|
| 40 | };
|
---|
[79a0317] | 41 | // @ts-expect-error TODO: figure out why this is erroring
|
---|
[d565449] | 42 | return channel;
|
---|
| 43 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.