fix: polyfill Map.prototype.getOrInsertComputed to stop PDF.js render crash

Fixes #662
This commit is contained in:
Matt Van Horn committed 2026-07-21 00:42:50 -07:00
1 parent 21c924a3e6
commit 115bc70ad1
3 files changed
+169

No files matched your search

+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 {};