Skip to content

Commit

Permalink
refactor: make side-channel smaller
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Aug 29, 2023
1 parent 5f58504 commit f23e4c3
Showing 1 changed file with 31 additions and 41 deletions.
72 changes: 31 additions & 41 deletions create.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -430,51 +430,41 @@ module.exports = function isGeneratorFunction(fn) {
return Object.getPrototypeOf(fn) === GeneratorFunction;
}`],
['side-channel', `module.exports = () => {
let $wm;
let $m;
let $wm, $m;
const channel = {
assert(key) {
if (!channel.has(key)) {
throw new TypeError('Side channel does not contain the given key');
}
},
get(key) {
if (key && (typeof key === 'object' || typeof key === 'function')) {
if ($wm) {
return $wm.get(key);
}
} else if ($m) {
return $m.get(key);
}
return undefined;
},
has(key) {
if (key && (typeof key === 'object' || typeof key === 'function')) {
if ($wm) {
return $wm.has(key);
}
} else if ($m) {
return $m.has(key);
}
return false;
},
set(key, value) {
if (key && (typeof key === 'object' || typeof key === 'function')) {
if (!$wm) {
$wm = new WeakMap();
}
$wm.set(key, value);
} else {
if (!$m) {
$m = new Map();
}
$m.set(key, value);
const get = (key) => {
if (key && (typeof key === 'object' || typeof key === 'function')) {
if ($wm) return $wm.get(key);
} else if ($m) {
return $m.get(key);
}
return undefined;
};
const set = (key, value) => {
if (key && (typeof key === 'object' || typeof key === 'function')) {
if (!$wm) $wm = new WeakMap();
$wm.set(key, value);
} else {
if (!$m) $m = new Map();
$m.set(key, value);
}
};
const has = (key) => {
if (key && (typeof key === 'object' || typeof key === 'function')) {
if ($wm) {
return $wm.has(key);
}
} else if ($m) {
return $m.has(key);
}
return false;
};
const assert = (key) => {
if (!has(key)) {
throw new TypeError('Side channel does not contain the given key');
}
};
// eslint-disable-next-line @fluffyfox/no-redundant-variable -- self-reference
return channel;
return { get, set, has, assert };
}`]
]);

Expand Down

0 comments on commit f23e4c3

Please sign in to comment.