From 0d805a64f63ee63d6ade4a44eff0f4fa8330f64b Mon Sep 17 00:00:00 2001 From: Huang Xin Date: Fri, 31 Oct 2025 13:15:59 +0800 Subject: [PATCH] compat: polyfill AbortController/AbortSignal on older browsers (#2371) --- apps/readest-app/package.json | 1 + apps/readest-app/src/components/Providers.tsx | 1 + apps/readest-app/src/libs/document.ts | 30 ------------------ apps/readest-app/src/utils/polyfill.ts | 31 +++++++++++++++++++ pnpm-lock.yaml | 8 +++++ 5 files changed, 41 insertions(+), 30 deletions(-) create mode 100644 apps/readest-app/src/utils/polyfill.ts diff --git a/apps/readest-app/package.json b/apps/readest-app/package.json index 6d2cce7c..f13890e6 100644 --- a/apps/readest-app/package.json +++ b/apps/readest-app/package.json @@ -67,6 +67,7 @@ "@tauri-apps/plugin-shell": "~2.3.1", "@tauri-apps/plugin-updater": "^2.9.0", "@zip.js/zip.js": "^2.7.53", + "abortcontroller-polyfill": "^1.7.8", "app-store-server-api": "^0.17.1", "aws4fetch": "^1.0.20", "clsx": "^2.1.1", diff --git a/apps/readest-app/src/components/Providers.tsx b/apps/readest-app/src/components/Providers.tsx index 2ae24251..935c451a 100644 --- a/apps/readest-app/src/components/Providers.tsx +++ b/apps/readest-app/src/components/Providers.tsx @@ -1,5 +1,6 @@ 'use client'; +import '@/utils/polyfill'; import i18n from '@/i18n/i18n'; import { useEffect } from 'react'; import { IconContext } from 'react-icons'; diff --git a/apps/readest-app/src/libs/document.ts b/apps/readest-app/src/libs/document.ts index a8ab4e0c..0a132dc3 100644 --- a/apps/readest-app/src/libs/document.ts +++ b/apps/readest-app/src/libs/document.ts @@ -2,36 +2,6 @@ import { BookFormat } from '@/types/book'; import { Contributor, Identifier, LanguageMap } from '@/utils/book'; import * as epubcfi from 'foliate-js/epubcfi.js'; -// A groupBy polyfill for foliate-js -Object.groupBy ??= (iterable, callbackfn) => { - const obj = Object.create(null); - let i = 0; - for (const value of iterable) { - const key = callbackfn(value, i++); - if (key in obj) { - obj[key].push(value); - } else { - obj[key] = [value]; - } - } - return obj; -}; - -Map.groupBy ??= (iterable, callbackfn) => { - const map = new Map(); - let i = 0; - for (const value of iterable) { - const key = callbackfn(value, i++), - list = map.get(key); - if (list) { - list.push(value); - } else { - map.set(key, [value]); - } - } - return map; -}; - export const CFI = epubcfi; export type DocumentFile = File; diff --git a/apps/readest-app/src/utils/polyfill.ts b/apps/readest-app/src/utils/polyfill.ts new file mode 100644 index 00000000..7b8753c0 --- /dev/null +++ b/apps/readest-app/src/utils/polyfill.ts @@ -0,0 +1,31 @@ +import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only'; + +// A groupBy polyfill for foliate-js +Object.groupBy ??= (iterable, callbackfn) => { + const obj = Object.create(null); + let i = 0; + for (const value of iterable) { + const key = callbackfn(value, i++); + if (key in obj) { + obj[key].push(value); + } else { + obj[key] = [value]; + } + } + return obj; +}; + +Map.groupBy ??= (iterable, callbackfn) => { + const map = new Map(); + let i = 0; + for (const value of iterable) { + const key = callbackfn(value, i++), + list = map.get(key); + if (list) { + list.push(value); + } else { + map.set(key, [value]); + } + } + return map; +}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 561c9c80..04ce045b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -110,6 +110,9 @@ importers: '@zip.js/zip.js': specifier: ^2.7.53 version: 2.7.53 + abortcontroller-polyfill: + specifier: ^1.7.8 + version: 1.7.8 app-store-server-api: specifier: ^0.17.1 version: 0.17.1 @@ -3352,6 +3355,9 @@ packages: resolution: {integrity: sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==} engines: {node: '>=6.5'} + abortcontroller-polyfill@1.7.8: + resolution: {integrity: sha512-9f1iZ2uWh92VcrU9Y8x+LdM4DLj75VE0MJB8zuF1iUnroEptStw+DQ8EQPMUdfe5k+PkB1uUfDQfWbhstH8LrQ==} + accepts@2.0.0: resolution: {integrity: sha512-5cvg6CtKwfgdmVqY1WIiXKc3Q1bkRqGLi+2W/6ao+6Y7gu/RCwRuAhGEzh5B4KlszSuTLgZYuqFqo5bImjNKng==} engines: {node: '>= 0.6'} @@ -11441,6 +11447,8 @@ snapshots: dependencies: event-target-shim: 5.0.1 + abortcontroller-polyfill@1.7.8: {} + accepts@2.0.0: dependencies: mime-types: 3.0.0