From 2549e5154ceaa8246b328f204abf8d63a68d0c25 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Wed, 26 Jun 2024 06:24:58 -0700 Subject: [PATCH] perf: optimize Buffer.from("base64") for forgiving-base64 conforming input (#24346) --- ext/node/polyfills/internal_binding/_utils.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ext/node/polyfills/internal_binding/_utils.ts b/ext/node/polyfills/internal_binding/_utils.ts index a773f0a9c3..74dc3cbcd6 100644 --- a/ext/node/polyfills/internal_binding/_utils.ts +++ b/ext/node/polyfills/internal_binding/_utils.ts @@ -18,9 +18,13 @@ export function asciiToBytes(str: string) { } export function base64ToBytes(str: string) { - str = base64clean(str); - str = str.replaceAll("-", "+").replaceAll("_", "/"); - return forgivingBase64Decode(str); + try { + return forgivingBase64Decode(str); + } catch { + str = base64clean(str); + str = str.replaceAll("-", "+").replaceAll("_", "/"); + return forgivingBase64Decode(str); + } } const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g;