diff --git a/packages/ses/src/error/note-log-args.js b/packages/ses/src/error/note-log-args.js index 347e648236..8ad8329b32 100644 --- a/packages/ses/src/error/note-log-args.js +++ b/packages/ses/src/error/note-log-args.js @@ -96,7 +96,7 @@ const spliceOut = cell => { * bookkeeping cell has not been pushed off the end of the cache by `budget` * number of more recently referenced cells. If the key is dropped by the user, * the value will no longer be held by the cache, but the bookkeeping cell - * itself vill stay in memory until it has cycled off the end of the cache. + * itself will stay in memory. * * @template {{}} K * @template {unknown} V @@ -107,11 +107,12 @@ export const makeLRUCacheMap = budget => { if (!isSafeInteger(budget) || budget < 0) { throw new TypeError('budget must be a safe non-negative integer number'); } - /** @type {WeakMap | undefined>>} */ + /** @typedef {DoublyLinkedCell | undefined>} LRUCacheCell */ + /** @type {WeakMap} */ const keyToCell = new WeakMap(); let size = 0; // `size` must remain <= `budget` // As a sigil, `head` uniquely is not in the `keyToCell` map. - /** @type {DoublyLinkedCell | undefined>} */ + /** @type {LRUCacheCell>} */ const head = makeSelfCell(undefined); const touchCell = key => { @@ -136,7 +137,7 @@ export const makeLRUCacheMap = budget => { */ // TODO Change to the following line, once our tools don't choke on `?.`. // See https://github.com/endojs/endo/issues/1514 - // const get = key => touchCell(key)?.keyValue.get(key); + // const get = key => touchCell(key)?.data?.get(key); const get = key => { const cell = touchCell(key); return cell && cell.data && cell.data.get(key); @@ -149,7 +150,7 @@ export const makeLRUCacheMap = budget => { */ const set = (key, value) => { if (budget >= 1) { - /** @type {DoublyLinkedCell | undefined> | undefined} */ + /** @type {LRUCacheCell | undefined} */ let cell = touchCell(key); if (cell === undefined) { if (size >= budget) { @@ -159,13 +160,16 @@ export const makeLRUCacheMap = budget => { size -= 1; } size += 1; - cell = makeSelfCell(new WeakMap()); - keyToCell.set(key, cell); + cell = makeSelfCell(undefined); spliceAfter(head, cell); // start most recently used } if (!cell.data) { - throw TypeError('Internal error; expected cell.data'); + // Either a new cell or a condemned cell. Add its data. + cell.data = new WeakMap(); + // Advertise the cell for this key. + keyToCell.set(key, cell); } + // Update the data. cell.data.set(key, value); } // eslint-disable-next-line no-use-before-define