Merge pull request #795 from mvanhorn/fix/662-map-upsert-polyfill

fix: polyfill Map.prototype.getOrInsertComputed to stop PDF.js render crash
This commit is contained in:
Alam
2026-08-22 15:58:31 +05:30
committed by GitHub
3 changed files with 169 additions and 0 deletions
+1
View File
@@ -1,3 +1,4 @@
import './utils/map-upsert-polyfill.js';
import { categories } from './config/tools.js';
import { dom, switchView, hideAlert } from './ui.js';
import { ShortcutsManager } from './logic/shortcuts.js';
+47
View File
@@ -0,0 +1,47 @@
type MapPrototypeWithUpsert = typeof Map.prototype & {
getOrInsert?: (key: unknown, value: unknown) => unknown;
getOrInsertComputed?: (
key: unknown,
callback: (key: unknown) => unknown
) => unknown;
};
const mapPrototype = Map.prototype as MapPrototypeWithUpsert;
const mapHas = Map.prototype.has;
const mapGet = Map.prototype.get;
const mapSet = Map.prototype.set;
if (typeof mapPrototype.getOrInsert !== 'function') {
Object.defineProperty(mapPrototype, 'getOrInsert', {
configurable: true,
writable: true,
value: function getOrInsert(key: unknown, value: unknown) {
if (mapHas.call(this, key)) return mapGet.call(this, key);
mapSet.call(this, key, value);
return value;
},
});
}
if (typeof mapPrototype.getOrInsertComputed !== 'function') {
Object.defineProperty(mapPrototype, 'getOrInsertComputed', {
configurable: true,
writable: true,
value: function getOrInsertComputed(
key: unknown,
callback: (key: unknown) => unknown
) {
const hasKey = mapHas.call(this, key);
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function');
}
if (hasKey) return mapGet.call(this, key);
const value = callback(key);
mapSet.call(this, key, value);
return value;
},
});
}
export {};