diff --git a/app/encoders-decoders/base64/page.tsx b/app/encoders-decoders/base64/page.tsx index 881fe15..78674f6 100644 --- a/app/encoders-decoders/base64/page.tsx +++ b/app/encoders-decoders/base64/page.tsx @@ -1,9 +1,9 @@ "use client"; import { useCallback, useState } from "react"; -import { decode, encode, isValid } from "js-base64"; import { toolGroups } from "@/config/tools"; +import { decode, encode } from "@/lib/base64"; import { Textarea, TextareaProps } from "@/components/ui/textarea"; import * as Button from "@/components/buttons"; import { ControlMenu } from "@/components/control-menu"; @@ -24,10 +24,10 @@ export default function Page() { }, []); const setFormByEncoded = useCallback((text: string) => { - const newDecoded = decode(text); + const newDecoded = decode(text) ?? ""; setForm({ - decoded: isValid(text) && !newDecoded.includes("�") ? newDecoded : "", + decoded: newDecoded.includes("�") ? "" : newDecoded, encoded: text, }); }, []); diff --git a/lib/base64.ts b/lib/base64.ts new file mode 100644 index 0000000..170aa0d --- /dev/null +++ b/lib/base64.ts @@ -0,0 +1,14 @@ +export const encode = (s: string) => { + const bytes = new TextEncoder().encode(s); + return btoa(String.fromCharCode(...bytes)); +}; + +export const decode = (base64: string) => { + try { + const binString = atob(base64); + const bytes = Uint8Array.from(binString, c => c.charCodeAt(0)); + return new TextDecoder().decode(bytes); + } catch { + return undefined; + } +}; diff --git a/package.json b/package.json index bdabebf..125d760 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,6 @@ "fp-ts": "^2.16.0", "fuse.js": "^6.6.2", "html-escaper": "^3.0.3", - "js-base64": "^3.7.5", "js-yaml": "^4.1.0", "jwt-decode": "^3.1.2", "lucide-react": "^0.221.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 95701e7..5050abc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -43,9 +43,6 @@ dependencies: html-escaper: specifier: ^3.0.3 version: 3.0.3 - js-base64: - specifier: ^3.7.5 - version: 3.7.5 js-yaml: specifier: ^4.1.0 version: 4.1.0 @@ -3645,10 +3642,6 @@ packages: resolution: {integrity: sha512-QAdOptna2NYiSSpv0O/BwoHBSmz4YhpzJHyi+fnMRTXFjp7B8i/YG5Z8IfusxB1ufjcD2Sre1F3R+nX3fvy7gg==} hasBin: true - /js-base64@3.7.5: - resolution: {integrity: sha512-3MEt5DTINKqfScXKfJFrRbxkrnk2AxPWGBL/ycjz4dK8iqiSJ06UxD8jh8xuh6p10TX4t2+7FsBYVxxQbMg+qA==} - dev: false - /js-tokens@4.0.0: resolution: {integrity: sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==}