Skip to content

Commit

Permalink
Fix: introduce findLastIndex polyfill (#2492)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtripg6666tdr authored Jul 11, 2024
1 parent 12b6ae0 commit cebaf1c
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,55 @@ import { getLogger } from "./logger";

const logger = getLogger("Polyfill");

let polyfillCount = 0;

if(typeof global.fetch === "undefined"){
logger.warn("Native fetch function is not defined.");
logger.warn("Installing a fetch polyfill.");
logger.warn("We strongly recommend you upgrading Node.js to v18 or higher.");

polyfillCount++;

global.fetch = require("undici").fetch;
}

if(typeof global.structuredClone === "undefined"){
logger.warn("Native structuredClone function is not defined.");
logger.warn("Installing a structuredClone polyfill.");
logger.warn("We strongly recommend you upgrading Node.js to v18 or higher.");

polyfillCount++;

global.structuredClone = function structuredClone<T>(value: T){
return JSON.parse(JSON.stringify(value));
};
}

if(typeof global.ReadableStream === "undefined"){
logger.warn("Native ReadableStream class is not defined.");
logger.warn("Native ReadableStream class is not globally defined.");
logger.warn("Setting up ReadableStream object imported from stream/web standard module.");

polyfillCount++;

global.ReadableStream = require("stream/web").ReadableStream;
}

if(typeof Array.prototype.findLastIndex === "undefined"){
logger.warn("Native Array.prototype.findLastIndex function is not defined.");
logger.warn("Installing a findLastIndex polyfill.");

polyfillCount++;

Array.prototype.findLastIndex = function findLastIndex<T>(callback: (value: T, index: number, array: T[]) => boolean, thisArg?: any): number{
for(let i = this.length - 1; i >= 0; i--){
if(callback.call(thisArg, this[i], i, this)){
return i;
}
}

return -1;
};
}

if(polyfillCount > 0){
logger.warn(`Installed ${polyfillCount} polyfill(s), which means Node.js may be stale.`);
logger.warn("We strongly recommend you upgrading Node.js to v18 at least or higher.");
}

0 comments on commit cebaf1c

Please sign in to comment.